From patchwork Thu May 24 06:56:23 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ilias Apalodimas X-Patchwork-Id: 136712 Delivered-To: patch@linaro.org Received: by 2002:a2e:9706:0:0:0:0:0 with SMTP id r6-v6csp1823739lji; Wed, 23 May 2018 23:56:51 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqW313hs8gmLITVnRmg+T3gXZCefNFK2AJUh7OWPEvE9SJyFJFCvOnG4V6vNTLTGKFUuInU X-Received: by 2002:a63:7f07:: with SMTP id a7-v6mr4788407pgd.173.1527145011213; Wed, 23 May 2018 23:56:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1527145011; cv=none; d=google.com; s=arc-20160816; b=zd2V3DLazj9Ut5OrgRMYpSgbHjZ5J88WyOQze/5PMoyzttOvXH4Yoq/HLYV0KpHBkH uMdGWGDtn4BdmpEpOEZvQCnGI8IXejJAAfvSmf55nnqd/vXh/pmPHcdr+Jh/o6FYsogW aSfDLt7VkGhNZLO0ehBuoVIVsh9/vYOaukfCcyK5NWnIk81qwjyZ1PcSDlmrrHJR5o0x 4+Y21ZXxd89NXaGnGB6kFnF7qAPP2suu3NGm4dJ0IxRRH7w/oM22hAtBFL8Q3N2cP5uY cIBp8ec3G0yEm8jZ5i2K2v2G+LWTBOErguu6tO6fs3kcqTlAE9NdCPXGZQBW83N0FWHC C4uQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature:arc-authentication-results; bh=ZYsWLC9UknldjSMzM5uWFFbZjt/mkoaIVMNBunWcTn8=; b=Sv9RW4QObB99e5qGcOj8Tl7RU2uVjPa3zOgzs+VDgbG8k+/FQzFta7P4b2FKTHzyA2 WoWnfqy6iLt01J7+Qayd6vJzzXI8vVGy1g3U6qsvjcKnlVSzhBWJKz8LZsT7EwUa4hp+ RgdDpu/Bpk35JqEQ7JvxKE0aQPfqeo0uAEXhfE+d44GJDZrIcuTcUuMWdNwQsVtFMbeT wNLZlUbs/to9r+DOQfl7ql2KY5xP/lxrzr+qSJMOInvsRvPC4Udzq61SX7VqBfy1OhS6 UwXE+TwkU1JyWCWOxwbPjWG+vyCcYBJFXv8DV4i7ORZSrhlOPq9c6EyKkOC+L+YdgAHE Uu5A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YSCW1d2S; spf=pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 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. [209.132.180.67]) by mx.google.com with ESMTP id v2-v6si8349741plo.577.2018.05.23.23.56.51; Wed, 23 May 2018 23:56:51 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=YSCW1d2S; spf=pass (google.com: best guess record for domain of netdev-owner@vger.kernel.org designates 209.132.180.67 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 S964876AbeEXG4t (ORCPT + 9 others); Thu, 24 May 2018 02:56:49 -0400 Received: from mail-wm0-f65.google.com ([74.125.82.65]:38156 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S935619AbeEXG4l (ORCPT ); Thu, 24 May 2018 02:56:41 -0400 Received: by mail-wm0-f65.google.com with SMTP id m129-v6so1993418wmb.3 for ; Wed, 23 May 2018 23:56:41 -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; bh=ZYsWLC9UknldjSMzM5uWFFbZjt/mkoaIVMNBunWcTn8=; b=YSCW1d2SpQu/CBNUAggNHT4Jk3iWuMbzfErkxZHhrsX6ZSJkDVMo1AN+SO2kQ0+mDC Re1kw5H5HOAFNg+z9oFP1ganwPjBFnQayr9mFAHk0xJRC06Nmub4plIS+uqKKWYOBL6k gDb3f8ztyQLuFt0XQSG5iucHfpyjGTxcbmNsU= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=ZYsWLC9UknldjSMzM5uWFFbZjt/mkoaIVMNBunWcTn8=; b=I+gMm7B8AJzLfLe8AIniHPlG189NRSs/kvMVu3L5zjzYjjmhXv4dfXQsVlGQ0VhUFK /Eicoydc2xBr4e/ZRpPAhivgUhei5YOiHilqd15aLjJXkqlS1qxhgfkE+cDdwTGG4I60 tEKzYjNrqgCnD5gPUEMU9dWzcLQWO81wNYu783EguNut4jmdX25PFdoUL/w+7J6+3mq+ 9AoqlFNtQhgaz+J9701P/EbDzAy56IUtYb3zeMMF/jQbuEHPKyDf50qfipGj9kKwwIha jaJ/izqiOmIdRWrMLyPH/t4RUT3fb6wNqmYWKUCbLBzXd0H1obqtg5HjskIyahLrA7cv PbKw== X-Gm-Message-State: ALKqPwdF7fSCic0Bbf+ASS0iBQd1EHtg6kETSpiDn6KTS94AmCFyzuJQ cxDcZNiHpaQH+Y9eCRdJ9nCCIyiqiSE= X-Received: by 2002:a1c:91c4:: with SMTP id t187-v6mr5750818wmd.19.1527145000073; Wed, 23 May 2018 23:56:40 -0700 (PDT) Received: from localhost.localdomain ([2a02:587:460a:f200:b540:ba10:add4:9422]) by smtp.gmail.com with ESMTPSA id t198-v6sm4795257wmt.23.2018.05.23.23.56.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 23 May 2018 23:56:39 -0700 (PDT) From: Ilias Apalodimas To: netdev@vger.kernel.org, grygorii.strashko@ti.com, ivan.khoronzhuk@linaro.org, nsekhar@ti.com, jiri@resnulli.us, ivecera@redhat.com Cc: francois.ozog@linaro.org, yogeshs@ti.com, spatton@ti.com, Ilias Apalodimas Subject: [PATCH 3/4] cpsw_switchdev: add switchdev support files Date: Thu, 24 May 2018 09:56:23 +0300 Message-Id: <1527144984-31236-4-git-send-email-ilias.apalodimas@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1527144984-31236-1-git-send-email-ilias.apalodimas@linaro.org> References: <1527144984-31236-1-git-send-email-ilias.apalodimas@linaro.org> Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org Signed-off-by: Ilias Apalodimas --- drivers/net/ethernet/ti/Kconfig | 9 + drivers/net/ethernet/ti/Makefile | 1 + drivers/net/ethernet/ti/cpsw_switchdev.c | 299 +++++++++++++++++++++++++++++++ drivers/net/ethernet/ti/cpsw_switchdev.h | 4 + 4 files changed, 313 insertions(+) create mode 100644 drivers/net/ethernet/ti/cpsw_switchdev.c create mode 100644 drivers/net/ethernet/ti/cpsw_switchdev.h -- 2.7.4 diff --git a/drivers/net/ethernet/ti/Kconfig b/drivers/net/ethernet/ti/Kconfig index 48a541e..b22ae7d 100644 --- a/drivers/net/ethernet/ti/Kconfig +++ b/drivers/net/ethernet/ti/Kconfig @@ -73,6 +73,15 @@ config TI_CPSW To compile this driver as a module, choose M here: the module will be called cpsw. +config TI_CPSW_SWITCHDEV + bool "TI CPSW switchdev support" + depends on TI_CPSW + depends on NET_SWITCHDEV + help + Enable switchdev support on TI's CPSW Ethernet Switch. + + This will allow you to configure the switch using standard tools. + config TI_CPTS bool "TI Common Platform Time Sync (CPTS) Support" depends on TI_CPSW || TI_KEYSTONE_NETCP diff --git a/drivers/net/ethernet/ti/Makefile b/drivers/net/ethernet/ti/Makefile index 0be551d..3926c6a 100644 --- a/drivers/net/ethernet/ti/Makefile +++ b/drivers/net/ethernet/ti/Makefile @@ -15,6 +15,7 @@ obj-$(CONFIG_TI_CPSW_PHY_SEL) += cpsw-phy-sel.o obj-$(CONFIG_TI_CPSW_ALE) += cpsw_ale.o obj-$(CONFIG_TI_CPTS_MOD) += cpts.o obj-$(CONFIG_TI_CPSW) += ti_cpsw.o +ti_cpsw-objs:= cpsw_switchdev.o ti_cpsw-y := cpsw.o obj-$(CONFIG_TI_KEYSTONE_NETCP) += keystone_netcp.o diff --git a/drivers/net/ethernet/ti/cpsw_switchdev.c b/drivers/net/ethernet/ti/cpsw_switchdev.c new file mode 100644 index 0000000..bf8c1bf --- /dev/null +++ b/drivers/net/ethernet/ti/cpsw_switchdev.c @@ -0,0 +1,299 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Texas Instruments switchdev Driver + * + * Copyright (C) 2018 Texas Instruments + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation version 2. + * + * This program is distributed "as is" WITHOUT ANY WARRANTY of any + * kind, whether express or implied; without even the implied warranty + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include +#include +#include +#include "cpsw.h" +#include "cpsw_priv.h" +#include "cpsw_ale.h" + +static u32 cpsw_switchdev_get_ver(struct net_device *ndev) +{ + struct cpsw_priv *priv = netdev_priv(ndev); + struct cpsw_common *cpsw = priv->cpsw; + + return cpsw->version; +} + +static int cpsw_port_attr_set(struct net_device *dev, + const struct switchdev_attr *attr, + struct switchdev_trans *trans) +{ + return -EOPNOTSUPP; +} + +static int cpsw_port_attr_get(struct net_device *dev, + struct switchdev_attr *attr) +{ + u32 cpsw_ver; + int err = 0; + + switch (attr->id) { + case SWITCHDEV_ATTR_ID_PORT_PARENT_ID: + cpsw_ver = cpsw_switchdev_get_ver(dev); + attr->u.ppid.id_len = sizeof(cpsw_ver); + memcpy(&attr->u.ppid.id, &cpsw_ver, attr->u.ppid.id_len); + break; + default: + return -EOPNOTSUPP; + } + + return err; +} + +static u16 cpsw_get_pvid(struct cpsw_priv *priv) +{ + struct cpsw_common *cpsw = priv->cpsw; + u32 __iomem *port_vlan_reg; + u32 pvid; + + if (priv->emac_port) { + int reg = CPSW2_PORT_VLAN; + + if (cpsw->version == CPSW_VERSION_1) + reg = CPSW1_PORT_VLAN; + pvid = slave_read(cpsw->slaves + (priv->emac_port - 1), reg); + } else { + port_vlan_reg = &cpsw->host_port_regs->port_vlan; + pvid = readl(port_vlan_reg); + } + + pvid = pvid & 0xfff; + + return pvid; +} + +static void cpsw_set_pvid(struct cpsw_priv *priv, u16 vid, bool cfi, u32 cos) +{ + struct cpsw_common *cpsw = priv->cpsw; + void __iomem *port_vlan_reg; + u32 pvid; + + pvid = vid; + pvid |= cfi ? BIT(12) : 0; + pvid |= (cos & 0x7) << 13; + + if (priv->emac_port) { + int reg = CPSW2_PORT_VLAN; + + if (cpsw->version == CPSW_VERSION_1) + reg = CPSW1_PORT_VLAN; + /* no barrier */ + slave_write(cpsw->slaves + (priv->emac_port - 1), pvid, reg); + } else { + /* CPU port */ + port_vlan_reg = &cpsw->host_port_regs->port_vlan; + writel(pvid, port_vlan_reg); + } +} + +static int cpsw_port_vlan_add(struct cpsw_priv *priv, bool untag, bool pvid, + u16 vid) +{ + struct cpsw_common *cpsw = priv->cpsw; + int port_mask = BIT(priv->emac_port); + int unreg_mcast_mask = 0; + int reg_mcast_mask = 0; + int untag_mask = 0; + int ret = 0; + + if (priv->ndev->flags & IFF_ALLMULTI) + unreg_mcast_mask = port_mask; + + if (priv->ndev->flags & IFF_MULTICAST) + reg_mcast_mask = port_mask; + + if (untag) + untag_mask = port_mask; + + ret = cpsw_ale_vlan_add_modify(cpsw->ale, vid, port_mask, untag_mask, + reg_mcast_mask, unreg_mcast_mask); + if (ret) { + dev_err(priv->dev, "Unable to add vlan\n"); + return ret; + } + + if (!pvid) + return ret; + + cpsw_set_pvid(priv, vid, 0, 0); + + dev_dbg(priv->dev, "VID: %u dev: %s port: %u\n", vid, + priv->ndev->name, priv->emac_port); + + return ret; +} + +static int cpsw_port_vlan_del(struct cpsw_priv *priv, u16 vid) +{ + struct cpsw_common *cpsw = priv->cpsw; + int port_mask = BIT(priv->emac_port); + int ret = 0; + + ret = cpsw_ale_vlan_del_modify(cpsw->ale, vid, port_mask); + if (ret != 0) + return ret; + + ret = cpsw_ale_del_ucast(cpsw->ale, priv->mac_addr, + HOST_PORT_NUM, ALE_VLAN, vid); + + if (vid == cpsw_get_pvid(priv)) + cpsw_set_pvid(priv, 0, 0, 0); + + if (ret != 0) { + dev_dbg(priv->dev, "Failed to delete unicast entry\n"); + ret = 0; + } + + ret = cpsw_ale_del_mcast(cpsw->ale, priv->ndev->broadcast, + 0, ALE_VLAN, vid); + if (ret != 0) { + dev_dbg(priv->dev, "Failed to delete multicast entry\n"); + ret = 0; + } + + return ret; +} + +static int cpsw_port_vlans_add(struct cpsw_priv *priv, + const struct switchdev_obj_port_vlan *vlan, + struct switchdev_trans *trans) +{ + bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; + bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; + u16 vid; + + if (switchdev_trans_ph_prepare(trans)) + return 0; + + for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { + int err; + + err = cpsw_port_vlan_add(priv, untagged, pvid, vid); + if (err) + return err; + } + + return 0; +} + +static int cpsw_port_vlans_del(struct cpsw_priv *priv, + const struct switchdev_obj_port_vlan *vlan) + +{ + u16 vid; + + for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { + int err; + + err = cpsw_port_vlan_del(priv, vid); + if (err) + return err; + } + + return 0; +} + +static int cpsw_port_mdb_add(struct cpsw_priv *priv, + struct switchdev_obj_port_mdb *mdb, + struct switchdev_trans *trans) +{ + struct cpsw_common *cpsw = priv->cpsw; + int port_mask; + int err; + + if (switchdev_trans_ph_prepare(trans)) + return 0; + + port_mask = BIT(priv->emac_port); + err = cpsw_ale_mcast_add_modify(cpsw->ale, mdb->addr, port_mask, + ALE_VLAN, mdb->vid, 0); + + return err; +} + +static int cpsw_port_mdb_del(struct cpsw_priv *priv, + struct switchdev_obj_port_mdb *mdb) + +{ + struct cpsw_common *cpsw = priv->cpsw; + int del_mask; + int err; + + del_mask = BIT(priv->emac_port); + err = cpsw_ale_mcast_del_modify(cpsw->ale, mdb->addr, del_mask, + ALE_VLAN, mdb->vid); + + return err; +} + +static int cpsw_port_obj_add(struct net_device *ndev, + const struct switchdev_obj *obj, + struct switchdev_trans *trans) +{ + struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); + struct switchdev_obj_port_mdb *mdb = SWITCHDEV_OBJ_PORT_MDB(obj); + struct cpsw_priv *priv = netdev_priv(ndev); + int err = 0; + + switch (obj->id) { + case SWITCHDEV_OBJ_ID_PORT_VLAN: + err = cpsw_port_vlans_add(priv, vlan, trans); + break; + case SWITCHDEV_OBJ_ID_PORT_MDB: + err = cpsw_port_mdb_add(priv, mdb, trans); + break; + default: + err = -EOPNOTSUPP; + break; + } + + return err; +} + +static int cpsw_port_obj_del(struct net_device *ndev, + const struct switchdev_obj *obj) +{ + struct switchdev_obj_port_vlan *vlan = SWITCHDEV_OBJ_PORT_VLAN(obj); + struct cpsw_priv *priv = netdev_priv(ndev); + int err = 0; + + switch (obj->id) { + case SWITCHDEV_OBJ_ID_PORT_VLAN: + err = cpsw_port_vlans_del(priv, vlan); + break; + case SWITCHDEV_OBJ_ID_PORT_MDB: + err = cpsw_port_mdb_del(priv, SWITCHDEV_OBJ_PORT_MDB(obj)); + break; + default: + err = -EOPNOTSUPP; + break; + } + + return err; +} + +static const struct switchdev_ops cpsw_port_switchdev_ops = { + .switchdev_port_attr_set = cpsw_port_attr_set, + .switchdev_port_attr_get = cpsw_port_attr_get, + .switchdev_port_obj_add = cpsw_port_obj_add, + .switchdev_port_obj_del = cpsw_port_obj_del, +}; + +void cpsw_port_switchdev_init(struct net_device *ndev) +{ + ndev->switchdev_ops = &cpsw_port_switchdev_ops; +} diff --git a/drivers/net/ethernet/ti/cpsw_switchdev.h b/drivers/net/ethernet/ti/cpsw_switchdev.h new file mode 100644 index 0000000..4940462 --- /dev/null +++ b/drivers/net/ethernet/ti/cpsw_switchdev.h @@ -0,0 +1,4 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#include + +void cpsw_port_switchdev_init(struct net_device *ndev);