2 * drivers/net/ethernet/mellanox/mlxsw/spectrum.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/ethtool.h>
43 #include <linux/slab.h>
44 #include <linux/device.h>
45 #include <linux/skbuff.h>
46 #include <linux/if_vlan.h>
47 #include <linux/if_bridge.h>
48 #include <linux/workqueue.h>
49 #include <linux/jiffies.h>
50 #include <linux/bitops.h>
51 #include <linux/list.h>
52 #include <linux/notifier.h>
53 #include <linux/dcbnl.h>
54 #include <linux/inetdevice.h>
55 #include <net/switchdev.h>
56 #include <generated/utsrelease.h>
57 #include <net/pkt_cls.h>
58 #include <net/tc_act/tc_mirred.h>
67 static const char mlxsw_sp_driver_name[] = "mlxsw_spectrum";
68 static const char mlxsw_sp_driver_version[] = "1.0";
74 MLXSW_ITEM32(tx, hdr, version, 0x00, 28, 4);
77 * Packet control type.
78 * 0 - Ethernet control (e.g. EMADs, LACP)
81 MLXSW_ITEM32(tx, hdr, ctl, 0x00, 26, 2);
84 * Packet protocol type. Must be set to 1 (Ethernet).
86 MLXSW_ITEM32(tx, hdr, proto, 0x00, 21, 3);
88 /* tx_hdr_rx_is_router
89 * Packet is sent from the router. Valid for data packets only.
91 MLXSW_ITEM32(tx, hdr, rx_is_router, 0x00, 19, 1);
94 * Indicates if the 'fid' field is valid and should be used for
95 * forwarding lookup. Valid for data packets only.
97 MLXSW_ITEM32(tx, hdr, fid_valid, 0x00, 16, 1);
100 * Switch partition ID. Must be set to 0.
102 MLXSW_ITEM32(tx, hdr, swid, 0x00, 12, 3);
104 /* tx_hdr_control_tclass
105 * Indicates if the packet should use the control TClass and not one
106 * of the data TClasses.
108 MLXSW_ITEM32(tx, hdr, control_tclass, 0x00, 6, 1);
111 * Egress TClass to be used on the egress device on the egress port.
113 MLXSW_ITEM32(tx, hdr, etclass, 0x00, 0, 4);
116 * Destination local port for unicast packets.
117 * Destination multicast ID for multicast packets.
119 * Control packets are directed to a specific egress port, while data
120 * packets are transmitted through the CPU port (0) into the switch partition,
121 * where forwarding rules are applied.
123 MLXSW_ITEM32(tx, hdr, port_mid, 0x04, 16, 16);
126 * Forwarding ID used for L2 forwarding lookup. Valid only if 'fid_valid' is
127 * set, otherwise calculated based on the packet's VID using VID to FID mapping.
128 * Valid for data packets only.
130 MLXSW_ITEM32(tx, hdr, fid, 0x08, 0, 16);
134 * 6 - Control packets
136 MLXSW_ITEM32(tx, hdr, type, 0x0C, 0, 4);
138 static bool mlxsw_sp_port_dev_check(const struct net_device *dev);
140 static void mlxsw_sp_txhdr_construct(struct sk_buff *skb,
141 const struct mlxsw_tx_info *tx_info)
143 char *txhdr = skb_push(skb, MLXSW_TXHDR_LEN);
145 memset(txhdr, 0, MLXSW_TXHDR_LEN);
147 mlxsw_tx_hdr_version_set(txhdr, MLXSW_TXHDR_VERSION_1);
148 mlxsw_tx_hdr_ctl_set(txhdr, MLXSW_TXHDR_ETH_CTL);
149 mlxsw_tx_hdr_proto_set(txhdr, MLXSW_TXHDR_PROTO_ETH);
150 mlxsw_tx_hdr_swid_set(txhdr, 0);
151 mlxsw_tx_hdr_control_tclass_set(txhdr, 1);
152 mlxsw_tx_hdr_port_mid_set(txhdr, tx_info->local_port);
153 mlxsw_tx_hdr_type_set(txhdr, MLXSW_TXHDR_TYPE_CONTROL);
156 static int mlxsw_sp_base_mac_get(struct mlxsw_sp *mlxsw_sp)
158 char spad_pl[MLXSW_REG_SPAD_LEN];
161 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(spad), spad_pl);
164 mlxsw_reg_spad_base_mac_memcpy_from(spad_pl, mlxsw_sp->base_mac);
168 static int mlxsw_sp_span_init(struct mlxsw_sp *mlxsw_sp)
170 struct mlxsw_resources *resources;
173 resources = mlxsw_core_resources_get(mlxsw_sp->core);
174 if (!resources->max_span_valid)
177 mlxsw_sp->span.entries_count = resources->max_span;
178 mlxsw_sp->span.entries = kcalloc(mlxsw_sp->span.entries_count,
179 sizeof(struct mlxsw_sp_span_entry),
181 if (!mlxsw_sp->span.entries)
184 for (i = 0; i < mlxsw_sp->span.entries_count; i++)
185 INIT_LIST_HEAD(&mlxsw_sp->span.entries[i].bound_ports_list);
190 static void mlxsw_sp_span_fini(struct mlxsw_sp *mlxsw_sp)
194 for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
195 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i];
197 WARN_ON_ONCE(!list_empty(&curr->bound_ports_list));
199 kfree(mlxsw_sp->span.entries);
202 static struct mlxsw_sp_span_entry *
203 mlxsw_sp_span_entry_create(struct mlxsw_sp_port *port)
205 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
206 struct mlxsw_sp_span_entry *span_entry;
207 char mpat_pl[MLXSW_REG_MPAT_LEN];
208 u8 local_port = port->local_port;
213 /* find a free entry to use */
215 for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
216 if (!mlxsw_sp->span.entries[i].used) {
218 span_entry = &mlxsw_sp->span.entries[i];
225 /* create a new port analayzer entry for local_port */
226 mlxsw_reg_mpat_pack(mpat_pl, index, local_port, true);
227 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
231 span_entry->used = true;
232 span_entry->id = index;
233 span_entry->ref_count = 0;
234 span_entry->local_port = local_port;
238 static void mlxsw_sp_span_entry_destroy(struct mlxsw_sp *mlxsw_sp,
239 struct mlxsw_sp_span_entry *span_entry)
241 u8 local_port = span_entry->local_port;
242 char mpat_pl[MLXSW_REG_MPAT_LEN];
243 int pa_id = span_entry->id;
245 mlxsw_reg_mpat_pack(mpat_pl, pa_id, local_port, false);
246 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpat), mpat_pl);
247 span_entry->used = false;
250 struct mlxsw_sp_span_entry *mlxsw_sp_span_entry_find(struct mlxsw_sp_port *port)
252 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
255 for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
256 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i];
258 if (curr->used && curr->local_port == port->local_port)
264 struct mlxsw_sp_span_entry *mlxsw_sp_span_entry_get(struct mlxsw_sp_port *port)
266 struct mlxsw_sp_span_entry *span_entry;
268 span_entry = mlxsw_sp_span_entry_find(port);
270 span_entry->ref_count++;
274 return mlxsw_sp_span_entry_create(port);
277 static int mlxsw_sp_span_entry_put(struct mlxsw_sp *mlxsw_sp,
278 struct mlxsw_sp_span_entry *span_entry)
280 if (--span_entry->ref_count == 0)
281 mlxsw_sp_span_entry_destroy(mlxsw_sp, span_entry);
285 static bool mlxsw_sp_span_is_egress_mirror(struct mlxsw_sp_port *port)
287 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
288 struct mlxsw_sp_span_inspected_port *p;
291 for (i = 0; i < mlxsw_sp->span.entries_count; i++) {
292 struct mlxsw_sp_span_entry *curr = &mlxsw_sp->span.entries[i];
294 list_for_each_entry(p, &curr->bound_ports_list, list)
295 if (p->local_port == port->local_port &&
296 p->type == MLXSW_SP_SPAN_EGRESS)
303 static int mlxsw_sp_span_mtu_to_buffsize(int mtu)
305 return MLXSW_SP_BYTES_TO_CELLS(mtu * 5 / 2) + 1;
308 static int mlxsw_sp_span_port_mtu_update(struct mlxsw_sp_port *port, u16 mtu)
310 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
311 char sbib_pl[MLXSW_REG_SBIB_LEN];
314 /* If port is egress mirrored, the shared buffer size should be
315 * updated according to the mtu value
317 if (mlxsw_sp_span_is_egress_mirror(port)) {
318 mlxsw_reg_sbib_pack(sbib_pl, port->local_port,
319 mlxsw_sp_span_mtu_to_buffsize(mtu));
320 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
322 netdev_err(port->dev, "Could not update shared buffer for mirroring\n");
330 static struct mlxsw_sp_span_inspected_port *
331 mlxsw_sp_span_entry_bound_port_find(struct mlxsw_sp_port *port,
332 struct mlxsw_sp_span_entry *span_entry)
334 struct mlxsw_sp_span_inspected_port *p;
336 list_for_each_entry(p, &span_entry->bound_ports_list, list)
337 if (port->local_port == p->local_port)
343 mlxsw_sp_span_inspected_port_bind(struct mlxsw_sp_port *port,
344 struct mlxsw_sp_span_entry *span_entry,
345 enum mlxsw_sp_span_type type)
347 struct mlxsw_sp_span_inspected_port *inspected_port;
348 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
349 char mpar_pl[MLXSW_REG_MPAR_LEN];
350 char sbib_pl[MLXSW_REG_SBIB_LEN];
351 int pa_id = span_entry->id;
354 /* if it is an egress SPAN, bind a shared buffer to it */
355 if (type == MLXSW_SP_SPAN_EGRESS) {
356 mlxsw_reg_sbib_pack(sbib_pl, port->local_port,
357 mlxsw_sp_span_mtu_to_buffsize(port->dev->mtu));
358 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
360 netdev_err(port->dev, "Could not create shared buffer for mirroring\n");
365 /* bind the port to the SPAN entry */
366 mlxsw_reg_mpar_pack(mpar_pl, port->local_port, type, true, pa_id);
367 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpar), mpar_pl);
369 goto err_mpar_reg_write;
371 inspected_port = kzalloc(sizeof(*inspected_port), GFP_KERNEL);
372 if (!inspected_port) {
374 goto err_inspected_port_alloc;
376 inspected_port->local_port = port->local_port;
377 inspected_port->type = type;
378 list_add_tail(&inspected_port->list, &span_entry->bound_ports_list);
383 err_inspected_port_alloc:
384 if (type == MLXSW_SP_SPAN_EGRESS) {
385 mlxsw_reg_sbib_pack(sbib_pl, port->local_port, 0);
386 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
392 mlxsw_sp_span_inspected_port_unbind(struct mlxsw_sp_port *port,
393 struct mlxsw_sp_span_entry *span_entry,
394 enum mlxsw_sp_span_type type)
396 struct mlxsw_sp_span_inspected_port *inspected_port;
397 struct mlxsw_sp *mlxsw_sp = port->mlxsw_sp;
398 char mpar_pl[MLXSW_REG_MPAR_LEN];
399 char sbib_pl[MLXSW_REG_SBIB_LEN];
400 int pa_id = span_entry->id;
402 inspected_port = mlxsw_sp_span_entry_bound_port_find(port, span_entry);
406 /* remove the inspected port */
407 mlxsw_reg_mpar_pack(mpar_pl, port->local_port, type, false, pa_id);
408 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mpar), mpar_pl);
410 /* remove the SBIB buffer if it was egress SPAN */
411 if (type == MLXSW_SP_SPAN_EGRESS) {
412 mlxsw_reg_sbib_pack(sbib_pl, port->local_port, 0);
413 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sbib), sbib_pl);
416 mlxsw_sp_span_entry_put(mlxsw_sp, span_entry);
418 list_del(&inspected_port->list);
419 kfree(inspected_port);
422 static int mlxsw_sp_span_mirror_add(struct mlxsw_sp_port *from,
423 struct mlxsw_sp_port *to,
424 enum mlxsw_sp_span_type type)
426 struct mlxsw_sp *mlxsw_sp = from->mlxsw_sp;
427 struct mlxsw_sp_span_entry *span_entry;
430 span_entry = mlxsw_sp_span_entry_get(to);
434 netdev_dbg(from->dev, "Adding inspected port to SPAN entry %d\n",
437 err = mlxsw_sp_span_inspected_port_bind(from, span_entry, type);
444 mlxsw_sp_span_entry_put(mlxsw_sp, span_entry);
448 static void mlxsw_sp_span_mirror_remove(struct mlxsw_sp_port *from,
449 struct mlxsw_sp_port *to,
450 enum mlxsw_sp_span_type type)
452 struct mlxsw_sp_span_entry *span_entry;
454 span_entry = mlxsw_sp_span_entry_find(to);
456 netdev_err(from->dev, "no span entry found\n");
460 netdev_dbg(from->dev, "removing inspected port from SPAN entry %d\n",
462 mlxsw_sp_span_inspected_port_unbind(from, span_entry, type);
465 static int mlxsw_sp_port_admin_status_set(struct mlxsw_sp_port *mlxsw_sp_port,
468 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
469 char paos_pl[MLXSW_REG_PAOS_LEN];
471 mlxsw_reg_paos_pack(paos_pl, mlxsw_sp_port->local_port,
472 is_up ? MLXSW_PORT_ADMIN_STATUS_UP :
473 MLXSW_PORT_ADMIN_STATUS_DOWN);
474 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(paos), paos_pl);
477 static int mlxsw_sp_port_dev_addr_set(struct mlxsw_sp_port *mlxsw_sp_port,
480 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
481 char ppad_pl[MLXSW_REG_PPAD_LEN];
483 mlxsw_reg_ppad_pack(ppad_pl, true, mlxsw_sp_port->local_port);
484 mlxsw_reg_ppad_mac_memcpy_to(ppad_pl, addr);
485 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ppad), ppad_pl);
488 static int mlxsw_sp_port_dev_addr_init(struct mlxsw_sp_port *mlxsw_sp_port)
490 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
491 unsigned char *addr = mlxsw_sp_port->dev->dev_addr;
493 ether_addr_copy(addr, mlxsw_sp->base_mac);
494 addr[ETH_ALEN - 1] += mlxsw_sp_port->local_port;
495 return mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr);
498 static int mlxsw_sp_port_mtu_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mtu)
500 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
501 char pmtu_pl[MLXSW_REG_PMTU_LEN];
505 mtu += MLXSW_TXHDR_LEN + ETH_HLEN;
506 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, 0);
507 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
510 max_mtu = mlxsw_reg_pmtu_max_mtu_get(pmtu_pl);
515 mlxsw_reg_pmtu_pack(pmtu_pl, mlxsw_sp_port->local_port, mtu);
516 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmtu), pmtu_pl);
519 static int __mlxsw_sp_port_swid_set(struct mlxsw_sp *mlxsw_sp, u8 local_port,
522 char pspa_pl[MLXSW_REG_PSPA_LEN];
524 mlxsw_reg_pspa_pack(pspa_pl, swid, local_port);
525 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pspa), pspa_pl);
528 static int mlxsw_sp_port_swid_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 swid)
530 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
532 return __mlxsw_sp_port_swid_set(mlxsw_sp, mlxsw_sp_port->local_port,
536 static int mlxsw_sp_port_vp_mode_set(struct mlxsw_sp_port *mlxsw_sp_port,
539 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
540 char svpe_pl[MLXSW_REG_SVPE_LEN];
542 mlxsw_reg_svpe_pack(svpe_pl, mlxsw_sp_port->local_port, enable);
543 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svpe), svpe_pl);
546 int mlxsw_sp_port_vid_to_fid_set(struct mlxsw_sp_port *mlxsw_sp_port,
547 enum mlxsw_reg_svfa_mt mt, bool valid, u16 fid,
550 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
551 char svfa_pl[MLXSW_REG_SVFA_LEN];
553 mlxsw_reg_svfa_pack(svfa_pl, mlxsw_sp_port->local_port, mt, valid,
555 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl);
558 static int mlxsw_sp_port_vid_learning_set(struct mlxsw_sp_port *mlxsw_sp_port,
559 u16 vid, bool learn_enable)
561 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
565 spvmlr_pl = kmalloc(MLXSW_REG_SPVMLR_LEN, GFP_KERNEL);
568 mlxsw_reg_spvmlr_pack(spvmlr_pl, mlxsw_sp_port->local_port, vid, vid,
570 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvmlr), spvmlr_pl);
576 mlxsw_sp_port_system_port_mapping_set(struct mlxsw_sp_port *mlxsw_sp_port)
578 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
579 char sspr_pl[MLXSW_REG_SSPR_LEN];
581 mlxsw_reg_sspr_pack(sspr_pl, mlxsw_sp_port->local_port);
582 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sspr), sspr_pl);
585 static int mlxsw_sp_port_module_info_get(struct mlxsw_sp *mlxsw_sp,
586 u8 local_port, u8 *p_module,
587 u8 *p_width, u8 *p_lane)
589 char pmlp_pl[MLXSW_REG_PMLP_LEN];
592 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
593 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
596 *p_module = mlxsw_reg_pmlp_module_get(pmlp_pl, 0);
597 *p_width = mlxsw_reg_pmlp_width_get(pmlp_pl);
598 *p_lane = mlxsw_reg_pmlp_tx_lane_get(pmlp_pl, 0);
602 static int mlxsw_sp_port_module_map(struct mlxsw_sp *mlxsw_sp, u8 local_port,
603 u8 module, u8 width, u8 lane)
605 char pmlp_pl[MLXSW_REG_PMLP_LEN];
608 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
609 mlxsw_reg_pmlp_width_set(pmlp_pl, width);
610 for (i = 0; i < width; i++) {
611 mlxsw_reg_pmlp_module_set(pmlp_pl, i, module);
612 mlxsw_reg_pmlp_tx_lane_set(pmlp_pl, i, lane + i); /* Rx & Tx */
615 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
618 static int mlxsw_sp_port_module_unmap(struct mlxsw_sp *mlxsw_sp, u8 local_port)
620 char pmlp_pl[MLXSW_REG_PMLP_LEN];
622 mlxsw_reg_pmlp_pack(pmlp_pl, local_port);
623 mlxsw_reg_pmlp_width_set(pmlp_pl, 0);
624 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pmlp), pmlp_pl);
627 static int mlxsw_sp_port_open(struct net_device *dev)
629 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
632 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
635 netif_start_queue(dev);
639 static int mlxsw_sp_port_stop(struct net_device *dev)
641 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
643 netif_stop_queue(dev);
644 return mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
647 static netdev_tx_t mlxsw_sp_port_xmit(struct sk_buff *skb,
648 struct net_device *dev)
650 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
651 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
652 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
653 const struct mlxsw_tx_info tx_info = {
654 .local_port = mlxsw_sp_port->local_port,
660 if (mlxsw_core_skb_transmit_busy(mlxsw_sp->core, &tx_info))
661 return NETDEV_TX_BUSY;
663 if (unlikely(skb_headroom(skb) < MLXSW_TXHDR_LEN)) {
664 struct sk_buff *skb_orig = skb;
666 skb = skb_realloc_headroom(skb, MLXSW_TXHDR_LEN);
668 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
669 dev_kfree_skb_any(skb_orig);
674 if (eth_skb_pad(skb)) {
675 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
679 mlxsw_sp_txhdr_construct(skb, &tx_info);
680 /* TX header is consumed by HW on the way so we shouldn't count its
681 * bytes as being sent.
683 len = skb->len - MLXSW_TXHDR_LEN;
685 /* Due to a race we might fail here because of a full queue. In that
686 * unlikely case we simply drop the packet.
688 err = mlxsw_core_skb_transmit(mlxsw_sp->core, skb, &tx_info);
691 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
692 u64_stats_update_begin(&pcpu_stats->syncp);
693 pcpu_stats->tx_packets++;
694 pcpu_stats->tx_bytes += len;
695 u64_stats_update_end(&pcpu_stats->syncp);
697 this_cpu_inc(mlxsw_sp_port->pcpu_stats->tx_dropped);
698 dev_kfree_skb_any(skb);
703 static void mlxsw_sp_set_rx_mode(struct net_device *dev)
707 static int mlxsw_sp_port_set_mac_address(struct net_device *dev, void *p)
709 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
710 struct sockaddr *addr = p;
713 if (!is_valid_ether_addr(addr->sa_data))
714 return -EADDRNOTAVAIL;
716 err = mlxsw_sp_port_dev_addr_set(mlxsw_sp_port, addr->sa_data);
719 memcpy(dev->dev_addr, addr->sa_data, dev->addr_len);
723 static void mlxsw_sp_pg_buf_pack(char *pbmc_pl, int pg_index, int mtu,
724 bool pause_en, bool pfc_en, u16 delay)
726 u16 pg_size = 2 * MLXSW_SP_BYTES_TO_CELLS(mtu);
728 delay = pfc_en ? mlxsw_sp_pfc_delay_get(mtu, delay) :
729 MLXSW_SP_PAUSE_DELAY;
731 if (pause_en || pfc_en)
732 mlxsw_reg_pbmc_lossless_buffer_pack(pbmc_pl, pg_index,
733 pg_size + delay, pg_size);
735 mlxsw_reg_pbmc_lossy_buffer_pack(pbmc_pl, pg_index, pg_size);
738 int __mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port, int mtu,
739 u8 *prio_tc, bool pause_en,
740 struct ieee_pfc *my_pfc)
742 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
743 u8 pfc_en = !!my_pfc ? my_pfc->pfc_en : 0;
744 u16 delay = !!my_pfc ? my_pfc->delay : 0;
745 char pbmc_pl[MLXSW_REG_PBMC_LEN];
748 mlxsw_reg_pbmc_pack(pbmc_pl, mlxsw_sp_port->local_port, 0, 0);
749 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
753 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
754 bool configure = false;
757 for (j = 0; j < IEEE_8021QAZ_MAX_TCS; j++) {
758 if (prio_tc[j] == i) {
759 pfc = pfc_en & BIT(j);
767 mlxsw_sp_pg_buf_pack(pbmc_pl, i, mtu, pause_en, pfc, delay);
770 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(pbmc), pbmc_pl);
773 static int mlxsw_sp_port_headroom_set(struct mlxsw_sp_port *mlxsw_sp_port,
774 int mtu, bool pause_en)
776 u8 def_prio_tc[IEEE_8021QAZ_MAX_TCS] = {0};
777 bool dcb_en = !!mlxsw_sp_port->dcb.ets;
778 struct ieee_pfc *my_pfc;
781 prio_tc = dcb_en ? mlxsw_sp_port->dcb.ets->prio_tc : def_prio_tc;
782 my_pfc = dcb_en ? mlxsw_sp_port->dcb.pfc : NULL;
784 return __mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, prio_tc,
788 static int mlxsw_sp_port_change_mtu(struct net_device *dev, int mtu)
790 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
791 bool pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
794 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, mtu, pause_en);
797 err = mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, mtu);
799 goto err_span_port_mtu_update;
800 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, mtu);
802 goto err_port_mtu_set;
807 mlxsw_sp_span_port_mtu_update(mlxsw_sp_port, dev->mtu);
808 err_span_port_mtu_update:
809 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
813 static struct rtnl_link_stats64 *
814 mlxsw_sp_port_get_stats64(struct net_device *dev,
815 struct rtnl_link_stats64 *stats)
817 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
818 struct mlxsw_sp_port_pcpu_stats *p;
819 u64 rx_packets, rx_bytes, tx_packets, tx_bytes;
824 for_each_possible_cpu(i) {
825 p = per_cpu_ptr(mlxsw_sp_port->pcpu_stats, i);
827 start = u64_stats_fetch_begin_irq(&p->syncp);
828 rx_packets = p->rx_packets;
829 rx_bytes = p->rx_bytes;
830 tx_packets = p->tx_packets;
831 tx_bytes = p->tx_bytes;
832 } while (u64_stats_fetch_retry_irq(&p->syncp, start));
834 stats->rx_packets += rx_packets;
835 stats->rx_bytes += rx_bytes;
836 stats->tx_packets += tx_packets;
837 stats->tx_bytes += tx_bytes;
838 /* tx_dropped is u32, updated without syncp protection. */
839 tx_dropped += p->tx_dropped;
841 stats->tx_dropped = tx_dropped;
845 int mlxsw_sp_port_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid_begin,
846 u16 vid_end, bool is_member, bool untagged)
848 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
852 spvm_pl = kmalloc(MLXSW_REG_SPVM_LEN, GFP_KERNEL);
856 mlxsw_reg_spvm_pack(spvm_pl, mlxsw_sp_port->local_port, vid_begin,
857 vid_end, is_member, untagged);
858 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvm), spvm_pl);
863 static int mlxsw_sp_port_vp_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port)
865 enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
866 u16 vid, last_visited_vid;
869 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
870 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, true, vid,
873 last_visited_vid = vid;
874 goto err_port_vid_to_fid_set;
878 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, true);
880 last_visited_vid = VLAN_N_VID;
881 goto err_port_vid_to_fid_set;
886 err_port_vid_to_fid_set:
887 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
888 mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, vid,
893 static int mlxsw_sp_port_vlan_mode_trans(struct mlxsw_sp_port *mlxsw_sp_port)
895 enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
899 err = mlxsw_sp_port_vp_mode_set(mlxsw_sp_port, false);
903 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
904 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false,
913 static struct mlxsw_sp_port *
914 mlxsw_sp_port_vport_create(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
916 struct mlxsw_sp_port *mlxsw_sp_vport;
918 mlxsw_sp_vport = kzalloc(sizeof(*mlxsw_sp_vport), GFP_KERNEL);
922 /* dev will be set correctly after the VLAN device is linked
923 * with the real device. In case of bridge SELF invocation, dev
926 mlxsw_sp_vport->dev = mlxsw_sp_port->dev;
927 mlxsw_sp_vport->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
928 mlxsw_sp_vport->local_port = mlxsw_sp_port->local_port;
929 mlxsw_sp_vport->stp_state = BR_STATE_FORWARDING;
930 mlxsw_sp_vport->lagged = mlxsw_sp_port->lagged;
931 mlxsw_sp_vport->lag_id = mlxsw_sp_port->lag_id;
932 mlxsw_sp_vport->vport.vid = vid;
934 list_add(&mlxsw_sp_vport->vport.list, &mlxsw_sp_port->vports_list);
936 return mlxsw_sp_vport;
939 static void mlxsw_sp_port_vport_destroy(struct mlxsw_sp_port *mlxsw_sp_vport)
941 list_del(&mlxsw_sp_vport->vport.list);
942 kfree(mlxsw_sp_vport);
945 static int mlxsw_sp_port_add_vid(struct net_device *dev,
946 __be16 __always_unused proto, u16 vid)
948 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
949 struct mlxsw_sp_port *mlxsw_sp_vport;
950 bool untagged = vid == 1;
953 /* VLAN 0 is added to HW filter when device goes up, but it is
954 * reserved in our case, so simply return.
959 if (mlxsw_sp_port_vport_find(mlxsw_sp_port, vid))
962 mlxsw_sp_vport = mlxsw_sp_port_vport_create(mlxsw_sp_port, vid);
966 /* When adding the first VLAN interface on a bridged port we need to
967 * transition all the active 802.1Q bridge VLANs to use explicit
968 * {Port, VID} to FID mappings and set the port's mode to Virtual mode.
970 if (list_is_singular(&mlxsw_sp_port->vports_list)) {
971 err = mlxsw_sp_port_vp_mode_trans(mlxsw_sp_port);
973 goto err_port_vp_mode_trans;
976 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false);
978 goto err_port_vid_learning_set;
980 err = mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, true, untagged);
982 goto err_port_add_vid;
987 mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true);
988 err_port_vid_learning_set:
989 if (list_is_singular(&mlxsw_sp_port->vports_list))
990 mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port);
991 err_port_vp_mode_trans:
992 mlxsw_sp_port_vport_destroy(mlxsw_sp_vport);
996 static int mlxsw_sp_port_kill_vid(struct net_device *dev,
997 __be16 __always_unused proto, u16 vid)
999 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1000 struct mlxsw_sp_port *mlxsw_sp_vport;
1001 struct mlxsw_sp_fid *f;
1003 /* VLAN 0 is removed from HW filter when device goes down, but
1004 * it is reserved in our case, so simply return.
1009 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
1010 if (WARN_ON(!mlxsw_sp_vport))
1013 mlxsw_sp_port_vlan_set(mlxsw_sp_vport, vid, vid, false, false);
1015 mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true);
1017 /* Drop FID reference. If this was the last reference the
1018 * resources will be freed.
1020 f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
1021 if (f && !WARN_ON(!f->leave))
1022 f->leave(mlxsw_sp_vport);
1024 /* When removing the last VLAN interface on a bridged port we need to
1025 * transition all active 802.1Q bridge VLANs to use VID to FID
1026 * mappings and set port's mode to VLAN mode.
1028 if (list_is_singular(&mlxsw_sp_port->vports_list))
1029 mlxsw_sp_port_vlan_mode_trans(mlxsw_sp_port);
1031 mlxsw_sp_port_vport_destroy(mlxsw_sp_vport);
1036 static int mlxsw_sp_port_get_phys_port_name(struct net_device *dev, char *name,
1039 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1040 u8 module = mlxsw_sp_port->mapping.module;
1041 u8 width = mlxsw_sp_port->mapping.width;
1042 u8 lane = mlxsw_sp_port->mapping.lane;
1045 if (!mlxsw_sp_port->split)
1046 err = snprintf(name, len, "p%d", module + 1);
1048 err = snprintf(name, len, "p%ds%d", module + 1,
1057 static struct mlxsw_sp_port_mall_tc_entry *
1058 mlxsw_sp_port_mirror_entry_find(struct mlxsw_sp_port *port,
1059 unsigned long cookie) {
1060 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1062 list_for_each_entry(mall_tc_entry, &port->mall_tc_list, list)
1063 if (mall_tc_entry->cookie == cookie)
1064 return mall_tc_entry;
1070 mlxsw_sp_port_add_cls_matchall_mirror(struct mlxsw_sp_port *mlxsw_sp_port,
1071 struct tc_cls_matchall_offload *cls,
1072 const struct tc_action *a,
1075 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1076 struct net *net = dev_net(mlxsw_sp_port->dev);
1077 enum mlxsw_sp_span_type span_type;
1078 struct mlxsw_sp_port *to_port;
1079 struct net_device *to_dev;
1083 ifindex = tcf_mirred_ifindex(a);
1084 to_dev = __dev_get_by_index(net, ifindex);
1086 netdev_err(mlxsw_sp_port->dev, "Could not find requested device\n");
1090 if (!mlxsw_sp_port_dev_check(to_dev)) {
1091 netdev_err(mlxsw_sp_port->dev, "Cannot mirror to a non-spectrum port");
1094 to_port = netdev_priv(to_dev);
1096 mall_tc_entry = kzalloc(sizeof(*mall_tc_entry), GFP_KERNEL);
1100 mall_tc_entry->cookie = cls->cookie;
1101 mall_tc_entry->type = MLXSW_SP_PORT_MALL_MIRROR;
1102 mall_tc_entry->mirror.to_local_port = to_port->local_port;
1103 mall_tc_entry->mirror.ingress = ingress;
1104 list_add_tail(&mall_tc_entry->list, &mlxsw_sp_port->mall_tc_list);
1106 span_type = ingress ? MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1107 err = mlxsw_sp_span_mirror_add(mlxsw_sp_port, to_port, span_type);
1109 goto err_mirror_add;
1113 list_del(&mall_tc_entry->list);
1114 kfree(mall_tc_entry);
1118 static int mlxsw_sp_port_add_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1120 struct tc_cls_matchall_offload *cls,
1123 const struct tc_action *a;
1126 if (!tc_single_action(cls->exts)) {
1127 netdev_err(mlxsw_sp_port->dev, "only singular actions are supported\n");
1131 tc_for_each_action(a, cls->exts) {
1132 if (!is_tcf_mirred_mirror(a) || protocol != htons(ETH_P_ALL))
1135 err = mlxsw_sp_port_add_cls_matchall_mirror(mlxsw_sp_port, cls,
1144 static void mlxsw_sp_port_del_cls_matchall(struct mlxsw_sp_port *mlxsw_sp_port,
1145 struct tc_cls_matchall_offload *cls)
1147 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1148 struct mlxsw_sp_port_mall_tc_entry *mall_tc_entry;
1149 enum mlxsw_sp_span_type span_type;
1150 struct mlxsw_sp_port *to_port;
1152 mall_tc_entry = mlxsw_sp_port_mirror_entry_find(mlxsw_sp_port,
1154 if (!mall_tc_entry) {
1155 netdev_dbg(mlxsw_sp_port->dev, "tc entry not found on port\n");
1159 switch (mall_tc_entry->type) {
1160 case MLXSW_SP_PORT_MALL_MIRROR:
1161 to_port = mlxsw_sp->ports[mall_tc_entry->mirror.to_local_port];
1162 span_type = mall_tc_entry->mirror.ingress ?
1163 MLXSW_SP_SPAN_INGRESS : MLXSW_SP_SPAN_EGRESS;
1165 mlxsw_sp_span_mirror_remove(mlxsw_sp_port, to_port, span_type);
1171 list_del(&mall_tc_entry->list);
1172 kfree(mall_tc_entry);
1175 static int mlxsw_sp_setup_tc(struct net_device *dev, u32 handle,
1176 __be16 proto, struct tc_to_netdev *tc)
1178 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1179 bool ingress = TC_H_MAJ(handle) == TC_H_MAJ(TC_H_INGRESS);
1181 if (tc->type == TC_SETUP_MATCHALL) {
1182 switch (tc->cls_mall->command) {
1183 case TC_CLSMATCHALL_REPLACE:
1184 return mlxsw_sp_port_add_cls_matchall(mlxsw_sp_port,
1188 case TC_CLSMATCHALL_DESTROY:
1189 mlxsw_sp_port_del_cls_matchall(mlxsw_sp_port,
1200 static const struct net_device_ops mlxsw_sp_port_netdev_ops = {
1201 .ndo_open = mlxsw_sp_port_open,
1202 .ndo_stop = mlxsw_sp_port_stop,
1203 .ndo_start_xmit = mlxsw_sp_port_xmit,
1204 .ndo_setup_tc = mlxsw_sp_setup_tc,
1205 .ndo_set_rx_mode = mlxsw_sp_set_rx_mode,
1206 .ndo_set_mac_address = mlxsw_sp_port_set_mac_address,
1207 .ndo_change_mtu = mlxsw_sp_port_change_mtu,
1208 .ndo_get_stats64 = mlxsw_sp_port_get_stats64,
1209 .ndo_vlan_rx_add_vid = mlxsw_sp_port_add_vid,
1210 .ndo_vlan_rx_kill_vid = mlxsw_sp_port_kill_vid,
1211 .ndo_neigh_construct = mlxsw_sp_router_neigh_construct,
1212 .ndo_neigh_destroy = mlxsw_sp_router_neigh_destroy,
1213 .ndo_fdb_add = switchdev_port_fdb_add,
1214 .ndo_fdb_del = switchdev_port_fdb_del,
1215 .ndo_fdb_dump = switchdev_port_fdb_dump,
1216 .ndo_bridge_setlink = switchdev_port_bridge_setlink,
1217 .ndo_bridge_getlink = switchdev_port_bridge_getlink,
1218 .ndo_bridge_dellink = switchdev_port_bridge_dellink,
1219 .ndo_get_phys_port_name = mlxsw_sp_port_get_phys_port_name,
1222 static void mlxsw_sp_port_get_drvinfo(struct net_device *dev,
1223 struct ethtool_drvinfo *drvinfo)
1225 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1226 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1228 strlcpy(drvinfo->driver, mlxsw_sp_driver_name, sizeof(drvinfo->driver));
1229 strlcpy(drvinfo->version, mlxsw_sp_driver_version,
1230 sizeof(drvinfo->version));
1231 snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
1233 mlxsw_sp->bus_info->fw_rev.major,
1234 mlxsw_sp->bus_info->fw_rev.minor,
1235 mlxsw_sp->bus_info->fw_rev.subminor);
1236 strlcpy(drvinfo->bus_info, mlxsw_sp->bus_info->device_name,
1237 sizeof(drvinfo->bus_info));
1240 static void mlxsw_sp_port_get_pauseparam(struct net_device *dev,
1241 struct ethtool_pauseparam *pause)
1243 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1245 pause->rx_pause = mlxsw_sp_port->link.rx_pause;
1246 pause->tx_pause = mlxsw_sp_port->link.tx_pause;
1249 static int mlxsw_sp_port_pause_set(struct mlxsw_sp_port *mlxsw_sp_port,
1250 struct ethtool_pauseparam *pause)
1252 char pfcc_pl[MLXSW_REG_PFCC_LEN];
1254 mlxsw_reg_pfcc_pack(pfcc_pl, mlxsw_sp_port->local_port);
1255 mlxsw_reg_pfcc_pprx_set(pfcc_pl, pause->rx_pause);
1256 mlxsw_reg_pfcc_pptx_set(pfcc_pl, pause->tx_pause);
1258 return mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(pfcc),
1262 static int mlxsw_sp_port_set_pauseparam(struct net_device *dev,
1263 struct ethtool_pauseparam *pause)
1265 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1266 bool pause_en = pause->tx_pause || pause->rx_pause;
1269 if (mlxsw_sp_port->dcb.pfc && mlxsw_sp_port->dcb.pfc->pfc_en) {
1270 netdev_err(dev, "PFC already enabled on port\n");
1274 if (pause->autoneg) {
1275 netdev_err(dev, "PAUSE frames autonegotiation isn't supported\n");
1279 err = mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1281 netdev_err(dev, "Failed to configure port's headroom\n");
1285 err = mlxsw_sp_port_pause_set(mlxsw_sp_port, pause);
1287 netdev_err(dev, "Failed to set PAUSE parameters\n");
1288 goto err_port_pause_configure;
1291 mlxsw_sp_port->link.rx_pause = pause->rx_pause;
1292 mlxsw_sp_port->link.tx_pause = pause->tx_pause;
1296 err_port_pause_configure:
1297 pause_en = mlxsw_sp_port_is_pause_en(mlxsw_sp_port);
1298 mlxsw_sp_port_headroom_set(mlxsw_sp_port, dev->mtu, pause_en);
1302 struct mlxsw_sp_port_hw_stats {
1303 char str[ETH_GSTRING_LEN];
1304 u64 (*getter)(char *payload);
1307 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_stats[] = {
1309 .str = "a_frames_transmitted_ok",
1310 .getter = mlxsw_reg_ppcnt_a_frames_transmitted_ok_get,
1313 .str = "a_frames_received_ok",
1314 .getter = mlxsw_reg_ppcnt_a_frames_received_ok_get,
1317 .str = "a_frame_check_sequence_errors",
1318 .getter = mlxsw_reg_ppcnt_a_frame_check_sequence_errors_get,
1321 .str = "a_alignment_errors",
1322 .getter = mlxsw_reg_ppcnt_a_alignment_errors_get,
1325 .str = "a_octets_transmitted_ok",
1326 .getter = mlxsw_reg_ppcnt_a_octets_transmitted_ok_get,
1329 .str = "a_octets_received_ok",
1330 .getter = mlxsw_reg_ppcnt_a_octets_received_ok_get,
1333 .str = "a_multicast_frames_xmitted_ok",
1334 .getter = mlxsw_reg_ppcnt_a_multicast_frames_xmitted_ok_get,
1337 .str = "a_broadcast_frames_xmitted_ok",
1338 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_xmitted_ok_get,
1341 .str = "a_multicast_frames_received_ok",
1342 .getter = mlxsw_reg_ppcnt_a_multicast_frames_received_ok_get,
1345 .str = "a_broadcast_frames_received_ok",
1346 .getter = mlxsw_reg_ppcnt_a_broadcast_frames_received_ok_get,
1349 .str = "a_in_range_length_errors",
1350 .getter = mlxsw_reg_ppcnt_a_in_range_length_errors_get,
1353 .str = "a_out_of_range_length_field",
1354 .getter = mlxsw_reg_ppcnt_a_out_of_range_length_field_get,
1357 .str = "a_frame_too_long_errors",
1358 .getter = mlxsw_reg_ppcnt_a_frame_too_long_errors_get,
1361 .str = "a_symbol_error_during_carrier",
1362 .getter = mlxsw_reg_ppcnt_a_symbol_error_during_carrier_get,
1365 .str = "a_mac_control_frames_transmitted",
1366 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_transmitted_get,
1369 .str = "a_mac_control_frames_received",
1370 .getter = mlxsw_reg_ppcnt_a_mac_control_frames_received_get,
1373 .str = "a_unsupported_opcodes_received",
1374 .getter = mlxsw_reg_ppcnt_a_unsupported_opcodes_received_get,
1377 .str = "a_pause_mac_ctrl_frames_received",
1378 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_received_get,
1381 .str = "a_pause_mac_ctrl_frames_xmitted",
1382 .getter = mlxsw_reg_ppcnt_a_pause_mac_ctrl_frames_transmitted_get,
1386 #define MLXSW_SP_PORT_HW_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_stats)
1388 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_prio_stats[] = {
1390 .str = "rx_octets_prio",
1391 .getter = mlxsw_reg_ppcnt_rx_octets_get,
1394 .str = "rx_frames_prio",
1395 .getter = mlxsw_reg_ppcnt_rx_frames_get,
1398 .str = "tx_octets_prio",
1399 .getter = mlxsw_reg_ppcnt_tx_octets_get,
1402 .str = "tx_frames_prio",
1403 .getter = mlxsw_reg_ppcnt_tx_frames_get,
1406 .str = "rx_pause_prio",
1407 .getter = mlxsw_reg_ppcnt_rx_pause_get,
1410 .str = "rx_pause_duration_prio",
1411 .getter = mlxsw_reg_ppcnt_rx_pause_duration_get,
1414 .str = "tx_pause_prio",
1415 .getter = mlxsw_reg_ppcnt_tx_pause_get,
1418 .str = "tx_pause_duration_prio",
1419 .getter = mlxsw_reg_ppcnt_tx_pause_duration_get,
1423 #define MLXSW_SP_PORT_HW_PRIO_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_prio_stats)
1425 static u64 mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get(char *ppcnt_pl)
1427 u64 transmit_queue = mlxsw_reg_ppcnt_tc_transmit_queue_get(ppcnt_pl);
1429 return MLXSW_SP_CELLS_TO_BYTES(transmit_queue);
1432 static struct mlxsw_sp_port_hw_stats mlxsw_sp_port_hw_tc_stats[] = {
1434 .str = "tc_transmit_queue_tc",
1435 .getter = mlxsw_reg_ppcnt_tc_transmit_queue_bytes_get,
1438 .str = "tc_no_buffer_discard_uc_tc",
1439 .getter = mlxsw_reg_ppcnt_tc_no_buffer_discard_uc_get,
1443 #define MLXSW_SP_PORT_HW_TC_STATS_LEN ARRAY_SIZE(mlxsw_sp_port_hw_tc_stats)
1445 #define MLXSW_SP_PORT_ETHTOOL_STATS_LEN (MLXSW_SP_PORT_HW_STATS_LEN + \
1446 (MLXSW_SP_PORT_HW_PRIO_STATS_LEN + \
1447 MLXSW_SP_PORT_HW_TC_STATS_LEN) * \
1448 IEEE_8021QAZ_MAX_TCS)
1450 static void mlxsw_sp_port_get_prio_strings(u8 **p, int prio)
1454 for (i = 0; i < MLXSW_SP_PORT_HW_PRIO_STATS_LEN; i++) {
1455 snprintf(*p, ETH_GSTRING_LEN, "%s_%d",
1456 mlxsw_sp_port_hw_prio_stats[i].str, prio);
1457 *p += ETH_GSTRING_LEN;
1461 static void mlxsw_sp_port_get_tc_strings(u8 **p, int tc)
1465 for (i = 0; i < MLXSW_SP_PORT_HW_TC_STATS_LEN; i++) {
1466 snprintf(*p, ETH_GSTRING_LEN, "%s_%d",
1467 mlxsw_sp_port_hw_tc_stats[i].str, tc);
1468 *p += ETH_GSTRING_LEN;
1472 static void mlxsw_sp_port_get_strings(struct net_device *dev,
1473 u32 stringset, u8 *data)
1478 switch (stringset) {
1480 for (i = 0; i < MLXSW_SP_PORT_HW_STATS_LEN; i++) {
1481 memcpy(p, mlxsw_sp_port_hw_stats[i].str,
1483 p += ETH_GSTRING_LEN;
1486 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
1487 mlxsw_sp_port_get_prio_strings(&p, i);
1489 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++)
1490 mlxsw_sp_port_get_tc_strings(&p, i);
1496 static int mlxsw_sp_port_set_phys_id(struct net_device *dev,
1497 enum ethtool_phys_id_state state)
1499 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1500 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1501 char mlcr_pl[MLXSW_REG_MLCR_LEN];
1505 case ETHTOOL_ID_ACTIVE:
1508 case ETHTOOL_ID_INACTIVE:
1515 mlxsw_reg_mlcr_pack(mlcr_pl, mlxsw_sp_port->local_port, active);
1516 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(mlcr), mlcr_pl);
1520 mlxsw_sp_get_hw_stats_by_group(struct mlxsw_sp_port_hw_stats **p_hw_stats,
1521 int *p_len, enum mlxsw_reg_ppcnt_grp grp)
1524 case MLXSW_REG_PPCNT_IEEE_8023_CNT:
1525 *p_hw_stats = mlxsw_sp_port_hw_stats;
1526 *p_len = MLXSW_SP_PORT_HW_STATS_LEN;
1528 case MLXSW_REG_PPCNT_PRIO_CNT:
1529 *p_hw_stats = mlxsw_sp_port_hw_prio_stats;
1530 *p_len = MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
1532 case MLXSW_REG_PPCNT_TC_CNT:
1533 *p_hw_stats = mlxsw_sp_port_hw_tc_stats;
1534 *p_len = MLXSW_SP_PORT_HW_TC_STATS_LEN;
1543 static void __mlxsw_sp_port_get_stats(struct net_device *dev,
1544 enum mlxsw_reg_ppcnt_grp grp, int prio,
1545 u64 *data, int data_index)
1547 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1548 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1549 struct mlxsw_sp_port_hw_stats *hw_stats;
1550 char ppcnt_pl[MLXSW_REG_PPCNT_LEN];
1554 err = mlxsw_sp_get_hw_stats_by_group(&hw_stats, &len, grp);
1557 mlxsw_reg_ppcnt_pack(ppcnt_pl, mlxsw_sp_port->local_port, grp, prio);
1558 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ppcnt), ppcnt_pl);
1559 for (i = 0; i < len; i++)
1560 data[data_index + i] = !err ? hw_stats[i].getter(ppcnt_pl) : 0;
1563 static void mlxsw_sp_port_get_stats(struct net_device *dev,
1564 struct ethtool_stats *stats, u64 *data)
1566 int i, data_index = 0;
1568 /* IEEE 802.3 Counters */
1569 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_IEEE_8023_CNT, 0,
1571 data_index = MLXSW_SP_PORT_HW_STATS_LEN;
1573 /* Per-Priority Counters */
1574 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1575 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_PRIO_CNT, i,
1577 data_index += MLXSW_SP_PORT_HW_PRIO_STATS_LEN;
1580 /* Per-TC Counters */
1581 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1582 __mlxsw_sp_port_get_stats(dev, MLXSW_REG_PPCNT_TC_CNT, i,
1584 data_index += MLXSW_SP_PORT_HW_TC_STATS_LEN;
1588 static int mlxsw_sp_port_get_sset_count(struct net_device *dev, int sset)
1592 return MLXSW_SP_PORT_ETHTOOL_STATS_LEN;
1598 struct mlxsw_sp_port_link_mode {
1605 static const struct mlxsw_sp_port_link_mode mlxsw_sp_port_link_mode[] = {
1607 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_T,
1608 .supported = SUPPORTED_100baseT_Full,
1609 .advertised = ADVERTISED_100baseT_Full,
1613 .mask = MLXSW_REG_PTYS_ETH_SPEED_100BASE_TX,
1617 .mask = MLXSW_REG_PTYS_ETH_SPEED_SGMII |
1618 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX,
1619 .supported = SUPPORTED_1000baseKX_Full,
1620 .advertised = ADVERTISED_1000baseKX_Full,
1624 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_T,
1625 .supported = SUPPORTED_10000baseT_Full,
1626 .advertised = ADVERTISED_10000baseT_Full,
1630 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CX4 |
1631 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4,
1632 .supported = SUPPORTED_10000baseKX4_Full,
1633 .advertised = ADVERTISED_10000baseKX4_Full,
1637 .mask = MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
1638 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
1639 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
1640 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_ER_LR,
1641 .supported = SUPPORTED_10000baseKR_Full,
1642 .advertised = ADVERTISED_10000baseKR_Full,
1646 .mask = MLXSW_REG_PTYS_ETH_SPEED_20GBASE_KR2,
1647 .supported = SUPPORTED_20000baseKR2_Full,
1648 .advertised = ADVERTISED_20000baseKR2_Full,
1652 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4,
1653 .supported = SUPPORTED_40000baseCR4_Full,
1654 .advertised = ADVERTISED_40000baseCR4_Full,
1658 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4,
1659 .supported = SUPPORTED_40000baseKR4_Full,
1660 .advertised = ADVERTISED_40000baseKR4_Full,
1664 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4,
1665 .supported = SUPPORTED_40000baseSR4_Full,
1666 .advertised = ADVERTISED_40000baseSR4_Full,
1670 .mask = MLXSW_REG_PTYS_ETH_SPEED_40GBASE_LR4_ER4,
1671 .supported = SUPPORTED_40000baseLR4_Full,
1672 .advertised = ADVERTISED_40000baseLR4_Full,
1676 .mask = MLXSW_REG_PTYS_ETH_SPEED_25GBASE_CR |
1677 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_KR |
1678 MLXSW_REG_PTYS_ETH_SPEED_25GBASE_SR,
1682 .mask = MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR4 |
1683 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_CR2 |
1684 MLXSW_REG_PTYS_ETH_SPEED_50GBASE_KR2,
1688 .mask = MLXSW_REG_PTYS_ETH_SPEED_56GBASE_R4,
1689 .supported = SUPPORTED_56000baseKR4_Full,
1690 .advertised = ADVERTISED_56000baseKR4_Full,
1694 .mask = MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4 |
1695 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
1696 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
1697 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_LR4_ER4,
1702 #define MLXSW_SP_PORT_LINK_MODE_LEN ARRAY_SIZE(mlxsw_sp_port_link_mode)
1704 static u32 mlxsw_sp_from_ptys_supported_port(u32 ptys_eth_proto)
1706 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
1707 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
1708 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
1709 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
1710 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
1711 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
1712 return SUPPORTED_FIBRE;
1714 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
1715 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
1716 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
1717 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4 |
1718 MLXSW_REG_PTYS_ETH_SPEED_1000BASE_KX))
1719 return SUPPORTED_Backplane;
1723 static u32 mlxsw_sp_from_ptys_supported_link(u32 ptys_eth_proto)
1728 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1729 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask)
1730 modes |= mlxsw_sp_port_link_mode[i].supported;
1735 static u32 mlxsw_sp_from_ptys_advert_link(u32 ptys_eth_proto)
1740 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1741 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask)
1742 modes |= mlxsw_sp_port_link_mode[i].advertised;
1747 static void mlxsw_sp_from_ptys_speed_duplex(bool carrier_ok, u32 ptys_eth_proto,
1748 struct ethtool_cmd *cmd)
1750 u32 speed = SPEED_UNKNOWN;
1751 u8 duplex = DUPLEX_UNKNOWN;
1757 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1758 if (ptys_eth_proto & mlxsw_sp_port_link_mode[i].mask) {
1759 speed = mlxsw_sp_port_link_mode[i].speed;
1760 duplex = DUPLEX_FULL;
1765 ethtool_cmd_speed_set(cmd, speed);
1766 cmd->duplex = duplex;
1769 static u8 mlxsw_sp_port_connector_port(u32 ptys_eth_proto)
1771 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_SR |
1772 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_SR4 |
1773 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_SR4 |
1774 MLXSW_REG_PTYS_ETH_SPEED_SGMII))
1777 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_CR |
1778 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_CR4 |
1779 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_CR4))
1782 if (ptys_eth_proto & (MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KR |
1783 MLXSW_REG_PTYS_ETH_SPEED_10GBASE_KX4 |
1784 MLXSW_REG_PTYS_ETH_SPEED_40GBASE_KR4 |
1785 MLXSW_REG_PTYS_ETH_SPEED_100GBASE_KR4))
1791 static int mlxsw_sp_port_get_settings(struct net_device *dev,
1792 struct ethtool_cmd *cmd)
1794 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1795 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1796 char ptys_pl[MLXSW_REG_PTYS_LEN];
1798 u32 eth_proto_admin;
1802 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, 0);
1803 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1805 netdev_err(dev, "Failed to get proto");
1808 mlxsw_reg_ptys_unpack(ptys_pl, ð_proto_cap,
1809 ð_proto_admin, ð_proto_oper);
1811 cmd->supported = mlxsw_sp_from_ptys_supported_port(eth_proto_cap) |
1812 mlxsw_sp_from_ptys_supported_link(eth_proto_cap) |
1813 SUPPORTED_Pause | SUPPORTED_Asym_Pause |
1815 cmd->advertising = mlxsw_sp_from_ptys_advert_link(eth_proto_admin);
1816 mlxsw_sp_from_ptys_speed_duplex(netif_carrier_ok(dev),
1817 eth_proto_oper, cmd);
1819 eth_proto_oper = eth_proto_oper ? eth_proto_oper : eth_proto_cap;
1820 cmd->port = mlxsw_sp_port_connector_port(eth_proto_oper);
1821 cmd->lp_advertising = mlxsw_sp_from_ptys_advert_link(eth_proto_oper);
1823 cmd->transceiver = XCVR_INTERNAL;
1827 static u32 mlxsw_sp_to_ptys_advert_link(u32 advertising)
1832 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1833 if (advertising & mlxsw_sp_port_link_mode[i].advertised)
1834 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
1839 static u32 mlxsw_sp_to_ptys_speed(u32 speed)
1844 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1845 if (speed == mlxsw_sp_port_link_mode[i].speed)
1846 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
1851 static u32 mlxsw_sp_to_ptys_upper_speed(u32 upper_speed)
1856 for (i = 0; i < MLXSW_SP_PORT_LINK_MODE_LEN; i++) {
1857 if (mlxsw_sp_port_link_mode[i].speed <= upper_speed)
1858 ptys_proto |= mlxsw_sp_port_link_mode[i].mask;
1863 static int mlxsw_sp_port_set_settings(struct net_device *dev,
1864 struct ethtool_cmd *cmd)
1866 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1867 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1868 char ptys_pl[MLXSW_REG_PTYS_LEN];
1872 u32 eth_proto_admin;
1875 speed = ethtool_cmd_speed(cmd);
1877 eth_proto_new = cmd->autoneg == AUTONEG_ENABLE ?
1878 mlxsw_sp_to_ptys_advert_link(cmd->advertising) :
1879 mlxsw_sp_to_ptys_speed(speed);
1881 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, 0);
1882 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1884 netdev_err(dev, "Failed to get proto");
1887 mlxsw_reg_ptys_unpack(ptys_pl, ð_proto_cap, ð_proto_admin, NULL);
1889 eth_proto_new = eth_proto_new & eth_proto_cap;
1890 if (!eth_proto_new) {
1891 netdev_err(dev, "Not supported proto admin requested");
1894 if (eth_proto_new == eth_proto_admin)
1897 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port, eth_proto_new);
1898 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1900 netdev_err(dev, "Failed to set proto admin");
1904 if (!netif_running(dev))
1907 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
1909 netdev_err(dev, "Failed to set admin status");
1913 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, true);
1915 netdev_err(dev, "Failed to set admin status");
1922 static const struct ethtool_ops mlxsw_sp_port_ethtool_ops = {
1923 .get_drvinfo = mlxsw_sp_port_get_drvinfo,
1924 .get_link = ethtool_op_get_link,
1925 .get_pauseparam = mlxsw_sp_port_get_pauseparam,
1926 .set_pauseparam = mlxsw_sp_port_set_pauseparam,
1927 .get_strings = mlxsw_sp_port_get_strings,
1928 .set_phys_id = mlxsw_sp_port_set_phys_id,
1929 .get_ethtool_stats = mlxsw_sp_port_get_stats,
1930 .get_sset_count = mlxsw_sp_port_get_sset_count,
1931 .get_settings = mlxsw_sp_port_get_settings,
1932 .set_settings = mlxsw_sp_port_set_settings,
1936 mlxsw_sp_port_speed_by_width_set(struct mlxsw_sp_port *mlxsw_sp_port, u8 width)
1938 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1939 u32 upper_speed = MLXSW_SP_PORT_BASE_SPEED * width;
1940 char ptys_pl[MLXSW_REG_PTYS_LEN];
1941 u32 eth_proto_admin;
1943 eth_proto_admin = mlxsw_sp_to_ptys_upper_speed(upper_speed);
1944 mlxsw_reg_ptys_pack(ptys_pl, mlxsw_sp_port->local_port,
1946 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ptys), ptys_pl);
1949 int mlxsw_sp_port_ets_set(struct mlxsw_sp_port *mlxsw_sp_port,
1950 enum mlxsw_reg_qeec_hr hr, u8 index, u8 next_index,
1951 bool dwrr, u8 dwrr_weight)
1953 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1954 char qeec_pl[MLXSW_REG_QEEC_LEN];
1956 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
1958 mlxsw_reg_qeec_de_set(qeec_pl, true);
1959 mlxsw_reg_qeec_dwrr_set(qeec_pl, dwrr);
1960 mlxsw_reg_qeec_dwrr_weight_set(qeec_pl, dwrr_weight);
1961 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
1964 int mlxsw_sp_port_ets_maxrate_set(struct mlxsw_sp_port *mlxsw_sp_port,
1965 enum mlxsw_reg_qeec_hr hr, u8 index,
1966 u8 next_index, u32 maxrate)
1968 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1969 char qeec_pl[MLXSW_REG_QEEC_LEN];
1971 mlxsw_reg_qeec_pack(qeec_pl, mlxsw_sp_port->local_port, hr, index,
1973 mlxsw_reg_qeec_mase_set(qeec_pl, true);
1974 mlxsw_reg_qeec_max_shaper_rate_set(qeec_pl, maxrate);
1975 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qeec), qeec_pl);
1978 int mlxsw_sp_port_prio_tc_set(struct mlxsw_sp_port *mlxsw_sp_port,
1979 u8 switch_prio, u8 tclass)
1981 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1982 char qtct_pl[MLXSW_REG_QTCT_LEN];
1984 mlxsw_reg_qtct_pack(qtct_pl, mlxsw_sp_port->local_port, switch_prio,
1986 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(qtct), qtct_pl);
1989 static int mlxsw_sp_port_ets_init(struct mlxsw_sp_port *mlxsw_sp_port)
1993 /* Setup the elements hierarcy, so that each TC is linked to
1994 * one subgroup, which are all member in the same group.
1996 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
1997 MLXSW_REG_QEEC_HIERARCY_GROUP, 0, 0, false,
2001 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2002 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
2003 MLXSW_REG_QEEC_HIERARCY_SUBGROUP, i,
2008 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2009 err = mlxsw_sp_port_ets_set(mlxsw_sp_port,
2010 MLXSW_REG_QEEC_HIERARCY_TC, i, i,
2016 /* Make sure the max shaper is disabled in all hierarcies that
2019 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2020 MLXSW_REG_QEEC_HIERARCY_PORT, 0, 0,
2021 MLXSW_REG_QEEC_MAS_DIS);
2024 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2025 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2026 MLXSW_REG_QEEC_HIERARCY_SUBGROUP,
2028 MLXSW_REG_QEEC_MAS_DIS);
2032 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2033 err = mlxsw_sp_port_ets_maxrate_set(mlxsw_sp_port,
2034 MLXSW_REG_QEEC_HIERARCY_TC,
2036 MLXSW_REG_QEEC_MAS_DIS);
2041 /* Map all priorities to traffic class 0. */
2042 for (i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
2043 err = mlxsw_sp_port_prio_tc_set(mlxsw_sp_port, i, 0);
2051 static int mlxsw_sp_port_pvid_vport_create(struct mlxsw_sp_port *mlxsw_sp_port)
2053 mlxsw_sp_port->pvid = 1;
2055 return mlxsw_sp_port_add_vid(mlxsw_sp_port->dev, 0, 1);
2058 static int mlxsw_sp_port_pvid_vport_destroy(struct mlxsw_sp_port *mlxsw_sp_port)
2060 return mlxsw_sp_port_kill_vid(mlxsw_sp_port->dev, 0, 1);
2063 static int mlxsw_sp_port_create(struct mlxsw_sp *mlxsw_sp, u8 local_port,
2064 bool split, u8 module, u8 width, u8 lane)
2066 struct mlxsw_sp_port *mlxsw_sp_port;
2067 struct net_device *dev;
2071 dev = alloc_etherdev(sizeof(struct mlxsw_sp_port));
2074 mlxsw_sp_port = netdev_priv(dev);
2075 mlxsw_sp_port->dev = dev;
2076 mlxsw_sp_port->mlxsw_sp = mlxsw_sp;
2077 mlxsw_sp_port->local_port = local_port;
2078 mlxsw_sp_port->split = split;
2079 mlxsw_sp_port->mapping.module = module;
2080 mlxsw_sp_port->mapping.width = width;
2081 mlxsw_sp_port->mapping.lane = lane;
2082 bytes = DIV_ROUND_UP(VLAN_N_VID, BITS_PER_BYTE);
2083 mlxsw_sp_port->active_vlans = kzalloc(bytes, GFP_KERNEL);
2084 if (!mlxsw_sp_port->active_vlans) {
2086 goto err_port_active_vlans_alloc;
2088 mlxsw_sp_port->untagged_vlans = kzalloc(bytes, GFP_KERNEL);
2089 if (!mlxsw_sp_port->untagged_vlans) {
2091 goto err_port_untagged_vlans_alloc;
2093 INIT_LIST_HEAD(&mlxsw_sp_port->vports_list);
2094 INIT_LIST_HEAD(&mlxsw_sp_port->mall_tc_list);
2096 mlxsw_sp_port->pcpu_stats =
2097 netdev_alloc_pcpu_stats(struct mlxsw_sp_port_pcpu_stats);
2098 if (!mlxsw_sp_port->pcpu_stats) {
2100 goto err_alloc_stats;
2103 dev->netdev_ops = &mlxsw_sp_port_netdev_ops;
2104 dev->ethtool_ops = &mlxsw_sp_port_ethtool_ops;
2106 err = mlxsw_sp_port_dev_addr_init(mlxsw_sp_port);
2108 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Unable to init port mac address\n",
2109 mlxsw_sp_port->local_port);
2110 goto err_dev_addr_init;
2113 netif_carrier_off(dev);
2115 dev->features |= NETIF_F_NETNS_LOCAL | NETIF_F_LLTX | NETIF_F_SG |
2116 NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
2117 dev->hw_features |= NETIF_F_HW_TC;
2119 /* Each packet needs to have a Tx header (metadata) on top all other
2122 dev->hard_header_len += MLXSW_TXHDR_LEN;
2124 err = mlxsw_sp_port_system_port_mapping_set(mlxsw_sp_port);
2126 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set system port mapping\n",
2127 mlxsw_sp_port->local_port);
2128 goto err_port_system_port_mapping_set;
2131 err = mlxsw_sp_port_swid_set(mlxsw_sp_port, 0);
2133 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set SWID\n",
2134 mlxsw_sp_port->local_port);
2135 goto err_port_swid_set;
2138 err = mlxsw_sp_port_speed_by_width_set(mlxsw_sp_port, width);
2140 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to enable speeds\n",
2141 mlxsw_sp_port->local_port);
2142 goto err_port_speed_by_width_set;
2145 err = mlxsw_sp_port_mtu_set(mlxsw_sp_port, ETH_DATA_LEN);
2147 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to set MTU\n",
2148 mlxsw_sp_port->local_port);
2149 goto err_port_mtu_set;
2152 err = mlxsw_sp_port_admin_status_set(mlxsw_sp_port, false);
2154 goto err_port_admin_status_set;
2156 err = mlxsw_sp_port_buffers_init(mlxsw_sp_port);
2158 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize buffers\n",
2159 mlxsw_sp_port->local_port);
2160 goto err_port_buffers_init;
2163 err = mlxsw_sp_port_ets_init(mlxsw_sp_port);
2165 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize ETS\n",
2166 mlxsw_sp_port->local_port);
2167 goto err_port_ets_init;
2170 /* ETS and buffers must be initialized before DCB. */
2171 err = mlxsw_sp_port_dcb_init(mlxsw_sp_port);
2173 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to initialize DCB\n",
2174 mlxsw_sp_port->local_port);
2175 goto err_port_dcb_init;
2178 err = mlxsw_sp_port_pvid_vport_create(mlxsw_sp_port);
2180 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to create PVID vPort\n",
2181 mlxsw_sp_port->local_port);
2182 goto err_port_pvid_vport_create;
2185 mlxsw_sp_port_switchdev_init(mlxsw_sp_port);
2186 err = register_netdev(dev);
2188 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to register netdev\n",
2189 mlxsw_sp_port->local_port);
2190 goto err_register_netdev;
2193 err = mlxsw_core_port_init(mlxsw_sp->core, &mlxsw_sp_port->core_port,
2194 mlxsw_sp_port->local_port, dev,
2195 mlxsw_sp_port->split, module);
2197 dev_err(mlxsw_sp->bus_info->dev, "Port %d: Failed to init core port\n",
2198 mlxsw_sp_port->local_port);
2199 goto err_core_port_init;
2202 mlxsw_sp->ports[local_port] = mlxsw_sp_port;
2206 unregister_netdev(dev);
2207 err_register_netdev:
2208 mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port);
2209 err_port_pvid_vport_create:
2210 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
2213 err_port_buffers_init:
2214 err_port_admin_status_set:
2216 err_port_speed_by_width_set:
2218 err_port_system_port_mapping_set:
2220 free_percpu(mlxsw_sp_port->pcpu_stats);
2222 kfree(mlxsw_sp_port->untagged_vlans);
2223 err_port_untagged_vlans_alloc:
2224 kfree(mlxsw_sp_port->active_vlans);
2225 err_port_active_vlans_alloc:
2230 static void mlxsw_sp_port_remove(struct mlxsw_sp *mlxsw_sp, u8 local_port)
2232 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
2236 mlxsw_sp->ports[local_port] = NULL;
2237 mlxsw_core_port_fini(&mlxsw_sp_port->core_port);
2238 unregister_netdev(mlxsw_sp_port->dev); /* This calls ndo_stop */
2239 mlxsw_sp_port_pvid_vport_destroy(mlxsw_sp_port);
2240 mlxsw_sp_port_dcb_fini(mlxsw_sp_port);
2241 mlxsw_sp_port_switchdev_fini(mlxsw_sp_port);
2242 mlxsw_sp_port_swid_set(mlxsw_sp_port, MLXSW_PORT_SWID_DISABLED_PORT);
2243 mlxsw_sp_port_module_unmap(mlxsw_sp, mlxsw_sp_port->local_port);
2244 free_percpu(mlxsw_sp_port->pcpu_stats);
2245 kfree(mlxsw_sp_port->untagged_vlans);
2246 kfree(mlxsw_sp_port->active_vlans);
2247 WARN_ON_ONCE(!list_empty(&mlxsw_sp_port->vports_list));
2248 free_netdev(mlxsw_sp_port->dev);
2251 static void mlxsw_sp_ports_remove(struct mlxsw_sp *mlxsw_sp)
2255 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
2256 mlxsw_sp_port_remove(mlxsw_sp, i);
2257 kfree(mlxsw_sp->ports);
2260 static int mlxsw_sp_ports_create(struct mlxsw_sp *mlxsw_sp)
2262 u8 module, width, lane;
2267 alloc_size = sizeof(struct mlxsw_sp_port *) * MLXSW_PORT_MAX_PORTS;
2268 mlxsw_sp->ports = kzalloc(alloc_size, GFP_KERNEL);
2269 if (!mlxsw_sp->ports)
2272 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++) {
2273 err = mlxsw_sp_port_module_info_get(mlxsw_sp, i, &module,
2276 goto err_port_module_info_get;
2279 mlxsw_sp->port_to_module[i] = module;
2280 err = mlxsw_sp_port_create(mlxsw_sp, i, false, module, width,
2283 goto err_port_create;
2288 err_port_module_info_get:
2289 for (i--; i >= 1; i--)
2290 mlxsw_sp_port_remove(mlxsw_sp, i);
2291 kfree(mlxsw_sp->ports);
2295 static u8 mlxsw_sp_cluster_base_port_get(u8 local_port)
2297 u8 offset = (local_port - 1) % MLXSW_SP_PORTS_PER_CLUSTER_MAX;
2299 return local_port - offset;
2302 static int mlxsw_sp_port_split_create(struct mlxsw_sp *mlxsw_sp, u8 base_port,
2303 u8 module, unsigned int count)
2305 u8 width = MLXSW_PORT_MODULE_MAX_WIDTH / count;
2308 for (i = 0; i < count; i++) {
2309 err = mlxsw_sp_port_module_map(mlxsw_sp, base_port + i, module,
2312 goto err_port_module_map;
2315 for (i = 0; i < count; i++) {
2316 err = __mlxsw_sp_port_swid_set(mlxsw_sp, base_port + i, 0);
2318 goto err_port_swid_set;
2321 for (i = 0; i < count; i++) {
2322 err = mlxsw_sp_port_create(mlxsw_sp, base_port + i, true,
2323 module, width, i * width);
2325 goto err_port_create;
2331 for (i--; i >= 0; i--)
2332 mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
2335 for (i--; i >= 0; i--)
2336 __mlxsw_sp_port_swid_set(mlxsw_sp, base_port + i,
2337 MLXSW_PORT_SWID_DISABLED_PORT);
2339 err_port_module_map:
2340 for (i--; i >= 0; i--)
2341 mlxsw_sp_port_module_unmap(mlxsw_sp, base_port + i);
2345 static void mlxsw_sp_port_unsplit_create(struct mlxsw_sp *mlxsw_sp,
2346 u8 base_port, unsigned int count)
2348 u8 local_port, module, width = MLXSW_PORT_MODULE_MAX_WIDTH;
2351 /* Split by four means we need to re-create two ports, otherwise
2356 for (i = 0; i < count; i++) {
2357 local_port = base_port + i * 2;
2358 module = mlxsw_sp->port_to_module[local_port];
2360 mlxsw_sp_port_module_map(mlxsw_sp, local_port, module, width,
2364 for (i = 0; i < count; i++)
2365 __mlxsw_sp_port_swid_set(mlxsw_sp, base_port + i * 2, 0);
2367 for (i = 0; i < count; i++) {
2368 local_port = base_port + i * 2;
2369 module = mlxsw_sp->port_to_module[local_port];
2371 mlxsw_sp_port_create(mlxsw_sp, local_port, false, module,
2376 static int mlxsw_sp_port_split(struct mlxsw_core *mlxsw_core, u8 local_port,
2379 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2380 struct mlxsw_sp_port *mlxsw_sp_port;
2381 u8 module, cur_width, base_port;
2385 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2386 if (!mlxsw_sp_port) {
2387 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
2392 module = mlxsw_sp_port->mapping.module;
2393 cur_width = mlxsw_sp_port->mapping.width;
2395 if (count != 2 && count != 4) {
2396 netdev_err(mlxsw_sp_port->dev, "Port can only be split into 2 or 4 ports\n");
2400 if (cur_width != MLXSW_PORT_MODULE_MAX_WIDTH) {
2401 netdev_err(mlxsw_sp_port->dev, "Port cannot be split further\n");
2405 /* Make sure we have enough slave (even) ports for the split. */
2407 base_port = local_port;
2408 if (mlxsw_sp->ports[base_port + 1]) {
2409 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
2413 base_port = mlxsw_sp_cluster_base_port_get(local_port);
2414 if (mlxsw_sp->ports[base_port + 1] ||
2415 mlxsw_sp->ports[base_port + 3]) {
2416 netdev_err(mlxsw_sp_port->dev, "Invalid split configuration\n");
2421 for (i = 0; i < count; i++)
2422 mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
2424 err = mlxsw_sp_port_split_create(mlxsw_sp, base_port, module, count);
2426 dev_err(mlxsw_sp->bus_info->dev, "Failed to create split ports\n");
2427 goto err_port_split_create;
2432 err_port_split_create:
2433 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
2437 static int mlxsw_sp_port_unsplit(struct mlxsw_core *mlxsw_core, u8 local_port)
2439 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2440 struct mlxsw_sp_port *mlxsw_sp_port;
2441 u8 cur_width, base_port;
2445 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2446 if (!mlxsw_sp_port) {
2447 dev_err(mlxsw_sp->bus_info->dev, "Port number \"%d\" does not exist\n",
2452 if (!mlxsw_sp_port->split) {
2453 netdev_err(mlxsw_sp_port->dev, "Port wasn't split\n");
2457 cur_width = mlxsw_sp_port->mapping.width;
2458 count = cur_width == 1 ? 4 : 2;
2460 base_port = mlxsw_sp_cluster_base_port_get(local_port);
2462 /* Determine which ports to remove. */
2463 if (count == 2 && local_port >= base_port + 2)
2464 base_port = base_port + 2;
2466 for (i = 0; i < count; i++)
2467 mlxsw_sp_port_remove(mlxsw_sp, base_port + i);
2469 mlxsw_sp_port_unsplit_create(mlxsw_sp, base_port, count);
2474 static void mlxsw_sp_pude_event_func(const struct mlxsw_reg_info *reg,
2475 char *pude_pl, void *priv)
2477 struct mlxsw_sp *mlxsw_sp = priv;
2478 struct mlxsw_sp_port *mlxsw_sp_port;
2479 enum mlxsw_reg_pude_oper_status status;
2482 local_port = mlxsw_reg_pude_local_port_get(pude_pl);
2483 mlxsw_sp_port = mlxsw_sp->ports[local_port];
2487 status = mlxsw_reg_pude_oper_status_get(pude_pl);
2488 if (status == MLXSW_PORT_OPER_STATUS_UP) {
2489 netdev_info(mlxsw_sp_port->dev, "link up\n");
2490 netif_carrier_on(mlxsw_sp_port->dev);
2492 netdev_info(mlxsw_sp_port->dev, "link down\n");
2493 netif_carrier_off(mlxsw_sp_port->dev);
2497 static struct mlxsw_event_listener mlxsw_sp_pude_event = {
2498 .func = mlxsw_sp_pude_event_func,
2499 .trap_id = MLXSW_TRAP_ID_PUDE,
2502 static int mlxsw_sp_event_register(struct mlxsw_sp *mlxsw_sp,
2503 enum mlxsw_event_trap_id trap_id)
2505 struct mlxsw_event_listener *el;
2506 char hpkt_pl[MLXSW_REG_HPKT_LEN];
2510 case MLXSW_TRAP_ID_PUDE:
2511 el = &mlxsw_sp_pude_event;
2514 err = mlxsw_core_event_listener_register(mlxsw_sp->core, el, mlxsw_sp);
2518 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_FORWARD, trap_id);
2519 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl);
2521 goto err_event_trap_set;
2526 mlxsw_core_event_listener_unregister(mlxsw_sp->core, el, mlxsw_sp);
2530 static void mlxsw_sp_event_unregister(struct mlxsw_sp *mlxsw_sp,
2531 enum mlxsw_event_trap_id trap_id)
2533 struct mlxsw_event_listener *el;
2536 case MLXSW_TRAP_ID_PUDE:
2537 el = &mlxsw_sp_pude_event;
2540 mlxsw_core_event_listener_unregister(mlxsw_sp->core, el, mlxsw_sp);
2543 static void mlxsw_sp_rx_listener_func(struct sk_buff *skb, u8 local_port,
2546 struct mlxsw_sp *mlxsw_sp = priv;
2547 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp->ports[local_port];
2548 struct mlxsw_sp_port_pcpu_stats *pcpu_stats;
2550 if (unlikely(!mlxsw_sp_port)) {
2551 dev_warn_ratelimited(mlxsw_sp->bus_info->dev, "Port %d: skb received for non-existent port\n",
2556 skb->dev = mlxsw_sp_port->dev;
2558 pcpu_stats = this_cpu_ptr(mlxsw_sp_port->pcpu_stats);
2559 u64_stats_update_begin(&pcpu_stats->syncp);
2560 pcpu_stats->rx_packets++;
2561 pcpu_stats->rx_bytes += skb->len;
2562 u64_stats_update_end(&pcpu_stats->syncp);
2564 skb->protocol = eth_type_trans(skb, skb->dev);
2565 netif_receive_skb(skb);
2568 static const struct mlxsw_rx_listener mlxsw_sp_rx_listener[] = {
2570 .func = mlxsw_sp_rx_listener_func,
2571 .local_port = MLXSW_PORT_DONT_CARE,
2572 .trap_id = MLXSW_TRAP_ID_FDB_MC,
2574 /* Traps for specific L2 packet types, not trapped as FDB MC */
2576 .func = mlxsw_sp_rx_listener_func,
2577 .local_port = MLXSW_PORT_DONT_CARE,
2578 .trap_id = MLXSW_TRAP_ID_STP,
2581 .func = mlxsw_sp_rx_listener_func,
2582 .local_port = MLXSW_PORT_DONT_CARE,
2583 .trap_id = MLXSW_TRAP_ID_LACP,
2586 .func = mlxsw_sp_rx_listener_func,
2587 .local_port = MLXSW_PORT_DONT_CARE,
2588 .trap_id = MLXSW_TRAP_ID_EAPOL,
2591 .func = mlxsw_sp_rx_listener_func,
2592 .local_port = MLXSW_PORT_DONT_CARE,
2593 .trap_id = MLXSW_TRAP_ID_LLDP,
2596 .func = mlxsw_sp_rx_listener_func,
2597 .local_port = MLXSW_PORT_DONT_CARE,
2598 .trap_id = MLXSW_TRAP_ID_MMRP,
2601 .func = mlxsw_sp_rx_listener_func,
2602 .local_port = MLXSW_PORT_DONT_CARE,
2603 .trap_id = MLXSW_TRAP_ID_MVRP,
2606 .func = mlxsw_sp_rx_listener_func,
2607 .local_port = MLXSW_PORT_DONT_CARE,
2608 .trap_id = MLXSW_TRAP_ID_RPVST,
2611 .func = mlxsw_sp_rx_listener_func,
2612 .local_port = MLXSW_PORT_DONT_CARE,
2613 .trap_id = MLXSW_TRAP_ID_DHCP,
2616 .func = mlxsw_sp_rx_listener_func,
2617 .local_port = MLXSW_PORT_DONT_CARE,
2618 .trap_id = MLXSW_TRAP_ID_IGMP_QUERY,
2621 .func = mlxsw_sp_rx_listener_func,
2622 .local_port = MLXSW_PORT_DONT_CARE,
2623 .trap_id = MLXSW_TRAP_ID_IGMP_V1_REPORT,
2626 .func = mlxsw_sp_rx_listener_func,
2627 .local_port = MLXSW_PORT_DONT_CARE,
2628 .trap_id = MLXSW_TRAP_ID_IGMP_V2_REPORT,
2631 .func = mlxsw_sp_rx_listener_func,
2632 .local_port = MLXSW_PORT_DONT_CARE,
2633 .trap_id = MLXSW_TRAP_ID_IGMP_V2_LEAVE,
2636 .func = mlxsw_sp_rx_listener_func,
2637 .local_port = MLXSW_PORT_DONT_CARE,
2638 .trap_id = MLXSW_TRAP_ID_IGMP_V3_REPORT,
2641 .func = mlxsw_sp_rx_listener_func,
2642 .local_port = MLXSW_PORT_DONT_CARE,
2643 .trap_id = MLXSW_TRAP_ID_ARPBC,
2646 .func = mlxsw_sp_rx_listener_func,
2647 .local_port = MLXSW_PORT_DONT_CARE,
2648 .trap_id = MLXSW_TRAP_ID_ARPUC,
2651 .func = mlxsw_sp_rx_listener_func,
2652 .local_port = MLXSW_PORT_DONT_CARE,
2653 .trap_id = MLXSW_TRAP_ID_IP2ME,
2656 .func = mlxsw_sp_rx_listener_func,
2657 .local_port = MLXSW_PORT_DONT_CARE,
2658 .trap_id = MLXSW_TRAP_ID_RTR_INGRESS0,
2661 .func = mlxsw_sp_rx_listener_func,
2662 .local_port = MLXSW_PORT_DONT_CARE,
2663 .trap_id = MLXSW_TRAP_ID_HOST_MISS_IPV4,
2667 static int mlxsw_sp_traps_init(struct mlxsw_sp *mlxsw_sp)
2669 char htgt_pl[MLXSW_REG_HTGT_LEN];
2670 char hpkt_pl[MLXSW_REG_HPKT_LEN];
2674 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_RX);
2675 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(htgt), htgt_pl);
2679 mlxsw_reg_htgt_pack(htgt_pl, MLXSW_REG_HTGT_TRAP_GROUP_CTRL);
2680 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(htgt), htgt_pl);
2684 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_rx_listener); i++) {
2685 err = mlxsw_core_rx_listener_register(mlxsw_sp->core,
2686 &mlxsw_sp_rx_listener[i],
2689 goto err_rx_listener_register;
2691 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_TRAP_TO_CPU,
2692 mlxsw_sp_rx_listener[i].trap_id);
2693 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl);
2695 goto err_rx_trap_set;
2700 mlxsw_core_rx_listener_unregister(mlxsw_sp->core,
2701 &mlxsw_sp_rx_listener[i],
2703 err_rx_listener_register:
2704 for (i--; i >= 0; i--) {
2705 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_DISCARD,
2706 mlxsw_sp_rx_listener[i].trap_id);
2707 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl);
2709 mlxsw_core_rx_listener_unregister(mlxsw_sp->core,
2710 &mlxsw_sp_rx_listener[i],
2716 static void mlxsw_sp_traps_fini(struct mlxsw_sp *mlxsw_sp)
2718 char hpkt_pl[MLXSW_REG_HPKT_LEN];
2721 for (i = 0; i < ARRAY_SIZE(mlxsw_sp_rx_listener); i++) {
2722 mlxsw_reg_hpkt_pack(hpkt_pl, MLXSW_REG_HPKT_ACTION_DISCARD,
2723 mlxsw_sp_rx_listener[i].trap_id);
2724 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(hpkt), hpkt_pl);
2726 mlxsw_core_rx_listener_unregister(mlxsw_sp->core,
2727 &mlxsw_sp_rx_listener[i],
2732 static int __mlxsw_sp_flood_init(struct mlxsw_core *mlxsw_core,
2733 enum mlxsw_reg_sfgc_type type,
2734 enum mlxsw_reg_sfgc_bridge_type bridge_type)
2736 enum mlxsw_flood_table_type table_type;
2737 enum mlxsw_sp_flood_table flood_table;
2738 char sfgc_pl[MLXSW_REG_SFGC_LEN];
2740 if (bridge_type == MLXSW_REG_SFGC_BRIDGE_TYPE_VFID)
2741 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID;
2743 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
2745 if (type == MLXSW_REG_SFGC_TYPE_UNKNOWN_UNICAST)
2746 flood_table = MLXSW_SP_FLOOD_TABLE_UC;
2748 flood_table = MLXSW_SP_FLOOD_TABLE_BM;
2750 mlxsw_reg_sfgc_pack(sfgc_pl, type, bridge_type, table_type,
2752 return mlxsw_reg_write(mlxsw_core, MLXSW_REG(sfgc), sfgc_pl);
2755 static int mlxsw_sp_flood_init(struct mlxsw_sp *mlxsw_sp)
2759 for (type = 0; type < MLXSW_REG_SFGC_TYPE_MAX; type++) {
2760 if (type == MLXSW_REG_SFGC_TYPE_RESERVED)
2763 err = __mlxsw_sp_flood_init(mlxsw_sp->core, type,
2764 MLXSW_REG_SFGC_BRIDGE_TYPE_VFID);
2768 err = __mlxsw_sp_flood_init(mlxsw_sp->core, type,
2769 MLXSW_REG_SFGC_BRIDGE_TYPE_1Q_FID);
2777 static int mlxsw_sp_lag_init(struct mlxsw_sp *mlxsw_sp)
2779 char slcr_pl[MLXSW_REG_SLCR_LEN];
2781 mlxsw_reg_slcr_pack(slcr_pl, MLXSW_REG_SLCR_LAG_HASH_SMAC |
2782 MLXSW_REG_SLCR_LAG_HASH_DMAC |
2783 MLXSW_REG_SLCR_LAG_HASH_ETHERTYPE |
2784 MLXSW_REG_SLCR_LAG_HASH_VLANID |
2785 MLXSW_REG_SLCR_LAG_HASH_SIP |
2786 MLXSW_REG_SLCR_LAG_HASH_DIP |
2787 MLXSW_REG_SLCR_LAG_HASH_SPORT |
2788 MLXSW_REG_SLCR_LAG_HASH_DPORT |
2789 MLXSW_REG_SLCR_LAG_HASH_IPPROTO);
2790 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcr), slcr_pl);
2793 static int mlxsw_sp_init(struct mlxsw_core *mlxsw_core,
2794 const struct mlxsw_bus_info *mlxsw_bus_info)
2796 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2799 mlxsw_sp->core = mlxsw_core;
2800 mlxsw_sp->bus_info = mlxsw_bus_info;
2801 INIT_LIST_HEAD(&mlxsw_sp->fids);
2802 INIT_LIST_HEAD(&mlxsw_sp->vfids.list);
2803 INIT_LIST_HEAD(&mlxsw_sp->br_mids.list);
2805 err = mlxsw_sp_base_mac_get(mlxsw_sp);
2807 dev_err(mlxsw_sp->bus_info->dev, "Failed to get base mac\n");
2811 err = mlxsw_sp_event_register(mlxsw_sp, MLXSW_TRAP_ID_PUDE);
2813 dev_err(mlxsw_sp->bus_info->dev, "Failed to register for PUDE events\n");
2817 err = mlxsw_sp_traps_init(mlxsw_sp);
2819 dev_err(mlxsw_sp->bus_info->dev, "Failed to set traps for RX\n");
2820 goto err_rx_listener_register;
2823 err = mlxsw_sp_flood_init(mlxsw_sp);
2825 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize flood tables\n");
2826 goto err_flood_init;
2829 err = mlxsw_sp_buffers_init(mlxsw_sp);
2831 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize buffers\n");
2832 goto err_buffers_init;
2835 err = mlxsw_sp_lag_init(mlxsw_sp);
2837 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize LAG\n");
2841 err = mlxsw_sp_switchdev_init(mlxsw_sp);
2843 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize switchdev\n");
2844 goto err_switchdev_init;
2847 err = mlxsw_sp_router_init(mlxsw_sp);
2849 dev_err(mlxsw_sp->bus_info->dev, "Failed to initialize router\n");
2850 goto err_router_init;
2853 err = mlxsw_sp_span_init(mlxsw_sp);
2855 dev_err(mlxsw_sp->bus_info->dev, "Failed to init span system\n");
2859 err = mlxsw_sp_ports_create(mlxsw_sp);
2861 dev_err(mlxsw_sp->bus_info->dev, "Failed to create ports\n");
2862 goto err_ports_create;
2868 mlxsw_sp_span_fini(mlxsw_sp);
2870 mlxsw_sp_router_fini(mlxsw_sp);
2872 mlxsw_sp_switchdev_fini(mlxsw_sp);
2875 mlxsw_sp_buffers_fini(mlxsw_sp);
2878 mlxsw_sp_traps_fini(mlxsw_sp);
2879 err_rx_listener_register:
2880 mlxsw_sp_event_unregister(mlxsw_sp, MLXSW_TRAP_ID_PUDE);
2884 static void mlxsw_sp_fini(struct mlxsw_core *mlxsw_core)
2886 struct mlxsw_sp *mlxsw_sp = mlxsw_core_driver_priv(mlxsw_core);
2889 mlxsw_sp_ports_remove(mlxsw_sp);
2890 mlxsw_sp_span_fini(mlxsw_sp);
2891 mlxsw_sp_router_fini(mlxsw_sp);
2892 mlxsw_sp_switchdev_fini(mlxsw_sp);
2893 mlxsw_sp_buffers_fini(mlxsw_sp);
2894 mlxsw_sp_traps_fini(mlxsw_sp);
2895 mlxsw_sp_event_unregister(mlxsw_sp, MLXSW_TRAP_ID_PUDE);
2896 WARN_ON(!list_empty(&mlxsw_sp->vfids.list));
2897 WARN_ON(!list_empty(&mlxsw_sp->fids));
2898 for (i = 0; i < MLXSW_SP_RIF_MAX; i++)
2899 WARN_ON_ONCE(mlxsw_sp->rifs[i]);
2902 static struct mlxsw_config_profile mlxsw_sp_config_profile = {
2903 .used_max_vepa_channels = 1,
2904 .max_vepa_channels = 0,
2906 .max_lag = MLXSW_SP_LAG_MAX,
2907 .used_max_port_per_lag = 1,
2908 .max_port_per_lag = MLXSW_SP_PORT_PER_LAG_MAX,
2910 .max_mid = MLXSW_SP_MID_MAX,
2913 .used_max_system_port = 1,
2914 .max_system_port = 64,
2915 .used_max_vlan_groups = 1,
2916 .max_vlan_groups = 127,
2917 .used_max_regions = 1,
2919 .used_flood_tables = 1,
2920 .used_flood_mode = 1,
2922 .max_fid_offset_flood_tables = 2,
2923 .fid_offset_flood_table_size = VLAN_N_VID - 1,
2924 .max_fid_flood_tables = 2,
2925 .fid_flood_table_size = MLXSW_SP_VFID_MAX,
2926 .used_max_ib_mc = 1,
2930 .used_kvd_sizes = 1,
2931 .kvd_linear_size = MLXSW_SP_KVD_LINEAR_SIZE,
2932 .kvd_hash_single_size = MLXSW_SP_KVD_HASH_SINGLE_SIZE,
2933 .kvd_hash_double_size = MLXSW_SP_KVD_HASH_DOUBLE_SIZE,
2937 .type = MLXSW_PORT_SWID_TYPE_ETH,
2940 .resource_query_enable = 1,
2943 static struct mlxsw_driver mlxsw_sp_driver = {
2944 .kind = MLXSW_DEVICE_KIND_SPECTRUM,
2945 .owner = THIS_MODULE,
2946 .priv_size = sizeof(struct mlxsw_sp),
2947 .init = mlxsw_sp_init,
2948 .fini = mlxsw_sp_fini,
2949 .port_split = mlxsw_sp_port_split,
2950 .port_unsplit = mlxsw_sp_port_unsplit,
2951 .sb_pool_get = mlxsw_sp_sb_pool_get,
2952 .sb_pool_set = mlxsw_sp_sb_pool_set,
2953 .sb_port_pool_get = mlxsw_sp_sb_port_pool_get,
2954 .sb_port_pool_set = mlxsw_sp_sb_port_pool_set,
2955 .sb_tc_pool_bind_get = mlxsw_sp_sb_tc_pool_bind_get,
2956 .sb_tc_pool_bind_set = mlxsw_sp_sb_tc_pool_bind_set,
2957 .sb_occ_snapshot = mlxsw_sp_sb_occ_snapshot,
2958 .sb_occ_max_clear = mlxsw_sp_sb_occ_max_clear,
2959 .sb_occ_port_pool_get = mlxsw_sp_sb_occ_port_pool_get,
2960 .sb_occ_tc_port_bind_get = mlxsw_sp_sb_occ_tc_port_bind_get,
2961 .txhdr_construct = mlxsw_sp_txhdr_construct,
2962 .txhdr_len = MLXSW_TXHDR_LEN,
2963 .profile = &mlxsw_sp_config_profile,
2966 static bool mlxsw_sp_port_dev_check(const struct net_device *dev)
2968 return dev->netdev_ops == &mlxsw_sp_port_netdev_ops;
2971 static struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find(struct net_device *dev)
2973 struct net_device *lower_dev;
2974 struct list_head *iter;
2976 if (mlxsw_sp_port_dev_check(dev))
2977 return netdev_priv(dev);
2979 netdev_for_each_all_lower_dev(dev, lower_dev, iter) {
2980 if (mlxsw_sp_port_dev_check(lower_dev))
2981 return netdev_priv(lower_dev);
2986 static struct mlxsw_sp *mlxsw_sp_lower_get(struct net_device *dev)
2988 struct mlxsw_sp_port *mlxsw_sp_port;
2990 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find(dev);
2991 return mlxsw_sp_port ? mlxsw_sp_port->mlxsw_sp : NULL;
2994 static struct mlxsw_sp_port *mlxsw_sp_port_dev_lower_find_rcu(struct net_device *dev)
2996 struct net_device *lower_dev;
2997 struct list_head *iter;
2999 if (mlxsw_sp_port_dev_check(dev))
3000 return netdev_priv(dev);
3002 netdev_for_each_all_lower_dev_rcu(dev, lower_dev, iter) {
3003 if (mlxsw_sp_port_dev_check(lower_dev))
3004 return netdev_priv(lower_dev);
3009 struct mlxsw_sp_port *mlxsw_sp_port_lower_dev_hold(struct net_device *dev)
3011 struct mlxsw_sp_port *mlxsw_sp_port;
3014 mlxsw_sp_port = mlxsw_sp_port_dev_lower_find_rcu(dev);
3016 dev_hold(mlxsw_sp_port->dev);
3018 return mlxsw_sp_port;
3021 void mlxsw_sp_port_dev_put(struct mlxsw_sp_port *mlxsw_sp_port)
3023 dev_put(mlxsw_sp_port->dev);
3026 static bool mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *r,
3027 unsigned long event)
3036 if (r && --r->ref_count == 0)
3038 /* It is possible we already removed the RIF ourselves
3039 * if it was assigned to a netdev that is now a bridge
3048 static int mlxsw_sp_avail_rif_get(struct mlxsw_sp *mlxsw_sp)
3052 for (i = 0; i < MLXSW_SP_RIF_MAX; i++)
3053 if (!mlxsw_sp->rifs[i])
3056 return MLXSW_SP_RIF_MAX;
3059 static void mlxsw_sp_vport_rif_sp_attr_get(struct mlxsw_sp_port *mlxsw_sp_vport,
3060 bool *p_lagged, u16 *p_system_port)
3062 u8 local_port = mlxsw_sp_vport->local_port;
3064 *p_lagged = mlxsw_sp_vport->lagged;
3065 *p_system_port = *p_lagged ? mlxsw_sp_vport->lag_id : local_port;
3068 static int mlxsw_sp_vport_rif_sp_op(struct mlxsw_sp_port *mlxsw_sp_vport,
3069 struct net_device *l3_dev, u16 rif,
3072 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3073 bool lagged = mlxsw_sp_vport->lagged;
3074 char ritr_pl[MLXSW_REG_RITR_LEN];
3077 mlxsw_reg_ritr_pack(ritr_pl, create, MLXSW_REG_RITR_SP_IF, rif,
3078 l3_dev->mtu, l3_dev->dev_addr);
3080 mlxsw_sp_vport_rif_sp_attr_get(mlxsw_sp_vport, &lagged, &system_port);
3081 mlxsw_reg_ritr_sp_if_pack(ritr_pl, lagged, system_port,
3082 mlxsw_sp_vport_vid_get(mlxsw_sp_vport));
3084 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3087 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport);
3089 static struct mlxsw_sp_fid *
3090 mlxsw_sp_rfid_alloc(u16 fid, struct net_device *l3_dev)
3092 struct mlxsw_sp_fid *f;
3094 f = kzalloc(sizeof(*f), GFP_KERNEL);
3098 f->leave = mlxsw_sp_vport_rif_sp_leave;
3106 static struct mlxsw_sp_rif *
3107 mlxsw_sp_rif_alloc(u16 rif, struct net_device *l3_dev, struct mlxsw_sp_fid *f)
3109 struct mlxsw_sp_rif *r;
3111 r = kzalloc(sizeof(*r), GFP_KERNEL);
3115 ether_addr_copy(r->addr, l3_dev->dev_addr);
3116 r->mtu = l3_dev->mtu;
3125 static struct mlxsw_sp_rif *
3126 mlxsw_sp_vport_rif_sp_create(struct mlxsw_sp_port *mlxsw_sp_vport,
3127 struct net_device *l3_dev)
3129 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3130 struct mlxsw_sp_fid *f;
3131 struct mlxsw_sp_rif *r;
3135 rif = mlxsw_sp_avail_rif_get(mlxsw_sp);
3136 if (rif == MLXSW_SP_RIF_MAX)
3137 return ERR_PTR(-ERANGE);
3139 err = mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, l3_dev, rif, true);
3141 return ERR_PTR(err);
3143 fid = mlxsw_sp_rif_sp_to_fid(rif);
3144 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, true);
3146 goto err_rif_fdb_op;
3148 f = mlxsw_sp_rfid_alloc(fid, l3_dev);
3151 goto err_rfid_alloc;
3154 r = mlxsw_sp_rif_alloc(rif, l3_dev, f);
3161 mlxsw_sp->rifs[rif] = r;
3168 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
3170 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, l3_dev, rif, false);
3171 return ERR_PTR(err);
3174 static void mlxsw_sp_vport_rif_sp_destroy(struct mlxsw_sp_port *mlxsw_sp_vport,
3175 struct mlxsw_sp_rif *r)
3177 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3178 struct net_device *l3_dev = r->dev;
3179 struct mlxsw_sp_fid *f = r->f;
3183 mlxsw_sp->rifs[rif] = NULL;
3190 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, fid, false);
3192 mlxsw_sp_vport_rif_sp_op(mlxsw_sp_vport, l3_dev, rif, false);
3195 static int mlxsw_sp_vport_rif_sp_join(struct mlxsw_sp_port *mlxsw_sp_vport,
3196 struct net_device *l3_dev)
3198 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_vport->mlxsw_sp;
3199 struct mlxsw_sp_rif *r;
3201 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
3203 r = mlxsw_sp_vport_rif_sp_create(mlxsw_sp_vport, l3_dev);
3208 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, r->f);
3211 netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", r->f->fid);
3216 static void mlxsw_sp_vport_rif_sp_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
3218 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
3220 netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid);
3222 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL);
3223 if (--f->ref_count == 0)
3224 mlxsw_sp_vport_rif_sp_destroy(mlxsw_sp_vport, f->r);
3227 static int mlxsw_sp_inetaddr_vport_event(struct net_device *l3_dev,
3228 struct net_device *port_dev,
3229 unsigned long event, u16 vid)
3231 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
3232 struct mlxsw_sp_port *mlxsw_sp_vport;
3234 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
3235 if (WARN_ON(!mlxsw_sp_vport))
3240 return mlxsw_sp_vport_rif_sp_join(mlxsw_sp_vport, l3_dev);
3242 mlxsw_sp_vport_rif_sp_leave(mlxsw_sp_vport);
3249 static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
3250 unsigned long event)
3252 if (netif_is_bridge_port(port_dev) || netif_is_lag_port(port_dev))
3255 return mlxsw_sp_inetaddr_vport_event(port_dev, port_dev, event, 1);
3258 static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
3259 struct net_device *lag_dev,
3260 unsigned long event, u16 vid)
3262 struct net_device *port_dev;
3263 struct list_head *iter;
3266 netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
3267 if (mlxsw_sp_port_dev_check(port_dev)) {
3268 err = mlxsw_sp_inetaddr_vport_event(l3_dev, port_dev,
3278 static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
3279 unsigned long event)
3281 if (netif_is_bridge_port(lag_dev))
3284 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1);
3287 static struct mlxsw_sp_fid *mlxsw_sp_bridge_fid_get(struct mlxsw_sp *mlxsw_sp,
3288 struct net_device *l3_dev)
3292 if (is_vlan_dev(l3_dev))
3293 fid = vlan_dev_vlan_id(l3_dev);
3294 else if (mlxsw_sp->master_bridge.dev == l3_dev)
3297 return mlxsw_sp_vfid_find(mlxsw_sp, l3_dev);
3299 return mlxsw_sp_fid_find(mlxsw_sp, fid);
3302 static enum mlxsw_reg_ritr_if_type mlxsw_sp_rif_type_get(u16 fid)
3304 if (mlxsw_sp_fid_is_vfid(fid))
3305 return MLXSW_REG_RITR_FID_IF;
3307 return MLXSW_REG_RITR_VLAN_IF;
3310 static int mlxsw_sp_rif_bridge_op(struct mlxsw_sp *mlxsw_sp,
3311 struct net_device *l3_dev,
3315 enum mlxsw_reg_ritr_if_type rif_type;
3316 char ritr_pl[MLXSW_REG_RITR_LEN];
3318 rif_type = mlxsw_sp_rif_type_get(fid);
3319 mlxsw_reg_ritr_pack(ritr_pl, create, rif_type, rif, l3_dev->mtu,
3321 mlxsw_reg_ritr_fid_set(ritr_pl, rif_type, fid);
3323 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3326 static int mlxsw_sp_rif_bridge_create(struct mlxsw_sp *mlxsw_sp,
3327 struct net_device *l3_dev,
3328 struct mlxsw_sp_fid *f)
3330 struct mlxsw_sp_rif *r;
3334 rif = mlxsw_sp_avail_rif_get(mlxsw_sp);
3335 if (rif == MLXSW_SP_RIF_MAX)
3338 err = mlxsw_sp_rif_bridge_op(mlxsw_sp, l3_dev, f->fid, rif, true);
3342 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, true);
3344 goto err_rif_fdb_op;
3346 r = mlxsw_sp_rif_alloc(rif, l3_dev, f);
3353 mlxsw_sp->rifs[rif] = r;
3355 netdev_dbg(l3_dev, "RIF=%d created\n", rif);
3360 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3362 mlxsw_sp_rif_bridge_op(mlxsw_sp, l3_dev, f->fid, rif, false);
3366 void mlxsw_sp_rif_bridge_destroy(struct mlxsw_sp *mlxsw_sp,
3367 struct mlxsw_sp_rif *r)
3369 struct net_device *l3_dev = r->dev;
3370 struct mlxsw_sp_fid *f = r->f;
3373 mlxsw_sp->rifs[rif] = NULL;
3378 mlxsw_sp_rif_fdb_op(mlxsw_sp, l3_dev->dev_addr, f->fid, false);
3380 mlxsw_sp_rif_bridge_op(mlxsw_sp, l3_dev, f->fid, rif, false);
3382 netdev_dbg(l3_dev, "RIF=%d destroyed\n", rif);
3385 static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
3386 struct net_device *br_dev,
3387 unsigned long event)
3389 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
3390 struct mlxsw_sp_fid *f;
3392 /* FID can either be an actual FID if the L3 device is the
3393 * VLAN-aware bridge or a VLAN device on top. Otherwise, the
3394 * L3 device is a VLAN-unaware bridge and we get a vFID.
3396 f = mlxsw_sp_bridge_fid_get(mlxsw_sp, l3_dev);
3402 return mlxsw_sp_rif_bridge_create(mlxsw_sp, l3_dev, f);
3404 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
3411 static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
3412 unsigned long event)
3414 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
3415 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(vlan_dev);
3416 u16 vid = vlan_dev_vlan_id(vlan_dev);
3418 if (mlxsw_sp_port_dev_check(real_dev))
3419 return mlxsw_sp_inetaddr_vport_event(vlan_dev, real_dev, event,
3421 else if (netif_is_lag_master(real_dev))
3422 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
3424 else if (netif_is_bridge_master(real_dev) &&
3425 mlxsw_sp->master_bridge.dev == real_dev)
3426 return mlxsw_sp_inetaddr_bridge_event(vlan_dev, real_dev,
3432 static int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
3433 unsigned long event, void *ptr)
3435 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
3436 struct net_device *dev = ifa->ifa_dev->dev;
3437 struct mlxsw_sp *mlxsw_sp;
3438 struct mlxsw_sp_rif *r;
3441 mlxsw_sp = mlxsw_sp_lower_get(dev);
3445 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3446 if (!mlxsw_sp_rif_should_config(r, event))
3449 if (mlxsw_sp_port_dev_check(dev))
3450 err = mlxsw_sp_inetaddr_port_event(dev, event);
3451 else if (netif_is_lag_master(dev))
3452 err = mlxsw_sp_inetaddr_lag_event(dev, event);
3453 else if (netif_is_bridge_master(dev))
3454 err = mlxsw_sp_inetaddr_bridge_event(dev, dev, event);
3455 else if (is_vlan_dev(dev))
3456 err = mlxsw_sp_inetaddr_vlan_event(dev, event);
3459 return notifier_from_errno(err);
3462 static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif,
3463 const char *mac, int mtu)
3465 char ritr_pl[MLXSW_REG_RITR_LEN];
3468 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
3469 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3473 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
3474 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
3475 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
3476 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
3479 static int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
3481 struct mlxsw_sp *mlxsw_sp;
3482 struct mlxsw_sp_rif *r;
3485 mlxsw_sp = mlxsw_sp_lower_get(dev);
3489 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3493 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, r->addr, r->f->fid, false);
3497 err = mlxsw_sp_rif_edit(mlxsw_sp, r->rif, dev->dev_addr, dev->mtu);
3501 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, r->f->fid, true);
3503 goto err_rif_fdb_op;
3505 ether_addr_copy(r->addr, dev->dev_addr);
3508 netdev_dbg(dev, "Updated RIF=%d\n", r->rif);
3513 mlxsw_sp_rif_edit(mlxsw_sp, r->rif, r->addr, r->mtu);
3515 mlxsw_sp_rif_fdb_op(mlxsw_sp, r->addr, r->f->fid, true);
3519 static bool mlxsw_sp_lag_port_fid_member(struct mlxsw_sp_port *lag_port,
3522 if (mlxsw_sp_fid_is_vfid(fid))
3523 return mlxsw_sp_port_vport_find_by_fid(lag_port, fid);
3525 return test_bit(fid, lag_port->active_vlans);
3528 static bool mlxsw_sp_port_fdb_should_flush(struct mlxsw_sp_port *mlxsw_sp_port,
3531 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3532 u8 local_port = mlxsw_sp_port->local_port;
3533 u16 lag_id = mlxsw_sp_port->lag_id;
3536 if (!mlxsw_sp_port->lagged)
3539 for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) {
3540 struct mlxsw_sp_port *lag_port;
3542 lag_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
3543 if (!lag_port || lag_port->local_port == local_port)
3545 if (mlxsw_sp_lag_port_fid_member(lag_port, fid))
3553 mlxsw_sp_port_fdb_flush_by_port_fid(const struct mlxsw_sp_port *mlxsw_sp_port,
3556 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3557 char sfdf_pl[MLXSW_REG_SFDF_LEN];
3559 mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_PORT_AND_FID);
3560 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid);
3561 mlxsw_reg_sfdf_port_fid_system_port_set(sfdf_pl,
3562 mlxsw_sp_port->local_port);
3564 netdev_dbg(mlxsw_sp_port->dev, "FDB flushed using Port=%d, FID=%d\n",
3565 mlxsw_sp_port->local_port, fid);
3567 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
3571 mlxsw_sp_port_fdb_flush_by_lag_id_fid(const struct mlxsw_sp_port *mlxsw_sp_port,
3574 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3575 char sfdf_pl[MLXSW_REG_SFDF_LEN];
3577 mlxsw_reg_sfdf_pack(sfdf_pl, MLXSW_REG_SFDF_FLUSH_PER_LAG_AND_FID);
3578 mlxsw_reg_sfdf_fid_set(sfdf_pl, fid);
3579 mlxsw_reg_sfdf_lag_fid_lag_id_set(sfdf_pl, mlxsw_sp_port->lag_id);
3581 netdev_dbg(mlxsw_sp_port->dev, "FDB flushed using LAG ID=%d, FID=%d\n",
3582 mlxsw_sp_port->lag_id, fid);
3584 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdf), sfdf_pl);
3587 int mlxsw_sp_port_fdb_flush(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
3589 if (!mlxsw_sp_port_fdb_should_flush(mlxsw_sp_port, fid))
3592 if (mlxsw_sp_port->lagged)
3593 return mlxsw_sp_port_fdb_flush_by_lag_id_fid(mlxsw_sp_port,
3596 return mlxsw_sp_port_fdb_flush_by_port_fid(mlxsw_sp_port, fid);
3599 static void mlxsw_sp_master_bridge_gone_sync(struct mlxsw_sp *mlxsw_sp)
3601 struct mlxsw_sp_fid *f, *tmp;
3603 list_for_each_entry_safe(f, tmp, &mlxsw_sp->fids, list)
3604 if (--f->ref_count == 0)
3605 mlxsw_sp_fid_destroy(mlxsw_sp, f);
3610 static bool mlxsw_sp_master_bridge_check(struct mlxsw_sp *mlxsw_sp,
3611 struct net_device *br_dev)
3613 return !mlxsw_sp->master_bridge.dev ||
3614 mlxsw_sp->master_bridge.dev == br_dev;
3617 static void mlxsw_sp_master_bridge_inc(struct mlxsw_sp *mlxsw_sp,
3618 struct net_device *br_dev)
3620 mlxsw_sp->master_bridge.dev = br_dev;
3621 mlxsw_sp->master_bridge.ref_count++;
3624 static void mlxsw_sp_master_bridge_dec(struct mlxsw_sp *mlxsw_sp)
3626 if (--mlxsw_sp->master_bridge.ref_count == 0) {
3627 mlxsw_sp->master_bridge.dev = NULL;
3628 /* It's possible upper VLAN devices are still holding
3629 * references to underlying FIDs. Drop the reference
3630 * and release the resources if it was the last one.
3631 * If it wasn't, then something bad happened.
3633 mlxsw_sp_master_bridge_gone_sync(mlxsw_sp);
3637 static int mlxsw_sp_port_bridge_join(struct mlxsw_sp_port *mlxsw_sp_port,
3638 struct net_device *br_dev)
3640 struct net_device *dev = mlxsw_sp_port->dev;
3643 /* When port is not bridged untagged packets are tagged with
3644 * PVID=VID=1, thereby creating an implicit VLAN interface in
3645 * the device. Remove it and let bridge code take care of its
3648 err = mlxsw_sp_port_kill_vid(dev, 0, 1);
3652 mlxsw_sp_master_bridge_inc(mlxsw_sp_port->mlxsw_sp, br_dev);
3654 mlxsw_sp_port->learning = 1;
3655 mlxsw_sp_port->learning_sync = 1;
3656 mlxsw_sp_port->uc_flood = 1;
3657 mlxsw_sp_port->bridged = 1;
3662 static void mlxsw_sp_port_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_port)
3664 struct net_device *dev = mlxsw_sp_port->dev;
3666 mlxsw_sp_port_pvid_set(mlxsw_sp_port, 1);
3668 mlxsw_sp_master_bridge_dec(mlxsw_sp_port->mlxsw_sp);
3670 mlxsw_sp_port->learning = 0;
3671 mlxsw_sp_port->learning_sync = 0;
3672 mlxsw_sp_port->uc_flood = 0;
3673 mlxsw_sp_port->bridged = 0;
3675 /* Add implicit VLAN interface in the device, so that untagged
3676 * packets will be classified to the default vFID.
3678 mlxsw_sp_port_add_vid(dev, 0, 1);
3681 static int mlxsw_sp_lag_create(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
3683 char sldr_pl[MLXSW_REG_SLDR_LEN];
3685 mlxsw_reg_sldr_lag_create_pack(sldr_pl, lag_id);
3686 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
3689 static int mlxsw_sp_lag_destroy(struct mlxsw_sp *mlxsw_sp, u16 lag_id)
3691 char sldr_pl[MLXSW_REG_SLDR_LEN];
3693 mlxsw_reg_sldr_lag_destroy_pack(sldr_pl, lag_id);
3694 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
3697 static int mlxsw_sp_lag_col_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
3698 u16 lag_id, u8 port_index)
3700 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3701 char slcor_pl[MLXSW_REG_SLCOR_LEN];
3703 mlxsw_reg_slcor_port_add_pack(slcor_pl, mlxsw_sp_port->local_port,
3704 lag_id, port_index);
3705 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
3708 static int mlxsw_sp_lag_col_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
3711 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3712 char slcor_pl[MLXSW_REG_SLCOR_LEN];
3714 mlxsw_reg_slcor_port_remove_pack(slcor_pl, mlxsw_sp_port->local_port,
3716 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
3719 static int mlxsw_sp_lag_col_port_enable(struct mlxsw_sp_port *mlxsw_sp_port,
3722 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3723 char slcor_pl[MLXSW_REG_SLCOR_LEN];
3725 mlxsw_reg_slcor_col_enable_pack(slcor_pl, mlxsw_sp_port->local_port,
3727 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
3730 static int mlxsw_sp_lag_col_port_disable(struct mlxsw_sp_port *mlxsw_sp_port,
3733 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3734 char slcor_pl[MLXSW_REG_SLCOR_LEN];
3736 mlxsw_reg_slcor_col_disable_pack(slcor_pl, mlxsw_sp_port->local_port,
3738 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(slcor), slcor_pl);
3741 static int mlxsw_sp_lag_index_get(struct mlxsw_sp *mlxsw_sp,
3742 struct net_device *lag_dev,
3745 struct mlxsw_sp_upper *lag;
3746 int free_lag_id = -1;
3749 for (i = 0; i < MLXSW_SP_LAG_MAX; i++) {
3750 lag = mlxsw_sp_lag_get(mlxsw_sp, i);
3751 if (lag->ref_count) {
3752 if (lag->dev == lag_dev) {
3756 } else if (free_lag_id < 0) {
3760 if (free_lag_id < 0)
3762 *p_lag_id = free_lag_id;
3767 mlxsw_sp_master_lag_check(struct mlxsw_sp *mlxsw_sp,
3768 struct net_device *lag_dev,
3769 struct netdev_lag_upper_info *lag_upper_info)
3773 if (mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id) != 0)
3775 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH)
3780 static int mlxsw_sp_port_lag_index_get(struct mlxsw_sp *mlxsw_sp,
3781 u16 lag_id, u8 *p_port_index)
3785 for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) {
3786 if (!mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i)) {
3795 mlxsw_sp_port_pvid_vport_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
3798 struct mlxsw_sp_port *mlxsw_sp_vport;
3799 struct mlxsw_sp_fid *f;
3801 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1);
3802 if (WARN_ON(!mlxsw_sp_vport))
3805 /* If vPort is assigned a RIF, then leave it since it's no
3808 f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
3810 f->leave(mlxsw_sp_vport);
3812 mlxsw_sp_vport->lag_id = lag_id;
3813 mlxsw_sp_vport->lagged = 1;
3817 mlxsw_sp_port_pvid_vport_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port)
3819 struct mlxsw_sp_port *mlxsw_sp_vport;
3820 struct mlxsw_sp_fid *f;
3822 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, 1);
3823 if (WARN_ON(!mlxsw_sp_vport))
3826 f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
3828 f->leave(mlxsw_sp_vport);
3830 mlxsw_sp_vport->lagged = 0;
3833 static int mlxsw_sp_port_lag_join(struct mlxsw_sp_port *mlxsw_sp_port,
3834 struct net_device *lag_dev)
3836 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3837 struct mlxsw_sp_upper *lag;
3842 err = mlxsw_sp_lag_index_get(mlxsw_sp, lag_dev, &lag_id);
3845 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
3846 if (!lag->ref_count) {
3847 err = mlxsw_sp_lag_create(mlxsw_sp, lag_id);
3853 err = mlxsw_sp_port_lag_index_get(mlxsw_sp, lag_id, &port_index);
3856 err = mlxsw_sp_lag_col_port_add(mlxsw_sp_port, lag_id, port_index);
3858 goto err_col_port_add;
3859 err = mlxsw_sp_lag_col_port_enable(mlxsw_sp_port, lag_id);
3861 goto err_col_port_enable;
3863 mlxsw_core_lag_mapping_set(mlxsw_sp->core, lag_id, port_index,
3864 mlxsw_sp_port->local_port);
3865 mlxsw_sp_port->lag_id = lag_id;
3866 mlxsw_sp_port->lagged = 1;
3869 mlxsw_sp_port_pvid_vport_lag_join(mlxsw_sp_port, lag_id);
3873 err_col_port_enable:
3874 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
3876 if (!lag->ref_count)
3877 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
3881 static void mlxsw_sp_port_lag_leave(struct mlxsw_sp_port *mlxsw_sp_port,
3882 struct net_device *lag_dev)
3884 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3885 u16 lag_id = mlxsw_sp_port->lag_id;
3886 struct mlxsw_sp_upper *lag;
3888 if (!mlxsw_sp_port->lagged)
3890 lag = mlxsw_sp_lag_get(mlxsw_sp, lag_id);
3891 WARN_ON(lag->ref_count == 0);
3893 mlxsw_sp_lag_col_port_disable(mlxsw_sp_port, lag_id);
3894 mlxsw_sp_lag_col_port_remove(mlxsw_sp_port, lag_id);
3896 if (mlxsw_sp_port->bridged) {
3897 mlxsw_sp_port_active_vlans_del(mlxsw_sp_port);
3898 mlxsw_sp_port_bridge_leave(mlxsw_sp_port);
3901 if (lag->ref_count == 1)
3902 mlxsw_sp_lag_destroy(mlxsw_sp, lag_id);
3904 mlxsw_core_lag_mapping_clear(mlxsw_sp->core, lag_id,
3905 mlxsw_sp_port->local_port);
3906 mlxsw_sp_port->lagged = 0;
3909 mlxsw_sp_port_pvid_vport_lag_leave(mlxsw_sp_port);
3912 static int mlxsw_sp_lag_dist_port_add(struct mlxsw_sp_port *mlxsw_sp_port,
3915 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3916 char sldr_pl[MLXSW_REG_SLDR_LEN];
3918 mlxsw_reg_sldr_lag_add_port_pack(sldr_pl, lag_id,
3919 mlxsw_sp_port->local_port);
3920 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
3923 static int mlxsw_sp_lag_dist_port_remove(struct mlxsw_sp_port *mlxsw_sp_port,
3926 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3927 char sldr_pl[MLXSW_REG_SLDR_LEN];
3929 mlxsw_reg_sldr_lag_remove_port_pack(sldr_pl, lag_id,
3930 mlxsw_sp_port->local_port);
3931 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sldr), sldr_pl);
3934 static int mlxsw_sp_port_lag_tx_en_set(struct mlxsw_sp_port *mlxsw_sp_port,
3935 bool lag_tx_enabled)
3938 return mlxsw_sp_lag_dist_port_add(mlxsw_sp_port,
3939 mlxsw_sp_port->lag_id);
3941 return mlxsw_sp_lag_dist_port_remove(mlxsw_sp_port,
3942 mlxsw_sp_port->lag_id);
3945 static int mlxsw_sp_port_lag_changed(struct mlxsw_sp_port *mlxsw_sp_port,
3946 struct netdev_lag_lower_state_info *info)
3948 return mlxsw_sp_port_lag_tx_en_set(mlxsw_sp_port, info->tx_enabled);
3951 static int mlxsw_sp_port_vlan_link(struct mlxsw_sp_port *mlxsw_sp_port,
3952 struct net_device *vlan_dev)
3954 struct mlxsw_sp_port *mlxsw_sp_vport;
3955 u16 vid = vlan_dev_vlan_id(vlan_dev);
3957 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
3958 if (WARN_ON(!mlxsw_sp_vport))
3961 mlxsw_sp_vport->dev = vlan_dev;
3966 static void mlxsw_sp_port_vlan_unlink(struct mlxsw_sp_port *mlxsw_sp_port,
3967 struct net_device *vlan_dev)
3969 struct mlxsw_sp_port *mlxsw_sp_vport;
3970 u16 vid = vlan_dev_vlan_id(vlan_dev);
3972 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
3973 if (WARN_ON(!mlxsw_sp_vport))
3976 mlxsw_sp_vport->dev = mlxsw_sp_port->dev;
3979 static int mlxsw_sp_netdevice_port_upper_event(struct net_device *dev,
3980 unsigned long event, void *ptr)
3982 struct netdev_notifier_changeupper_info *info;
3983 struct mlxsw_sp_port *mlxsw_sp_port;
3984 struct net_device *upper_dev;
3985 struct mlxsw_sp *mlxsw_sp;
3988 mlxsw_sp_port = netdev_priv(dev);
3989 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
3993 case NETDEV_PRECHANGEUPPER:
3994 upper_dev = info->upper_dev;
3995 if (!is_vlan_dev(upper_dev) &&
3996 !netif_is_lag_master(upper_dev) &&
3997 !netif_is_bridge_master(upper_dev))
4001 /* HW limitation forbids to put ports to multiple bridges. */
4002 if (netif_is_bridge_master(upper_dev) &&
4003 !mlxsw_sp_master_bridge_check(mlxsw_sp, upper_dev))
4005 if (netif_is_lag_master(upper_dev) &&
4006 !mlxsw_sp_master_lag_check(mlxsw_sp, upper_dev,
4009 if (netif_is_lag_master(upper_dev) && vlan_uses_dev(dev))
4011 if (netif_is_lag_port(dev) && is_vlan_dev(upper_dev) &&
4012 !netif_is_lag_master(vlan_dev_real_dev(upper_dev)))
4015 case NETDEV_CHANGEUPPER:
4016 upper_dev = info->upper_dev;
4017 if (is_vlan_dev(upper_dev)) {
4019 err = mlxsw_sp_port_vlan_link(mlxsw_sp_port,
4022 mlxsw_sp_port_vlan_unlink(mlxsw_sp_port,
4024 } else if (netif_is_bridge_master(upper_dev)) {
4026 err = mlxsw_sp_port_bridge_join(mlxsw_sp_port,
4029 mlxsw_sp_port_bridge_leave(mlxsw_sp_port);
4030 } else if (netif_is_lag_master(upper_dev)) {
4032 err = mlxsw_sp_port_lag_join(mlxsw_sp_port,
4035 mlxsw_sp_port_lag_leave(mlxsw_sp_port,
4047 static int mlxsw_sp_netdevice_port_lower_event(struct net_device *dev,
4048 unsigned long event, void *ptr)
4050 struct netdev_notifier_changelowerstate_info *info;
4051 struct mlxsw_sp_port *mlxsw_sp_port;
4054 mlxsw_sp_port = netdev_priv(dev);
4058 case NETDEV_CHANGELOWERSTATE:
4059 if (netif_is_lag_port(dev) && mlxsw_sp_port->lagged) {
4060 err = mlxsw_sp_port_lag_changed(mlxsw_sp_port,
4061 info->lower_state_info);
4063 netdev_err(dev, "Failed to reflect link aggregation lower state change\n");
4071 static int mlxsw_sp_netdevice_port_event(struct net_device *dev,
4072 unsigned long event, void *ptr)
4075 case NETDEV_PRECHANGEUPPER:
4076 case NETDEV_CHANGEUPPER:
4077 return mlxsw_sp_netdevice_port_upper_event(dev, event, ptr);
4078 case NETDEV_CHANGELOWERSTATE:
4079 return mlxsw_sp_netdevice_port_lower_event(dev, event, ptr);
4085 static int mlxsw_sp_netdevice_lag_event(struct net_device *lag_dev,
4086 unsigned long event, void *ptr)
4088 struct net_device *dev;
4089 struct list_head *iter;
4092 netdev_for_each_lower_dev(lag_dev, dev, iter) {
4093 if (mlxsw_sp_port_dev_check(dev)) {
4094 ret = mlxsw_sp_netdevice_port_event(dev, event, ptr);
4103 static int mlxsw_sp_master_bridge_vlan_link(struct mlxsw_sp *mlxsw_sp,
4104 struct net_device *vlan_dev)
4106 u16 fid = vlan_dev_vlan_id(vlan_dev);
4107 struct mlxsw_sp_fid *f;
4109 f = mlxsw_sp_fid_find(mlxsw_sp, fid);
4111 f = mlxsw_sp_fid_create(mlxsw_sp, fid);
4121 static void mlxsw_sp_master_bridge_vlan_unlink(struct mlxsw_sp *mlxsw_sp,
4122 struct net_device *vlan_dev)
4124 u16 fid = vlan_dev_vlan_id(vlan_dev);
4125 struct mlxsw_sp_fid *f;
4127 f = mlxsw_sp_fid_find(mlxsw_sp, fid);
4129 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
4130 if (f && --f->ref_count == 0)
4131 mlxsw_sp_fid_destroy(mlxsw_sp, f);
4134 static int mlxsw_sp_netdevice_bridge_event(struct net_device *br_dev,
4135 unsigned long event, void *ptr)
4137 struct netdev_notifier_changeupper_info *info;
4138 struct net_device *upper_dev;
4139 struct mlxsw_sp *mlxsw_sp;
4142 mlxsw_sp = mlxsw_sp_lower_get(br_dev);
4145 if (br_dev != mlxsw_sp->master_bridge.dev)
4151 case NETDEV_CHANGEUPPER:
4152 upper_dev = info->upper_dev;
4153 if (!is_vlan_dev(upper_dev))
4155 if (info->linking) {
4156 err = mlxsw_sp_master_bridge_vlan_link(mlxsw_sp,
4161 mlxsw_sp_master_bridge_vlan_unlink(mlxsw_sp, upper_dev);
4169 static u16 mlxsw_sp_avail_vfid_get(const struct mlxsw_sp *mlxsw_sp)
4171 return find_first_zero_bit(mlxsw_sp->vfids.mapped,
4175 static int mlxsw_sp_vfid_op(struct mlxsw_sp *mlxsw_sp, u16 fid, bool create)
4177 char sfmr_pl[MLXSW_REG_SFMR_LEN];
4179 mlxsw_reg_sfmr_pack(sfmr_pl, !create, fid, 0);
4180 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
4183 static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port *mlxsw_sp_vport);
4185 static struct mlxsw_sp_fid *mlxsw_sp_vfid_create(struct mlxsw_sp *mlxsw_sp,
4186 struct net_device *br_dev)
4188 struct device *dev = mlxsw_sp->bus_info->dev;
4189 struct mlxsw_sp_fid *f;
4193 vfid = mlxsw_sp_avail_vfid_get(mlxsw_sp);
4194 if (vfid == MLXSW_SP_VFID_MAX) {
4195 dev_err(dev, "No available vFIDs\n");
4196 return ERR_PTR(-ERANGE);
4199 fid = mlxsw_sp_vfid_to_fid(vfid);
4200 err = mlxsw_sp_vfid_op(mlxsw_sp, fid, true);
4202 dev_err(dev, "Failed to create FID=%d\n", fid);
4203 return ERR_PTR(err);
4206 f = kzalloc(sizeof(*f), GFP_KERNEL);
4208 goto err_allocate_vfid;
4210 f->leave = mlxsw_sp_vport_vfid_leave;
4214 list_add(&f->list, &mlxsw_sp->vfids.list);
4215 set_bit(vfid, mlxsw_sp->vfids.mapped);
4220 mlxsw_sp_vfid_op(mlxsw_sp, fid, false);
4221 return ERR_PTR(-ENOMEM);
4224 static void mlxsw_sp_vfid_destroy(struct mlxsw_sp *mlxsw_sp,
4225 struct mlxsw_sp_fid *f)
4227 u16 vfid = mlxsw_sp_fid_to_vfid(f->fid);
4230 clear_bit(vfid, mlxsw_sp->vfids.mapped);
4234 mlxsw_sp_rif_bridge_destroy(mlxsw_sp, f->r);
4238 mlxsw_sp_vfid_op(mlxsw_sp, fid, false);
4241 static int mlxsw_sp_vport_fid_map(struct mlxsw_sp_port *mlxsw_sp_vport, u16 fid,
4244 enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
4245 u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
4247 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_vport, mt, valid, fid,
4251 static int mlxsw_sp_vport_vfid_join(struct mlxsw_sp_port *mlxsw_sp_vport,
4252 struct net_device *br_dev)
4254 struct mlxsw_sp_fid *f;
4257 f = mlxsw_sp_vfid_find(mlxsw_sp_vport->mlxsw_sp, br_dev);
4259 f = mlxsw_sp_vfid_create(mlxsw_sp_vport->mlxsw_sp, br_dev);
4264 err = mlxsw_sp_vport_flood_set(mlxsw_sp_vport, f->fid, true);
4266 goto err_vport_flood_set;
4268 err = mlxsw_sp_vport_fid_map(mlxsw_sp_vport, f->fid, true);
4270 goto err_vport_fid_map;
4272 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, f);
4275 netdev_dbg(mlxsw_sp_vport->dev, "Joined FID=%d\n", f->fid);
4280 mlxsw_sp_vport_flood_set(mlxsw_sp_vport, f->fid, false);
4281 err_vport_flood_set:
4283 mlxsw_sp_vfid_destroy(mlxsw_sp_vport->mlxsw_sp, f);
4287 static void mlxsw_sp_vport_vfid_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
4289 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
4291 netdev_dbg(mlxsw_sp_vport->dev, "Left FID=%d\n", f->fid);
4293 mlxsw_sp_vport_fid_map(mlxsw_sp_vport, f->fid, false);
4295 mlxsw_sp_vport_flood_set(mlxsw_sp_vport, f->fid, false);
4297 mlxsw_sp_port_fdb_flush(mlxsw_sp_vport, f->fid);
4299 mlxsw_sp_vport_fid_set(mlxsw_sp_vport, NULL);
4300 if (--f->ref_count == 0)
4301 mlxsw_sp_vfid_destroy(mlxsw_sp_vport->mlxsw_sp, f);
4304 static int mlxsw_sp_vport_bridge_join(struct mlxsw_sp_port *mlxsw_sp_vport,
4305 struct net_device *br_dev)
4307 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_vport);
4308 u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
4309 struct net_device *dev = mlxsw_sp_vport->dev;
4312 if (f && !WARN_ON(!f->leave))
4313 f->leave(mlxsw_sp_vport);
4315 err = mlxsw_sp_vport_vfid_join(mlxsw_sp_vport, br_dev);
4317 netdev_err(dev, "Failed to join vFID\n");
4321 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, true);
4323 netdev_err(dev, "Failed to enable learning\n");
4324 goto err_port_vid_learning_set;
4327 mlxsw_sp_vport->learning = 1;
4328 mlxsw_sp_vport->learning_sync = 1;
4329 mlxsw_sp_vport->uc_flood = 1;
4330 mlxsw_sp_vport->bridged = 1;
4334 err_port_vid_learning_set:
4335 mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport);
4339 static void mlxsw_sp_vport_bridge_leave(struct mlxsw_sp_port *mlxsw_sp_vport)
4341 u16 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
4343 mlxsw_sp_port_vid_learning_set(mlxsw_sp_vport, vid, false);
4345 mlxsw_sp_vport_vfid_leave(mlxsw_sp_vport);
4347 mlxsw_sp_vport->learning = 0;
4348 mlxsw_sp_vport->learning_sync = 0;
4349 mlxsw_sp_vport->uc_flood = 0;
4350 mlxsw_sp_vport->bridged = 0;
4354 mlxsw_sp_port_master_bridge_check(const struct mlxsw_sp_port *mlxsw_sp_port,
4355 const struct net_device *br_dev)
4357 struct mlxsw_sp_port *mlxsw_sp_vport;
4359 list_for_each_entry(mlxsw_sp_vport, &mlxsw_sp_port->vports_list,
4361 struct net_device *dev = mlxsw_sp_vport_dev_get(mlxsw_sp_vport);
4363 if (dev && dev == br_dev)
4370 static int mlxsw_sp_netdevice_vport_event(struct net_device *dev,
4371 unsigned long event, void *ptr,
4374 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
4375 struct netdev_notifier_changeupper_info *info = ptr;
4376 struct mlxsw_sp_port *mlxsw_sp_vport;
4377 struct net_device *upper_dev;
4380 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
4383 case NETDEV_PRECHANGEUPPER:
4384 upper_dev = info->upper_dev;
4385 if (!netif_is_bridge_master(upper_dev))
4389 /* We can't have multiple VLAN interfaces configured on
4390 * the same port and being members in the same bridge.
4392 if (!mlxsw_sp_port_master_bridge_check(mlxsw_sp_port,
4396 case NETDEV_CHANGEUPPER:
4397 upper_dev = info->upper_dev;
4398 if (info->linking) {
4399 if (WARN_ON(!mlxsw_sp_vport))
4401 err = mlxsw_sp_vport_bridge_join(mlxsw_sp_vport,
4404 if (!mlxsw_sp_vport)
4406 mlxsw_sp_vport_bridge_leave(mlxsw_sp_vport);
4413 static int mlxsw_sp_netdevice_lag_vport_event(struct net_device *lag_dev,
4414 unsigned long event, void *ptr,
4417 struct net_device *dev;
4418 struct list_head *iter;
4421 netdev_for_each_lower_dev(lag_dev, dev, iter) {
4422 if (mlxsw_sp_port_dev_check(dev)) {
4423 ret = mlxsw_sp_netdevice_vport_event(dev, event, ptr,
4433 static int mlxsw_sp_netdevice_vlan_event(struct net_device *vlan_dev,
4434 unsigned long event, void *ptr)
4436 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
4437 u16 vid = vlan_dev_vlan_id(vlan_dev);
4439 if (mlxsw_sp_port_dev_check(real_dev))
4440 return mlxsw_sp_netdevice_vport_event(real_dev, event, ptr,
4442 else if (netif_is_lag_master(real_dev))
4443 return mlxsw_sp_netdevice_lag_vport_event(real_dev, event, ptr,
4449 static int mlxsw_sp_netdevice_event(struct notifier_block *unused,
4450 unsigned long event, void *ptr)
4452 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4455 if (event == NETDEV_CHANGEADDR || event == NETDEV_CHANGEMTU)
4456 err = mlxsw_sp_netdevice_router_port_event(dev);
4457 else if (mlxsw_sp_port_dev_check(dev))
4458 err = mlxsw_sp_netdevice_port_event(dev, event, ptr);
4459 else if (netif_is_lag_master(dev))
4460 err = mlxsw_sp_netdevice_lag_event(dev, event, ptr);
4461 else if (netif_is_bridge_master(dev))
4462 err = mlxsw_sp_netdevice_bridge_event(dev, event, ptr);
4463 else if (is_vlan_dev(dev))
4464 err = mlxsw_sp_netdevice_vlan_event(dev, event, ptr);
4466 return notifier_from_errno(err);
4469 static struct notifier_block mlxsw_sp_netdevice_nb __read_mostly = {
4470 .notifier_call = mlxsw_sp_netdevice_event,
4473 static struct notifier_block mlxsw_sp_inetaddr_nb __read_mostly = {
4474 .notifier_call = mlxsw_sp_inetaddr_event,
4475 .priority = 10, /* Must be called before FIB notifier block */
4478 static int __init mlxsw_sp_module_init(void)
4482 register_netdevice_notifier(&mlxsw_sp_netdevice_nb);
4483 register_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
4484 err = mlxsw_core_driver_register(&mlxsw_sp_driver);
4486 goto err_core_driver_register;
4489 err_core_driver_register:
4490 unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb);
4494 static void __exit mlxsw_sp_module_exit(void)
4496 mlxsw_core_driver_unregister(&mlxsw_sp_driver);
4497 unregister_inetaddr_notifier(&mlxsw_sp_inetaddr_nb);
4498 unregister_netdevice_notifier(&mlxsw_sp_netdevice_nb);
4501 module_init(mlxsw_sp_module_init);
4502 module_exit(mlxsw_sp_module_exit);
4504 MODULE_LICENSE("Dual BSD/GPL");
4505 MODULE_AUTHOR("Jiri Pirko <jiri@mellanox.com>");
4506 MODULE_DESCRIPTION("Mellanox Spectrum driver");
4507 MODULE_MLXSW_DRIVER_ALIAS(MLXSW_DEVICE_KIND_SPECTRUM);