1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2016-2019 Mellanox Technologies. All rights reserved */
3 
4 #include <linux/netdevice.h>
5 #include <linux/etherdevice.h>
6 #include <linux/ethtool.h>
7 #include <linux/i2c.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/types.h>
12 
13 #include "core.h"
14 #include "core_env.h"
15 #include "i2c.h"
16 
17 static const char mlxsw_m_driver_name[] = "mlxsw_minimal";
18 
19 struct mlxsw_m_port;
20 
21 struct mlxsw_m {
22 	struct mlxsw_m_port **ports;
23 	int *module_to_port;
24 	struct mlxsw_core *core;
25 	const struct mlxsw_bus_info *bus_info;
26 	u8 base_mac[ETH_ALEN];
27 	u8 max_ports;
28 };
29 
30 struct mlxsw_m_port {
31 	struct net_device *dev;
32 	struct mlxsw_m *mlxsw_m;
33 	u8 local_port;
34 	u8 module;
35 };
36 
37 static int mlxsw_m_base_mac_get(struct mlxsw_m *mlxsw_m)
38 {
39 	char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
40 	int err;
41 
42 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(spad), spad_pl);
43 	if (err)
44 		return err;
45 	mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_m->base_mac);
46 	return 0;
47 }
48 
49 static int mlxsw_m_port_dummy_open_stop(struct net_device *dev)
50 {
51 	return 0;
52 }
53 
54 static struct devlink_port *
55 mlxsw_m_port_get_devlink_port(struct net_device *dev)
56 {
57 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
58 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
59 
60 	return mlxsw_core_port_devlink_port_get(mlxsw_m->core,
61 						mlxsw_m_port->local_port);
62 }
63 
64 static const struct net_device_ops mlxsw_m_port_netdev_ops = {
65 	.ndo_open		= mlxsw_m_port_dummy_open_stop,
66 	.ndo_stop		= mlxsw_m_port_dummy_open_stop,
67 	.ndo_get_devlink_port	= mlxsw_m_port_get_devlink_port,
68 };
69 
70 static int mlxsw_m_get_module_info(struct net_device *netdev,
71 				   struct ethtool_modinfo *modinfo)
72 {
73 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
74 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
75 
76 	return mlxsw_env_get_module_info(core, mlxsw_m_port->module, modinfo);
77 }
78 
79 static int
80 mlxsw_m_get_module_eeprom(struct net_device *netdev, struct ethtool_eeprom *ee,
81 			  u8 *data)
82 {
83 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
84 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
85 
86 	return mlxsw_env_get_module_eeprom(netdev, core, mlxsw_m_port->module,
87 					   ee, data);
88 }
89 
90 static const struct ethtool_ops mlxsw_m_port_ethtool_ops = {
91 	.get_module_info	= mlxsw_m_get_module_info,
92 	.get_module_eeprom	= mlxsw_m_get_module_eeprom,
93 };
94 
95 static int
96 mlxsw_m_port_module_info_get(struct mlxsw_m *mlxsw_m, u8 local_port,
97 			     u8 *p_module, u8 *p_width)
98 {
99 	char pmlp_pl[MLXSW_REG_PMLP_LEN];
100 	int err;
101 
102 	mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
103 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(pmlp), pmlp_pl);
104 	if (err)
105 		return err;
106 	*p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
107 	*p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
108 
109 	return 0;
110 }
111 
112 static int
113 mlxsw_m_port_dev_addr_get(struct mlxsw_m_port *mlxsw_m_port)
114 {
115 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
116 	struct net_device *dev = mlxsw_m_port->dev;
117 	char ppad_pl[MLXSW_REG_PPAD_LEN];
118 	int err;
119 
120 	mlxsw_reg_ppad_pack(ppad_pl, false, 0);
121 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(ppad), ppad_pl);
122 	if (err)
123 		return err;
124 	mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, dev->dev_addr);
125 	/* The last byte value in base mac address is guaranteed
126 	 * to be such it does not overflow when adding local_port
127 	 * value.
128 	 */
129 	dev->dev_addr[ETH_ALEN - 1] += mlxsw_m_port->module + 1;
130 	return 0;
131 }
132 
133 static int
134 mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u8 local_port, u8 module)
135 {
136 	struct mlxsw_m_port *mlxsw_m_port;
137 	struct net_device *dev;
138 	int err;
139 
140 	err = mlxsw_core_port_init(mlxsw_m->core, local_port,
141 				   module + 1, false, 0,
142 				   mlxsw_m->base_mac,
143 				   sizeof(mlxsw_m->base_mac));
144 	if (err) {
145 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n",
146 			local_port);
147 		return err;
148 	}
149 
150 	dev = alloc_etherdev(sizeof(struct mlxsw_m_port));
151 	if (!dev) {
152 		err = -ENOMEM;
153 		goto err_alloc_etherdev;
154 	}
155 
156 	SET_NETDEV_DEV(dev, mlxsw_m->bus_info->dev);
157 	mlxsw_m_port = netdev_priv(dev);
158 	mlxsw_m_port->dev = dev;
159 	mlxsw_m_port->mlxsw_m = mlxsw_m;
160 	mlxsw_m_port->local_port = local_port;
161 	mlxsw_m_port->module = module;
162 
163 	dev->netdev_ops = &mlxsw_m_port_netdev_ops;
164 	dev->ethtool_ops = &mlxsw_m_port_ethtool_ops;
165 
166 	err = mlxsw_m_port_dev_addr_get(mlxsw_m_port);
167 	if (err) {
168 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Unable to get port mac address\n",
169 			mlxsw_m_port->local_port);
170 		goto err_dev_addr_get;
171 	}
172 
173 	netif_carrier_off(dev);
174 	mlxsw_m->ports[local_port] = mlxsw_m_port;
175 	err = register_netdev(dev);
176 	if (err) {
177 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to register netdev\n",
178 			mlxsw_m_port->local_port);
179 		goto err_register_netdev;
180 	}
181 
182 	mlxsw_core_port_eth_set(mlxsw_m->core, mlxsw_m_port->local_port,
183 				mlxsw_m_port, dev);
184 
185 	return 0;
186 
187 err_register_netdev:
188 	mlxsw_m->ports[local_port] = NULL;
189 	free_netdev(dev);
190 err_dev_addr_get:
191 err_alloc_etherdev:
192 	mlxsw_core_port_fini(mlxsw_m->core, local_port);
193 	return err;
194 }
195 
196 static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u8 local_port)
197 {
198 	struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port];
199 
200 	mlxsw_core_port_clear(mlxsw_m->core, local_port, mlxsw_m);
201 	unregister_netdev(mlxsw_m_port->dev); /* This calls ndo_stop */
202 	mlxsw_m->ports[local_port] = NULL;
203 	free_netdev(mlxsw_m_port->dev);
204 	mlxsw_core_port_fini(mlxsw_m->core, local_port);
205 }
206 
207 static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u8 local_port,
208 				   u8 *last_module)
209 {
210 	u8 module, width;
211 	int err;
212 
213 	/* Fill out to local port mapping array */
214 	err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module,
215 					   &width);
216 	if (err)
217 		return err;
218 
219 	if (!width)
220 		return 0;
221 	/* Skip, if port belongs to the cluster */
222 	if (module == *last_module)
223 		return 0;
224 	*last_module = module;
225 	mlxsw_m->module_to_port[module] = ++mlxsw_m->max_ports;
226 
227 	return 0;
228 }
229 
230 static void mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 module)
231 {
232 	mlxsw_m->module_to_port[module] = -1;
233 }
234 
235 static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m)
236 {
237 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
238 	u8 last_module = max_ports;
239 	int i;
240 	int err;
241 
242 	mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports),
243 				 GFP_KERNEL);
244 	if (!mlxsw_m->ports)
245 		return -ENOMEM;
246 
247 	mlxsw_m->module_to_port = kmalloc_array(max_ports, sizeof(int),
248 						GFP_KERNEL);
249 	if (!mlxsw_m->module_to_port) {
250 		err = -ENOMEM;
251 		goto err_module_to_port_alloc;
252 	}
253 
254 	/* Invalidate the entries of module to local port mapping array */
255 	for (i = 0; i < max_ports; i++)
256 		mlxsw_m->module_to_port[i] = -1;
257 
258 	/* Fill out module to local port mapping array */
259 	for (i = 1; i < max_ports; i++) {
260 		err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module);
261 		if (err)
262 			goto err_module_to_port_map;
263 	}
264 
265 	/* Create port objects for each valid entry */
266 	for (i = 0; i < mlxsw_m->max_ports; i++) {
267 		if (mlxsw_m->module_to_port[i] > 0) {
268 			err = mlxsw_m_port_create(mlxsw_m,
269 						  mlxsw_m->module_to_port[i],
270 						  i);
271 			if (err)
272 				goto err_module_to_port_create;
273 		}
274 	}
275 
276 	return 0;
277 
278 err_module_to_port_create:
279 	for (i--; i >= 0; i--) {
280 		if (mlxsw_m->module_to_port[i] > 0)
281 			mlxsw_m_port_remove(mlxsw_m,
282 					    mlxsw_m->module_to_port[i]);
283 	}
284 	i = max_ports;
285 err_module_to_port_map:
286 	for (i--; i > 0; i--)
287 		mlxsw_m_port_module_unmap(mlxsw_m, i);
288 	kfree(mlxsw_m->module_to_port);
289 err_module_to_port_alloc:
290 	kfree(mlxsw_m->ports);
291 	return err;
292 }
293 
294 static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m)
295 {
296 	int i;
297 
298 	for (i = 0; i < mlxsw_m->max_ports; i++) {
299 		if (mlxsw_m->module_to_port[i] > 0) {
300 			mlxsw_m_port_remove(mlxsw_m,
301 					    mlxsw_m->module_to_port[i]);
302 			mlxsw_m_port_module_unmap(mlxsw_m, i);
303 		}
304 	}
305 
306 	kfree(mlxsw_m->module_to_port);
307 	kfree(mlxsw_m->ports);
308 }
309 
310 static int mlxsw_m_init(struct mlxsw_core *mlxsw_core,
311 			const struct mlxsw_bus_info *mlxsw_bus_info)
312 {
313 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
314 	int err;
315 
316 	mlxsw_m->core = mlxsw_core;
317 	mlxsw_m->bus_info = mlxsw_bus_info;
318 
319 	err = mlxsw_m_base_mac_get(mlxsw_m);
320 	if (err) {
321 		dev_err(mlxsw_m->bus_info->dev, "Failed to get base mac\n");
322 		return err;
323 	}
324 
325 	err = mlxsw_m_ports_create(mlxsw_m);
326 	if (err) {
327 		dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n");
328 		return err;
329 	}
330 
331 	return 0;
332 }
333 
334 static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core)
335 {
336 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
337 
338 	mlxsw_m_ports_remove(mlxsw_m);
339 }
340 
341 static const struct mlxsw_config_profile mlxsw_m_config_profile;
342 
343 static struct mlxsw_driver mlxsw_m_driver = {
344 	.kind			= mlxsw_m_driver_name,
345 	.priv_size		= sizeof(struct mlxsw_m),
346 	.init			= mlxsw_m_init,
347 	.fini			= mlxsw_m_fini,
348 	.profile		= &mlxsw_m_config_profile,
349 	.res_query_enabled	= true,
350 };
351 
352 static const struct i2c_device_id mlxsw_m_i2c_id[] = {
353 	{ "mlxsw_minimal", 0},
354 	{ },
355 };
356 
357 static struct i2c_driver mlxsw_m_i2c_driver = {
358 	.driver.name = "mlxsw_minimal",
359 	.class = I2C_CLASS_HWMON,
360 	.id_table = mlxsw_m_i2c_id,
361 };
362 
363 static int __init mlxsw_m_module_init(void)
364 {
365 	int err;
366 
367 	err = mlxsw_core_driver_register(&mlxsw_m_driver);
368 	if (err)
369 		return err;
370 
371 	err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver);
372 	if (err)
373 		goto err_i2c_driver_register;
374 
375 	return 0;
376 
377 err_i2c_driver_register:
378 	mlxsw_core_driver_unregister(&mlxsw_m_driver);
379 
380 	return err;
381 }
382 
383 static void __exit mlxsw_m_module_exit(void)
384 {
385 	mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver);
386 	mlxsw_core_driver_unregister(&mlxsw_m_driver);
387 }
388 
389 module_init(mlxsw_m_module_init);
390 module_exit(mlxsw_m_module_exit);
391 
392 MODULE_LICENSE("Dual BSD/GPL");
393 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
394 MODULE_DESCRIPTION("Mellanox minimal driver");
395 MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id);
396