From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-vk1-f179.google.com (mail-vk1-f179.google.com [209.85.221.179]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 41EAF3093B8 for ; Sat, 23 May 2026 05:56:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.179 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779515763; cv=none; b=haqUTjOt3WIJuYI9oHdFBuOWVRY5nPiZa04UPElYnaXyRpRvTTPn4rprRgG0Qt2mOZkdZLvJg+2LhSq8x1AFwlGAGMasSE4EjgFTJPzWaoLk+W0YYZgX5F+f+5RyL5oH21EtP2ibsrrgd9FMBa+qryTAUovZsOmk57EOVuADtEs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779515763; c=relaxed/simple; bh=D1wj/JFTaFjBniNMTmZjoOwzM5LCRO5q0kzkuvfbs7g=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=JMAUz73/iJEynncwvb1b/OIEXpu2y16DPWEPVITIbWUTuEFBWErBagVBEP7gop9aTLc3pBYna3JE3yEGzTujaNV+viY0nufgB/OV2cRvFF7qOJGL/7z3xSDNEX3j37rzs6cpOKVbvyaPJ6YUrRBfUb4tJgyqjewNzY1LawDcPuQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=gfShCqQ2; arc=none smtp.client-ip=209.85.221.179 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gfShCqQ2" Received: by mail-vk1-f179.google.com with SMTP id 71dfb90a1353d-575212eb419so5859206e0c.1 for ; Fri, 22 May 2026 22:56:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1779515760; x=1780120560; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=aYHIcd4bbGqwm2U+v3yuHG+2tAyqIOBeGKeTJonOCew=; b=gfShCqQ2o84jT1g1bTQX1jhNXA/uZKidFandMIPXWzIZRRe8YVL/8fcF3QF4NN0N0s KZRfFMJzo7ufDY3Rr/zL/dGDoJJtq/Plje6mCmGdSlgK2BL2WEgzPiNWBvRBKKGEXjsY PCEyRYYJQyI+og/MLBadadGnhSp6Nr5oVvBlNtp8nib6n9kC5kcKs1vrJAQXxazTqhIE sFH/mfABkPX2v/A9xWtPKz7/FXCEgDCLoT9QZ0GYjMgvmELns3OvB3XLt96zIDoWBA01 xOSRpYEF5lcE8l8P7riKTGgOR0RQI7JU/hpYlo0u5EDDteCersPDhevEY9TOJoij/28Z rbXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779515760; x=1780120560; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=aYHIcd4bbGqwm2U+v3yuHG+2tAyqIOBeGKeTJonOCew=; b=oRRahLmizuCQ/+YK2BLcSdNVqSbqj+8dkiTvZfXhFgrAu7MqSkHnQGv00FsbE3sNDI +iQhYCJ5+wpsSoQxFXCooR5iYbdTrYwNItgQ2d1GpfpcR432fGVByHYv69+1lrZLoZb3 3xv1cZYuSuGatH3OGw5BbngKizXKTM5fns+htSfulX7/Ay6gUpfavp8Zjo9XzrtWuzCC qw+YeKQR14+m+XX+yv9W6oymstrQegfrDa1gumq5BUjiiNqJkBmBNcvWjipyE7l5VMBy jvHH/Wz4McCe1Ka0+O0zM46ibWb/5jIYDFzVn4Ewe2PICWQGHoZFDVrYvJE0zQM+9ZlI vF2w== X-Forwarded-Encrypted: i=1; AFNElJ+ojV7BlWO/Rs9XfK/QSy8ydwRl01MVGcZMS+mS2flekx8/X17586fAQUQFFp5lIpDDv7rvFU9hsTWs0pE=@vger.kernel.org X-Gm-Message-State: AOJu0Yz7PIySAJ1DVaxs/JzVz7GpCD+XGoWEXtF9HU6G0sRrbHAc8uSO dLQBIhNFc4Y0OvTnnYrQmVVuosWRL0PXBa36AXnYDyjesxX0TVU+T77W X-Gm-Gg: Acq92OFVYkIuwSMFURNXrjRbjzJlv0gAE83vSJsyoDTf9IyYzdyaaNL/7qzYL0cwh84 3EjggyAwclGnFAHb6FC8QK1uKS+MFWQFeHEJYSabv2rWbHluYE8uCAN/2AG2l6AIizhwk2vOrat bRefBVBJ1FBdjMHxXdukSjX1mQ4BUl+MrSILC5369dg5K5jkiHEQQHRFQ8OqzazmAKYpjmuxIQY BRm+8L0YS4J5gRyIYQziRwadVFBwJIPx4ysoH1d/8gEbJcaMaatg4baQsXFYJ7wz/lcAWabYamp pXvr6aDmYig+FOJ8VeEG/mu+SxhJB93PtHiRiQ7v2XZ/JxibmtgED0/Rle9yx8HAFdk3vxhJKXJ 1dX0q5mYNcRgEtxHLY3EW3ItUoW/qj5GWOeCS1wqxxCwJAYLTkBCP7fH4T0uFRamJACMiv4xQMU MSa50wlR1U3a4j9N4D4/lOjD5s3oWqKMQZ6qAw/BI= X-Received: by 2002:a05:6122:2a1:b0:56b:9534:c06f with SMTP id 71dfb90a1353d-5840e635876mr3724170e0c.3.1779515760263; Fri, 22 May 2026 22:56:00 -0700 (PDT) Received: from tresc054937.tre-sc.gov.br ([187.65.210.13]) by smtp.gmail.com with ESMTPSA id 71dfb90a1353d-586f973300dsm5023718e0c.18.2026.05.22.22.55.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 May 2026 22:55:59 -0700 (PDT) From: Luiz Angelo Daros de Luca Date: Sat, 23 May 2026 02:55:35 -0300 Subject: [net-next PATCH v7 2/8] net: dsa: realtek: rtl8365mb: use dsa helpers for port iteration Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20260523-realtek_forward-v7-2-313805e218f3@gmail.com> References: <20260523-realtek_forward-v7-0-313805e218f3@gmail.com> In-Reply-To: <20260523-realtek_forward-v7-0-313805e218f3@gmail.com> To: Andrew Lunn , Vladimir Oltean , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Simon Horman , Linus Walleij , =?utf-8?q?Alvin_=C5=A0ipraga?= , Yury Norov , Rasmus Villemoes , Russell King Cc: netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Abdulkader Alrezej , Mieczyslaw Nalewaj , Luiz Angelo Daros de Luca X-Mailer: b4 0.15.2 Convert open-coded port iteration loops to use the DSA helpers and restructure rtl8365mb_setup() into clear blocking, user, and CPU port phases. This refactoring introduces the following behavioral and defensive changes: - CPU port forwarding/isolation is now explicitly configured. This is necessary to prevent egress traffic from being blocked by the new initialization loop that starts with all ports disabled (reported by Abdulkader Alrezej during series testing). - A guard check is added to the interrupt handler (rtl8365mb_irq) to safely skip ports without a virtual IRQ mapping. Since unused ports no longer receive a virq mapping, this prevents dangerous calls to handle_nested_irq(0) if a link event fires on an unmapped port. - The driver now explicitly returns -EOPNOTSUPP if a DSA cascade link port is detected. This topology is not supported yet and has historically been broken because the absence of a direct CPU port disables CPU tagging entirely, rendering the switch non-functional for DSA. - The rtl8365mb_setup() function now explicitly enforces the presence of a CPU port, returning -EINVAL if none are found. Suggested-by: Abdulkader Alrezej Reviewed-by: Linus Walleij Reviewed-by: Mieczyslaw Nalewaj Signed-off-by: Luiz Angelo Daros de Luca --- drivers/net/dsa/realtek/rtl8365mb.c | 158 +++++++++++++++++++++++++----------- 1 file changed, 109 insertions(+), 49 deletions(-) diff --git a/drivers/net/dsa/realtek/rtl8365mb.c b/drivers/net/dsa/realtek/rtl8365mb.c index 2637884fe472..27746b0694c9 100644 --- a/drivers/net/dsa/realtek/rtl8365mb.c +++ b/drivers/net/dsa/realtek/rtl8365mb.c @@ -1554,18 +1554,15 @@ static void rtl8365mb_stats_setup(struct realtek_priv *priv) { struct rtl8365mb *mb = priv->chip_data; struct dsa_switch *ds = &priv->ds; - int i; + struct dsa_port *dp; /* Per-chip global mutex to protect MIB counter access, since doing * so requires accessing a series of registers in a particular order. */ mutex_init(&mb->mib_lock); - for (i = 0; i < priv->num_ports; i++) { - struct rtl8365mb_port *p = &mb->ports[i]; - - if (dsa_is_unused_port(ds, i)) - continue; + dsa_switch_for_each_available_port(dp, ds) { + struct rtl8365mb_port *p = &mb->ports[dp->index]; /* Per-port spinlock to protect the stats64 data */ spin_lock_init(&p->stats_lock); @@ -1581,13 +1578,10 @@ static void rtl8365mb_stats_teardown(struct realtek_priv *priv) { struct rtl8365mb *mb = priv->chip_data; struct dsa_switch *ds = &priv->ds; - int i; - - for (i = 0; i < priv->num_ports; i++) { - struct rtl8365mb_port *p = &mb->ports[i]; + struct dsa_port *dp; - if (dsa_is_unused_port(ds, i)) - continue; + dsa_switch_for_each_available_port(dp, ds) { + struct rtl8365mb_port *p = &mb->ports[dp->index]; cancel_delayed_work_sync(&p->mib_work); } @@ -1646,6 +1640,9 @@ static irqreturn_t rtl8365mb_irq(int irq, void *data) for_each_set_bit(line, &line_changes, priv->num_ports) { int child_irq = irq_find_mapping(priv->irqdomain, line); + if (!child_irq) + continue; + handle_nested_irq(child_irq); } @@ -1709,13 +1706,14 @@ static int rtl8365mb_irq_disable(struct realtek_priv *priv) static int rtl8365mb_irq_setup(struct realtek_priv *priv) { struct rtl8365mb *mb = priv->chip_data; + struct dsa_switch *ds = &priv->ds; struct device_node *intc; + struct dsa_port *dp; u32 irq_trig; int virq; int irq; u32 val; int ret; - int i; intc = of_get_child_by_name(priv->dev->of_node, "interrupt-controller"); if (!intc) { @@ -1744,8 +1742,8 @@ static int rtl8365mb_irq_setup(struct realtek_priv *priv) goto out_put_node; } - for (i = 0; i < priv->num_ports; i++) { - virq = irq_create_mapping(priv->irqdomain, i); + dsa_switch_for_each_available_port(dp, ds) { + virq = irq_create_mapping(priv->irqdomain, dp->index); if (!virq) { dev_err(priv->dev, "failed to create irq domain mapping\n"); @@ -1815,9 +1813,11 @@ static int rtl8365mb_irq_setup(struct realtek_priv *priv) mb->irq = 0; out_remove_irqdomain: - for (i = 0; i < priv->num_ports; i++) { - virq = irq_find_mapping(priv->irqdomain, i); - irq_dispose_mapping(virq); + dsa_switch_for_each_available_port(dp, ds) { + virq = irq_find_mapping(priv->irqdomain, dp->index); + + if (virq) + irq_dispose_mapping(virq); } irq_domain_remove(priv->irqdomain); @@ -1832,8 +1832,9 @@ static int rtl8365mb_irq_setup(struct realtek_priv *priv) static void rtl8365mb_irq_teardown(struct realtek_priv *priv) { struct rtl8365mb *mb = priv->chip_data; + struct dsa_switch *ds = &priv->ds; + struct dsa_port *dp; int virq; - int i; if (mb->irq) { free_irq(mb->irq, priv); @@ -1841,9 +1842,15 @@ static void rtl8365mb_irq_teardown(struct realtek_priv *priv) } if (priv->irqdomain) { - for (i = 0; i < priv->num_ports; i++) { - virq = irq_find_mapping(priv->irqdomain, i); - irq_dispose_mapping(virq); + /* Unused ports with a linked PHY still have an active IRQ + * mapping that must be disposed of during teardown. Loop + * through all ports. + */ + dsa_switch_for_each_port(dp, ds) { + virq = irq_find_mapping(priv->irqdomain, dp->index); + + if (virq) + irq_dispose_mapping(virq); } irq_domain_remove(priv->irqdomain); @@ -1961,10 +1968,11 @@ static int rtl8365mb_setup(struct dsa_switch *ds) { struct realtek_priv *priv = ds->priv; struct rtl8365mb_cpu *cpu; - struct dsa_port *cpu_dp; + u32 downports_mask = 0; + u32 upports_mask = 0; struct rtl8365mb *mb; + struct dsa_port *dp; int ret; - int i; mb = priv->chip_data; cpu = &mb->cpu; @@ -1991,46 +1999,98 @@ static int rtl8365mb_setup(struct dsa_switch *ds) else if (ret) dev_info(priv->dev, "no interrupt support\n"); - /* Configure CPU tagging */ - dsa_switch_for_each_cpu_port(cpu_dp, ds) { - cpu->mask |= BIT(cpu_dp->index); + /* Start with all ports blocked, including unused ports */ + dsa_switch_for_each_port(dp, ds) { + struct rtl8365mb_port *p = &mb->ports[dp->index]; + + if (dsa_port_is_dsa(dp)) { + /* Cascading (DSA links) is not supported yet. + * Historically, the driver has always been broken + * without a dedicated CPU port because CPU tagging + * would be disabled, rendering the switch entirely + * non-functional for DSA operations. + */ + dev_err(ds->dev, + "Cascading (DSA link) not supported\n"); + ret = -EOPNOTSUPP; + goto out_teardown_irq; + } - if (cpu->trap_port == RTL8365MB_MAX_NUM_PORTS) - cpu->trap_port = cpu_dp->index; - } - cpu->enable = cpu->mask > 0; - ret = rtl8365mb_cpu_config(priv); - if (ret) - goto out_teardown_irq; + /* Set the initial STP state of all ports to DISABLED, otherwise + * ports will still forward frames to the CPU despite being + * administratively down by default. + */ + rtl8365mb_port_stp_state_set(ds, dp->index, BR_STATE_DISABLED); - /* Configure ports */ - for (i = 0; i < priv->num_ports; i++) { - struct rtl8365mb_port *p = &mb->ports[i]; + /* Start with all port completely isolated */ + ret = rtl8365mb_port_set_isolation(priv, dp->index, 0); + if (ret) + goto out_teardown_irq; + + /* Disable learning */ + ret = rtl8365mb_port_set_learning(priv, dp->index, false); + if (ret) + goto out_teardown_irq; + + /* Set up per-port private data */ + p->priv = priv; + p->index = dp->index; - if (dsa_is_unused_port(ds, i)) + /* Collect CPU ports. If we support cascade switches, it should + * also include the upstream DSA ports. + */ + if (!dsa_port_is_cpu(dp)) + continue; + + upports_mask |= BIT(dp->index); + } + + /* Configure user ports */ + dsa_switch_for_each_port(dp, ds) { + if (!dsa_port_is_user(dp)) continue; /* Forward only to the CPU */ - ret = rtl8365mb_port_set_isolation(priv, i, cpu->mask); + ret = rtl8365mb_port_set_isolation(priv, dp->index, + upports_mask); if (ret) goto out_teardown_irq; - /* Disable learning */ - ret = rtl8365mb_port_set_learning(priv, i, false); + /* If we support cascade switches, it should also include the + * downstream DSA ports. + */ + downports_mask |= BIT(dp->index); + } + + /* Configure CPU tagging */ + /* If we support cascade switches, it should also include the upstream + * DSA ports. + */ + dsa_switch_for_each_cpu_port(dp, ds) { + /* Use the first CPU port as trap_port */ + if (cpu->trap_port == RTL8365MB_MAX_NUM_PORTS) + cpu->trap_port = dp->index; + + /* Forward to all user ports */ + ret = rtl8365mb_port_set_isolation(priv, dp->index, + downports_mask); if (ret) goto out_teardown_irq; + } - /* Set the initial STP state of all ports to DISABLED, otherwise - * ports will still forward frames to the CPU despite being - * administratively down by default. - */ - rtl8365mb_port_stp_state_set(ds, i, BR_STATE_DISABLED); + cpu->mask = upports_mask; + cpu->enable = cpu->mask > 0; - /* Set up per-port private data */ - p->priv = priv; - p->index = i; + if (!cpu->enable) { + dev_err(priv->dev, "no upstream (CPU, Link) port defined\n"); + ret = -EINVAL; + goto out_teardown_irq; } + ret = rtl8365mb_cpu_config(priv); + if (ret) + goto out_teardown_irq; + ret = rtl8365mb_port_change_mtu(ds, cpu->trap_port, ETH_DATA_LEN); if (ret) goto out_teardown_irq; -- 2.54.0