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"
10 static void axgbe_an37_clear_interrupts(struct axgbe_port *pdata)
14 reg = XMDIO_READ(pdata, MDIO_MMD_VEND2, MDIO_VEND2_AN_STAT);
15 reg &= ~AXGBE_AN_CL37_INT_MASK;
16 XMDIO_WRITE(pdata, MDIO_MMD_VEND2, MDIO_VEND2_AN_STAT, reg);
19 static void axgbe_an37_disable_interrupts(struct axgbe_port *pdata)
23 reg = XMDIO_READ(pdata, MDIO_MMD_VEND2, MDIO_VEND2_AN_CTRL);
24 reg &= ~AXGBE_AN_CL37_INT_MASK;
25 XMDIO_WRITE(pdata, MDIO_MMD_VEND2, MDIO_VEND2_AN_CTRL, reg);
27 reg = XMDIO_READ(pdata, MDIO_MMD_PCS, MDIO_PCS_DIG_CTRL);
28 reg &= ~AXGBE_PCS_CL37_BP;
29 XMDIO_WRITE(pdata, MDIO_MMD_PCS, MDIO_PCS_DIG_CTRL, reg);
32 static void axgbe_an73_clear_interrupts(struct axgbe_port *pdata)
34 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, 0);
37 static void axgbe_an73_disable_interrupts(struct axgbe_port *pdata)
39 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK, 0);
42 static void axgbe_an73_enable_interrupts(struct axgbe_port *pdata)
44 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INTMASK,
45 AXGBE_AN_CL73_INT_MASK);
48 static void axgbe_an_enable_interrupts(struct axgbe_port *pdata)
50 switch (pdata->an_mode) {
51 case AXGBE_AN_MODE_CL73:
52 case AXGBE_AN_MODE_CL73_REDRV:
53 axgbe_an73_enable_interrupts(pdata);
55 case AXGBE_AN_MODE_CL37:
56 case AXGBE_AN_MODE_CL37_SGMII:
57 PMD_DRV_LOG(ERR, "Unsupported AN_MOD_37\n");
64 static void axgbe_an_clear_interrupts_all(struct axgbe_port *pdata)
66 axgbe_an73_clear_interrupts(pdata);
67 axgbe_an37_clear_interrupts(pdata);
70 static void axgbe_an73_enable_kr_training(struct axgbe_port *pdata)
74 reg = XMDIO_READ(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
76 reg |= AXGBE_KR_TRAINING_ENABLE;
77 XMDIO_WRITE(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, reg);
80 static void axgbe_an73_disable_kr_training(struct axgbe_port *pdata)
84 reg = XMDIO_READ(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
86 reg &= ~AXGBE_KR_TRAINING_ENABLE;
87 XMDIO_WRITE(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL, reg);
90 static void axgbe_kr_mode(struct axgbe_port *pdata)
92 /* Enable KR training */
93 axgbe_an73_enable_kr_training(pdata);
95 /* Set MAC to 10G speed */
96 pdata->hw_if.set_speed(pdata, SPEED_10000);
98 /* Call PHY implementation support to complete rate change */
99 pdata->phy_if.phy_impl.set_mode(pdata, AXGBE_MODE_KR);
102 static void axgbe_kx_2500_mode(struct axgbe_port *pdata)
104 /* Disable KR training */
105 axgbe_an73_disable_kr_training(pdata);
107 /* Set MAC to 2.5G speed */
108 pdata->hw_if.set_speed(pdata, SPEED_2500);
110 /* Call PHY implementation support to complete rate change */
111 pdata->phy_if.phy_impl.set_mode(pdata, AXGBE_MODE_KX_2500);
114 static void axgbe_kx_1000_mode(struct axgbe_port *pdata)
116 /* Disable KR training */
117 axgbe_an73_disable_kr_training(pdata);
119 /* Set MAC to 1G speed */
120 pdata->hw_if.set_speed(pdata, SPEED_1000);
122 /* Call PHY implementation support to complete rate change */
123 pdata->phy_if.phy_impl.set_mode(pdata, AXGBE_MODE_KX_1000);
126 static void axgbe_sfi_mode(struct axgbe_port *pdata)
128 /* If a KR re-driver is present, change to KR mode instead */
130 return axgbe_kr_mode(pdata);
132 /* Disable KR training */
133 axgbe_an73_disable_kr_training(pdata);
135 /* Set MAC to 10G speed */
136 pdata->hw_if.set_speed(pdata, SPEED_10000);
138 /* Call PHY implementation support to complete rate change */
139 pdata->phy_if.phy_impl.set_mode(pdata, AXGBE_MODE_SFI);
142 static void axgbe_x_mode(struct axgbe_port *pdata)
144 /* Disable KR training */
145 axgbe_an73_disable_kr_training(pdata);
147 /* Set MAC to 1G speed */
148 pdata->hw_if.set_speed(pdata, SPEED_1000);
150 /* Call PHY implementation support to complete rate change */
151 pdata->phy_if.phy_impl.set_mode(pdata, AXGBE_MODE_X);
154 static void axgbe_sgmii_1000_mode(struct axgbe_port *pdata)
156 /* Disable KR training */
157 axgbe_an73_disable_kr_training(pdata);
159 /* Set MAC to 1G speed */
160 pdata->hw_if.set_speed(pdata, SPEED_1000);
162 /* Call PHY implementation support to complete rate change */
163 pdata->phy_if.phy_impl.set_mode(pdata, AXGBE_MODE_SGMII_1000);
166 static void axgbe_sgmii_100_mode(struct axgbe_port *pdata)
168 /* Disable KR training */
169 axgbe_an73_disable_kr_training(pdata);
171 /* Set MAC to 1G speed */
172 pdata->hw_if.set_speed(pdata, SPEED_1000);
174 /* Call PHY implementation support to complete rate change */
175 pdata->phy_if.phy_impl.set_mode(pdata, AXGBE_MODE_SGMII_100);
178 static enum axgbe_mode axgbe_cur_mode(struct axgbe_port *pdata)
180 return pdata->phy_if.phy_impl.cur_mode(pdata);
183 static bool axgbe_in_kr_mode(struct axgbe_port *pdata)
185 return axgbe_cur_mode(pdata) == AXGBE_MODE_KR;
188 static void axgbe_change_mode(struct axgbe_port *pdata,
189 enum axgbe_mode mode)
192 case AXGBE_MODE_KX_1000:
193 axgbe_kx_1000_mode(pdata);
195 case AXGBE_MODE_KX_2500:
196 axgbe_kx_2500_mode(pdata);
199 axgbe_kr_mode(pdata);
201 case AXGBE_MODE_SGMII_100:
202 axgbe_sgmii_100_mode(pdata);
204 case AXGBE_MODE_SGMII_1000:
205 axgbe_sgmii_1000_mode(pdata);
211 axgbe_sfi_mode(pdata);
213 case AXGBE_MODE_UNKNOWN:
216 PMD_DRV_LOG(ERR, "invalid operation mode requested (%u)\n", mode);
220 static void axgbe_switch_mode(struct axgbe_port *pdata)
222 axgbe_change_mode(pdata, pdata->phy_if.phy_impl.switch_mode(pdata));
225 static void axgbe_set_mode(struct axgbe_port *pdata,
226 enum axgbe_mode mode)
228 if (mode == axgbe_cur_mode(pdata))
231 axgbe_change_mode(pdata, mode);
234 static bool axgbe_use_mode(struct axgbe_port *pdata,
235 enum axgbe_mode mode)
237 return pdata->phy_if.phy_impl.use_mode(pdata, mode);
240 static void axgbe_an37_set(struct axgbe_port *pdata, bool enable,
245 reg = XMDIO_READ(pdata, MDIO_MMD_VEND2, MDIO_CTRL1);
246 reg &= ~MDIO_VEND2_CTRL1_AN_ENABLE;
249 reg |= MDIO_VEND2_CTRL1_AN_ENABLE;
252 reg |= MDIO_VEND2_CTRL1_AN_RESTART;
254 XMDIO_WRITE(pdata, MDIO_MMD_VEND2, MDIO_CTRL1, reg);
257 static void axgbe_an37_disable(struct axgbe_port *pdata)
259 axgbe_an37_set(pdata, false, false);
260 axgbe_an37_disable_interrupts(pdata);
263 static void axgbe_an73_set(struct axgbe_port *pdata, bool enable,
268 reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_CTRL1);
269 reg &= ~MDIO_AN_CTRL1_ENABLE;
272 reg |= MDIO_AN_CTRL1_ENABLE;
275 reg |= MDIO_AN_CTRL1_RESTART;
277 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_CTRL1, reg);
280 static void axgbe_an73_restart(struct axgbe_port *pdata)
282 axgbe_an73_enable_interrupts(pdata);
283 axgbe_an73_set(pdata, true, true);
286 static void axgbe_an73_disable(struct axgbe_port *pdata)
288 axgbe_an73_set(pdata, false, false);
289 axgbe_an73_disable_interrupts(pdata);
293 static void axgbe_an_restart(struct axgbe_port *pdata)
295 if (pdata->phy_if.phy_impl.an_pre)
296 pdata->phy_if.phy_impl.an_pre(pdata);
298 switch (pdata->an_mode) {
299 case AXGBE_AN_MODE_CL73:
300 case AXGBE_AN_MODE_CL73_REDRV:
301 axgbe_an73_restart(pdata);
303 case AXGBE_AN_MODE_CL37:
304 case AXGBE_AN_MODE_CL37_SGMII:
305 PMD_DRV_LOG(ERR, "Unsupported AN_MODE_CL37\n");
312 static void axgbe_an_disable(struct axgbe_port *pdata)
314 if (pdata->phy_if.phy_impl.an_post)
315 pdata->phy_if.phy_impl.an_post(pdata);
317 switch (pdata->an_mode) {
318 case AXGBE_AN_MODE_CL73:
319 case AXGBE_AN_MODE_CL73_REDRV:
320 axgbe_an73_disable(pdata);
322 case AXGBE_AN_MODE_CL37:
323 case AXGBE_AN_MODE_CL37_SGMII:
324 PMD_DRV_LOG(ERR, "Unsupported AN_MODE_CL37\n");
331 static void axgbe_an_disable_all(struct axgbe_port *pdata)
333 axgbe_an73_disable(pdata);
334 axgbe_an37_disable(pdata);
337 static enum axgbe_an axgbe_an73_tx_training(struct axgbe_port *pdata,
338 enum axgbe_rx *state)
340 unsigned int ad_reg, lp_reg, reg;
342 *state = AXGBE_RX_COMPLETE;
344 /* If we're not in KR mode then we're done */
345 if (!axgbe_in_kr_mode(pdata))
346 return AXGBE_AN_PAGE_RECEIVED;
348 /* Enable/Disable FEC */
349 ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
350 lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 2);
352 reg = XMDIO_READ(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FECCTRL);
353 reg &= ~(MDIO_PMA_10GBR_FECABLE_ABLE | MDIO_PMA_10GBR_FECABLE_ERRABLE);
354 if ((ad_reg & 0xc000) && (lp_reg & 0xc000))
355 reg |= pdata->fec_ability;
356 XMDIO_WRITE(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_FECCTRL, reg);
358 /* Start KR training */
359 reg = XMDIO_READ(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL);
360 if (reg & AXGBE_KR_TRAINING_ENABLE) {
361 if (pdata->phy_if.phy_impl.kr_training_pre)
362 pdata->phy_if.phy_impl.kr_training_pre(pdata);
364 reg |= AXGBE_KR_TRAINING_START;
365 XMDIO_WRITE(pdata, MDIO_MMD_PMAPMD, MDIO_PMA_10GBR_PMD_CTRL,
368 if (pdata->phy_if.phy_impl.kr_training_post)
369 pdata->phy_if.phy_impl.kr_training_post(pdata);
372 return AXGBE_AN_PAGE_RECEIVED;
375 static enum axgbe_an axgbe_an73_tx_xnp(struct axgbe_port *pdata,
376 enum axgbe_rx *state)
380 *state = AXGBE_RX_XNP;
382 msg = AXGBE_XNP_MCF_NULL_MESSAGE;
383 msg |= AXGBE_XNP_MP_FORMATTED;
385 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_XNP + 2, 0);
386 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_XNP + 1, 0);
387 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_XNP, msg);
389 return AXGBE_AN_PAGE_RECEIVED;
392 static enum axgbe_an axgbe_an73_rx_bpa(struct axgbe_port *pdata,
393 enum axgbe_rx *state)
395 unsigned int link_support;
396 unsigned int reg, ad_reg, lp_reg;
398 /* Read Base Ability register 2 first */
399 reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA + 1);
401 /* Check for a supported mode, otherwise restart in a different one */
402 link_support = axgbe_in_kr_mode(pdata) ? 0x80 : 0x20;
403 if (!(reg & link_support))
404 return AXGBE_AN_INCOMPAT_LINK;
406 /* Check Extended Next Page support */
407 ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
408 lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPA);
410 return ((ad_reg & AXGBE_XNP_NP_EXCHANGE) ||
411 (lp_reg & AXGBE_XNP_NP_EXCHANGE))
412 ? axgbe_an73_tx_xnp(pdata, state)
413 : axgbe_an73_tx_training(pdata, state);
416 static enum axgbe_an axgbe_an73_rx_xnp(struct axgbe_port *pdata,
417 enum axgbe_rx *state)
419 unsigned int ad_reg, lp_reg;
421 /* Check Extended Next Page support */
422 ad_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_XNP);
423 lp_reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_LPX);
425 return ((ad_reg & AXGBE_XNP_NP_EXCHANGE) ||
426 (lp_reg & AXGBE_XNP_NP_EXCHANGE))
427 ? axgbe_an73_tx_xnp(pdata, state)
428 : axgbe_an73_tx_training(pdata, state);
431 static enum axgbe_an axgbe_an73_page_received(struct axgbe_port *pdata)
433 enum axgbe_rx *state;
434 unsigned long an_timeout;
438 if (!pdata->an_start) {
439 pdata->an_start = rte_get_timer_cycles();
441 an_timeout = pdata->an_start +
442 msecs_to_timer_cycles(AXGBE_AN_MS_TIMEOUT);
443 ticks = rte_get_timer_cycles();
444 if (time_after(ticks, an_timeout)) {
445 /* Auto-negotiation timed out, reset state */
446 pdata->kr_state = AXGBE_RX_BPA;
447 pdata->kx_state = AXGBE_RX_BPA;
449 pdata->an_start = rte_get_timer_cycles();
453 state = axgbe_in_kr_mode(pdata) ? &pdata->kr_state
458 ret = axgbe_an73_rx_bpa(pdata, state);
461 ret = axgbe_an73_rx_xnp(pdata, state);
464 ret = AXGBE_AN_ERROR;
470 static enum axgbe_an axgbe_an73_incompat_link(struct axgbe_port *pdata)
472 /* Be sure we aren't looping trying to negotiate */
473 if (axgbe_in_kr_mode(pdata)) {
474 pdata->kr_state = AXGBE_RX_ERROR;
476 if (!(pdata->phy.advertising & ADVERTISED_1000baseKX_Full) &&
477 !(pdata->phy.advertising & ADVERTISED_2500baseX_Full))
478 return AXGBE_AN_NO_LINK;
480 if (pdata->kx_state != AXGBE_RX_BPA)
481 return AXGBE_AN_NO_LINK;
483 pdata->kx_state = AXGBE_RX_ERROR;
485 if (!(pdata->phy.advertising & ADVERTISED_10000baseKR_Full))
486 return AXGBE_AN_NO_LINK;
488 if (pdata->kr_state != AXGBE_RX_BPA)
489 return AXGBE_AN_NO_LINK;
492 axgbe_an_disable(pdata);
493 axgbe_switch_mode(pdata);
494 axgbe_an_restart(pdata);
496 return AXGBE_AN_INCOMPAT_LINK;
499 static void axgbe_an73_state_machine(struct axgbe_port *pdata)
501 enum axgbe_an cur_state = pdata->an_state;
507 if (pdata->an_int & AXGBE_AN_CL73_PG_RCV) {
508 pdata->an_state = AXGBE_AN_PAGE_RECEIVED;
509 pdata->an_int &= ~AXGBE_AN_CL73_PG_RCV;
510 } else if (pdata->an_int & AXGBE_AN_CL73_INC_LINK) {
511 pdata->an_state = AXGBE_AN_INCOMPAT_LINK;
512 pdata->an_int &= ~AXGBE_AN_CL73_INC_LINK;
513 } else if (pdata->an_int & AXGBE_AN_CL73_INT_CMPLT) {
514 pdata->an_state = AXGBE_AN_COMPLETE;
515 pdata->an_int &= ~AXGBE_AN_CL73_INT_CMPLT;
517 pdata->an_state = AXGBE_AN_ERROR;
521 cur_state = pdata->an_state;
523 switch (pdata->an_state) {
525 pdata->an_supported = 0;
527 case AXGBE_AN_PAGE_RECEIVED:
528 pdata->an_state = axgbe_an73_page_received(pdata);
529 pdata->an_supported++;
531 case AXGBE_AN_INCOMPAT_LINK:
532 pdata->an_supported = 0;
533 pdata->parallel_detect = 0;
534 pdata->an_state = axgbe_an73_incompat_link(pdata);
536 case AXGBE_AN_COMPLETE:
537 pdata->parallel_detect = pdata->an_supported ? 0 : 1;
539 case AXGBE_AN_NO_LINK:
542 pdata->an_state = AXGBE_AN_ERROR;
545 if (pdata->an_state == AXGBE_AN_NO_LINK) {
547 axgbe_an73_clear_interrupts(pdata);
548 pdata->eth_dev->data->dev_link.link_status =
550 } else if (pdata->an_state == AXGBE_AN_ERROR) {
551 PMD_DRV_LOG(ERR, "error during auto-negotiation, state=%u\n",
554 axgbe_an73_clear_interrupts(pdata);
557 if (pdata->an_state >= AXGBE_AN_COMPLETE) {
558 pdata->an_result = pdata->an_state;
559 pdata->an_state = AXGBE_AN_READY;
560 pdata->kr_state = AXGBE_RX_BPA;
561 pdata->kx_state = AXGBE_RX_BPA;
563 if (pdata->phy_if.phy_impl.an_post)
564 pdata->phy_if.phy_impl.an_post(pdata);
567 if (cur_state != pdata->an_state)
573 axgbe_an73_enable_interrupts(pdata);
576 static void axgbe_an73_isr(struct axgbe_port *pdata)
578 /* Disable AN interrupts */
579 axgbe_an73_disable_interrupts(pdata);
581 /* Save the interrupt(s) that fired */
582 pdata->an_int = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_INT);
585 /* Clear the interrupt(s) that fired and process them */
586 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_INT, ~pdata->an_int);
587 pthread_mutex_lock(&pdata->an_mutex);
588 axgbe_an73_state_machine(pdata);
589 pthread_mutex_unlock(&pdata->an_mutex);
591 /* Enable AN interrupts */
592 axgbe_an73_enable_interrupts(pdata);
596 static void axgbe_an_isr(struct axgbe_port *pdata)
598 switch (pdata->an_mode) {
599 case AXGBE_AN_MODE_CL73:
600 case AXGBE_AN_MODE_CL73_REDRV:
601 axgbe_an73_isr(pdata);
603 case AXGBE_AN_MODE_CL37:
604 case AXGBE_AN_MODE_CL37_SGMII:
605 PMD_DRV_LOG(ERR, "AN_MODE_37 not supported\n");
612 static void axgbe_an_combined_isr(struct axgbe_port *pdata)
617 static void axgbe_an73_init(struct axgbe_port *pdata)
619 unsigned int advertising, reg;
621 advertising = pdata->phy_if.phy_impl.an_advertising(pdata);
623 /* Set up Advertisement register 3 first */
624 reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2);
625 if (advertising & ADVERTISED_10000baseR_FEC)
630 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 2, reg);
632 /* Set up Advertisement register 2 next */
633 reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1);
634 if (advertising & ADVERTISED_10000baseKR_Full)
639 if ((advertising & ADVERTISED_1000baseKX_Full) ||
640 (advertising & ADVERTISED_2500baseX_Full))
645 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE + 1, reg);
647 /* Set up Advertisement register 1 last */
648 reg = XMDIO_READ(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE);
649 if (advertising & ADVERTISED_Pause)
654 if (advertising & ADVERTISED_Asym_Pause)
659 /* We don't intend to perform XNP */
660 reg &= ~AXGBE_XNP_NP_EXCHANGE;
662 XMDIO_WRITE(pdata, MDIO_MMD_AN, MDIO_AN_ADVERTISE, reg);
665 static void axgbe_an_init(struct axgbe_port *pdata)
667 /* Set up advertisement registers based on current settings */
668 pdata->an_mode = pdata->phy_if.phy_impl.an_mode(pdata);
669 switch (pdata->an_mode) {
670 case AXGBE_AN_MODE_CL73:
671 case AXGBE_AN_MODE_CL73_REDRV:
672 axgbe_an73_init(pdata);
674 case AXGBE_AN_MODE_CL37:
675 case AXGBE_AN_MODE_CL37_SGMII:
676 PMD_DRV_LOG(ERR, "Unsupported AN_CL37\n");
683 static void axgbe_phy_adjust_link(struct axgbe_port *pdata)
685 if (pdata->phy.link) {
686 /* Flow control support */
687 pdata->pause_autoneg = pdata->phy.pause_autoneg;
689 if (pdata->tx_pause != (unsigned int)pdata->phy.tx_pause) {
690 pdata->hw_if.config_tx_flow_control(pdata);
691 pdata->tx_pause = pdata->phy.tx_pause;
694 if (pdata->rx_pause != (unsigned int)pdata->phy.rx_pause) {
695 pdata->hw_if.config_rx_flow_control(pdata);
696 pdata->rx_pause = pdata->phy.rx_pause;
700 if (pdata->phy_speed != pdata->phy.speed)
701 pdata->phy_speed = pdata->phy.speed;
702 if (pdata->phy_link != pdata->phy.link)
703 pdata->phy_link = pdata->phy.link;
704 } else if (pdata->phy_link) {
706 pdata->phy_speed = SPEED_UNKNOWN;
710 static int axgbe_phy_config_fixed(struct axgbe_port *pdata)
712 enum axgbe_mode mode;
714 /* Disable auto-negotiation */
715 axgbe_an_disable(pdata);
717 /* Set specified mode for specified speed */
718 mode = pdata->phy_if.phy_impl.get_mode(pdata, pdata->phy.speed);
720 case AXGBE_MODE_KX_1000:
721 case AXGBE_MODE_KX_2500:
723 case AXGBE_MODE_SGMII_100:
724 case AXGBE_MODE_SGMII_1000:
728 case AXGBE_MODE_UNKNOWN:
733 /* Validate duplex mode */
734 if (pdata->phy.duplex != DUPLEX_FULL)
737 axgbe_set_mode(pdata, mode);
742 static int __axgbe_phy_config_aneg(struct axgbe_port *pdata)
746 axgbe_set_bit(AXGBE_LINK_INIT, &pdata->dev_state);
747 pdata->link_check = rte_get_timer_cycles();
749 ret = pdata->phy_if.phy_impl.an_config(pdata);
753 if (pdata->phy.autoneg != AUTONEG_ENABLE) {
754 ret = axgbe_phy_config_fixed(pdata);
755 if (ret || !pdata->kr_redrv)
759 /* Disable auto-negotiation interrupt */
760 rte_intr_disable(&pdata->pci_dev->intr_handle);
762 /* Start auto-negotiation in a supported mode */
763 if (axgbe_use_mode(pdata, AXGBE_MODE_KR)) {
764 axgbe_set_mode(pdata, AXGBE_MODE_KR);
765 } else if (axgbe_use_mode(pdata, AXGBE_MODE_KX_2500)) {
766 axgbe_set_mode(pdata, AXGBE_MODE_KX_2500);
767 } else if (axgbe_use_mode(pdata, AXGBE_MODE_KX_1000)) {
768 axgbe_set_mode(pdata, AXGBE_MODE_KX_1000);
769 } else if (axgbe_use_mode(pdata, AXGBE_MODE_SFI)) {
770 axgbe_set_mode(pdata, AXGBE_MODE_SFI);
771 } else if (axgbe_use_mode(pdata, AXGBE_MODE_X)) {
772 axgbe_set_mode(pdata, AXGBE_MODE_X);
773 } else if (axgbe_use_mode(pdata, AXGBE_MODE_SGMII_1000)) {
774 axgbe_set_mode(pdata, AXGBE_MODE_SGMII_1000);
775 } else if (axgbe_use_mode(pdata, AXGBE_MODE_SGMII_100)) {
776 axgbe_set_mode(pdata, AXGBE_MODE_SGMII_100);
778 rte_intr_enable(&pdata->pci_dev->intr_handle);
782 /* Disable and stop any in progress auto-negotiation */
783 axgbe_an_disable_all(pdata);
785 /* Clear any auto-negotitation interrupts */
786 axgbe_an_clear_interrupts_all(pdata);
788 pdata->an_result = AXGBE_AN_READY;
789 pdata->an_state = AXGBE_AN_READY;
790 pdata->kr_state = AXGBE_RX_BPA;
791 pdata->kx_state = AXGBE_RX_BPA;
793 /* Re-enable auto-negotiation interrupt */
794 rte_intr_enable(&pdata->pci_dev->intr_handle);
796 axgbe_an_init(pdata);
797 axgbe_an_restart(pdata);
802 static int axgbe_phy_config_aneg(struct axgbe_port *pdata)
806 pthread_mutex_lock(&pdata->an_mutex);
808 ret = __axgbe_phy_config_aneg(pdata);
810 axgbe_set_bit(AXGBE_LINK_ERR, &pdata->dev_state);
812 axgbe_clear_bit(AXGBE_LINK_ERR, &pdata->dev_state);
814 pthread_mutex_unlock(&pdata->an_mutex);
819 static bool axgbe_phy_aneg_done(struct axgbe_port *pdata)
821 return pdata->an_result == AXGBE_AN_COMPLETE;
824 static void axgbe_check_link_timeout(struct axgbe_port *pdata)
826 unsigned long link_timeout;
829 link_timeout = pdata->link_check + (AXGBE_LINK_TIMEOUT *
830 2 * rte_get_timer_hz());
831 ticks = rte_get_timer_cycles();
832 if (time_after(ticks, link_timeout))
833 axgbe_phy_config_aneg(pdata);
836 static enum axgbe_mode axgbe_phy_status_aneg(struct axgbe_port *pdata)
838 return pdata->phy_if.phy_impl.an_outcome(pdata);
841 static void axgbe_phy_status_result(struct axgbe_port *pdata)
843 enum axgbe_mode mode;
845 pdata->phy.lp_advertising = 0;
847 if ((pdata->phy.autoneg != AUTONEG_ENABLE) || pdata->parallel_detect)
848 mode = axgbe_cur_mode(pdata);
850 mode = axgbe_phy_status_aneg(pdata);
853 case AXGBE_MODE_SGMII_100:
854 pdata->phy.speed = SPEED_100;
857 case AXGBE_MODE_KX_1000:
858 case AXGBE_MODE_SGMII_1000:
859 pdata->phy.speed = SPEED_1000;
861 case AXGBE_MODE_KX_2500:
862 pdata->phy.speed = SPEED_2500;
866 pdata->phy.speed = SPEED_10000;
868 case AXGBE_MODE_UNKNOWN:
870 pdata->phy.speed = SPEED_UNKNOWN;
873 pdata->phy.duplex = DUPLEX_FULL;
875 axgbe_set_mode(pdata, mode);
878 static void axgbe_phy_status(struct axgbe_port *pdata)
880 unsigned int link_aneg;
883 if (axgbe_test_bit(AXGBE_LINK_ERR, &pdata->dev_state)) {
888 link_aneg = (pdata->phy.autoneg == AUTONEG_ENABLE);
890 pdata->phy.link = pdata->phy_if.phy_impl.link_status(pdata,
893 axgbe_phy_config_aneg(pdata);
897 if (pdata->phy.link) {
898 if (link_aneg && !axgbe_phy_aneg_done(pdata)) {
899 axgbe_check_link_timeout(pdata);
902 axgbe_phy_status_result(pdata);
903 if (axgbe_test_bit(AXGBE_LINK_INIT, &pdata->dev_state))
904 axgbe_clear_bit(AXGBE_LINK_INIT, &pdata->dev_state);
906 if (axgbe_test_bit(AXGBE_LINK_INIT, &pdata->dev_state)) {
907 axgbe_check_link_timeout(pdata);
912 axgbe_phy_status_result(pdata);
916 axgbe_phy_adjust_link(pdata);
919 static void axgbe_phy_stop(struct axgbe_port *pdata)
921 if (!pdata->phy_started)
923 /* Indicate the PHY is down */
924 pdata->phy_started = 0;
925 /* Disable auto-negotiation */
926 axgbe_an_disable_all(pdata);
927 pdata->phy_if.phy_impl.stop(pdata);
929 axgbe_phy_adjust_link(pdata);
932 static int axgbe_phy_start(struct axgbe_port *pdata)
936 ret = pdata->phy_if.phy_impl.start(pdata);
939 /* Set initial mode - call the mode setting routines
940 * directly to insure we are properly configured
942 if (axgbe_use_mode(pdata, AXGBE_MODE_KR)) {
943 axgbe_kr_mode(pdata);
944 } else if (axgbe_use_mode(pdata, AXGBE_MODE_KX_2500)) {
945 axgbe_kx_2500_mode(pdata);
946 } else if (axgbe_use_mode(pdata, AXGBE_MODE_KX_1000)) {
947 axgbe_kx_1000_mode(pdata);
948 } else if (axgbe_use_mode(pdata, AXGBE_MODE_SFI)) {
949 axgbe_sfi_mode(pdata);
950 } else if (axgbe_use_mode(pdata, AXGBE_MODE_X)) {
952 } else if (axgbe_use_mode(pdata, AXGBE_MODE_SGMII_1000)) {
953 axgbe_sgmii_1000_mode(pdata);
954 } else if (axgbe_use_mode(pdata, AXGBE_MODE_SGMII_100)) {
955 axgbe_sgmii_100_mode(pdata);
960 /* Indicate the PHY is up and running */
961 pdata->phy_started = 1;
962 axgbe_an_init(pdata);
963 axgbe_an_enable_interrupts(pdata);
964 return axgbe_phy_config_aneg(pdata);
967 pdata->phy_if.phy_impl.stop(pdata);
972 static int axgbe_phy_reset(struct axgbe_port *pdata)
976 ret = pdata->phy_if.phy_impl.reset(pdata);
980 /* Disable auto-negotiation for now */
981 axgbe_an_disable_all(pdata);
983 /* Clear auto-negotiation interrupts */
984 axgbe_an_clear_interrupts_all(pdata);
989 static int axgbe_phy_best_advertised_speed(struct axgbe_port *pdata)
991 if (pdata->phy.advertising & ADVERTISED_10000baseKR_Full)
993 else if (pdata->phy.advertising & ADVERTISED_10000baseT_Full)
995 else if (pdata->phy.advertising & ADVERTISED_2500baseX_Full)
997 else if (pdata->phy.advertising & ADVERTISED_1000baseKX_Full)
999 else if (pdata->phy.advertising & ADVERTISED_1000baseT_Full)
1001 else if (pdata->phy.advertising & ADVERTISED_100baseT_Full)
1004 return SPEED_UNKNOWN;
1007 static int axgbe_phy_init(struct axgbe_port *pdata)
1011 pdata->mdio_mmd = MDIO_MMD_PCS;
1013 /* Check for FEC support */
1014 pdata->fec_ability = XMDIO_READ(pdata, MDIO_MMD_PMAPMD,
1015 MDIO_PMA_10GBR_FECABLE);
1016 pdata->fec_ability &= (MDIO_PMA_10GBR_FECABLE_ABLE |
1017 MDIO_PMA_10GBR_FECABLE_ERRABLE);
1019 /* Setup the phy (including supported features) */
1020 ret = pdata->phy_if.phy_impl.init(pdata);
1023 pdata->phy.advertising = pdata->phy.supported;
1025 pdata->phy.address = 0;
1027 if (pdata->phy.advertising & ADVERTISED_Autoneg) {
1028 pdata->phy.autoneg = AUTONEG_ENABLE;
1029 pdata->phy.speed = SPEED_UNKNOWN;
1030 pdata->phy.duplex = DUPLEX_UNKNOWN;
1032 pdata->phy.autoneg = AUTONEG_DISABLE;
1033 pdata->phy.speed = axgbe_phy_best_advertised_speed(pdata);
1034 pdata->phy.duplex = DUPLEX_FULL;
1037 pdata->phy.link = 0;
1039 pdata->phy.pause_autoneg = pdata->pause_autoneg;
1040 pdata->phy.tx_pause = pdata->tx_pause;
1041 pdata->phy.rx_pause = pdata->rx_pause;
1043 /* Fix up Flow Control advertising */
1044 pdata->phy.advertising &= ~ADVERTISED_Pause;
1045 pdata->phy.advertising &= ~ADVERTISED_Asym_Pause;
1047 if (pdata->rx_pause) {
1048 pdata->phy.advertising |= ADVERTISED_Pause;
1049 pdata->phy.advertising |= ADVERTISED_Asym_Pause;
1052 if (pdata->tx_pause)
1053 pdata->phy.advertising ^= ADVERTISED_Asym_Pause;
1057 void axgbe_init_function_ptrs_phy(struct axgbe_phy_if *phy_if)
1059 phy_if->phy_init = axgbe_phy_init;
1060 phy_if->phy_reset = axgbe_phy_reset;
1061 phy_if->phy_start = axgbe_phy_start;
1062 phy_if->phy_stop = axgbe_phy_stop;
1063 phy_if->phy_status = axgbe_phy_status;
1064 phy_if->phy_config_aneg = axgbe_phy_config_aneg;
1065 phy_if->an_isr = axgbe_an_combined_isr;