From patchwork Fri Sep 24 23:36:23 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 514612 Delivered-To: patch@linaro.org Received: by 2002:a17:906:f46:0:0:0:0 with SMTP id h6csp1777392ejj; Fri, 24 Sep 2021 16:38:53 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwahtogvmh90/CMHWed04bhgAR8QedQFJqGLg/tXYiFOxIRgYoljYTmzg18Nn/wODPzMX88 X-Received: by 2002:a17:906:fc7:: with SMTP id c7mr14584958ejk.333.1632526733648; Fri, 24 Sep 2021 16:38:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632526733; cv=none; d=google.com; s=arc-20160816; b=JhkY1yIdRmvrdgtbXY2/RJ9O1bRJ0muyyrp0gcuc172gUrwUUwbW2YodRoaMTLUpsA z+z+4abHCUGyc3ILTphzPL/ze4uLcwaMT+mEb++fVZCzmquW07Dyl3H4r6eVSXFmSJiG 5AFZ6fLBK3ryG2wxKU+ArYLRCE0KeJlhGShC2e92li99XxPZr1GJuTpjxnNgWaVwDRJs SLKfSfcaaqlKNc3ZnOZEXCcHBr33hKW/GjrVuowl3k3w9uo+u3cVm6q9+UqTpz5IKOlr asXvnCra512CKn/z2rSQ2qaHNRHI7pglhIEt/u8z4/WjpPKw5bBGxeDOiS0ESclVwQMd /2Gg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=jnT3sJXLA57GZ/AQJZhWi+XbpVu8sqEszBnk3MfHqYw=; b=puJpb9//XX1egO74nast1DVn+bpcOAqC8fxTTcTuUlMuQLAsLNRoLKxC5qWHLB5Dn4 UFgqTAh1xFkdAC7whh2a9cLaFbNWcOlJWifoqvVQFdE6Mder8dOuHbRDsRhJSQ6Uent4 QX6DNrY1rq61F6a0BQMntQUEXtvNFBJo78A7TzNH3GKCegDY103BGpcu9TmOOqeCtf1W PZRrSX8icNo0uyoEHANU9aFS93zSlFibouYPcCXaZ62hXdGPmtyMKmaBfUBAGeRyQ8z5 nAD4oSem9b79sOwAdol/Pq+qY9Wkj8EYCjRyvjbdvGsOu9i5g2EWmKJn4MIPxVRiw3AO tjuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=N9KaThGn; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a11si10803843edx.158.2021.09.24.16.38.53; Fri, 24 Sep 2021 16:38:53 -0700 (PDT) Received-SPF: pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=N9KaThGn; spf=pass (google.com: domain of netdev-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=netdev-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230303AbhIXXk0 (ORCPT + 8 others); Fri, 24 Sep 2021 19:40:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51582 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245038AbhIXXkZ (ORCPT ); Fri, 24 Sep 2021 19:40:25 -0400 Received: from mail-lf1-x135.google.com (mail-lf1-x135.google.com [IPv6:2a00:1450:4864:20::135]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 941F4C061571 for ; Fri, 24 Sep 2021 16:38:51 -0700 (PDT) Received: by mail-lf1-x135.google.com with SMTP id y28so46385668lfb.0 for ; Fri, 24 Sep 2021 16:38:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=jnT3sJXLA57GZ/AQJZhWi+XbpVu8sqEszBnk3MfHqYw=; b=N9KaThGn050IWLOTn66Xm2lRr5ap3DTFLn0IKfXqELLK6pL788ReLU7AhgNq/fxoG/ fM/LDX7dpHk0SP+IQwE8Wp2bEbdS+uifcgzkrKooikYCQJ99GpEInsjblxRls+yU1Xql VLBq7rAdFgWIimTayIrndpFJzzY8yGgfkfWMG9pHsiCZ5rk+c9SbZw1ed7GjtErfEoET EemczMimWqxTKj0L7/W4m4a+o81qbe2XzJDC8efrOxIEILIR7f5P/fohGmwFmWThyAeq 8llTJbFfOBXEy83/+Uy4+4paytvsVK5qO25fbBwAdh3pf2vshTdH2RvpgChlmlJaFDbg 4T3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=jnT3sJXLA57GZ/AQJZhWi+XbpVu8sqEszBnk3MfHqYw=; b=AjWae71xihsUMCYfZNrdcZCdDK0WohMqzjm2Jtft4KuwZEerTKyU8HBB5G3OMfTOC9 bRHJDnRZJ5KXLTyCoAhbuQPJAFtMCrx6Ms6/v6mMa4a2SNLhFRwqzlLIRgfGwkKt13au /Dl46zvZSG0XVGQYlCbDLkojcjygXNK9KaXkJrlh7LNIM5p38cTe33hM3rwAdSegmPbq HFl0CxewOnl0KsQox7zpDyIsTwnLrcu+Rrk3KNaV8slBC4+iyLfijn1qyJGlVRPNq6G6 nLBG4lqoui634RpvRoTBJMJ1NHbFbb8tMoW5OHYHOCOc1LE+AfZ6PFJcJq8v6Vv7T+qE R6Lg== X-Gm-Message-State: AOAM531M9cm9j9901DHHhwI71cRz/zAdZShdHU0/jjpz/0WwvQG4jvux LkR2HjQDyHbEkQQidFNXarvtJA== X-Received: by 2002:a2e:87ca:: with SMTP id v10mr13094638ljj.369.1632526729882; Fri, 24 Sep 2021 16:38:49 -0700 (PDT) Received: from localhost.localdomain (c-fdcc225c.014-348-6c756e10.bbcust.telenor.se. [92.34.204.253]) by smtp.gmail.com with ESMTPSA id k21sm1176652lji.81.2021.09.24.16.38.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Sep 2021 16:38:49 -0700 (PDT) From: Linus Walleij To: Andrew Lunn , Vivien Didelot , Florian Fainelli , Vladimir Oltean , "David S . Miller" , Jakub Kicinski Cc: netdev@vger.kernel.org, DENG Qingfang , Mauri Sandberg , =?utf-8?q?Alvin_=C5=A0ipraga?= , Linus Walleij Subject: [PATCH net-next 1/6 v5] net: dsa: rtl8366rb: Support bridge offloading Date: Sat, 25 Sep 2021 01:36:23 +0200 Message-Id: <20210924233628.2016227-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210924233628.2016227-1-linus.walleij@linaro.org> References: <20210924233628.2016227-1-linus.walleij@linaro.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org From: DENG Qingfang Use port isolation registers to configure bridge offloading. Tested on the D-Link DIR-685, switching between ports and sniffing ports to make sure no packets leak. Cc: Vladimir Oltean Cc: Mauri Sandberg Reviewed-by: Alvin Šipraga Reviewed-by: Florian Fainelli Signed-off-by: DENG Qingfang Signed-off-by: Linus Walleij --- ChangeLog v4->v5: - No changes just resending with the rest of the patches. ChangeLog v3->v4: - Fix a bug where I managed to mask off the CPU port from the ports we could access leading to numb bridge. - Reword some comments. ChangeLog v2->v3: - Parens around the (pmask) in the port isolation macro. - Do not exit join/leave functions on regmap failures, print an error and continue. - Clarify comments around the port in join/leave functions. ChangeLog v1->v2: - introduce RTL8366RB_PORT_ISO_PORTS() to shift the port mask into place so we are not confused by the enable bit. - Use this with dsa_user_ports() to isolate the CPU port from itself. --- drivers/net/dsa/rtl8366rb.c | 86 +++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) -- 2.31.1 diff --git a/drivers/net/dsa/rtl8366rb.c b/drivers/net/dsa/rtl8366rb.c index a89093bc6c6a..b930050cfd1b 100644 --- a/drivers/net/dsa/rtl8366rb.c +++ b/drivers/net/dsa/rtl8366rb.c @@ -300,6 +300,13 @@ #define RTL8366RB_INTERRUPT_STATUS_REG 0x0442 #define RTL8366RB_NUM_INTERRUPT 14 /* 0..13 */ +/* Port isolation registers */ +#define RTL8366RB_PORT_ISO_BASE 0x0F08 +#define RTL8366RB_PORT_ISO(pnum) (RTL8366RB_PORT_ISO_BASE + (pnum)) +#define RTL8366RB_PORT_ISO_EN BIT(0) +#define RTL8366RB_PORT_ISO_PORTS_MASK GENMASK(7, 1) +#define RTL8366RB_PORT_ISO_PORTS(pmask) ((pmask) << 1) + /* bits 0..5 enable force when cleared */ #define RTL8366RB_MAC_FORCE_CTRL_REG 0x0F11 @@ -835,6 +842,21 @@ static int rtl8366rb_setup(struct dsa_switch *ds) if (ret) return ret; + /* Isolate all user ports so they can only send packets to itself and the CPU port */ + for (i = 0; i < RTL8366RB_PORT_NUM_CPU; i++) { + ret = regmap_write(smi->map, RTL8366RB_PORT_ISO(i), + RTL8366RB_PORT_ISO_PORTS(BIT(RTL8366RB_PORT_NUM_CPU)) | + RTL8366RB_PORT_ISO_EN); + if (ret) + return ret; + } + /* CPU port can send packets to all ports */ + ret = regmap_write(smi->map, RTL8366RB_PORT_ISO(RTL8366RB_PORT_NUM_CPU), + RTL8366RB_PORT_ISO_PORTS(dsa_user_ports(ds)) | + RTL8366RB_PORT_ISO_EN); + if (ret) + return ret; + /* Set up the "green ethernet" feature */ ret = rtl8366rb_jam_table(rtl8366rb_green_jam, ARRAY_SIZE(rtl8366rb_green_jam), smi, false); @@ -1127,6 +1149,68 @@ rtl8366rb_port_disable(struct dsa_switch *ds, int port) rb8366rb_set_port_led(smi, port, false); } +static int +rtl8366rb_port_bridge_join(struct dsa_switch *ds, int port, + struct net_device *bridge) +{ + struct realtek_smi *smi = ds->priv; + unsigned int port_bitmap = 0; + int ret, i; + + /* Loop over all other ports than the current one */ + for (i = 0; i < RTL8366RB_PORT_NUM_CPU; i++) { + /* Current port handled last */ + if (i == port) + continue; + /* Not on this bridge */ + if (dsa_to_port(ds, i)->bridge_dev != bridge) + continue; + /* Join this port to each other port on the bridge */ + ret = regmap_update_bits(smi->map, RTL8366RB_PORT_ISO(i), + RTL8366RB_PORT_ISO_PORTS(BIT(port)), + RTL8366RB_PORT_ISO_PORTS(BIT(port))); + if (ret) + dev_err(smi->dev, "failed to join port %d\n", port); + + port_bitmap |= BIT(i); + } + + /* Set the bits for the ports we can access */ + return regmap_update_bits(smi->map, RTL8366RB_PORT_ISO(port), + RTL8366RB_PORT_ISO_PORTS(port_bitmap), + RTL8366RB_PORT_ISO_PORTS(port_bitmap)); +} + +static void +rtl8366rb_port_bridge_leave(struct dsa_switch *ds, int port, + struct net_device *bridge) +{ + struct realtek_smi *smi = ds->priv; + unsigned int port_bitmap = 0; + int ret, i; + + /* Loop over all other ports than this one */ + for (i = 0; i < RTL8366RB_PORT_NUM_CPU; i++) { + /* Current port handled last */ + if (i == port) + continue; + /* Not on this bridge */ + if (dsa_to_port(ds, i)->bridge_dev != bridge) + continue; + /* Remove this port from any other port on the bridge */ + ret = regmap_update_bits(smi->map, RTL8366RB_PORT_ISO(i), + RTL8366RB_PORT_ISO_PORTS(BIT(port)), 0); + if (ret) + dev_err(smi->dev, "failed to leave port %d\n", port); + + port_bitmap |= BIT(i); + } + + /* Clear the bits for the ports we can not access, leave ourselves */ + regmap_update_bits(smi->map, RTL8366RB_PORT_ISO(port), + RTL8366RB_PORT_ISO_PORTS(port_bitmap), 0); +} + static int rtl8366rb_change_mtu(struct dsa_switch *ds, int port, int new_mtu) { struct realtek_smi *smi = ds->priv; @@ -1510,6 +1594,8 @@ static const struct dsa_switch_ops rtl8366rb_switch_ops = { .get_strings = rtl8366_get_strings, .get_ethtool_stats = rtl8366_get_ethtool_stats, .get_sset_count = rtl8366_get_sset_count, + .port_bridge_join = rtl8366rb_port_bridge_join, + .port_bridge_leave = rtl8366rb_port_bridge_leave, .port_vlan_filtering = rtl8366_vlan_filtering, .port_vlan_add = rtl8366_vlan_add, .port_vlan_del = rtl8366_vlan_del,