xref: /linux/drivers/net/ethernet/freescale/dpaa2/dpaa2-mac.c (revision 41fb0cf1bced59c1fe178cf6cc9f716b5da9e40e)
1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2019 NXP */
3 
4 #include <linux/acpi.h>
5 #include <linux/property.h>
6 
7 #include "dpaa2-eth.h"
8 #include "dpaa2-mac.h"
9 
10 #define phylink_to_dpaa2_mac(config) \
11 	container_of((config), struct dpaa2_mac, phylink_config)
12 
13 static int phy_mode(enum dpmac_eth_if eth_if, phy_interface_t *if_mode)
14 {
15 	*if_mode = PHY_INTERFACE_MODE_NA;
16 
17 	switch (eth_if) {
18 	case DPMAC_ETH_IF_RGMII:
19 		*if_mode = PHY_INTERFACE_MODE_RGMII;
20 		break;
21 	case DPMAC_ETH_IF_USXGMII:
22 		*if_mode = PHY_INTERFACE_MODE_USXGMII;
23 		break;
24 	case DPMAC_ETH_IF_QSGMII:
25 		*if_mode = PHY_INTERFACE_MODE_QSGMII;
26 		break;
27 	case DPMAC_ETH_IF_SGMII:
28 		*if_mode = PHY_INTERFACE_MODE_SGMII;
29 		break;
30 	case DPMAC_ETH_IF_XFI:
31 		*if_mode = PHY_INTERFACE_MODE_10GBASER;
32 		break;
33 	default:
34 		return -EINVAL;
35 	}
36 
37 	return 0;
38 }
39 
40 static struct fwnode_handle *dpaa2_mac_get_node(struct device *dev,
41 						u16 dpmac_id)
42 {
43 	struct fwnode_handle *fwnode, *parent, *child  = NULL;
44 	struct device_node *dpmacs = NULL;
45 	int err;
46 	u32 id;
47 
48 	fwnode = dev_fwnode(dev->parent);
49 	if (is_of_node(fwnode)) {
50 		dpmacs = of_find_node_by_name(NULL, "dpmacs");
51 		if (!dpmacs)
52 			return NULL;
53 		parent = of_fwnode_handle(dpmacs);
54 	} else if (is_acpi_node(fwnode)) {
55 		parent = fwnode;
56 	}
57 
58 	fwnode_for_each_child_node(parent, child) {
59 		err = -EINVAL;
60 		if (is_acpi_device_node(child))
61 			err = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), &id);
62 		else if (is_of_node(child))
63 			err = of_property_read_u32(to_of_node(child), "reg", &id);
64 		if (err)
65 			continue;
66 
67 		if (id == dpmac_id) {
68 			of_node_put(dpmacs);
69 			return child;
70 		}
71 	}
72 	of_node_put(dpmacs);
73 	return NULL;
74 }
75 
76 static int dpaa2_mac_get_if_mode(struct fwnode_handle *dpmac_node,
77 				 struct dpmac_attr attr)
78 {
79 	phy_interface_t if_mode;
80 	int err;
81 
82 	err = fwnode_get_phy_mode(dpmac_node);
83 	if (err > 0)
84 		return err;
85 
86 	err = phy_mode(attr.eth_if, &if_mode);
87 	if (!err)
88 		return if_mode;
89 
90 	return err;
91 }
92 
93 static void dpaa2_mac_config(struct phylink_config *config, unsigned int mode,
94 			     const struct phylink_link_state *state)
95 {
96 	struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config);
97 	struct dpmac_link_state *dpmac_state = &mac->state;
98 	int err;
99 
100 	if (state->an_enabled)
101 		dpmac_state->options |= DPMAC_LINK_OPT_AUTONEG;
102 	else
103 		dpmac_state->options &= ~DPMAC_LINK_OPT_AUTONEG;
104 
105 	err = dpmac_set_link_state(mac->mc_io, 0,
106 				   mac->mc_dev->mc_handle, dpmac_state);
107 	if (err)
108 		netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n",
109 			   __func__, err);
110 }
111 
112 static void dpaa2_mac_link_up(struct phylink_config *config,
113 			      struct phy_device *phy,
114 			      unsigned int mode, phy_interface_t interface,
115 			      int speed, int duplex,
116 			      bool tx_pause, bool rx_pause)
117 {
118 	struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config);
119 	struct dpmac_link_state *dpmac_state = &mac->state;
120 	int err;
121 
122 	dpmac_state->up = 1;
123 
124 	dpmac_state->rate = speed;
125 
126 	if (duplex == DUPLEX_HALF)
127 		dpmac_state->options |= DPMAC_LINK_OPT_HALF_DUPLEX;
128 	else if (duplex == DUPLEX_FULL)
129 		dpmac_state->options &= ~DPMAC_LINK_OPT_HALF_DUPLEX;
130 
131 	if (rx_pause)
132 		dpmac_state->options |= DPMAC_LINK_OPT_PAUSE;
133 	else
134 		dpmac_state->options &= ~DPMAC_LINK_OPT_PAUSE;
135 
136 	if (rx_pause ^ tx_pause)
137 		dpmac_state->options |= DPMAC_LINK_OPT_ASYM_PAUSE;
138 	else
139 		dpmac_state->options &= ~DPMAC_LINK_OPT_ASYM_PAUSE;
140 
141 	err = dpmac_set_link_state(mac->mc_io, 0,
142 				   mac->mc_dev->mc_handle, dpmac_state);
143 	if (err)
144 		netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n",
145 			   __func__, err);
146 }
147 
148 static void dpaa2_mac_link_down(struct phylink_config *config,
149 				unsigned int mode,
150 				phy_interface_t interface)
151 {
152 	struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config);
153 	struct dpmac_link_state *dpmac_state = &mac->state;
154 	int err;
155 
156 	dpmac_state->up = 0;
157 	err = dpmac_set_link_state(mac->mc_io, 0,
158 				   mac->mc_dev->mc_handle, dpmac_state);
159 	if (err)
160 		netdev_err(mac->net_dev, "dpmac_set_link_state() = %d\n", err);
161 }
162 
163 static const struct phylink_mac_ops dpaa2_mac_phylink_ops = {
164 	.validate = phylink_generic_validate,
165 	.mac_config = dpaa2_mac_config,
166 	.mac_link_up = dpaa2_mac_link_up,
167 	.mac_link_down = dpaa2_mac_link_down,
168 };
169 
170 static int dpaa2_pcs_create(struct dpaa2_mac *mac,
171 			    struct fwnode_handle *dpmac_node,
172 			    int id)
173 {
174 	struct mdio_device *mdiodev;
175 	struct fwnode_handle *node;
176 
177 	node = fwnode_find_reference(dpmac_node, "pcs-handle", 0);
178 	if (IS_ERR(node)) {
179 		/* do not error out on old DTS files */
180 		netdev_warn(mac->net_dev, "pcs-handle node not found\n");
181 		return 0;
182 	}
183 
184 	if (!fwnode_device_is_available(node)) {
185 		netdev_err(mac->net_dev, "pcs-handle node not available\n");
186 		fwnode_handle_put(node);
187 		return -ENODEV;
188 	}
189 
190 	mdiodev = fwnode_mdio_find_device(node);
191 	fwnode_handle_put(node);
192 	if (!mdiodev)
193 		return -EPROBE_DEFER;
194 
195 	mac->pcs = lynx_pcs_create(mdiodev);
196 	if (!mac->pcs) {
197 		netdev_err(mac->net_dev, "lynx_pcs_create() failed\n");
198 		put_device(&mdiodev->dev);
199 		return -ENOMEM;
200 	}
201 
202 	return 0;
203 }
204 
205 static void dpaa2_pcs_destroy(struct dpaa2_mac *mac)
206 {
207 	struct lynx_pcs *pcs = mac->pcs;
208 
209 	if (pcs) {
210 		struct device *dev = &pcs->mdio->dev;
211 		lynx_pcs_destroy(pcs);
212 		put_device(dev);
213 		mac->pcs = NULL;
214 	}
215 }
216 
217 int dpaa2_mac_connect(struct dpaa2_mac *mac)
218 {
219 	struct net_device *net_dev = mac->net_dev;
220 	struct fwnode_handle *dpmac_node;
221 	struct phylink *phylink;
222 	int err;
223 
224 	mac->if_link_type = mac->attr.link_type;
225 
226 	dpmac_node = mac->fw_node;
227 	if (!dpmac_node) {
228 		netdev_err(net_dev, "No dpmac@%d node found.\n", mac->attr.id);
229 		return -ENODEV;
230 	}
231 
232 	err = dpaa2_mac_get_if_mode(dpmac_node, mac->attr);
233 	if (err < 0)
234 		return -EINVAL;
235 	mac->if_mode = err;
236 
237 	/* The MAC does not have the capability to add RGMII delays so
238 	 * error out if the interface mode requests them and there is no PHY
239 	 * to act upon them
240 	 */
241 	if (of_phy_is_fixed_link(to_of_node(dpmac_node)) &&
242 	    (mac->if_mode == PHY_INTERFACE_MODE_RGMII_ID ||
243 	     mac->if_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
244 	     mac->if_mode == PHY_INTERFACE_MODE_RGMII_TXID)) {
245 		netdev_err(net_dev, "RGMII delay not supported\n");
246 		return -EINVAL;
247 	}
248 
249 	if ((mac->attr.link_type == DPMAC_LINK_TYPE_PHY &&
250 	     mac->attr.eth_if != DPMAC_ETH_IF_RGMII) ||
251 	    mac->attr.link_type == DPMAC_LINK_TYPE_BACKPLANE) {
252 		err = dpaa2_pcs_create(mac, dpmac_node, mac->attr.id);
253 		if (err)
254 			return err;
255 	}
256 
257 	memset(&mac->phylink_config, 0, sizeof(mac->phylink_config));
258 	mac->phylink_config.dev = &net_dev->dev;
259 	mac->phylink_config.type = PHYLINK_NETDEV;
260 
261 	mac->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE |
262 		MAC_10FD | MAC_100FD | MAC_1000FD | MAC_2500FD | MAC_5000FD |
263 		MAC_10000FD;
264 
265 	/* We support the current interface mode, and if we have a PCS
266 	 * similar interface modes that do not require the PLLs to be
267 	 * reconfigured.
268 	 */
269 	__set_bit(mac->if_mode, mac->phylink_config.supported_interfaces);
270 	if (mac->pcs) {
271 		switch (mac->if_mode) {
272 		case PHY_INTERFACE_MODE_1000BASEX:
273 		case PHY_INTERFACE_MODE_SGMII:
274 			__set_bit(PHY_INTERFACE_MODE_1000BASEX,
275 				  mac->phylink_config.supported_interfaces);
276 			__set_bit(PHY_INTERFACE_MODE_SGMII,
277 				  mac->phylink_config.supported_interfaces);
278 			break;
279 
280 		default:
281 			break;
282 		}
283 	}
284 
285 	phylink = phylink_create(&mac->phylink_config,
286 				 dpmac_node, mac->if_mode,
287 				 &dpaa2_mac_phylink_ops);
288 	if (IS_ERR(phylink)) {
289 		err = PTR_ERR(phylink);
290 		goto err_pcs_destroy;
291 	}
292 	mac->phylink = phylink;
293 
294 	if (mac->pcs)
295 		phylink_set_pcs(mac->phylink, &mac->pcs->pcs);
296 
297 	err = phylink_fwnode_phy_connect(mac->phylink, dpmac_node, 0);
298 	if (err) {
299 		netdev_err(net_dev, "phylink_fwnode_phy_connect() = %d\n", err);
300 		goto err_phylink_destroy;
301 	}
302 
303 	return 0;
304 
305 err_phylink_destroy:
306 	phylink_destroy(mac->phylink);
307 err_pcs_destroy:
308 	dpaa2_pcs_destroy(mac);
309 
310 	return err;
311 }
312 
313 void dpaa2_mac_disconnect(struct dpaa2_mac *mac)
314 {
315 	if (!mac->phylink)
316 		return;
317 
318 	phylink_disconnect_phy(mac->phylink);
319 	phylink_destroy(mac->phylink);
320 	dpaa2_pcs_destroy(mac);
321 }
322 
323 int dpaa2_mac_open(struct dpaa2_mac *mac)
324 {
325 	struct fsl_mc_device *dpmac_dev = mac->mc_dev;
326 	struct net_device *net_dev = mac->net_dev;
327 	int err;
328 
329 	err = dpmac_open(mac->mc_io, 0, dpmac_dev->obj_desc.id,
330 			 &dpmac_dev->mc_handle);
331 	if (err || !dpmac_dev->mc_handle) {
332 		netdev_err(net_dev, "dpmac_open() = %d\n", err);
333 		return -ENODEV;
334 	}
335 
336 	err = dpmac_get_attributes(mac->mc_io, 0, dpmac_dev->mc_handle,
337 				   &mac->attr);
338 	if (err) {
339 		netdev_err(net_dev, "dpmac_get_attributes() = %d\n", err);
340 		goto err_close_dpmac;
341 	}
342 
343 	/* Find the device node representing the MAC device and link the device
344 	 * behind the associated netdev to it.
345 	 */
346 	mac->fw_node = dpaa2_mac_get_node(&mac->mc_dev->dev, mac->attr.id);
347 	net_dev->dev.of_node = to_of_node(mac->fw_node);
348 
349 	return 0;
350 
351 err_close_dpmac:
352 	dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle);
353 	return err;
354 }
355 
356 void dpaa2_mac_close(struct dpaa2_mac *mac)
357 {
358 	struct fsl_mc_device *dpmac_dev = mac->mc_dev;
359 
360 	dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle);
361 	if (mac->fw_node)
362 		fwnode_handle_put(mac->fw_node);
363 }
364 
365 static char dpaa2_mac_ethtool_stats[][ETH_GSTRING_LEN] = {
366 	[DPMAC_CNT_ING_ALL_FRAME]		= "[mac] rx all frames",
367 	[DPMAC_CNT_ING_GOOD_FRAME]		= "[mac] rx frames ok",
368 	[DPMAC_CNT_ING_ERR_FRAME]		= "[mac] rx frame errors",
369 	[DPMAC_CNT_ING_FRAME_DISCARD]		= "[mac] rx frame discards",
370 	[DPMAC_CNT_ING_UCAST_FRAME]		= "[mac] rx u-cast",
371 	[DPMAC_CNT_ING_BCAST_FRAME]		= "[mac] rx b-cast",
372 	[DPMAC_CNT_ING_MCAST_FRAME]		= "[mac] rx m-cast",
373 	[DPMAC_CNT_ING_FRAME_64]		= "[mac] rx 64 bytes",
374 	[DPMAC_CNT_ING_FRAME_127]		= "[mac] rx 65-127 bytes",
375 	[DPMAC_CNT_ING_FRAME_255]		= "[mac] rx 128-255 bytes",
376 	[DPMAC_CNT_ING_FRAME_511]		= "[mac] rx 256-511 bytes",
377 	[DPMAC_CNT_ING_FRAME_1023]		= "[mac] rx 512-1023 bytes",
378 	[DPMAC_CNT_ING_FRAME_1518]		= "[mac] rx 1024-1518 bytes",
379 	[DPMAC_CNT_ING_FRAME_1519_MAX]		= "[mac] rx 1519-max bytes",
380 	[DPMAC_CNT_ING_FRAG]			= "[mac] rx frags",
381 	[DPMAC_CNT_ING_JABBER]			= "[mac] rx jabber",
382 	[DPMAC_CNT_ING_ALIGN_ERR]		= "[mac] rx align errors",
383 	[DPMAC_CNT_ING_OVERSIZED]		= "[mac] rx oversized",
384 	[DPMAC_CNT_ING_VALID_PAUSE_FRAME]	= "[mac] rx pause",
385 	[DPMAC_CNT_ING_BYTE]			= "[mac] rx bytes",
386 	[DPMAC_CNT_EGR_GOOD_FRAME]		= "[mac] tx frames ok",
387 	[DPMAC_CNT_EGR_UCAST_FRAME]		= "[mac] tx u-cast",
388 	[DPMAC_CNT_EGR_MCAST_FRAME]		= "[mac] tx m-cast",
389 	[DPMAC_CNT_EGR_BCAST_FRAME]		= "[mac] tx b-cast",
390 	[DPMAC_CNT_EGR_ERR_FRAME]		= "[mac] tx frame errors",
391 	[DPMAC_CNT_EGR_UNDERSIZED]		= "[mac] tx undersized",
392 	[DPMAC_CNT_EGR_VALID_PAUSE_FRAME]	= "[mac] tx b-pause",
393 	[DPMAC_CNT_EGR_BYTE]			= "[mac] tx bytes",
394 };
395 
396 #define DPAA2_MAC_NUM_STATS	ARRAY_SIZE(dpaa2_mac_ethtool_stats)
397 
398 int dpaa2_mac_get_sset_count(void)
399 {
400 	return DPAA2_MAC_NUM_STATS;
401 }
402 
403 void dpaa2_mac_get_strings(u8 *data)
404 {
405 	u8 *p = data;
406 	int i;
407 
408 	for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) {
409 		strlcpy(p, dpaa2_mac_ethtool_stats[i], ETH_GSTRING_LEN);
410 		p += ETH_GSTRING_LEN;
411 	}
412 }
413 
414 void dpaa2_mac_get_ethtool_stats(struct dpaa2_mac *mac, u64 *data)
415 {
416 	struct fsl_mc_device *dpmac_dev = mac->mc_dev;
417 	int i, err;
418 	u64 value;
419 
420 	for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) {
421 		err = dpmac_get_counter(mac->mc_io, 0, dpmac_dev->mc_handle,
422 					i, &value);
423 		if (err) {
424 			netdev_err_once(mac->net_dev,
425 					"dpmac_get_counter error %d\n", err);
426 			*(data + i) = U64_MAX;
427 			continue;
428 		}
429 		*(data + i) = value;
430 	}
431 }
432