8fcbdc9b7c0a1c957c4a886bff9e09c83e42dd91
[dpdk.git] / drivers / net / octeontx2 / otx2_link.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2019 Marvell International Ltd.
3  */
4
5 #include <rte_common.h>
6 #include <rte_ethdev_pci.h>
7
8 #include "otx2_ethdev.h"
9
10 void
11 otx2_nix_toggle_flag_link_cfg(struct otx2_eth_dev *dev, bool set)
12 {
13         if (set)
14                 dev->flags |= OTX2_LINK_CFG_IN_PROGRESS_F;
15         else
16                 dev->flags &= ~OTX2_LINK_CFG_IN_PROGRESS_F;
17
18         rte_wmb();
19 }
20
21 static inline int
22 nix_wait_for_link_cfg(struct otx2_eth_dev *dev)
23 {
24         uint16_t wait = 1000;
25
26         do {
27                 rte_rmb();
28                 if (!(dev->flags & OTX2_LINK_CFG_IN_PROGRESS_F))
29                         break;
30                 wait--;
31                 rte_delay_ms(1);
32         } while (wait);
33
34         return wait ? 0 : -1;
35 }
36
37 static void
38 nix_link_status_print(struct rte_eth_dev *eth_dev, struct rte_eth_link *link)
39 {
40         if (link && link->link_status)
41                 otx2_info("Port %d: Link Up - speed %u Mbps - %s",
42                           (int)(eth_dev->data->port_id),
43                           (uint32_t)link->link_speed,
44                           link->link_duplex == ETH_LINK_FULL_DUPLEX ?
45                           "full-duplex" : "half-duplex");
46         else
47                 otx2_info("Port %d: Link Down", (int)(eth_dev->data->port_id));
48 }
49
50 void
51 otx2_eth_dev_link_status_update(struct otx2_dev *dev,
52                                 struct cgx_link_user_info *link)
53 {
54         struct otx2_eth_dev *otx2_dev = (struct otx2_eth_dev *)dev;
55         struct rte_eth_dev *eth_dev = otx2_dev->eth_dev;
56         struct rte_eth_link eth_link;
57
58         if (!link || !dev || !eth_dev->data->dev_conf.intr_conf.lsc)
59                 return;
60
61         if (nix_wait_for_link_cfg(otx2_dev)) {
62                 otx2_err("Timeout waiting for link_cfg to complete");
63                 return;
64         }
65
66         eth_link.link_status = link->link_up;
67         eth_link.link_speed = link->speed;
68         eth_link.link_autoneg = ETH_LINK_AUTONEG;
69         eth_link.link_duplex = link->full_duplex;
70
71         /* Print link info */
72         nix_link_status_print(eth_dev, &eth_link);
73
74         /* Update link info */
75         rte_eth_linkstatus_set(eth_dev, &eth_link);
76
77         /* Set the flag and execute application callbacks */
78         _rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
79 }
80
81 int
82 otx2_nix_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete)
83 {
84         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
85         struct otx2_mbox *mbox = dev->mbox;
86         struct cgx_link_info_msg *rsp;
87         struct rte_eth_link link;
88         int rc;
89
90         RTE_SET_USED(wait_to_complete);
91
92         if (otx2_dev_is_lbk(dev))
93                 return 0;
94
95         otx2_mbox_alloc_msg_cgx_get_linkinfo(mbox);
96         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
97         if (rc)
98                 return rc;
99
100         link.link_status = rsp->link_info.link_up;
101         link.link_speed = rsp->link_info.speed;
102         link.link_autoneg = ETH_LINK_AUTONEG;
103
104         if (rsp->link_info.full_duplex)
105                 link.link_duplex = rsp->link_info.full_duplex;
106
107         return rte_eth_linkstatus_set(eth_dev, &link);
108 }
109
110 static int
111 nix_dev_set_link_state(struct rte_eth_dev *eth_dev, uint8_t enable)
112 {
113         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
114         struct otx2_mbox *mbox = dev->mbox;
115         struct cgx_set_link_state_msg *req;
116
117         req = otx2_mbox_alloc_msg_cgx_set_link_state(mbox);
118         req->enable = enable;
119         return otx2_mbox_process(mbox);
120 }
121
122 int
123 otx2_nix_dev_set_link_up(struct rte_eth_dev *eth_dev)
124 {
125         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
126         int rc, i;
127
128         if (otx2_dev_is_vf(dev))
129                 return -ENOTSUP;
130
131         rc = nix_dev_set_link_state(eth_dev, 1);
132         if (rc)
133                 goto done;
134
135         /* Start tx queues  */
136         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
137                 otx2_nix_tx_queue_start(eth_dev, i);
138
139 done:
140         return rc;
141 }
142
143 int
144 otx2_nix_dev_set_link_down(struct rte_eth_dev *eth_dev)
145 {
146         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
147         int i;
148
149         if (otx2_dev_is_vf(dev))
150                 return -ENOTSUP;
151
152         /* Stop tx queues  */
153         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
154                 otx2_nix_tx_queue_stop(eth_dev, i);
155
156         return nix_dev_set_link_state(eth_dev, 0);
157 }