xref: /openbmc/linux/drivers/net/ethernet/freescale/dpaa2/dpaa2-eth-dcb.c (revision cbecf716ca618fd44feda6bd9a64a8179d031fc5)
1f395b69fSIoana Ciornei // SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
2f395b69fSIoana Ciornei /* Copyright 2020 NXP */
3f395b69fSIoana Ciornei 
4f395b69fSIoana Ciornei #include "dpaa2-eth.h"
5f395b69fSIoana Ciornei 
dpaa2_eth_dcbnl_ieee_getpfc(struct net_device * net_dev,struct ieee_pfc * pfc)6f395b69fSIoana Ciornei static int dpaa2_eth_dcbnl_ieee_getpfc(struct net_device *net_dev,
7f395b69fSIoana Ciornei 				       struct ieee_pfc *pfc)
8f395b69fSIoana Ciornei {
9f395b69fSIoana Ciornei 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
10f395b69fSIoana Ciornei 
11f395b69fSIoana Ciornei 	if (!(priv->link_state.options & DPNI_LINK_OPT_PFC_PAUSE))
12f395b69fSIoana Ciornei 		return 0;
13f395b69fSIoana Ciornei 
14f395b69fSIoana Ciornei 	memcpy(pfc, &priv->pfc, sizeof(priv->pfc));
15f395b69fSIoana Ciornei 	pfc->pfc_cap = dpaa2_eth_tc_count(priv);
16f395b69fSIoana Ciornei 
17f395b69fSIoana Ciornei 	return 0;
18f395b69fSIoana Ciornei }
19f395b69fSIoana Ciornei 
dpaa2_eth_is_prio_enabled(u8 pfc_en,u8 tc)20*8d138373SIoana Ciornei static inline bool dpaa2_eth_is_prio_enabled(u8 pfc_en, u8 tc)
21f395b69fSIoana Ciornei {
22f395b69fSIoana Ciornei 	return !!(pfc_en & (1 << tc));
23f395b69fSIoana Ciornei }
24f395b69fSIoana Ciornei 
dpaa2_eth_set_pfc_cn(struct dpaa2_eth_priv * priv,u8 pfc_en)25*8d138373SIoana Ciornei static int dpaa2_eth_set_pfc_cn(struct dpaa2_eth_priv *priv, u8 pfc_en)
26f395b69fSIoana Ciornei {
27f395b69fSIoana Ciornei 	struct dpni_congestion_notification_cfg cfg = {0};
28f395b69fSIoana Ciornei 	int i, err;
29f395b69fSIoana Ciornei 
30f395b69fSIoana Ciornei 	cfg.notification_mode = DPNI_CONG_OPT_FLOW_CONTROL;
31f395b69fSIoana Ciornei 	cfg.units = DPNI_CONGESTION_UNIT_FRAMES;
32f395b69fSIoana Ciornei 	cfg.message_iova = 0ULL;
33f395b69fSIoana Ciornei 	cfg.message_ctx = 0ULL;
34f395b69fSIoana Ciornei 
35f395b69fSIoana Ciornei 	for (i = 0; i < dpaa2_eth_tc_count(priv); i++) {
36*8d138373SIoana Ciornei 		if (dpaa2_eth_is_prio_enabled(pfc_en, i)) {
37f395b69fSIoana Ciornei 			cfg.threshold_entry = DPAA2_ETH_CN_THRESH_ENTRY(priv);
38f395b69fSIoana Ciornei 			cfg.threshold_exit = DPAA2_ETH_CN_THRESH_EXIT(priv);
39f395b69fSIoana Ciornei 		} else {
40f395b69fSIoana Ciornei 			/* For priorities not set in the pfc_en mask, we leave
41f395b69fSIoana Ciornei 			 * the congestion thresholds at zero, which effectively
42f395b69fSIoana Ciornei 			 * disables generation of PFC frames for them
43f395b69fSIoana Ciornei 			 */
44f395b69fSIoana Ciornei 			cfg.threshold_entry = 0;
45f395b69fSIoana Ciornei 			cfg.threshold_exit = 0;
46f395b69fSIoana Ciornei 		}
47f395b69fSIoana Ciornei 
48f395b69fSIoana Ciornei 		err = dpni_set_congestion_notification(priv->mc_io, 0,
49f395b69fSIoana Ciornei 						       priv->mc_token,
50f395b69fSIoana Ciornei 						       DPNI_QUEUE_RX, i, &cfg);
51f395b69fSIoana Ciornei 		if (err) {
52f395b69fSIoana Ciornei 			netdev_err(priv->net_dev,
53f395b69fSIoana Ciornei 				   "dpni_set_congestion_notification failed\n");
54f395b69fSIoana Ciornei 			return err;
55f395b69fSIoana Ciornei 		}
56f395b69fSIoana Ciornei 	}
57f395b69fSIoana Ciornei 
58f395b69fSIoana Ciornei 	return 0;
59f395b69fSIoana Ciornei }
60f395b69fSIoana Ciornei 
dpaa2_eth_dcbnl_ieee_setpfc(struct net_device * net_dev,struct ieee_pfc * pfc)61f395b69fSIoana Ciornei static int dpaa2_eth_dcbnl_ieee_setpfc(struct net_device *net_dev,
62f395b69fSIoana Ciornei 				       struct ieee_pfc *pfc)
63f395b69fSIoana Ciornei {
64f395b69fSIoana Ciornei 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
65f395b69fSIoana Ciornei 	struct dpni_link_cfg link_cfg = {0};
6607beb165SIoana Ciornei 	bool tx_pause;
67f395b69fSIoana Ciornei 	int err;
68f395b69fSIoana Ciornei 
69f395b69fSIoana Ciornei 	if (pfc->mbc || pfc->delay)
70f395b69fSIoana Ciornei 		return -EOPNOTSUPP;
71f395b69fSIoana Ciornei 
72f395b69fSIoana Ciornei 	/* If same PFC enabled mask, nothing to do */
73f395b69fSIoana Ciornei 	if (priv->pfc.pfc_en == pfc->pfc_en)
74f395b69fSIoana Ciornei 		return 0;
75f395b69fSIoana Ciornei 
76f395b69fSIoana Ciornei 	/* We allow PFC configuration even if it won't have any effect until
77f395b69fSIoana Ciornei 	 * general pause frames are enabled
78f395b69fSIoana Ciornei 	 */
7907beb165SIoana Ciornei 	tx_pause = dpaa2_eth_tx_pause_enabled(priv->link_state.options);
8007beb165SIoana Ciornei 	if (!dpaa2_eth_rx_pause_enabled(priv->link_state.options) || !tx_pause)
81f395b69fSIoana Ciornei 		netdev_warn(net_dev, "Pause support must be enabled in order for PFC to work!\n");
82f395b69fSIoana Ciornei 
83f395b69fSIoana Ciornei 	link_cfg.rate = priv->link_state.rate;
84f395b69fSIoana Ciornei 	link_cfg.options = priv->link_state.options;
85f395b69fSIoana Ciornei 	if (pfc->pfc_en)
86f395b69fSIoana Ciornei 		link_cfg.options |= DPNI_LINK_OPT_PFC_PAUSE;
87f395b69fSIoana Ciornei 	else
88f395b69fSIoana Ciornei 		link_cfg.options &= ~DPNI_LINK_OPT_PFC_PAUSE;
89f395b69fSIoana Ciornei 	err = dpni_set_link_cfg(priv->mc_io, 0, priv->mc_token, &link_cfg);
90f395b69fSIoana Ciornei 	if (err) {
91f395b69fSIoana Ciornei 		netdev_err(net_dev, "dpni_set_link_cfg failed\n");
92f395b69fSIoana Ciornei 		return err;
93f395b69fSIoana Ciornei 	}
94f395b69fSIoana Ciornei 
95f395b69fSIoana Ciornei 	/* Configure congestion notifications for the enabled priorities */
96*8d138373SIoana Ciornei 	err = dpaa2_eth_set_pfc_cn(priv, pfc->pfc_en);
97f395b69fSIoana Ciornei 	if (err)
98f395b69fSIoana Ciornei 		return err;
99f395b69fSIoana Ciornei 
100f395b69fSIoana Ciornei 	memcpy(&priv->pfc, pfc, sizeof(priv->pfc));
10107beb165SIoana Ciornei 	priv->pfc_enabled = !!pfc->pfc_en;
10207beb165SIoana Ciornei 
10307beb165SIoana Ciornei 	dpaa2_eth_set_rx_taildrop(priv, tx_pause, priv->pfc_enabled);
104f395b69fSIoana Ciornei 
105f395b69fSIoana Ciornei 	return 0;
106f395b69fSIoana Ciornei }
107f395b69fSIoana Ciornei 
dpaa2_eth_dcbnl_getdcbx(struct net_device * net_dev)108f395b69fSIoana Ciornei static u8 dpaa2_eth_dcbnl_getdcbx(struct net_device *net_dev)
109f395b69fSIoana Ciornei {
110f395b69fSIoana Ciornei 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
111f395b69fSIoana Ciornei 
112f395b69fSIoana Ciornei 	return priv->dcbx_mode;
113f395b69fSIoana Ciornei }
114f395b69fSIoana Ciornei 
dpaa2_eth_dcbnl_setdcbx(struct net_device * net_dev,u8 mode)115f395b69fSIoana Ciornei static u8 dpaa2_eth_dcbnl_setdcbx(struct net_device *net_dev, u8 mode)
116f395b69fSIoana Ciornei {
117f395b69fSIoana Ciornei 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
118f395b69fSIoana Ciornei 
119f395b69fSIoana Ciornei 	return (mode != (priv->dcbx_mode)) ? 1 : 0;
120f395b69fSIoana Ciornei }
121f395b69fSIoana Ciornei 
dpaa2_eth_dcbnl_getcap(struct net_device * net_dev,int capid,u8 * cap)122f395b69fSIoana Ciornei static u8 dpaa2_eth_dcbnl_getcap(struct net_device *net_dev, int capid, u8 *cap)
123f395b69fSIoana Ciornei {
124f395b69fSIoana Ciornei 	struct dpaa2_eth_priv *priv = netdev_priv(net_dev);
125f395b69fSIoana Ciornei 
126f395b69fSIoana Ciornei 	switch (capid) {
127f395b69fSIoana Ciornei 	case DCB_CAP_ATTR_PFC:
128f395b69fSIoana Ciornei 		*cap = true;
129f395b69fSIoana Ciornei 		break;
130f395b69fSIoana Ciornei 	case DCB_CAP_ATTR_PFC_TCS:
131f395b69fSIoana Ciornei 		*cap = 1 << (dpaa2_eth_tc_count(priv) - 1);
132f395b69fSIoana Ciornei 		break;
133f395b69fSIoana Ciornei 	case DCB_CAP_ATTR_DCBX:
134f395b69fSIoana Ciornei 		*cap = priv->dcbx_mode;
135f395b69fSIoana Ciornei 		break;
136f395b69fSIoana Ciornei 	default:
137f395b69fSIoana Ciornei 		*cap = false;
138f395b69fSIoana Ciornei 		break;
139f395b69fSIoana Ciornei 	}
140f395b69fSIoana Ciornei 
141f395b69fSIoana Ciornei 	return 0;
142f395b69fSIoana Ciornei }
143f395b69fSIoana Ciornei 
144f395b69fSIoana Ciornei const struct dcbnl_rtnl_ops dpaa2_eth_dcbnl_ops = {
145f395b69fSIoana Ciornei 	.ieee_getpfc	= dpaa2_eth_dcbnl_ieee_getpfc,
146f395b69fSIoana Ciornei 	.ieee_setpfc	= dpaa2_eth_dcbnl_ieee_setpfc,
147f395b69fSIoana Ciornei 	.getdcbx	= dpaa2_eth_dcbnl_getdcbx,
148f395b69fSIoana Ciornei 	.setdcbx	= dpaa2_eth_dcbnl_setdcbx,
149f395b69fSIoana Ciornei 	.getcap		= dpaa2_eth_dcbnl_getcap,
150f395b69fSIoana Ciornei };
151