c31a47018245ea1b88ed79a1f40ee55c60f29e5d
[openwrt/staging/pepe2k.git] /
1 From 06efc9b8a1360cad83cae6e71558e5458cc1fbf3 Mon Sep 17 00:00:00 2001
2 From: Vladimir Oltean <vladimir.oltean@nxp.com>
3 Date: Tue, 29 Nov 2022 16:12:14 +0200
4 Subject: [PATCH 07/14] net: dpaa2-eth: assign priv->mac after
5 dpaa2_mac_connect() call
6
7 There are 2 requirements for correct code:
8
9 - Any time the driver accesses the priv->mac pointer at runtime, it
10 either holds NULL to indicate a DPNI-DPNI connection (or unconnected
11 DPNI), or a struct dpaa2_mac whose phylink instance was fully
12 initialized (created and connected to the PHY). No changes are made to
13 priv->mac while it is being used. Currently, rtnl_lock() watches over
14 the call to dpaa2_eth_connect_mac(), so it serves the purpose of
15 serializing this with all readers of priv->mac.
16
17 - dpaa2_mac_connect() should run unlocked, because inside it are 2
18 phylink calls with incompatible locking requirements: phylink_create()
19 requires that the rtnl_mutex isn't held, and phylink_fwnode_phy_connect()
20 requires that the rtnl_mutex is held. The only way to solve those
21 contradictory requirements is to let dpaa2_mac_connect() take
22 rtnl_lock() when it needs to.
23
24 To solve both requirements, we need to identify the writer side of the
25 priv->mac pointer, which can be wrapped in a mutex private to the driver
26 in a future patch. The dpaa2_mac_connect() cannot be part of the writer
27 side critical section, because of an AB/BA deadlock with rtnl_lock().
28
29 So the strategy needs to be that where we prepare the DPMAC by calling
30 dpaa2_mac_connect(), and only make priv->mac point to it once it's fully
31 prepared. This ensures that the writer side critical section has the
32 absolute minimum surface it can.
33
34 The reverse strategy is adopted in the dpaa2_eth_disconnect_mac() code
35 path. This makes sure that priv->mac is NULL when we start tearing down
36 the DPMAC that we disconnected from, and concurrent code will simply not
37 see it.
38
39 No locking changes in this patch (concurrent code is still blocked by
40 the rtnl_mutex).
41
42 Signed-off-by: Vladimir Oltean <vladimir.oltean@nxp.com>
43 Reviewed-by: Ioana Ciornei <ioana.ciornei@nxp.com>
44 Tested-by: Ioana Ciornei <ioana.ciornei@nxp.com>
45 Signed-off-by: Paolo Abeni <pabeni@redhat.com>
46 ---
47 .../net/ethernet/freescale/dpaa2/dpaa2-eth.c | 21 +++++++++++--------
48 1 file changed, 12 insertions(+), 9 deletions(-)
49
50 --- a/drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c
51 +++ b/drivers/net/ethernet/freescale/dpaa2/dpaa2-eth.c
52 @@ -4444,9 +4444,8 @@ static int dpaa2_eth_connect_mac(struct
53 err = dpaa2_mac_open(mac);
54 if (err)
55 goto err_free_mac;
56 - priv->mac = mac;
57
58 - if (dpaa2_eth_is_type_phy(priv)) {
59 + if (dpaa2_mac_is_type_phy(mac)) {
60 err = dpaa2_mac_connect(mac);
61 if (err && err != -EPROBE_DEFER)
62 netdev_err(priv->net_dev, "Error connecting to the MAC endpoint: %pe",
63 @@ -4455,11 +4454,12 @@ static int dpaa2_eth_connect_mac(struct
64 goto err_close_mac;
65 }
66
67 + priv->mac = mac;
68 +
69 return 0;
70
71 err_close_mac:
72 dpaa2_mac_close(mac);
73 - priv->mac = NULL;
74 err_free_mac:
75 kfree(mac);
76 return err;
77 @@ -4467,15 +4467,18 @@ err_free_mac:
78
79 static void dpaa2_eth_disconnect_mac(struct dpaa2_eth_priv *priv)
80 {
81 - if (dpaa2_eth_is_type_phy(priv))
82 - dpaa2_mac_disconnect(priv->mac);
83 + struct dpaa2_mac *mac = priv->mac;
84 +
85 + priv->mac = NULL;
86
87 - if (!dpaa2_eth_has_mac(priv))
88 + if (!mac)
89 return;
90
91 - dpaa2_mac_close(priv->mac);
92 - kfree(priv->mac);
93 - priv->mac = NULL;
94 + if (dpaa2_mac_is_type_phy(mac))
95 + dpaa2_mac_disconnect(mac);
96 +
97 + dpaa2_mac_close(mac);
98 + kfree(mac);
99 }
100
101 static irqreturn_t dpni_irq0_handler_thread(int irq_num, void *arg)