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 kfree(mlxsw_m->line_cards);
434 err_kcalloc:
435 kfree(mlxsw_m->ports);
436 return err;
437 }
438
mlxsw_m_linecards_fini(struct mlxsw_m * mlxsw_m)439 static void mlxsw_m_linecards_fini(struct mlxsw_m *mlxsw_m)
440 {
441 int i = mlxsw_m->num_of_slots;
442
443 for (i--; i >= 0; i--)
444 kfree(mlxsw_m->line_cards[i]);
445 kfree(mlxsw_m->line_cards);
446 kfree(mlxsw_m->ports);
447 }
448
449 static void
mlxsw_m_linecard_port_module_unmap(struct mlxsw_m * mlxsw_m,u8 slot_index)450 mlxsw_m_linecard_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 slot_index)
451 {
452 int i;
453
454 for (i = mlxsw_m->max_modules_per_slot - 1; i >= 0; i--) {
455 int *module_to_port;
456
457 module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, i);
458 if (*module_to_port > 0)
459 mlxsw_m_port_module_unmap(mlxsw_m, slot_index, i);
460 }
461 }
462
463 static int
mlxsw_m_linecard_ports_create(struct mlxsw_m * mlxsw_m,u8 slot_index)464 mlxsw_m_linecard_ports_create(struct mlxsw_m *mlxsw_m, u8 slot_index)
465 {
466 int *module_to_port;
467 int i, err;
468
469 for (i = 0; i < mlxsw_m->max_modules_per_slot; i++) {
470 module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, i);
471 if (*module_to_port > 0) {
472 err = mlxsw_m_port_create(mlxsw_m, *module_to_port,
473 slot_index, i);
474 if (err)
475 goto err_port_create;
476 /* Mark slot as active */
477 if (!mlxsw_m->line_cards[slot_index]->active)
478 mlxsw_m->line_cards[slot_index]->active = true;
479 }
480 }
481 return 0;
482
483 err_port_create:
484 for (i--; i >= 0; i--) {
485 module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, i);
486 if (*module_to_port > 0 &&
487 mlxsw_m_port_created(mlxsw_m, *module_to_port)) {
488 mlxsw_m_port_remove(mlxsw_m, *module_to_port);
489 /* Mark slot as inactive */
490 if (mlxsw_m->line_cards[slot_index]->active)
491 mlxsw_m->line_cards[slot_index]->active = false;
492 }
493 }
494 return err;
495 }
496
497 static void
mlxsw_m_linecard_ports_remove(struct mlxsw_m * mlxsw_m,u8 slot_index)498 mlxsw_m_linecard_ports_remove(struct mlxsw_m *mlxsw_m, u8 slot_index)
499 {
500 int i;
501
502 for (i = 0; i < mlxsw_m->max_modules_per_slot; i++) {
503 int *module_to_port = mlxsw_m_port_mapping_get(mlxsw_m,
504 slot_index, i);
505
506 if (*module_to_port > 0 &&
507 mlxsw_m_port_created(mlxsw_m, *module_to_port)) {
508 mlxsw_m_port_remove(mlxsw_m, *module_to_port);
509 mlxsw_m_port_module_unmap(mlxsw_m, slot_index, i);
510 }
511 }
512 }
513
mlxsw_m_ports_module_map(struct mlxsw_m * mlxsw_m)514 static int mlxsw_m_ports_module_map(struct mlxsw_m *mlxsw_m)
515 {
516 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
517 u8 last_module = max_ports;
518 int i, err;
519
520 for (i = 1; i < max_ports; i++) {
521 err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module);
522 if (err)
523 return err;
524 }
525
526 return 0;
527 }
528
mlxsw_m_ports_create(struct mlxsw_m * mlxsw_m)529 static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m)
530 {
531 int err;
532
533 /* Fill out module to local port mapping array */
534 err = mlxsw_m_ports_module_map(mlxsw_m);
535 if (err)
536 goto err_ports_module_map;
537
538 /* Create port objects for each valid entry */
539 err = mlxsw_m_linecard_ports_create(mlxsw_m, 0);
540 if (err)
541 goto err_linecard_ports_create;
542
543 return 0;
544
545 err_linecard_ports_create:
546 err_ports_module_map:
547 mlxsw_m_linecard_port_module_unmap(mlxsw_m, 0);
548
549 return err;
550 }
551
mlxsw_m_ports_remove(struct mlxsw_m * mlxsw_m)552 static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m)
553 {
554 mlxsw_m_linecard_ports_remove(mlxsw_m, 0);
555 }
556
557 static void
mlxsw_m_ports_remove_selected(struct mlxsw_core * mlxsw_core,bool (* selector)(void * priv,u16 local_port),void * priv)558 mlxsw_m_ports_remove_selected(struct mlxsw_core *mlxsw_core,
559 bool (*selector)(void *priv, u16 local_port),
560 void *priv)
561 {
562 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
563 struct mlxsw_linecard *linecard_priv = priv;
564 struct mlxsw_m_line_card *linecard;
565
566 linecard = mlxsw_m->line_cards[linecard_priv->slot_index];
567
568 if (WARN_ON(!linecard->active))
569 return;
570
571 mlxsw_m_linecard_ports_remove(mlxsw_m, linecard_priv->slot_index);
572 linecard->active = false;
573 }
574
mlxsw_m_fw_rev_validate(struct mlxsw_m * mlxsw_m)575 static int mlxsw_m_fw_rev_validate(struct mlxsw_m *mlxsw_m)
576 {
577 const struct mlxsw_fw_rev *rev = &mlxsw_m->bus_info->fw_rev;
578
579 /* Validate driver and FW are compatible.
580 * Do not check major version, since it defines chip type, while
581 * driver is supposed to support any type.
582 */
583 if (mlxsw_core_fw_rev_minor_subminor_validate(rev, &mlxsw_m_fw_rev))
584 return 0;
585
586 dev_err(mlxsw_m->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver (required >= %d.%d.%d)\n",
587 rev->major, rev->minor, rev->subminor, rev->major,
588 mlxsw_m_fw_rev.minor, mlxsw_m_fw_rev.subminor);
589
590 return -EINVAL;
591 }
592
593 static void
mlxsw_m_got_active(struct mlxsw_core * mlxsw_core,u8 slot_index,void * priv)594 mlxsw_m_got_active(struct mlxsw_core *mlxsw_core, u8 slot_index, void *priv)
595 {
596 struct mlxsw_m_line_card *linecard;
597 struct mlxsw_m *mlxsw_m = priv;
598 int err;
599
600 linecard = mlxsw_m->line_cards[slot_index];
601 /* Skip if line card has been already configured during init */
602 if (linecard->active)
603 return;
604
605 /* Fill out module to local port mapping array */
606 err = mlxsw_m_ports_module_map(mlxsw_m);
607 if (err)
608 goto err_ports_module_map;
609
610 /* Create port objects for each valid entry */
611 err = mlxsw_m_linecard_ports_create(mlxsw_m, slot_index);
612 if (err) {
613 dev_err(mlxsw_m->bus_info->dev, "Failed to create port for line card at slot %d\n",
614 slot_index);
615 goto err_linecard_ports_create;
616 }
617
618 linecard->active = true;
619
620 return;
621
622 err_linecard_ports_create:
623 err_ports_module_map:
624 mlxsw_m_linecard_port_module_unmap(mlxsw_m, slot_index);
625 }
626
627 static void
mlxsw_m_got_inactive(struct mlxsw_core * mlxsw_core,u8 slot_index,void * priv)628 mlxsw_m_got_inactive(struct mlxsw_core *mlxsw_core, u8 slot_index, void *priv)
629 {
630 struct mlxsw_m_line_card *linecard;
631 struct mlxsw_m *mlxsw_m = priv;
632
633 linecard = mlxsw_m->line_cards[slot_index];
634
635 if (WARN_ON(!linecard->active))
636 return;
637
638 mlxsw_m_linecard_ports_remove(mlxsw_m, slot_index);
639 linecard->active = false;
640 }
641
642 static struct mlxsw_linecards_event_ops mlxsw_m_event_ops = {
643 .got_active = mlxsw_m_got_active,
644 .got_inactive = mlxsw_m_got_inactive,
645 };
646
mlxsw_m_init(struct mlxsw_core * mlxsw_core,const struct mlxsw_bus_info * mlxsw_bus_info,struct netlink_ext_ack * extack)647 static int mlxsw_m_init(struct mlxsw_core *mlxsw_core,
648 const struct mlxsw_bus_info *mlxsw_bus_info,
649 struct netlink_ext_ack *extack)
650 {
651 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
652 int err;
653
654 mlxsw_m->core = mlxsw_core;
655 mlxsw_m->bus_info = mlxsw_bus_info;
656
657 err = mlxsw_m_fw_rev_validate(mlxsw_m);
658 if (err)
659 return err;
660
661 err = mlxsw_m_base_mac_get(mlxsw_m);
662 if (err) {
663 dev_err(mlxsw_m->bus_info->dev, "Failed to get base mac\n");
664 return err;
665 }
666
667 err = mlxsw_m_linecards_init(mlxsw_m);
668 if (err) {
669 dev_err(mlxsw_m->bus_info->dev, "Failed to create line cards\n");
670 return err;
671 }
672
673 err = mlxsw_linecards_event_ops_register(mlxsw_core,
674 &mlxsw_m_event_ops, mlxsw_m);
675 if (err) {
676 dev_err(mlxsw_m->bus_info->dev, "Failed to register line cards operations\n");
677 goto linecards_event_ops_register;
678 }
679
680 err = mlxsw_m_ports_create(mlxsw_m);
681 if (err) {
682 dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n");
683 goto err_ports_create;
684 }
685
686 return 0;
687
688 err_ports_create:
689 mlxsw_linecards_event_ops_unregister(mlxsw_core,
690 &mlxsw_m_event_ops, mlxsw_m);
691 linecards_event_ops_register:
692 mlxsw_m_linecards_fini(mlxsw_m);
693 return err;
694 }
695
mlxsw_m_fini(struct mlxsw_core * mlxsw_core)696 static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core)
697 {
698 struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
699
700 mlxsw_m_ports_remove(mlxsw_m);
701 mlxsw_linecards_event_ops_unregister(mlxsw_core,
702 &mlxsw_m_event_ops, mlxsw_m);
703 mlxsw_m_linecards_fini(mlxsw_m);
704 }
705
706 static const struct mlxsw_config_profile mlxsw_m_config_profile;
707
708 static struct mlxsw_driver mlxsw_m_driver = {
709 .kind = mlxsw_m_driver_name,
710 .priv_size = sizeof(struct mlxsw_m),
711 .init = mlxsw_m_init,
712 .fini = mlxsw_m_fini,
713 .ports_remove_selected = mlxsw_m_ports_remove_selected,
714 .profile = &mlxsw_m_config_profile,
715 };
716
717 static const struct i2c_device_id mlxsw_m_i2c_id[] = {
718 { "mlxsw_minimal", 0},
719 { },
720 };
721
722 static struct i2c_driver mlxsw_m_i2c_driver = {
723 .driver.name = "mlxsw_minimal",
724 .class = I2C_CLASS_HWMON,
725 .id_table = mlxsw_m_i2c_id,
726 };
727
mlxsw_m_module_init(void)728 static int __init mlxsw_m_module_init(void)
729 {
730 int err;
731
732 err = mlxsw_core_driver_register(&mlxsw_m_driver);
733 if (err)
734 return err;
735
736 err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver);
737 if (err)
738 goto err_i2c_driver_register;
739
740 return 0;
741
742 err_i2c_driver_register:
743 mlxsw_core_driver_unregister(&mlxsw_m_driver);
744
745 return err;
746 }
747
mlxsw_m_module_exit(void)748 static void __exit mlxsw_m_module_exit(void)
749 {
750 mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver);
751 mlxsw_core_driver_unregister(&mlxsw_m_driver);
752 }
753
754 module_init(mlxsw_m_module_init);
755 module_exit(mlxsw_m_module_exit);
756
757 MODULE_LICENSE("Dual BSD/GPL");
758 MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
759 MODULE_DESCRIPTION("Mellanox minimal driver");
760 MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id);
761