1 /*
2  * Copyright (c) 2017, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include "en.h"
34 #include "ipoib.h"
35 
36 static void mlx5i_get_drvinfo(struct net_device *dev,
37 			      struct ethtool_drvinfo *drvinfo)
38 {
39 	struct mlx5e_priv *priv = mlx5i_epriv(dev);
40 
41 	mlx5e_ethtool_get_drvinfo(priv, drvinfo);
42 	strlcpy(drvinfo->driver, KBUILD_MODNAME "[ib_ipoib]",
43 		sizeof(drvinfo->driver));
44 }
45 
46 static void mlx5i_get_strings(struct net_device *dev, u32 stringset, u8 *data)
47 {
48 	struct mlx5e_priv *priv  = mlx5i_epriv(dev);
49 
50 	mlx5e_ethtool_get_strings(priv, stringset, data);
51 }
52 
53 static int mlx5i_get_sset_count(struct net_device *dev, int sset)
54 {
55 	struct mlx5e_priv *priv = mlx5i_epriv(dev);
56 
57 	return mlx5e_ethtool_get_sset_count(priv, sset);
58 }
59 
60 static void mlx5i_get_ethtool_stats(struct net_device *dev,
61 				    struct ethtool_stats *stats,
62 				    u64 *data)
63 {
64 	struct mlx5e_priv *priv = mlx5i_epriv(dev);
65 
66 	mlx5e_ethtool_get_ethtool_stats(priv, stats, data);
67 }
68 
69 static int mlx5i_set_ringparam(struct net_device *dev,
70 			       struct ethtool_ringparam *param)
71 {
72 	struct mlx5e_priv *priv = mlx5i_epriv(dev);
73 
74 	return mlx5e_ethtool_set_ringparam(priv, param);
75 }
76 
77 static void mlx5i_get_ringparam(struct net_device *dev,
78 				struct ethtool_ringparam *param)
79 {
80 	struct mlx5e_priv *priv = mlx5i_epriv(dev);
81 
82 	mlx5e_ethtool_get_ringparam(priv, param);
83 }
84 
85 static int mlx5i_set_channels(struct net_device *dev,
86 			      struct ethtool_channels *ch)
87 {
88 	struct mlx5e_priv *priv = mlx5i_epriv(dev);
89 
90 	return mlx5e_ethtool_set_channels(priv, ch);
91 }
92 
93 static void mlx5i_get_channels(struct net_device *dev,
94 			       struct ethtool_channels *ch)
95 {
96 	struct mlx5e_priv *priv = mlx5i_epriv(dev);
97 
98 	mlx5e_ethtool_get_channels(priv, ch);
99 }
100 
101 static int mlx5i_set_coalesce(struct net_device *netdev,
102 			      struct ethtool_coalesce *coal)
103 {
104 	struct mlx5e_priv *priv = mlx5i_epriv(netdev);
105 
106 	return mlx5e_ethtool_set_coalesce(priv, coal);
107 }
108 
109 static int mlx5i_get_coalesce(struct net_device *netdev,
110 			      struct ethtool_coalesce *coal)
111 {
112 	struct mlx5e_priv *priv = mlx5i_epriv(netdev);
113 
114 	return mlx5e_ethtool_get_coalesce(priv, coal);
115 }
116 
117 static int mlx5i_get_ts_info(struct net_device *netdev,
118 			     struct ethtool_ts_info *info)
119 {
120 	struct mlx5e_priv *priv = mlx5i_epriv(netdev);
121 
122 	return mlx5e_ethtool_get_ts_info(priv, info);
123 }
124 
125 static int mlx5i_flash_device(struct net_device *netdev,
126 			      struct ethtool_flash *flash)
127 {
128 	struct mlx5e_priv *priv = mlx5i_epriv(netdev);
129 
130 	return mlx5e_ethtool_flash_device(priv, flash);
131 }
132 
133 static inline int mlx5_ptys_width_enum_to_int(enum mlx5_ptys_width width)
134 {
135 	switch (width) {
136 	case MLX5_PTYS_WIDTH_1X:  return  1;
137 	case MLX5_PTYS_WIDTH_2X:  return  2;
138 	case MLX5_PTYS_WIDTH_4X:  return  4;
139 	case MLX5_PTYS_WIDTH_8X:  return  8;
140 	case MLX5_PTYS_WIDTH_12X: return 12;
141 	default:		  return -1;
142 	}
143 }
144 
145 enum mlx5_ptys_rate {
146 	MLX5_PTYS_RATE_SDR	= 1 << 0,
147 	MLX5_PTYS_RATE_DDR	= 1 << 1,
148 	MLX5_PTYS_RATE_QDR	= 1 << 2,
149 	MLX5_PTYS_RATE_FDR10	= 1 << 3,
150 	MLX5_PTYS_RATE_FDR	= 1 << 4,
151 	MLX5_PTYS_RATE_EDR	= 1 << 5,
152 	MLX5_PTYS_RATE_HDR	= 1 << 6,
153 };
154 
155 static inline int mlx5_ptys_rate_enum_to_int(enum mlx5_ptys_rate rate)
156 {
157 	switch (rate) {
158 	case MLX5_PTYS_RATE_SDR:   return 2500;
159 	case MLX5_PTYS_RATE_DDR:   return 5000;
160 	case MLX5_PTYS_RATE_QDR:
161 	case MLX5_PTYS_RATE_FDR10: return 10000;
162 	case MLX5_PTYS_RATE_FDR:   return 14000;
163 	case MLX5_PTYS_RATE_EDR:   return 25000;
164 	case MLX5_PTYS_RATE_HDR:   return 50000;
165 	default:		   return -1;
166 	}
167 }
168 
169 static int mlx5i_get_speed_settings(u16 ib_link_width_oper, u16 ib_proto_oper)
170 {
171 	int rate, width;
172 
173 	rate = mlx5_ptys_rate_enum_to_int(ib_proto_oper);
174 	if (rate < 0)
175 		return -EINVAL;
176 	width = mlx5_ptys_width_enum_to_int(ib_link_width_oper);
177 	if (width < 0)
178 		return -EINVAL;
179 
180 	return rate * width;
181 }
182 
183 static int mlx5i_get_link_ksettings(struct net_device *netdev,
184 				    struct ethtool_link_ksettings *link_ksettings)
185 {
186 	struct mlx5e_priv *priv = mlx5i_epriv(netdev);
187 	struct mlx5_core_dev *mdev = priv->mdev;
188 	u16 ib_link_width_oper;
189 	u16 ib_proto_oper;
190 	int speed, ret;
191 
192 	ret = mlx5_query_ib_port_oper(mdev, &ib_link_width_oper, &ib_proto_oper,
193 				      1);
194 	if (ret)
195 		return ret;
196 
197 	ethtool_link_ksettings_zero_link_mode(link_ksettings, supported);
198 	ethtool_link_ksettings_zero_link_mode(link_ksettings, advertising);
199 
200 	speed = mlx5i_get_speed_settings(ib_link_width_oper, ib_proto_oper);
201 	if (speed < 0)
202 		return -EINVAL;
203 
204 	link_ksettings->base.duplex = DUPLEX_FULL;
205 	link_ksettings->base.port = PORT_OTHER;
206 
207 	link_ksettings->base.autoneg = AUTONEG_DISABLE;
208 
209 	link_ksettings->base.speed = speed;
210 
211 	return 0;
212 }
213 
214 const struct ethtool_ops mlx5i_ethtool_ops = {
215 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
216 				     ETHTOOL_COALESCE_MAX_FRAMES |
217 				     ETHTOOL_COALESCE_USE_ADAPTIVE,
218 	.get_drvinfo        = mlx5i_get_drvinfo,
219 	.get_strings        = mlx5i_get_strings,
220 	.get_sset_count     = mlx5i_get_sset_count,
221 	.get_ethtool_stats  = mlx5i_get_ethtool_stats,
222 	.get_ringparam      = mlx5i_get_ringparam,
223 	.set_ringparam      = mlx5i_set_ringparam,
224 	.flash_device       = mlx5i_flash_device,
225 	.get_channels       = mlx5i_get_channels,
226 	.set_channels       = mlx5i_set_channels,
227 	.get_coalesce       = mlx5i_get_coalesce,
228 	.set_coalesce       = mlx5i_set_coalesce,
229 	.get_ts_info        = mlx5i_get_ts_info,
230 	.get_link_ksettings = mlx5i_get_link_ksettings,
231 	.get_link           = ethtool_op_get_link,
232 };
233 
234 const struct ethtool_ops mlx5i_pkey_ethtool_ops = {
235 	.get_drvinfo        = mlx5i_get_drvinfo,
236 	.get_link           = ethtool_op_get_link,
237 	.get_ts_info        = mlx5i_get_ts_info,
238 };
239