From patchwork Sat Sep 25 22:59:24 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 514630 Delivered-To: patch@linaro.org Received: by 2002:a02:c816:0:0:0:0:0 with SMTP id p22csp2775883jao; Sat, 25 Sep 2021 16:01:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwETj2ndQTdDE3Rov2GxMk9EkGU9fyduvuvtE+a+6TJRaG95KUsOoNMwT/37R7cuYtk62wq X-Received: by 2002:a05:6e02:bc4:: with SMTP id c4mr13789176ilu.129.1632610915078; Sat, 25 Sep 2021 16:01:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1632610915; cv=none; d=google.com; s=arc-20160816; b=q3ah0tYhJIDDJze3SpNpzT3C16O19RXNDpvom6B13RvZohCY9PPQWn4l9CE6gQKDMx egA1RsRQApxx5XcYiIGhfs5nOd7EJvlt38mMmCfakJmfXQB82Ery1gioutUfJHVO7h1M rtErkDsParJXNJtPNuKWfmWmr7I/Zm25QpPJWYfPtdpRX8cwKlkU8LBxJ6Q+ARZUtlVt vVMkd11SxIi4eS7rDyEjF3MPyw72ggKmFhqKG0sADqLia4Xvvu6cSiCDr0R8MnpGMU4m dSjZ6d9gdg5nrMToEPA7GF8cVJdNj6UeY/a/VNgAZkoBkH7+xeoSC5V6rO6kCWIu66Od 0QKA== 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=MR0PfYx22ke+kh/lxH00XljqzBmM0dQBgyVIJd3ubUI=; b=nZB37bZtq5Uep7uQTsgiS/wOGHMbBM42XKL26EkIZvhwupHevKOM3OiC5Xh9PmLZu9 9tubhce97S7OS4fUXtjKDPD5PDhUeMfB9V4sxck7KqJUm3ugP6IlkAQClqHIfaKYv0XM RC7r67E+R8PFYvkOzBMby8UfpRXc5afIabzc9AYmMB0aEsnMWcmbDTqGfkwvN3zOjMcF 9yJ8Y9ksI826lkyh395j+Xgl/BHNYHhLFvUBz5Ntryl3iaty8ru+gnLDItgvK8/TijHc ExGYf3izqQ/zRR+R4AIGcsOA7gByf0bwJTZrqduUxz0UcdVvZDn2emtrJR1jA5pHvkl+ L+Lw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=ovrA7yy4; 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 u6si16405707jao.3.2021.09.25.16.01.53; Sat, 25 Sep 2021 16:01:54 -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=ovrA7yy4; 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 S230185AbhIYXD2 (ORCPT + 8 others); Sat, 25 Sep 2021 19:03:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45046 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230078AbhIYXD1 (ORCPT ); Sat, 25 Sep 2021 19:03:27 -0400 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ED482C061570 for ; Sat, 25 Sep 2021 16:01:51 -0700 (PDT) Received: by mail-lf1-x136.google.com with SMTP id z24so58286955lfu.13 for ; Sat, 25 Sep 2021 16:01: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=MR0PfYx22ke+kh/lxH00XljqzBmM0dQBgyVIJd3ubUI=; b=ovrA7yy4r3PPO/W6llfIAmnxGiNudqE3KRajjaehEBA1OA8Sv+wlMg6JOC7W3qTGgD CmAaAM3C3slNSD1K70rfgN6xtcCT1CWOuZIjz+C+zt5LfH+uJlWIWUwMgQDwM98oivn4 XXgLYpTkXyDtnRTszctePc4PG7VhC3FBfUghJBYWIg25xzsraw/ZOaw2DkkiguzLuUPt RiVn/DqOsR0UVFA+18gfFzT5LFAKyIV0nhywKjCQMsWZ0+U9ueVz8KyQ4615pOcdGDI7 XsIzSrqBf/XzUBzAWJKwJTmF1Xekl/k3vPOdZxWdUbUA10OAk5u0FKVwhjF8Ki5qEt7l uW/w== 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=MR0PfYx22ke+kh/lxH00XljqzBmM0dQBgyVIJd3ubUI=; b=PINoqTpy4zx1W0U9BQrWSjnNgtnsUjdCWzVREtHBWgbUWfATYehDBa8ZgfDt/fN6LS loNmSLbQWKm2bJ7mBi7bvn7nhKB2eD8jULntmtPXDE1xAP2sZGETSCWlHtY4umW7ffxM A7ypbKgVtdnCtzXnEp0qbhIM+4lJ8KJkInw6t6SuSRDdnbQDeo2XTbC1l5x2zc16R8PH B6eyBIrISIdZDnUgAyz4nv0AazjTN7nX63MvAYLwjFhJsUo60C2FkQDCCswb65zmlkoo pzubQ9fNLSDHqrbwPv//VfxFBLGNaEF3BoLRe8PiBpvnTc73LASl/yTxOYdxZLm8TIsz GBMQ== X-Gm-Message-State: AOAM530ZKn2V56SA15b9plp+LR3WByKLN0+Rd1i0LoN0vpyakeYCn5jE 08fb714Zac92jZ+guCeZGRXn2A== X-Received: by 2002:a2e:8782:: with SMTP id n2mr20157642lji.177.1632610910343; Sat, 25 Sep 2021 16:01:50 -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 d27sm1448111ljo.119.2021.09.25.16.01.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 25 Sep 2021 16:01:50 -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 v7] net: dsa: rtl8366rb: Support bridge offloading Date: Sun, 26 Sep 2021 00:59:24 +0200 Message-Id: <20210925225929.2082046-2-linus.walleij@linaro.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210925225929.2082046-1-linus.walleij@linaro.org> References: <20210925225929.2082046-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: Vladimir Oltean Reviewed-by: Alvin Šipraga Reviewed-by: Florian Fainelli Signed-off-by: DENG Qingfang Signed-off-by: Linus Walleij --- ChangeLog v6->v7: - Collect Vladimir's review tag. ChangeLog v5->v6: - No changes just resending with the rest of the patches. 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,