1 // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2 /* Copyright 2019 NXP */
3 
4 #include <linux/acpi.h>
5 #include <linux/pcs-lynx.h>
6 #include <linux/phy/phy.h>
7 #include <linux/property.h>
8 
9 #include "dpaa2-eth.h"
10 #include "dpaa2-mac.h"
11 
12 #define phylink_to_dpaa2_mac(config) \
13 	container_of((config), struct dpaa2_mac, phylink_config)
14 
15 #define DPMAC_PROTOCOL_CHANGE_VER_MAJOR		4
16 #define DPMAC_PROTOCOL_CHANGE_VER_MINOR		8
17 
18 #define DPAA2_MAC_FEATURE_PROTOCOL_CHANGE	BIT(0)
19 
20 static int dpaa2_mac_cmp_ver(struct dpaa2_mac *mac,
21 			     u16 ver_major, u16 ver_minor)
22 {
23 	if (mac->ver_major == ver_major)
24 		return mac->ver_minor - ver_minor;
25 	return mac->ver_major - ver_major;
26 }
27 
28 static void dpaa2_mac_detect_features(struct dpaa2_mac *mac)
29 {
30 	mac->features = 0;
31 
32 	if (dpaa2_mac_cmp_ver(mac, DPMAC_PROTOCOL_CHANGE_VER_MAJOR,
33 			      DPMAC_PROTOCOL_CHANGE_VER_MINOR) >= 0)
34 		mac->features |= DPAA2_MAC_FEATURE_PROTOCOL_CHANGE;
35 }
36 
37 static int phy_mode(enum dpmac_eth_if eth_if, phy_interface_t *if_mode)
38 {
39 	*if_mode = PHY_INTERFACE_MODE_NA;
40 
41 	switch (eth_if) {
42 	case DPMAC_ETH_IF_RGMII:
43 		*if_mode = PHY_INTERFACE_MODE_RGMII;
44 		break;
45 	case DPMAC_ETH_IF_USXGMII:
46 		*if_mode = PHY_INTERFACE_MODE_USXGMII;
47 		break;
48 	case DPMAC_ETH_IF_QSGMII:
49 		*if_mode = PHY_INTERFACE_MODE_QSGMII;
50 		break;
51 	case DPMAC_ETH_IF_SGMII:
52 		*if_mode = PHY_INTERFACE_MODE_SGMII;
53 		break;
54 	case DPMAC_ETH_IF_XFI:
55 		*if_mode = PHY_INTERFACE_MODE_10GBASER;
56 		break;
57 	default:
58 		return -EINVAL;
59 	}
60 
61 	return 0;
62 }
63 
64 static enum dpmac_eth_if dpmac_eth_if_mode(phy_interface_t if_mode)
65 {
66 	switch (if_mode) {
67 	case PHY_INTERFACE_MODE_RGMII:
68 	case PHY_INTERFACE_MODE_RGMII_ID:
69 	case PHY_INTERFACE_MODE_RGMII_RXID:
70 	case PHY_INTERFACE_MODE_RGMII_TXID:
71 		return DPMAC_ETH_IF_RGMII;
72 	case PHY_INTERFACE_MODE_USXGMII:
73 		return DPMAC_ETH_IF_USXGMII;
74 	case PHY_INTERFACE_MODE_QSGMII:
75 		return DPMAC_ETH_IF_QSGMII;
76 	case PHY_INTERFACE_MODE_SGMII:
77 		return DPMAC_ETH_IF_SGMII;
78 	case PHY_INTERFACE_MODE_10GBASER:
79 		return DPMAC_ETH_IF_XFI;
80 	case PHY_INTERFACE_MODE_1000BASEX:
81 		return DPMAC_ETH_IF_1000BASEX;
82 	default:
83 		return DPMAC_ETH_IF_MII;
84 	}
85 }
86 
87 static struct fwnode_handle *dpaa2_mac_get_node(struct device *dev,
88 						u16 dpmac_id)
89 {
90 	struct fwnode_handle *fwnode, *parent = NULL, *child  = NULL;
91 	struct device_node *dpmacs = NULL;
92 	int err;
93 	u32 id;
94 
95 	fwnode = dev_fwnode(dev->parent);
96 	if (is_of_node(fwnode)) {
97 		dpmacs = of_find_node_by_name(NULL, "dpmacs");
98 		if (!dpmacs)
99 			return NULL;
100 		parent = of_fwnode_handle(dpmacs);
101 	} else if (is_acpi_node(fwnode)) {
102 		parent = fwnode;
103 	} else {
104 		/* The root dprc device didn't yet get to finalize it's probe,
105 		 * thus the fwnode field is not yet set. Defer probe if we are
106 		 * facing this situation.
107 		 */
108 		return ERR_PTR(-EPROBE_DEFER);
109 	}
110 
111 	if (!parent)
112 		return NULL;
113 
114 	fwnode_for_each_child_node(parent, child) {
115 		err = -EINVAL;
116 		if (is_acpi_device_node(child))
117 			err = acpi_get_local_address(ACPI_HANDLE_FWNODE(child), &id);
118 		else if (is_of_node(child))
119 			err = of_property_read_u32(to_of_node(child), "reg", &id);
120 		if (err)
121 			continue;
122 
123 		if (id == dpmac_id) {
124 			of_node_put(dpmacs);
125 			return child;
126 		}
127 	}
128 	of_node_put(dpmacs);
129 	return NULL;
130 }
131 
132 static int dpaa2_mac_get_if_mode(struct fwnode_handle *dpmac_node,
133 				 struct dpmac_attr attr)
134 {
135 	phy_interface_t if_mode;
136 	int err;
137 
138 	err = fwnode_get_phy_mode(dpmac_node);
139 	if (err > 0)
140 		return err;
141 
142 	err = phy_mode(attr.eth_if, &if_mode);
143 	if (!err)
144 		return if_mode;
145 
146 	return err;
147 }
148 
149 static struct phylink_pcs *dpaa2_mac_select_pcs(struct phylink_config *config,
150 						phy_interface_t interface)
151 {
152 	struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config);
153 
154 	return mac->pcs;
155 }
156 
157 static void dpaa2_mac_config(struct phylink_config *config, unsigned int mode,
158 			     const struct phylink_link_state *state)
159 {
160 	struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config);
161 	struct dpmac_link_state *dpmac_state = &mac->state;
162 	int err;
163 
164 	if (state->an_enabled)
165 		dpmac_state->options |= DPMAC_LINK_OPT_AUTONEG;
166 	else
167 		dpmac_state->options &= ~DPMAC_LINK_OPT_AUTONEG;
168 
169 	err = dpmac_set_link_state(mac->mc_io, 0,
170 				   mac->mc_dev->mc_handle, dpmac_state);
171 	if (err)
172 		netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n",
173 			   __func__, err);
174 
175 	if (!mac->serdes_phy)
176 		return;
177 
178 	/* This happens only if we support changing of protocol at runtime */
179 	err = dpmac_set_protocol(mac->mc_io, 0, mac->mc_dev->mc_handle,
180 				 dpmac_eth_if_mode(state->interface));
181 	if (err)
182 		netdev_err(mac->net_dev,  "dpmac_set_protocol() = %d\n", err);
183 
184 	err = phy_set_mode_ext(mac->serdes_phy, PHY_MODE_ETHERNET, state->interface);
185 	if (err)
186 		netdev_err(mac->net_dev, "phy_set_mode_ext() = %d\n", err);
187 }
188 
189 static void dpaa2_mac_link_up(struct phylink_config *config,
190 			      struct phy_device *phy,
191 			      unsigned int mode, phy_interface_t interface,
192 			      int speed, int duplex,
193 			      bool tx_pause, bool rx_pause)
194 {
195 	struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config);
196 	struct dpmac_link_state *dpmac_state = &mac->state;
197 	int err;
198 
199 	dpmac_state->up = 1;
200 
201 	dpmac_state->rate = speed;
202 
203 	if (duplex == DUPLEX_HALF)
204 		dpmac_state->options |= DPMAC_LINK_OPT_HALF_DUPLEX;
205 	else if (duplex == DUPLEX_FULL)
206 		dpmac_state->options &= ~DPMAC_LINK_OPT_HALF_DUPLEX;
207 
208 	if (rx_pause)
209 		dpmac_state->options |= DPMAC_LINK_OPT_PAUSE;
210 	else
211 		dpmac_state->options &= ~DPMAC_LINK_OPT_PAUSE;
212 
213 	if (rx_pause ^ tx_pause)
214 		dpmac_state->options |= DPMAC_LINK_OPT_ASYM_PAUSE;
215 	else
216 		dpmac_state->options &= ~DPMAC_LINK_OPT_ASYM_PAUSE;
217 
218 	err = dpmac_set_link_state(mac->mc_io, 0,
219 				   mac->mc_dev->mc_handle, dpmac_state);
220 	if (err)
221 		netdev_err(mac->net_dev, "%s: dpmac_set_link_state() = %d\n",
222 			   __func__, err);
223 }
224 
225 static void dpaa2_mac_link_down(struct phylink_config *config,
226 				unsigned int mode,
227 				phy_interface_t interface)
228 {
229 	struct dpaa2_mac *mac = phylink_to_dpaa2_mac(config);
230 	struct dpmac_link_state *dpmac_state = &mac->state;
231 	int err;
232 
233 	dpmac_state->up = 0;
234 	err = dpmac_set_link_state(mac->mc_io, 0,
235 				   mac->mc_dev->mc_handle, dpmac_state);
236 	if (err)
237 		netdev_err(mac->net_dev, "dpmac_set_link_state() = %d\n", err);
238 }
239 
240 static const struct phylink_mac_ops dpaa2_mac_phylink_ops = {
241 	.validate = phylink_generic_validate,
242 	.mac_select_pcs = dpaa2_mac_select_pcs,
243 	.mac_config = dpaa2_mac_config,
244 	.mac_link_up = dpaa2_mac_link_up,
245 	.mac_link_down = dpaa2_mac_link_down,
246 };
247 
248 static int dpaa2_pcs_create(struct dpaa2_mac *mac,
249 			    struct fwnode_handle *dpmac_node,
250 			    int id)
251 {
252 	struct mdio_device *mdiodev;
253 	struct fwnode_handle *node;
254 
255 	node = fwnode_find_reference(dpmac_node, "pcs-handle", 0);
256 	if (IS_ERR(node)) {
257 		/* do not error out on old DTS files */
258 		netdev_warn(mac->net_dev, "pcs-handle node not found\n");
259 		return 0;
260 	}
261 
262 	if (!fwnode_device_is_available(node)) {
263 		netdev_err(mac->net_dev, "pcs-handle node not available\n");
264 		fwnode_handle_put(node);
265 		return -ENODEV;
266 	}
267 
268 	mdiodev = fwnode_mdio_find_device(node);
269 	fwnode_handle_put(node);
270 	if (!mdiodev)
271 		return -EPROBE_DEFER;
272 
273 	mac->pcs = lynx_pcs_create(mdiodev);
274 	if (!mac->pcs) {
275 		netdev_err(mac->net_dev, "lynx_pcs_create() failed\n");
276 		put_device(&mdiodev->dev);
277 		return -ENOMEM;
278 	}
279 
280 	return 0;
281 }
282 
283 static void dpaa2_pcs_destroy(struct dpaa2_mac *mac)
284 {
285 	struct phylink_pcs *phylink_pcs = mac->pcs;
286 
287 	if (phylink_pcs) {
288 		struct mdio_device *mdio = lynx_get_mdio_device(phylink_pcs);
289 		struct device *dev = &mdio->dev;
290 
291 		lynx_pcs_destroy(phylink_pcs);
292 		put_device(dev);
293 		mac->pcs = NULL;
294 	}
295 }
296 
297 static void dpaa2_mac_set_supported_interfaces(struct dpaa2_mac *mac)
298 {
299 	int intf, err;
300 
301 	/* We support the current interface mode, and if we have a PCS
302 	 * similar interface modes that do not require the SerDes lane to be
303 	 * reconfigured.
304 	 */
305 	__set_bit(mac->if_mode, mac->phylink_config.supported_interfaces);
306 	if (mac->pcs) {
307 		switch (mac->if_mode) {
308 		case PHY_INTERFACE_MODE_1000BASEX:
309 		case PHY_INTERFACE_MODE_SGMII:
310 			__set_bit(PHY_INTERFACE_MODE_1000BASEX,
311 				  mac->phylink_config.supported_interfaces);
312 			__set_bit(PHY_INTERFACE_MODE_SGMII,
313 				  mac->phylink_config.supported_interfaces);
314 			break;
315 
316 		default:
317 			break;
318 		}
319 	}
320 
321 	if (!mac->serdes_phy)
322 		return;
323 
324 	/* In case we have access to the SerDes phy/lane, then ask the SerDes
325 	 * driver what interfaces are supported based on the current PLL
326 	 * configuration.
327 	 */
328 	for (intf = 0; intf < PHY_INTERFACE_MODE_MAX; intf++) {
329 		if (intf == PHY_INTERFACE_MODE_NA)
330 			continue;
331 
332 		err = phy_validate(mac->serdes_phy, PHY_MODE_ETHERNET, intf, NULL);
333 		if (err)
334 			continue;
335 
336 		__set_bit(intf, mac->phylink_config.supported_interfaces);
337 	}
338 }
339 
340 void dpaa2_mac_start(struct dpaa2_mac *mac)
341 {
342 	if (mac->serdes_phy)
343 		phy_power_on(mac->serdes_phy);
344 }
345 
346 void dpaa2_mac_stop(struct dpaa2_mac *mac)
347 {
348 	if (mac->serdes_phy)
349 		phy_power_off(mac->serdes_phy);
350 }
351 
352 int dpaa2_mac_connect(struct dpaa2_mac *mac)
353 {
354 	struct net_device *net_dev = mac->net_dev;
355 	struct fwnode_handle *dpmac_node;
356 	struct phy *serdes_phy = NULL;
357 	struct phylink *phylink;
358 	int err;
359 
360 	mac->if_link_type = mac->attr.link_type;
361 
362 	dpmac_node = mac->fw_node;
363 	if (!dpmac_node) {
364 		netdev_err(net_dev, "No dpmac@%d node found.\n", mac->attr.id);
365 		return -ENODEV;
366 	}
367 
368 	err = dpaa2_mac_get_if_mode(dpmac_node, mac->attr);
369 	if (err < 0)
370 		return -EINVAL;
371 	mac->if_mode = err;
372 
373 	if (mac->features & DPAA2_MAC_FEATURE_PROTOCOL_CHANGE &&
374 	    !phy_interface_mode_is_rgmii(mac->if_mode) &&
375 	    is_of_node(dpmac_node)) {
376 		serdes_phy = of_phy_get(to_of_node(dpmac_node), NULL);
377 
378 		if (serdes_phy == ERR_PTR(-ENODEV))
379 			serdes_phy = NULL;
380 		else if (IS_ERR(serdes_phy))
381 			return PTR_ERR(serdes_phy);
382 		else
383 			phy_init(serdes_phy);
384 	}
385 	mac->serdes_phy = serdes_phy;
386 
387 	/* The MAC does not have the capability to add RGMII delays so
388 	 * error out if the interface mode requests them and there is no PHY
389 	 * to act upon them
390 	 */
391 	if (of_phy_is_fixed_link(to_of_node(dpmac_node)) &&
392 	    (mac->if_mode == PHY_INTERFACE_MODE_RGMII_ID ||
393 	     mac->if_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
394 	     mac->if_mode == PHY_INTERFACE_MODE_RGMII_TXID)) {
395 		netdev_err(net_dev, "RGMII delay not supported\n");
396 		return -EINVAL;
397 	}
398 
399 	if ((mac->attr.link_type == DPMAC_LINK_TYPE_PHY &&
400 	     mac->attr.eth_if != DPMAC_ETH_IF_RGMII) ||
401 	    mac->attr.link_type == DPMAC_LINK_TYPE_BACKPLANE) {
402 		err = dpaa2_pcs_create(mac, dpmac_node, mac->attr.id);
403 		if (err)
404 			return err;
405 	}
406 
407 	memset(&mac->phylink_config, 0, sizeof(mac->phylink_config));
408 	mac->phylink_config.dev = &net_dev->dev;
409 	mac->phylink_config.type = PHYLINK_NETDEV;
410 
411 	mac->phylink_config.mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE |
412 		MAC_10FD | MAC_100FD | MAC_1000FD | MAC_2500FD | MAC_5000FD |
413 		MAC_10000FD;
414 
415 	dpaa2_mac_set_supported_interfaces(mac);
416 
417 	phylink = phylink_create(&mac->phylink_config,
418 				 dpmac_node, mac->if_mode,
419 				 &dpaa2_mac_phylink_ops);
420 	if (IS_ERR(phylink)) {
421 		err = PTR_ERR(phylink);
422 		goto err_pcs_destroy;
423 	}
424 	mac->phylink = phylink;
425 
426 	err = phylink_fwnode_phy_connect(mac->phylink, dpmac_node, 0);
427 	if (err) {
428 		netdev_err(net_dev, "phylink_fwnode_phy_connect() = %d\n", err);
429 		goto err_phylink_destroy;
430 	}
431 
432 	return 0;
433 
434 err_phylink_destroy:
435 	phylink_destroy(mac->phylink);
436 err_pcs_destroy:
437 	dpaa2_pcs_destroy(mac);
438 
439 	return err;
440 }
441 
442 void dpaa2_mac_disconnect(struct dpaa2_mac *mac)
443 {
444 	if (!mac->phylink)
445 		return;
446 
447 	phylink_disconnect_phy(mac->phylink);
448 	phylink_destroy(mac->phylink);
449 	dpaa2_pcs_destroy(mac);
450 	of_phy_put(mac->serdes_phy);
451 	mac->serdes_phy = NULL;
452 }
453 
454 int dpaa2_mac_open(struct dpaa2_mac *mac)
455 {
456 	struct fsl_mc_device *dpmac_dev = mac->mc_dev;
457 	struct net_device *net_dev = mac->net_dev;
458 	struct fwnode_handle *fw_node;
459 	int err;
460 
461 	err = dpmac_open(mac->mc_io, 0, dpmac_dev->obj_desc.id,
462 			 &dpmac_dev->mc_handle);
463 	if (err || !dpmac_dev->mc_handle) {
464 		netdev_err(net_dev, "dpmac_open() = %d\n", err);
465 		return -ENODEV;
466 	}
467 
468 	err = dpmac_get_attributes(mac->mc_io, 0, dpmac_dev->mc_handle,
469 				   &mac->attr);
470 	if (err) {
471 		netdev_err(net_dev, "dpmac_get_attributes() = %d\n", err);
472 		goto err_close_dpmac;
473 	}
474 
475 	err = dpmac_get_api_version(mac->mc_io, 0, &mac->ver_major, &mac->ver_minor);
476 	if (err) {
477 		netdev_err(net_dev, "dpmac_get_api_version() = %d\n", err);
478 		goto err_close_dpmac;
479 	}
480 
481 	dpaa2_mac_detect_features(mac);
482 
483 	/* Find the device node representing the MAC device and link the device
484 	 * behind the associated netdev to it.
485 	 */
486 	fw_node = dpaa2_mac_get_node(&mac->mc_dev->dev, mac->attr.id);
487 	if (IS_ERR(fw_node)) {
488 		err = PTR_ERR(fw_node);
489 		goto err_close_dpmac;
490 	}
491 
492 	mac->fw_node = fw_node;
493 	net_dev->dev.of_node = to_of_node(mac->fw_node);
494 
495 	return 0;
496 
497 err_close_dpmac:
498 	dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle);
499 	return err;
500 }
501 
502 void dpaa2_mac_close(struct dpaa2_mac *mac)
503 {
504 	struct fsl_mc_device *dpmac_dev = mac->mc_dev;
505 
506 	dpmac_close(mac->mc_io, 0, dpmac_dev->mc_handle);
507 	if (mac->fw_node)
508 		fwnode_handle_put(mac->fw_node);
509 }
510 
511 static char dpaa2_mac_ethtool_stats[][ETH_GSTRING_LEN] = {
512 	[DPMAC_CNT_ING_ALL_FRAME]		= "[mac] rx all frames",
513 	[DPMAC_CNT_ING_GOOD_FRAME]		= "[mac] rx frames ok",
514 	[DPMAC_CNT_ING_ERR_FRAME]		= "[mac] rx frame errors",
515 	[DPMAC_CNT_ING_FRAME_DISCARD]		= "[mac] rx frame discards",
516 	[DPMAC_CNT_ING_UCAST_FRAME]		= "[mac] rx u-cast",
517 	[DPMAC_CNT_ING_BCAST_FRAME]		= "[mac] rx b-cast",
518 	[DPMAC_CNT_ING_MCAST_FRAME]		= "[mac] rx m-cast",
519 	[DPMAC_CNT_ING_FRAME_64]		= "[mac] rx 64 bytes",
520 	[DPMAC_CNT_ING_FRAME_127]		= "[mac] rx 65-127 bytes",
521 	[DPMAC_CNT_ING_FRAME_255]		= "[mac] rx 128-255 bytes",
522 	[DPMAC_CNT_ING_FRAME_511]		= "[mac] rx 256-511 bytes",
523 	[DPMAC_CNT_ING_FRAME_1023]		= "[mac] rx 512-1023 bytes",
524 	[DPMAC_CNT_ING_FRAME_1518]		= "[mac] rx 1024-1518 bytes",
525 	[DPMAC_CNT_ING_FRAME_1519_MAX]		= "[mac] rx 1519-max bytes",
526 	[DPMAC_CNT_ING_FRAG]			= "[mac] rx frags",
527 	[DPMAC_CNT_ING_JABBER]			= "[mac] rx jabber",
528 	[DPMAC_CNT_ING_ALIGN_ERR]		= "[mac] rx align errors",
529 	[DPMAC_CNT_ING_OVERSIZED]		= "[mac] rx oversized",
530 	[DPMAC_CNT_ING_VALID_PAUSE_FRAME]	= "[mac] rx pause",
531 	[DPMAC_CNT_ING_BYTE]			= "[mac] rx bytes",
532 	[DPMAC_CNT_EGR_GOOD_FRAME]		= "[mac] tx frames ok",
533 	[DPMAC_CNT_EGR_UCAST_FRAME]		= "[mac] tx u-cast",
534 	[DPMAC_CNT_EGR_MCAST_FRAME]		= "[mac] tx m-cast",
535 	[DPMAC_CNT_EGR_BCAST_FRAME]		= "[mac] tx b-cast",
536 	[DPMAC_CNT_EGR_ERR_FRAME]		= "[mac] tx frame errors",
537 	[DPMAC_CNT_EGR_UNDERSIZED]		= "[mac] tx undersized",
538 	[DPMAC_CNT_EGR_VALID_PAUSE_FRAME]	= "[mac] tx b-pause",
539 	[DPMAC_CNT_EGR_BYTE]			= "[mac] tx bytes",
540 };
541 
542 #define DPAA2_MAC_NUM_STATS	ARRAY_SIZE(dpaa2_mac_ethtool_stats)
543 
544 int dpaa2_mac_get_sset_count(void)
545 {
546 	return DPAA2_MAC_NUM_STATS;
547 }
548 
549 void dpaa2_mac_get_strings(u8 *data)
550 {
551 	u8 *p = data;
552 	int i;
553 
554 	for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) {
555 		strlcpy(p, dpaa2_mac_ethtool_stats[i], ETH_GSTRING_LEN);
556 		p += ETH_GSTRING_LEN;
557 	}
558 }
559 
560 void dpaa2_mac_get_ethtool_stats(struct dpaa2_mac *mac, u64 *data)
561 {
562 	struct fsl_mc_device *dpmac_dev = mac->mc_dev;
563 	int i, err;
564 	u64 value;
565 
566 	for (i = 0; i < DPAA2_MAC_NUM_STATS; i++) {
567 		err = dpmac_get_counter(mac->mc_io, 0, dpmac_dev->mc_handle,
568 					i, &value);
569 		if (err) {
570 			netdev_err_once(mac->net_dev,
571 					"dpmac_get_counter error %d\n", err);
572 			*(data + i) = U64_MAX;
573 			continue;
574 		}
575 		*(data + i) = value;
576 	}
577 }
578