2 * drivers/net/ethernet/mellanox/mlxsw/switchx2.c
3 * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6 * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/types.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/slab.h>
43 #include <linux/device.h>
44 #include <linux/skbuff.h>
45 #include <linux/if_vlan.h>
46 #include <net/switchdev.h>
47 #include <generated/utsrelease.h>
55 static const char mlxsw_sx_driver_name[] = "mlxsw_switchx2";
56 static const char mlxsw_sx_driver_version[] = "1.0";
61 struct mlxsw_sx_port **ports;
62 struct mlxsw_core *core;
63 const struct mlxsw_bus_info *bus_info;
67 struct mlxsw_sx_port_pcpu_stats {
72 struct u64_stats_sync syncp;
76 struct mlxsw_sx_port {
77 struct net_device *dev;
78 struct mlxsw_sx_port_pcpu_stats __percpu *pcpu_stats;
79 struct mlxsw_sx *mlxsw_sx;
87 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
90 * Packet control type.
91 * 0 - Ethernet control (e.g. EMADs, LACP)
94 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
97 * Packet protocol type. Must be set to 1 (Ethernet).
99 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
102 * Egress TClass to be used on the egress device on the egress port.
103 * The MSB is specified in the 'ctclass3' field.
104 * Range is 0-15, where 15 is the highest priority.
106 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 18, 3);
109 * Switch partition ID.
111 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
114 * Destination local port for unicast packets.
115 * Destination multicast ID for multicast packets.
117 * Control packets are directed to a specific egress port, while data
118 * packets are transmitted through the CPU port (0) into the switch partition,
119 * where forwarding rules are applied.
121 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
124 * See field 'etclass'.
126 MLXSW_ITEM32(tx, hdr, ctclass3, 0x04, 14, 1);
129 * RDQ for control packets sent to remote CPU.
130 * Must be set to 0x1F for EMADs, otherwise 0.
132 MLXSW_ITEM32(tx, hdr, rdq, 0x04, 9, 5);
135 * Signature control for packets going to CPU. Must be set to 0.
137 MLXSW_ITEM32(tx, hdr, cpu_sig, 0x04, 0, 9);
140 * Stacking protocl signature. Must be set to 0xE0E0.
142 MLXSW_ITEM32(tx, hdr, sig, 0x0C, 16, 16);
147 MLXSW_ITEM32(tx, hdr, stclass, 0x0C, 13, 3);
150 * EMAD bit. Must be set for EMADs.
152 MLXSW_ITEM32(tx, hdr, emad, 0x0C, 5, 1);
156 * 6 - Control packets
158 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
160 static void mlxsw_sx_txhdr_construct(struct sk_buff *skb,
161 const struct mlxsw_tx_info *tx_info)
163 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
164 bool is_emad = tx_info->is_emad;
166 memset(txhdr, 0, MLXSW_TXHDR_LEN);
168 /* We currently set default values for the egress tclass (QoS). */
169 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_0);
170 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
171 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
172 mlxsw_tx_hdr_etclass_set(txhdr, is_emad ? MLXSW_TXHDR_ETCLASS_6 :
173 MLXSW_TXHDR_ETCLASS_5);
174 mlxsw_tx_hdr_swid_set(txhdr, 0);
175 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
176 mlxsw_tx_hdr_ctclass3_set(txhdr, MLXSW_TXHDR_CTCLASS3);
177 mlxsw_tx_hdr_rdq_set(txhdr, is_emad ? MLXSW_TXHDR_RDQ_EMAD :
178 MLXSW_TXHDR_RDQ_OTHER);
179 mlxsw_tx_hdr_cpu_sig_set(txhdr, MLXSW_TXHDR_CPU_SIG);
180 mlxsw_tx_hdr_sig_set(txhdr, MLXSW_TXHDR_SIG);
181 mlxsw_tx_hdr_stclass_set(txhdr, MLXSW_TXHDR_STCLASS_NONE);
182 mlxsw_tx_hdr_emad_set(txhdr, is_emad ? MLXSW_TXHDR_EMAD :
183 MLXSW_TXHDR_NOT_EMAD);
184 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
187 static int mlxsw_sx_port_admin_status_set(struct mlxsw_sx_port *mlxsw_sx_port,
190 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
191 char paos_pl[MLXSW_REG_PAOS_LEN];
193 mlxsw_reg_paos_pack(paos_pl, mlxsw_sx_port->local_port,
194 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
195 MLXSW_PORT_ADMIN_STATUS_DOWN);
196 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(paos), paos_pl);
199 static int mlxsw_sx_port_oper_status_get(struct mlxsw_sx_port *mlxsw_sx_port,
202 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
203 char paos_pl[MLXSW_REG_PAOS_LEN];
207 mlxsw_reg_paos_pack(paos_pl, mlxsw_sx_port->local_port, 0);
208 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(paos), paos_pl);
211 oper_status = mlxsw_reg_paos_oper_status_get(paos_pl);
212 *p_is_up = oper_status == MLXSW_PORT_ADMIN_STATUS_UP ? true : false;
216 static int mlxsw_sx_port_mtu_set(struct mlxsw_sx_port *mlxsw_sx_port, u16 mtu)
218 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
219 char pmtu_pl[MLXSW_REG_PMTU_LEN];
223 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
224 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sx_port->local_port, 0);
225 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(pmtu), pmtu_pl);
228 max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
233 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sx_port->local_port, mtu);
234 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(pmtu), pmtu_pl);
237 static int mlxsw_sx_port_swid_set(struct mlxsw_sx_port *mlxsw_sx_port, u8 swid)
239 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
240 char pspa_pl[MLXSW_REG_PSPA_LEN];
242 mlxsw_reg_pspa_pack(pspa_pl, swid, mlxsw_sx_port->local_port);
243 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(pspa), pspa_pl);
247 mlxsw_sx_port_system_port_mapping_set(struct mlxsw_sx_port *mlxsw_sx_port)
249 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
250 char sspr_pl[MLXSW_REG_SSPR_LEN];
252 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sx_port->local_port);
253 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sspr), sspr_pl);
256 static int mlxsw_sx_port_module_check(struct mlxsw_sx_port *mlxsw_sx_port,
259 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
260 char pmlp_pl[MLXSW_REG_PMLP_LEN];
263 mlxsw_reg_pmlp_pack(pmlp_pl, mlxsw_sx_port->local_port);
264 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(pmlp), pmlp_pl);
267 *p_usable = mlxsw_reg_pmlp_width_get(pmlp_pl) ? true : false;
271 static int mlxsw_sx_port_open(struct net_device *dev)
273 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
276 err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
279 netif_start_queue(dev);
283 static int mlxsw_sx_port_stop(struct net_device *dev)
285 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
287 netif_stop_queue(dev);
288 return mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
291 static netdev_tx_t mlxsw_sx_port_xmit(struct sk_buff *skb,
292 struct net_device *dev)
294 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
295 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
296 struct mlxsw_sx_port_pcpu_stats *pcpu_stats;
297 const struct mlxsw_tx_info tx_info = {
298 .local_port = mlxsw_sx_port->local_port,
304 if (mlxsw_core_skb_transmit_busy(mlxsw_sx, &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);
320 /* Due to a race we might fail here because of a full queue. In that
321 * unlikely case we simply drop the packet.
323 err = mlxsw_core_skb_transmit(mlxsw_sx, skb, &tx_info);
326 pcpu_stats = this_cpu_ptr(mlxsw_sx_port->pcpu_stats);
327 u64_stats_update_begin(&pcpu_stats->syncp);
328 pcpu_stats->tx_packets++;
329 pcpu_stats->tx_bytes += len;
330 u64_stats_update_end(&pcpu_stats->syncp);
332 this_cpu_inc(mlxsw_sx_port->pcpu_stats->tx_dropped);
333 dev_kfree_skb_any(skb);
338 static int mlxsw_sx_port_change_mtu(struct net_device *dev, int mtu)
340 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
343 err = mlxsw_sx_port_mtu_set(mlxsw_sx_port, mtu);
350 static struct rtnl_link_stats64 *
351 mlxsw_sx_port_get_stats64(struct net_device *dev,
352 struct rtnl_link_stats64 *stats)
354 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
355 struct mlxsw_sx_port_pcpu_stats *p;
356 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
361 for_each_possible_cpu(i) {
362 p = per_cpu_ptr(mlxsw_sx_port->pcpu_stats, i);
364 start = u64_stats_fetch_begin_irq(&p->syncp);
365 rx_packets = p->rx_packets;
366 rx_bytes = p->rx_bytes;
367 tx_packets = p->tx_packets;
368 tx_bytes = p->tx_bytes;
369 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
371 stats->rx_packets += rx_packets;
372 stats->rx_bytes += rx_bytes;
373 stats->tx_packets += tx_packets;
374 stats->tx_bytes += tx_bytes;
375 /* tx_dropped is u32, updated without syncp protection. */
376 tx_dropped += p->tx_dropped;
378 stats->tx_dropped = tx_dropped;
382 static const struct net_device_ops mlxsw_sx_port_netdev_ops = {
383 .ndo_open = mlxsw_sx_port_open,
384 .ndo_stop = mlxsw_sx_port_stop,
385 .ndo_start_xmit = mlxsw_sx_port_xmit,
386 .ndo_change_mtu = mlxsw_sx_port_change_mtu,
387 .ndo_get_stats64 = mlxsw_sx_port_get_stats64,
390 static void mlxsw_sx_port_get_drvinfo(struct net_device *dev,
391 struct ethtool_drvinfo *drvinfo)
393 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
394 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
396 strlcpy(drvinfo->driver, mlxsw_sx_driver_name, sizeof(drvinfo->driver));
397 strlcpy(drvinfo->version, mlxsw_sx_driver_version,
398 sizeof(drvinfo->version));
399 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
401 mlxsw_sx->bus_info->fw_rev.major,
402 mlxsw_sx->bus_info->fw_rev.minor,
403 mlxsw_sx->bus_info->fw_rev.subminor);
404 strlcpy(drvinfo->bus_info, mlxsw_sx->bus_info->device_name,
405 sizeof(drvinfo->bus_info));
408 struct mlxsw_sx_port_hw_stats {
409 char str[ETH_GSTRING_LEN];
410 u64 (*getter)(char *payload);
413 static const struct mlxsw_sx_port_hw_stats mlxsw_sx_port_hw_stats[] = {
415 .str = "a_frames_transmitted_ok",
416 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
419 .str = "a_frames_received_ok",
420 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
423 .str = "a_frame_check_sequence_errors",
424 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
427 .str = "a_alignment_errors",
428 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
431 .str = "a_octets_transmitted_ok",
432 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
435 .str = "a_octets_received_ok",
436 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
439 .str = "a_multicast_frames_xmitted_ok",
440 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
443 .str = "a_broadcast_frames_xmitted_ok",
444 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
447 .str = "a_multicast_frames_received_ok",
448 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
451 .str = "a_broadcast_frames_received_ok",
452 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
455 .str = "a_in_range_length_errors",
456 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
459 .str = "a_out_of_range_length_field",
460 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
463 .str = "a_frame_too_long_errors",
464 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
467 .str = "a_symbol_error_during_carrier",
468 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
471 .str = "a_mac_control_frames_transmitted",
472 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
475 .str = "a_mac_control_frames_received",
476 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
479 .str = "a_unsupported_opcodes_received",
480 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
483 .str = "a_pause_mac_ctrl_frames_received",
484 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
487 .str = "a_pause_mac_ctrl_frames_xmitted",
488 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
492 #define MLXSW_SX_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sx_port_hw_stats)
494 static void mlxsw_sx_port_get_strings(struct net_device *dev,
495 u32 stringset, u8 *data)
502 for (i = 0; i < MLXSW_SX_PORT_HW_STATS_LEN; i++) {
503 memcpy(p, mlxsw_sx_port_hw_stats[i].str,
505 p += ETH_GSTRING_LEN;
511 static void mlxsw_sx_port_get_stats(struct net_device *dev,
512 struct ethtool_stats *stats, u64 *data)
514 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
515 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
516 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
520 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sx_port->local_port);
521 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ppcnt), ppcnt_pl);
522 for (i = 0; i < MLXSW_SX_PORT_HW_STATS_LEN; i++)
523 data[i] = !err ? mlxsw_sx_port_hw_stats[i].getter(ppcnt_pl) : 0;
526 static int mlxsw_sx_port_get_sset_count(struct net_device *dev, int sset)
530 return MLXSW_SX_PORT_HW_STATS_LEN;
536 struct mlxsw_sx_port_link_mode {
543 static const struct mlxsw_sx_port_link_mode mlxsw_sx_port_link_mode[] = {
545 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
546 .supported = SUPPORTED_100baseT_Full,
547 .advertised = ADVERTISED_100baseT_Full,
551 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_TX,
555 .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
556 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
557 .supported = SUPPORTED_1000baseKX_Full,
558 .advertised = ADVERTISED_1000baseKX_Full,
562 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
563 .supported = SUPPORTED_10000baseT_Full,
564 .advertised = ADVERTISED_10000baseT_Full,
568 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
569 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
570 .supported = SUPPORTED_10000baseKX4_Full,
571 .advertised = ADVERTISED_10000baseKX4_Full,
575 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
576 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
577 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
578 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
579 .supported = SUPPORTED_10000baseKR_Full,
580 .advertised = ADVERTISED_10000baseKR_Full,
584 .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
585 .supported = SUPPORTED_20000baseKR2_Full,
586 .advertised = ADVERTISED_20000baseKR2_Full,
590 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
591 .supported = SUPPORTED_40000baseCR4_Full,
592 .advertised = ADVERTISED_40000baseCR4_Full,
596 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
597 .supported = SUPPORTED_40000baseKR4_Full,
598 .advertised = ADVERTISED_40000baseKR4_Full,
602 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
603 .supported = SUPPORTED_40000baseSR4_Full,
604 .advertised = ADVERTISED_40000baseSR4_Full,
608 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
609 .supported = SUPPORTED_40000baseLR4_Full,
610 .advertised = ADVERTISED_40000baseLR4_Full,
614 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR |
615 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR |
616 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
620 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR4 |
621 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2 |
622 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
626 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
627 .supported = SUPPORTED_56000baseKR4_Full,
628 .advertised = ADVERTISED_56000baseKR4_Full,
632 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4 |
633 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
634 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
635 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
640 #define MLXSW_SX_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sx_port_link_mode)
642 static u32 mlxsw_sx_from_ptys_supported_port(u32 ptys_eth_proto)
644 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
645 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
646 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
647 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
648 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
649 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
650 return SUPPORTED_FIBRE;
652 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
653 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
654 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
655 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
656 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
657 return SUPPORTED_Backplane;
661 static u32 mlxsw_sx_from_ptys_supported_link(u32 ptys_eth_proto)
666 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
667 if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask)
668 modes |= mlxsw_sx_port_link_mode[i].supported;
673 static u32 mlxsw_sx_from_ptys_advert_link(u32 ptys_eth_proto)
678 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
679 if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask)
680 modes |= mlxsw_sx_port_link_mode[i].advertised;
685 static void mlxsw_sx_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto,
686 struct ethtool_cmd *cmd)
688 u32 speed = SPEED_UNKNOWN;
689 u8 duplex = DUPLEX_UNKNOWN;
695 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
696 if (ptys_eth_proto & mlxsw_sx_port_link_mode[i].mask) {
697 speed = mlxsw_sx_port_link_mode[i].speed;
698 duplex = DUPLEX_FULL;
703 ethtool_cmd_speed_set(cmd, speed);
704 cmd->duplex = duplex;
707 static u8 mlxsw_sx_port_connector_port(u32 ptys_eth_proto)
709 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
710 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
711 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
712 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
715 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
716 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
717 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4))
720 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
721 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
722 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
723 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4))
729 static int mlxsw_sx_port_get_settings(struct net_device *dev,
730 struct ethtool_cmd *cmd)
732 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
733 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
734 char ptys_pl[MLXSW_REG_PTYS_LEN];
740 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sx_port->local_port, 0);
741 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
743 netdev_err(dev, "Failed to get proto");
746 mlxsw_reg_ptys_unpack(ptys_pl, ð_proto_cap,
747 ð_proto_admin, ð_proto_oper);
749 cmd->supported = mlxsw_sx_from_ptys_supported_port(eth_proto_cap) |
750 mlxsw_sx_from_ptys_supported_link(eth_proto_cap) |
751 SUPPORTED_Pause | SUPPORTED_Asym_Pause;
752 cmd->advertising = mlxsw_sx_from_ptys_advert_link(eth_proto_admin);
753 mlxsw_sx_from_ptys_speed_duplex(netif_carrier_ok(dev),
754 eth_proto_oper, cmd);
756 eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
757 cmd->port = mlxsw_sx_port_connector_port(eth_proto_oper);
758 cmd->lp_advertising = mlxsw_sx_from_ptys_advert_link(eth_proto_oper);
760 cmd->transceiver = XCVR_INTERNAL;
764 static u32 mlxsw_sx_to_ptys_advert_link(u32 advertising)
769 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
770 if (advertising & mlxsw_sx_port_link_mode[i].advertised)
771 ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
776 static u32 mlxsw_sx_to_ptys_speed(u32 speed)
781 for (i = 0; i < MLXSW_SX_PORT_LINK_MODE_LEN; i++) {
782 if (speed == mlxsw_sx_port_link_mode[i].speed)
783 ptys_proto |= mlxsw_sx_port_link_mode[i].mask;
788 static int mlxsw_sx_port_set_settings(struct net_device *dev,
789 struct ethtool_cmd *cmd)
791 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
792 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
793 char ptys_pl[MLXSW_REG_PTYS_LEN];
801 speed = ethtool_cmd_speed(cmd);
803 eth_proto_new = cmd->autoneg == AUTONEG_ENABLE ?
804 mlxsw_sx_to_ptys_advert_link(cmd->advertising) :
805 mlxsw_sx_to_ptys_speed(speed);
807 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sx_port->local_port, 0);
808 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
810 netdev_err(dev, "Failed to get proto");
813 mlxsw_reg_ptys_unpack(ptys_pl, ð_proto_cap, ð_proto_admin, NULL);
815 eth_proto_new = eth_proto_new & eth_proto_cap;
816 if (!eth_proto_new) {
817 netdev_err(dev, "Not supported proto admin requested");
820 if (eth_proto_new == eth_proto_admin)
823 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sx_port->local_port, eth_proto_new);
824 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
826 netdev_err(dev, "Failed to set proto admin");
830 err = mlxsw_sx_port_oper_status_get(mlxsw_sx_port, &is_up);
832 netdev_err(dev, "Failed to get oper status");
838 err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
840 netdev_err(dev, "Failed to set admin status");
844 err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, true);
846 netdev_err(dev, "Failed to set admin status");
853 static const struct ethtool_ops mlxsw_sx_port_ethtool_ops = {
854 .get_drvinfo = mlxsw_sx_port_get_drvinfo,
855 .get_link = ethtool_op_get_link,
856 .get_strings = mlxsw_sx_port_get_strings,
857 .get_ethtool_stats = mlxsw_sx_port_get_stats,
858 .get_sset_count = mlxsw_sx_port_get_sset_count,
859 .get_settings = mlxsw_sx_port_get_settings,
860 .set_settings = mlxsw_sx_port_set_settings,
863 static int mlxsw_sx_port_attr_get(struct net_device *dev,
864 struct switchdev_attr *attr)
866 struct mlxsw_sx_port *mlxsw_sx_port = netdev_priv(dev);
867 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
870 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
871 attr->u.ppid.id_len = sizeof(mlxsw_sx->hw_id);
872 memcpy(&attr->u.ppid.id, &mlxsw_sx->hw_id, attr->u.ppid.id_len);
881 static const struct switchdev_ops mlxsw_sx_port_switchdev_ops = {
882 .switchdev_port_attr_get = mlxsw_sx_port_attr_get,
885 static int mlxsw_sx_hw_id_get(struct mlxsw_sx *mlxsw_sx)
887 char spad_pl[MLXSW_REG_SPAD_LEN];
890 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(spad), spad_pl);
893 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sx->hw_id);
897 static int mlxsw_sx_port_dev_addr_get(struct mlxsw_sx_port *mlxsw_sx_port)
899 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
900 struct net_device *dev = mlxsw_sx_port->dev;
901 char ppad_pl[MLXSW_REG_PPAD_LEN];
904 mlxsw_reg_ppad_pack(ppad_pl, false, 0);
905 err = mlxsw_reg_query(mlxsw_sx->core, MLXSW_REG(ppad), ppad_pl);
908 mlxsw_reg_ppad_mac_memcpy_from(ppad_pl, dev->dev_addr);
909 /* The last byte value in base mac address is guaranteed
910 * to be such it does not overflow when adding local_port
913 dev->dev_addr[ETH_ALEN - 1] += mlxsw_sx_port->local_port;
917 static int mlxsw_sx_port_stp_state_set(struct mlxsw_sx_port *mlxsw_sx_port,
918 u16 vid, enum mlxsw_reg_spms_state state)
920 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
924 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
927 mlxsw_reg_spms_pack(spms_pl, mlxsw_sx_port->local_port);
928 mlxsw_reg_spms_vid_pack(spms_pl, vid, state);
929 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(spms), spms_pl);
934 static int mlxsw_sx_port_speed_set(struct mlxsw_sx_port *mlxsw_sx_port,
937 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
938 char ptys_pl[MLXSW_REG_PTYS_LEN];
940 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sx_port->local_port, speed);
941 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(ptys), ptys_pl);
945 mlxsw_sx_port_mac_learning_mode_set(struct mlxsw_sx_port *mlxsw_sx_port,
946 enum mlxsw_reg_spmlr_learn_mode mode)
948 struct mlxsw_sx *mlxsw_sx = mlxsw_sx_port->mlxsw_sx;
949 char spmlr_pl[MLXSW_REG_SPMLR_LEN];
951 mlxsw_reg_spmlr_pack(spmlr_pl, mlxsw_sx_port->local_port, mode);
952 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(spmlr), spmlr_pl);
955 static int mlxsw_sx_port_create(struct mlxsw_sx *mlxsw_sx, u8 local_port)
957 struct mlxsw_sx_port *mlxsw_sx_port;
958 struct net_device *dev;
962 dev = alloc_etherdev(sizeof(struct mlxsw_sx_port));
965 mlxsw_sx_port = netdev_priv(dev);
966 mlxsw_sx_port->dev = dev;
967 mlxsw_sx_port->mlxsw_sx = mlxsw_sx;
968 mlxsw_sx_port->local_port = local_port;
970 mlxsw_sx_port->pcpu_stats =
971 netdev_alloc_pcpu_stats(struct mlxsw_sx_port_pcpu_stats);
972 if (!mlxsw_sx_port->pcpu_stats) {
974 goto err_alloc_stats;
977 dev->netdev_ops = &mlxsw_sx_port_netdev_ops;
978 dev->ethtool_ops = &mlxsw_sx_port_ethtool_ops;
979 dev->switchdev_ops = &mlxsw_sx_port_switchdev_ops;
981 err = mlxsw_sx_port_dev_addr_get(mlxsw_sx_port);
983 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Unable to get port mac address\n",
984 mlxsw_sx_port->local_port);
985 goto err_dev_addr_get;
988 netif_carrier_off(dev);
990 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
991 NETIF_F_VLAN_CHALLENGED;
993 /* Each packet needs to have a Tx header (metadata) on top all other
996 dev->needed_headroom = MLXSW_TXHDR_LEN;
998 err = mlxsw_sx_port_module_check(mlxsw_sx_port, &usable);
1000 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to check module\n",
1001 mlxsw_sx_port->local_port);
1002 goto err_port_module_check;
1006 dev_dbg(mlxsw_sx->bus_info->dev, "Port %d: Not usable, skipping initialization\n",
1007 mlxsw_sx_port->local_port);
1008 goto port_not_usable;
1011 err = mlxsw_sx_port_system_port_mapping_set(mlxsw_sx_port);
1013 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set system port mapping\n",
1014 mlxsw_sx_port->local_port);
1015 goto err_port_system_port_mapping_set;
1018 err = mlxsw_sx_port_swid_set(mlxsw_sx_port, 0);
1020 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set SWID\n",
1021 mlxsw_sx_port->local_port);
1022 goto err_port_swid_set;
1025 err = mlxsw_sx_port_speed_set(mlxsw_sx_port,
1026 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4);
1028 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set speed\n",
1029 mlxsw_sx_port->local_port);
1030 goto err_port_speed_set;
1033 err = mlxsw_sx_port_mtu_set(mlxsw_sx_port, ETH_DATA_LEN);
1035 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MTU\n",
1036 mlxsw_sx_port->local_port);
1037 goto err_port_mtu_set;
1040 err = mlxsw_sx_port_admin_status_set(mlxsw_sx_port, false);
1042 goto err_port_admin_status_set;
1044 err = mlxsw_sx_port_stp_state_set(mlxsw_sx_port,
1045 MLXSW_PORT_DEFAULT_VID,
1046 MLXSW_REG_SPMS_STATE_FORWARDING);
1048 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set STP state\n",
1049 mlxsw_sx_port->local_port);
1050 goto err_port_stp_state_set;
1053 err = mlxsw_sx_port_mac_learning_mode_set(mlxsw_sx_port,
1054 MLXSW_REG_SPMLR_LEARN_MODE_DISABLE);
1056 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to set MAC learning mode\n",
1057 mlxsw_sx_port->local_port);
1058 goto err_port_mac_learning_mode_set;
1061 err = register_netdev(dev);
1063 dev_err(mlxsw_sx->bus_info->dev, "Port %d: Failed to register netdev\n",
1064 mlxsw_sx_port->local_port);
1065 goto err_register_netdev;
1068 mlxsw_sx->ports[local_port] = mlxsw_sx_port;
1071 err_register_netdev:
1072 err_port_mac_learning_mode_set:
1073 err_port_stp_state_set:
1074 err_port_admin_status_set:
1077 mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
1079 err_port_system_port_mapping_set:
1081 err_port_module_check:
1083 free_percpu(mlxsw_sx_port->pcpu_stats);
1089 static void mlxsw_sx_port_remove(struct mlxsw_sx *mlxsw_sx, u8 local_port)
1091 struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
1095 unregister_netdev(mlxsw_sx_port->dev); /* This calls ndo_stop */
1096 mlxsw_sx_port_swid_set(mlxsw_sx_port, MLXSW_PORT_SWID_DISABLED_PORT);
1097 free_percpu(mlxsw_sx_port->pcpu_stats);
1098 free_netdev(mlxsw_sx_port->dev);
1101 static void mlxsw_sx_ports_remove(struct mlxsw_sx *mlxsw_sx)
1105 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
1106 mlxsw_sx_port_remove(mlxsw_sx, i);
1107 kfree(mlxsw_sx->ports);
1110 static int mlxsw_sx_ports_create(struct mlxsw_sx *mlxsw_sx)
1116 alloc_size = sizeof(struct mlxsw_sx_port *) * MLXSW_PORT_MAX_PORTS;
1117 mlxsw_sx->ports = kzalloc(alloc_size, GFP_KERNEL);
1118 if (!mlxsw_sx->ports)
1121 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) {
1122 err = mlxsw_sx_port_create(mlxsw_sx, i);
1124 goto err_port_create;
1129 for (i--; i >= 1; i--)
1130 mlxsw_sx_port_remove(mlxsw_sx, i);
1131 kfree(mlxsw_sx->ports);
1135 static void mlxsw_sx_pude_event_func(const struct mlxsw_reg_info *reg,
1136 char *pude_pl, void *priv)
1138 struct mlxsw_sx *mlxsw_sx = priv;
1139 struct mlxsw_sx_port *mlxsw_sx_port;
1140 enum mlxsw_reg_pude_oper_status status;
1143 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
1144 mlxsw_sx_port = mlxsw_sx->ports[local_port];
1145 if (!mlxsw_sx_port) {
1146 dev_warn(mlxsw_sx->bus_info->dev, "Port %d: Link event received for non-existent port\n",
1151 status = mlxsw_reg_pude_oper_status_get(pude_pl);
1152 if (status == MLXSW_PORT_OPER_STATUS_UP) {
1153 netdev_info(mlxsw_sx_port->dev, "link up\n");
1154 netif_carrier_on(mlxsw_sx_port->dev);
1156 netdev_info(mlxsw_sx_port->dev, "link down\n");
1157 netif_carrier_off(mlxsw_sx_port->dev);
1161 static struct mlxsw_event_listener mlxsw_sx_pude_event = {
1162 .func = mlxsw_sx_pude_event_func,
1163 .trap_id = MLXSW_TRAP_ID_PUDE,
1166 static int mlxsw_sx_event_register(struct mlxsw_sx *mlxsw_sx,
1167 enum mlxsw_event_trap_id trap_id)
1169 struct mlxsw_event_listener *el;
1170 char hpkt_pl[MLXSW_REG_HPKT_LEN];
1174 case MLXSW_TRAP_ID_PUDE:
1175 el = &mlxsw_sx_pude_event;
1178 err = mlxsw_core_event_listener_register(mlxsw_sx->core, el, mlxsw_sx);
1182 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD, trap_id);
1183 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(hpkt), hpkt_pl);
1185 goto err_event_trap_set;
1190 mlxsw_core_event_listener_unregister(mlxsw_sx->core, el, mlxsw_sx);
1194 static void mlxsw_sx_event_unregister(struct mlxsw_sx *mlxsw_sx,
1195 enum mlxsw_event_trap_id trap_id)
1197 struct mlxsw_event_listener *el;
1200 case MLXSW_TRAP_ID_PUDE:
1201 el = &mlxsw_sx_pude_event;
1204 mlxsw_core_event_listener_unregister(mlxsw_sx->core, el, mlxsw_sx);
1207 static void mlxsw_sx_rx_listener_func(struct sk_buff *skb, u8 local_port,
1210 struct mlxsw_sx *mlxsw_sx = priv;
1211 struct mlxsw_sx_port *mlxsw_sx_port = mlxsw_sx->ports[local_port];
1212 struct mlxsw_sx_port_pcpu_stats *pcpu_stats;
1214 if (unlikely(!mlxsw_sx_port)) {
1215 dev_warn_ratelimited(mlxsw_sx->bus_info->dev, "Port %d: skb received for non-existent port\n",
1220 skb->dev = mlxsw_sx_port->dev;
1222 pcpu_stats = this_cpu_ptr(mlxsw_sx_port->pcpu_stats);
1223 u64_stats_update_begin(&pcpu_stats->syncp);
1224 pcpu_stats->rx_packets++;
1225 pcpu_stats->rx_bytes += skb->len;
1226 u64_stats_update_end(&pcpu_stats->syncp);
1228 skb->protocol = eth_type_trans(skb, skb->dev);
1229 netif_receive_skb(skb);
1232 static const struct mlxsw_rx_listener mlxsw_sx_rx_listener[] = {
1234 .func = mlxsw_sx_rx_listener_func,
1235 .local_port = MLXSW_PORT_DONT_CARE,
1236 .trap_id = MLXSW_TRAP_ID_FDB_MC,
1238 /* Traps for specific L2 packet types, not trapped as FDB MC */
1240 .func = mlxsw_sx_rx_listener_func,
1241 .local_port = MLXSW_PORT_DONT_CARE,
1242 .trap_id = MLXSW_TRAP_ID_STP,
1245 .func = mlxsw_sx_rx_listener_func,
1246 .local_port = MLXSW_PORT_DONT_CARE,
1247 .trap_id = MLXSW_TRAP_ID_LACP,
1250 .func = mlxsw_sx_rx_listener_func,
1251 .local_port = MLXSW_PORT_DONT_CARE,
1252 .trap_id = MLXSW_TRAP_ID_EAPOL,
1255 .func = mlxsw_sx_rx_listener_func,
1256 .local_port = MLXSW_PORT_DONT_CARE,
1257 .trap_id = MLXSW_TRAP_ID_LLDP,
1260 .func = mlxsw_sx_rx_listener_func,
1261 .local_port = MLXSW_PORT_DONT_CARE,
1262 .trap_id = MLXSW_TRAP_ID_MMRP,
1265 .func = mlxsw_sx_rx_listener_func,
1266 .local_port = MLXSW_PORT_DONT_CARE,
1267 .trap_id = MLXSW_TRAP_ID_MVRP,
1270 .func = mlxsw_sx_rx_listener_func,
1271 .local_port = MLXSW_PORT_DONT_CARE,
1272 .trap_id = MLXSW_TRAP_ID_RPVST,
1275 .func = mlxsw_sx_rx_listener_func,
1276 .local_port = MLXSW_PORT_DONT_CARE,
1277 .trap_id = MLXSW_TRAP_ID_DHCP,
1280 .func = mlxsw_sx_rx_listener_func,
1281 .local_port = MLXSW_PORT_DONT_CARE,
1282 .trap_id = MLXSW_TRAP_ID_IGMP_QUERY,
1285 .func = mlxsw_sx_rx_listener_func,
1286 .local_port = MLXSW_PORT_DONT_CARE,
1287 .trap_id = MLXSW_TRAP_ID_IGMP_V1_REPORT,
1290 .func = mlxsw_sx_rx_listener_func,
1291 .local_port = MLXSW_PORT_DONT_CARE,
1292 .trap_id = MLXSW_TRAP_ID_IGMP_V2_REPORT,
1295 .func = mlxsw_sx_rx_listener_func,
1296 .local_port = MLXSW_PORT_DONT_CARE,
1297 .trap_id = MLXSW_TRAP_ID_IGMP_V2_LEAVE,
1300 .func = mlxsw_sx_rx_listener_func,
1301 .local_port = MLXSW_PORT_DONT_CARE,
1302 .trap_id = MLXSW_TRAP_ID_IGMP_V3_REPORT,
1306 static int mlxsw_sx_traps_init(struct mlxsw_sx *mlxsw_sx)
1308 char htgt_pl[MLXSW_REG_HTGT_LEN];
1309 char hpkt_pl[MLXSW_REG_HPKT_LEN];
1313 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_RX);
1314 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(htgt), htgt_pl);
1318 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_CTRL);
1319 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(htgt), htgt_pl);
1323 for (i = 0; i < ARRAY_SIZE(mlxsw_sx_rx_listener); i++) {
1324 err = mlxsw_core_rx_listener_register(mlxsw_sx->core,
1325 &mlxsw_sx_rx_listener[i],
1328 goto err_rx_listener_register;
1330 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_TRAP_TO_CPU,
1331 mlxsw_sx_rx_listener[i].trap_id);
1332 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(hpkt), hpkt_pl);
1334 goto err_rx_trap_set;
1339 mlxsw_core_rx_listener_unregister(mlxsw_sx->core,
1340 &mlxsw_sx_rx_listener[i],
1342 err_rx_listener_register:
1343 for (i--; i >= 0; i--) {
1344 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD,
1345 mlxsw_sx_rx_listener[i].trap_id);
1346 mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(hpkt), hpkt_pl);
1348 mlxsw_core_rx_listener_unregister(mlxsw_sx->core,
1349 &mlxsw_sx_rx_listener[i],
1355 static void mlxsw_sx_traps_fini(struct mlxsw_sx *mlxsw_sx)
1357 char hpkt_pl[MLXSW_REG_HPKT_LEN];
1360 for (i = 0; i < ARRAY_SIZE(mlxsw_sx_rx_listener); i++) {
1361 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD,
1362 mlxsw_sx_rx_listener[i].trap_id);
1363 mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(hpkt), hpkt_pl);
1365 mlxsw_core_rx_listener_unregister(mlxsw_sx->core,
1366 &mlxsw_sx_rx_listener[i],
1371 static int mlxsw_sx_flood_init(struct mlxsw_sx *mlxsw_sx)
1373 char sfgc_pl[MLXSW_REG_SFGC_LEN];
1374 char sgcr_pl[MLXSW_REG_SGCR_LEN];
1378 /* Configure a flooding table, which includes only CPU port. */
1379 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
1382 mlxsw_reg_sftr_pack(sftr_pl, 0, 0, MLXSW_REG_SFGC_TABLE_TYPE_SINGLE, 0,
1383 MLXSW_PORT_CPU_PORT, true);
1384 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sftr), sftr_pl);
1389 /* Flood different packet types using the flooding table. */
1390 mlxsw_reg_sfgc_pack(sfgc_pl,
1391 MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST,
1392 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1393 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1395 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1399 mlxsw_reg_sfgc_pack(sfgc_pl,
1400 MLXSW_REG_SFGC_TYPE_BROADCAST,
1401 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1402 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1404 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1408 mlxsw_reg_sfgc_pack(sfgc_pl,
1409 MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_NON_IP,
1410 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1411 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1413 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1417 mlxsw_reg_sfgc_pack(sfgc_pl,
1418 MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_IPV6,
1419 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1420 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1422 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1426 mlxsw_reg_sfgc_pack(sfgc_pl,
1427 MLXSW_REG_SFGC_TYPE_UNREGISTERED_MULTICAST_IPV4,
1428 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID,
1429 MLXSW_REG_SFGC_TABLE_TYPE_SINGLE,
1431 err = mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sfgc), sfgc_pl);
1435 mlxsw_reg_sgcr_pack(sgcr_pl, true);
1436 return mlxsw_reg_write(mlxsw_sx->core, MLXSW_REG(sgcr), sgcr_pl);
1439 static int mlxsw_sx_init(void *priv, struct mlxsw_core *mlxsw_core,
1440 const struct mlxsw_bus_info *mlxsw_bus_info)
1442 struct mlxsw_sx *mlxsw_sx = priv;
1445 mlxsw_sx->core = mlxsw_core;
1446 mlxsw_sx->bus_info = mlxsw_bus_info;
1448 err = mlxsw_sx_hw_id_get(mlxsw_sx);
1450 dev_err(mlxsw_sx->bus_info->dev, "Failed to get switch HW ID\n");
1454 err = mlxsw_sx_ports_create(mlxsw_sx);
1456 dev_err(mlxsw_sx->bus_info->dev, "Failed to create ports\n");
1460 err = mlxsw_sx_event_register(mlxsw_sx, MLXSW_TRAP_ID_PUDE);
1462 dev_err(mlxsw_sx->bus_info->dev, "Failed to register for PUDE events\n");
1463 goto err_event_register;
1466 err = mlxsw_sx_traps_init(mlxsw_sx);
1468 dev_err(mlxsw_sx->bus_info->dev, "Failed to set traps for RX\n");
1469 goto err_rx_listener_register;
1472 err = mlxsw_sx_flood_init(mlxsw_sx);
1474 dev_err(mlxsw_sx->bus_info->dev, "Failed to initialize flood tables\n");
1475 goto err_flood_init;
1481 mlxsw_sx_traps_fini(mlxsw_sx);
1482 err_rx_listener_register:
1483 mlxsw_sx_event_unregister(mlxsw_sx, MLXSW_TRAP_ID_PUDE);
1485 mlxsw_sx_ports_remove(mlxsw_sx);
1489 static void mlxsw_sx_fini(void *priv)
1491 struct mlxsw_sx *mlxsw_sx = priv;
1493 mlxsw_sx_traps_fini(mlxsw_sx);
1494 mlxsw_sx_event_unregister(mlxsw_sx, MLXSW_TRAP_ID_PUDE);
1495 mlxsw_sx_ports_remove(mlxsw_sx);
1498 static struct mlxsw_config_profile mlxsw_sx_config_profile = {
1499 .used_max_vepa_channels = 1,
1500 .max_vepa_channels = 0,
1503 .used_max_port_per_lag = 1,
1504 .max_port_per_lag = 16,
1509 .used_max_system_port = 1,
1510 .max_system_port = 48000,
1511 .used_max_vlan_groups = 1,
1512 .max_vlan_groups = 127,
1513 .used_max_regions = 1,
1515 .used_flood_tables = 1,
1516 .max_flood_tables = 2,
1517 .max_vid_flood_tables = 1,
1518 .used_flood_mode = 1,
1520 .used_max_ib_mc = 1,
1527 .type = MLXSW_PORT_SWID_TYPE_ETH,
1532 static struct mlxsw_driver mlxsw_sx_driver = {
1533 .kind = MLXSW_DEVICE_KIND_SWITCHX2,
1534 .owner = THIS_MODULE,
1535 .priv_size = sizeof(struct mlxsw_sx),
1536 .init = mlxsw_sx_init,
1537 .fini = mlxsw_sx_fini,
1538 .txhdr_construct = mlxsw_sx_txhdr_construct,
1539 .txhdr_len = MLXSW_TXHDR_LEN,
1540 .profile = &mlxsw_sx_config_profile,
1543 static int __init mlxsw_sx_module_init(void)
1545 return mlxsw_core_driver_register(&mlxsw_sx_driver);
1548 static void __exit mlxsw_sx_module_exit(void)
1550 mlxsw_core_driver_unregister(&mlxsw_sx_driver);
1553 module_init(mlxsw_sx_module_init);
1554 module_exit(mlxsw_sx_module_exit);
1556 MODULE_LICENSE("Dual BSD/GPL");
1557 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
1558 MODULE_DESCRIPTION("Mellanox SwitchX-2 driver");
1559 MODULE_MLXSW_DRIVER_ALIAS(MLXSW_DEVICE_KIND_SWITCHX2);