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