Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6ACFDC54EED for ; Wed, 25 Jan 2023 19:51:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235773AbjAYTvT (ORCPT ); Wed, 25 Jan 2023 14:51:19 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48432 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235501AbjAYTvJ (ORCPT ); Wed, 25 Jan 2023 14:51:09 -0500 Received: from mail-ej1-x631.google.com (mail-ej1-x631.google.com [IPv6:2a00:1450:4864:20::631]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4557589BD for ; Wed, 25 Jan 2023 11:51:07 -0800 (PST) Received: by mail-ej1-x631.google.com with SMTP id az20so50682119ejc.1 for ; Wed, 25 Jan 2023 11:51:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=baylibre-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=49DCUG0FS6JWgdUUPYZw1kraoVWOTArEkjTgYYh32tM=; b=T3x6SlzyLZpgEUFsbjMT4SA+6d24DwTx7kCaFHQK6NA+tO62hkIfFFUC5i9HlGvCiY VO1SlLpwDGSuZfJJKYhiD+S0j0eVMaIL4ca2k/orI5EVJl27fYkjEu0M3tSnkawNjJlw oeAC6uAjZTp8awX53JRC3TjOu0yIv8IpLIP9JBNSM1Uj1sMjYltGPuojFVVBQkvtB8yw 1a+O+3D6ZFz/TbnfTmouKhL3+9tY8pUtJdA88EzMDdPGgKJB2JFoSR2q87wUwe+rc7m8 RKMkHbwEEsN1KvzR/ijJXBB5pKGtTjkjuE5L6FVGxZAslrQUiMn1yK9WtRFlnW30bePi 72sQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=49DCUG0FS6JWgdUUPYZw1kraoVWOTArEkjTgYYh32tM=; b=a6EkS6+R9Zf67saKeBo4k4hKQE/ufykJdGD4y9XWWPD0sXMK9sqndC8Zakn7JFWpFH clAN8IBR64Gv5ynILiioxovgGBQ6nchsj3nbhOa8Ns+sJv3JWt9hEGTPqDTFqbFjlxG6 MkDqn3VCYxYg1u2+qngG5wLFj2t6wZDZMajNpQxPh9vE0VpScud7zEK0ifBwFJYhmdZm A6KH8NKO8F19juwn5WVPhQdRheup+pwdEBJCfKTGDNy0zFGRZUG4MfgdaQhcJrJ7UdPG uUTqGYs6JE0YnOGOj+YSbaTiYk0UjjxyW/dvxFl/yp42KdPE4EDTVZlP4tj8UdYOwAXh 5gSw== X-Gm-Message-State: AFqh2kpE2jStFZvpGk0NdbTCfCS/yYplHueRt8sT9IsrylCejKQDpBeR jeWOPIBOTaDtXivlmr1xbsvPqA== X-Google-Smtp-Source: AMrXdXuaATdDQZz4skFXXx2TzmuGeba97qCYF/+ECEIFRTwjdFBu8vNPSrGcQNgwcWejnzb1i80U0w== X-Received: by 2002:a17:906:3515:b0:7c1:1b89:1fe0 with SMTP id r21-20020a170906351500b007c11b891fe0mr33115318eja.65.1674676266354; Wed, 25 Jan 2023 11:51:06 -0800 (PST) Received: from blmsp.fritz.box ([2001:4091:a247:815f:ef74:e427:628a:752c]) by smtp.gmail.com with ESMTPSA id s15-20020a170906454f00b00872c0bccab2sm2778830ejq.35.2023.01.25.11.51.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 25 Jan 2023 11:51:06 -0800 (PST) From: Markus Schneider-Pargmann To: Marc Kleine-Budde , Chandrasekar Ramakrishnan , Wolfgang Grandegger Cc: Vincent MAILHOL , linux-can@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Markus Schneider-Pargmann Subject: [PATCH v2 02/18] can: tcan4x5x: Check size of mram configuration Date: Wed, 25 Jan 2023 20:50:43 +0100 Message-Id: <20230125195059.630377-3-msp@baylibre.com> X-Mailer: git-send-email 2.39.0 In-Reply-To: <20230125195059.630377-1-msp@baylibre.com> References: <20230125195059.630377-1-msp@baylibre.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org To reduce debugging effort in case the mram is misconfigured, add this size check of the DT configuration. Currently if the mram configuration doesn't fit into the available MRAM it just overwrites other areas of the MRAM. Signed-off-by: Markus Schneider-Pargmann --- drivers/net/can/m_can/m_can.c | 16 ++++++++++++++++ drivers/net/can/m_can/m_can.h | 1 + drivers/net/can/m_can/tcan4x5x-core.c | 5 +++++ 3 files changed, 22 insertions(+) diff --git a/drivers/net/can/m_can/m_can.c b/drivers/net/can/m_can/m_can.c index 8e83d6963d85..8ccf20f093f8 100644 --- a/drivers/net/can/m_can/m_can.c +++ b/drivers/net/can/m_can/m_can.c @@ -1886,6 +1886,22 @@ static int register_m_can_dev(struct net_device *dev) return register_candev(dev); } +int m_can_check_mram_cfg(struct m_can_classdev *cdev, u32 mram_max_size) +{ + u32 total_size; + + total_size = cdev->mcfg[MRAM_TXB].off - cdev->mcfg[MRAM_SIDF].off + + cdev->mcfg[MRAM_TXB].num * TXB_ELEMENT_SIZE; + if (total_size > mram_max_size) { + dev_err(cdev->dev, "Total size of mram config(%u) exceeds mram(%u)\n", + total_size, mram_max_size); + return -EINVAL; + } + + return 0; +} +EXPORT_SYMBOL_GPL(m_can_check_mram_cfg); + static void m_can_of_parse_mram(struct m_can_classdev *cdev, const u32 *mram_config_vals) { diff --git a/drivers/net/can/m_can/m_can.h b/drivers/net/can/m_can/m_can.h index a839dc71dc9b..d8150d8128e7 100644 --- a/drivers/net/can/m_can/m_can.h +++ b/drivers/net/can/m_can/m_can.h @@ -101,6 +101,7 @@ int m_can_class_register(struct m_can_classdev *cdev); void m_can_class_unregister(struct m_can_classdev *cdev); int m_can_class_get_clocks(struct m_can_classdev *cdev); int m_can_init_ram(struct m_can_classdev *priv); +int m_can_check_mram_cfg(struct m_can_classdev *cdev, u32 mram_max_size); int m_can_class_suspend(struct device *dev); int m_can_class_resume(struct device *dev); diff --git a/drivers/net/can/m_can/tcan4x5x-core.c b/drivers/net/can/m_can/tcan4x5x-core.c index 2342aa011647..e706518176e4 100644 --- a/drivers/net/can/m_can/tcan4x5x-core.c +++ b/drivers/net/can/m_can/tcan4x5x-core.c @@ -80,6 +80,7 @@ TCAN4X5X_MCAN_IR_RF1F) #define TCAN4X5X_MRAM_START 0x8000 +#define TCAN4X5X_MRAM_SIZE 0x800 #define TCAN4X5X_MCAN_OFFSET 0x1000 #define TCAN4X5X_CLEAR_ALL_INT 0xffffffff @@ -307,6 +308,10 @@ static int tcan4x5x_can_probe(struct spi_device *spi) if (!mcan_class) return -ENOMEM; + ret = m_can_check_mram_cfg(mcan_class, TCAN4X5X_MRAM_SIZE); + if (ret) + goto out_m_can_class_free_dev; + priv = cdev_to_priv(mcan_class); priv->power = devm_regulator_get_optional(&spi->dev, "vsup"); -- 2.39.0