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