Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | |
| 3 | #include "netlink.h" |
| 4 | #include "common.h" |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 5 | #include <linux/phy.h> |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 6 | |
| 7 | struct linkstate_req_info { |
| 8 | struct ethnl_req_info base; |
| 9 | }; |
| 10 | |
| 11 | struct linkstate_reply_data { |
Amit Cohen | ecc31c6 | 2020-06-29 23:46:16 +0300 | [diff] [blame] | 12 | struct ethnl_reply_data base; |
| 13 | int link; |
| 14 | int sqi; |
| 15 | int sqi_max; |
| 16 | bool link_ext_state_provided; |
| 17 | struct ethtool_link_ext_state_info ethtool_link_ext_state_info; |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 18 | }; |
| 19 | |
| 20 | #define LINKSTATE_REPDATA(__reply_base) \ |
| 21 | container_of(__reply_base, struct linkstate_reply_data, base) |
| 22 | |
Jakub Kicinski | ff419af | 2020-10-05 15:07:35 -0700 | [diff] [blame] | 23 | const struct nla_policy ethnl_linkstate_get_policy[] = { |
Jakub Kicinski | 329d9c3 | 2020-10-05 15:07:36 -0700 | [diff] [blame] | 24 | [ETHTOOL_A_LINKSTATE_HEADER] = |
| 25 | NLA_POLICY_NESTED(ethnl_header_policy), |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 26 | }; |
| 27 | |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 28 | static int linkstate_get_sqi(struct net_device *dev) |
| 29 | { |
| 30 | struct phy_device *phydev = dev->phydev; |
| 31 | int ret; |
| 32 | |
| 33 | if (!phydev) |
| 34 | return -EOPNOTSUPP; |
| 35 | |
| 36 | mutex_lock(&phydev->lock); |
| 37 | if (!phydev->drv || !phydev->drv->get_sqi) |
| 38 | ret = -EOPNOTSUPP; |
| 39 | else |
| 40 | ret = phydev->drv->get_sqi(phydev); |
| 41 | mutex_unlock(&phydev->lock); |
| 42 | |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | static int linkstate_get_sqi_max(struct net_device *dev) |
| 47 | { |
| 48 | struct phy_device *phydev = dev->phydev; |
| 49 | int ret; |
| 50 | |
| 51 | if (!phydev) |
| 52 | return -EOPNOTSUPP; |
| 53 | |
| 54 | mutex_lock(&phydev->lock); |
| 55 | if (!phydev->drv || !phydev->drv->get_sqi_max) |
| 56 | ret = -EOPNOTSUPP; |
| 57 | else |
| 58 | ret = phydev->drv->get_sqi_max(phydev); |
| 59 | mutex_unlock(&phydev->lock); |
| 60 | |
| 61 | return ret; |
Amit Cohen | ecc31c6 | 2020-06-29 23:46:16 +0300 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static int linkstate_get_link_ext_state(struct net_device *dev, |
| 65 | struct linkstate_reply_data *data) |
| 66 | { |
| 67 | int err; |
| 68 | |
| 69 | if (!dev->ethtool_ops->get_link_ext_state) |
| 70 | return -EOPNOTSUPP; |
| 71 | |
| 72 | err = dev->ethtool_ops->get_link_ext_state(dev, &data->ethtool_link_ext_state_info); |
| 73 | if (err) |
| 74 | return err; |
| 75 | |
| 76 | data->link_ext_state_provided = true; |
| 77 | |
| 78 | return 0; |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 81 | static int linkstate_prepare_data(const struct ethnl_req_info *req_base, |
| 82 | struct ethnl_reply_data *reply_base, |
| 83 | struct genl_info *info) |
| 84 | { |
| 85 | struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base); |
| 86 | struct net_device *dev = reply_base->dev; |
| 87 | int ret; |
| 88 | |
| 89 | ret = ethnl_ops_begin(dev); |
| 90 | if (ret < 0) |
| 91 | return ret; |
| 92 | data->link = __ethtool_get_link(dev); |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 93 | |
| 94 | ret = linkstate_get_sqi(dev); |
| 95 | if (ret < 0 && ret != -EOPNOTSUPP) |
Michal Kubecek | 1ae71d9 | 2020-06-25 00:09:08 +0200 | [diff] [blame] | 96 | goto out; |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 97 | data->sqi = ret; |
| 98 | |
| 99 | ret = linkstate_get_sqi_max(dev); |
| 100 | if (ret < 0 && ret != -EOPNOTSUPP) |
Michal Kubecek | 1ae71d9 | 2020-06-25 00:09:08 +0200 | [diff] [blame] | 101 | goto out; |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 102 | data->sqi_max = ret; |
| 103 | |
Amit Cohen | ecc31c6 | 2020-06-29 23:46:16 +0300 | [diff] [blame] | 104 | if (dev->flags & IFF_UP) { |
| 105 | ret = linkstate_get_link_ext_state(dev, data); |
| 106 | if (ret < 0 && ret != -EOPNOTSUPP && ret != -ENODATA) |
| 107 | goto out; |
| 108 | } |
| 109 | |
Michal Kubecek | 1ae71d9 | 2020-06-25 00:09:08 +0200 | [diff] [blame] | 110 | ret = 0; |
| 111 | out: |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 112 | ethnl_ops_complete(dev); |
Michal Kubecek | 1ae71d9 | 2020-06-25 00:09:08 +0200 | [diff] [blame] | 113 | return ret; |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static int linkstate_reply_size(const struct ethnl_req_info *req_base, |
| 117 | const struct ethnl_reply_data *reply_base) |
| 118 | { |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 119 | struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base); |
| 120 | int len; |
| 121 | |
| 122 | len = nla_total_size(sizeof(u8)) /* LINKSTATE_LINK */ |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 123 | + 0; |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 124 | |
| 125 | if (data->sqi != -EOPNOTSUPP) |
| 126 | len += nla_total_size(sizeof(u32)); |
| 127 | |
| 128 | if (data->sqi_max != -EOPNOTSUPP) |
| 129 | len += nla_total_size(sizeof(u32)); |
| 130 | |
Amit Cohen | ecc31c6 | 2020-06-29 23:46:16 +0300 | [diff] [blame] | 131 | if (data->link_ext_state_provided) |
| 132 | len += nla_total_size(sizeof(u8)); /* LINKSTATE_EXT_STATE */ |
| 133 | |
| 134 | if (data->ethtool_link_ext_state_info.__link_ext_substate) |
| 135 | len += nla_total_size(sizeof(u8)); /* LINKSTATE_EXT_SUBSTATE */ |
| 136 | |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 137 | return len; |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int linkstate_fill_reply(struct sk_buff *skb, |
| 141 | const struct ethnl_req_info *req_base, |
| 142 | const struct ethnl_reply_data *reply_base) |
| 143 | { |
| 144 | struct linkstate_reply_data *data = LINKSTATE_REPDATA(reply_base); |
| 145 | |
| 146 | if (data->link >= 0 && |
| 147 | nla_put_u8(skb, ETHTOOL_A_LINKSTATE_LINK, !!data->link)) |
| 148 | return -EMSGSIZE; |
| 149 | |
Oleksij Rempel | 8066021 | 2020-05-20 08:29:14 +0200 | [diff] [blame] | 150 | if (data->sqi != -EOPNOTSUPP && |
| 151 | nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI, data->sqi)) |
| 152 | return -EMSGSIZE; |
| 153 | |
| 154 | if (data->sqi_max != -EOPNOTSUPP && |
| 155 | nla_put_u32(skb, ETHTOOL_A_LINKSTATE_SQI_MAX, data->sqi_max)) |
| 156 | return -EMSGSIZE; |
| 157 | |
Amit Cohen | ecc31c6 | 2020-06-29 23:46:16 +0300 | [diff] [blame] | 158 | if (data->link_ext_state_provided) { |
| 159 | if (nla_put_u8(skb, ETHTOOL_A_LINKSTATE_EXT_STATE, |
| 160 | data->ethtool_link_ext_state_info.link_ext_state)) |
| 161 | return -EMSGSIZE; |
| 162 | |
| 163 | if (data->ethtool_link_ext_state_info.__link_ext_substate && |
| 164 | nla_put_u8(skb, ETHTOOL_A_LINKSTATE_EXT_SUBSTATE, |
| 165 | data->ethtool_link_ext_state_info.__link_ext_substate)) |
| 166 | return -EMSGSIZE; |
| 167 | } |
| 168 | |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | const struct ethnl_request_ops ethnl_linkstate_request_ops = { |
| 173 | .request_cmd = ETHTOOL_MSG_LINKSTATE_GET, |
| 174 | .reply_cmd = ETHTOOL_MSG_LINKSTATE_GET_REPLY, |
| 175 | .hdr_attr = ETHTOOL_A_LINKSTATE_HEADER, |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 176 | .req_info_size = sizeof(struct linkstate_req_info), |
| 177 | .reply_data_size = sizeof(struct linkstate_reply_data), |
Michal Kubecek | 3d2b847 | 2019-12-27 15:56:23 +0100 | [diff] [blame] | 178 | |
| 179 | .prepare_data = linkstate_prepare_data, |
| 180 | .reply_size = linkstate_reply_size, |
| 181 | .fill_reply = linkstate_fill_reply, |
| 182 | }; |