xref: /openbmc/linux/drivers/net/ethernet/mellanox/mlxsw/minimal.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
19948a064SJiri Pirko // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2c100e47cSVadim Pasternak /* Copyright (c) 2016-2019 Mellanox Technologies. All rights reserved */
3d556e929SVadim Pasternak 
4c100e47cSVadim Pasternak #include <linux/netdevice.h>
5c100e47cSVadim Pasternak #include <linux/etherdevice.h>
6c100e47cSVadim Pasternak #include <linux/ethtool.h>
7d556e929SVadim Pasternak #include <linux/i2c.h>
8d556e929SVadim Pasternak #include <linux/kernel.h>
9d556e929SVadim Pasternak #include <linux/module.h>
10d556e929SVadim Pasternak #include <linux/mod_devicetable.h>
11d556e929SVadim Pasternak #include <linux/types.h>
12d556e929SVadim Pasternak 
13d556e929SVadim Pasternak #include "core.h"
14c100e47cSVadim Pasternak #include "core_env.h"
15d556e929SVadim Pasternak #include "i2c.h"
16d556e929SVadim Pasternak 
171ded391dSVadim Pasternak static const char mlxsw_m_driver_name[] = "mlxsw_minimal";
18d556e929SVadim Pasternak 
196935af80SVadim Pasternak #define MLXSW_M_FWREV_MINOR	2000
206935af80SVadim Pasternak #define MLXSW_M_FWREV_SUBMINOR	1886
216935af80SVadim Pasternak 
226935af80SVadim Pasternak static const struct mlxsw_fw_rev mlxsw_m_fw_rev = {
236935af80SVadim Pasternak 	.minor = MLXSW_M_FWREV_MINOR,
246935af80SVadim Pasternak 	.subminor = MLXSW_M_FWREV_SUBMINOR,
256935af80SVadim Pasternak };
266935af80SVadim Pasternak 
27c100e47cSVadim Pasternak struct mlxsw_m_port;
28c100e47cSVadim Pasternak 
2901328e23SVadim Pasternak struct mlxsw_m_line_card {
3001328e23SVadim Pasternak 	bool active;
3101328e23SVadim Pasternak 	int module_to_port[];
3201328e23SVadim Pasternak };
3301328e23SVadim Pasternak 
34c100e47cSVadim Pasternak struct mlxsw_m {
35c100e47cSVadim Pasternak 	struct mlxsw_m_port **ports;
36c100e47cSVadim Pasternak 	struct mlxsw_core *core;
37c100e47cSVadim Pasternak 	const struct mlxsw_bus_info *bus_info;
38c100e47cSVadim Pasternak 	u8 base_mac[ETH_ALEN];
39c100e47cSVadim Pasternak 	u8 max_ports;
4001328e23SVadim Pasternak 	u8 max_modules_per_slot; /* Maximum number of modules per-slot. */
4101328e23SVadim Pasternak 	u8 num_of_slots; /* Including the main board. */
4201328e23SVadim Pasternak 	struct mlxsw_m_line_card **line_cards;
43c100e47cSVadim Pasternak };
44c100e47cSVadim Pasternak 
45c100e47cSVadim Pasternak struct mlxsw_m_port {
46c100e47cSVadim Pasternak 	struct net_device *dev;
47c100e47cSVadim Pasternak 	struct mlxsw_m *mlxsw_m;
48c934757dSAmit Cohen 	u16 local_port;
49c7ea08baSVadim Pasternak 	u8 slot_index;
50c100e47cSVadim Pasternak 	u8 module;
51706ddb78SVadim Pasternak 	u8 module_offset;
52c100e47cSVadim Pasternak };
53c100e47cSVadim Pasternak 
mlxsw_m_base_mac_get(struct mlxsw_m * mlxsw_m)54426aa1fcSJiri Pirko static int mlxsw_m_base_mac_get(struct mlxsw_m *mlxsw_m)
55426aa1fcSJiri Pirko {
56426aa1fcSJiri Pirko 	char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
57426aa1fcSJiri Pirko 	int err;
58426aa1fcSJiri Pirko 
59426aa1fcSJiri Pirko 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(spad), spad_pl);
60426aa1fcSJiri Pirko 	if (err)
61426aa1fcSJiri Pirko 		return err;
62426aa1fcSJiri Pirko 	mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_m->base_mac);
63426aa1fcSJiri Pirko 	return 0;
64426aa1fcSJiri Pirko }
65426aa1fcSJiri Pirko 
mlxsw_m_port_open(struct net_device * dev)66896f399bSIdo Schimmel static int mlxsw_m_port_open(struct net_device *dev)
67c100e47cSVadim Pasternak {
68896f399bSIdo Schimmel 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
69896f399bSIdo Schimmel 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
70896f399bSIdo Schimmel 
7134945452SVadim Pasternak 	return mlxsw_env_module_port_up(mlxsw_m->core, 0,
7234945452SVadim Pasternak 					mlxsw_m_port->module);
73896f399bSIdo Schimmel }
74896f399bSIdo Schimmel 
mlxsw_m_port_stop(struct net_device * dev)75896f399bSIdo Schimmel static int mlxsw_m_port_stop(struct net_device *dev)
76896f399bSIdo Schimmel {
77896f399bSIdo Schimmel 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
78896f399bSIdo Schimmel 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
79896f399bSIdo Schimmel 
8034945452SVadim Pasternak 	mlxsw_env_module_port_down(mlxsw_m->core, 0, mlxsw_m_port->module);
81c100e47cSVadim Pasternak 	return 0;
82c100e47cSVadim Pasternak }
83c100e47cSVadim Pasternak 
84c100e47cSVadim Pasternak static const struct net_device_ops mlxsw_m_port_netdev_ops = {
85896f399bSIdo Schimmel 	.ndo_open		= mlxsw_m_port_open,
86896f399bSIdo Schimmel 	.ndo_stop		= mlxsw_m_port_stop,
87c100e47cSVadim Pasternak };
88c100e47cSVadim Pasternak 
mlxsw_m_module_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * drvinfo)899bbd7efbSVadim Pasternak static void mlxsw_m_module_get_drvinfo(struct net_device *dev,
909bbd7efbSVadim Pasternak 				       struct ethtool_drvinfo *drvinfo)
919bbd7efbSVadim Pasternak {
929bbd7efbSVadim Pasternak 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(dev);
939bbd7efbSVadim Pasternak 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
949bbd7efbSVadim Pasternak 
95f029c781SWolfram Sang 	strscpy(drvinfo->driver, mlxsw_m->bus_info->device_kind,
969bbd7efbSVadim Pasternak 		sizeof(drvinfo->driver));
979bbd7efbSVadim Pasternak 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
989bbd7efbSVadim Pasternak 		 "%d.%d.%d",
999bbd7efbSVadim Pasternak 		 mlxsw_m->bus_info->fw_rev.major,
1009bbd7efbSVadim Pasternak 		 mlxsw_m->bus_info->fw_rev.minor,
1019bbd7efbSVadim Pasternak 		 mlxsw_m->bus_info->fw_rev.subminor);
102f029c781SWolfram Sang 	strscpy(drvinfo->bus_info, mlxsw_m->bus_info->device_name,
1039bbd7efbSVadim Pasternak 		sizeof(drvinfo->bus_info));
1049bbd7efbSVadim Pasternak }
1059bbd7efbSVadim Pasternak 
mlxsw_m_get_module_info(struct net_device * netdev,struct ethtool_modinfo * modinfo)106c100e47cSVadim Pasternak static int mlxsw_m_get_module_info(struct net_device *netdev,
107c100e47cSVadim Pasternak 				   struct ethtool_modinfo *modinfo)
108c100e47cSVadim Pasternak {
109c100e47cSVadim Pasternak 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
110c100e47cSVadim Pasternak 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
111c100e47cSVadim Pasternak 
112c7ea08baSVadim Pasternak 	return mlxsw_env_get_module_info(netdev, core,
113c7ea08baSVadim Pasternak 					 mlxsw_m_port->slot_index,
114c7ea08baSVadim Pasternak 					 mlxsw_m_port->module, modinfo);
115c100e47cSVadim Pasternak }
116c100e47cSVadim Pasternak 
117c100e47cSVadim Pasternak static int
mlxsw_m_get_module_eeprom(struct net_device * netdev,struct ethtool_eeprom * ee,u8 * data)118c100e47cSVadim Pasternak mlxsw_m_get_module_eeprom(struct net_device *netdev, struct ethtool_eeprom *ee,
119c100e47cSVadim Pasternak 			  u8 *data)
120c100e47cSVadim Pasternak {
121c100e47cSVadim Pasternak 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
122c100e47cSVadim Pasternak 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
123c100e47cSVadim Pasternak 
124c7ea08baSVadim Pasternak 	return mlxsw_env_get_module_eeprom(netdev, core,
125c7ea08baSVadim Pasternak 					   mlxsw_m_port->slot_index,
12634945452SVadim Pasternak 					   mlxsw_m_port->module, ee, data);
127c100e47cSVadim Pasternak }
128c100e47cSVadim Pasternak 
1291e27b9e4SIdo Schimmel static int
mlxsw_m_get_module_eeprom_by_page(struct net_device * netdev,const struct ethtool_module_eeprom * page,struct netlink_ext_ack * extack)1301e27b9e4SIdo Schimmel mlxsw_m_get_module_eeprom_by_page(struct net_device *netdev,
1311e27b9e4SIdo Schimmel 				  const struct ethtool_module_eeprom *page,
1321e27b9e4SIdo Schimmel 				  struct netlink_ext_ack *extack)
1331e27b9e4SIdo Schimmel {
1341e27b9e4SIdo Schimmel 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
1351e27b9e4SIdo Schimmel 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
1361e27b9e4SIdo Schimmel 
137c7ea08baSVadim Pasternak 	return mlxsw_env_get_module_eeprom_by_page(core,
138c7ea08baSVadim Pasternak 						   mlxsw_m_port->slot_index,
13934945452SVadim Pasternak 						   mlxsw_m_port->module,
1401e27b9e4SIdo Schimmel 						   page, extack);
1411e27b9e4SIdo Schimmel }
1421e27b9e4SIdo Schimmel 
mlxsw_m_reset(struct net_device * netdev,u32 * flags)14349fd3b64SIdo Schimmel static int mlxsw_m_reset(struct net_device *netdev, u32 *flags)
14449fd3b64SIdo Schimmel {
14549fd3b64SIdo Schimmel 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
14649fd3b64SIdo Schimmel 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
14749fd3b64SIdo Schimmel 
148c7ea08baSVadim Pasternak 	return mlxsw_env_reset_module(netdev, core, mlxsw_m_port->slot_index,
149c7ea08baSVadim Pasternak 				      mlxsw_m_port->module,
15049fd3b64SIdo Schimmel 				      flags);
15149fd3b64SIdo Schimmel }
15249fd3b64SIdo Schimmel 
1530455dc50SIdo Schimmel static int
mlxsw_m_get_module_power_mode(struct net_device * netdev,struct ethtool_module_power_mode_params * params,struct netlink_ext_ack * extack)1540455dc50SIdo Schimmel mlxsw_m_get_module_power_mode(struct net_device *netdev,
1550455dc50SIdo Schimmel 			      struct ethtool_module_power_mode_params *params,
1560455dc50SIdo Schimmel 			      struct netlink_ext_ack *extack)
1570455dc50SIdo Schimmel {
1580455dc50SIdo Schimmel 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
1590455dc50SIdo Schimmel 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
1600455dc50SIdo Schimmel 
161c7ea08baSVadim Pasternak 	return mlxsw_env_get_module_power_mode(core, mlxsw_m_port->slot_index,
162c7ea08baSVadim Pasternak 					       mlxsw_m_port->module,
1630455dc50SIdo Schimmel 					       params, extack);
1640455dc50SIdo Schimmel }
1650455dc50SIdo Schimmel 
1660455dc50SIdo Schimmel 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)1670455dc50SIdo Schimmel mlxsw_m_set_module_power_mode(struct net_device *netdev,
1680455dc50SIdo Schimmel 			      const struct ethtool_module_power_mode_params *params,
1690455dc50SIdo Schimmel 			      struct netlink_ext_ack *extack)
1700455dc50SIdo Schimmel {
1710455dc50SIdo Schimmel 	struct mlxsw_m_port *mlxsw_m_port = netdev_priv(netdev);
1720455dc50SIdo Schimmel 	struct mlxsw_core *core = mlxsw_m_port->mlxsw_m->core;
1730455dc50SIdo Schimmel 
174c7ea08baSVadim Pasternak 	return mlxsw_env_set_module_power_mode(core, mlxsw_m_port->slot_index,
175c7ea08baSVadim Pasternak 					       mlxsw_m_port->module,
1760455dc50SIdo Schimmel 					       params->policy, extack);
1770455dc50SIdo Schimmel }
1780455dc50SIdo Schimmel 
179c100e47cSVadim Pasternak static const struct ethtool_ops mlxsw_m_port_ethtool_ops = {
1809bbd7efbSVadim Pasternak 	.get_drvinfo		= mlxsw_m_module_get_drvinfo,
181c100e47cSVadim Pasternak 	.get_module_info	= mlxsw_m_get_module_info,
182c100e47cSVadim Pasternak 	.get_module_eeprom	= mlxsw_m_get_module_eeprom,
1831e27b9e4SIdo Schimmel 	.get_module_eeprom_by_page = mlxsw_m_get_module_eeprom_by_page,
18449fd3b64SIdo Schimmel 	.reset			= mlxsw_m_reset,
1850455dc50SIdo Schimmel 	.get_module_power_mode	= mlxsw_m_get_module_power_mode,
1860455dc50SIdo Schimmel 	.set_module_power_mode	= mlxsw_m_set_module_power_mode,
187c100e47cSVadim Pasternak };
188c100e47cSVadim Pasternak 
189c100e47cSVadim Pasternak 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)190c934757dSAmit Cohen mlxsw_m_port_module_info_get(struct mlxsw_m *mlxsw_m, u16 local_port,
19101328e23SVadim Pasternak 			     u8 *p_module, u8 *p_width, u8 *p_slot_index)
192c100e47cSVadim Pasternak {
193c100e47cSVadim Pasternak 	char pmlp_pl[MLXSW_REG_PMLP_LEN];
194c100e47cSVadim Pasternak 	int err;
195c100e47cSVadim Pasternak 
196c100e47cSVadim Pasternak 	mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
197c100e47cSVadim Pasternak 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(pmlp), pmlp_pl);
198c100e47cSVadim Pasternak 	if (err)
199c100e47cSVadim Pasternak 		return err;
200c100e47cSVadim Pasternak 	*p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
201c100e47cSVadim Pasternak 	*p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
20201328e23SVadim Pasternak 	*p_slot_index = mlxsw_reg_pmlp_slot_index_get(pmlp_pl, 0);
203c100e47cSVadim Pasternak 
204c100e47cSVadim Pasternak 	return 0;
205c100e47cSVadim Pasternak }
206c100e47cSVadim Pasternak 
207c100e47cSVadim Pasternak static int
mlxsw_m_port_dev_addr_get(struct mlxsw_m_port * mlxsw_m_port)208c100e47cSVadim Pasternak mlxsw_m_port_dev_addr_get(struct mlxsw_m_port *mlxsw_m_port)
209c100e47cSVadim Pasternak {
210c100e47cSVadim Pasternak 	struct mlxsw_m *mlxsw_m = mlxsw_m_port->mlxsw_m;
211c100e47cSVadim Pasternak 	char ppad_pl[MLXSW_REG_PPAD_LEN];
212be755054SJakub Kicinski 	u8 addr[ETH_ALEN];
213c100e47cSVadim Pasternak 	int err;
214c100e47cSVadim Pasternak 
215c100e47cSVadim Pasternak 	mlxsw_reg_ppad_pack(ppad_pl, false, 0);
216c100e47cSVadim Pasternak 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(ppad), ppad_pl);
217c100e47cSVadim Pasternak 	if (err)
218c100e47cSVadim Pasternak 		return err;
219be755054SJakub Kicinski 	mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, addr);
220706ddb78SVadim Pasternak 	eth_hw_addr_gen(mlxsw_m_port->dev, addr, mlxsw_m_port->module + 1 +
221706ddb78SVadim Pasternak 			mlxsw_m_port->module_offset);
222c100e47cSVadim Pasternak 	return 0;
223c100e47cSVadim Pasternak }
224c100e47cSVadim Pasternak 
mlxsw_m_port_created(struct mlxsw_m * mlxsw_m,u16 local_port)22501328e23SVadim Pasternak static bool mlxsw_m_port_created(struct mlxsw_m *mlxsw_m, u16 local_port)
22601328e23SVadim Pasternak {
22701328e23SVadim Pasternak 	return mlxsw_m->ports[local_port];
22801328e23SVadim Pasternak }
22901328e23SVadim Pasternak 
230c100e47cSVadim Pasternak static int
mlxsw_m_port_create(struct mlxsw_m * mlxsw_m,u16 local_port,u8 slot_index,u8 module)231c7ea08baSVadim Pasternak mlxsw_m_port_create(struct mlxsw_m *mlxsw_m, u16 local_port, u8 slot_index,
232c7ea08baSVadim Pasternak 		    u8 module)
233c100e47cSVadim Pasternak {
234c100e47cSVadim Pasternak 	struct mlxsw_m_port *mlxsw_m_port;
235c100e47cSVadim Pasternak 	struct net_device *dev;
236c100e47cSVadim Pasternak 	int err;
237c100e47cSVadim Pasternak 
238c7ea08baSVadim Pasternak 	err = mlxsw_core_port_init(mlxsw_m->core, local_port, slot_index,
2391b604efbSDanielle Ratson 				   module + 1, false, 0, false,
2401b604efbSDanielle Ratson 				   0, mlxsw_m->base_mac,
241cdf29f4aSJiri Pirko 				   sizeof(mlxsw_m->base_mac));
242c100e47cSVadim Pasternak 	if (err) {
243c100e47cSVadim Pasternak 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to init core port\n",
244c100e47cSVadim Pasternak 			local_port);
245c100e47cSVadim Pasternak 		return err;
246c100e47cSVadim Pasternak 	}
247c100e47cSVadim Pasternak 
248c100e47cSVadim Pasternak 	dev = alloc_etherdev(sizeof(struct mlxsw_m_port));
249c100e47cSVadim Pasternak 	if (!dev) {
250c100e47cSVadim Pasternak 		err = -ENOMEM;
251c100e47cSVadim Pasternak 		goto err_alloc_etherdev;
252c100e47cSVadim Pasternak 	}
253c100e47cSVadim Pasternak 
254c100e47cSVadim Pasternak 	SET_NETDEV_DEV(dev, mlxsw_m->bus_info->dev);
2556b2a880fSJiri Pirko 	dev_net_set(dev, mlxsw_core_net(mlxsw_m->core));
256c100e47cSVadim Pasternak 	mlxsw_m_port = netdev_priv(dev);
257ac73d4bfSJiri Pirko 	mlxsw_core_port_netdev_link(mlxsw_m->core, local_port,
258ac73d4bfSJiri Pirko 				    mlxsw_m_port, dev);
259c100e47cSVadim Pasternak 	mlxsw_m_port->dev = dev;
260c100e47cSVadim Pasternak 	mlxsw_m_port->mlxsw_m = mlxsw_m;
261c100e47cSVadim Pasternak 	mlxsw_m_port->local_port = local_port;
262c100e47cSVadim Pasternak 	mlxsw_m_port->module = module;
263c7ea08baSVadim Pasternak 	mlxsw_m_port->slot_index = slot_index;
264706ddb78SVadim Pasternak 	/* Add module offset for line card. Offset for main board iz zero.
265706ddb78SVadim Pasternak 	 * For line card in slot #n offset is calculated as (#n - 1)
266706ddb78SVadim Pasternak 	 * multiplied by maximum modules number, which could be found on a line
267706ddb78SVadim Pasternak 	 * card.
268706ddb78SVadim Pasternak 	 */
269706ddb78SVadim Pasternak 	mlxsw_m_port->module_offset = mlxsw_m_port->slot_index ?
270706ddb78SVadim Pasternak 				      (mlxsw_m_port->slot_index - 1) *
271706ddb78SVadim Pasternak 				      mlxsw_m->max_modules_per_slot : 0;
272c100e47cSVadim Pasternak 
273c100e47cSVadim Pasternak 	dev->netdev_ops = &mlxsw_m_port_netdev_ops;
274c100e47cSVadim Pasternak 	dev->ethtool_ops = &mlxsw_m_port_ethtool_ops;
275c100e47cSVadim Pasternak 
276c100e47cSVadim Pasternak 	err = mlxsw_m_port_dev_addr_get(mlxsw_m_port);
277c100e47cSVadim Pasternak 	if (err) {
278c100e47cSVadim Pasternak 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Unable to get port mac address\n",
279c100e47cSVadim Pasternak 			mlxsw_m_port->local_port);
280c100e47cSVadim Pasternak 		goto err_dev_addr_get;
281c100e47cSVadim Pasternak 	}
282c100e47cSVadim Pasternak 
283c100e47cSVadim Pasternak 	netif_carrier_off(dev);
284c100e47cSVadim Pasternak 	mlxsw_m->ports[local_port] = mlxsw_m_port;
285c100e47cSVadim Pasternak 	err = register_netdev(dev);
286c100e47cSVadim Pasternak 	if (err) {
287c100e47cSVadim Pasternak 		dev_err(mlxsw_m->bus_info->dev, "Port %d: Failed to register netdev\n",
288c100e47cSVadim Pasternak 			mlxsw_m_port->local_port);
289c100e47cSVadim Pasternak 		goto err_register_netdev;
290c100e47cSVadim Pasternak 	}
291c100e47cSVadim Pasternak 
292c100e47cSVadim Pasternak 	return 0;
293c100e47cSVadim Pasternak 
294c100e47cSVadim Pasternak err_register_netdev:
295c100e47cSVadim Pasternak 	mlxsw_m->ports[local_port] = NULL;
296c100e47cSVadim Pasternak err_dev_addr_get:
2976dd4b4f3SChristophe JAILLET 	free_netdev(dev);
298c100e47cSVadim Pasternak err_alloc_etherdev:
299c100e47cSVadim Pasternak 	mlxsw_core_port_fini(mlxsw_m->core, local_port);
300c100e47cSVadim Pasternak 	return err;
301c100e47cSVadim Pasternak }
302c100e47cSVadim Pasternak 
mlxsw_m_port_remove(struct mlxsw_m * mlxsw_m,u16 local_port)303c934757dSAmit Cohen static void mlxsw_m_port_remove(struct mlxsw_m *mlxsw_m, u16 local_port)
304c100e47cSVadim Pasternak {
305c100e47cSVadim Pasternak 	struct mlxsw_m_port *mlxsw_m_port = mlxsw_m->ports[local_port];
306c100e47cSVadim Pasternak 
307c100e47cSVadim Pasternak 	unregister_netdev(mlxsw_m_port->dev); /* This calls ndo_stop */
308c100e47cSVadim Pasternak 	mlxsw_m->ports[local_port] = NULL;
309c100e47cSVadim Pasternak 	free_netdev(mlxsw_m_port->dev);
310c100e47cSVadim Pasternak 	mlxsw_core_port_fini(mlxsw_m->core, local_port);
311c100e47cSVadim Pasternak }
312c100e47cSVadim Pasternak 
31301328e23SVadim Pasternak static int*
mlxsw_m_port_mapping_get(struct mlxsw_m * mlxsw_m,u8 slot_index,u8 module)31401328e23SVadim Pasternak mlxsw_m_port_mapping_get(struct mlxsw_m *mlxsw_m, u8 slot_index, u8 module)
31501328e23SVadim Pasternak {
31601328e23SVadim Pasternak 	return &mlxsw_m->line_cards[slot_index]->module_to_port[module];
31701328e23SVadim Pasternak }
31801328e23SVadim Pasternak 
mlxsw_m_port_module_map(struct mlxsw_m * mlxsw_m,u16 local_port,u8 * last_module)319c934757dSAmit Cohen static int mlxsw_m_port_module_map(struct mlxsw_m *mlxsw_m, u16 local_port,
320c100e47cSVadim Pasternak 				   u8 *last_module)
321c100e47cSVadim Pasternak {
322837ec05cSDanielle Ratson 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
32301328e23SVadim Pasternak 	u8 module, width, slot_index;
32401328e23SVadim Pasternak 	int *module_to_port;
325c100e47cSVadim Pasternak 	int err;
326c100e47cSVadim Pasternak 
327c100e47cSVadim Pasternak 	/* Fill out to local port mapping array */
328c100e47cSVadim Pasternak 	err = mlxsw_m_port_module_info_get(mlxsw_m, local_port, &module,
32901328e23SVadim Pasternak 					   &width, &slot_index);
330c100e47cSVadim Pasternak 	if (err)
331c100e47cSVadim Pasternak 		return err;
332c100e47cSVadim Pasternak 
333706ddb78SVadim Pasternak 	/* Skip if line card has been already configured */
334706ddb78SVadim Pasternak 	if (mlxsw_m->line_cards[slot_index]->active)
335706ddb78SVadim Pasternak 		return 0;
336c100e47cSVadim Pasternak 	if (!width)
337c100e47cSVadim Pasternak 		return 0;
338c100e47cSVadim Pasternak 	/* Skip, if port belongs to the cluster */
339c100e47cSVadim Pasternak 	if (module == *last_module)
340c100e47cSVadim Pasternak 		return 0;
341c100e47cSVadim Pasternak 	*last_module = module;
342837ec05cSDanielle Ratson 
343837ec05cSDanielle Ratson 	if (WARN_ON_ONCE(module >= max_ports))
344837ec05cSDanielle Ratson 		return -EINVAL;
34501328e23SVadim Pasternak 	mlxsw_env_module_port_map(mlxsw_m->core, slot_index, module);
34601328e23SVadim Pasternak 	module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, module);
34701328e23SVadim Pasternak 	*module_to_port = local_port;
348c100e47cSVadim Pasternak 
349c100e47cSVadim Pasternak 	return 0;
350c100e47cSVadim Pasternak }
351c100e47cSVadim Pasternak 
352c7ea08baSVadim Pasternak static void
mlxsw_m_port_module_unmap(struct mlxsw_m * mlxsw_m,u8 slot_index,u8 module)353c7ea08baSVadim Pasternak mlxsw_m_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 slot_index, u8 module)
354c100e47cSVadim Pasternak {
35501328e23SVadim Pasternak 	int *module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index,
35601328e23SVadim Pasternak 						       module);
35701328e23SVadim Pasternak 	*module_to_port = -1;
358c7ea08baSVadim Pasternak 	mlxsw_env_module_port_unmap(mlxsw_m->core, slot_index, module);
359c100e47cSVadim Pasternak }
360c100e47cSVadim Pasternak 
mlxsw_m_linecards_init(struct mlxsw_m * mlxsw_m)3619421c8b8SVadim Pasternak static int mlxsw_m_linecards_init(struct mlxsw_m *mlxsw_m)
362c100e47cSVadim Pasternak {
363c100e47cSVadim Pasternak 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
36401328e23SVadim Pasternak 	char mgpir_pl[MLXSW_REG_MGPIR_LEN];
36501328e23SVadim Pasternak 	u8 num_of_modules;
36601328e23SVadim Pasternak 	int i, j, err;
36701328e23SVadim Pasternak 
36801328e23SVadim Pasternak 	mlxsw_reg_mgpir_pack(mgpir_pl, 0);
36901328e23SVadim Pasternak 	err = mlxsw_reg_query(mlxsw_m->core, MLXSW_REG(mgpir), mgpir_pl);
37001328e23SVadim Pasternak 	if (err)
37101328e23SVadim Pasternak 		return err;
37201328e23SVadim Pasternak 
37301328e23SVadim Pasternak 	mlxsw_reg_mgpir_unpack(mgpir_pl, NULL, NULL, NULL, &num_of_modules,
37401328e23SVadim Pasternak 			       &mlxsw_m->num_of_slots);
37501328e23SVadim Pasternak 	/* If the system is modular, get the maximum number of modules per-slot.
37601328e23SVadim Pasternak 	 * Otherwise, get the maximum number of modules on the main board.
37701328e23SVadim Pasternak 	 */
37801328e23SVadim Pasternak 	if (mlxsw_m->num_of_slots)
37901328e23SVadim Pasternak 		mlxsw_m->max_modules_per_slot =
38001328e23SVadim Pasternak 			mlxsw_reg_mgpir_max_modules_per_slot_get(mgpir_pl);
38101328e23SVadim Pasternak 	else
38201328e23SVadim Pasternak 		mlxsw_m->max_modules_per_slot = num_of_modules;
38301328e23SVadim Pasternak 	/* Add slot for main board. */
38401328e23SVadim Pasternak 	mlxsw_m->num_of_slots += 1;
385c100e47cSVadim Pasternak 
386c100e47cSVadim Pasternak 	mlxsw_m->ports = kcalloc(max_ports, sizeof(*mlxsw_m->ports),
387c100e47cSVadim Pasternak 				 GFP_KERNEL);
388c100e47cSVadim Pasternak 	if (!mlxsw_m->ports)
389c100e47cSVadim Pasternak 		return -ENOMEM;
390c100e47cSVadim Pasternak 
39101328e23SVadim Pasternak 	mlxsw_m->line_cards = kcalloc(mlxsw_m->num_of_slots,
39201328e23SVadim Pasternak 				      sizeof(*mlxsw_m->line_cards),
393c100e47cSVadim Pasternak 				      GFP_KERNEL);
39457688eb8SDan Carpenter 	if (!mlxsw_m->line_cards) {
39557688eb8SDan Carpenter 		err = -ENOMEM;
39601328e23SVadim Pasternak 		goto err_kcalloc;
39757688eb8SDan Carpenter 	}
398c100e47cSVadim Pasternak 
39901328e23SVadim Pasternak 	for (i = 0; i < mlxsw_m->num_of_slots; i++) {
40001328e23SVadim Pasternak 		mlxsw_m->line_cards[i] =
40101328e23SVadim Pasternak 			kzalloc(struct_size(mlxsw_m->line_cards[i],
40201328e23SVadim Pasternak 					    module_to_port,
40301328e23SVadim Pasternak 					    mlxsw_m->max_modules_per_slot),
40401328e23SVadim Pasternak 				GFP_KERNEL);
40557688eb8SDan Carpenter 		if (!mlxsw_m->line_cards[i]) {
40657688eb8SDan Carpenter 			err = -ENOMEM;
40701328e23SVadim Pasternak 			goto err_kmalloc_array;
40857688eb8SDan Carpenter 		}
40901328e23SVadim Pasternak 
41001328e23SVadim Pasternak 		/* Invalidate the entries of module to local port mapping array. */
41101328e23SVadim Pasternak 		for (j = 0; j < mlxsw_m->max_modules_per_slot; j++)
41201328e23SVadim Pasternak 			mlxsw_m->line_cards[i]->module_to_port[j] = -1;
41301328e23SVadim Pasternak 	}
414c100e47cSVadim Pasternak 
4159421c8b8SVadim Pasternak 	return 0;
4169421c8b8SVadim Pasternak 
41701328e23SVadim Pasternak err_kmalloc_array:
41801328e23SVadim Pasternak 	for (i--; i >= 0; i--)
41901328e23SVadim Pasternak 		kfree(mlxsw_m->line_cards[i]);
420*08fc7573SZhengchao Shao 	kfree(mlxsw_m->line_cards);
42101328e23SVadim Pasternak err_kcalloc:
4229421c8b8SVadim Pasternak 	kfree(mlxsw_m->ports);
4239421c8b8SVadim Pasternak 	return err;
4249421c8b8SVadim Pasternak }
4259421c8b8SVadim Pasternak 
mlxsw_m_linecards_fini(struct mlxsw_m * mlxsw_m)4269421c8b8SVadim Pasternak static void mlxsw_m_linecards_fini(struct mlxsw_m *mlxsw_m)
4279421c8b8SVadim Pasternak {
42801328e23SVadim Pasternak 	int i = mlxsw_m->num_of_slots;
42901328e23SVadim Pasternak 
43001328e23SVadim Pasternak 	for (i--; i >= 0; i--)
43101328e23SVadim Pasternak 		kfree(mlxsw_m->line_cards[i]);
43201328e23SVadim Pasternak 	kfree(mlxsw_m->line_cards);
4339421c8b8SVadim Pasternak 	kfree(mlxsw_m->ports);
4349421c8b8SVadim Pasternak }
4359421c8b8SVadim Pasternak 
43601328e23SVadim Pasternak static void
mlxsw_m_linecard_port_module_unmap(struct mlxsw_m * mlxsw_m,u8 slot_index)43701328e23SVadim Pasternak mlxsw_m_linecard_port_module_unmap(struct mlxsw_m *mlxsw_m, u8 slot_index)
43801328e23SVadim Pasternak {
43901328e23SVadim Pasternak 	int i;
44001328e23SVadim Pasternak 
44101328e23SVadim Pasternak 	for (i = mlxsw_m->max_modules_per_slot - 1; i >= 0; i--) {
44201328e23SVadim Pasternak 		int *module_to_port;
44301328e23SVadim Pasternak 
44401328e23SVadim Pasternak 		module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, i);
44501328e23SVadim Pasternak 		if (*module_to_port > 0)
44601328e23SVadim Pasternak 			mlxsw_m_port_module_unmap(mlxsw_m, slot_index, i);
44701328e23SVadim Pasternak 	}
44801328e23SVadim Pasternak }
44901328e23SVadim Pasternak 
45001328e23SVadim Pasternak static int
mlxsw_m_linecard_ports_create(struct mlxsw_m * mlxsw_m,u8 slot_index)45101328e23SVadim Pasternak mlxsw_m_linecard_ports_create(struct mlxsw_m *mlxsw_m, u8 slot_index)
45201328e23SVadim Pasternak {
45301328e23SVadim Pasternak 	int *module_to_port;
45401328e23SVadim Pasternak 	int i, err;
45501328e23SVadim Pasternak 
45601328e23SVadim Pasternak 	for (i = 0; i < mlxsw_m->max_modules_per_slot; i++) {
45701328e23SVadim Pasternak 		module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, i);
45801328e23SVadim Pasternak 		if (*module_to_port > 0) {
45901328e23SVadim Pasternak 			err = mlxsw_m_port_create(mlxsw_m, *module_to_port,
46001328e23SVadim Pasternak 						  slot_index, i);
46101328e23SVadim Pasternak 			if (err)
46201328e23SVadim Pasternak 				goto err_port_create;
46301328e23SVadim Pasternak 			/* Mark slot as active */
46401328e23SVadim Pasternak 			if (!mlxsw_m->line_cards[slot_index]->active)
46501328e23SVadim Pasternak 				mlxsw_m->line_cards[slot_index]->active = true;
46601328e23SVadim Pasternak 		}
46701328e23SVadim Pasternak 	}
46801328e23SVadim Pasternak 	return 0;
46901328e23SVadim Pasternak 
47001328e23SVadim Pasternak err_port_create:
47101328e23SVadim Pasternak 	for (i--; i >= 0; i--) {
47201328e23SVadim Pasternak 		module_to_port = mlxsw_m_port_mapping_get(mlxsw_m, slot_index, i);
47301328e23SVadim Pasternak 		if (*module_to_port > 0 &&
47401328e23SVadim Pasternak 		    mlxsw_m_port_created(mlxsw_m, *module_to_port)) {
47501328e23SVadim Pasternak 			mlxsw_m_port_remove(mlxsw_m, *module_to_port);
47601328e23SVadim Pasternak 			/* Mark slot as inactive */
47701328e23SVadim Pasternak 			if (mlxsw_m->line_cards[slot_index]->active)
47801328e23SVadim Pasternak 				mlxsw_m->line_cards[slot_index]->active = false;
47901328e23SVadim Pasternak 		}
48001328e23SVadim Pasternak 	}
48101328e23SVadim Pasternak 	return err;
48201328e23SVadim Pasternak }
48301328e23SVadim Pasternak 
48401328e23SVadim Pasternak static void
mlxsw_m_linecard_ports_remove(struct mlxsw_m * mlxsw_m,u8 slot_index)48501328e23SVadim Pasternak mlxsw_m_linecard_ports_remove(struct mlxsw_m *mlxsw_m, u8 slot_index)
48601328e23SVadim Pasternak {
48701328e23SVadim Pasternak 	int i;
48801328e23SVadim Pasternak 
48901328e23SVadim Pasternak 	for (i = 0; i < mlxsw_m->max_modules_per_slot; i++) {
49001328e23SVadim Pasternak 		int *module_to_port = mlxsw_m_port_mapping_get(mlxsw_m,
49101328e23SVadim Pasternak 							       slot_index, i);
49201328e23SVadim Pasternak 
49301328e23SVadim Pasternak 		if (*module_to_port > 0 &&
49401328e23SVadim Pasternak 		    mlxsw_m_port_created(mlxsw_m, *module_to_port)) {
49501328e23SVadim Pasternak 			mlxsw_m_port_remove(mlxsw_m, *module_to_port);
49601328e23SVadim Pasternak 			mlxsw_m_port_module_unmap(mlxsw_m, slot_index, i);
49701328e23SVadim Pasternak 		}
49801328e23SVadim Pasternak 	}
49901328e23SVadim Pasternak }
50001328e23SVadim Pasternak 
mlxsw_m_ports_module_map(struct mlxsw_m * mlxsw_m)50101328e23SVadim Pasternak static int mlxsw_m_ports_module_map(struct mlxsw_m *mlxsw_m)
50201328e23SVadim Pasternak {
50301328e23SVadim Pasternak 	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_m->core);
50401328e23SVadim Pasternak 	u8 last_module = max_ports;
50501328e23SVadim Pasternak 	int i, err;
50601328e23SVadim Pasternak 
50701328e23SVadim Pasternak 	for (i = 1; i < max_ports; i++) {
50801328e23SVadim Pasternak 		err = mlxsw_m_port_module_map(mlxsw_m, i, &last_module);
50901328e23SVadim Pasternak 		if (err)
51001328e23SVadim Pasternak 			return err;
51101328e23SVadim Pasternak 	}
51201328e23SVadim Pasternak 
51301328e23SVadim Pasternak 	return 0;
51401328e23SVadim Pasternak }
51501328e23SVadim Pasternak 
mlxsw_m_ports_create(struct mlxsw_m * mlxsw_m)5169421c8b8SVadim Pasternak static int mlxsw_m_ports_create(struct mlxsw_m *mlxsw_m)
5179421c8b8SVadim Pasternak {
5189421c8b8SVadim Pasternak 	int err;
5199421c8b8SVadim Pasternak 
520c100e47cSVadim Pasternak 	/* Fill out module to local port mapping array */
52101328e23SVadim Pasternak 	err = mlxsw_m_ports_module_map(mlxsw_m);
522c100e47cSVadim Pasternak 	if (err)
52301328e23SVadim Pasternak 		goto err_ports_module_map;
524c100e47cSVadim Pasternak 
525c100e47cSVadim Pasternak 	/* Create port objects for each valid entry */
52601328e23SVadim Pasternak 	err = mlxsw_m_linecard_ports_create(mlxsw_m, 0);
527c100e47cSVadim Pasternak 	if (err)
52801328e23SVadim Pasternak 		goto err_linecard_ports_create;
529c100e47cSVadim Pasternak 
530c100e47cSVadim Pasternak 	return 0;
531c100e47cSVadim Pasternak 
53201328e23SVadim Pasternak err_linecard_ports_create:
53301328e23SVadim Pasternak err_ports_module_map:
53401328e23SVadim Pasternak 	mlxsw_m_linecard_port_module_unmap(mlxsw_m, 0);
53501328e23SVadim Pasternak 
536c100e47cSVadim Pasternak 	return err;
537c100e47cSVadim Pasternak }
538c100e47cSVadim Pasternak 
mlxsw_m_ports_remove(struct mlxsw_m * mlxsw_m)539c100e47cSVadim Pasternak static void mlxsw_m_ports_remove(struct mlxsw_m *mlxsw_m)
540c100e47cSVadim Pasternak {
54101328e23SVadim Pasternak 	mlxsw_m_linecard_ports_remove(mlxsw_m, 0);
542c100e47cSVadim Pasternak }
543c100e47cSVadim Pasternak 
544706ddb78SVadim Pasternak static void
mlxsw_m_ports_remove_selected(struct mlxsw_core * mlxsw_core,bool (* selector)(void * priv,u16 local_port),void * priv)545706ddb78SVadim Pasternak mlxsw_m_ports_remove_selected(struct mlxsw_core *mlxsw_core,
546706ddb78SVadim Pasternak 			      bool (*selector)(void *priv, u16 local_port),
547706ddb78SVadim Pasternak 			      void *priv)
548706ddb78SVadim Pasternak {
549706ddb78SVadim Pasternak 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
550706ddb78SVadim Pasternak 	struct mlxsw_linecard *linecard_priv = priv;
551706ddb78SVadim Pasternak 	struct mlxsw_m_line_card *linecard;
552706ddb78SVadim Pasternak 
553706ddb78SVadim Pasternak 	linecard = mlxsw_m->line_cards[linecard_priv->slot_index];
554706ddb78SVadim Pasternak 
555706ddb78SVadim Pasternak 	if (WARN_ON(!linecard->active))
556706ddb78SVadim Pasternak 		return;
557706ddb78SVadim Pasternak 
558706ddb78SVadim Pasternak 	mlxsw_m_linecard_ports_remove(mlxsw_m, linecard_priv->slot_index);
559706ddb78SVadim Pasternak 	linecard->active = false;
560706ddb78SVadim Pasternak }
561706ddb78SVadim Pasternak 
mlxsw_m_fw_rev_validate(struct mlxsw_m * mlxsw_m)5626935af80SVadim Pasternak static int mlxsw_m_fw_rev_validate(struct mlxsw_m *mlxsw_m)
5636935af80SVadim Pasternak {
5646935af80SVadim Pasternak 	const struct mlxsw_fw_rev *rev = &mlxsw_m->bus_info->fw_rev;
5656935af80SVadim Pasternak 
5666935af80SVadim Pasternak 	/* Validate driver and FW are compatible.
5676935af80SVadim Pasternak 	 * Do not check major version, since it defines chip type, while
5686935af80SVadim Pasternak 	 * driver is supposed to support any type.
5696935af80SVadim Pasternak 	 */
5706935af80SVadim Pasternak 	if (mlxsw_core_fw_rev_minor_subminor_validate(rev, &mlxsw_m_fw_rev))
5716935af80SVadim Pasternak 		return 0;
5726935af80SVadim Pasternak 
5736935af80SVadim Pasternak 	dev_err(mlxsw_m->bus_info->dev, "The firmware version %d.%d.%d is incompatible with the driver (required >= %d.%d.%d)\n",
5746935af80SVadim Pasternak 		rev->major, rev->minor, rev->subminor, rev->major,
5756935af80SVadim Pasternak 		mlxsw_m_fw_rev.minor, mlxsw_m_fw_rev.subminor);
5766935af80SVadim Pasternak 
5776935af80SVadim Pasternak 	return -EINVAL;
5786935af80SVadim Pasternak }
5796935af80SVadim Pasternak 
580706ddb78SVadim Pasternak static void
mlxsw_m_got_active(struct mlxsw_core * mlxsw_core,u8 slot_index,void * priv)581706ddb78SVadim Pasternak mlxsw_m_got_active(struct mlxsw_core *mlxsw_core, u8 slot_index, void *priv)
582706ddb78SVadim Pasternak {
583706ddb78SVadim Pasternak 	struct mlxsw_m_line_card *linecard;
584706ddb78SVadim Pasternak 	struct mlxsw_m *mlxsw_m = priv;
585706ddb78SVadim Pasternak 	int err;
586706ddb78SVadim Pasternak 
587706ddb78SVadim Pasternak 	linecard = mlxsw_m->line_cards[slot_index];
588706ddb78SVadim Pasternak 	/* Skip if line card has been already configured during init */
589706ddb78SVadim Pasternak 	if (linecard->active)
590706ddb78SVadim Pasternak 		return;
591706ddb78SVadim Pasternak 
592706ddb78SVadim Pasternak 	/* Fill out module to local port mapping array */
593706ddb78SVadim Pasternak 	err = mlxsw_m_ports_module_map(mlxsw_m);
594706ddb78SVadim Pasternak 	if (err)
595706ddb78SVadim Pasternak 		goto err_ports_module_map;
596706ddb78SVadim Pasternak 
597706ddb78SVadim Pasternak 	/* Create port objects for each valid entry */
598706ddb78SVadim Pasternak 	err = mlxsw_m_linecard_ports_create(mlxsw_m, slot_index);
599706ddb78SVadim Pasternak 	if (err) {
600706ddb78SVadim Pasternak 		dev_err(mlxsw_m->bus_info->dev, "Failed to create port for line card at slot %d\n",
601706ddb78SVadim Pasternak 			slot_index);
602706ddb78SVadim Pasternak 		goto err_linecard_ports_create;
603706ddb78SVadim Pasternak 	}
604706ddb78SVadim Pasternak 
605706ddb78SVadim Pasternak 	linecard->active = true;
606706ddb78SVadim Pasternak 
607706ddb78SVadim Pasternak 	return;
608706ddb78SVadim Pasternak 
609706ddb78SVadim Pasternak err_linecard_ports_create:
610706ddb78SVadim Pasternak err_ports_module_map:
611706ddb78SVadim Pasternak 	mlxsw_m_linecard_port_module_unmap(mlxsw_m, slot_index);
612706ddb78SVadim Pasternak }
613706ddb78SVadim Pasternak 
614706ddb78SVadim Pasternak static void
mlxsw_m_got_inactive(struct mlxsw_core * mlxsw_core,u8 slot_index,void * priv)615706ddb78SVadim Pasternak mlxsw_m_got_inactive(struct mlxsw_core *mlxsw_core, u8 slot_index, void *priv)
616706ddb78SVadim Pasternak {
617706ddb78SVadim Pasternak 	struct mlxsw_m_line_card *linecard;
618706ddb78SVadim Pasternak 	struct mlxsw_m *mlxsw_m = priv;
619706ddb78SVadim Pasternak 
620706ddb78SVadim Pasternak 	linecard = mlxsw_m->line_cards[slot_index];
621706ddb78SVadim Pasternak 
622706ddb78SVadim Pasternak 	if (WARN_ON(!linecard->active))
623706ddb78SVadim Pasternak 		return;
624706ddb78SVadim Pasternak 
625706ddb78SVadim Pasternak 	mlxsw_m_linecard_ports_remove(mlxsw_m, slot_index);
626706ddb78SVadim Pasternak 	linecard->active = false;
627706ddb78SVadim Pasternak }
628706ddb78SVadim Pasternak 
629706ddb78SVadim Pasternak static struct mlxsw_linecards_event_ops mlxsw_m_event_ops = {
630706ddb78SVadim Pasternak 	.got_active = mlxsw_m_got_active,
631706ddb78SVadim Pasternak 	.got_inactive = mlxsw_m_got_inactive,
632706ddb78SVadim Pasternak };
633706ddb78SVadim Pasternak 
mlxsw_m_init(struct mlxsw_core * mlxsw_core,const struct mlxsw_bus_info * mlxsw_bus_info,struct netlink_ext_ack * extack)634c100e47cSVadim Pasternak static int mlxsw_m_init(struct mlxsw_core *mlxsw_core,
6355bcfb6a4SJiri Pirko 			const struct mlxsw_bus_info *mlxsw_bus_info,
6365bcfb6a4SJiri Pirko 			struct netlink_ext_ack *extack)
637c100e47cSVadim Pasternak {
638c100e47cSVadim Pasternak 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
639c100e47cSVadim Pasternak 	int err;
640c100e47cSVadim Pasternak 
641c100e47cSVadim Pasternak 	mlxsw_m->core = mlxsw_core;
642c100e47cSVadim Pasternak 	mlxsw_m->bus_info = mlxsw_bus_info;
643c100e47cSVadim Pasternak 
6446935af80SVadim Pasternak 	err = mlxsw_m_fw_rev_validate(mlxsw_m);
6456935af80SVadim Pasternak 	if (err)
6466935af80SVadim Pasternak 		return err;
6476935af80SVadim Pasternak 
648426aa1fcSJiri Pirko 	err = mlxsw_m_base_mac_get(mlxsw_m);
649426aa1fcSJiri Pirko 	if (err) {
650426aa1fcSJiri Pirko 		dev_err(mlxsw_m->bus_info->dev, "Failed to get base mac\n");
651426aa1fcSJiri Pirko 		return err;
652426aa1fcSJiri Pirko 	}
653426aa1fcSJiri Pirko 
6549421c8b8SVadim Pasternak 	err = mlxsw_m_linecards_init(mlxsw_m);
655c100e47cSVadim Pasternak 	if (err) {
6569421c8b8SVadim Pasternak 		dev_err(mlxsw_m->bus_info->dev, "Failed to create line cards\n");
657c100e47cSVadim Pasternak 		return err;
658c100e47cSVadim Pasternak 	}
659c100e47cSVadim Pasternak 
660706ddb78SVadim Pasternak 	err = mlxsw_linecards_event_ops_register(mlxsw_core,
661706ddb78SVadim Pasternak 						 &mlxsw_m_event_ops, mlxsw_m);
662706ddb78SVadim Pasternak 	if (err) {
663706ddb78SVadim Pasternak 		dev_err(mlxsw_m->bus_info->dev, "Failed to register line cards operations\n");
664706ddb78SVadim Pasternak 		goto linecards_event_ops_register;
665706ddb78SVadim Pasternak 	}
666706ddb78SVadim Pasternak 
6679421c8b8SVadim Pasternak 	err = mlxsw_m_ports_create(mlxsw_m);
6689421c8b8SVadim Pasternak 	if (err) {
6699421c8b8SVadim Pasternak 		dev_err(mlxsw_m->bus_info->dev, "Failed to create ports\n");
6709421c8b8SVadim Pasternak 		goto err_ports_create;
6719421c8b8SVadim Pasternak 	}
6729421c8b8SVadim Pasternak 
673c100e47cSVadim Pasternak 	return 0;
6749421c8b8SVadim Pasternak 
6759421c8b8SVadim Pasternak err_ports_create:
676706ddb78SVadim Pasternak 	mlxsw_linecards_event_ops_unregister(mlxsw_core,
677706ddb78SVadim Pasternak 					     &mlxsw_m_event_ops, mlxsw_m);
678706ddb78SVadim Pasternak linecards_event_ops_register:
6799421c8b8SVadim Pasternak 	mlxsw_m_linecards_fini(mlxsw_m);
6809421c8b8SVadim Pasternak 	return err;
681c100e47cSVadim Pasternak }
682c100e47cSVadim Pasternak 
mlxsw_m_fini(struct mlxsw_core * mlxsw_core)683c100e47cSVadim Pasternak static void mlxsw_m_fini(struct mlxsw_core *mlxsw_core)
684c100e47cSVadim Pasternak {
685c100e47cSVadim Pasternak 	struct mlxsw_m *mlxsw_m = mlxsw_core_driver_priv(mlxsw_core);
686c100e47cSVadim Pasternak 
687c100e47cSVadim Pasternak 	mlxsw_m_ports_remove(mlxsw_m);
688706ddb78SVadim Pasternak 	mlxsw_linecards_event_ops_unregister(mlxsw_core,
689706ddb78SVadim Pasternak 					     &mlxsw_m_event_ops, mlxsw_m);
6909421c8b8SVadim Pasternak 	mlxsw_m_linecards_fini(mlxsw_m);
691c100e47cSVadim Pasternak }
692c100e47cSVadim Pasternak 
6931ded391dSVadim Pasternak static const struct mlxsw_config_profile mlxsw_m_config_profile;
694d556e929SVadim Pasternak 
6951ded391dSVadim Pasternak static struct mlxsw_driver mlxsw_m_driver = {
6961ded391dSVadim Pasternak 	.kind			= mlxsw_m_driver_name,
697c100e47cSVadim Pasternak 	.priv_size		= sizeof(struct mlxsw_m),
698c100e47cSVadim Pasternak 	.init			= mlxsw_m_init,
699c100e47cSVadim Pasternak 	.fini			= mlxsw_m_fini,
700706ddb78SVadim Pasternak 	.ports_remove_selected	= mlxsw_m_ports_remove_selected,
7011ded391dSVadim Pasternak 	.profile		= &mlxsw_m_config_profile,
702d556e929SVadim Pasternak };
703d556e929SVadim Pasternak 
7041ded391dSVadim Pasternak static const struct i2c_device_id mlxsw_m_i2c_id[] = {
705d556e929SVadim Pasternak 	{ "mlxsw_minimal", 0},
706d556e929SVadim Pasternak 	{ },
707d556e929SVadim Pasternak };
708d556e929SVadim Pasternak 
7091ded391dSVadim Pasternak static struct i2c_driver mlxsw_m_i2c_driver = {
710d556e929SVadim Pasternak 	.driver.name = "mlxsw_minimal",
711d556e929SVadim Pasternak 	.class = I2C_CLASS_HWMON,
7121ded391dSVadim Pasternak 	.id_table = mlxsw_m_i2c_id,
713d556e929SVadim Pasternak };
714d556e929SVadim Pasternak 
mlxsw_m_module_init(void)7151ded391dSVadim Pasternak static int __init mlxsw_m_module_init(void)
716d556e929SVadim Pasternak {
717d556e929SVadim Pasternak 	int err;
718d556e929SVadim Pasternak 
7191ded391dSVadim Pasternak 	err = mlxsw_core_driver_register(&mlxsw_m_driver);
720d556e929SVadim Pasternak 	if (err)
721d556e929SVadim Pasternak 		return err;
722d556e929SVadim Pasternak 
7231ded391dSVadim Pasternak 	err = mlxsw_i2c_driver_register(&mlxsw_m_i2c_driver);
724d556e929SVadim Pasternak 	if (err)
725d556e929SVadim Pasternak 		goto err_i2c_driver_register;
726d556e929SVadim Pasternak 
727d556e929SVadim Pasternak 	return 0;
728d556e929SVadim Pasternak 
729d556e929SVadim Pasternak err_i2c_driver_register:
7301ded391dSVadim Pasternak 	mlxsw_core_driver_unregister(&mlxsw_m_driver);
731d556e929SVadim Pasternak 
732d556e929SVadim Pasternak 	return err;
733d556e929SVadim Pasternak }
734d556e929SVadim Pasternak 
mlxsw_m_module_exit(void)7351ded391dSVadim Pasternak static void __exit mlxsw_m_module_exit(void)
736d556e929SVadim Pasternak {
7371ded391dSVadim Pasternak 	mlxsw_i2c_driver_unregister(&mlxsw_m_i2c_driver);
7381ded391dSVadim Pasternak 	mlxsw_core_driver_unregister(&mlxsw_m_driver);
739d556e929SVadim Pasternak }
740d556e929SVadim Pasternak 
7411ded391dSVadim Pasternak module_init(mlxsw_m_module_init);
7421ded391dSVadim Pasternak module_exit(mlxsw_m_module_exit);
743d556e929SVadim Pasternak 
744d556e929SVadim Pasternak MODULE_LICENSE("Dual BSD/GPL");
745d556e929SVadim Pasternak MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
746d556e929SVadim Pasternak MODULE_DESCRIPTION("Mellanox minimal driver");
7471ded391dSVadim Pasternak MODULE_DEVICE_TABLE(i2c, mlxsw_m_i2c_id);
748