5d38fe7e91cbb002ae4fcdd1907cbc9e41dd1825
[openwrt/staging/pepe2k.git] /
1 From e0e9ada1df6133513249861c1d91c1dbefd9383b Mon Sep 17 00:00:00 2001
2 From: Christian Marangi <ansuelsmth@gmail.com>
3 Date: Thu, 4 Jan 2024 22:30:39 +0100
4 Subject: [PATCH 2/4] net: phy: at803x: refactor qca808x cable test get status
5 function
6
7 Refactor qca808x cable test get status function to remove code
8 duplication and clean things up.
9
10 The same logic is applied to each pair hence it can be generalized and
11 moved to a common function.
12
13 Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
14 Reviewed-by: Simon Horman <horms@kernel.org>
15 Signed-off-by: David S. Miller <davem@davemloft.net>
16 ---
17 drivers/net/phy/at803x.c | 80 ++++++++++++++++++++++++----------------
18 1 file changed, 49 insertions(+), 31 deletions(-)
19
20 --- a/drivers/net/phy/at803x.c
21 +++ b/drivers/net/phy/at803x.c
22 @@ -2035,10 +2035,43 @@ static int qca808x_cable_test_start(stru
23 return 0;
24 }
25
26 +static int qca808x_cable_test_get_pair_status(struct phy_device *phydev, u8 pair,
27 + u16 status)
28 +{
29 + u16 pair_code;
30 + int length;
31 +
32 + switch (pair) {
33 + case ETHTOOL_A_CABLE_PAIR_A:
34 + pair_code = FIELD_GET(QCA808X_CDT_CODE_PAIR_A, status);
35 + break;
36 + case ETHTOOL_A_CABLE_PAIR_B:
37 + pair_code = FIELD_GET(QCA808X_CDT_CODE_PAIR_B, status);
38 + break;
39 + case ETHTOOL_A_CABLE_PAIR_C:
40 + pair_code = FIELD_GET(QCA808X_CDT_CODE_PAIR_C, status);
41 + break;
42 + case ETHTOOL_A_CABLE_PAIR_D:
43 + pair_code = FIELD_GET(QCA808X_CDT_CODE_PAIR_D, status);
44 + break;
45 + default:
46 + return -EINVAL;
47 + }
48 +
49 + ethnl_cable_test_result(phydev, pair,
50 + qca808x_cable_test_result_trans(pair_code));
51 +
52 + if (qca808x_cdt_fault_length_valid(pair_code)) {
53 + length = qca808x_cdt_fault_length(phydev, pair);
54 + ethnl_cable_test_fault_length(phydev, pair, length);
55 + }
56 +
57 + return 0;
58 +}
59 +
60 static int qca808x_cable_test_get_status(struct phy_device *phydev, bool *finished)
61 {
62 int ret, val;
63 - int pair_a, pair_b, pair_c, pair_d;
64
65 *finished = false;
66
67 @@ -2057,36 +2090,21 @@ static int qca808x_cable_test_get_status
68 if (val < 0)
69 return val;
70
71 - pair_a = FIELD_GET(QCA808X_CDT_CODE_PAIR_A, val);
72 - pair_b = FIELD_GET(QCA808X_CDT_CODE_PAIR_B, val);
73 - pair_c = FIELD_GET(QCA808X_CDT_CODE_PAIR_C, val);
74 - pair_d = FIELD_GET(QCA808X_CDT_CODE_PAIR_D, val);
75 -
76 - ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_A,
77 - qca808x_cable_test_result_trans(pair_a));
78 - ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_B,
79 - qca808x_cable_test_result_trans(pair_b));
80 - ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_C,
81 - qca808x_cable_test_result_trans(pair_c));
82 - ethnl_cable_test_result(phydev, ETHTOOL_A_CABLE_PAIR_D,
83 - qca808x_cable_test_result_trans(pair_d));
84 -
85 - if (qca808x_cdt_fault_length_valid(pair_a)) {
86 - val = qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A);
87 - ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_A, val);
88 - }
89 - if (qca808x_cdt_fault_length_valid(pair_b)) {
90 - val = qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B);
91 - ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_B, val);
92 - }
93 - if (qca808x_cdt_fault_length_valid(pair_c)) {
94 - val = qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C);
95 - ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_C, val);
96 - }
97 - if (qca808x_cdt_fault_length_valid(pair_d)) {
98 - val = qca808x_cdt_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D);
99 - ethnl_cable_test_fault_length(phydev, ETHTOOL_A_CABLE_PAIR_D, val);
100 - }
101 + ret = qca808x_cable_test_get_pair_status(phydev, ETHTOOL_A_CABLE_PAIR_A, val);
102 + if (ret)
103 + return ret;
104 +
105 + ret = qca808x_cable_test_get_pair_status(phydev, ETHTOOL_A_CABLE_PAIR_B, val);
106 + if (ret)
107 + return ret;
108 +
109 + ret = qca808x_cable_test_get_pair_status(phydev, ETHTOOL_A_CABLE_PAIR_C, val);
110 + if (ret)
111 + return ret;
112 +
113 + ret = qca808x_cable_test_get_pair_status(phydev, ETHTOOL_A_CABLE_PAIR_D, val);
114 + if (ret)
115 + return ret;
116
117 *finished = true;
118