1 From 2cd5485663847d468dc207b3ff85fb1fab44d97f Mon Sep 17 00:00:00 2001
2 From: Ansuel Smith <ansuelsmth@gmail.com>
3 Date: Wed, 2 Feb 2022 01:03:31 +0100
4 Subject: [PATCH 12/16] net: dsa: qca8k: add support for phy read/write with
7 Use mgmt Ethernet also for phy read/write if availabale. Use a different
8 seq number to make sure we receive the correct packet.
9 On any error, we fallback to the legacy mdio read/write.
11 Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
12 Signed-off-by: David S. Miller <davem@davemloft.net>
14 drivers/net/dsa/qca8k.c | 216 ++++++++++++++++++++++++++++++++++++++++
15 drivers/net/dsa/qca8k.h | 1 +
16 2 files changed, 217 insertions(+)
18 --- a/drivers/net/dsa/qca8k.c
19 +++ b/drivers/net/dsa/qca8k.c
20 @@ -867,6 +867,199 @@ qca8k_port_set_status(struct qca8k_priv
21 regmap_clear_bits(priv->regmap, QCA8K_REG_PORT_STATUS(port), mask);
25 +qca8k_phy_eth_busy_wait(struct qca8k_mgmt_eth_data *mgmt_eth_data,
26 + struct sk_buff *read_skb, u32 *val)
28 + struct sk_buff *skb = skb_copy(read_skb, GFP_KERNEL);
32 + reinit_completion(&mgmt_eth_data->rw_done);
34 + /* Increment seq_num and set it in the copy pkt */
35 + mgmt_eth_data->seq++;
36 + qca8k_mdio_header_fill_seq_num(skb, mgmt_eth_data->seq);
37 + mgmt_eth_data->ack = false;
39 + dev_queue_xmit(skb);
41 + ret = wait_for_completion_timeout(&mgmt_eth_data->rw_done,
42 + QCA8K_ETHERNET_TIMEOUT);
44 + ack = mgmt_eth_data->ack;
52 + *val = mgmt_eth_data->data[0];
58 +qca8k_phy_eth_command(struct qca8k_priv *priv, bool read, int phy,
59 + int regnum, u16 data)
61 + struct sk_buff *write_skb, *clear_skb, *read_skb;
62 + struct qca8k_mgmt_eth_data *mgmt_eth_data;
63 + u32 write_val, clear_val = 0, val;
64 + struct net_device *mgmt_master;
68 + if (regnum >= QCA8K_MDIO_MASTER_MAX_REG)
71 + mgmt_eth_data = &priv->mgmt_eth_data;
73 + write_val = QCA8K_MDIO_MASTER_BUSY | QCA8K_MDIO_MASTER_EN |
74 + QCA8K_MDIO_MASTER_PHY_ADDR(phy) |
75 + QCA8K_MDIO_MASTER_REG_ADDR(regnum);
78 + write_val |= QCA8K_MDIO_MASTER_READ;
80 + write_val |= QCA8K_MDIO_MASTER_WRITE;
81 + write_val |= QCA8K_MDIO_MASTER_DATA(data);
84 + /* Prealloc all the needed skb before the lock */
85 + write_skb = qca8k_alloc_mdio_header(MDIO_WRITE, QCA8K_MDIO_MASTER_CTRL,
86 + &write_val, QCA8K_ETHERNET_PHY_PRIORITY);
90 + clear_skb = qca8k_alloc_mdio_header(MDIO_WRITE, QCA8K_MDIO_MASTER_CTRL,
91 + &clear_val, QCA8K_ETHERNET_PHY_PRIORITY);
97 + read_skb = qca8k_alloc_mdio_header(MDIO_READ, QCA8K_MDIO_MASTER_CTRL,
98 + &clear_val, QCA8K_ETHERNET_PHY_PRIORITY);
104 + /* Actually start the request:
105 + * 1. Send mdio master packet
106 + * 2. Busy Wait for mdio master command
107 + * 3. Get the data if we are reading
108 + * 4. Reset the mdio master (even with error)
110 + mutex_lock(&mgmt_eth_data->mutex);
112 + /* Check if mgmt_master is operational */
113 + mgmt_master = priv->mgmt_master;
114 + if (!mgmt_master) {
115 + mutex_unlock(&mgmt_eth_data->mutex);
117 + goto err_mgmt_master;
120 + read_skb->dev = mgmt_master;
121 + clear_skb->dev = mgmt_master;
122 + write_skb->dev = mgmt_master;
124 + reinit_completion(&mgmt_eth_data->rw_done);
126 + /* Increment seq_num and set it in the write pkt */
127 + mgmt_eth_data->seq++;
128 + qca8k_mdio_header_fill_seq_num(write_skb, mgmt_eth_data->seq);
129 + mgmt_eth_data->ack = false;
131 + dev_queue_xmit(write_skb);
133 + ret = wait_for_completion_timeout(&mgmt_eth_data->rw_done,
134 + QCA8K_ETHERNET_TIMEOUT);
136 + ack = mgmt_eth_data->ack;
140 + kfree_skb(read_skb);
146 + kfree_skb(read_skb);
150 + ret = read_poll_timeout(qca8k_phy_eth_busy_wait, ret1,
151 + !(val & QCA8K_MDIO_MASTER_BUSY), 0,
152 + QCA8K_BUSY_WAIT_TIMEOUT * USEC_PER_MSEC, false,
153 + mgmt_eth_data, read_skb, &val);
155 + if (ret < 0 && ret1 < 0) {
161 + reinit_completion(&mgmt_eth_data->rw_done);
163 + /* Increment seq_num and set it in the read pkt */
164 + mgmt_eth_data->seq++;
165 + qca8k_mdio_header_fill_seq_num(read_skb, mgmt_eth_data->seq);
166 + mgmt_eth_data->ack = false;
168 + dev_queue_xmit(read_skb);
170 + ret = wait_for_completion_timeout(&mgmt_eth_data->rw_done,
171 + QCA8K_ETHERNET_TIMEOUT);
173 + ack = mgmt_eth_data->ack;
185 + ret = mgmt_eth_data->data[0] & QCA8K_MDIO_MASTER_DATA_MASK;
187 + kfree_skb(read_skb);
190 + reinit_completion(&mgmt_eth_data->rw_done);
192 + /* Increment seq_num and set it in the clear pkt */
193 + mgmt_eth_data->seq++;
194 + qca8k_mdio_header_fill_seq_num(clear_skb, mgmt_eth_data->seq);
195 + mgmt_eth_data->ack = false;
197 + dev_queue_xmit(clear_skb);
199 + wait_for_completion_timeout(&mgmt_eth_data->rw_done,
200 + QCA8K_ETHERNET_TIMEOUT);
202 + mutex_unlock(&mgmt_eth_data->mutex);
206 + /* Error handling before lock */
208 + kfree_skb(read_skb);
210 + kfree_skb(clear_skb);
212 + kfree_skb(write_skb);
218 qca8k_port_to_phy(int port)
220 @@ -989,6 +1182,12 @@ qca8k_internal_mdio_write(struct mii_bus
222 struct qca8k_priv *priv = slave_bus->priv;
223 struct mii_bus *bus = priv->bus;
226 + /* Use mdio Ethernet when available, fallback to legacy one on error */
227 + ret = qca8k_phy_eth_command(priv, false, phy, regnum, data);
231 return qca8k_mdio_write(bus, phy, regnum, data);
233 @@ -998,6 +1197,12 @@ qca8k_internal_mdio_read(struct mii_bus
235 struct qca8k_priv *priv = slave_bus->priv;
236 struct mii_bus *bus = priv->bus;
239 + /* Use mdio Ethernet when available, fallback to legacy one on error */
240 + ret = qca8k_phy_eth_command(priv, true, phy, regnum, 0);
244 return qca8k_mdio_read(bus, phy, regnum);
246 @@ -1006,6 +1211,7 @@ static int
247 qca8k_phy_write(struct dsa_switch *ds, int port, int regnum, u16 data)
249 struct qca8k_priv *priv = ds->priv;
252 /* Check if the legacy mapping should be used and the
253 * port is not correctly mapped to the right PHY in the
254 @@ -1014,6 +1220,11 @@ qca8k_phy_write(struct dsa_switch *ds, i
255 if (priv->legacy_phy_port_mapping)
256 port = qca8k_port_to_phy(port) % PHY_MAX_ADDR;
258 + /* Use mdio Ethernet when available, fallback to legacy one on error */
259 + ret = qca8k_phy_eth_command(priv, false, port, regnum, 0);
263 return qca8k_mdio_write(priv->bus, port, regnum, data);
266 @@ -1030,6 +1241,11 @@ qca8k_phy_read(struct dsa_switch *ds, in
267 if (priv->legacy_phy_port_mapping)
268 port = qca8k_port_to_phy(port) % PHY_MAX_ADDR;
270 + /* Use mdio Ethernet when available, fallback to legacy one on error */
271 + ret = qca8k_phy_eth_command(priv, true, port, regnum, 0);
275 ret = qca8k_mdio_read(priv->bus, port, regnum);
278 --- a/drivers/net/dsa/qca8k.h
279 +++ b/drivers/net/dsa/qca8k.h
281 #include <linux/dsa/tag_qca.h>
283 #define QCA8K_ETHERNET_MDIO_PRIORITY 7
284 +#define QCA8K_ETHERNET_PHY_PRIORITY 6
285 #define QCA8K_ETHERNET_TIMEOUT 100
287 #define QCA8K_NUM_PORTS 7