1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /* Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved */
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/types.h>
8 #include <linux/netdevice.h>
9 #include <linux/etherdevice.h>
10 #include <linux/slab.h>
11 #include <linux/device.h>
12 #include <linux/skbuff.h>
13 #include <linux/if_vlan.h>
23 static const char mlxsw_sx_driver_name[] = "mlxsw_switchx2";
24 static const char mlxsw_sx_driver_version[] = "1.0";
29 struct mlxsw_sx_port **ports;
30 struct mlxsw_core *core;
31 const struct mlxsw_bus_info *bus_info;
35 struct mlxsw_sx_port_pcpu_stats {
40 struct u64_stats_sync syncp;
44 struct mlxsw_sx_port {
45 struct net_device *dev;
46 struct mlxsw_sx_port_pcpu_stats __percpu *pcpu_stats;
47 struct mlxsw_sx *mlxsw_sx;
58 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
61 * Packet control type.
62 * 0 - Ethernet control (e.g. EMADs, LACP)
65 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
68 * Packet protocol type. Must be set to 1 (Ethernet).
70 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
73 * Egress TClass to be used on the egress device on the egress port.
74 * The MSB is specified in the 'ctclass3' field.
75 * Range is 0-15, where 15 is the highest priority.
77 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 18, 3);
80 * Switch partition ID.
82 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
85 * Destination local port for unicast packets.
86 * Destination multicast ID for multicast packets.
88 * Control packets are directed to a specific egress port, while data
89 * packets are transmitted through the CPU port (0) into the switch partition,
90 * where forwarding rules are applied.
92 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
95 * See field 'etclass'.
97 MLXSW_ITEM32(tx, hdr, ctclass3, 0x04, 14, 1);
100 * RDQ for control packets sent to remote CPU.
101 * Must be set to 0x1F for EMADs, otherwise 0.
103 MLXSW_ITEM32(tx, hdr, rdq, 0x04, 9, 5);
106 * Signature control for packets going to CPU. Must be set to 0.
108 MLXSW_ITEM32(tx, hdr, cpu_sig, 0x04, 0, 9);
111 * Stacking protocl signature. Must be set to 0xE0E0.
113 MLXSW_ITEM32(tx, hdr, sig, 0x0C, 16, 16);
118 MLXSW_ITEM32(tx, hdr, stclass, 0x0C, 13, 3);
121 * EMAD bit. Must be set for EMADs.
123 MLXSW_ITEM32(tx, hdr, emad, 0x0C, 5, 1);
127 * 6 - Control packets
129 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
131 static void mlxsw_sx_txhdr_construct(struct sk_buff *skb,
132 const struct mlxsw_tx_info *tx_info)
134 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
135 bool is_emad = tx_info->is_emad;
137 memset(txhdr, 0, MLXSW_TXHDR_LEN);
139 /* We currently set default values for the egress tclass (QoS). */
140 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_0);
141 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
142 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
143 mlxsw_tx_hdr_etclass_set(txhdr, is_emad ? MLXSW_TXHDR_ETCLASS_6 :
144 MLXSW_TXHDR_ETCLASS_5);
145 mlxsw_tx_hdr_swid_set(txhdr, 0);
146 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
147 mlxsw_tx_hdr_ctclass3_set(txhdr, MLXSW_TXHDR_CTCLASS3);
148 mlxsw_tx_hdr_rdq_set(txhdr, is_emad ? MLXSW_TXHDR_RDQ_EMAD :
149 MLXSW_TXHDR_RDQ_OTHER);
150 mlxsw_tx_hdr_cpu_sig_set(txhdr, MLXSW_TXHDR_CPU_SIG);
151 mlxsw_tx_hdr_sig_set(txhdr, MLXSW_TXHDR_SIG);
152 mlxsw_tx_hdr_stclass_set(txhdr, MLXSW_TXHDR_STCLASS_NONE);
153 mlxsw_tx_hdr_emad_set(txhdr, is_emad ? MLXSW_TXHDR_EMAD :
154 MLXSW_TXHDR_NOT_EMAD);
155 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
158 static int mlxsw_sx_port_admin_status_set(struct mlxsw_sx_port *mlxsw_sx_port,
161 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
162 char paos_pl[MLXSW_REG_PAOS_LEN];
164 mlxsw_reg_paos_pack(paos_pl, mlxsw_sx_port->local_port,
165 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
166 MLXSW_PORT_ADMIN_STATUS_DOWN);
167 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(paos), paos_pl);
170 static int mlxsw_sx_port_oper_status_get(struct mlxsw_sx_port *mlxsw_sx_port,
173 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
174 char paos_pl[MLXSW_REG_PAOS_LEN];
178 mlxsw_reg_paos_pack(paos_pl, mlxsw_sx_port->local_port, 0);
179 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(paos), paos_pl);
182 oper_status = mlxsw_reg_paos_oper_status_get(paos_pl);
183 *p_is_up = oper_status == MLXSW_PORT_ADMIN_STATUS_UP ? true : false;
187 static int __mlxsw_sx_port_mtu_set(struct mlxsw_sx_port *mlxsw_sx_port,
190 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
191 char pmtu_pl[MLXSW_REG_PMTU_LEN];
195 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sx_port->local_port, 0);
196 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(pmtu), pmtu_pl);
199 max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
204 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sx_port->local_port, mtu);
205 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(pmtu), pmtu_pl);
208 static int mlxsw_sx_port_mtu_eth_set(struct mlxsw_sx_port *mlxsw_sx_port,
211 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
212 return __mlxsw_sx_port_mtu_set(mlxsw_sx_port, mtu);
215 static int mlxsw_sx_port_mtu_ib_set(struct mlxsw_sx_port *mlxsw_sx_port,
218 return __mlxsw_sx_port_mtu_set(mlxsw_sx_port, mtu);
221 static int mlxsw_sx_port_ib_port_set(struct mlxsw_sx_port *mlxsw_sx_port,
224 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
225 char plib_pl[MLXSW_REG_PLIB_LEN] = {0};
228 mlxsw_reg_plib_local_port_set(plib_pl, mlxsw_sx_port->local_port);
229 mlxsw_reg_plib_ib_port_set(plib_pl, ib_port);
230 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(plib), plib_pl);
234 static int mlxsw_sx_port_swid_set(struct mlxsw_sx_port *mlxsw_sx_port, u8 swid)
236 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
237 char pspa_pl[MLXSW_REG_PSPA_LEN];
239 mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sx_port->local_port);
240 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(pspa), pspa_pl);
244 mlxsw_sx_port_system_port_mapping_set(struct mlxsw_sx_port *mlxsw_sx_port)
246 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
247 char sspr_pl[MLXSW_REG_SSPR_LEN];
249 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sx_port->local_port);
250 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sspr), sspr_pl);
253 static int mlxsw_sx_port_module_info_get(struct mlxsw_sx *mlxsw_sx,
254 u8 local_port, u8 *p_module,
257 char pmlp_pl[MLXSW_REG_PMLP_LEN];
260 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
261 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(pmlp), pmlp_pl);
264 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
265 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
269 static int mlxsw_sx_port_open(struct net_device *dev)
271 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
274 err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
277 netif_start_queue(dev);
281 static int mlxsw_sx_port_stop(struct net_device *dev)
283 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
285 netif_stop_queue(dev);
286 return mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
289 static netdev_tx_t mlxsw_sx_port_xmit(struct sk_buff *skb,
290 struct net_device *dev)
292 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
293 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
294 struct mlxsw_sx_port_pcpu_stats *pcpu_stats;
295 const struct mlxsw_tx_info tx_info = {
296 .local_port = mlxsw_sx_port->local_port,
302 memset(skb->cb, 0, sizeof(struct mlxsw_skb_cb));
304 if (mlxsw_core_skb_transmit_busy(mlxsw_sx->core, &tx_info))
305 return NETDEV_TX_BUSY;
307 if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) {
308 struct sk_buff *skb_orig = skb;
310 skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN);
312 this_cpu_inc(mlxsw_sx_port->pcpu_stats->tx_dropped);
313 dev_kfree_skb_any(skb_orig);
316 dev_consume_skb_any(skb_orig);
318 mlxsw_sx_txhdr_construct(skb, &tx_info);
319 /* TX header is consumed by HW on the way so we shouldn't count its
320 * bytes as being sent.
322 len = skb->len - MLXSW_TXHDR_LEN;
323 /* Due to a race we might fail here because of a full queue. In that
324 * unlikely case we simply drop the packet.
326 err = mlxsw_core_skb_transmit(mlxsw_sx->core, skb, &tx_info);
329 pcpu_stats = this_cpu_ptr(mlxsw_sx_port->pcpu_stats);
330 u64_stats_update_begin(&pcpu_stats->syncp);
331 pcpu_stats->tx_packets++;
332 pcpu_stats->tx_bytes += len;
333 u64_stats_update_end(&pcpu_stats->syncp);
335 this_cpu_inc(mlxsw_sx_port->pcpu_stats->tx_dropped);
336 dev_kfree_skb_any(skb);
341 static int mlxsw_sx_port_change_mtu(struct net_device *dev, int mtu)
343 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
346 err = mlxsw_sx_port_mtu_eth_set(mlxsw_sx_port, mtu);
354 mlxsw_sx_port_get_stats64(struct net_device *dev,
355 struct rtnl_link_stats64 *stats)
357 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
358 struct mlxsw_sx_port_pcpu_stats *p;
359 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
364 for_each_possible_cpu(i) {
365 p = per_cpu_ptr(mlxsw_sx_port->pcpu_stats, i);
367 start = u64_stats_fetch_begin_irq(&p->syncp);
368 rx_packets = p->rx_packets;
369 rx_bytes = p->rx_bytes;
370 tx_packets = p->tx_packets;
371 tx_bytes = p->tx_bytes;
372 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
374 stats->rx_packets += rx_packets;
375 stats->rx_bytes += rx_bytes;
376 stats->tx_packets += tx_packets;
377 stats->tx_bytes += tx_bytes;
378 /* tx_dropped is u32, updated without syncp protection. */
379 tx_dropped += p->tx_dropped;
381 stats->tx_dropped = tx_dropped;
384 static struct devlink_port *
385 mlxsw_sx_port_get_devlink_port(struct net_device *dev)
387 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
388 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
390 return mlxsw_core_port_devlink_port_get(mlxsw_sx->core,
391 mlxsw_sx_port->local_port);
394 static const struct net_device_ops mlxsw_sx_port_netdev_ops = {
395 .ndo_open = mlxsw_sx_port_open,
396 .ndo_stop = mlxsw_sx_port_stop,
397 .ndo_start_xmit = mlxsw_sx_port_xmit,
398 .ndo_change_mtu = mlxsw_sx_port_change_mtu,
399 .ndo_get_stats64 = mlxsw_sx_port_get_stats64,
400 .ndo_get_devlink_port = mlxsw_sx_port_get_devlink_port,
403 static void mlxsw_sx_port_get_drvinfo(struct net_device *dev,
404 struct ethtool_drvinfo *drvinfo)
406 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
407 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
409 strlcpy(drvinfo->driver, mlxsw_sx_driver_name, sizeof(drvinfo->driver));
410 strlcpy(drvinfo->version, mlxsw_sx_driver_version,
411 sizeof(drvinfo->version));
412 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
414 mlxsw_sx->bus_info->fw_rev.major,
415 mlxsw_sx->bus_info->fw_rev.minor,
416 mlxsw_sx->bus_info->fw_rev.subminor);
417 strlcpy(drvinfo->bus_info, mlxsw_sx->bus_info->device_name,
418 sizeof(drvinfo->bus_info));
421 struct mlxsw_sx_port_hw_stats {
422 char str[ETH_GSTRING_LEN];
423 u64 (*getter)(const char *payload);
426 static const struct mlxsw_sx_port_hw_stats mlxsw_sx_port_hw_stats[] = {
428 .str = "a_frames_transmitted_ok",
429 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
432 .str = "a_frames_received_ok",
433 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
436 .str = "a_frame_check_sequence_errors",
437 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
440 .str = "a_alignment_errors",
441 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
444 .str = "a_octets_transmitted_ok",
445 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
448 .str = "a_octets_received_ok",
449 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
452 .str = "a_multicast_frames_xmitted_ok",
453 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
456 .str = "a_broadcast_frames_xmitted_ok",
457 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
460 .str = "a_multicast_frames_received_ok",
461 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
464 .str = "a_broadcast_frames_received_ok",
465 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
468 .str = "a_in_range_length_errors",
469 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
472 .str = "a_out_of_range_length_field",
473 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
476 .str = "a_frame_too_long_errors",
477 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
480 .str = "a_symbol_error_during_carrier",
481 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
484 .str = "a_mac_control_frames_transmitted",
485 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
488 .str = "a_mac_control_frames_received",
489 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
492 .str = "a_unsupported_opcodes_received",
493 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
496 .str = "a_pause_mac_ctrl_frames_received",
497 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
500 .str = "a_pause_mac_ctrl_frames_xmitted",
501 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
505 #define MLXSW_SX_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sx_port_hw_stats)
507 static void mlxsw_sx_port_get_strings(struct net_device *dev,
508 u32 stringset, u8 *data)
515 for (i = 0; i < MLXSW_SX_PORT_HW_STATS_LEN; i++) {
516 memcpy(p, mlxsw_sx_port_hw_stats[i].str,
518 p += ETH_GSTRING_LEN;
524 static void mlxsw_sx_port_get_stats(struct net_device *dev,
525 struct ethtool_stats *stats, u64 *data)
527 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
528 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
529 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
533 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sx_port->local_port,
534 MLXSW_REG_PPCNT_IEEE_8023_CNT, 0);
535 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ppcnt), ppcnt_pl);
536 for (i = 0; i < MLXSW_SX_PORT_HW_STATS_LEN; i++)
537 data[i] = !err ? mlxsw_sx_port_hw_stats[i].getter(ppcnt_pl) : 0;
540 static int mlxsw_sx_port_get_sset_count(struct net_device *dev, int sset)
544 return MLXSW_SX_PORT_HW_STATS_LEN;
550 struct mlxsw_sx_port_link_mode {
557 static const struct mlxsw_sx_port_link_mode mlxsw_sx_port_link_mode[] = {
559 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
560 .supported = SUPPORTED_100baseT_Full,
561 .advertised = ADVERTISED_100baseT_Full,
565 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_TX,
569 .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
570 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
571 .supported = SUPPORTED_1000baseKX_Full,
572 .advertised = ADVERTISED_1000baseKX_Full,
576 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
577 .supported = SUPPORTED_10000baseT_Full,
578 .advertised = ADVERTISED_10000baseT_Full,
582 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
583 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
584 .supported = SUPPORTED_10000baseKX4_Full,
585 .advertised = ADVERTISED_10000baseKX4_Full,
589 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
590 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
591 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
592 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
593 .supported = SUPPORTED_10000baseKR_Full,
594 .advertised = ADVERTISED_10000baseKR_Full,
598 .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
599 .supported = SUPPORTED_20000baseKR2_Full,
600 .advertised = ADVERTISED_20000baseKR2_Full,
604 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
605 .supported = SUPPORTED_40000baseCR4_Full,
606 .advertised = ADVERTISED_40000baseCR4_Full,
610 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
611 .supported = SUPPORTED_40000baseKR4_Full,
612 .advertised = ADVERTISED_40000baseKR4_Full,
616 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
617 .supported = SUPPORTED_40000baseSR4_Full,
618 .advertised = ADVERTISED_40000baseSR4_Full,
622 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
623 .supported = SUPPORTED_40000baseLR4_Full,
624 .advertised = ADVERTISED_40000baseLR4_Full,
628 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR |
629 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR |
630 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
634 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR4 |
635 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2 |
636 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
640 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4 |
641 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
642 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
643 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
648 #define MLXSW_SX_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sx_port_link_mode)
649 #define MLXSW_SX_PORT_BASE_SPEED 10000 /* Mb/s */
651 static u32 mlxsw_sx_from_ptys_supported_port(u32 ptys_eth_proto)
653 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
654 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
655 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
656 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
657 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
658 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
659 return SUPPORTED_FIBRE;
661 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
662 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
663 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
664 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
665 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
666 return SUPPORTED_Backplane;
670 static u32 mlxsw_sx_from_ptys_supported_link(u32 ptys_eth_proto)
675 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
676 if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask)
677 modes |= mlxsw_sx_port_link_mode[i].supported;
682 static u32 mlxsw_sx_from_ptys_advert_link(u32 ptys_eth_proto)
687 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
688 if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask)
689 modes |= mlxsw_sx_port_link_mode[i].advertised;
694 static void mlxsw_sx_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto,
695 struct ethtool_link_ksettings *cmd)
697 u32 speed = SPEED_UNKNOWN;
698 u8 duplex = DUPLEX_UNKNOWN;
704 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
705 if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask) {
706 speed = mlxsw_sx_port_link_mode[i].speed;
707 duplex = DUPLEX_FULL;
712 cmd->base.speed = speed;
713 cmd->base.duplex = duplex;
716 static u8 mlxsw_sx_port_connector_port(u32 ptys_eth_proto)
718 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
719 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
720 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
721 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
724 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
725 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
726 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4))
729 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
730 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
731 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
732 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4))
739 mlxsw_sx_port_get_link_ksettings(struct net_device *dev,
740 struct ethtool_link_ksettings *cmd)
742 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
743 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
744 char ptys_pl[MLXSW_REG_PTYS_LEN];
748 u32 supported, advertising, lp_advertising;
751 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sx_port->local_port, 0, false);
752 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
754 netdev_err(dev, "Failed to get proto");
757 mlxsw_reg_ptys_eth_unpack(ptys_pl, ð_proto_cap,
758 ð_proto_admin, ð_proto_oper);
760 supported = mlxsw_sx_from_ptys_supported_port(eth_proto_cap) |
761 mlxsw_sx_from_ptys_supported_link(eth_proto_cap) |
762 SUPPORTED_Pause | SUPPORTED_Asym_Pause;
763 advertising = mlxsw_sx_from_ptys_advert_link(eth_proto_admin);
764 mlxsw_sx_from_ptys_speed_duplex(netif_carrier_ok(dev),
765 eth_proto_oper, cmd);
767 eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
768 cmd->base.port = mlxsw_sx_port_connector_port(eth_proto_oper);
769 lp_advertising = mlxsw_sx_from_ptys_advert_link(eth_proto_oper);
771 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
773 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
775 ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.lp_advertising,
781 static u32 mlxsw_sx_to_ptys_advert_link(u32 advertising)
786 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
787 if (advertising & mlxsw_sx_port_link_mode[i].advertised)
788 ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
793 static u32 mlxsw_sx_to_ptys_speed(u32 speed)
798 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
799 if (speed == mlxsw_sx_port_link_mode[i].speed)
800 ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
805 static u32 mlxsw_sx_to_ptys_upper_speed(u32 upper_speed)
810 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
811 if (mlxsw_sx_port_link_mode[i].speed <= upper_speed)
812 ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
818 mlxsw_sx_port_set_link_ksettings(struct net_device *dev,
819 const struct ethtool_link_ksettings *cmd)
821 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
822 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
823 char ptys_pl[MLXSW_REG_PTYS_LEN];
832 speed = cmd->base.speed;
834 ethtool_convert_link_mode_to_legacy_u32(&advertising,
835 cmd->link_modes.advertising);
837 eth_proto_new = cmd->base.autoneg == AUTONEG_ENABLE ?
838 mlxsw_sx_to_ptys_advert_link(advertising) :
839 mlxsw_sx_to_ptys_speed(speed);
841 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sx_port->local_port, 0, false);
842 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
844 netdev_err(dev, "Failed to get proto");
847 mlxsw_reg_ptys_eth_unpack(ptys_pl, ð_proto_cap, ð_proto_admin,
850 eth_proto_new = eth_proto_new & eth_proto_cap;
851 if (!eth_proto_new) {
852 netdev_err(dev, "Not supported proto admin requested");
855 if (eth_proto_new == eth_proto_admin)
858 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sx_port->local_port,
859 eth_proto_new, true);
860 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
862 netdev_err(dev, "Failed to set proto admin");
866 err = mlxsw_sx_port_oper_status_get(mlxsw_sx_port, &is_up);
868 netdev_err(dev, "Failed to get oper status");
874 err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
876 netdev_err(dev, "Failed to set admin status");
880 err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
882 netdev_err(dev, "Failed to set admin status");
889 static const struct ethtool_ops mlxsw_sx_port_ethtool_ops = {
890 .get_drvinfo = mlxsw_sx_port_get_drvinfo,
891 .get_link = ethtool_op_get_link,
892 .get_strings = mlxsw_sx_port_get_strings,
893 .get_ethtool_stats = mlxsw_sx_port_get_stats,
894 .get_sset_count = mlxsw_sx_port_get_sset_count,
895 .get_link_ksettings = mlxsw_sx_port_get_link_ksettings,
896 .set_link_ksettings = mlxsw_sx_port_set_link_ksettings,
899 static int mlxsw_sx_hw_id_get(struct mlxsw_sx *mlxsw_sx)
901 char spad_pl[MLXSW_REG_SPAD_LEN] = {0};
904 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(spad), spad_pl);
907 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sx->hw_id);
911 static int mlxsw_sx_port_dev_addr_get(struct mlxsw_sx_port *mlxsw_sx_port)
913 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
914 struct net_device *dev = mlxsw_sx_port->dev;
915 char ppad_pl[MLXSW_REG_PPAD_LEN];
918 mlxsw_reg_ppad_pack(ppad_pl, false, 0);
919 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ppad), ppad_pl);
922 mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, dev->dev_addr);
923 /* The last byte value in base mac address is guaranteed
924 * to be such it does not overflow when adding local_port
927 dev->dev_addr[ETH_ALEN - 1] += mlxsw_sx_port->local_port;
931 static int mlxsw_sx_port_stp_state_set(struct mlxsw_sx_port *mlxsw_sx_port,
932 u16 vid, enum mlxsw_reg_spms_state state)
934 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
938 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
941 mlxsw_reg_spms_pack(spms_pl, mlxsw_sx_port->local_port);
942 mlxsw_reg_spms_vid_pack(spms_pl, vid, state);
943 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(spms), spms_pl);
948 static int mlxsw_sx_port_ib_speed_set(struct mlxsw_sx_port *mlxsw_sx_port,
949 u16 speed, u16 width)
951 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
952 char ptys_pl[MLXSW_REG_PTYS_LEN];
954 mlxsw_reg_ptys_ib_pack(ptys_pl, mlxsw_sx_port->local_port, speed,
956 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
960 mlxsw_sx_port_speed_by_width_set(struct mlxsw_sx_port *mlxsw_sx_port, u8 width)
962 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
963 u32 upper_speed = MLXSW_SX_PORT_BASE_SPEED * width;
964 char ptys_pl[MLXSW_REG_PTYS_LEN];
967 eth_proto_admin = mlxsw_sx_to_ptys_upper_speed(upper_speed);
968 mlxsw_reg_ptys_eth_pack(ptys_pl, mlxsw_sx_port->local_port,
969 eth_proto_admin, true);
970 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
974 mlxsw_sx_port_mac_learning_mode_set(struct mlxsw_sx_port *mlxsw_sx_port,
975 enum mlxsw_reg_spmlr_learn_mode mode)
977 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
978 char spmlr_pl[MLXSW_REG_SPMLR_LEN];
980 mlxsw_reg_spmlr_pack(spmlr_pl, mlxsw_sx_port->local_port, mode);
981 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(spmlr), spmlr_pl);
984 static int __mlxsw_sx_port_eth_create(struct mlxsw_sx *mlxsw_sx, u8 local_port,
987 struct mlxsw_sx_port *mlxsw_sx_port;
988 struct net_device *dev;
991 dev = alloc_etherdev(sizeof(struct mlxsw_sx_port));
994 SET_NETDEV_DEV(dev, mlxsw_sx->bus_info->dev);
995 dev_net_set(dev, mlxsw_core_net(mlxsw_sx->core));
996 mlxsw_sx_port = netdev_priv(dev);
997 mlxsw_sx_port->dev = dev;
998 mlxsw_sx_port->mlxsw_sx = mlxsw_sx;
999 mlxsw_sx_port->local_port = local_port;
1000 mlxsw_sx_port->mapping.module = module;
1002 mlxsw_sx_port->pcpu_stats =
1003 netdev_alloc_pcpu_stats(struct mlxsw_sx_port_pcpu_stats);
1004 if (!mlxsw_sx_port->pcpu_stats) {
1006 goto err_alloc_stats;
1009 dev->netdev_ops = &mlxsw_sx_port_netdev_ops;
1010 dev->ethtool_ops = &mlxsw_sx_port_ethtool_ops;
1012 err = mlxsw_sx_port_dev_addr_get(mlxsw_sx_port);
1014 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Unable to get port mac address\n",
1015 mlxsw_sx_port->local_port);
1016 goto err_dev_addr_get;
1019 netif_carrier_off(dev);
1021 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
1022 NETIF_F_VLAN_CHALLENGED;
1025 dev->max_mtu = ETH_MAX_MTU;
1027 /* Each packet needs to have a Tx header (metadata) on top all other
1030 dev->needed_headroom = MLXSW_TXHDR_LEN;
1032 err = mlxsw_sx_port_system_port_mapping_set(mlxsw_sx_port);
1034 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set system port mapping\n",
1035 mlxsw_sx_port->local_port);
1036 goto err_port_system_port_mapping_set;
1039 err = mlxsw_sx_port_swid_set(mlxsw_sx_port, 0);
1041 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set SWID\n",
1042 mlxsw_sx_port->local_port);
1043 goto err_port_swid_set;
1046 err = mlxsw_sx_port_speed_by_width_set(mlxsw_sx_port, width);
1048 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set speed\n",
1049 mlxsw_sx_port->local_port);
1050 goto err_port_speed_set;
1053 err = mlxsw_sx_port_mtu_eth_set(mlxsw_sx_port, ETH_DATA_LEN);
1055 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MTU\n",
1056 mlxsw_sx_port->local_port);
1057 goto err_port_mtu_set;
1060 err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
1062 goto err_port_admin_status_set;
1064 err = mlxsw_sx_port_stp_state_set(mlxsw_sx_port,
1065 MLXSW_PORT_DEFAULT_VID,
1066 MLXSW_REG_SPMS_STATE_FORWARDING);
1068 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set STP state\n",
1069 mlxsw_sx_port->local_port);
1070 goto err_port_stp_state_set;
1073 err = mlxsw_sx_port_mac_learning_mode_set(mlxsw_sx_port,
1074 MLXSW_REG_SPMLR_LEARN_MODE_DISABLE);
1076 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MAC learning mode\n",
1077 mlxsw_sx_port->local_port);
1078 goto err_port_mac_learning_mode_set;
1081 err = register_netdev(dev);
1083 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to register netdev\n",
1084 mlxsw_sx_port->local_port);
1085 goto err_register_netdev;
1088 mlxsw_core_port_eth_set(mlxsw_sx->core, mlxsw_sx_port->local_port,
1089 mlxsw_sx_port, dev);
1090 mlxsw_sx->ports[local_port] = mlxsw_sx_port;
1093 err_register_netdev:
1094 err_port_mac_learning_mode_set:
1095 err_port_stp_state_set:
1096 err_port_admin_status_set:
1099 mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
1101 err_port_system_port_mapping_set:
1103 free_percpu(mlxsw_sx_port->pcpu_stats);
1109 static int mlxsw_sx_port_eth_create(struct mlxsw_sx *mlxsw_sx, u8 local_port,
1110 u8 module, u8 width)
1114 err = mlxsw_core_port_init(mlxsw_sx->core, local_port,
1115 module + 1, false, 0,
1116 mlxsw_sx->hw_id, sizeof(mlxsw_sx->hw_id));
1118 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to init core port\n",
1122 err = __mlxsw_sx_port_eth_create(mlxsw_sx, local_port, module, width);
1124 goto err_port_create;
1129 mlxsw_core_port_fini(mlxsw_sx->core, local_port);
1133 static void __mlxsw_sx_port_eth_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
1135 struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
1137 mlxsw_core_port_clear(mlxsw_sx->core, local_port, mlxsw_sx);
1138 unregister_netdev(mlxsw_sx_port->dev); /* This calls ndo_stop */
1139 mlxsw_sx->ports[local_port] = NULL;
1140 mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
1141 free_percpu(mlxsw_sx_port->pcpu_stats);
1142 free_netdev(mlxsw_sx_port->dev);
1145 static bool mlxsw_sx_port_created(struct mlxsw_sx *mlxsw_sx, u8 local_port)
1147 return mlxsw_sx->ports[local_port] != NULL;
1150 static int __mlxsw_sx_port_ib_create(struct mlxsw_sx *mlxsw_sx, u8 local_port,
1151 u8 module, u8 width)
1153 struct mlxsw_sx_port *mlxsw_sx_port;
1156 mlxsw_sx_port = kzalloc(sizeof(*mlxsw_sx_port), GFP_KERNEL);
1159 mlxsw_sx_port->mlxsw_sx = mlxsw_sx;
1160 mlxsw_sx_port->local_port = local_port;
1161 mlxsw_sx_port->mapping.module = module;
1163 err = mlxsw_sx_port_system_port_mapping_set(mlxsw_sx_port);
1165 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set system port mapping\n",
1166 mlxsw_sx_port->local_port);
1167 goto err_port_system_port_mapping_set;
1170 /* Adding port to Infiniband swid (1) */
1171 err = mlxsw_sx_port_swid_set(mlxsw_sx_port, 1);
1173 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set SWID\n",
1174 mlxsw_sx_port->local_port);
1175 goto err_port_swid_set;
1178 /* Expose the IB port number as it's front panel name */
1179 err = mlxsw_sx_port_ib_port_set(mlxsw_sx_port, module + 1);
1181 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set IB port\n",
1182 mlxsw_sx_port->local_port);
1183 goto err_port_ib_set;
1186 /* Supports all speeds from SDR to FDR (bitmask) and support bus width
1187 * of 1x, 2x and 4x (3 bits bitmask)
1189 err = mlxsw_sx_port_ib_speed_set(mlxsw_sx_port,
1190 MLXSW_REG_PTYS_IB_SPEED_EDR - 1,
1193 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set speed\n",
1194 mlxsw_sx_port->local_port);
1195 goto err_port_speed_set;
1198 /* Change to the maximum MTU the device supports, the SMA will take
1199 * care of the active MTU
1201 err = mlxsw_sx_port_mtu_ib_set(mlxsw_sx_port, MLXSW_IB_DEFAULT_MTU);
1203 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MTU\n",
1204 mlxsw_sx_port->local_port);
1205 goto err_port_mtu_set;
1208 err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
1210 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to change admin state to UP\n",
1211 mlxsw_sx_port->local_port);
1212 goto err_port_admin_set;
1215 mlxsw_core_port_ib_set(mlxsw_sx->core, mlxsw_sx_port->local_port,
1217 mlxsw_sx->ports[local_port] = mlxsw_sx_port;
1224 mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
1226 err_port_system_port_mapping_set:
1227 kfree(mlxsw_sx_port);
1231 static void __mlxsw_sx_port_ib_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
1233 struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
1235 mlxsw_core_port_clear(mlxsw_sx->core, local_port, mlxsw_sx);
1236 mlxsw_sx->ports[local_port] = NULL;
1237 mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
1238 mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
1239 kfree(mlxsw_sx_port);
1242 static void __mlxsw_sx_port_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
1244 enum devlink_port_type port_type =
1245 mlxsw_core_port_type_get(mlxsw_sx->core, local_port);
1247 if (port_type == DEVLINK_PORT_TYPE_ETH)
1248 __mlxsw_sx_port_eth_remove(mlxsw_sx, local_port);
1249 else if (port_type == DEVLINK_PORT_TYPE_IB)
1250 __mlxsw_sx_port_ib_remove(mlxsw_sx, local_port);
1253 static void mlxsw_sx_port_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
1255 __mlxsw_sx_port_remove(mlxsw_sx, local_port);
1256 mlxsw_core_port_fini(mlxsw_sx->core, local_port);
1259 static void mlxsw_sx_ports_remove(struct mlxsw_sx *mlxsw_sx)
1263 for (i = 1; i < mlxsw_core_max_ports(mlxsw_sx->core); i++)
1264 if (mlxsw_sx_port_created(mlxsw_sx, i))
1265 mlxsw_sx_port_remove(mlxsw_sx, i);
1266 kfree(mlxsw_sx->ports);
1269 static int mlxsw_sx_ports_create(struct mlxsw_sx *mlxsw_sx)
1271 unsigned int max_ports = mlxsw_core_max_ports(mlxsw_sx->core);
1277 alloc_size = sizeof(struct mlxsw_sx_port *) * max_ports;
1278 mlxsw_sx->ports = kzalloc(alloc_size, GFP_KERNEL);
1279 if (!mlxsw_sx->ports)
1282 for (i = 1; i < max_ports; i++) {
1283 err = mlxsw_sx_port_module_info_get(mlxsw_sx, i, &module,
1286 goto err_port_module_info_get;
1289 err = mlxsw_sx_port_eth_create(mlxsw_sx, i, module, width);
1291 goto err_port_create;
1296 err_port_module_info_get:
1297 for (i--; i >= 1; i--)
1298 if (mlxsw_sx_port_created(mlxsw_sx, i))
1299 mlxsw_sx_port_remove(mlxsw_sx, i);
1300 kfree(mlxsw_sx->ports);
1304 static void mlxsw_sx_pude_eth_event_func(struct mlxsw_sx_port *mlxsw_sx_port,
1305 enum mlxsw_reg_pude_oper_status status)
1307 if (status == MLXSW_PORT_OPER_STATUS_UP) {
1308 netdev_info(mlxsw_sx_port->dev, "link up\n");
1309 netif_carrier_on(mlxsw_sx_port->dev);
1311 netdev_info(mlxsw_sx_port->dev, "link down\n");
1312 netif_carrier_off(mlxsw_sx_port->dev);
1316 static void mlxsw_sx_pude_ib_event_func(struct mlxsw_sx_port *mlxsw_sx_port,
1317 enum mlxsw_reg_pude_oper_status status)
1319 if (status == MLXSW_PORT_OPER_STATUS_UP)
1320 pr_info("ib link for port %d - up\n",
1321 mlxsw_sx_port->mapping.module + 1);
1323 pr_info("ib link for port %d - down\n",
1324 mlxsw_sx_port->mapping.module + 1);
1327 static void mlxsw_sx_pude_event_func(const struct mlxsw_reg_info *reg,
1328 char *pude_pl, void *priv)
1330 struct mlxsw_sx *mlxsw_sx = priv;
1331 struct mlxsw_sx_port *mlxsw_sx_port;
1332 enum mlxsw_reg_pude_oper_status status;
1333 enum devlink_port_type port_type;
1336 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
1337 mlxsw_sx_port = mlxsw_sx->ports[local_port];
1338 if (!mlxsw_sx_port) {
1339 dev_warn(mlxsw_sx->bus_info->dev, "Port %d: Link event received for non-existent port\n",
1344 status = mlxsw_reg_pude_oper_status_get(pude_pl);
1345 port_type = mlxsw_core_port_type_get(mlxsw_sx->core, local_port);
1346 if (port_type == DEVLINK_PORT_TYPE_ETH)
1347 mlxsw_sx_pude_eth_event_func(mlxsw_sx_port, status);
1348 else if (port_type == DEVLINK_PORT_TYPE_IB)
1349 mlxsw_sx_pude_ib_event_func(mlxsw_sx_port, status);
1352 static void mlxsw_sx_rx_listener_func(struct sk_buff *skb, u8 local_port,
1355 struct mlxsw_sx *mlxsw_sx = priv;
1356 struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
1357 struct mlxsw_sx_port_pcpu_stats *pcpu_stats;
1359 if (unlikely(!mlxsw_sx_port)) {
1360 dev_warn_ratelimited(mlxsw_sx->bus_info->dev, "Port %d: skb received for non-existent port\n",
1365 skb->dev = mlxsw_sx_port->dev;
1367 pcpu_stats = this_cpu_ptr(mlxsw_sx_port->pcpu_stats);
1368 u64_stats_update_begin(&pcpu_stats->syncp);
1369 pcpu_stats->rx_packets++;
1370 pcpu_stats->rx_bytes += skb->len;
1371 u64_stats_update_end(&pcpu_stats->syncp);
1373 skb->protocol = eth_type_trans(skb, skb->dev);
1374 netif_receive_skb(skb);
1377 static int mlxsw_sx_port_type_set(struct mlxsw_core *mlxsw_core, u8 local_port,
1378 enum devlink_port_type new_type)
1380 struct mlxsw_sx *mlxsw_sx = mlxsw_core_driver_priv(mlxsw_core);
1384 if (new_type == DEVLINK_PORT_TYPE_AUTO)
1387 __mlxsw_sx_port_remove(mlxsw_sx, local_port);
1388 err = mlxsw_sx_port_module_info_get(mlxsw_sx, local_port, &module,
1391 goto err_port_module_info_get;
1393 if (new_type == DEVLINK_PORT_TYPE_ETH)
1394 err = __mlxsw_sx_port_eth_create(mlxsw_sx, local_port, module,
1396 else if (new_type == DEVLINK_PORT_TYPE_IB)
1397 err = __mlxsw_sx_port_ib_create(mlxsw_sx, local_port, module,
1400 err_port_module_info_get:
1404 #define MLXSW_SX_RXL(_trap_id) \
1405 MLXSW_RXL(mlxsw_sx_rx_listener_func, _trap_id, TRAP_TO_CPU, \
1406 false, SX2_RX, FORWARD)
1408 static const struct mlxsw_listener mlxsw_sx_listener[] = {
1409 MLXSW_EVENTL(mlxsw_sx_pude_event_func, PUDE, EMAD),
1410 MLXSW_SX_RXL(FDB_MC),
1413 MLXSW_SX_RXL(EAPOL),
1417 MLXSW_SX_RXL(RPVST),
1419 MLXSW_SX_RXL(IGMP_QUERY),
1420 MLXSW_SX_RXL(IGMP_V1_REPORT),
1421 MLXSW_SX_RXL(IGMP_V2_REPORT),
1422 MLXSW_SX_RXL(IGMP_V2_LEAVE),
1423 MLXSW_SX_RXL(IGMP_V3_REPORT),
1426 static int mlxsw_sx_traps_init(struct mlxsw_sx *mlxsw_sx)
1428 char htgt_pl[MLXSW_REG_HTGT_LEN];
1432 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_SX2_RX,
1433 MLXSW_REG_HTGT_INVALID_POLICER,
1434 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
1435 MLXSW_REG_HTGT_DEFAULT_TC);
1436 mlxsw_reg_htgt_local_path_rdq_set(htgt_pl,
1437 MLXSW_REG_HTGT_LOCAL_PATH_RDQ_SX2_RX);
1439 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(htgt), htgt_pl);
1443 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_SX2_CTRL,
1444 MLXSW_REG_HTGT_INVALID_POLICER,
1445 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
1446 MLXSW_REG_HTGT_DEFAULT_TC);
1447 mlxsw_reg_htgt_local_path_rdq_set(htgt_pl,
1448 MLXSW_REG_HTGT_LOCAL_PATH_RDQ_SX2_CTRL);
1450 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(htgt), htgt_pl);
1454 for (i = 0; i < ARRAY_SIZE(mlxsw_sx_listener); i++) {
1455 err = mlxsw_core_trap_register(mlxsw_sx->core,
1456 &mlxsw_sx_listener[i],
1459 goto err_listener_register;
1464 err_listener_register:
1465 for (i--; i >= 0; i--) {
1466 mlxsw_core_trap_unregister(mlxsw_sx->core,
1467 &mlxsw_sx_listener[i],
1473 static void mlxsw_sx_traps_fini(struct mlxsw_sx *mlxsw_sx)
1477 for (i = 0; i < ARRAY_SIZE(mlxsw_sx_listener); i++) {
1478 mlxsw_core_trap_unregister(mlxsw_sx->core,
1479 &mlxsw_sx_listener[i],
1484 static int mlxsw_sx_flood_init(struct mlxsw_sx *mlxsw_sx)
1486 char sfgc_pl[MLXSW_REG_SFGC_LEN];
1487 char sgcr_pl[MLXSW_REG_SGCR_LEN];
1491 /* Configure a flooding table, which includes only CPU port. */
1492 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
1495 mlxsw_reg_sftr_pack(sftr_pl, 0, 0, MLXSW_REG_SFGC_TABLE_TYPE_SINGLE, 0,
1496 MLXSW_PORT_CPU_PORT, true);
1497 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sftr), sftr_pl);
1502 /* Flood different packet types using the flooding table. */
1503 mlxsw_reg_sfgc_pack(sfgc_pl,
1504 MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST,
1505 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1506 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1508 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1512 mlxsw_reg_sfgc_pack(sfgc_pl,
1513 MLXSW_REG_SFGC_TYPE_BROADCAST,
1514 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1515 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1517 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1521 mlxsw_reg_sfgc_pack(sfgc_pl,
1522 MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_NON_IP,
1523 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1524 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1526 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1530 mlxsw_reg_sfgc_pack(sfgc_pl,
1531 MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_IPV6,
1532 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1533 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1535 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1539 mlxsw_reg_sfgc_pack(sfgc_pl,
1540 MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_IPV4,
1541 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1542 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1544 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1548 mlxsw_reg_sgcr_pack(sgcr_pl, true);
1549 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sgcr), sgcr_pl);
1552 static int mlxsw_sx_basic_trap_groups_set(struct mlxsw_core *mlxsw_core)
1554 char htgt_pl[MLXSW_REG_HTGT_LEN];
1556 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_EMAD,
1557 MLXSW_REG_HTGT_INVALID_POLICER,
1558 MLXSW_REG_HTGT_DEFAULT_PRIORITY,
1559 MLXSW_REG_HTGT_DEFAULT_TC);
1560 mlxsw_reg_htgt_swid_set(htgt_pl, MLXSW_PORT_SWID_ALL_SWIDS);
1561 mlxsw_reg_htgt_local_path_rdq_set(htgt_pl,
1562 MLXSW_REG_HTGT_LOCAL_PATH_RDQ_SX2_EMAD);
1563 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(htgt), htgt_pl);
1566 static int mlxsw_sx_init(struct mlxsw_core *mlxsw_core,
1567 const struct mlxsw_bus_info *mlxsw_bus_info,
1568 struct netlink_ext_ack *extack)
1570 struct mlxsw_sx *mlxsw_sx = mlxsw_core_driver_priv(mlxsw_core);
1573 mlxsw_sx->core = mlxsw_core;
1574 mlxsw_sx->bus_info = mlxsw_bus_info;
1576 err = mlxsw_sx_hw_id_get(mlxsw_sx);
1578 dev_err(mlxsw_sx->bus_info->dev, "Failed to get switch HW ID\n");
1582 err = mlxsw_sx_ports_create(mlxsw_sx);
1584 dev_err(mlxsw_sx->bus_info->dev, "Failed to create ports\n");
1588 err = mlxsw_sx_traps_init(mlxsw_sx);
1590 dev_err(mlxsw_sx->bus_info->dev, "Failed to set traps\n");
1591 goto err_listener_register;
1594 err = mlxsw_sx_flood_init(mlxsw_sx);
1596 dev_err(mlxsw_sx->bus_info->dev, "Failed to initialize flood tables\n");
1597 goto err_flood_init;
1603 mlxsw_sx_traps_fini(mlxsw_sx);
1604 err_listener_register:
1605 mlxsw_sx_ports_remove(mlxsw_sx);
1609 static void mlxsw_sx_fini(struct mlxsw_core *mlxsw_core)
1611 struct mlxsw_sx *mlxsw_sx = mlxsw_core_driver_priv(mlxsw_core);
1613 mlxsw_sx_traps_fini(mlxsw_sx);
1614 mlxsw_sx_ports_remove(mlxsw_sx);
1617 static const struct mlxsw_config_profile mlxsw_sx_config_profile = {
1618 .used_max_vepa_channels = 1,
1619 .max_vepa_channels = 0,
1624 .used_max_system_port = 1,
1625 .max_system_port = 48000,
1626 .used_max_vlan_groups = 1,
1627 .max_vlan_groups = 127,
1628 .used_max_regions = 1,
1630 .used_flood_tables = 1,
1631 .max_flood_tables = 2,
1632 .max_vid_flood_tables = 1,
1633 .used_flood_mode = 1,
1635 .used_max_ib_mc = 1,
1642 .type = MLXSW_PORT_SWID_TYPE_ETH,
1646 .type = MLXSW_PORT_SWID_TYPE_IB,
1651 static struct mlxsw_driver mlxsw_sx_driver = {
1652 .kind = mlxsw_sx_driver_name,
1653 .priv_size = sizeof(struct mlxsw_sx),
1654 .init = mlxsw_sx_init,
1655 .fini = mlxsw_sx_fini,
1656 .basic_trap_groups_set = mlxsw_sx_basic_trap_groups_set,
1657 .txhdr_construct = mlxsw_sx_txhdr_construct,
1658 .txhdr_len = MLXSW_TXHDR_LEN,
1659 .profile = &mlxsw_sx_config_profile,
1660 .port_type_set = mlxsw_sx_port_type_set,
1663 static const struct pci_device_id mlxsw_sx_pci_id_table[] = {
1664 {PCI_VDEVICE(MELLANOX, PCI_DEVICE_ID_MELLANOX_SWITCHX2), 0},
1668 static struct pci_driver mlxsw_sx_pci_driver = {
1669 .name = mlxsw_sx_driver_name,
1670 .id_table = mlxsw_sx_pci_id_table,
1673 static int __init mlxsw_sx_module_init(void)
1677 err = mlxsw_core_driver_register(&mlxsw_sx_driver);
1681 err = mlxsw_pci_driver_register(&mlxsw_sx_pci_driver);
1683 goto err_pci_driver_register;
1687 err_pci_driver_register:
1688 mlxsw_core_driver_unregister(&mlxsw_sx_driver);
1692 static void __exit mlxsw_sx_module_exit(void)
1694 mlxsw_pci_driver_unregister(&mlxsw_sx_pci_driver);
1695 mlxsw_core_driver_unregister(&mlxsw_sx_driver);
1698 module_init(mlxsw_sx_module_init);
1699 module_exit(mlxsw_sx_module_exit);
1701 MODULE_LICENSE("Dual BSD/GPL");
1702 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
1703 MODULE_DESCRIPTION("Mellanox SwitchX-2 driver");
1704 MODULE_DEVICE_TABLE(pci, mlxsw_sx_pci_id_table);