Linux Audio

Check our new training course

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