18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
28c2ecf20Sopenharmony_ci/* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
38c2ecf20Sopenharmony_ci
48c2ecf20Sopenharmony_ci#include <linux/kernel.h>
58c2ecf20Sopenharmony_ci#include <linux/module.h>
68c2ecf20Sopenharmony_ci#include <linux/types.h>
78c2ecf20Sopenharmony_ci#include <linux/pci.h>
88c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
98c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/device.h>
128c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
138c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "pci.h"
168c2ecf20Sopenharmony_ci#include "core.h"
178c2ecf20Sopenharmony_ci#include "reg.h"
188c2ecf20Sopenharmony_ci#include "port.h"
198c2ecf20Sopenharmony_ci#include "trap.h"
208c2ecf20Sopenharmony_ci#include "txheader.h"
218c2ecf20Sopenharmony_ci#include "ib.h"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistatic const char mlxsw_sx_driver_name[] = "mlxsw_switchx2";
248c2ecf20Sopenharmony_cistatic const char mlxsw_sx_driver_version[] = "1.0";
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_cistruct mlxsw_sx_port;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_cistruct mlxsw_sx {
298c2ecf20Sopenharmony_ci	struct mlxsw_sx_port **ports;
308c2ecf20Sopenharmony_ci	struct mlxsw_core *core;
318c2ecf20Sopenharmony_ci	const struct mlxsw_bus_info *bus_info;
328c2ecf20Sopenharmony_ci	u8 hw_id[ETH_ALEN];
338c2ecf20Sopenharmony_ci};
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_cistruct mlxsw_sx_port_pcpu_stats {
368c2ecf20Sopenharmony_ci	u64			rx_packets;
378c2ecf20Sopenharmony_ci	u64			rx_bytes;
388c2ecf20Sopenharmony_ci	u64			tx_packets;
398c2ecf20Sopenharmony_ci	u64			tx_bytes;
408c2ecf20Sopenharmony_ci	struct u64_stats_sync	syncp;
418c2ecf20Sopenharmony_ci	u32			tx_dropped;
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cistruct mlxsw_sx_port {
458c2ecf20Sopenharmony_ci	struct net_device *dev;
468c2ecf20Sopenharmony_ci	struct mlxsw_sx_port_pcpu_stats __percpu *pcpu_stats;
478c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx;
488c2ecf20Sopenharmony_ci	u8 local_port;
498c2ecf20Sopenharmony_ci	struct {
508c2ecf20Sopenharmony_ci		u8 module;
518c2ecf20Sopenharmony_ci	} mapping;
528c2ecf20Sopenharmony_ci};
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci/* tx_hdr_version
558c2ecf20Sopenharmony_ci * Tx header version.
568c2ecf20Sopenharmony_ci * Must be set to 0.
578c2ecf20Sopenharmony_ci */
588c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* tx_hdr_ctl
618c2ecf20Sopenharmony_ci * Packet control type.
628c2ecf20Sopenharmony_ci * 0 - Ethernet control (e.g. EMADs, LACP)
638c2ecf20Sopenharmony_ci * 1 - Ethernet data
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci/* tx_hdr_proto
688c2ecf20Sopenharmony_ci * Packet protocol type. Must be set to 1 (Ethernet).
698c2ecf20Sopenharmony_ci */
708c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci/* tx_hdr_etclass
738c2ecf20Sopenharmony_ci * Egress TClass to be used on the egress device on the egress port.
748c2ecf20Sopenharmony_ci * The MSB is specified in the 'ctclass3' field.
758c2ecf20Sopenharmony_ci * Range is 0-15, where 15 is the highest priority.
768c2ecf20Sopenharmony_ci */
778c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, etclass, 0x00, 18, 3);
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/* tx_hdr_swid
808c2ecf20Sopenharmony_ci * Switch partition ID.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* tx_hdr_port_mid
858c2ecf20Sopenharmony_ci * Destination local port for unicast packets.
868c2ecf20Sopenharmony_ci * Destination multicast ID for multicast packets.
878c2ecf20Sopenharmony_ci *
888c2ecf20Sopenharmony_ci * Control packets are directed to a specific egress port, while data
898c2ecf20Sopenharmony_ci * packets are transmitted through the CPU port (0) into the switch partition,
908c2ecf20Sopenharmony_ci * where forwarding rules are applied.
918c2ecf20Sopenharmony_ci */
928c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci/* tx_hdr_ctclass3
958c2ecf20Sopenharmony_ci * See field 'etclass'.
968c2ecf20Sopenharmony_ci */
978c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, ctclass3, 0x04, 14, 1);
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci/* tx_hdr_rdq
1008c2ecf20Sopenharmony_ci * RDQ for control packets sent to remote CPU.
1018c2ecf20Sopenharmony_ci * Must be set to 0x1F for EMADs, otherwise 0.
1028c2ecf20Sopenharmony_ci */
1038c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, rdq, 0x04, 9, 5);
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/* tx_hdr_cpu_sig
1068c2ecf20Sopenharmony_ci * Signature control for packets going to CPU. Must be set to 0.
1078c2ecf20Sopenharmony_ci */
1088c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, cpu_sig, 0x04, 0, 9);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/* tx_hdr_sig
1118c2ecf20Sopenharmony_ci * Stacking protocl signature. Must be set to 0xE0E0.
1128c2ecf20Sopenharmony_ci */
1138c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, sig, 0x0C, 16, 16);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/* tx_hdr_stclass
1168c2ecf20Sopenharmony_ci * Stacking TClass.
1178c2ecf20Sopenharmony_ci */
1188c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, stclass, 0x0C, 13, 3);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci/* tx_hdr_emad
1218c2ecf20Sopenharmony_ci * EMAD bit. Must be set for EMADs.
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, emad, 0x0C, 5, 1);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci/* tx_hdr_type
1268c2ecf20Sopenharmony_ci * 0 - Data packets
1278c2ecf20Sopenharmony_ci * 6 - Control packets
1288c2ecf20Sopenharmony_ci */
1298c2ecf20Sopenharmony_ciMLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic void mlxsw_sx_txhdr_construct(struct sk_buff *skb,
1328c2ecf20Sopenharmony_ci				     const struct mlxsw_tx_info *tx_info)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
1358c2ecf20Sopenharmony_ci	bool is_emad = tx_info->is_emad;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	memset(txhdr, 0, MLXSW_TXHDR_LEN);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/* We currently set default values for the egress tclass (QoS). */
1408c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_0);
1418c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
1428c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
1438c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_etclass_set(txhdr, is_emad ? MLXSW_TXHDR_ETCLASS_6 :
1448c2ecf20Sopenharmony_ci						  MLXSW_TXHDR_ETCLASS_5);
1458c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_swid_set(txhdr, 0);
1468c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
1478c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_ctclass3_set(txhdr, MLXSW_TXHDR_CTCLASS3);
1488c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_rdq_set(txhdr, is_emad ? MLXSW_TXHDR_RDQ_EMAD :
1498c2ecf20Sopenharmony_ci					      MLXSW_TXHDR_RDQ_OTHER);
1508c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_cpu_sig_set(txhdr, MLXSW_TXHDR_CPU_SIG);
1518c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_sig_set(txhdr, MLXSW_TXHDR_SIG);
1528c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_stclass_set(txhdr, MLXSW_TXHDR_STCLASS_NONE);
1538c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_emad_set(txhdr, is_emad ? MLXSW_TXHDR_EMAD :
1548c2ecf20Sopenharmony_ci					       MLXSW_TXHDR_NOT_EMAD);
1558c2ecf20Sopenharmony_ci	mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
1568c2ecf20Sopenharmony_ci}
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_admin_status_set(struct mlxsw_sx_port *mlxsw_sx_port,
1598c2ecf20Sopenharmony_ci					  bool is_up)
1608c2ecf20Sopenharmony_ci{
1618c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
1628c2ecf20Sopenharmony_ci	char paos_pl[MLXSW_REG_PAOS_LEN];
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci	mlxsw_reg_paos_pack(paos_pl, mlxsw_sx_port->local_port,
1658c2ecf20Sopenharmony_ci			    is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
1668c2ecf20Sopenharmony_ci			    MLXSW_PORT_ADMIN_STATUS_DOWN);
1678c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(paos), paos_pl);
1688c2ecf20Sopenharmony_ci}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_oper_status_get(struct mlxsw_sx_port *mlxsw_sx_port,
1718c2ecf20Sopenharmony_ci					 bool *p_is_up)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
1748c2ecf20Sopenharmony_ci	char paos_pl[MLXSW_REG_PAOS_LEN];
1758c2ecf20Sopenharmony_ci	u8 oper_status;
1768c2ecf20Sopenharmony_ci	int err;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	mlxsw_reg_paos_pack(paos_pl, mlxsw_sx_port->local_port, 0);
1798c2ecf20Sopenharmony_ci	err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(paos), paos_pl);
1808c2ecf20Sopenharmony_ci	if (err)
1818c2ecf20Sopenharmony_ci		return err;
1828c2ecf20Sopenharmony_ci	oper_status = mlxsw_reg_paos_oper_status_get(paos_pl);
1838c2ecf20Sopenharmony_ci	*p_is_up = oper_status == MLXSW_PORT_ADMIN_STATUS_UP;
1848c2ecf20Sopenharmony_ci	return 0;
1858c2ecf20Sopenharmony_ci}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_cistatic int __mlxsw_sx_port_mtu_set(struct mlxsw_sx_port *mlxsw_sx_port,
1888c2ecf20Sopenharmony_ci				   u16 mtu)
1898c2ecf20Sopenharmony_ci{
1908c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
1918c2ecf20Sopenharmony_ci	char pmtu_pl[MLXSW_REG_PMTU_LEN];
1928c2ecf20Sopenharmony_ci	int max_mtu;
1938c2ecf20Sopenharmony_ci	int err;
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sx_port->local_port, 0);
1968c2ecf20Sopenharmony_ci	err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(pmtu), pmtu_pl);
1978c2ecf20Sopenharmony_ci	if (err)
1988c2ecf20Sopenharmony_ci		return err;
1998c2ecf20Sopenharmony_ci	max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	if (mtu > max_mtu)
2028c2ecf20Sopenharmony_ci		return -EINVAL;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sx_port->local_port, mtu);
2058c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(pmtu), pmtu_pl);
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_mtu_eth_set(struct mlxsw_sx_port *mlxsw_sx_port,
2098c2ecf20Sopenharmony_ci				     u16 mtu)
2108c2ecf20Sopenharmony_ci{
2118c2ecf20Sopenharmony_ci	mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
2128c2ecf20Sopenharmony_ci	return __mlxsw_sx_port_mtu_set(mlxsw_sx_port, mtu);
2138c2ecf20Sopenharmony_ci}
2148c2ecf20Sopenharmony_ci
2158c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_mtu_ib_set(struct mlxsw_sx_port *mlxsw_sx_port,
2168c2ecf20Sopenharmony_ci				    u16 mtu)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	return __mlxsw_sx_port_mtu_set(mlxsw_sx_port, mtu);
2198c2ecf20Sopenharmony_ci}
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_ib_port_set(struct mlxsw_sx_port *mlxsw_sx_port,
2228c2ecf20Sopenharmony_ci				     u8 ib_port)
2238c2ecf20Sopenharmony_ci{
2248c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
2258c2ecf20Sopenharmony_ci	char plib_pl[MLXSW_REG_PLIB_LEN] = {0};
2268c2ecf20Sopenharmony_ci	int err;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	mlxsw_reg_plib_local_port_set(plib_pl, mlxsw_sx_port->local_port);
2298c2ecf20Sopenharmony_ci	mlxsw_reg_plib_ib_port_set(plib_pl, ib_port);
2308c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(plib), plib_pl);
2318c2ecf20Sopenharmony_ci	return err;
2328c2ecf20Sopenharmony_ci}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_swid_set(struct mlxsw_sx_port *mlxsw_sx_port, u8 swid)
2358c2ecf20Sopenharmony_ci{
2368c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
2378c2ecf20Sopenharmony_ci	char pspa_pl[MLXSW_REG_PSPA_LEN];
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sx_port->local_port);
2408c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(pspa), pspa_pl);
2418c2ecf20Sopenharmony_ci}
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_cistatic int
2448c2ecf20Sopenharmony_cimlxsw_sx_port_system_port_mapping_set(struct mlxsw_sx_port *mlxsw_sx_port)
2458c2ecf20Sopenharmony_ci{
2468c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
2478c2ecf20Sopenharmony_ci	char sspr_pl[MLXSW_REG_SSPR_LEN];
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sx_port->local_port);
2508c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sspr), sspr_pl);
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_module_info_get(struct mlxsw_sx *mlxsw_sx,
2548c2ecf20Sopenharmony_ci					 u8 local_port, u8 *p_module,
2558c2ecf20Sopenharmony_ci					 u8 *p_width)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	char pmlp_pl[MLXSW_REG_PMLP_LEN];
2588c2ecf20Sopenharmony_ci	int err;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
2618c2ecf20Sopenharmony_ci	err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(pmlp), pmlp_pl);
2628c2ecf20Sopenharmony_ci	if (err)
2638c2ecf20Sopenharmony_ci		return err;
2648c2ecf20Sopenharmony_ci	*p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
2658c2ecf20Sopenharmony_ci	*p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
2668c2ecf20Sopenharmony_ci	return 0;
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_open(struct net_device *dev)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
2728c2ecf20Sopenharmony_ci	int err;
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
2758c2ecf20Sopenharmony_ci	if (err)
2768c2ecf20Sopenharmony_ci		return err;
2778c2ecf20Sopenharmony_ci	netif_start_queue(dev);
2788c2ecf20Sopenharmony_ci	return 0;
2798c2ecf20Sopenharmony_ci}
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_stop(struct net_device *dev)
2828c2ecf20Sopenharmony_ci{
2838c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci	netif_stop_queue(dev);
2868c2ecf20Sopenharmony_ci	return mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
2878c2ecf20Sopenharmony_ci}
2888c2ecf20Sopenharmony_ci
2898c2ecf20Sopenharmony_cistatic netdev_tx_t mlxsw_sx_port_xmit(struct sk_buff *skb,
2908c2ecf20Sopenharmony_ci				      struct net_device *dev)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
2938c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
2948c2ecf20Sopenharmony_ci	struct mlxsw_sx_port_pcpu_stats *pcpu_stats;
2958c2ecf20Sopenharmony_ci	const struct mlxsw_tx_info tx_info = {
2968c2ecf20Sopenharmony_ci		.local_port = mlxsw_sx_port->local_port,
2978c2ecf20Sopenharmony_ci		.is_emad = false,
2988c2ecf20Sopenharmony_ci	};
2998c2ecf20Sopenharmony_ci	u64 len;
3008c2ecf20Sopenharmony_ci	int err;
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	if (skb_cow_head(skb, MLXSW_TXHDR_LEN)) {
3038c2ecf20Sopenharmony_ci		this_cpu_inc(mlxsw_sx_port->pcpu_stats->tx_dropped);
3048c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
3058c2ecf20Sopenharmony_ci		return NETDEV_TX_OK;
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	memset(skb->cb, 0, sizeof(struct mlxsw_skb_cb));
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	if (mlxsw_core_skb_transmit_busy(mlxsw_sx->core, &tx_info))
3118c2ecf20Sopenharmony_ci		return NETDEV_TX_BUSY;
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci	mlxsw_sx_txhdr_construct(skb, &tx_info);
3148c2ecf20Sopenharmony_ci	/* TX header is consumed by HW on the way so we shouldn't count its
3158c2ecf20Sopenharmony_ci	 * bytes as being sent.
3168c2ecf20Sopenharmony_ci	 */
3178c2ecf20Sopenharmony_ci	len = skb->len - MLXSW_TXHDR_LEN;
3188c2ecf20Sopenharmony_ci	/* Due to a race we might fail here because of a full queue. In that
3198c2ecf20Sopenharmony_ci	 * unlikely case we simply drop the packet.
3208c2ecf20Sopenharmony_ci	 */
3218c2ecf20Sopenharmony_ci	err = mlxsw_core_skb_transmit(mlxsw_sx->core, skb, &tx_info);
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	if (!err) {
3248c2ecf20Sopenharmony_ci		pcpu_stats = this_cpu_ptr(mlxsw_sx_port->pcpu_stats);
3258c2ecf20Sopenharmony_ci		u64_stats_update_begin(&pcpu_stats->syncp);
3268c2ecf20Sopenharmony_ci		pcpu_stats->tx_packets++;
3278c2ecf20Sopenharmony_ci		pcpu_stats->tx_bytes += len;
3288c2ecf20Sopenharmony_ci		u64_stats_update_end(&pcpu_stats->syncp);
3298c2ecf20Sopenharmony_ci	} else {
3308c2ecf20Sopenharmony_ci		this_cpu_inc(mlxsw_sx_port->pcpu_stats->tx_dropped);
3318c2ecf20Sopenharmony_ci		dev_kfree_skb_any(skb);
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_change_mtu(struct net_device *dev, int mtu)
3378c2ecf20Sopenharmony_ci{
3388c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
3398c2ecf20Sopenharmony_ci	int err;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_mtu_eth_set(mlxsw_sx_port, mtu);
3428c2ecf20Sopenharmony_ci	if (err)
3438c2ecf20Sopenharmony_ci		return err;
3448c2ecf20Sopenharmony_ci	dev->mtu = mtu;
3458c2ecf20Sopenharmony_ci	return 0;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic void
3498c2ecf20Sopenharmony_cimlxsw_sx_port_get_stats64(struct net_device *dev,
3508c2ecf20Sopenharmony_ci			  struct rtnl_link_stats64 *stats)
3518c2ecf20Sopenharmony_ci{
3528c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
3538c2ecf20Sopenharmony_ci	struct mlxsw_sx_port_pcpu_stats *p;
3548c2ecf20Sopenharmony_ci	u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
3558c2ecf20Sopenharmony_ci	u32 tx_dropped = 0;
3568c2ecf20Sopenharmony_ci	unsigned int start;
3578c2ecf20Sopenharmony_ci	int i;
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci	for_each_possible_cpu(i) {
3608c2ecf20Sopenharmony_ci		p = per_cpu_ptr(mlxsw_sx_port->pcpu_stats, i);
3618c2ecf20Sopenharmony_ci		do {
3628c2ecf20Sopenharmony_ci			start = u64_stats_fetch_begin_irq(&p->syncp);
3638c2ecf20Sopenharmony_ci			rx_packets	= p->rx_packets;
3648c2ecf20Sopenharmony_ci			rx_bytes	= p->rx_bytes;
3658c2ecf20Sopenharmony_ci			tx_packets	= p->tx_packets;
3668c2ecf20Sopenharmony_ci			tx_bytes	= p->tx_bytes;
3678c2ecf20Sopenharmony_ci		} while (u64_stats_fetch_retry_irq(&p->syncp, start));
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci		stats->rx_packets	+= rx_packets;
3708c2ecf20Sopenharmony_ci		stats->rx_bytes		+= rx_bytes;
3718c2ecf20Sopenharmony_ci		stats->tx_packets	+= tx_packets;
3728c2ecf20Sopenharmony_ci		stats->tx_bytes		+= tx_bytes;
3738c2ecf20Sopenharmony_ci		/* tx_dropped is u32, updated without syncp protection. */
3748c2ecf20Sopenharmony_ci		tx_dropped	+= p->tx_dropped;
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci	stats->tx_dropped	= tx_dropped;
3778c2ecf20Sopenharmony_ci}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_cistatic struct devlink_port *
3808c2ecf20Sopenharmony_cimlxsw_sx_port_get_devlink_port(struct net_device *dev)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
3838c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	return mlxsw_core_port_devlink_port_get(mlxsw_sx->core,
3868c2ecf20Sopenharmony_ci						mlxsw_sx_port->local_port);
3878c2ecf20Sopenharmony_ci}
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_cistatic const struct net_device_ops mlxsw_sx_port_netdev_ops = {
3908c2ecf20Sopenharmony_ci	.ndo_open		= mlxsw_sx_port_open,
3918c2ecf20Sopenharmony_ci	.ndo_stop		= mlxsw_sx_port_stop,
3928c2ecf20Sopenharmony_ci	.ndo_start_xmit		= mlxsw_sx_port_xmit,
3938c2ecf20Sopenharmony_ci	.ndo_change_mtu		= mlxsw_sx_port_change_mtu,
3948c2ecf20Sopenharmony_ci	.ndo_get_stats64	= mlxsw_sx_port_get_stats64,
3958c2ecf20Sopenharmony_ci	.ndo_get_devlink_port	= mlxsw_sx_port_get_devlink_port,
3968c2ecf20Sopenharmony_ci};
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic void mlxsw_sx_port_get_drvinfo(struct net_device *dev,
3998c2ecf20Sopenharmony_ci				      struct ethtool_drvinfo *drvinfo)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
4028c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	strlcpy(drvinfo->driver, mlxsw_sx_driver_name, sizeof(drvinfo->driver));
4058c2ecf20Sopenharmony_ci	strlcpy(drvinfo->version, mlxsw_sx_driver_version,
4068c2ecf20Sopenharmony_ci		sizeof(drvinfo->version));
4078c2ecf20Sopenharmony_ci	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
4088c2ecf20Sopenharmony_ci		 "%d.%d.%d",
4098c2ecf20Sopenharmony_ci		 mlxsw_sx->bus_info->fw_rev.major,
4108c2ecf20Sopenharmony_ci		 mlxsw_sx->bus_info->fw_rev.minor,
4118c2ecf20Sopenharmony_ci		 mlxsw_sx->bus_info->fw_rev.subminor);
4128c2ecf20Sopenharmony_ci	strlcpy(drvinfo->bus_info, mlxsw_sx->bus_info->device_name,
4138c2ecf20Sopenharmony_ci		sizeof(drvinfo->bus_info));
4148c2ecf20Sopenharmony_ci}
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_cistruct mlxsw_sx_port_hw_stats {
4178c2ecf20Sopenharmony_ci	char str[ETH_GSTRING_LEN];
4188c2ecf20Sopenharmony_ci	u64 (*getter)(const char *payload);
4198c2ecf20Sopenharmony_ci};
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_cistatic const struct mlxsw_sx_port_hw_stats mlxsw_sx_port_hw_stats[] = {
4228c2ecf20Sopenharmony_ci	{
4238c2ecf20Sopenharmony_ci		.str = "a_frames_transmitted_ok",
4248c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
4258c2ecf20Sopenharmony_ci	},
4268c2ecf20Sopenharmony_ci	{
4278c2ecf20Sopenharmony_ci		.str = "a_frames_received_ok",
4288c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
4298c2ecf20Sopenharmony_ci	},
4308c2ecf20Sopenharmony_ci	{
4318c2ecf20Sopenharmony_ci		.str = "a_frame_check_sequence_errors",
4328c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
4338c2ecf20Sopenharmony_ci	},
4348c2ecf20Sopenharmony_ci	{
4358c2ecf20Sopenharmony_ci		.str = "a_alignment_errors",
4368c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
4378c2ecf20Sopenharmony_ci	},
4388c2ecf20Sopenharmony_ci	{
4398c2ecf20Sopenharmony_ci		.str = "a_octets_transmitted_ok",
4408c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
4418c2ecf20Sopenharmony_ci	},
4428c2ecf20Sopenharmony_ci	{
4438c2ecf20Sopenharmony_ci		.str = "a_octets_received_ok",
4448c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
4458c2ecf20Sopenharmony_ci	},
4468c2ecf20Sopenharmony_ci	{
4478c2ecf20Sopenharmony_ci		.str = "a_multicast_frames_xmitted_ok",
4488c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
4498c2ecf20Sopenharmony_ci	},
4508c2ecf20Sopenharmony_ci	{
4518c2ecf20Sopenharmony_ci		.str = "a_broadcast_frames_xmitted_ok",
4528c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
4538c2ecf20Sopenharmony_ci	},
4548c2ecf20Sopenharmony_ci	{
4558c2ecf20Sopenharmony_ci		.str = "a_multicast_frames_received_ok",
4568c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
4578c2ecf20Sopenharmony_ci	},
4588c2ecf20Sopenharmony_ci	{
4598c2ecf20Sopenharmony_ci		.str = "a_broadcast_frames_received_ok",
4608c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
4618c2ecf20Sopenharmony_ci	},
4628c2ecf20Sopenharmony_ci	{
4638c2ecf20Sopenharmony_ci		.str = "a_in_range_length_errors",
4648c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
4658c2ecf20Sopenharmony_ci	},
4668c2ecf20Sopenharmony_ci	{
4678c2ecf20Sopenharmony_ci		.str = "a_out_of_range_length_field",
4688c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
4698c2ecf20Sopenharmony_ci	},
4708c2ecf20Sopenharmony_ci	{
4718c2ecf20Sopenharmony_ci		.str = "a_frame_too_long_errors",
4728c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
4738c2ecf20Sopenharmony_ci	},
4748c2ecf20Sopenharmony_ci	{
4758c2ecf20Sopenharmony_ci		.str = "a_symbol_error_during_carrier",
4768c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
4778c2ecf20Sopenharmony_ci	},
4788c2ecf20Sopenharmony_ci	{
4798c2ecf20Sopenharmony_ci		.str = "a_mac_control_frames_transmitted",
4808c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
4818c2ecf20Sopenharmony_ci	},
4828c2ecf20Sopenharmony_ci	{
4838c2ecf20Sopenharmony_ci		.str = "a_mac_control_frames_received",
4848c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
4858c2ecf20Sopenharmony_ci	},
4868c2ecf20Sopenharmony_ci	{
4878c2ecf20Sopenharmony_ci		.str = "a_unsupported_opcodes_received",
4888c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
4898c2ecf20Sopenharmony_ci	},
4908c2ecf20Sopenharmony_ci	{
4918c2ecf20Sopenharmony_ci		.str = "a_pause_mac_ctrl_frames_received",
4928c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
4938c2ecf20Sopenharmony_ci	},
4948c2ecf20Sopenharmony_ci	{
4958c2ecf20Sopenharmony_ci		.str = "a_pause_mac_ctrl_frames_xmitted",
4968c2ecf20Sopenharmony_ci		.getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
4978c2ecf20Sopenharmony_ci	},
4988c2ecf20Sopenharmony_ci};
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci#define MLXSW_SX_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sx_port_hw_stats)
5018c2ecf20Sopenharmony_ci
5028c2ecf20Sopenharmony_cistatic void mlxsw_sx_port_get_strings(struct net_device *dev,
5038c2ecf20Sopenharmony_ci				      u32 stringset, u8 *data)
5048c2ecf20Sopenharmony_ci{
5058c2ecf20Sopenharmony_ci	u8 *p = data;
5068c2ecf20Sopenharmony_ci	int i;
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci	switch (stringset) {
5098c2ecf20Sopenharmony_ci	case ETH_SS_STATS:
5108c2ecf20Sopenharmony_ci		for (i = 0; i < MLXSW_SX_PORT_HW_STATS_LEN; i++) {
5118c2ecf20Sopenharmony_ci			memcpy(p, mlxsw_sx_port_hw_stats[i].str,
5128c2ecf20Sopenharmony_ci			       ETH_GSTRING_LEN);
5138c2ecf20Sopenharmony_ci			p += ETH_GSTRING_LEN;
5148c2ecf20Sopenharmony_ci		}
5158c2ecf20Sopenharmony_ci		break;
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci}
5188c2ecf20Sopenharmony_ci
5198c2ecf20Sopenharmony_cistatic void mlxsw_sx_port_get_stats(struct net_device *dev,
5208c2ecf20Sopenharmony_ci				    struct ethtool_stats *stats, u64 *data)
5218c2ecf20Sopenharmony_ci{
5228c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
5238c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
5248c2ecf20Sopenharmony_ci	char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
5258c2ecf20Sopenharmony_ci	int i;
5268c2ecf20Sopenharmony_ci	int err;
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_ci	mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sx_port->local_port,
5298c2ecf20Sopenharmony_ci			     MLXSW_REG_PPCNT_IEEE_8023_CNT, 0);
5308c2ecf20Sopenharmony_ci	err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ppcnt), ppcnt_pl);
5318c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SX_PORT_HW_STATS_LEN; i++)
5328c2ecf20Sopenharmony_ci		data[i] = !err ? mlxsw_sx_port_hw_stats[i].getter(ppcnt_pl) : 0;
5338c2ecf20Sopenharmony_ci}
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_get_sset_count(struct net_device *dev, int sset)
5368c2ecf20Sopenharmony_ci{
5378c2ecf20Sopenharmony_ci	switch (sset) {
5388c2ecf20Sopenharmony_ci	case ETH_SS_STATS:
5398c2ecf20Sopenharmony_ci		return MLXSW_SX_PORT_HW_STATS_LEN;
5408c2ecf20Sopenharmony_ci	default:
5418c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
5428c2ecf20Sopenharmony_ci	}
5438c2ecf20Sopenharmony_ci}
5448c2ecf20Sopenharmony_ci
5458c2ecf20Sopenharmony_cistruct mlxsw_sx_port_link_mode {
5468c2ecf20Sopenharmony_ci	u32 mask;
5478c2ecf20Sopenharmony_ci	u32 supported;
5488c2ecf20Sopenharmony_ci	u32 advertised;
5498c2ecf20Sopenharmony_ci	u32 speed;
5508c2ecf20Sopenharmony_ci};
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_cistatic const struct mlxsw_sx_port_link_mode mlxsw_sx_port_link_mode[] = {
5538c2ecf20Sopenharmony_ci	{
5548c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_SGMII |
5558c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
5568c2ecf20Sopenharmony_ci		.supported	= SUPPORTED_1000baseKX_Full,
5578c2ecf20Sopenharmony_ci		.advertised	= ADVERTISED_1000baseKX_Full,
5588c2ecf20Sopenharmony_ci		.speed		= 1000,
5598c2ecf20Sopenharmony_ci	},
5608c2ecf20Sopenharmony_ci	{
5618c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
5628c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
5638c2ecf20Sopenharmony_ci		.supported	= SUPPORTED_10000baseKX4_Full,
5648c2ecf20Sopenharmony_ci		.advertised	= ADVERTISED_10000baseKX4_Full,
5658c2ecf20Sopenharmony_ci		.speed		= 10000,
5668c2ecf20Sopenharmony_ci	},
5678c2ecf20Sopenharmony_ci	{
5688c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
5698c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
5708c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
5718c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
5728c2ecf20Sopenharmony_ci		.supported	= SUPPORTED_10000baseKR_Full,
5738c2ecf20Sopenharmony_ci		.advertised	= ADVERTISED_10000baseKR_Full,
5748c2ecf20Sopenharmony_ci		.speed		= 10000,
5758c2ecf20Sopenharmony_ci	},
5768c2ecf20Sopenharmony_ci	{
5778c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
5788c2ecf20Sopenharmony_ci		.supported	= SUPPORTED_40000baseCR4_Full,
5798c2ecf20Sopenharmony_ci		.advertised	= ADVERTISED_40000baseCR4_Full,
5808c2ecf20Sopenharmony_ci		.speed		= 40000,
5818c2ecf20Sopenharmony_ci	},
5828c2ecf20Sopenharmony_ci	{
5838c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
5848c2ecf20Sopenharmony_ci		.supported	= SUPPORTED_40000baseKR4_Full,
5858c2ecf20Sopenharmony_ci		.advertised	= ADVERTISED_40000baseKR4_Full,
5868c2ecf20Sopenharmony_ci		.speed		= 40000,
5878c2ecf20Sopenharmony_ci	},
5888c2ecf20Sopenharmony_ci	{
5898c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
5908c2ecf20Sopenharmony_ci		.supported	= SUPPORTED_40000baseSR4_Full,
5918c2ecf20Sopenharmony_ci		.advertised	= ADVERTISED_40000baseSR4_Full,
5928c2ecf20Sopenharmony_ci		.speed		= 40000,
5938c2ecf20Sopenharmony_ci	},
5948c2ecf20Sopenharmony_ci	{
5958c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
5968c2ecf20Sopenharmony_ci		.supported	= SUPPORTED_40000baseLR4_Full,
5978c2ecf20Sopenharmony_ci		.advertised	= ADVERTISED_40000baseLR4_Full,
5988c2ecf20Sopenharmony_ci		.speed		= 40000,
5998c2ecf20Sopenharmony_ci	},
6008c2ecf20Sopenharmony_ci	{
6018c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR |
6028c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR |
6038c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
6048c2ecf20Sopenharmony_ci		.speed		= 25000,
6058c2ecf20Sopenharmony_ci	},
6068c2ecf20Sopenharmony_ci	{
6078c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR4 |
6088c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2 |
6098c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
6108c2ecf20Sopenharmony_ci		.speed		= 50000,
6118c2ecf20Sopenharmony_ci	},
6128c2ecf20Sopenharmony_ci	{
6138c2ecf20Sopenharmony_ci		.mask		= MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4 |
6148c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
6158c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
6168c2ecf20Sopenharmony_ci				  MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
6178c2ecf20Sopenharmony_ci		.speed		= 100000,
6188c2ecf20Sopenharmony_ci	},
6198c2ecf20Sopenharmony_ci};
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci#define MLXSW_SX_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sx_port_link_mode)
6228c2ecf20Sopenharmony_ci#define MLXSW_SX_PORT_BASE_SPEED 10000 /* Mb/s */
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_cistatic u32 mlxsw_sx_from_ptys_supported_port(u32 ptys_eth_proto)
6258c2ecf20Sopenharmony_ci{
6268c2ecf20Sopenharmony_ci	if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
6278c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
6288c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
6298c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
6308c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
6318c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_SGMII))
6328c2ecf20Sopenharmony_ci		return SUPPORTED_FIBRE;
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
6358c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
6368c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
6378c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
6388c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
6398c2ecf20Sopenharmony_ci		return SUPPORTED_Backplane;
6408c2ecf20Sopenharmony_ci	return 0;
6418c2ecf20Sopenharmony_ci}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_cistatic u32 mlxsw_sx_from_ptys_supported_link(u32 ptys_eth_proto)
6448c2ecf20Sopenharmony_ci{
6458c2ecf20Sopenharmony_ci	u32 modes = 0;
6468c2ecf20Sopenharmony_ci	int i;
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
6498c2ecf20Sopenharmony_ci		if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask)
6508c2ecf20Sopenharmony_ci			modes |= mlxsw_sx_port_link_mode[i].supported;
6518c2ecf20Sopenharmony_ci	}
6528c2ecf20Sopenharmony_ci	return modes;
6538c2ecf20Sopenharmony_ci}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_cistatic u32 mlxsw_sx_from_ptys_advert_link(u32 ptys_eth_proto)
6568c2ecf20Sopenharmony_ci{
6578c2ecf20Sopenharmony_ci	u32 modes = 0;
6588c2ecf20Sopenharmony_ci	int i;
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
6618c2ecf20Sopenharmony_ci		if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask)
6628c2ecf20Sopenharmony_ci			modes |= mlxsw_sx_port_link_mode[i].advertised;
6638c2ecf20Sopenharmony_ci	}
6648c2ecf20Sopenharmony_ci	return modes;
6658c2ecf20Sopenharmony_ci}
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_cistatic void mlxsw_sx_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto,
6688c2ecf20Sopenharmony_ci					    struct ethtool_link_ksettings *cmd)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	u32 speed = SPEED_UNKNOWN;
6718c2ecf20Sopenharmony_ci	u8 duplex = DUPLEX_UNKNOWN;
6728c2ecf20Sopenharmony_ci	int i;
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	if (!carrier_ok)
6758c2ecf20Sopenharmony_ci		goto out;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
6788c2ecf20Sopenharmony_ci		if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask) {
6798c2ecf20Sopenharmony_ci			speed = mlxsw_sx_port_link_mode[i].speed;
6808c2ecf20Sopenharmony_ci			duplex = DUPLEX_FULL;
6818c2ecf20Sopenharmony_ci			break;
6828c2ecf20Sopenharmony_ci		}
6838c2ecf20Sopenharmony_ci	}
6848c2ecf20Sopenharmony_ciout:
6858c2ecf20Sopenharmony_ci	cmd->base.speed = speed;
6868c2ecf20Sopenharmony_ci	cmd->base.duplex = duplex;
6878c2ecf20Sopenharmony_ci}
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_cistatic u8 mlxsw_sx_port_connector_port(u32 ptys_eth_proto)
6908c2ecf20Sopenharmony_ci{
6918c2ecf20Sopenharmony_ci	if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
6928c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
6938c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
6948c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_SGMII))
6958c2ecf20Sopenharmony_ci		return PORT_FIBRE;
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
6988c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
6998c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4))
7008c2ecf20Sopenharmony_ci		return PORT_DA;
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
7038c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
7048c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
7058c2ecf20Sopenharmony_ci			      MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4))
7068c2ecf20Sopenharmony_ci		return PORT_NONE;
7078c2ecf20Sopenharmony_ci
7088c2ecf20Sopenharmony_ci	return PORT_OTHER;
7098c2ecf20Sopenharmony_ci}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_cistatic int
7128c2ecf20Sopenharmony_cimlxsw_sx_port_get_link_ksettings(struct net_device *dev,
7138c2ecf20Sopenharmony_ci				 struct ethtool_link_ksettings *cmd)
7148c2ecf20Sopenharmony_ci{
7158c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
7168c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
7178c2ecf20Sopenharmony_ci	char ptys_pl[MLXSW_REG_PTYS_LEN];
7188c2ecf20Sopenharmony_ci	u32 eth_proto_cap;
7198c2ecf20Sopenharmony_ci	u32 eth_proto_admin;
7208c2ecf20Sopenharmony_ci	u32 eth_proto_oper;
7218c2ecf20Sopenharmony_ci	u32 supported, advertising, lp_advertising;
7228c2ecf20Sopenharmony_ci	int err;
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_ci	mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sx_port->local_port, 0, false);
7258c2ecf20Sopenharmony_ci	err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
7268c2ecf20Sopenharmony_ci	if (err) {
7278c2ecf20Sopenharmony_ci		netdev_err(dev, "Failed to get proto");
7288c2ecf20Sopenharmony_ci		return err;
7298c2ecf20Sopenharmony_ci	}
7308c2ecf20Sopenharmony_ci	mlxsw_reg_ptys_eth_unpack(ptys_pl, &eth_proto_cap,
7318c2ecf20Sopenharmony_ci				  &eth_proto_admin, &eth_proto_oper);
7328c2ecf20Sopenharmony_ci
7338c2ecf20Sopenharmony_ci	supported = mlxsw_sx_from_ptys_supported_port(eth_proto_cap) |
7348c2ecf20Sopenharmony_ci			 mlxsw_sx_from_ptys_supported_link(eth_proto_cap) |
7358c2ecf20Sopenharmony_ci			 SUPPORTED_Pause | SUPPORTED_Asym_Pause;
7368c2ecf20Sopenharmony_ci	advertising = mlxsw_sx_from_ptys_advert_link(eth_proto_admin);
7378c2ecf20Sopenharmony_ci	mlxsw_sx_from_ptys_speed_duplex(netif_carrier_ok(dev),
7388c2ecf20Sopenharmony_ci					eth_proto_oper, cmd);
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
7418c2ecf20Sopenharmony_ci	cmd->base.port = mlxsw_sx_port_connector_port(eth_proto_oper);
7428c2ecf20Sopenharmony_ci	lp_advertising = mlxsw_sx_from_ptys_advert_link(eth_proto_oper);
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
7458c2ecf20Sopenharmony_ci						supported);
7468c2ecf20Sopenharmony_ci	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
7478c2ecf20Sopenharmony_ci						advertising);
7488c2ecf20Sopenharmony_ci	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
7498c2ecf20Sopenharmony_ci						lp_advertising);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	return 0;
7528c2ecf20Sopenharmony_ci}
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_cistatic u32 mlxsw_sx_to_ptys_advert_link(u32 advertising)
7558c2ecf20Sopenharmony_ci{
7568c2ecf20Sopenharmony_ci	u32 ptys_proto = 0;
7578c2ecf20Sopenharmony_ci	int i;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
7608c2ecf20Sopenharmony_ci		if (advertising & mlxsw_sx_port_link_mode[i].advertised)
7618c2ecf20Sopenharmony_ci			ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
7628c2ecf20Sopenharmony_ci	}
7638c2ecf20Sopenharmony_ci	return ptys_proto;
7648c2ecf20Sopenharmony_ci}
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_cistatic u32 mlxsw_sx_to_ptys_speed(u32 speed)
7678c2ecf20Sopenharmony_ci{
7688c2ecf20Sopenharmony_ci	u32 ptys_proto = 0;
7698c2ecf20Sopenharmony_ci	int i;
7708c2ecf20Sopenharmony_ci
7718c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
7728c2ecf20Sopenharmony_ci		if (speed == mlxsw_sx_port_link_mode[i].speed)
7738c2ecf20Sopenharmony_ci			ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
7748c2ecf20Sopenharmony_ci	}
7758c2ecf20Sopenharmony_ci	return ptys_proto;
7768c2ecf20Sopenharmony_ci}
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_cistatic u32 mlxsw_sx_to_ptys_upper_speed(u32 upper_speed)
7798c2ecf20Sopenharmony_ci{
7808c2ecf20Sopenharmony_ci	u32 ptys_proto = 0;
7818c2ecf20Sopenharmony_ci	int i;
7828c2ecf20Sopenharmony_ci
7838c2ecf20Sopenharmony_ci	for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
7848c2ecf20Sopenharmony_ci		if (mlxsw_sx_port_link_mode[i].speed <= upper_speed)
7858c2ecf20Sopenharmony_ci			ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
7868c2ecf20Sopenharmony_ci	}
7878c2ecf20Sopenharmony_ci	return ptys_proto;
7888c2ecf20Sopenharmony_ci}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_cistatic int
7918c2ecf20Sopenharmony_cimlxsw_sx_port_set_link_ksettings(struct net_device *dev,
7928c2ecf20Sopenharmony_ci				 const struct ethtool_link_ksettings *cmd)
7938c2ecf20Sopenharmony_ci{
7948c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
7958c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
7968c2ecf20Sopenharmony_ci	char ptys_pl[MLXSW_REG_PTYS_LEN];
7978c2ecf20Sopenharmony_ci	u32 speed;
7988c2ecf20Sopenharmony_ci	u32 eth_proto_new;
7998c2ecf20Sopenharmony_ci	u32 eth_proto_cap;
8008c2ecf20Sopenharmony_ci	u32 eth_proto_admin;
8018c2ecf20Sopenharmony_ci	u32 advertising;
8028c2ecf20Sopenharmony_ci	bool is_up;
8038c2ecf20Sopenharmony_ci	int err;
8048c2ecf20Sopenharmony_ci
8058c2ecf20Sopenharmony_ci	speed = cmd->base.speed;
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	ethtool_convert_link_mode_to_legacy_u32(&advertising,
8088c2ecf20Sopenharmony_ci						cmd->link_modes.advertising);
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	eth_proto_new = cmd->base.autoneg == AUTONEG_ENABLE ?
8118c2ecf20Sopenharmony_ci		mlxsw_sx_to_ptys_advert_link(advertising) :
8128c2ecf20Sopenharmony_ci		mlxsw_sx_to_ptys_speed(speed);
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sx_port->local_port, 0, false);
8158c2ecf20Sopenharmony_ci	err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
8168c2ecf20Sopenharmony_ci	if (err) {
8178c2ecf20Sopenharmony_ci		netdev_err(dev, "Failed to get proto");
8188c2ecf20Sopenharmony_ci		return err;
8198c2ecf20Sopenharmony_ci	}
8208c2ecf20Sopenharmony_ci	mlxsw_reg_ptys_eth_unpack(ptys_pl, &eth_proto_cap, &eth_proto_admin,
8218c2ecf20Sopenharmony_ci				  NULL);
8228c2ecf20Sopenharmony_ci
8238c2ecf20Sopenharmony_ci	eth_proto_new = eth_proto_new & eth_proto_cap;
8248c2ecf20Sopenharmony_ci	if (!eth_proto_new) {
8258c2ecf20Sopenharmony_ci		netdev_err(dev, "Not supported proto admin requested");
8268c2ecf20Sopenharmony_ci		return -EINVAL;
8278c2ecf20Sopenharmony_ci	}
8288c2ecf20Sopenharmony_ci	if (eth_proto_new == eth_proto_admin)
8298c2ecf20Sopenharmony_ci		return 0;
8308c2ecf20Sopenharmony_ci
8318c2ecf20Sopenharmony_ci	mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sx_port->local_port,
8328c2ecf20Sopenharmony_ci				eth_proto_new, true);
8338c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
8348c2ecf20Sopenharmony_ci	if (err) {
8358c2ecf20Sopenharmony_ci		netdev_err(dev, "Failed to set proto admin");
8368c2ecf20Sopenharmony_ci		return err;
8378c2ecf20Sopenharmony_ci	}
8388c2ecf20Sopenharmony_ci
8398c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_oper_status_get(mlxsw_sx_port, &is_up);
8408c2ecf20Sopenharmony_ci	if (err) {
8418c2ecf20Sopenharmony_ci		netdev_err(dev, "Failed to get oper status");
8428c2ecf20Sopenharmony_ci		return err;
8438c2ecf20Sopenharmony_ci	}
8448c2ecf20Sopenharmony_ci	if (!is_up)
8458c2ecf20Sopenharmony_ci		return 0;
8468c2ecf20Sopenharmony_ci
8478c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
8488c2ecf20Sopenharmony_ci	if (err) {
8498c2ecf20Sopenharmony_ci		netdev_err(dev, "Failed to set admin status");
8508c2ecf20Sopenharmony_ci		return err;
8518c2ecf20Sopenharmony_ci	}
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
8548c2ecf20Sopenharmony_ci	if (err) {
8558c2ecf20Sopenharmony_ci		netdev_err(dev, "Failed to set admin status");
8568c2ecf20Sopenharmony_ci		return err;
8578c2ecf20Sopenharmony_ci	}
8588c2ecf20Sopenharmony_ci
8598c2ecf20Sopenharmony_ci	return 0;
8608c2ecf20Sopenharmony_ci}
8618c2ecf20Sopenharmony_ci
8628c2ecf20Sopenharmony_cistatic const struct ethtool_ops mlxsw_sx_port_ethtool_ops = {
8638c2ecf20Sopenharmony_ci	.get_drvinfo		= mlxsw_sx_port_get_drvinfo,
8648c2ecf20Sopenharmony_ci	.get_link		= ethtool_op_get_link,
8658c2ecf20Sopenharmony_ci	.get_strings		= mlxsw_sx_port_get_strings,
8668c2ecf20Sopenharmony_ci	.get_ethtool_stats	= mlxsw_sx_port_get_stats,
8678c2ecf20Sopenharmony_ci	.get_sset_count		= mlxsw_sx_port_get_sset_count,
8688c2ecf20Sopenharmony_ci	.get_link_ksettings	= mlxsw_sx_port_get_link_ksettings,
8698c2ecf20Sopenharmony_ci	.set_link_ksettings	= mlxsw_sx_port_set_link_ksettings,
8708c2ecf20Sopenharmony_ci};
8718c2ecf20Sopenharmony_ci
8728c2ecf20Sopenharmony_cistatic int mlxsw_sx_hw_id_get(struct mlxsw_sx *mlxsw_sx)
8738c2ecf20Sopenharmony_ci{
8748c2ecf20Sopenharmony_ci	char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
8758c2ecf20Sopenharmony_ci	int err;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(spad), spad_pl);
8788c2ecf20Sopenharmony_ci	if (err)
8798c2ecf20Sopenharmony_ci		return err;
8808c2ecf20Sopenharmony_ci	mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sx->hw_id);
8818c2ecf20Sopenharmony_ci	return 0;
8828c2ecf20Sopenharmony_ci}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_dev_addr_get(struct mlxsw_sx_port *mlxsw_sx_port)
8858c2ecf20Sopenharmony_ci{
8868c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
8878c2ecf20Sopenharmony_ci	struct net_device *dev = mlxsw_sx_port->dev;
8888c2ecf20Sopenharmony_ci	char ppad_pl[MLXSW_REG_PPAD_LEN];
8898c2ecf20Sopenharmony_ci	int err;
8908c2ecf20Sopenharmony_ci
8918c2ecf20Sopenharmony_ci	mlxsw_reg_ppad_pack(ppad_pl, false, 0);
8928c2ecf20Sopenharmony_ci	err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ppad), ppad_pl);
8938c2ecf20Sopenharmony_ci	if (err)
8948c2ecf20Sopenharmony_ci		return err;
8958c2ecf20Sopenharmony_ci	mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, dev->dev_addr);
8968c2ecf20Sopenharmony_ci	/* The last byte value in base mac address is guaranteed
8978c2ecf20Sopenharmony_ci	 * to be such it does not overflow when adding local_port
8988c2ecf20Sopenharmony_ci	 * value.
8998c2ecf20Sopenharmony_ci	 */
9008c2ecf20Sopenharmony_ci	dev->dev_addr[ETH_ALEN - 1] += mlxsw_sx_port->local_port;
9018c2ecf20Sopenharmony_ci	return 0;
9028c2ecf20Sopenharmony_ci}
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_stp_state_set(struct mlxsw_sx_port *mlxsw_sx_port,
9058c2ecf20Sopenharmony_ci				       u16 vid, enum mlxsw_reg_spms_state state)
9068c2ecf20Sopenharmony_ci{
9078c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
9088c2ecf20Sopenharmony_ci	char *spms_pl;
9098c2ecf20Sopenharmony_ci	int err;
9108c2ecf20Sopenharmony_ci
9118c2ecf20Sopenharmony_ci	spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
9128c2ecf20Sopenharmony_ci	if (!spms_pl)
9138c2ecf20Sopenharmony_ci		return -ENOMEM;
9148c2ecf20Sopenharmony_ci	mlxsw_reg_spms_pack(spms_pl, mlxsw_sx_port->local_port);
9158c2ecf20Sopenharmony_ci	mlxsw_reg_spms_vid_pack(spms_pl, vid, state);
9168c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(spms), spms_pl);
9178c2ecf20Sopenharmony_ci	kfree(spms_pl);
9188c2ecf20Sopenharmony_ci	return err;
9198c2ecf20Sopenharmony_ci}
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_ib_speed_set(struct mlxsw_sx_port *mlxsw_sx_port,
9228c2ecf20Sopenharmony_ci				      u16 speed, u16 width)
9238c2ecf20Sopenharmony_ci{
9248c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
9258c2ecf20Sopenharmony_ci	char ptys_pl[MLXSW_REG_PTYS_LEN];
9268c2ecf20Sopenharmony_ci
9278c2ecf20Sopenharmony_ci	mlxsw_reg_ptys_ib_pack(ptys_pl, mlxsw_sx_port->local_port, speed,
9288c2ecf20Sopenharmony_ci			       width);
9298c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
9308c2ecf20Sopenharmony_ci}
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_cistatic int
9338c2ecf20Sopenharmony_cimlxsw_sx_port_speed_by_width_set(struct mlxsw_sx_port *mlxsw_sx_port, u8 width)
9348c2ecf20Sopenharmony_ci{
9358c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
9368c2ecf20Sopenharmony_ci	u32 upper_speed = MLXSW_SX_PORT_BASE_SPEED * width;
9378c2ecf20Sopenharmony_ci	char ptys_pl[MLXSW_REG_PTYS_LEN];
9388c2ecf20Sopenharmony_ci	u32 eth_proto_admin;
9398c2ecf20Sopenharmony_ci
9408c2ecf20Sopenharmony_ci	eth_proto_admin = mlxsw_sx_to_ptys_upper_speed(upper_speed);
9418c2ecf20Sopenharmony_ci	mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sx_port->local_port,
9428c2ecf20Sopenharmony_ci				eth_proto_admin, true);
9438c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
9448c2ecf20Sopenharmony_ci}
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_cistatic int
9478c2ecf20Sopenharmony_cimlxsw_sx_port_mac_learning_mode_set(struct mlxsw_sx_port *mlxsw_sx_port,
9488c2ecf20Sopenharmony_ci				    enum mlxsw_reg_spmlr_learn_mode mode)
9498c2ecf20Sopenharmony_ci{
9508c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
9518c2ecf20Sopenharmony_ci	char spmlr_pl[MLXSW_REG_SPMLR_LEN];
9528c2ecf20Sopenharmony_ci
9538c2ecf20Sopenharmony_ci	mlxsw_reg_spmlr_pack(spmlr_pl, mlxsw_sx_port->local_port, mode);
9548c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(spmlr), spmlr_pl);
9558c2ecf20Sopenharmony_ci}
9568c2ecf20Sopenharmony_ci
9578c2ecf20Sopenharmony_cistatic int __mlxsw_sx_port_eth_create(struct mlxsw_sx *mlxsw_sx, u8 local_port,
9588c2ecf20Sopenharmony_ci				      u8 module, u8 width)
9598c2ecf20Sopenharmony_ci{
9608c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port;
9618c2ecf20Sopenharmony_ci	struct net_device *dev;
9628c2ecf20Sopenharmony_ci	int err;
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	dev = alloc_etherdev(sizeof(struct mlxsw_sx_port));
9658c2ecf20Sopenharmony_ci	if (!dev)
9668c2ecf20Sopenharmony_ci		return -ENOMEM;
9678c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(dev, mlxsw_sx->bus_info->dev);
9688c2ecf20Sopenharmony_ci	dev_net_set(dev, mlxsw_core_net(mlxsw_sx->core));
9698c2ecf20Sopenharmony_ci	mlxsw_sx_port = netdev_priv(dev);
9708c2ecf20Sopenharmony_ci	mlxsw_sx_port->dev = dev;
9718c2ecf20Sopenharmony_ci	mlxsw_sx_port->mlxsw_sx = mlxsw_sx;
9728c2ecf20Sopenharmony_ci	mlxsw_sx_port->local_port = local_port;
9738c2ecf20Sopenharmony_ci	mlxsw_sx_port->mapping.module = module;
9748c2ecf20Sopenharmony_ci
9758c2ecf20Sopenharmony_ci	mlxsw_sx_port->pcpu_stats =
9768c2ecf20Sopenharmony_ci		netdev_alloc_pcpu_stats(struct mlxsw_sx_port_pcpu_stats);
9778c2ecf20Sopenharmony_ci	if (!mlxsw_sx_port->pcpu_stats) {
9788c2ecf20Sopenharmony_ci		err = -ENOMEM;
9798c2ecf20Sopenharmony_ci		goto err_alloc_stats;
9808c2ecf20Sopenharmony_ci	}
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	dev->netdev_ops = &mlxsw_sx_port_netdev_ops;
9838c2ecf20Sopenharmony_ci	dev->ethtool_ops = &mlxsw_sx_port_ethtool_ops;
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_dev_addr_get(mlxsw_sx_port);
9868c2ecf20Sopenharmony_ci	if (err) {
9878c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Unable to get port mac address\n",
9888c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
9898c2ecf20Sopenharmony_ci		goto err_dev_addr_get;
9908c2ecf20Sopenharmony_ci	}
9918c2ecf20Sopenharmony_ci
9928c2ecf20Sopenharmony_ci	netif_carrier_off(dev);
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci	dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
9958c2ecf20Sopenharmony_ci			 NETIF_F_VLAN_CHALLENGED;
9968c2ecf20Sopenharmony_ci
9978c2ecf20Sopenharmony_ci	dev->min_mtu = 0;
9988c2ecf20Sopenharmony_ci	dev->max_mtu = ETH_MAX_MTU;
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	/* Each packet needs to have a Tx header (metadata) on top all other
10018c2ecf20Sopenharmony_ci	 * headers.
10028c2ecf20Sopenharmony_ci	 */
10038c2ecf20Sopenharmony_ci	dev->needed_headroom = MLXSW_TXHDR_LEN;
10048c2ecf20Sopenharmony_ci
10058c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_system_port_mapping_set(mlxsw_sx_port);
10068c2ecf20Sopenharmony_ci	if (err) {
10078c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set system port mapping\n",
10088c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
10098c2ecf20Sopenharmony_ci		goto err_port_system_port_mapping_set;
10108c2ecf20Sopenharmony_ci	}
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_swid_set(mlxsw_sx_port, 0);
10138c2ecf20Sopenharmony_ci	if (err) {
10148c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set SWID\n",
10158c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
10168c2ecf20Sopenharmony_ci		goto err_port_swid_set;
10178c2ecf20Sopenharmony_ci	}
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_speed_by_width_set(mlxsw_sx_port, width);
10208c2ecf20Sopenharmony_ci	if (err) {
10218c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set speed\n",
10228c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
10238c2ecf20Sopenharmony_ci		goto err_port_speed_set;
10248c2ecf20Sopenharmony_ci	}
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_mtu_eth_set(mlxsw_sx_port, ETH_DATA_LEN);
10278c2ecf20Sopenharmony_ci	if (err) {
10288c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MTU\n",
10298c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
10308c2ecf20Sopenharmony_ci		goto err_port_mtu_set;
10318c2ecf20Sopenharmony_ci	}
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
10348c2ecf20Sopenharmony_ci	if (err)
10358c2ecf20Sopenharmony_ci		goto err_port_admin_status_set;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_stp_state_set(mlxsw_sx_port,
10388c2ecf20Sopenharmony_ci					  MLXSW_PORT_DEFAULT_VID,
10398c2ecf20Sopenharmony_ci					  MLXSW_REG_SPMS_STATE_FORWARDING);
10408c2ecf20Sopenharmony_ci	if (err) {
10418c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set STP state\n",
10428c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
10438c2ecf20Sopenharmony_ci		goto err_port_stp_state_set;
10448c2ecf20Sopenharmony_ci	}
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_mac_learning_mode_set(mlxsw_sx_port,
10478c2ecf20Sopenharmony_ci						  MLXSW_REG_SPMLR_LEARN_MODE_DISABLE);
10488c2ecf20Sopenharmony_ci	if (err) {
10498c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MAC learning mode\n",
10508c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
10518c2ecf20Sopenharmony_ci		goto err_port_mac_learning_mode_set;
10528c2ecf20Sopenharmony_ci	}
10538c2ecf20Sopenharmony_ci
10548c2ecf20Sopenharmony_ci	err = register_netdev(dev);
10558c2ecf20Sopenharmony_ci	if (err) {
10568c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to register netdev\n",
10578c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
10588c2ecf20Sopenharmony_ci		goto err_register_netdev;
10598c2ecf20Sopenharmony_ci	}
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	mlxsw_core_port_eth_set(mlxsw_sx->core, mlxsw_sx_port->local_port,
10628c2ecf20Sopenharmony_ci				mlxsw_sx_port, dev);
10638c2ecf20Sopenharmony_ci	mlxsw_sx->ports[local_port] = mlxsw_sx_port;
10648c2ecf20Sopenharmony_ci	return 0;
10658c2ecf20Sopenharmony_ci
10668c2ecf20Sopenharmony_cierr_register_netdev:
10678c2ecf20Sopenharmony_cierr_port_mac_learning_mode_set:
10688c2ecf20Sopenharmony_cierr_port_stp_state_set:
10698c2ecf20Sopenharmony_cierr_port_admin_status_set:
10708c2ecf20Sopenharmony_cierr_port_mtu_set:
10718c2ecf20Sopenharmony_cierr_port_speed_set:
10728c2ecf20Sopenharmony_ci	mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
10738c2ecf20Sopenharmony_cierr_port_swid_set:
10748c2ecf20Sopenharmony_cierr_port_system_port_mapping_set:
10758c2ecf20Sopenharmony_cierr_dev_addr_get:
10768c2ecf20Sopenharmony_ci	free_percpu(mlxsw_sx_port->pcpu_stats);
10778c2ecf20Sopenharmony_cierr_alloc_stats:
10788c2ecf20Sopenharmony_ci	free_netdev(dev);
10798c2ecf20Sopenharmony_ci	return err;
10808c2ecf20Sopenharmony_ci}
10818c2ecf20Sopenharmony_ci
10828c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_eth_create(struct mlxsw_sx *mlxsw_sx, u8 local_port,
10838c2ecf20Sopenharmony_ci				    u8 module, u8 width)
10848c2ecf20Sopenharmony_ci{
10858c2ecf20Sopenharmony_ci	int err;
10868c2ecf20Sopenharmony_ci
10878c2ecf20Sopenharmony_ci	err = mlxsw_core_port_init(mlxsw_sx->core, local_port,
10888c2ecf20Sopenharmony_ci				   module + 1, false, 0, false, 0,
10898c2ecf20Sopenharmony_ci				   mlxsw_sx->hw_id, sizeof(mlxsw_sx->hw_id));
10908c2ecf20Sopenharmony_ci	if (err) {
10918c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to init core port\n",
10928c2ecf20Sopenharmony_ci			local_port);
10938c2ecf20Sopenharmony_ci		return err;
10948c2ecf20Sopenharmony_ci	}
10958c2ecf20Sopenharmony_ci	err = __mlxsw_sx_port_eth_create(mlxsw_sx, local_port, module, width);
10968c2ecf20Sopenharmony_ci	if (err)
10978c2ecf20Sopenharmony_ci		goto err_port_create;
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	return 0;
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_cierr_port_create:
11028c2ecf20Sopenharmony_ci	mlxsw_core_port_fini(mlxsw_sx->core, local_port);
11038c2ecf20Sopenharmony_ci	return err;
11048c2ecf20Sopenharmony_ci}
11058c2ecf20Sopenharmony_ci
11068c2ecf20Sopenharmony_cistatic void __mlxsw_sx_port_eth_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
11078c2ecf20Sopenharmony_ci{
11088c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
11098c2ecf20Sopenharmony_ci
11108c2ecf20Sopenharmony_ci	mlxsw_core_port_clear(mlxsw_sx->core, local_port, mlxsw_sx);
11118c2ecf20Sopenharmony_ci	unregister_netdev(mlxsw_sx_port->dev); /* This calls ndo_stop */
11128c2ecf20Sopenharmony_ci	mlxsw_sx->ports[local_port] = NULL;
11138c2ecf20Sopenharmony_ci	mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
11148c2ecf20Sopenharmony_ci	free_percpu(mlxsw_sx_port->pcpu_stats);
11158c2ecf20Sopenharmony_ci	free_netdev(mlxsw_sx_port->dev);
11168c2ecf20Sopenharmony_ci}
11178c2ecf20Sopenharmony_ci
11188c2ecf20Sopenharmony_cistatic bool mlxsw_sx_port_created(struct mlxsw_sx *mlxsw_sx, u8 local_port)
11198c2ecf20Sopenharmony_ci{
11208c2ecf20Sopenharmony_ci	return mlxsw_sx->ports[local_port] != NULL;
11218c2ecf20Sopenharmony_ci}
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_cistatic int __mlxsw_sx_port_ib_create(struct mlxsw_sx *mlxsw_sx, u8 local_port,
11248c2ecf20Sopenharmony_ci				     u8 module, u8 width)
11258c2ecf20Sopenharmony_ci{
11268c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port;
11278c2ecf20Sopenharmony_ci	int err;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	mlxsw_sx_port = kzalloc(sizeof(*mlxsw_sx_port), GFP_KERNEL);
11308c2ecf20Sopenharmony_ci	if (!mlxsw_sx_port)
11318c2ecf20Sopenharmony_ci		return -ENOMEM;
11328c2ecf20Sopenharmony_ci	mlxsw_sx_port->mlxsw_sx = mlxsw_sx;
11338c2ecf20Sopenharmony_ci	mlxsw_sx_port->local_port = local_port;
11348c2ecf20Sopenharmony_ci	mlxsw_sx_port->mapping.module = module;
11358c2ecf20Sopenharmony_ci
11368c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_system_port_mapping_set(mlxsw_sx_port);
11378c2ecf20Sopenharmony_ci	if (err) {
11388c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set system port mapping\n",
11398c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
11408c2ecf20Sopenharmony_ci		goto err_port_system_port_mapping_set;
11418c2ecf20Sopenharmony_ci	}
11428c2ecf20Sopenharmony_ci
11438c2ecf20Sopenharmony_ci	/* Adding port to Infiniband swid (1) */
11448c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_swid_set(mlxsw_sx_port, 1);
11458c2ecf20Sopenharmony_ci	if (err) {
11468c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set SWID\n",
11478c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
11488c2ecf20Sopenharmony_ci		goto err_port_swid_set;
11498c2ecf20Sopenharmony_ci	}
11508c2ecf20Sopenharmony_ci
11518c2ecf20Sopenharmony_ci	/* Expose the IB port number as it's front panel name */
11528c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_ib_port_set(mlxsw_sx_port, module + 1);
11538c2ecf20Sopenharmony_ci	if (err) {
11548c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set IB port\n",
11558c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
11568c2ecf20Sopenharmony_ci		goto err_port_ib_set;
11578c2ecf20Sopenharmony_ci	}
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_ci	/* Supports all speeds from SDR to FDR (bitmask) and support bus width
11608c2ecf20Sopenharmony_ci	 * of 1x, 2x and 4x (3 bits bitmask)
11618c2ecf20Sopenharmony_ci	 */
11628c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_ib_speed_set(mlxsw_sx_port,
11638c2ecf20Sopenharmony_ci					 MLXSW_REG_PTYS_IB_SPEED_EDR - 1,
11648c2ecf20Sopenharmony_ci					 BIT(3) - 1);
11658c2ecf20Sopenharmony_ci	if (err) {
11668c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set speed\n",
11678c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
11688c2ecf20Sopenharmony_ci		goto err_port_speed_set;
11698c2ecf20Sopenharmony_ci	}
11708c2ecf20Sopenharmony_ci
11718c2ecf20Sopenharmony_ci	/* Change to the maximum MTU the device supports, the SMA will take
11728c2ecf20Sopenharmony_ci	 * care of the active MTU
11738c2ecf20Sopenharmony_ci	 */
11748c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_mtu_ib_set(mlxsw_sx_port, MLXSW_IB_DEFAULT_MTU);
11758c2ecf20Sopenharmony_ci	if (err) {
11768c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MTU\n",
11778c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
11788c2ecf20Sopenharmony_ci		goto err_port_mtu_set;
11798c2ecf20Sopenharmony_ci	}
11808c2ecf20Sopenharmony_ci
11818c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
11828c2ecf20Sopenharmony_ci	if (err) {
11838c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to change admin state to UP\n",
11848c2ecf20Sopenharmony_ci			mlxsw_sx_port->local_port);
11858c2ecf20Sopenharmony_ci		goto err_port_admin_set;
11868c2ecf20Sopenharmony_ci	}
11878c2ecf20Sopenharmony_ci
11888c2ecf20Sopenharmony_ci	mlxsw_core_port_ib_set(mlxsw_sx->core, mlxsw_sx_port->local_port,
11898c2ecf20Sopenharmony_ci			       mlxsw_sx_port);
11908c2ecf20Sopenharmony_ci	mlxsw_sx->ports[local_port] = mlxsw_sx_port;
11918c2ecf20Sopenharmony_ci	return 0;
11928c2ecf20Sopenharmony_ci
11938c2ecf20Sopenharmony_cierr_port_admin_set:
11948c2ecf20Sopenharmony_cierr_port_mtu_set:
11958c2ecf20Sopenharmony_cierr_port_speed_set:
11968c2ecf20Sopenharmony_cierr_port_ib_set:
11978c2ecf20Sopenharmony_ci	mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
11988c2ecf20Sopenharmony_cierr_port_swid_set:
11998c2ecf20Sopenharmony_cierr_port_system_port_mapping_set:
12008c2ecf20Sopenharmony_ci	kfree(mlxsw_sx_port);
12018c2ecf20Sopenharmony_ci	return err;
12028c2ecf20Sopenharmony_ci}
12038c2ecf20Sopenharmony_ci
12048c2ecf20Sopenharmony_cistatic void __mlxsw_sx_port_ib_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
12058c2ecf20Sopenharmony_ci{
12068c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
12078c2ecf20Sopenharmony_ci
12088c2ecf20Sopenharmony_ci	mlxsw_core_port_clear(mlxsw_sx->core, local_port, mlxsw_sx);
12098c2ecf20Sopenharmony_ci	mlxsw_sx->ports[local_port] = NULL;
12108c2ecf20Sopenharmony_ci	mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
12118c2ecf20Sopenharmony_ci	mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
12128c2ecf20Sopenharmony_ci	kfree(mlxsw_sx_port);
12138c2ecf20Sopenharmony_ci}
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_cistatic void __mlxsw_sx_port_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
12168c2ecf20Sopenharmony_ci{
12178c2ecf20Sopenharmony_ci	enum devlink_port_type port_type =
12188c2ecf20Sopenharmony_ci		mlxsw_core_port_type_get(mlxsw_sx->core, local_port);
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_ci	if (port_type == DEVLINK_PORT_TYPE_ETH)
12218c2ecf20Sopenharmony_ci		__mlxsw_sx_port_eth_remove(mlxsw_sx, local_port);
12228c2ecf20Sopenharmony_ci	else if (port_type == DEVLINK_PORT_TYPE_IB)
12238c2ecf20Sopenharmony_ci		__mlxsw_sx_port_ib_remove(mlxsw_sx, local_port);
12248c2ecf20Sopenharmony_ci}
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_cistatic void mlxsw_sx_port_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
12278c2ecf20Sopenharmony_ci{
12288c2ecf20Sopenharmony_ci	__mlxsw_sx_port_remove(mlxsw_sx, local_port);
12298c2ecf20Sopenharmony_ci	mlxsw_core_port_fini(mlxsw_sx->core, local_port);
12308c2ecf20Sopenharmony_ci}
12318c2ecf20Sopenharmony_ci
12328c2ecf20Sopenharmony_cistatic void mlxsw_sx_ports_remove(struct mlxsw_sx *mlxsw_sx)
12338c2ecf20Sopenharmony_ci{
12348c2ecf20Sopenharmony_ci	int i;
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	for (i = 1; i < mlxsw_core_max_ports(mlxsw_sx->core); i++)
12378c2ecf20Sopenharmony_ci		if (mlxsw_sx_port_created(mlxsw_sx, i))
12388c2ecf20Sopenharmony_ci			mlxsw_sx_port_remove(mlxsw_sx, i);
12398c2ecf20Sopenharmony_ci	kfree(mlxsw_sx->ports);
12408c2ecf20Sopenharmony_ci	mlxsw_sx->ports = NULL;
12418c2ecf20Sopenharmony_ci}
12428c2ecf20Sopenharmony_ci
12438c2ecf20Sopenharmony_cistatic int mlxsw_sx_ports_create(struct mlxsw_sx *mlxsw_sx)
12448c2ecf20Sopenharmony_ci{
12458c2ecf20Sopenharmony_ci	unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sx->core);
12468c2ecf20Sopenharmony_ci	size_t alloc_size;
12478c2ecf20Sopenharmony_ci	u8 module, width;
12488c2ecf20Sopenharmony_ci	int i;
12498c2ecf20Sopenharmony_ci	int err;
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	alloc_size = sizeof(struct mlxsw_sx_port *) * max_ports;
12528c2ecf20Sopenharmony_ci	mlxsw_sx->ports = kzalloc(alloc_size, GFP_KERNEL);
12538c2ecf20Sopenharmony_ci	if (!mlxsw_sx->ports)
12548c2ecf20Sopenharmony_ci		return -ENOMEM;
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	for (i = 1; i < max_ports; i++) {
12578c2ecf20Sopenharmony_ci		err = mlxsw_sx_port_module_info_get(mlxsw_sx, i, &module,
12588c2ecf20Sopenharmony_ci						    &width);
12598c2ecf20Sopenharmony_ci		if (err)
12608c2ecf20Sopenharmony_ci			goto err_port_module_info_get;
12618c2ecf20Sopenharmony_ci		if (!width)
12628c2ecf20Sopenharmony_ci			continue;
12638c2ecf20Sopenharmony_ci		err = mlxsw_sx_port_eth_create(mlxsw_sx, i, module, width);
12648c2ecf20Sopenharmony_ci		if (err)
12658c2ecf20Sopenharmony_ci			goto err_port_create;
12668c2ecf20Sopenharmony_ci	}
12678c2ecf20Sopenharmony_ci	return 0;
12688c2ecf20Sopenharmony_ci
12698c2ecf20Sopenharmony_cierr_port_create:
12708c2ecf20Sopenharmony_cierr_port_module_info_get:
12718c2ecf20Sopenharmony_ci	for (i--; i >= 1; i--)
12728c2ecf20Sopenharmony_ci		if (mlxsw_sx_port_created(mlxsw_sx, i))
12738c2ecf20Sopenharmony_ci			mlxsw_sx_port_remove(mlxsw_sx, i);
12748c2ecf20Sopenharmony_ci	kfree(mlxsw_sx->ports);
12758c2ecf20Sopenharmony_ci	mlxsw_sx->ports = NULL;
12768c2ecf20Sopenharmony_ci	return err;
12778c2ecf20Sopenharmony_ci}
12788c2ecf20Sopenharmony_ci
12798c2ecf20Sopenharmony_cistatic void mlxsw_sx_pude_eth_event_func(struct mlxsw_sx_port *mlxsw_sx_port,
12808c2ecf20Sopenharmony_ci					 enum mlxsw_reg_pude_oper_status status)
12818c2ecf20Sopenharmony_ci{
12828c2ecf20Sopenharmony_ci	if (status == MLXSW_PORT_OPER_STATUS_UP) {
12838c2ecf20Sopenharmony_ci		netdev_info(mlxsw_sx_port->dev, "link up\n");
12848c2ecf20Sopenharmony_ci		netif_carrier_on(mlxsw_sx_port->dev);
12858c2ecf20Sopenharmony_ci	} else {
12868c2ecf20Sopenharmony_ci		netdev_info(mlxsw_sx_port->dev, "link down\n");
12878c2ecf20Sopenharmony_ci		netif_carrier_off(mlxsw_sx_port->dev);
12888c2ecf20Sopenharmony_ci	}
12898c2ecf20Sopenharmony_ci}
12908c2ecf20Sopenharmony_ci
12918c2ecf20Sopenharmony_cistatic void mlxsw_sx_pude_ib_event_func(struct mlxsw_sx_port *mlxsw_sx_port,
12928c2ecf20Sopenharmony_ci					enum mlxsw_reg_pude_oper_status status)
12938c2ecf20Sopenharmony_ci{
12948c2ecf20Sopenharmony_ci	if (status == MLXSW_PORT_OPER_STATUS_UP)
12958c2ecf20Sopenharmony_ci		pr_info("ib link for port %d - up\n",
12968c2ecf20Sopenharmony_ci			mlxsw_sx_port->mapping.module + 1);
12978c2ecf20Sopenharmony_ci	else
12988c2ecf20Sopenharmony_ci		pr_info("ib link for port %d - down\n",
12998c2ecf20Sopenharmony_ci			mlxsw_sx_port->mapping.module + 1);
13008c2ecf20Sopenharmony_ci}
13018c2ecf20Sopenharmony_ci
13028c2ecf20Sopenharmony_cistatic void mlxsw_sx_pude_event_func(const struct mlxsw_reg_info *reg,
13038c2ecf20Sopenharmony_ci				     char *pude_pl, void *priv)
13048c2ecf20Sopenharmony_ci{
13058c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = priv;
13068c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port;
13078c2ecf20Sopenharmony_ci	enum mlxsw_reg_pude_oper_status status;
13088c2ecf20Sopenharmony_ci	enum devlink_port_type port_type;
13098c2ecf20Sopenharmony_ci	u8 local_port;
13108c2ecf20Sopenharmony_ci
13118c2ecf20Sopenharmony_ci	local_port = mlxsw_reg_pude_local_port_get(pude_pl);
13128c2ecf20Sopenharmony_ci	mlxsw_sx_port = mlxsw_sx->ports[local_port];
13138c2ecf20Sopenharmony_ci	if (!mlxsw_sx_port) {
13148c2ecf20Sopenharmony_ci		dev_warn(mlxsw_sx->bus_info->dev, "Port %d: Link event received for non-existent port\n",
13158c2ecf20Sopenharmony_ci			 local_port);
13168c2ecf20Sopenharmony_ci		return;
13178c2ecf20Sopenharmony_ci	}
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	status = mlxsw_reg_pude_oper_status_get(pude_pl);
13208c2ecf20Sopenharmony_ci	port_type = mlxsw_core_port_type_get(mlxsw_sx->core, local_port);
13218c2ecf20Sopenharmony_ci	if (port_type == DEVLINK_PORT_TYPE_ETH)
13228c2ecf20Sopenharmony_ci		mlxsw_sx_pude_eth_event_func(mlxsw_sx_port, status);
13238c2ecf20Sopenharmony_ci	else if (port_type == DEVLINK_PORT_TYPE_IB)
13248c2ecf20Sopenharmony_ci		mlxsw_sx_pude_ib_event_func(mlxsw_sx_port, status);
13258c2ecf20Sopenharmony_ci}
13268c2ecf20Sopenharmony_ci
13278c2ecf20Sopenharmony_cistatic void mlxsw_sx_rx_listener_func(struct sk_buff *skb, u8 local_port,
13288c2ecf20Sopenharmony_ci				      void *priv)
13298c2ecf20Sopenharmony_ci{
13308c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = priv;
13318c2ecf20Sopenharmony_ci	struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
13328c2ecf20Sopenharmony_ci	struct mlxsw_sx_port_pcpu_stats *pcpu_stats;
13338c2ecf20Sopenharmony_ci
13348c2ecf20Sopenharmony_ci	if (unlikely(!mlxsw_sx_port)) {
13358c2ecf20Sopenharmony_ci		dev_warn_ratelimited(mlxsw_sx->bus_info->dev, "Port %d: skb received for non-existent port\n",
13368c2ecf20Sopenharmony_ci				     local_port);
13378c2ecf20Sopenharmony_ci		return;
13388c2ecf20Sopenharmony_ci	}
13398c2ecf20Sopenharmony_ci
13408c2ecf20Sopenharmony_ci	skb->dev = mlxsw_sx_port->dev;
13418c2ecf20Sopenharmony_ci
13428c2ecf20Sopenharmony_ci	pcpu_stats = this_cpu_ptr(mlxsw_sx_port->pcpu_stats);
13438c2ecf20Sopenharmony_ci	u64_stats_update_begin(&pcpu_stats->syncp);
13448c2ecf20Sopenharmony_ci	pcpu_stats->rx_packets++;
13458c2ecf20Sopenharmony_ci	pcpu_stats->rx_bytes += skb->len;
13468c2ecf20Sopenharmony_ci	u64_stats_update_end(&pcpu_stats->syncp);
13478c2ecf20Sopenharmony_ci
13488c2ecf20Sopenharmony_ci	skb->protocol = eth_type_trans(skb, skb->dev);
13498c2ecf20Sopenharmony_ci	netif_receive_skb(skb);
13508c2ecf20Sopenharmony_ci}
13518c2ecf20Sopenharmony_ci
13528c2ecf20Sopenharmony_cistatic int mlxsw_sx_port_type_set(struct mlxsw_core *mlxsw_core, u8 local_port,
13538c2ecf20Sopenharmony_ci				  enum devlink_port_type new_type)
13548c2ecf20Sopenharmony_ci{
13558c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_core_driver_priv(mlxsw_core);
13568c2ecf20Sopenharmony_ci	u8 module, width;
13578c2ecf20Sopenharmony_ci	int err;
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	if (!mlxsw_sx->ports || !mlxsw_sx->ports[local_port]) {
13608c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Port number \"%d\" does not exist\n",
13618c2ecf20Sopenharmony_ci			local_port);
13628c2ecf20Sopenharmony_ci		return -EINVAL;
13638c2ecf20Sopenharmony_ci	}
13648c2ecf20Sopenharmony_ci
13658c2ecf20Sopenharmony_ci	if (new_type == DEVLINK_PORT_TYPE_AUTO)
13668c2ecf20Sopenharmony_ci		return -EOPNOTSUPP;
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	__mlxsw_sx_port_remove(mlxsw_sx, local_port);
13698c2ecf20Sopenharmony_ci	err = mlxsw_sx_port_module_info_get(mlxsw_sx, local_port, &module,
13708c2ecf20Sopenharmony_ci					    &width);
13718c2ecf20Sopenharmony_ci	if (err)
13728c2ecf20Sopenharmony_ci		goto err_port_module_info_get;
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_ci	if (new_type == DEVLINK_PORT_TYPE_ETH)
13758c2ecf20Sopenharmony_ci		err = __mlxsw_sx_port_eth_create(mlxsw_sx, local_port, module,
13768c2ecf20Sopenharmony_ci						 width);
13778c2ecf20Sopenharmony_ci	else if (new_type == DEVLINK_PORT_TYPE_IB)
13788c2ecf20Sopenharmony_ci		err = __mlxsw_sx_port_ib_create(mlxsw_sx, local_port, module,
13798c2ecf20Sopenharmony_ci						width);
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_cierr_port_module_info_get:
13828c2ecf20Sopenharmony_ci	return err;
13838c2ecf20Sopenharmony_ci}
13848c2ecf20Sopenharmony_ci
13858c2ecf20Sopenharmony_cienum {
13868c2ecf20Sopenharmony_ci	MLXSW_REG_HTGT_TRAP_GROUP_SX2_RX = 1,
13878c2ecf20Sopenharmony_ci	MLXSW_REG_HTGT_TRAP_GROUP_SX2_CTRL = 2,
13888c2ecf20Sopenharmony_ci};
13898c2ecf20Sopenharmony_ci
13908c2ecf20Sopenharmony_ci#define MLXSW_SX_RXL(_trap_id) \
13918c2ecf20Sopenharmony_ci	MLXSW_RXL(mlxsw_sx_rx_listener_func, _trap_id, TRAP_TO_CPU,	\
13928c2ecf20Sopenharmony_ci		  false, SX2_RX, FORWARD)
13938c2ecf20Sopenharmony_ci
13948c2ecf20Sopenharmony_cistatic const struct mlxsw_listener mlxsw_sx_listener[] = {
13958c2ecf20Sopenharmony_ci	MLXSW_EVENTL(mlxsw_sx_pude_event_func, PUDE, EMAD),
13968c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(FDB_MC),
13978c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(STP),
13988c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(LACP),
13998c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(EAPOL),
14008c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(LLDP),
14018c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(MMRP),
14028c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(MVRP),
14038c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(RPVST),
14048c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(DHCP),
14058c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(IGMP_QUERY),
14068c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(IGMP_V1_REPORT),
14078c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(IGMP_V2_REPORT),
14088c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(IGMP_V2_LEAVE),
14098c2ecf20Sopenharmony_ci	MLXSW_SX_RXL(IGMP_V3_REPORT),
14108c2ecf20Sopenharmony_ci};
14118c2ecf20Sopenharmony_ci
14128c2ecf20Sopenharmony_cistatic int mlxsw_sx_traps_init(struct mlxsw_sx *mlxsw_sx)
14138c2ecf20Sopenharmony_ci{
14148c2ecf20Sopenharmony_ci	char htgt_pl[MLXSW_REG_HTGT_LEN];
14158c2ecf20Sopenharmony_ci	int i;
14168c2ecf20Sopenharmony_ci	int err;
14178c2ecf20Sopenharmony_ci
14188c2ecf20Sopenharmony_ci	mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_SX2_RX,
14198c2ecf20Sopenharmony_ci			    MLXSW_REG_HTGT_INVALID_POLICER,
14208c2ecf20Sopenharmony_ci			    MLXSW_REG_HTGT_DEFAULT_PRIORITY,
14218c2ecf20Sopenharmony_ci			    MLXSW_REG_HTGT_DEFAULT_TC);
14228c2ecf20Sopenharmony_ci	mlxsw_reg_htgt_local_path_rdq_set(htgt_pl,
14238c2ecf20Sopenharmony_ci					  MLXSW_REG_HTGT_LOCAL_PATH_RDQ_SX2_RX);
14248c2ecf20Sopenharmony_ci
14258c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(htgt), htgt_pl);
14268c2ecf20Sopenharmony_ci	if (err)
14278c2ecf20Sopenharmony_ci		return err;
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_SX2_CTRL,
14308c2ecf20Sopenharmony_ci			    MLXSW_REG_HTGT_INVALID_POLICER,
14318c2ecf20Sopenharmony_ci			    MLXSW_REG_HTGT_DEFAULT_PRIORITY,
14328c2ecf20Sopenharmony_ci			    MLXSW_REG_HTGT_DEFAULT_TC);
14338c2ecf20Sopenharmony_ci	mlxsw_reg_htgt_local_path_rdq_set(htgt_pl,
14348c2ecf20Sopenharmony_ci					MLXSW_REG_HTGT_LOCAL_PATH_RDQ_SX2_CTRL);
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(htgt), htgt_pl);
14378c2ecf20Sopenharmony_ci	if (err)
14388c2ecf20Sopenharmony_ci		return err;
14398c2ecf20Sopenharmony_ci
14408c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(mlxsw_sx_listener); i++) {
14418c2ecf20Sopenharmony_ci		err = mlxsw_core_trap_register(mlxsw_sx->core,
14428c2ecf20Sopenharmony_ci					       &mlxsw_sx_listener[i],
14438c2ecf20Sopenharmony_ci					       mlxsw_sx);
14448c2ecf20Sopenharmony_ci		if (err)
14458c2ecf20Sopenharmony_ci			goto err_listener_register;
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_ci	}
14488c2ecf20Sopenharmony_ci	return 0;
14498c2ecf20Sopenharmony_ci
14508c2ecf20Sopenharmony_cierr_listener_register:
14518c2ecf20Sopenharmony_ci	for (i--; i >= 0; i--) {
14528c2ecf20Sopenharmony_ci		mlxsw_core_trap_unregister(mlxsw_sx->core,
14538c2ecf20Sopenharmony_ci					   &mlxsw_sx_listener[i],
14548c2ecf20Sopenharmony_ci					   mlxsw_sx);
14558c2ecf20Sopenharmony_ci	}
14568c2ecf20Sopenharmony_ci	return err;
14578c2ecf20Sopenharmony_ci}
14588c2ecf20Sopenharmony_ci
14598c2ecf20Sopenharmony_cistatic void mlxsw_sx_traps_fini(struct mlxsw_sx *mlxsw_sx)
14608c2ecf20Sopenharmony_ci{
14618c2ecf20Sopenharmony_ci	int i;
14628c2ecf20Sopenharmony_ci
14638c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(mlxsw_sx_listener); i++) {
14648c2ecf20Sopenharmony_ci		mlxsw_core_trap_unregister(mlxsw_sx->core,
14658c2ecf20Sopenharmony_ci					   &mlxsw_sx_listener[i],
14668c2ecf20Sopenharmony_ci					   mlxsw_sx);
14678c2ecf20Sopenharmony_ci	}
14688c2ecf20Sopenharmony_ci}
14698c2ecf20Sopenharmony_ci
14708c2ecf20Sopenharmony_cistatic int mlxsw_sx_flood_init(struct mlxsw_sx *mlxsw_sx)
14718c2ecf20Sopenharmony_ci{
14728c2ecf20Sopenharmony_ci	char sfgc_pl[MLXSW_REG_SFGC_LEN];
14738c2ecf20Sopenharmony_ci	char sgcr_pl[MLXSW_REG_SGCR_LEN];
14748c2ecf20Sopenharmony_ci	char *sftr_pl;
14758c2ecf20Sopenharmony_ci	int err;
14768c2ecf20Sopenharmony_ci
14778c2ecf20Sopenharmony_ci	/* Configure a flooding table, which includes only CPU port. */
14788c2ecf20Sopenharmony_ci	sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
14798c2ecf20Sopenharmony_ci	if (!sftr_pl)
14808c2ecf20Sopenharmony_ci		return -ENOMEM;
14818c2ecf20Sopenharmony_ci	mlxsw_reg_sftr_pack(sftr_pl, 0, 0, MLXSW_REG_SFGC_TABLE_TYPE_SINGLE, 0,
14828c2ecf20Sopenharmony_ci			    MLXSW_PORT_CPU_PORT, true);
14838c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sftr), sftr_pl);
14848c2ecf20Sopenharmony_ci	kfree(sftr_pl);
14858c2ecf20Sopenharmony_ci	if (err)
14868c2ecf20Sopenharmony_ci		return err;
14878c2ecf20Sopenharmony_ci
14888c2ecf20Sopenharmony_ci	/* Flood different packet types using the flooding table. */
14898c2ecf20Sopenharmony_ci	mlxsw_reg_sfgc_pack(sfgc_pl,
14908c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST,
14918c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
14928c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
14938c2ecf20Sopenharmony_ci			    0);
14948c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
14958c2ecf20Sopenharmony_ci	if (err)
14968c2ecf20Sopenharmony_ci		return err;
14978c2ecf20Sopenharmony_ci
14988c2ecf20Sopenharmony_ci	mlxsw_reg_sfgc_pack(sfgc_pl,
14998c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TYPE_BROADCAST,
15008c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
15018c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
15028c2ecf20Sopenharmony_ci			    0);
15038c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
15048c2ecf20Sopenharmony_ci	if (err)
15058c2ecf20Sopenharmony_ci		return err;
15068c2ecf20Sopenharmony_ci
15078c2ecf20Sopenharmony_ci	mlxsw_reg_sfgc_pack(sfgc_pl,
15088c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_NON_IP,
15098c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
15108c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
15118c2ecf20Sopenharmony_ci			    0);
15128c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
15138c2ecf20Sopenharmony_ci	if (err)
15148c2ecf20Sopenharmony_ci		return err;
15158c2ecf20Sopenharmony_ci
15168c2ecf20Sopenharmony_ci	mlxsw_reg_sfgc_pack(sfgc_pl,
15178c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_IPV6,
15188c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
15198c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
15208c2ecf20Sopenharmony_ci			    0);
15218c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
15228c2ecf20Sopenharmony_ci	if (err)
15238c2ecf20Sopenharmony_ci		return err;
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci	mlxsw_reg_sfgc_pack(sfgc_pl,
15268c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_IPV4,
15278c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
15288c2ecf20Sopenharmony_ci			    MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
15298c2ecf20Sopenharmony_ci			    0);
15308c2ecf20Sopenharmony_ci	err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
15318c2ecf20Sopenharmony_ci	if (err)
15328c2ecf20Sopenharmony_ci		return err;
15338c2ecf20Sopenharmony_ci
15348c2ecf20Sopenharmony_ci	mlxsw_reg_sgcr_pack(sgcr_pl, true);
15358c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sgcr), sgcr_pl);
15368c2ecf20Sopenharmony_ci}
15378c2ecf20Sopenharmony_ci
15388c2ecf20Sopenharmony_cistatic int mlxsw_sx_basic_trap_groups_set(struct mlxsw_core *mlxsw_core)
15398c2ecf20Sopenharmony_ci{
15408c2ecf20Sopenharmony_ci	char htgt_pl[MLXSW_REG_HTGT_LEN];
15418c2ecf20Sopenharmony_ci
15428c2ecf20Sopenharmony_ci	mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_EMAD,
15438c2ecf20Sopenharmony_ci			    MLXSW_REG_HTGT_INVALID_POLICER,
15448c2ecf20Sopenharmony_ci			    MLXSW_REG_HTGT_DEFAULT_PRIORITY,
15458c2ecf20Sopenharmony_ci			    MLXSW_REG_HTGT_DEFAULT_TC);
15468c2ecf20Sopenharmony_ci	mlxsw_reg_htgt_swid_set(htgt_pl, MLXSW_PORT_SWID_ALL_SWIDS);
15478c2ecf20Sopenharmony_ci	mlxsw_reg_htgt_local_path_rdq_set(htgt_pl,
15488c2ecf20Sopenharmony_ci					MLXSW_REG_HTGT_LOCAL_PATH_RDQ_SX2_EMAD);
15498c2ecf20Sopenharmony_ci	return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
15508c2ecf20Sopenharmony_ci}
15518c2ecf20Sopenharmony_ci
15528c2ecf20Sopenharmony_cistatic int mlxsw_sx_init(struct mlxsw_core *mlxsw_core,
15538c2ecf20Sopenharmony_ci			 const struct mlxsw_bus_info *mlxsw_bus_info,
15548c2ecf20Sopenharmony_ci			 struct netlink_ext_ack *extack)
15558c2ecf20Sopenharmony_ci{
15568c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_core_driver_priv(mlxsw_core);
15578c2ecf20Sopenharmony_ci	int err;
15588c2ecf20Sopenharmony_ci
15598c2ecf20Sopenharmony_ci	mlxsw_sx->core = mlxsw_core;
15608c2ecf20Sopenharmony_ci	mlxsw_sx->bus_info = mlxsw_bus_info;
15618c2ecf20Sopenharmony_ci
15628c2ecf20Sopenharmony_ci	err = mlxsw_sx_hw_id_get(mlxsw_sx);
15638c2ecf20Sopenharmony_ci	if (err) {
15648c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Failed to get switch HW ID\n");
15658c2ecf20Sopenharmony_ci		return err;
15668c2ecf20Sopenharmony_ci	}
15678c2ecf20Sopenharmony_ci
15688c2ecf20Sopenharmony_ci	err = mlxsw_sx_ports_create(mlxsw_sx);
15698c2ecf20Sopenharmony_ci	if (err) {
15708c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Failed to create ports\n");
15718c2ecf20Sopenharmony_ci		return err;
15728c2ecf20Sopenharmony_ci	}
15738c2ecf20Sopenharmony_ci
15748c2ecf20Sopenharmony_ci	err = mlxsw_sx_traps_init(mlxsw_sx);
15758c2ecf20Sopenharmony_ci	if (err) {
15768c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Failed to set traps\n");
15778c2ecf20Sopenharmony_ci		goto err_listener_register;
15788c2ecf20Sopenharmony_ci	}
15798c2ecf20Sopenharmony_ci
15808c2ecf20Sopenharmony_ci	err = mlxsw_sx_flood_init(mlxsw_sx);
15818c2ecf20Sopenharmony_ci	if (err) {
15828c2ecf20Sopenharmony_ci		dev_err(mlxsw_sx->bus_info->dev, "Failed to initialize flood tables\n");
15838c2ecf20Sopenharmony_ci		goto err_flood_init;
15848c2ecf20Sopenharmony_ci	}
15858c2ecf20Sopenharmony_ci
15868c2ecf20Sopenharmony_ci	return 0;
15878c2ecf20Sopenharmony_ci
15888c2ecf20Sopenharmony_cierr_flood_init:
15898c2ecf20Sopenharmony_ci	mlxsw_sx_traps_fini(mlxsw_sx);
15908c2ecf20Sopenharmony_cierr_listener_register:
15918c2ecf20Sopenharmony_ci	mlxsw_sx_ports_remove(mlxsw_sx);
15928c2ecf20Sopenharmony_ci	return err;
15938c2ecf20Sopenharmony_ci}
15948c2ecf20Sopenharmony_ci
15958c2ecf20Sopenharmony_cistatic void mlxsw_sx_fini(struct mlxsw_core *mlxsw_core)
15968c2ecf20Sopenharmony_ci{
15978c2ecf20Sopenharmony_ci	struct mlxsw_sx *mlxsw_sx = mlxsw_core_driver_priv(mlxsw_core);
15988c2ecf20Sopenharmony_ci
15998c2ecf20Sopenharmony_ci	mlxsw_sx_traps_fini(mlxsw_sx);
16008c2ecf20Sopenharmony_ci	mlxsw_sx_ports_remove(mlxsw_sx);
16018c2ecf20Sopenharmony_ci}
16028c2ecf20Sopenharmony_ci
16038c2ecf20Sopenharmony_cistatic const struct mlxsw_config_profile mlxsw_sx_config_profile = {
16048c2ecf20Sopenharmony_ci	.used_max_vepa_channels		= 1,
16058c2ecf20Sopenharmony_ci	.max_vepa_channels		= 0,
16068c2ecf20Sopenharmony_ci	.used_max_mid			= 1,
16078c2ecf20Sopenharmony_ci	.max_mid			= 7000,
16088c2ecf20Sopenharmony_ci	.used_max_pgt			= 1,
16098c2ecf20Sopenharmony_ci	.max_pgt			= 0,
16108c2ecf20Sopenharmony_ci	.used_max_system_port		= 1,
16118c2ecf20Sopenharmony_ci	.max_system_port		= 48000,
16128c2ecf20Sopenharmony_ci	.used_max_vlan_groups		= 1,
16138c2ecf20Sopenharmony_ci	.max_vlan_groups		= 127,
16148c2ecf20Sopenharmony_ci	.used_max_regions		= 1,
16158c2ecf20Sopenharmony_ci	.max_regions			= 400,
16168c2ecf20Sopenharmony_ci	.used_flood_tables		= 1,
16178c2ecf20Sopenharmony_ci	.max_flood_tables		= 2,
16188c2ecf20Sopenharmony_ci	.max_vid_flood_tables		= 1,
16198c2ecf20Sopenharmony_ci	.used_flood_mode		= 1,
16208c2ecf20Sopenharmony_ci	.flood_mode			= 3,
16218c2ecf20Sopenharmony_ci	.used_max_ib_mc			= 1,
16228c2ecf20Sopenharmony_ci	.max_ib_mc			= 6,
16238c2ecf20Sopenharmony_ci	.used_max_pkey			= 1,
16248c2ecf20Sopenharmony_ci	.max_pkey			= 0,
16258c2ecf20Sopenharmony_ci	.swid_config			= {
16268c2ecf20Sopenharmony_ci		{
16278c2ecf20Sopenharmony_ci			.used_type	= 1,
16288c2ecf20Sopenharmony_ci			.type		= MLXSW_PORT_SWID_TYPE_ETH,
16298c2ecf20Sopenharmony_ci		},
16308c2ecf20Sopenharmony_ci		{
16318c2ecf20Sopenharmony_ci			.used_type	= 1,
16328c2ecf20Sopenharmony_ci			.type		= MLXSW_PORT_SWID_TYPE_IB,
16338c2ecf20Sopenharmony_ci		}
16348c2ecf20Sopenharmony_ci	},
16358c2ecf20Sopenharmony_ci};
16368c2ecf20Sopenharmony_ci
16378c2ecf20Sopenharmony_cistatic struct mlxsw_driver mlxsw_sx_driver = {
16388c2ecf20Sopenharmony_ci	.kind			= mlxsw_sx_driver_name,
16398c2ecf20Sopenharmony_ci	.priv_size		= sizeof(struct mlxsw_sx),
16408c2ecf20Sopenharmony_ci	.init			= mlxsw_sx_init,
16418c2ecf20Sopenharmony_ci	.fini			= mlxsw_sx_fini,
16428c2ecf20Sopenharmony_ci	.basic_trap_groups_set	= mlxsw_sx_basic_trap_groups_set,
16438c2ecf20Sopenharmony_ci	.txhdr_construct	= mlxsw_sx_txhdr_construct,
16448c2ecf20Sopenharmony_ci	.txhdr_len		= MLXSW_TXHDR_LEN,
16458c2ecf20Sopenharmony_ci	.profile		= &mlxsw_sx_config_profile,
16468c2ecf20Sopenharmony_ci	.port_type_set		= mlxsw_sx_port_type_set,
16478c2ecf20Sopenharmony_ci};
16488c2ecf20Sopenharmony_ci
16498c2ecf20Sopenharmony_cistatic const struct pci_device_id mlxsw_sx_pci_id_table[] = {
16508c2ecf20Sopenharmony_ci	{PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SWITCHX2), 0},
16518c2ecf20Sopenharmony_ci	{0, },
16528c2ecf20Sopenharmony_ci};
16538c2ecf20Sopenharmony_ci
16548c2ecf20Sopenharmony_cistatic struct pci_driver mlxsw_sx_pci_driver = {
16558c2ecf20Sopenharmony_ci	.name = mlxsw_sx_driver_name,
16568c2ecf20Sopenharmony_ci	.id_table = mlxsw_sx_pci_id_table,
16578c2ecf20Sopenharmony_ci};
16588c2ecf20Sopenharmony_ci
16598c2ecf20Sopenharmony_cistatic int __init mlxsw_sx_module_init(void)
16608c2ecf20Sopenharmony_ci{
16618c2ecf20Sopenharmony_ci	int err;
16628c2ecf20Sopenharmony_ci
16638c2ecf20Sopenharmony_ci	err = mlxsw_core_driver_register(&mlxsw_sx_driver);
16648c2ecf20Sopenharmony_ci	if (err)
16658c2ecf20Sopenharmony_ci		return err;
16668c2ecf20Sopenharmony_ci
16678c2ecf20Sopenharmony_ci	err = mlxsw_pci_driver_register(&mlxsw_sx_pci_driver);
16688c2ecf20Sopenharmony_ci	if (err)
16698c2ecf20Sopenharmony_ci		goto err_pci_driver_register;
16708c2ecf20Sopenharmony_ci
16718c2ecf20Sopenharmony_ci	return 0;
16728c2ecf20Sopenharmony_ci
16738c2ecf20Sopenharmony_cierr_pci_driver_register:
16748c2ecf20Sopenharmony_ci	mlxsw_core_driver_unregister(&mlxsw_sx_driver);
16758c2ecf20Sopenharmony_ci	return err;
16768c2ecf20Sopenharmony_ci}
16778c2ecf20Sopenharmony_ci
16788c2ecf20Sopenharmony_cistatic void __exit mlxsw_sx_module_exit(void)
16798c2ecf20Sopenharmony_ci{
16808c2ecf20Sopenharmony_ci	mlxsw_pci_driver_unregister(&mlxsw_sx_pci_driver);
16818c2ecf20Sopenharmony_ci	mlxsw_core_driver_unregister(&mlxsw_sx_driver);
16828c2ecf20Sopenharmony_ci}
16838c2ecf20Sopenharmony_ci
16848c2ecf20Sopenharmony_cimodule_init(mlxsw_sx_module_init);
16858c2ecf20Sopenharmony_cimodule_exit(mlxsw_sx_module_exit);
16868c2ecf20Sopenharmony_ci
16878c2ecf20Sopenharmony_ciMODULE_LICENSE("Dual BSD/GPL");
16888c2ecf20Sopenharmony_ciMODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
16898c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Mellanox SwitchX-2 driver");
16908c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, mlxsw_sx_pci_id_table);
1691