1 From 80d12452a5f160c39d63efc1be07df36f9d07133 Mon Sep 17 00:00:00 2001
2 From: Vladimir Oltean <vladimir.oltean@nxp.com>
3 Date: Tue, 29 Nov 2022 16:12:20 +0200
4 Subject: [PATCH 13/14] net: dpaa2-switch: serialize changes to priv->mac with
7 The dpaa2-switch driver uses a DPMAC in the same way as the dpaa2-eth
8 driver, so we need to duplicate the locking solution established by the
9 previous change to the switch driver as well.
11 Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
12 Reviewed-by: Ioana Ciornei <ioana.ciornei@nxp.com>
13 Tested-by: Ioana Ciornei <ioana.ciornei@nxp.com>
14 Signed-off-by: Paolo Abeni <pabeni@redhat.com>
16 .../freescale/dpaa2/dpaa2-switch-ethtool.c | 32 +++++++++++++++----
17 .../ethernet/freescale/dpaa2/dpaa2-switch.c | 31 ++++++++++++++++--
18 .../ethernet/freescale/dpaa2/dpaa2-switch.h | 2 ++
19 3 files changed, 55 insertions(+), 10 deletions(-)
21 --- a/drivers/net/ethernet/freescale/dpaa2/dpaa2-switch-ethtool.c
22 +++ b/drivers/net/ethernet/freescale/dpaa2/dpaa2-switch-ethtool.c
23 @@ -60,11 +60,18 @@ dpaa2_switch_get_link_ksettings(struct n
25 struct ethsw_port_priv *port_priv = netdev_priv(netdev);
26 struct dpsw_link_state state = {0};
30 - if (dpaa2_switch_port_is_type_phy(port_priv))
31 - return phylink_ethtool_ksettings_get(port_priv->mac->phylink,
33 + mutex_lock(&port_priv->mac_lock);
35 + if (dpaa2_switch_port_is_type_phy(port_priv)) {
36 + err = phylink_ethtool_ksettings_get(port_priv->mac->phylink,
38 + mutex_unlock(&port_priv->mac_lock);
42 + mutex_unlock(&port_priv->mac_lock);
44 err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
45 port_priv->ethsw_data->dpsw_handle,
46 @@ -99,9 +106,16 @@ dpaa2_switch_set_link_ksettings(struct n
50 - if (dpaa2_switch_port_is_type_phy(port_priv))
51 - return phylink_ethtool_ksettings_set(port_priv->mac->phylink,
53 + mutex_lock(&port_priv->mac_lock);
55 + if (dpaa2_switch_port_is_type_phy(port_priv)) {
56 + err = phylink_ethtool_ksettings_set(port_priv->mac->phylink,
58 + mutex_unlock(&port_priv->mac_lock);
62 + mutex_unlock(&port_priv->mac_lock);
64 /* Interface needs to be down to change link settings */
65 if_running = netif_running(netdev);
66 @@ -189,8 +203,12 @@ static void dpaa2_switch_ethtool_get_sta
67 dpaa2_switch_ethtool_counters[i].name, err);
70 + mutex_lock(&port_priv->mac_lock);
72 if (dpaa2_switch_port_has_mac(port_priv))
73 dpaa2_mac_get_ethtool_stats(port_priv->mac, data + i);
75 + mutex_unlock(&port_priv->mac_lock);
78 const struct ethtool_ops dpaa2_switch_port_ethtool_ops = {
79 --- a/drivers/net/ethernet/freescale/dpaa2/dpaa2-switch.c
80 +++ b/drivers/net/ethernet/freescale/dpaa2/dpaa2-switch.c
81 @@ -603,8 +603,11 @@ static int dpaa2_switch_port_link_state_
83 /* When we manage the MAC/PHY using phylink there is no need
84 * to manually update the netif_carrier.
85 + * We can avoid locking because we are called from the "link changed"
86 + * IRQ handler, which is the same as the "endpoint changed" IRQ handler
87 + * (the writer to port_priv->mac), so we cannot race with it.
89 - if (dpaa2_switch_port_is_type_phy(port_priv))
90 + if (dpaa2_mac_is_type_phy(port_priv->mac))
93 /* Interrupts are received even though no one issued an 'ifconfig up'
94 @@ -684,6 +687,8 @@ static int dpaa2_switch_port_open(struct
95 struct ethsw_core *ethsw = port_priv->ethsw_data;
98 + mutex_lock(&port_priv->mac_lock);
100 if (!dpaa2_switch_port_is_type_phy(port_priv)) {
101 /* Explicitly set carrier off, otherwise
102 * netif_carrier_ok() will return true and cause 'ip link show'
103 @@ -697,6 +702,7 @@ static int dpaa2_switch_port_open(struct
104 port_priv->ethsw_data->dpsw_handle,
107 + mutex_unlock(&port_priv->mac_lock);
108 netdev_err(netdev, "dpsw_if_enable err %d\n", err);
111 @@ -706,6 +712,8 @@ static int dpaa2_switch_port_open(struct
112 if (dpaa2_switch_port_is_type_phy(port_priv))
113 dpaa2_mac_start(port_priv->mac);
115 + mutex_unlock(&port_priv->mac_lock);
120 @@ -715,6 +723,8 @@ static int dpaa2_switch_port_stop(struct
121 struct ethsw_core *ethsw = port_priv->ethsw_data;
124 + mutex_lock(&port_priv->mac_lock);
126 if (dpaa2_switch_port_is_type_phy(port_priv)) {
127 dpaa2_mac_stop(port_priv->mac);
129 @@ -722,6 +732,8 @@ static int dpaa2_switch_port_stop(struct
130 netif_carrier_off(netdev);
133 + mutex_unlock(&port_priv->mac_lock);
135 err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
136 port_priv->ethsw_data->dpsw_handle,
138 @@ -1461,7 +1473,9 @@ static int dpaa2_switch_port_connect_mac
142 + mutex_lock(&port_priv->mac_lock);
143 port_priv->mac = mac;
144 + mutex_unlock(&port_priv->mac_lock);
148 @@ -1474,9 +1488,12 @@ err_free_mac:
150 static void dpaa2_switch_port_disconnect_mac(struct ethsw_port_priv *port_priv)
152 - struct dpaa2_mac *mac = port_priv->mac;
153 + struct dpaa2_mac *mac;
155 + mutex_lock(&port_priv->mac_lock);
156 + mac = port_priv->mac;
157 port_priv->mac = NULL;
158 + mutex_unlock(&port_priv->mac_lock);
162 @@ -1495,6 +1512,7 @@ static irqreturn_t dpaa2_switch_irq0_han
163 struct ethsw_port_priv *port_priv;
168 err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
169 DPSW_IRQ_INDEX_IF, &status);
170 @@ -1513,7 +1531,12 @@ static irqreturn_t dpaa2_switch_irq0_han
172 if (status & DPSW_IRQ_EVENT_ENDPOINT_CHANGED) {
174 - if (dpaa2_switch_port_has_mac(port_priv))
175 + /* We can avoid locking because the "endpoint changed" IRQ
176 + * handler is the only one who changes priv->mac at runtime,
177 + * so we are not racing with anyone.
179 + had_mac = !!port_priv->mac;
181 dpaa2_switch_port_disconnect_mac(port_priv);
183 dpaa2_switch_port_connect_mac(port_priv);
184 @@ -3249,6 +3272,8 @@ static int dpaa2_switch_probe_port(struc
185 port_priv->netdev = port_netdev;
186 port_priv->ethsw_data = ethsw;
188 + mutex_init(&port_priv->mac_lock);
190 port_priv->idx = port_idx;
191 port_priv->stp_state = BR_STATE_FORWARDING;
193 --- a/drivers/net/ethernet/freescale/dpaa2/dpaa2-switch.h
194 +++ b/drivers/net/ethernet/freescale/dpaa2/dpaa2-switch.h
195 @@ -161,6 +161,8 @@ struct ethsw_port_priv {
197 struct dpaa2_switch_filter_block *filter_block;
198 struct dpaa2_mac *mac;
199 + /* Protects against changes to port_priv->mac */
200 + struct mutex mac_lock;