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