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_line_card {
30 	bool active;
31 	int module_to_port[];
32 };
33 
34 struct mlxsw_m {
35 	struct mlxsw_m_port **ports;
36 	struct mlxsw_core *core;
37 	const struct mlxsw_bus_info *bus_info;
38 	u8 base_mac[ETH_ALEN];
39 	u8 max_ports;
40 	u8 max_modules_per_slot; /* Maximum number of modules per-slot. */
41 	u8 num_of_slots; /* Including the main board. */
42 	struct mlxsw_m_line_card **line_cards;
43 };
44 
45 struct mlxsw_m_port {
46 	struct net_device *dev;
47 	struct mlxsw_m *mlxsw_m;
48 	u16 local_port;
49 	u8 slot_index;
50 	u8 module;
51 	u8 module_offset;
52 };
53 
mlxsw_m_base_mac_get(struct mlxsw_m * mlxsw_m)54 static int mlxsw_m_base_mac_get(struct mlxsw_m *mlxsw_m)
55 {
56 	char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
57 	int err;
58 
59 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(spad), spad_pl);
60 	if (err)
61 		return err;
62 	mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_m->base_mac);
63 	return 0;
64 }
65 
mlxsw_m_port_open(struct net_device * dev)66 static int mlxsw_m_port_open(struct net_device *dev)
67 {
68 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
69 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
70 
71 	return mlxsw_env_module_port_up(mlxsw_m->core, 0,
72 					mlxsw_m_port->module);
73 }
74 
mlxsw_m_port_stop(struct net_device * dev)75 static int mlxsw_m_port_stop(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 	mlxsw_env_module_port_down(mlxsw_m->core, 0, mlxsw_m_port->module);
81 	return 0;
82 }
83 
84 static struct devlink_port *
mlxsw_m_port_get_devlink_port(struct net_device * dev)85 mlxsw_m_port_get_devlink_port(struct net_device *dev)
86 {
87 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
88 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
89 
90 	return mlxsw_core_port_devlink_port_get(mlxsw_m->core,
91 						mlxsw_m_port->local_port);
92 }
93 
94 static const struct net_device_ops mlxsw_m_port_netdev_ops = {
95 	.ndo_open		= mlxsw_m_port_open,
96 	.ndo_stop		= mlxsw_m_port_stop,
97 	.ndo_get_devlink_port	= mlxsw_m_port_get_devlink_port,
98 };
99 
mlxsw_m_module_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)100 static void mlxsw_m_module_get_drvinfo(struct net_device *dev,
101 				       struct ethtool_drvinfo *drvinfo)
102 {
103 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
104 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
105 
106 	strscpy(drvinfo->driver, mlxsw_m->bus_info->device_kind,
107 		sizeof(drvinfo->driver));
108 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
109 		 "%d.%d.%d",
110 		 mlxsw_m->bus_info->fw_rev.major,
111 		 mlxsw_m->bus_info->fw_rev.minor,
112 		 mlxsw_m->bus_info->fw_rev.subminor);
113 	strscpy(drvinfo->bus_info, mlxsw_m->bus_info->device_name,
114 		sizeof(drvinfo->bus_info));
115 }
116 
mlxsw_m_get_module_info(struct net_device * netdev,struct ethtool_modinfo * modinfo)117 static int mlxsw_m_get_module_info(struct net_device *netdev,
118 				   struct ethtool_modinfo *modinfo)
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_info(netdev, core,
124 					 mlxsw_m_port->slot_index,
125 					 mlxsw_m_port->module, modinfo);
126 }
127 
128 static int
mlxsw_m_get_module_eeprom(struct net_device * netdev,struct ethtool_eeprom * ee,u8 * data)129 mlxsw_m_get_module_eeprom(struct net_device *netdev, struct ethtool_eeprom *ee,
130 			  u8 *data)
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(netdev, core,
136 					   mlxsw_m_port->slot_index,
137 					   mlxsw_m_port->module, ee, data);
138 }
139 
140 static int
mlxsw_m_get_module_eeprom_by_page(struct net_device * netdev,const struct ethtool_module_eeprom * page,struct netlink_ext_ack * extack)141 mlxsw_m_get_module_eeprom_by_page(struct net_device *netdev,
142 				  const struct ethtool_module_eeprom *page,
143 				  struct netlink_ext_ack *extack)
144 {
145 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
146 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
147 
148 	return mlxsw_env_get_module_eeprom_by_page(core,
149 						   mlxsw_m_port->slot_index,
150 						   mlxsw_m_port->module,
151 						   page, extack);
152 }
153 
mlxsw_m_reset(struct net_device * netdev,u32 * flags)154 static int mlxsw_m_reset(struct net_device *netdev, u32 *flags)
155 {
156 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
157 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
158 
159 	return mlxsw_env_reset_module(netdev, core, mlxsw_m_port->slot_index,
160 				      mlxsw_m_port->module,
161 				      flags);
162 }
163 
164 static int
mlxsw_m_get_module_power_mode(struct net_device * netdev,struct ethtool_module_power_mode_params * params,struct netlink_ext_ack * extack)165 mlxsw_m_get_module_power_mode(struct net_device *netdev,
166 			      struct ethtool_module_power_mode_params *params,
167 			      struct netlink_ext_ack *extack)
168 {
169 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
170 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
171 
172 	return mlxsw_env_get_module_power_mode(core, mlxsw_m_port->slot_index,
173 					       mlxsw_m_port->module,
174 					       params, extack);
175 }
176 
177 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)178 mlxsw_m_set_module_power_mode(struct net_device *netdev,
179 			      const struct ethtool_module_power_mode_params *params,
180 			      struct netlink_ext_ack *extack)
181 {
182 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
183 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
184 
185 	return mlxsw_env_set_module_power_mode(core, mlxsw_m_port->slot_index,
186 					       mlxsw_m_port->module,
187 					       params->policy, extack);
188 }
189 
190 static const struct ethtool_ops mlxsw_m_port_ethtool_ops = {
191 	.get_drvinfo		= mlxsw_m_module_get_drvinfo,
192 	.get_module_info	= mlxsw_m_get_module_info,
193 	.get_module_eeprom	= mlxsw_m_get_module_eeprom,
194 	.get_module_eeprom_by_page = mlxsw_m_get_module_eeprom_by_page,
195 	.reset			= mlxsw_m_reset,
196 	.get_module_power_mode	= mlxsw_m_get_module_power_mode,
197 	.set_module_power_mode	= mlxsw_m_set_module_power_mode,
198 };
199 
200 static int
mlxsw_m_port_module_info_get(struct mlxsw_m * mlxsw_m,u16 local_port,u8 * p_module,u8 * p_width,u8 * p_slot_index)201 mlxsw_m_port_module_info_get(struct mlxsw_m *mlxsw_m, u16 local_port,
202 			     u8 *p_module, u8 *p_width, u8 *p_slot_index)
203 {
204 	char pmlp_pl[MLXSW_REG_PMLP_LEN];
205 	int err;
206 
207 	mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
208 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(pmlp), pmlp_pl);
209 	if (err)
210 		return err;
211 	*p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
212 	*p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
213 	*p_slot_index = mlxsw_reg_pmlp_slot_index_get(pmlp_pl, 0);
214 
215 	return 0;
216 }
217 
218 static int
mlxsw_m_port_dev_addr_get(struct mlxsw_m_port * mlxsw_m_port)219 mlxsw_m_port_dev_addr_get(struct mlxsw_m_port *mlxsw_m_port)
220 {
221 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
222 	char ppad_pl[MLXSW_REG_PPAD_LEN];
223 	u8 addr[ETH_ALEN];
224 	int err;
225 
226 	mlxsw_reg_ppad_pack(ppad_pl, false, 0);
227 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(ppad), ppad_pl);
228 	if (err)
229 		return err;
230 	mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, addr);
231 	eth_hw_addr_gen(mlxsw_m_port->dev, addr, mlxsw_m_port->module + 1 +
232 			mlxsw_m_port->module_offset);
233 	return 0;
234 }
235 
mlxsw_m_port_created(struct mlxsw_m * mlxsw_m,u16 local_port)236 static bool mlxsw_m_port_created(struct mlxsw_m *mlxsw_m, u16 local_port)
237 {
238 	return mlxsw_m->ports[local_port];
239 }
240 
241 static int
mlxsw_m_port_create(struct mlxsw_m * mlxsw_m,u16 local_port,u8 slot_index,u8 module)242 mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u16 local_port, u8 slot_index,
243 		    u8 module)
244 {
245 	struct mlxsw_m_port *mlxsw_m_port;
246 	struct net_device *dev;
247 	int err;
248 
249 	err = mlxsw_core_port_init(mlxsw_m->core, local_port, slot_index,
250 				   module + 1, false, 0, false,
251 				   0, mlxsw_m->base_mac,
252 				   sizeof(mlxsw_m->base_mac));
253 	if (err) {
254 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n",
255 			local_port);
256 		return err;
257 	}
258 
259 	dev = alloc_etherdev(sizeof(struct mlxsw_m_port));
260 	if (!dev) {
261 		err = -ENOMEM;
262 		goto err_alloc_etherdev;
263 	}
264 
265 	SET_NETDEV_DEV(dev, mlxsw_m->bus_info->dev);
266 	dev_net_set(dev, mlxsw_core_net(mlxsw_m->core));
267 	mlxsw_m_port = netdev_priv(dev);
268 	mlxsw_m_port->dev = dev;
269 	mlxsw_m_port->mlxsw_m = mlxsw_m;
270 	mlxsw_m_port->local_port = local_port;
271 	mlxsw_m_port->module = module;
272 	mlxsw_m_port->slot_index = slot_index;
273 	/* Add module offset for line card. Offset for main board iz zero.
274 	 * For line card in slot #n offset is calculated as (#n - 1)
275 	 * multiplied by maximum modules number, which could be found on a line
276 	 * card.
277 	 */
278 	mlxsw_m_port->module_offset = mlxsw_m_port->slot_index ?
279 				      (mlxsw_m_port->slot_index - 1) *
280 				      mlxsw_m->max_modules_per_slot : 0;
281 
282 	dev->netdev_ops = &mlxsw_m_port_netdev_ops;
283 	dev->ethtool_ops = &mlxsw_m_port_ethtool_ops;
284 
285 	err = mlxsw_m_port_dev_addr_get(mlxsw_m_port);
286 	if (err) {
287 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Unable to get port mac address\n",
288 			mlxsw_m_port->local_port);
289 		goto err_dev_addr_get;
290 	}
291 
292 	netif_carrier_off(dev);
293 	mlxsw_m->ports[local_port] = mlxsw_m_port;
294 	err = register_netdev(dev);
295 	if (err) {
296 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to register netdev\n",
297 			mlxsw_m_port->local_port);
298 		goto err_register_netdev;
299 	}
300 
301 	mlxsw_core_port_eth_set(mlxsw_m->core, mlxsw_m_port->local_port,
302 				mlxsw_m_port, dev);
303 
304 	return 0;
305 
306 err_register_netdev:
307 	mlxsw_m->ports[local_port] = NULL;
308 err_dev_addr_get:
309 	free_netdev(dev);
310 err_alloc_etherdev:
311 	mlxsw_core_port_fini(mlxsw_m->core, local_port);
312 	return err;
313 }
314 
mlxsw_m_port_remove(struct mlxsw_m * mlxsw_m,u16 local_port)315 static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u16 local_port)
316 {
317 	struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port];
318 
319 	mlxsw_core_port_clear(mlxsw_m->core, local_port, mlxsw_m);
320 	unregister_netdev(mlxsw_m_port->dev); /* This calls ndo_stop */
321 	mlxsw_m->ports[local_port] = NULL;
322 	free_netdev(mlxsw_m_port->dev);
323 	mlxsw_core_port_fini(mlxsw_m->core, local_port);
324 }
325 
326 static int*
mlxsw_m_port_mapping_get(struct mlxsw_m * mlxsw_m,u8 slot_index,u8 module)327 mlxsw_m_port_mapping_get(struct mlxsw_m *mlxsw_m, u8 slot_index, u8 module)
328 {
329 	return &mlxsw_m->line_cards[slot_index]->module_to_port[module];
330 }
331 
mlxsw_m_port_module_map(struct mlxsw_m * mlxsw_m,u16 local_port,u8 * last_module)332 static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u16 local_port,
333 				   u8 *last_module)
334 {
335 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
336 	u8 module, width, slot_index;
337 	int *module_to_port;
338 	int err;
339 
340 	/* Fill out to local port mapping array */
341 	err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module,
342 					   &width, &slot_index);
343 	if (err)
344 		return err;
345 
346 	/* Skip if line card has been already configured */
347 	if (mlxsw_m->line_cards[slot_index]->active)
348 		return 0;
349 	if (!width)
350 		return 0;
351 	/* Skip, if port belongs to the cluster */
352 	if (module == *last_module)
353 		return 0;
354 	*last_module = module;
355 
356 	if (WARN_ON_ONCE(module >= max_ports))
357 		return -EINVAL;
358 	mlxsw_env_module_port_map(mlxsw_m->core, slot_index, module);
359 	module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, module);
360 	*module_to_port = local_port;
361 
362 	return 0;
363 }
364 
365 static void
mlxsw_m_port_module_unmap(struct mlxsw_m * mlxsw_m,u8 slot_index,u8 module)366 mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 slot_index, u8 module)
367 {
368 	int *module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index,
369 						       module);
370 	*module_to_port = -1;
371 	mlxsw_env_module_port_unmap(mlxsw_m->core, slot_index, module);
372 }
373 
mlxsw_m_linecards_init(struct mlxsw_m * mlxsw_m)374 static int mlxsw_m_linecards_init(struct mlxsw_m *mlxsw_m)
375 {
376 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
377 	char mgpir_pl[MLXSW_REG_MGPIR_LEN];
378 	u8 num_of_modules;
379 	int i, j, err;
380 
381 	mlxsw_reg_mgpir_pack(mgpir_pl, 0);
382 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(mgpir), mgpir_pl);
383 	if (err)
384 		return err;
385 
386 	mlxsw_reg_mgpir_unpack(mgpir_pl, NULL, NULL, NULL, &num_of_modules,
387 			       &mlxsw_m->num_of_slots);
388 	/* If the system is modular, get the maximum number of modules per-slot.
389 	 * Otherwise, get the maximum number of modules on the main board.
390 	 */
391 	if (mlxsw_m->num_of_slots)
392 		mlxsw_m->max_modules_per_slot =
393 			mlxsw_reg_mgpir_max_modules_per_slot_get(mgpir_pl);
394 	else
395 		mlxsw_m->max_modules_per_slot = num_of_modules;
396 	/* Add slot for main board. */
397 	mlxsw_m->num_of_slots += 1;
398 
399 	mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports),
400 				 GFP_KERNEL);
401 	if (!mlxsw_m->ports)
402 		return -ENOMEM;
403 
404 	mlxsw_m->line_cards = kcalloc(mlxsw_m->num_of_slots,
405 				      sizeof(*mlxsw_m->line_cards),
406 				      GFP_KERNEL);
407 	if (!mlxsw_m->line_cards) {
408 		err = -ENOMEM;
409 		goto err_kcalloc;
410 	}
411 
412 	for (i = 0; i < mlxsw_m->num_of_slots; i++) {
413 		mlxsw_m->line_cards[i] =
414 			kzalloc(struct_size(mlxsw_m->line_cards[i],
415 					    module_to_port,
416 					    mlxsw_m->max_modules_per_slot),
417 				GFP_KERNEL);
418 		if (!mlxsw_m->line_cards[i]) {
419 			err = -ENOMEM;
420 			goto err_kmalloc_array;
421 		}
422 
423 		/* Invalidate the entries of module to local port mapping array. */
424 		for (j = 0; j < mlxsw_m->max_modules_per_slot; j++)
425 			mlxsw_m->line_cards[i]->module_to_port[j] = -1;
426 	}
427 
428 	return 0;
429 
430 err_kmalloc_array:
431 	for (i--; i >= 0; i--)
432 		kfree(mlxsw_m->line_cards[i]);
433 err_kcalloc:
434 	kfree(mlxsw_m->ports);
435 	return err;
436 }
437 
mlxsw_m_linecards_fini(struct mlxsw_m * mlxsw_m)438 static void mlxsw_m_linecards_fini(struct mlxsw_m *mlxsw_m)
439 {
440 	int i = mlxsw_m->num_of_slots;
441 
442 	for (i--; i >= 0; i--)
443 		kfree(mlxsw_m->line_cards[i]);
444 	kfree(mlxsw_m->line_cards);
445 	kfree(mlxsw_m->ports);
446 }
447 
448 static void
mlxsw_m_linecard_port_module_unmap(struct mlxsw_m * mlxsw_m,u8 slot_index)449 mlxsw_m_linecard_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 slot_index)
450 {
451 	int i;
452 
453 	for (i = mlxsw_m->max_modules_per_slot - 1; i >= 0; i--) {
454 		int *module_to_port;
455 
456 		module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, i);
457 		if (*module_to_port > 0)
458 			mlxsw_m_port_module_unmap(mlxsw_m, slot_index, i);
459 	}
460 }
461 
462 static int
mlxsw_m_linecard_ports_create(struct mlxsw_m * mlxsw_m,u8 slot_index)463 mlxsw_m_linecard_ports_create(struct mlxsw_m *mlxsw_m, u8 slot_index)
464 {
465 	int *module_to_port;
466 	int i, err;
467 
468 	for (i = 0; i < mlxsw_m->max_modules_per_slot; i++) {
469 		module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, i);
470 		if (*module_to_port > 0) {
471 			err = mlxsw_m_port_create(mlxsw_m, *module_to_port,
472 						  slot_index, i);
473 			if (err)
474 				goto err_port_create;
475 			/* Mark slot as active */
476 			if (!mlxsw_m->line_cards[slot_index]->active)
477 				mlxsw_m->line_cards[slot_index]->active = true;
478 		}
479 	}
480 	return 0;
481 
482 err_port_create:
483 	for (i--; i >= 0; i--) {
484 		module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, i);
485 		if (*module_to_port > 0 &&
486 		    mlxsw_m_port_created(mlxsw_m, *module_to_port)) {
487 			mlxsw_m_port_remove(mlxsw_m, *module_to_port);
488 			/* Mark slot as inactive */
489 			if (mlxsw_m->line_cards[slot_index]->active)
490 				mlxsw_m->line_cards[slot_index]->active = false;
491 		}
492 	}
493 	return err;
494 }
495 
496 static void
mlxsw_m_linecard_ports_remove(struct mlxsw_m * mlxsw_m,u8 slot_index)497 mlxsw_m_linecard_ports_remove(struct mlxsw_m *mlxsw_m, u8 slot_index)
498 {
499 	int i;
500 
501 	for (i = 0; i < mlxsw_m->max_modules_per_slot; i++) {
502 		int *module_to_port = mlxsw_m_port_mapping_get(mlxsw_m,
503 							       slot_index, i);
504 
505 		if (*module_to_port > 0 &&
506 		    mlxsw_m_port_created(mlxsw_m, *module_to_port)) {
507 			mlxsw_m_port_remove(mlxsw_m, *module_to_port);
508 			mlxsw_m_port_module_unmap(mlxsw_m, slot_index, i);
509 		}
510 	}
511 }
512 
mlxsw_m_ports_module_map(struct mlxsw_m * mlxsw_m)513 static int mlxsw_m_ports_module_map(struct mlxsw_m *mlxsw_m)
514 {
515 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
516 	u8 last_module = max_ports;
517 	int i, err;
518 
519 	for (i = 1; i < max_ports; i++) {
520 		err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module);
521 		if (err)
522 			return err;
523 	}
524 
525 	return 0;
526 }
527 
mlxsw_m_ports_create(struct mlxsw_m * mlxsw_m)528 static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m)
529 {
530 	int err;
531 
532 	/* Fill out module to local port mapping array */
533 	err = mlxsw_m_ports_module_map(mlxsw_m);
534 	if (err)
535 		goto err_ports_module_map;
536 
537 	/* Create port objects for each valid entry */
538 	err = mlxsw_m_linecard_ports_create(mlxsw_m, 0);
539 	if (err)
540 		goto err_linecard_ports_create;
541 
542 	return 0;
543 
544 err_linecard_ports_create:
545 err_ports_module_map:
546 	mlxsw_m_linecard_port_module_unmap(mlxsw_m, 0);
547 
548 	return err;
549 }
550 
mlxsw_m_ports_remove(struct mlxsw_m * mlxsw_m)551 static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m)
552 {
553 	mlxsw_m_linecard_ports_remove(mlxsw_m, 0);
554 }
555 
556 static void
mlxsw_m_ports_remove_selected(struct mlxsw_core * mlxsw_core,bool (* selector)(void * priv,u16 local_port),void * priv)557 mlxsw_m_ports_remove_selected(struct mlxsw_core *mlxsw_core,
558 			      bool (*selector)(void *priv, u16 local_port),
559 			      void *priv)
560 {
561 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
562 	struct mlxsw_linecard *linecard_priv = priv;
563 	struct mlxsw_m_line_card *linecard;
564 
565 	linecard = mlxsw_m->line_cards[linecard_priv->slot_index];
566 
567 	if (WARN_ON(!linecard->active))
568 		return;
569 
570 	mlxsw_m_linecard_ports_remove(mlxsw_m, linecard_priv->slot_index);
571 	linecard->active = false;
572 }
573 
mlxsw_m_fw_rev_validate(struct mlxsw_m * mlxsw_m)574 static int mlxsw_m_fw_rev_validate(struct mlxsw_m *mlxsw_m)
575 {
576 	const struct mlxsw_fw_rev *rev = &mlxsw_m->bus_info->fw_rev;
577 
578 	/* Validate driver and FW are compatible.
579 	 * Do not check major version, since it defines chip type, while
580 	 * driver is supposed to support any type.
581 	 */
582 	if (mlxsw_core_fw_rev_minor_subminor_validate(rev, &mlxsw_m_fw_rev))
583 		return 0;
584 
585 	dev_err(mlxsw_m->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver (required >= %d.%d.%d)\n",
586 		rev->major, rev->minor, rev->subminor, rev->major,
587 		mlxsw_m_fw_rev.minor, mlxsw_m_fw_rev.subminor);
588 
589 	return -EINVAL;
590 }
591 
592 static void
mlxsw_m_got_active(struct mlxsw_core * mlxsw_core,u8 slot_index,void * priv)593 mlxsw_m_got_active(struct mlxsw_core *mlxsw_core, u8 slot_index, void *priv)
594 {
595 	struct mlxsw_m_line_card *linecard;
596 	struct mlxsw_m *mlxsw_m = priv;
597 	int err;
598 
599 	linecard = mlxsw_m->line_cards[slot_index];
600 	/* Skip if line card has been already configured during init */
601 	if (linecard->active)
602 		return;
603 
604 	/* Fill out module to local port mapping array */
605 	err = mlxsw_m_ports_module_map(mlxsw_m);
606 	if (err)
607 		goto err_ports_module_map;
608 
609 	/* Create port objects for each valid entry */
610 	err = mlxsw_m_linecard_ports_create(mlxsw_m, slot_index);
611 	if (err) {
612 		dev_err(mlxsw_m->bus_info->dev, "Failed to create port for line card at slot %d\n",
613 			slot_index);
614 		goto err_linecard_ports_create;
615 	}
616 
617 	linecard->active = true;
618 
619 	return;
620 
621 err_linecard_ports_create:
622 err_ports_module_map:
623 	mlxsw_m_linecard_port_module_unmap(mlxsw_m, slot_index);
624 }
625 
626 static void
mlxsw_m_got_inactive(struct mlxsw_core * mlxsw_core,u8 slot_index,void * priv)627 mlxsw_m_got_inactive(struct mlxsw_core *mlxsw_core, u8 slot_index, void *priv)
628 {
629 	struct mlxsw_m_line_card *linecard;
630 	struct mlxsw_m *mlxsw_m = priv;
631 
632 	linecard = mlxsw_m->line_cards[slot_index];
633 
634 	if (WARN_ON(!linecard->active))
635 		return;
636 
637 	mlxsw_m_linecard_ports_remove(mlxsw_m, slot_index);
638 	linecard->active = false;
639 }
640 
641 static struct mlxsw_linecards_event_ops mlxsw_m_event_ops = {
642 	.got_active = mlxsw_m_got_active,
643 	.got_inactive = mlxsw_m_got_inactive,
644 };
645 
mlxsw_m_init(struct mlxsw_core * mlxsw_core,const struct mlxsw_bus_info * mlxsw_bus_info,struct netlink_ext_ack * extack)646 static int mlxsw_m_init(struct mlxsw_core *mlxsw_core,
647 			const struct mlxsw_bus_info *mlxsw_bus_info,
648 			struct netlink_ext_ack *extack)
649 {
650 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
651 	int err;
652 
653 	mlxsw_m->core = mlxsw_core;
654 	mlxsw_m->bus_info = mlxsw_bus_info;
655 
656 	err = mlxsw_m_fw_rev_validate(mlxsw_m);
657 	if (err)
658 		return err;
659 
660 	err = mlxsw_m_base_mac_get(mlxsw_m);
661 	if (err) {
662 		dev_err(mlxsw_m->bus_info->dev, "Failed to get base mac\n");
663 		return err;
664 	}
665 
666 	err = mlxsw_m_linecards_init(mlxsw_m);
667 	if (err) {
668 		dev_err(mlxsw_m->bus_info->dev, "Failed to create line cards\n");
669 		return err;
670 	}
671 
672 	err = mlxsw_linecards_event_ops_register(mlxsw_core,
673 						 &mlxsw_m_event_ops, mlxsw_m);
674 	if (err) {
675 		dev_err(mlxsw_m->bus_info->dev, "Failed to register line cards operations\n");
676 		goto linecards_event_ops_register;
677 	}
678 
679 	err = mlxsw_m_ports_create(mlxsw_m);
680 	if (err) {
681 		dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n");
682 		goto err_ports_create;
683 	}
684 
685 	return 0;
686 
687 err_ports_create:
688 	mlxsw_linecards_event_ops_unregister(mlxsw_core,
689 					     &mlxsw_m_event_ops, mlxsw_m);
690 linecards_event_ops_register:
691 	mlxsw_m_linecards_fini(mlxsw_m);
692 	return err;
693 }
694 
mlxsw_m_fini(struct mlxsw_core * mlxsw_core)695 static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core)
696 {
697 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
698 
699 	mlxsw_m_ports_remove(mlxsw_m);
700 	mlxsw_linecards_event_ops_unregister(mlxsw_core,
701 					     &mlxsw_m_event_ops, mlxsw_m);
702 	mlxsw_m_linecards_fini(mlxsw_m);
703 }
704 
705 static const struct mlxsw_config_profile mlxsw_m_config_profile;
706 
707 static struct mlxsw_driver mlxsw_m_driver = {
708 	.kind			= mlxsw_m_driver_name,
709 	.priv_size		= sizeof(struct mlxsw_m),
710 	.init			= mlxsw_m_init,
711 	.fini			= mlxsw_m_fini,
712 	.ports_remove_selected	= mlxsw_m_ports_remove_selected,
713 	.profile		= &mlxsw_m_config_profile,
714 };
715 
716 static const struct i2c_device_id mlxsw_m_i2c_id[] = {
717 	{ "mlxsw_minimal", 0},
718 	{ },
719 };
720 
721 static struct i2c_driver mlxsw_m_i2c_driver = {
722 	.driver.name = "mlxsw_minimal",
723 	.class = I2C_CLASS_HWMON,
724 	.id_table = mlxsw_m_i2c_id,
725 };
726 
mlxsw_m_module_init(void)727 static int __init mlxsw_m_module_init(void)
728 {
729 	int err;
730 
731 	err = mlxsw_core_driver_register(&mlxsw_m_driver);
732 	if (err)
733 		return err;
734 
735 	err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver);
736 	if (err)
737 		goto err_i2c_driver_register;
738 
739 	return 0;
740 
741 err_i2c_driver_register:
742 	mlxsw_core_driver_unregister(&mlxsw_m_driver);
743 
744 	return err;
745 }
746 
mlxsw_m_module_exit(void)747 static void __exit mlxsw_m_module_exit(void)
748 {
749 	mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver);
750 	mlxsw_core_driver_unregister(&mlxsw_m_driver);
751 }
752 
753 module_init(mlxsw_m_module_init);
754 module_exit(mlxsw_m_module_exit);
755 
756 MODULE_LICENSE("Dual BSD/GPL");
757 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
758 MODULE_DESCRIPTION("Mellanox minimal driver");
759 MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id);
760