Received: by 2002:a05:6358:9144:b0:117:f937:c515 with SMTP id r4csp3535852rwr; Sun, 7 May 2023 14:12:33 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4oaKNR87TATZXX07gdlGJD7JVq4H5nY+Cc4VdD3koLXXBiDjq1QzIwv6bCFZNZKNqT/1Nc X-Received: by 2002:a05:6a00:cca:b0:643:7fcf:836d with SMTP id b10-20020a056a000cca00b006437fcf836dmr11968626pfv.25.1683493953577; Sun, 07 May 2023 14:12:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1683493953; cv=none; d=google.com; s=arc-20160816; b=l23unm172l+f+0xq365ANw1PrYuhvyraf+qjoCdxEYkRKzF07DGsIHWYfuCH1cC74g BXCKzWM2k0mXwa0V9zMbqsmHNYScHDib3hQ2dLb7bnCQsUqX6dmayMN/SiMbesC8T1Th 5whepXdyBalasRvewourEiaXWARJEpKkEszeKeOfZk3b2xxDAME74vnlYCt5XULbU0st VER1G/yQ5aD/6U1Gkay0EowVamsx03WM5LYo+WCNwdTvQGBoAlcYD4GtjEJaDunYn/gG mSEK00u4zj6ZP7a7CaYN2qq/F0xUZh1Cr3OBjxyoSmfu5khq0LUXWThsd1LH3hhZQ2iL R7jA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=MqbJTT+sMaeKas/BpWr0IKDZ657iqFch3lMWKDzXqxY=; b=TUVl8jnJ/NKCP+uA9peQhWX2t22TipaP7V7JSQjM2NehmjdyY4t5/8rwZz7Xb4Wm9g QaZZTjO3ya29qi2TOL8+3xLtUbbyjlXKCn13btvlR0M+K82Wz4KWiB1zyck4DIfj47ha 4PPXhKoFznNz1KyGGYzXEvtijOp9mwqmS76n+8Zj6zWSFMdGHbVaEk3NZ77ym5QGJnhK TueP0R+8JdYmjLF4BEcY1d5FHJKisja/FPI6slMfRv3fDqiRIoh7Q9N5EhFA/BiwBwQT fbMRYRgyZ1zT/hif1ZC/9GA6iwrnB1xjf8S8fCNuVLnKw1Aq4oF50bghwGiKlLw6E5J6 wGJw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a15-20020aa795af000000b006439accb0a9si7202358pfk.183.2023.05.07.14.12.17; Sun, 07 May 2023 14:12:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232087AbjEGUi7 (ORCPT + 99 others); Sun, 7 May 2023 16:38:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39224 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229757AbjEGUi6 (ORCPT ); Sun, 7 May 2023 16:38:58 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 463C66A6D; Sun, 7 May 2023 13:38:53 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 482AE4B3; Sun, 7 May 2023 13:39:37 -0700 (PDT) Received: from e120937-lin (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 914253F663; Sun, 7 May 2023 13:38:51 -0700 (PDT) Date: Sun, 7 May 2023 21:38:49 +0100 From: Cristian Marussi To: Oleksii Moisieiev Cc: "sudeep.holla@arm.com" , Linus Walleij , Peng Fan , Michal Simek , "linux-kernel@vger.kernel.org" , "linux-arm-kernel@lists.infradead.org" , "linux-gpio@vger.kernel.org" , cristian.marussi@arm.com Subject: Re: [RFC v2 1/3] firmware: arm_scmi: Add SCMI v3.2 pincontrol protocol basic support Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, May 05, 2023 at 08:52:03PM +0100, Cristian Marussi wrote: > On Wed, Apr 26, 2023 at 01:26:37PM +0000, Oleksii Moisieiev wrote: > > scmi: Introduce pinctrl SCMI protocol driver > > > > Add basic implementation of the SCMI v3.2 pincontrol protocol > > excluding GPIO support. All pinctrl related callbacks and operations > > are exposed in the include/linux/scmi_protocol.h > > > > Hi Oleksii, Hi Oleksii, mostly replying to myself here really (O_o) ... see below. [snip] > > + > > +static int scmi_pinctrl_attributes_get(const struct scmi_protocol_handle *ph, > > + struct scmi_pinctrl_info *pi) > > +{ > > + int ret; > > + struct scmi_xfer *t; > > + struct scmi_msg_pinctrl_protocol_attributes *attr; > > + > > + if (!pi) > > + return -EINVAL; > > + > > + ret = ph->xops->xfer_get_init(ph, PROTOCOL_ATTRIBUTES, > > + 0, sizeof(*attr), &t); > > + if (ret) > > + return ret; > > + > > + attr = t->rx.buf; > > + > > + ret = ph->xops->do_xfer(ph, t); > > + if (!ret) { > > + pi->nr_functions = > > + le16_to_cpu(GET_FUNCTIONS_NR(attr->attributes_high)); > > + pi->nr_groups = le16_to_cpu(GET_GROUPS_NR(attr->attributes_low)); > > + pi->nr_pins = le16_to_cpu(GET_PINS_NR(attr->attributes_low)); > > I see a couple of issues here present in general all across this patch when > you use these macros; > > You should take care of the endianity in the RX msg payload BEFORE and THEN > DISSECT the bitfields AND as a consequence use also an _le helper that fits > the size of the type that you are processing as in (being attributes 32 bit > little endian in the msg payload): > > pi->nr_pins = GET_PINS_NR(le32_to_cpu(attr->attributes_low)); > > Now all works just because everything is little endian really so nothing > is done by these macros.... > Re-thinking about this, it turns out that the above advice of mine is just plain wrong :< ... being a bitfield access you cannot do what I badly advised you: GET_PINS_NR(le32_to_cpu(attr->attributes_low)); the right way is indeed how you did it originally...my bad... ... BUT at the same time if you check your original solution: le16_to_cpu(GET_FUNCTIONS_NR(attr->attributes_high)) with a static analyzer like sparse, which dutifully checks that __le32 style labeled are accessed in a congruent manner, I get a lot of: drivers/firmware/arm_scmi/pinctrl.c:136:25: warning: cast to restricted __le32 drivers/firmware/arm_scmi/pinctrl.c:136:25: warning: restricted __le32 degrades to integer drivers/firmware/arm_scmi/pinctrl.c:136:25: warning: restricted __le32 degrades to integer drivers/firmware/arm_scmi/pinctrl.c:136:25: warning: cast to restricted __le16 with your original solution and no errors with my broken proposal... :O ...this is due to the fact that indeed FIELD_GET & C. are not meant to be used on such __le32 endian-styled vars, so sparse warns you about that (but my bad advice remain broken even though fine for the static analyzer...) Digging into bitfield.h, indeed, turns out that near the end, there are defined some LE dedicated bitfield macros (https://lwn.net/Articles/741762/) like: u32 le32_get_bits(__le32 val, u32 field) So you can just convert your macros to use le32_get_bits (insted of FIELD_GET) and use those macros directly without the need for the additional le32_to_cpu conversion as in the example below. (that indeed gives me no more sparse errors) Hope not to have made to much noise :P Thanks, Cristian P.S.: I would also drop the RFC on V3 so that maybe a few more public check-bots will engage with your series ->8--- diff --git a/drivers/firmware/arm_scmi/pinctrl.c b/drivers/firmware/arm_scmi/pinctrl.c index 1392d15b3a58..66ea51606f46 100644 --- a/drivers/firmware/arm_scmi/pinctrl.c +++ b/drivers/firmware/arm_scmi/pinctrl.c @@ -14,15 +14,15 @@ #define REG_TYPE_BITS GENMASK(9, 8) #define REG_CONFIG GENMASK(7, 0) -#define GET_GROUPS_NR(x) FIELD_GET(GENMASK(31, 16), (x)) -#define GET_PINS_NR(x) FIELD_GET(GENMASK(15, 0), (x)) -#define GET_FUNCTIONS_NR(x) FIELD_GET(GENMASK(15, 0), (x)) +#define GET_GROUPS_NR(x) le32_get_bits((x), GENMASK(31, 16)) +#define GET_PINS_NR(x) le32_get_bits((x), GENMASK(15, 0)) +#define GET_FUNCTIONS_NR(x) le32_get_bits((x), GENMASK(15, 0)) -#define EXT_NAME_FLAG(x) FIELD_GET(BIT(31), (x)) -#define NUM_ELEMS(x) FIELD_GET(GENMASK(15, 0), (x)) +#define EXT_NAME_FLAG(x) le32_get_bits((x), BIT(31)) +#define NUM_ELEMS(x) le32_get_bits((x) ,GENMASK(15, 0)) -#define REMAINING(x) FIELD_GET(GENMASK(31, 16), (x)) -#define RETURNED(x) FIELD_GET(GENMASK(11, 0), (x)) +#define REMAINING(x) le32_get_bits((x), GENMASK(31, 16)) +#define RETURNED(x) le32_get_bits((x), GENMASK(11, 0)) enum scmi_pinctrl_protocol_cmd { PINCTRL_ATTRIBUTES = 0x3, @@ -132,10 +132,9 @@ static int scmi_pinctrl_attributes_get(const struct scmi_protocol_handle *ph, ret = ph->xops->do_xfer(ph, t); if (!ret) { - pi->nr_functions = - le16_to_cpu(GET_FUNCTIONS_NR(attr->attributes_high)); - pi->nr_groups = le16_to_cpu(GET_GROUPS_NR(attr->attributes_low)); - pi->nr_pins = le16_to_cpu(GET_PINS_NR(attr->attributes_low)); + pi->nr_functions = GET_FUNCTIONS_NR(attr->attributes_high); + pi->nr_groups = GET_GROUPS_NR(attr->attributes_low); + pi->nr_pins = GET_PINS_NR(attr->attributes_low); } ph->xops->xfer_put(ph, t); @@ -209,7 +208,7 @@ static int scmi_pinctrl_attributes(const struct scmi_protocol_handle *ph, ret = ph->xops->do_xfer(ph, t); if (!ret) { if (n_elems) - *n_elems = le32_to_cpu(NUM_ELEMS(rx->attributes)); + *n_elems = NUM_ELEMS(rx->attributes); strscpy(name, rx->name, SCMI_SHORT_NAME_MAX_SIZE); } @@ -253,8 +252,8 @@ static int iter_pinctrl_assoc_update_state(struct scmi_iterator_state *st, { const struct scmi_resp_pinctrl_list_assoc *r = response; - st->num_returned = le32_to_cpu(RETURNED(r->flags)); - st->num_remaining = le32_to_cpu(REMAINING(r->flags)); + st->num_returned = RETURNED(r->flags); + st->num_remaining = REMAINING(r->flags); return 0; } -8<---