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