3fdbdba495490f60b77d7f8eaf068e0b7bf48a0d
[dpdk.git] / drivers / net / cnxk / cnxk_link.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2021 Marvell.
3  */
4
5 #include "cnxk_ethdev.h"
6
7 void
8 cnxk_nix_toggle_flag_link_cfg(struct cnxk_eth_dev *dev, bool set)
9 {
10         if (set)
11                 dev->flags |= CNXK_LINK_CFG_IN_PROGRESS_F;
12         else
13                 dev->flags &= ~CNXK_LINK_CFG_IN_PROGRESS_F;
14
15         rte_wmb();
16 }
17
18 static inline int
19 nix_wait_for_link_cfg(struct cnxk_eth_dev *dev)
20 {
21         uint16_t wait = 1000;
22
23         do {
24                 rte_atomic_thread_fence(__ATOMIC_ACQUIRE);
25                 if (!(dev->flags & CNXK_LINK_CFG_IN_PROGRESS_F))
26                         break;
27                 wait--;
28                 rte_delay_ms(1);
29         } while (wait);
30
31         return wait ? 0 : -1;
32 }
33
34 static void
35 nix_link_status_print(struct rte_eth_dev *eth_dev, struct rte_eth_link *link)
36 {
37         if (link && link->link_status)
38                 plt_info("Port %d: Link Up - speed %u Mbps - %s",
39                          (int)(eth_dev->data->port_id),
40                          (uint32_t)link->link_speed,
41                          link->link_duplex == ETH_LINK_FULL_DUPLEX
42                                  ? "full-duplex"
43                                  : "half-duplex");
44         else
45                 plt_info("Port %d: Link Down", (int)(eth_dev->data->port_id));
46 }
47
48 void
49 cnxk_eth_dev_link_status_cb(struct roc_nix *nix, struct roc_nix_link_info *link)
50 {
51         struct cnxk_eth_dev *dev = (struct cnxk_eth_dev *)nix;
52         struct rte_eth_link eth_link;
53         struct rte_eth_dev *eth_dev;
54
55         if (!link || !nix)
56                 return;
57
58         eth_dev = dev->eth_dev;
59         if (!eth_dev || !eth_dev->data->dev_conf.intr_conf.lsc)
60                 return;
61
62         if (nix_wait_for_link_cfg(dev)) {
63                 plt_err("Timeout waiting for link_cfg to complete");
64                 return;
65         }
66
67         eth_link.link_status = link->status;
68         eth_link.link_speed = link->speed;
69         eth_link.link_autoneg = ETH_LINK_AUTONEG;
70         eth_link.link_duplex = link->full_duplex;
71
72         /* Print link info */
73         nix_link_status_print(eth_dev, &eth_link);
74
75         /* Update link info */
76         rte_eth_linkstatus_set(eth_dev, &eth_link);
77
78         /* Set the flag and execute application callbacks */
79         rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
80 }
81
82 int
83 cnxk_nix_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete)
84 {
85         struct cnxk_eth_dev *dev = cnxk_eth_pmd_priv(eth_dev);
86         struct roc_nix_link_info info;
87         struct rte_eth_link link;
88         int rc;
89
90         RTE_SET_USED(wait_to_complete);
91         memset(&link, 0, sizeof(struct rte_eth_link));
92
93         if (!eth_dev->data->dev_started || roc_nix_is_sdp(&dev->nix))
94                 return 0;
95
96         if (roc_nix_is_lbk(&dev->nix)) {
97                 link.link_status = ETH_LINK_UP;
98                 link.link_speed = ETH_SPEED_NUM_100G;
99                 link.link_autoneg = ETH_LINK_FIXED;
100                 link.link_duplex = ETH_LINK_FULL_DUPLEX;
101         } else {
102                 rc = roc_nix_mac_link_info_get(&dev->nix, &info);
103                 if (rc)
104                         return rc;
105                 link.link_status = info.status;
106                 link.link_speed = info.speed;
107                 link.link_autoneg = ETH_LINK_AUTONEG;
108                 if (info.full_duplex)
109                         link.link_duplex = info.full_duplex;
110         }
111
112         return rte_eth_linkstatus_set(eth_dev, &link);
113 }