ethdev: add namespace
[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 <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 == RTE_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_get(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_link eth_link;
56         struct rte_eth_dev *eth_dev;
57
58         if (!link || !dev)
59                 return;
60
61         eth_dev = otx2_dev->eth_dev;
62         if (!eth_dev)
63                 return;
64
65         rte_eth_linkstatus_get(eth_dev, &eth_link);
66
67         link->link_up = eth_link.link_status;
68         link->speed = eth_link.link_speed;
69         link->an = eth_link.link_autoneg;
70         link->full_duplex = eth_link.link_duplex;
71 }
72
73 void
74 otx2_eth_dev_link_status_update(struct otx2_dev *dev,
75                                 struct cgx_link_user_info *link)
76 {
77         struct otx2_eth_dev *otx2_dev = (struct otx2_eth_dev *)dev;
78         struct rte_eth_link eth_link;
79         struct rte_eth_dev *eth_dev;
80
81         if (!link || !dev)
82                 return;
83
84         eth_dev = otx2_dev->eth_dev;
85         if (!eth_dev || !eth_dev->data->dev_conf.intr_conf.lsc)
86                 return;
87
88         if (nix_wait_for_link_cfg(otx2_dev)) {
89                 otx2_err("Timeout waiting for link_cfg to complete");
90                 return;
91         }
92
93         eth_link.link_status = link->link_up;
94         eth_link.link_speed = link->speed;
95         eth_link.link_autoneg = RTE_ETH_LINK_AUTONEG;
96         eth_link.link_duplex = link->full_duplex;
97
98         otx2_dev->speed = link->speed;
99         otx2_dev->duplex = link->full_duplex;
100
101         /* Print link info */
102         nix_link_status_print(eth_dev, &eth_link);
103
104         /* Update link info */
105         rte_eth_linkstatus_set(eth_dev, &eth_link);
106
107         /* Set the flag and execute application callbacks */
108         rte_eth_dev_callback_process(eth_dev, RTE_ETH_EVENT_INTR_LSC, NULL);
109 }
110
111 static int
112 lbk_link_update(struct rte_eth_link *link)
113 {
114         link->link_status = RTE_ETH_LINK_UP;
115         link->link_speed = RTE_ETH_SPEED_NUM_100G;
116         link->link_autoneg = RTE_ETH_LINK_FIXED;
117         link->link_duplex = RTE_ETH_LINK_FULL_DUPLEX;
118         return 0;
119 }
120
121 static int
122 cgx_link_update(struct otx2_eth_dev *dev, struct rte_eth_link *link)
123 {
124         struct otx2_mbox *mbox = dev->mbox;
125         struct cgx_link_info_msg *rsp;
126         int rc;
127         otx2_mbox_alloc_msg_cgx_get_linkinfo(mbox);
128         rc = otx2_mbox_process_msg(mbox, (void *)&rsp);
129         if (rc)
130                 return rc;
131
132         link->link_status = rsp->link_info.link_up;
133         link->link_speed = rsp->link_info.speed;
134         link->link_autoneg = RTE_ETH_LINK_AUTONEG;
135
136         if (rsp->link_info.full_duplex)
137                 link->link_duplex = rsp->link_info.full_duplex;
138         return 0;
139 }
140
141 int
142 otx2_nix_link_update(struct rte_eth_dev *eth_dev, int wait_to_complete)
143 {
144         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
145         struct rte_eth_link link;
146         int rc;
147
148         RTE_SET_USED(wait_to_complete);
149         memset(&link, 0, sizeof(struct rte_eth_link));
150
151         if (!eth_dev->data->dev_started || otx2_dev_is_sdp(dev))
152                 return 0;
153
154         if (otx2_dev_is_lbk(dev))
155                 rc = lbk_link_update(&link);
156         else
157                 rc = cgx_link_update(dev, &link);
158
159         if (rc)
160                 return rc;
161
162         return rte_eth_linkstatus_set(eth_dev, &link);
163 }
164
165 static int
166 nix_dev_set_link_state(struct rte_eth_dev *eth_dev, uint8_t enable)
167 {
168         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
169         struct otx2_mbox *mbox = dev->mbox;
170         struct cgx_set_link_state_msg *req;
171
172         req = otx2_mbox_alloc_msg_cgx_set_link_state(mbox);
173         req->enable = enable;
174         return otx2_mbox_process(mbox);
175 }
176
177 int
178 otx2_nix_dev_set_link_up(struct rte_eth_dev *eth_dev)
179 {
180         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
181         int rc, i;
182
183         if (otx2_dev_is_vf_or_sdp(dev))
184                 return -ENOTSUP;
185
186         rc = nix_dev_set_link_state(eth_dev, 1);
187         if (rc)
188                 goto done;
189
190         /* Start tx queues  */
191         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
192                 otx2_nix_tx_queue_start(eth_dev, i);
193
194 done:
195         return rc;
196 }
197
198 int
199 otx2_nix_dev_set_link_down(struct rte_eth_dev *eth_dev)
200 {
201         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
202         int i;
203
204         if (otx2_dev_is_vf_or_sdp(dev))
205                 return -ENOTSUP;
206
207         /* Stop tx queues  */
208         for (i = 0; i < eth_dev->data->nb_tx_queues; i++)
209                 otx2_nix_tx_queue_stop(eth_dev, i);
210
211         return nix_dev_set_link_state(eth_dev, 0);
212 }
213
214 static int
215 cgx_change_mode(struct otx2_eth_dev *dev, struct cgx_set_link_mode_args *cfg)
216 {
217         struct otx2_mbox *mbox = dev->mbox;
218         struct cgx_set_link_mode_req *req;
219
220         req = otx2_mbox_alloc_msg_cgx_set_link_mode(mbox);
221         req->args.speed = cfg->speed;
222         req->args.duplex = cfg->duplex;
223         req->args.an = cfg->an;
224
225         return otx2_mbox_process(mbox);
226 }
227
228 #define SPEED_NONE 0
229 static inline uint32_t
230 nix_parse_link_speeds(struct otx2_eth_dev *dev, uint32_t link_speeds)
231 {
232         uint32_t link_speed = SPEED_NONE;
233
234         /* 50G and 100G to be supported for board version C0 and above */
235         if (!otx2_dev_is_Ax(dev)) {
236                 if (link_speeds & RTE_ETH_LINK_SPEED_100G)
237                         link_speed = 100000;
238                 if (link_speeds & RTE_ETH_LINK_SPEED_50G)
239                         link_speed = 50000;
240         }
241         if (link_speeds & RTE_ETH_LINK_SPEED_40G)
242                 link_speed = 40000;
243         if (link_speeds & RTE_ETH_LINK_SPEED_25G)
244                 link_speed = 25000;
245         if (link_speeds & RTE_ETH_LINK_SPEED_20G)
246                 link_speed = 20000;
247         if (link_speeds & RTE_ETH_LINK_SPEED_10G)
248                 link_speed = 10000;
249         if (link_speeds & RTE_ETH_LINK_SPEED_5G)
250                 link_speed = 5000;
251         if (link_speeds & RTE_ETH_LINK_SPEED_1G)
252                 link_speed = 1000;
253
254         return link_speed;
255 }
256
257 static inline uint8_t
258 nix_parse_eth_link_duplex(uint32_t link_speeds)
259 {
260         if ((link_speeds & RTE_ETH_LINK_SPEED_10M_HD) ||
261                         (link_speeds & RTE_ETH_LINK_SPEED_100M_HD))
262                 return RTE_ETH_LINK_HALF_DUPLEX;
263         else
264                 return RTE_ETH_LINK_FULL_DUPLEX;
265 }
266
267 int
268 otx2_apply_link_speed(struct rte_eth_dev *eth_dev)
269 {
270         struct otx2_eth_dev *dev = otx2_eth_pmd_priv(eth_dev);
271         struct rte_eth_conf *conf = &eth_dev->data->dev_conf;
272         struct cgx_set_link_mode_args cfg;
273
274         /* If VF/SDP/LBK, link attributes cannot be changed */
275         if (otx2_dev_is_vf_or_sdp(dev) || otx2_dev_is_lbk(dev))
276                 return 0;
277
278         memset(&cfg, 0, sizeof(struct cgx_set_link_mode_args));
279         cfg.speed = nix_parse_link_speeds(dev, conf->link_speeds);
280         if (cfg.speed != SPEED_NONE && cfg.speed != dev->speed) {
281                 cfg.duplex = nix_parse_eth_link_duplex(conf->link_speeds);
282                 cfg.an = (conf->link_speeds & RTE_ETH_LINK_SPEED_FIXED) == 0;
283
284                 return cgx_change_mode(dev, &cfg);
285         }
286         return 0;
287 }