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