Received: by 10.192.165.148 with SMTP id m20csp3678300imm; Mon, 23 Apr 2018 10:25:24 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqGPyGkt5uC+u42HuWSaTtXly+sWwuVpQ10MEd3YVEO0u434VU/h4oIWXZw1oWmUZUluY8d X-Received: by 10.98.9.27 with SMTP id e27mr684333pfd.93.1524504324084; Mon, 23 Apr 2018 10:25:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1524504324; cv=none; d=google.com; s=arc-20160816; b=ddOJUgJq0uGqeYICumxyLp77Xswc3l/eh5oWICAQR0PeTe7Z1pKPJEnR9MPVOWjgYA TfdkEO7d2ofwFrtQf/5rYG5FGKro6JwFPdYoRt2sZTeEHzniotx5wvlzm7mWxwNoEFJj dkWVOOuYkzqPtJVa3lnHDfv4l1L5s33yEM/xtXomVed/KQTmsFo2DW4FmNnnereJ6tQI jLBXsn4fa87LafuP7xfrHIKOxnUFwR0LAAlAxfbtp1y1NPTE8H3MxwzJpwJRoRd1pe/g a8YdnJqACyqrq7bRVtOwcPFSoFwitWNe/G68db3/2D/5CQfCv7FTZ1wogbioWMA+OqDC omoA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dmarc-filter:dkim-signature:dkim-signature :arc-authentication-results; bh=+8YWo3m/YJ39GaF9oL2LgxL0e+5uU1g9oB/tdlzoTts=; b=A9itvbfAQvyI/HAIIT+6kvXAc3dwRJJU9SkV2di6cC29u6wE9UnCcptknwaAoYiaCE +S5pH5PdI+nbfR6G79BwxlM6f6nBPiZW+ija8g9731hq2BHsuskIW6/mq9rtCfp0+NF0 K4yMJASlgbosxmXQ0NNIDbLxhR8lVMkWT2GFwDRYjDfq7IyJGISQ/CA1Gcm1NIsZZjHq wa9Omlsr3/czHl0dk8Lc+IcXaEGq8UJy5Gf6/U243zbwXPxzf5kNdKtqw6DVOjeaD2XN b0fnPfPlPoGDhaDo6Ou7mPyPJ2dPE4bLTfmsL49/LLQD633KhTM+EylcIqyloebl9yaO hc2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@codeaurora.org header.s=default header.b=ZImNDd1a; dkim=pass header.i=@codeaurora.org header.s=default header.b=YcntsijO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id p6si11460069pfg.329.2018.04.23.10.25.09; Mon, 23 Apr 2018 10:25:24 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-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=@codeaurora.org header.s=default header.b=ZImNDd1a; dkim=pass header.i=@codeaurora.org header.s=default header.b=YcntsijO; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932161AbeDWRWj (ORCPT + 99 others); Mon, 23 Apr 2018 13:22:39 -0400 Received: from smtp.codeaurora.org ([198.145.29.96]:57984 "EHLO smtp.codeaurora.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932141AbeDWRWd (ORCPT ); Mon, 23 Apr 2018 13:22:33 -0400 Received: by smtp.codeaurora.org (Postfix, from userid 1000) id 6AB8D60881; Mon, 23 Apr 2018 17:22:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1524504152; bh=ghtEtkT5OQCOWN1gviEhzkZjAlcUVvQuBlyQZE+Ixu4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=ZImNDd1aPBOWEnZr1HbLeciKujW64QqNdSwX1S9Ou8fTTw/tH8tb+zoTdaKaCWXDh EzNfE6QMUDrY0YCuD96s23uUejdT/xaYrIdHN4N9kng8MchSNFFG8lA2BJ74mFKbYz xtqRYjeq/BGongBBFL0rqLyswcLTW4CKef4wu+4g= X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on pdx-caf-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.8 required=2.0 tests=ALL_TRUSTED,BAYES_00, DKIM_SIGNED,T_DKIM_INVALID autolearn=no autolearn_force=no version=3.4.0 Received: from localhost (i-global254.qualcomm.com [199.106.103.254]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: ilina@smtp.codeaurora.org) by smtp.codeaurora.org (Postfix) with ESMTPSA id CB0F360117; Mon, 23 Apr 2018 17:22:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=codeaurora.org; s=default; t=1524504151; bh=ghtEtkT5OQCOWN1gviEhzkZjAlcUVvQuBlyQZE+Ixu4=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=YcntsijOME/UqWspp03YWIiw2KwLQyY7L2NCRYn9TdORWbqf+S+9On9KsqD9H8R8s REy1fMyufy2QtpHrFJ2l36jQKx1q1CqwqWCVbda2kXSippuPpqfpg9YCpYAAh355VK UP0XKHKfqbaMgZiftJ/GROig9hnFdhzDEQhWA4t4= DMARC-Filter: OpenDMARC Filter v1.3.2 smtp.codeaurora.org CB0F360117 Authentication-Results: pdx-caf-mail.web.codeaurora.org; dmarc=none (p=none dis=none) header.from=codeaurora.org Authentication-Results: pdx-caf-mail.web.codeaurora.org; spf=none smtp.mailfrom=ilina@codeaurora.org Date: Mon, 23 Apr 2018 11:22:30 -0600 From: Lina Iyer To: Stephen Boyd Cc: Andy Gross , linux-arm-msm@vger.kernel.org, linux-soc@vger.kernel.org, linux-kernel@vger.kernel.org, Mahesh Sivasubramanian , Bjorn Andersson , Evan Green Subject: Re: [PATCH v3] soc: qcom: cmd-db: Make endian-agnostic Message-ID: <20180423172230.GD18235@codeaurora.org> References: <20180423162054.238544-1-swboyd@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline In-Reply-To: <20180423162054.238544-1-swboyd@chromium.org> User-Agent: Mutt/1.9.5 (2018-04-13) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Apr 23 2018 at 10:21 -0600, Stephen Boyd wrote: >This driver deals with memory that is stored in little-endian format. >Update the structures with the proper little-endian types and then >do the proper conversions when reading the fields. Note that we compare >the ids with a memcmp() because we already pad out the string 'id' field >to exactly 8 bytes with the strncpy() onto the stack. > >Cc: Mahesh Sivasubramanian >Cc: Lina Iyer >Cc: Bjorn Andersson >Cc: Evan Green >Signed-off-by: Stephen Boyd >--- > Tested-by: Lina Iyer >Changes from v2: > * Fixed rsc_offset conversion to use data_offset instead of header_offset > for finding first offset > * Tested and confirmed slave ids and aux data for ARC regulators look > the same as before > >Changes from v1: > * Fixed negation on probe time magic matches check to be correct > * Fixed kernel doc and renamed magic_num to magic > * Killed cmd_db_get_header_by_rsc_id() entirely > * Changed chars to u8s for simplicity of thinking about signedness > * Fixed cmd_db_get_header()'s memcmp to do the right thing by checking > for a match instead of the opposite > * Tested on real hardware > > drivers/soc/qcom/cmd-db.c | 131 ++++++++++++++++++++------------------ > 1 file changed, 69 insertions(+), 62 deletions(-) > >diff --git a/drivers/soc/qcom/cmd-db.c b/drivers/soc/qcom/cmd-db.c >index b5172049f608..a6f646295f06 100644 >--- a/drivers/soc/qcom/cmd-db.c >+++ b/drivers/soc/qcom/cmd-db.c >@@ -13,18 +13,9 @@ > > #define NUM_PRIORITY 2 > #define MAX_SLV_ID 8 >-#define CMD_DB_MAGIC 0x0C0330DBUL > #define SLAVE_ID_MASK 0x7 > #define SLAVE_ID_SHIFT 16 > >-#define ENTRY_HEADER(hdr) ((void *)cmd_db_header + \ >- sizeof(*cmd_db_header) + \ >- hdr->header_offset) >- >-#define RSC_OFFSET(hdr, ent) ((void *)cmd_db_header + \ >- sizeof(*cmd_db_header) + \ >- hdr.data_offset + ent.offset) >- > /** > * struct entry_header: header for each entry in cmddb > * >@@ -35,11 +26,11 @@ > * @offset: offset from :@data_offset, start of the data > */ > struct entry_header { >- u64 id; >- u32 priority[NUM_PRIORITY]; >- u32 addr; >- u16 len; >- u16 offset; >+ u8 id[8]; >+ __le32 priority[NUM_PRIORITY]; >+ __le32 addr; >+ __le16 len; >+ __le16 offset; > }; > > /** >@@ -53,30 +44,30 @@ struct entry_header { > * @reserved: reserved for future use. > */ > struct rsc_hdr { >- u16 slv_id; >- u16 header_offset; >- u16 data_offset; >- u16 cnt; >- u16 version; >- u16 reserved[3]; >+ __le16 slv_id; >+ __le16 header_offset; >+ __le16 data_offset; >+ __le16 cnt; >+ __le16 version; >+ __le16 reserved[3]; > }; > > /** > * struct cmd_db_header: The DB header information > * > * @version: The cmd db version >- * @magic_number: constant expected in the database >+ * @magic: constant expected in the database > * @header: array of resources > * @checksum: checksum for the header. Unused. > * @reserved: reserved memory > * @data: driver specific data > */ > struct cmd_db_header { >- u32 version; >- u32 magic_num; >+ __le32 version; >+ u8 magic[4]; > struct rsc_hdr header[MAX_SLV_ID]; >- u32 checksum; >- u32 reserved; >+ __le32 checksum; >+ __le32 reserved; > u8 data[]; > }; > >@@ -99,8 +90,34 @@ struct cmd_db_header { > * h/w accelerator and request a resource state. > */ > >+static const u8 CMD_DB_MAGIC[] = { 0xdb, 0x30, 0x03, 0x0c }; >+ >+static bool cmd_db_magic_matches(const struct cmd_db_header *header) >+{ >+ const u8 *magic = header->magic; >+ >+ return memcmp(magic, CMD_DB_MAGIC, ARRAY_SIZE(CMD_DB_MAGIC)) == 0; >+} >+ > static struct cmd_db_header *cmd_db_header; > >+ >+static inline void *rsc_to_entry_header(struct rsc_hdr *hdr) >+{ >+ u16 offset = le16_to_cpu(hdr->header_offset); >+ >+ return cmd_db_header->data + offset; >+} >+ >+static inline void * >+rsc_offset(struct rsc_hdr *hdr, struct entry_header *ent) >+{ >+ u16 offset = le16_to_cpu(hdr->data_offset); >+ u16 loffset = le16_to_cpu(ent->offset); >+ >+ return cmd_db_header->data + offset + loffset; >+} >+ > /** > * cmd_db_ready - Indicates if command DB is available > * >@@ -110,29 +127,20 @@ int cmd_db_ready(void) > { > if (cmd_db_header == NULL) > return -EPROBE_DEFER; >- else if (cmd_db_header->magic_num != CMD_DB_MAGIC) >+ else if (!cmd_db_magic_matches(cmd_db_header)) > return -EINVAL; > > return 0; > } > EXPORT_SYMBOL(cmd_db_ready); > >-static u64 cmd_db_get_u64_id(const char *id) >-{ >- u64 rsc_id = 0; >- u8 *ch = (u8 *)&rsc_id; >- >- strncpy(ch, id, min(strlen(id), sizeof(rsc_id))); >- >- return rsc_id; >-} >- >-static int cmd_db_get_header(u64 query, struct entry_header *eh, >+static int cmd_db_get_header(const char *id, struct entry_header *eh, > struct rsc_hdr *rh) > { > struct rsc_hdr *rsc_hdr; > struct entry_header *ent; > int ret, i, j; >+ u8 query[8]; > > ret = cmd_db_ready(); > if (ret) >@@ -141,18 +149,21 @@ static int cmd_db_get_header(u64 query, struct entry_header *eh, > if (!eh || !rh) > return -EINVAL; > >+ /* Pad out query string to same length as in DB */ >+ strncpy(query, id, sizeof(query)); >+ > for (i = 0; i < MAX_SLV_ID; i++) { > rsc_hdr = &cmd_db_header->header[i]; > if (!rsc_hdr->slv_id) > break; > >- ent = ENTRY_HEADER(rsc_hdr); >- for (j = 0; j < rsc_hdr->cnt; j++, ent++) { >- if (ent->id == query) >+ ent = rsc_to_entry_header(rsc_hdr); >+ for (j = 0; j < le16_to_cpu(rsc_hdr->cnt); j++, ent++) { >+ if (memcmp(ent->id, query, sizeof(ent->id)) == 0) > break; > } > >- if (j < rsc_hdr->cnt) { >+ if (j < le16_to_cpu(rsc_hdr->cnt)) { > memcpy(eh, ent, sizeof(*ent)); > memcpy(rh, rsc_hdr, sizeof(*rh)); > return 0; >@@ -162,15 +173,6 @@ static int cmd_db_get_header(u64 query, struct entry_header *eh, > return -ENODEV; > } > >-static int cmd_db_get_header_by_rsc_id(const char *id, >- struct entry_header *ent_hdr, >- struct rsc_hdr *rsc_hdr) >-{ >- u64 rsc_id = cmd_db_get_u64_id(id); >- >- return cmd_db_get_header(rsc_id, ent_hdr, rsc_hdr); >-} >- > /** > * cmd_db_read_addr() - Query command db for resource id address. > * >@@ -187,9 +189,9 @@ u32 cmd_db_read_addr(const char *id) > struct entry_header ent; > struct rsc_hdr rsc_hdr; > >- ret = cmd_db_get_header_by_rsc_id(id, &ent, &rsc_hdr); >+ ret = cmd_db_get_header(id, &ent, &rsc_hdr); > >- return ret < 0 ? 0 : ent.addr; >+ return ret < 0 ? 0 : le32_to_cpu(ent.addr); > } > EXPORT_SYMBOL(cmd_db_read_addr); > >@@ -207,19 +209,21 @@ int cmd_db_read_aux_data(const char *id, u8 *data, size_t len) > int ret; > struct entry_header ent; > struct rsc_hdr rsc_hdr; >+ u16 ent_len; > > if (!data) > return -EINVAL; > >- ret = cmd_db_get_header_by_rsc_id(id, &ent, &rsc_hdr); >+ ret = cmd_db_get_header(id, &ent, &rsc_hdr); > if (ret) > return ret; > >- if (len < ent.len) >+ ent_len = le16_to_cpu(ent.len); >+ if (len < ent_len) > return -EINVAL; > >- len = min_t(u16, ent.len, len); >- memcpy(data, RSC_OFFSET(rsc_hdr, ent), len); >+ len = min_t(u16, ent_len, len); >+ memcpy(data, rsc_offset(&rsc_hdr, &ent), len); > > return len; > } >@@ -238,9 +242,9 @@ size_t cmd_db_read_aux_data_len(const char *id) > struct entry_header ent; > struct rsc_hdr rsc_hdr; > >- ret = cmd_db_get_header_by_rsc_id(id, &ent, &rsc_hdr); >+ ret = cmd_db_get_header(id, &ent, &rsc_hdr); > >- return ret < 0 ? 0 : ent.len; >+ return ret < 0 ? 0 : le16_to_cpu(ent.len); > } > EXPORT_SYMBOL(cmd_db_read_aux_data_len); > >@@ -256,11 +260,14 @@ enum cmd_db_hw_type cmd_db_read_slave_id(const char *id) > int ret; > struct entry_header ent; > struct rsc_hdr rsc_hdr; >+ u32 addr; > >- ret = cmd_db_get_header_by_rsc_id(id, &ent, &rsc_hdr); >+ ret = cmd_db_get_header(id, &ent, &rsc_hdr); >+ if (ret < 0) >+ return CMD_DB_HW_INVALID; > >- return ret < 0 ? CMD_DB_HW_INVALID : >- (ent.addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK; >+ addr = le32_to_cpu(ent.addr); >+ return (addr >> SLAVE_ID_SHIFT) & SLAVE_ID_MASK; > } > EXPORT_SYMBOL(cmd_db_read_slave_id); > >@@ -282,7 +289,7 @@ static int cmd_db_dev_probe(struct platform_device *pdev) > return ret; > } > >- if (cmd_db_header->magic_num != CMD_DB_MAGIC) { >+ if (!cmd_db_magic_matches(cmd_db_header)) { > dev_err(&pdev->dev, "Invalid Command DB Magic\n"); > return -EINVAL; > } >-- >Sent by a computer through tubes >