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 
mlxsw_m_base_mac_get(struct mlxsw_m * mlxsw_m)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 
mlxsw_m_port_open(struct net_device * dev)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 
mlxsw_m_port_stop(struct net_device * dev)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 *
mlxsw_m_port_get_devlink_port(struct net_device * dev)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 
mlxsw_m_module_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)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 
mlxsw_m_get_module_info(struct net_device * netdev,struct ethtool_modinfo * modinfo)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
mlxsw_m_get_module_eeprom(struct net_device * netdev,struct ethtool_eeprom * ee,u8 * data)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
mlxsw_m_get_module_eeprom_by_page(struct net_device * netdev,const struct ethtool_module_eeprom * page,struct netlink_ext_ack * extack)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 
mlxsw_m_reset(struct net_device * netdev,u32 * flags)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
mlxsw_m_get_module_power_mode(struct net_device * netdev,struct ethtool_module_power_mode_params * params,struct netlink_ext_ack * extack)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
mlxsw_m_set_module_power_mode(struct net_device * netdev,const struct ethtool_module_power_mode_params * params,struct netlink_ext_ack * extack)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
mlxsw_m_port_module_info_get(struct mlxsw_m * mlxsw_m,u8 local_port,u8 * p_module,u8 * p_width)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
mlxsw_m_port_dev_addr_get(struct mlxsw_m_port * mlxsw_m_port)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 	char ppad_pl[MLXSW_REG_PPAD_LEN];
204 	u8 addr[ETH_ALEN];
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, addr);
212 	eth_hw_addr_gen(mlxsw_m_port->dev, addr, mlxsw_m_port->module + 1);
213 	return 0;
214 }
215 
216 static int
mlxsw_m_port_create(struct mlxsw_m * mlxsw_m,u8 local_port,u8 module)217 mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u8 local_port, u8 module)
218 {
219 	struct mlxsw_m_port *mlxsw_m_port;
220 	struct net_device *dev;
221 	int err;
222 
223 	err = mlxsw_core_port_init(mlxsw_m->core, local_port,
224 				   module + 1, false, 0, false,
225 				   0, mlxsw_m->base_mac,
226 				   sizeof(mlxsw_m->base_mac));
227 	if (err) {
228 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n",
229 			local_port);
230 		return err;
231 	}
232 
233 	dev = alloc_etherdev(sizeof(struct mlxsw_m_port));
234 	if (!dev) {
235 		err = -ENOMEM;
236 		goto err_alloc_etherdev;
237 	}
238 
239 	SET_NETDEV_DEV(dev, mlxsw_m->bus_info->dev);
240 	dev_net_set(dev, mlxsw_core_net(mlxsw_m->core));
241 	mlxsw_m_port = netdev_priv(dev);
242 	mlxsw_m_port->dev = dev;
243 	mlxsw_m_port->mlxsw_m = mlxsw_m;
244 	mlxsw_m_port->local_port = local_port;
245 	mlxsw_m_port->module = module;
246 
247 	dev->netdev_ops = &mlxsw_m_port_netdev_ops;
248 	dev->ethtool_ops = &mlxsw_m_port_ethtool_ops;
249 
250 	err = mlxsw_m_port_dev_addr_get(mlxsw_m_port);
251 	if (err) {
252 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Unable to get port mac address\n",
253 			mlxsw_m_port->local_port);
254 		goto err_dev_addr_get;
255 	}
256 
257 	netif_carrier_off(dev);
258 	mlxsw_m->ports[local_port] = mlxsw_m_port;
259 	err = register_netdev(dev);
260 	if (err) {
261 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to register netdev\n",
262 			mlxsw_m_port->local_port);
263 		goto err_register_netdev;
264 	}
265 
266 	mlxsw_core_port_eth_set(mlxsw_m->core, mlxsw_m_port->local_port,
267 				mlxsw_m_port, dev);
268 
269 	return 0;
270 
271 err_register_netdev:
272 	mlxsw_m->ports[local_port] = NULL;
273 err_dev_addr_get:
274 	free_netdev(dev);
275 err_alloc_etherdev:
276 	mlxsw_core_port_fini(mlxsw_m->core, local_port);
277 	return err;
278 }
279 
mlxsw_m_port_remove(struct mlxsw_m * mlxsw_m,u8 local_port)280 static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u8 local_port)
281 {
282 	struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port];
283 
284 	mlxsw_core_port_clear(mlxsw_m->core, local_port, mlxsw_m);
285 	unregister_netdev(mlxsw_m_port->dev); /* This calls ndo_stop */
286 	mlxsw_m->ports[local_port] = NULL;
287 	free_netdev(mlxsw_m_port->dev);
288 	mlxsw_core_port_fini(mlxsw_m->core, local_port);
289 }
290 
mlxsw_m_port_module_map(struct mlxsw_m * mlxsw_m,u8 local_port,u8 * last_module)291 static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u8 local_port,
292 				   u8 *last_module)
293 {
294 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
295 	u8 module, width;
296 	int err;
297 
298 	/* Fill out to local port mapping array */
299 	err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module,
300 					   &width);
301 	if (err)
302 		return err;
303 
304 	if (!width)
305 		return 0;
306 	/* Skip, if port belongs to the cluster */
307 	if (module == *last_module)
308 		return 0;
309 	*last_module = module;
310 
311 	if (WARN_ON_ONCE(module >= max_ports))
312 		return -EINVAL;
313 	mlxsw_env_module_port_map(mlxsw_m->core, module);
314 	mlxsw_m->module_to_port[module] = ++mlxsw_m->max_ports;
315 
316 	return 0;
317 }
318 
mlxsw_m_port_module_unmap(struct mlxsw_m * mlxsw_m,u8 module)319 static void mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 module)
320 {
321 	mlxsw_m->module_to_port[module] = -1;
322 	mlxsw_env_module_port_unmap(mlxsw_m->core, module);
323 }
324 
mlxsw_m_ports_create(struct mlxsw_m * mlxsw_m)325 static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m)
326 {
327 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
328 	u8 last_module = max_ports;
329 	int i;
330 	int err;
331 
332 	mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports),
333 				 GFP_KERNEL);
334 	if (!mlxsw_m->ports)
335 		return -ENOMEM;
336 
337 	mlxsw_m->module_to_port = kmalloc_array(max_ports, sizeof(int),
338 						GFP_KERNEL);
339 	if (!mlxsw_m->module_to_port) {
340 		err = -ENOMEM;
341 		goto err_module_to_port_alloc;
342 	}
343 
344 	/* Invalidate the entries of module to local port mapping array */
345 	for (i = 0; i < max_ports; i++)
346 		mlxsw_m->module_to_port[i] = -1;
347 
348 	/* Fill out module to local port mapping array */
349 	for (i = 1; i < max_ports; i++) {
350 		err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module);
351 		if (err)
352 			goto err_module_to_port_map;
353 	}
354 
355 	/* Create port objects for each valid entry */
356 	for (i = 0; i < mlxsw_m->max_ports; i++) {
357 		if (mlxsw_m->module_to_port[i] > 0 &&
358 		    !mlxsw_core_port_is_xm(mlxsw_m->core, i)) {
359 			err = mlxsw_m_port_create(mlxsw_m,
360 						  mlxsw_m->module_to_port[i],
361 						  i);
362 			if (err)
363 				goto err_module_to_port_create;
364 		}
365 	}
366 
367 	return 0;
368 
369 err_module_to_port_create:
370 	for (i--; i >= 0; i--) {
371 		if (mlxsw_m->module_to_port[i] > 0)
372 			mlxsw_m_port_remove(mlxsw_m,
373 					    mlxsw_m->module_to_port[i]);
374 	}
375 	i = max_ports;
376 err_module_to_port_map:
377 	for (i--; i > 0; i--)
378 		mlxsw_m_port_module_unmap(mlxsw_m, i);
379 	kfree(mlxsw_m->module_to_port);
380 err_module_to_port_alloc:
381 	kfree(mlxsw_m->ports);
382 	return err;
383 }
384 
mlxsw_m_ports_remove(struct mlxsw_m * mlxsw_m)385 static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m)
386 {
387 	int i;
388 
389 	for (i = 0; i < mlxsw_m->max_ports; i++) {
390 		if (mlxsw_m->module_to_port[i] > 0) {
391 			mlxsw_m_port_remove(mlxsw_m,
392 					    mlxsw_m->module_to_port[i]);
393 			mlxsw_m_port_module_unmap(mlxsw_m, i);
394 		}
395 	}
396 
397 	kfree(mlxsw_m->module_to_port);
398 	kfree(mlxsw_m->ports);
399 }
400 
mlxsw_m_fw_rev_validate(struct mlxsw_m * mlxsw_m)401 static int mlxsw_m_fw_rev_validate(struct mlxsw_m *mlxsw_m)
402 {
403 	const struct mlxsw_fw_rev *rev = &mlxsw_m->bus_info->fw_rev;
404 
405 	/* Validate driver and FW are compatible.
406 	 * Do not check major version, since it defines chip type, while
407 	 * driver is supposed to support any type.
408 	 */
409 	if (mlxsw_core_fw_rev_minor_subminor_validate(rev, &mlxsw_m_fw_rev))
410 		return 0;
411 
412 	dev_err(mlxsw_m->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver (required >= %d.%d.%d)\n",
413 		rev->major, rev->minor, rev->subminor, rev->major,
414 		mlxsw_m_fw_rev.minor, mlxsw_m_fw_rev.subminor);
415 
416 	return -EINVAL;
417 }
418 
mlxsw_m_init(struct mlxsw_core * mlxsw_core,const struct mlxsw_bus_info * mlxsw_bus_info,struct netlink_ext_ack * extack)419 static int mlxsw_m_init(struct mlxsw_core *mlxsw_core,
420 			const struct mlxsw_bus_info *mlxsw_bus_info,
421 			struct netlink_ext_ack *extack)
422 {
423 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
424 	int err;
425 
426 	mlxsw_m->core = mlxsw_core;
427 	mlxsw_m->bus_info = mlxsw_bus_info;
428 
429 	err = mlxsw_m_fw_rev_validate(mlxsw_m);
430 	if (err)
431 		return err;
432 
433 	err = mlxsw_m_base_mac_get(mlxsw_m);
434 	if (err) {
435 		dev_err(mlxsw_m->bus_info->dev, "Failed to get base mac\n");
436 		return err;
437 	}
438 
439 	err = mlxsw_m_ports_create(mlxsw_m);
440 	if (err) {
441 		dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n");
442 		return err;
443 	}
444 
445 	return 0;
446 }
447 
mlxsw_m_fini(struct mlxsw_core * mlxsw_core)448 static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core)
449 {
450 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
451 
452 	mlxsw_m_ports_remove(mlxsw_m);
453 }
454 
455 static const struct mlxsw_config_profile mlxsw_m_config_profile;
456 
457 static struct mlxsw_driver mlxsw_m_driver = {
458 	.kind			= mlxsw_m_driver_name,
459 	.priv_size		= sizeof(struct mlxsw_m),
460 	.init			= mlxsw_m_init,
461 	.fini			= mlxsw_m_fini,
462 	.profile		= &mlxsw_m_config_profile,
463 	.res_query_enabled	= true,
464 };
465 
466 static const struct i2c_device_id mlxsw_m_i2c_id[] = {
467 	{ "mlxsw_minimal", 0},
468 	{ },
469 };
470 
471 static struct i2c_driver mlxsw_m_i2c_driver = {
472 	.driver.name = "mlxsw_minimal",
473 	.class = I2C_CLASS_HWMON,
474 	.id_table = mlxsw_m_i2c_id,
475 };
476 
mlxsw_m_module_init(void)477 static int __init mlxsw_m_module_init(void)
478 {
479 	int err;
480 
481 	err = mlxsw_core_driver_register(&mlxsw_m_driver);
482 	if (err)
483 		return err;
484 
485 	err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver);
486 	if (err)
487 		goto err_i2c_driver_register;
488 
489 	return 0;
490 
491 err_i2c_driver_register:
492 	mlxsw_core_driver_unregister(&mlxsw_m_driver);
493 
494 	return err;
495 }
496 
mlxsw_m_module_exit(void)497 static void __exit mlxsw_m_module_exit(void)
498 {
499 	mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver);
500 	mlxsw_core_driver_unregister(&mlxsw_m_driver);
501 }
502 
503 module_init(mlxsw_m_module_init);
504 module_exit(mlxsw_m_module_exit);
505 
506 MODULE_LICENSE("Dual BSD/GPL");
507 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
508 MODULE_DESCRIPTION("Mellanox minimal driver");
509 MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id);
510