From patchwork Sat Dec 16 16:12:04 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13495623 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f53.google.com (mail-wm1-f53.google.com [209.85.128.53]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D3BB430352; Sat, 16 Dec 2023 16:12:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DNUXbh4s" Received: by mail-wm1-f53.google.com with SMTP id 5b1f17b1804b1-40c29f7b068so18275115e9.0; Sat, 16 Dec 2023 08:12:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702743133; x=1703347933; darn=vger.kernel.org; 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=cokl3LuwGBmLMkAOd5vR5wXsVLEjgGEmnr03uCIMe70=; b=DNUXbh4szx25rnYEsnFQTGejWvxVejXYw2i0vmHusz4q5bvcLwA/gzvCRK8wk/m3uq 6jCEvWq9SFfsDaxhpBUpTx/X+TLs3EFccr+BEyQCEm/vijKsf1TBNcAle9Fw5ttnTqq1 syarkaqZY9SMQ9ARPOWf7gZPvmvHvKBJTSORiswG9ALNvMzcfDOLjpjAWgzkbDMN+1Pe 7j+yJtr0/u3MkOdV7xilpe5KuimWcylMDz4f+aX30zTafDNAxWlZhSDEJPN9x+cAJZVK DvUgP3mjVraFtgqUoz5+LEFUlCfwCv/Vt9y0qj5reky/7x5INkO6apVQxtfNVF2yy148 Olwg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702743133; x=1703347933; 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=cokl3LuwGBmLMkAOd5vR5wXsVLEjgGEmnr03uCIMe70=; b=Ktld7DaXdYXlQUNhQD59LA4HTXQTu5Lf59jiUCt7JUTICEXtRpK0VRzl6S3U/XEUQ0 MW32Emg8nen/neJn/OGdFLFTiSZcFh2TvDkogF00mLKFfHBP55DNT/ZELuX/rB+dK1Nq GeVo8GNUxuQ3D/OuS/FUEIdbn4Z3y/jltFF4kv2XiT4sKga+d2Yi7+8QesFHa8EqE9ZF sCYp2SjIUb9LrXc0zM3H9ELoNbzh84m5fNJgIONQlya+ickw19jSsYf0+bncWrRcBr5p 7o8YadVRySSTlkp2zjTHNaeuv0M0g273HlnGlBjaX4YfMt6U9VyP5mrmuJhUayDKsy2e gcRA== X-Gm-Message-State: AOJu0YxbzWm9vXrZdaupWT53N8jNlFgo7CIk4qAYU/c23C+4qprVaULO TCdjNqOP2/sAh7U7S14mKCQ= X-Google-Smtp-Source: AGHT+IGWclf3Klwf2dyJ2veSDySklMp4+jGhuWI8fOrteSyRGofMF6BX5BpyVedw9ED/jgKEHbBIdA== X-Received: by 2002:a05:600c:1e02:b0:40c:6862:4244 with SMTP id ay2-20020a05600c1e0200b0040c68624244mr1630089wmb.111.1702743132962; Sat, 16 Dec 2023 08:12:12 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id g9-20020a05600c310900b0040c3953cda5sm30818925wmo.45.2023.12.16.08.12.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 08:12:12 -0800 (PST) From: Christian Marangi To: Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Christian Marangi Subject: [net-next PATCH v4 1/3] net: phy: refactor and better document phy_speeds function Date: Sat, 16 Dec 2023 17:12:04 +0100 Message-Id: <20231216161206.1729-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231216161206.1729-1-ansuelsmth@gmail.com> References: <20231216161206.1729-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Refactor the phy_speeds function to be more readable and understandable and add some documentation on it. While on it extend it to take NULL speeds values to make it return only the count of speed modes in the passed mask. Signed-off-by: Christian Marangi --- drivers/net/phy/phy-core.c | 50 ++++++++++++++++++++++++++++++++++---- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/drivers/net/phy/phy-core.c b/drivers/net/phy/phy-core.c index 966c93cbe616..f3f5e4c41c97 100644 --- a/drivers/net/phy/phy-core.c +++ b/drivers/net/phy/phy-core.c @@ -317,17 +317,57 @@ phy_lookup_setting(int speed, int duplex, const unsigned long *mask, bool exact) } EXPORT_SYMBOL_GPL(phy_lookup_setting); +/** + * phy_speeds - return all speeds in mask + * @speeds: pointer to array where to put the speed modes + * @size: size of array where to put the speed modes + * @mask: mask of speed modes to compare with + * + * Take mask, test bit in mask with the settings table and compose the + * speeds array based on that as many as size permits. + * + * With speeds NULL, only the number of detected modes is returned and + * no array is composed. (size value is ignored) + * + * Return: the number of detected modes in mask. + */ size_t phy_speeds(unsigned int *speeds, size_t size, unsigned long *mask) { + unsigned int curr_speed; size_t count; int i; - for (i = 0, count = 0; i < ARRAY_SIZE(settings) && count < size; i++) - if (settings[i].bit < __ETHTOOL_LINK_MODE_MASK_NBITS && - test_bit(settings[i].bit, mask) && - (count == 0 || speeds[count - 1] != settings[i].speed)) - speeds[count++] = settings[i].speed; + for (i = 0, count = 0; i < ARRAY_SIZE(settings); i++) { + /* Inconsistent mapping with ethtool modes? */ + if (unlikely(settings[i].bit >= __ETHTOOL_LINK_MODE_MASK_NBITS)) + return count; + + /* Skip. Speed not in provided mask */ + if (!test_bit(settings[i].bit, mask)) + continue; + + /* settings struct is set in descending order with + * ordered speed modes. Detect a new speed mode by + * checking if it's different than the current one. + */ + if (count == 0 || curr_speed != settings[i].speed) { + curr_speed = settings[i].speed; + + /* With speeds not declared, we return only + * the number of detected speed mode in the mask. + */ + if (speeds) { + /* No more space to put new modes */ + if (count > size) + return count; + + speeds[count] = curr_speed; + } + + count++; + } + } return count; } From patchwork Sat Dec 16 16:12:05 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13495624 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39AF330646; Sat, 16 Dec 2023 16:12:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TNJH/GEd" Received: by mail-wm1-f47.google.com with SMTP id 5b1f17b1804b1-40c39e936b4so18813905e9.1; Sat, 16 Dec 2023 08:12:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702743134; x=1703347934; darn=vger.kernel.org; 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=vCjqm7RfhFeKXkonYo6dSLfvs7PV8vamdp/VJ5ZVACE=; b=TNJH/GEd2VoRbgEdcgG1OZYsvDyq+Pw74SZaorSLxpXww4hqBAHO7UagdEOFEdXZY2 L9krvocd6yxJGx0EqCYem5Q2ZP7EiFooDZ8m8HqkFTBstgAk+DPZ9PaGtV2NcvW+0CvF 0DKdyUlaChUTcF3cvqfu2Wdo//nwFFfJcg2Ot9kEyvNRxhpU2RL2uMjoQ7Cpb6w5M6Np 6Df0wtel8eUHZxeXq/EqS6wes1kcmkE1PGEgIyI2UPnerQq0a00EPJDupCOtkEphTfmg l7cI8JDbQGaHuBn1sCEx6uktFlylIctaTXO+os43C9uVRfBVah/ZpaFKJqZRykkI/G45 qatg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702743134; x=1703347934; 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=vCjqm7RfhFeKXkonYo6dSLfvs7PV8vamdp/VJ5ZVACE=; b=Iqo0iDUllqhHez9fIRBpxVLTA04xFNr4dL2Pql+tC6HfQ/Ta1LIyW6eNHIxOYkC3V4 HSvd1LpwtbK37WJvhrdY3AjzRDBFmWYCFl7ow3r/2ZyKy8h1+Oy7r4vGBL9tff9JgXs5 NVryJnXxKrMfIsFP5YYTUYdzhe8zHY/gNN3y909h9FHSvq41Nfwo00KkvvwgASs7qiuU TxrI+hQYoH9x4zXTkwlBGoR2YyXlbG8KcuhLrk0/VBxwgazpW9G1WSpfb9q5BMHwoSYd 0DDIG5cWmkaARJd38H7rgmikxm9te0FEUgSXWECcHxc1D6p38v6bYRu+1KC26gP3gP/n XCCA== X-Gm-Message-State: AOJu0Yz/pIJwkdBIqqhn1eLbT77IhhucdhyWfI6suZmEcagmWrLxwMJP dvEa/o7PeFQuwc+xvFaylDo= X-Google-Smtp-Source: AGHT+IFbKzoSnlr83q8OVyknj1B3OnTkB3mec+uQYFrlXhfwIVZ7oX0oP0tiA/66h2gLbabFWEWLRA== X-Received: by 2002:a7b:ca54:0:b0:40c:2db0:c803 with SMTP id m20-20020a7bca54000000b0040c2db0c803mr3467156wml.92.1702743134091; Sat, 16 Dec 2023 08:12:14 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id g9-20020a05600c310900b0040c3953cda5sm30818925wmo.45.2023.12.16.08.12.13 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 08:12:13 -0800 (PST) From: Christian Marangi To: Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Christian Marangi Subject: [net-next PATCH v4 2/3] net: phy: add simple helper to return count of supported speeds Date: Sat, 16 Dec 2023 17:12:05 +0100 Message-Id: <20231216161206.1729-3-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231216161206.1729-1-ansuelsmth@gmail.com> References: <20231216161206.1729-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Add simple helper to return count of supported speeds for the passed PHY device. This can be useful to know the number of speed modes to dynamically allocate a speed array for it. Signed-off-by: Christian Marangi --- drivers/net/phy/phy.c | 12 ++++++++++++ include/linux/phy.h | 2 ++ 2 files changed, 14 insertions(+) diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c index a5fa077650e8..3e579dd8a5e7 100644 --- a/drivers/net/phy/phy.c +++ b/drivers/net/phy/phy.c @@ -229,6 +229,18 @@ phy_find_valid(int speed, int duplex, unsigned long *supported) return phy_lookup_setting(speed, duplex, supported, false); } +/** + * phy_supported_speeds_num - return the number of all speeds currently + * supported by a phy device + * @phy: The phy device to return supported speeds of. + * + * Returns: the number of supported speeds. + */ +unsigned int phy_supported_speeds_num(struct phy_device *phy) +{ + return phy_speeds(NULL, 0, phy->supported); +} + /** * phy_supported_speeds - return all speeds currently supported by a phy device * @phy: The phy device to return supported speeds of. diff --git a/include/linux/phy.h b/include/linux/phy.h index 3cc52826f18e..52aa415fab0f 100644 --- a/include/linux/phy.h +++ b/include/linux/phy.h @@ -202,6 +202,8 @@ static inline void phy_interface_set_rgmii(unsigned long *intf) __set_bit(PHY_INTERFACE_MODE_RGMII_TXID, intf); } +unsigned int phy_supported_speeds_num(struct phy_device *phy); + /* * phy_supported_speeds - return all speeds currently supported by a PHY device */ From patchwork Sat Dec 16 16:12:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 13495625 X-Patchwork-Delegate: kuba@kernel.org Received: from mail-wm1-f52.google.com (mail-wm1-f52.google.com [209.85.128.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7959530D10; Sat, 16 Dec 2023 16:12:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NX2dreHZ" Received: by mail-wm1-f52.google.com with SMTP id 5b1f17b1804b1-40c6ea99429so12275175e9.3; Sat, 16 Dec 2023 08:12:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702743136; x=1703347936; darn=vger.kernel.org; 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=v8KycpmoEY28RLHnfyVEt7HZvLA/Co1CVDklFA2THj0=; b=NX2dreHZxTuFC5btUwKWWLKVXY0n2rVS86Txrlg1vQQb9pjTFobTaeJeBNrXIyRBmi 5yT4eEa9RqWPW2hjLcvKePbi1192uFn45sCMTY3Iuh02e6yjfRtObb1OChN2ngv06/nA RmUrrdDrVxVDA3KuURIa/2xLDnIiz9iaRs2IcWKubEMSkGh5Tpz+2M9GZ1e0j985jnaD d9HEpNFtJXEHryycseodOjvTQIlYx/sbpEJ24/LXtuR0AOx6EeUK2QYqKJ+AGowygQtT I+elf2DFLW3qcClDpR7LZHeQEOf0g9eHst9IrdDGk3u9+WuCSTttZXGON5YjwnxRkVjV cboA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702743136; x=1703347936; 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=v8KycpmoEY28RLHnfyVEt7HZvLA/Co1CVDklFA2THj0=; b=cy08CKtaS3vpfXXSjWapb2tGrFylcVpjiY+GVYoOy4p1WD0GdTPv4mHHUTQSOiyTqp p5PUIUuR50Xvv5vrTAKGcx+5fYJB4J3rLD0dU9/OYhzPZO/jQIpaTTLkieBp4pDkgAXu hDBVNKupgoXSyTnfS3De2HsirxPxBgRujo+/qLeIVDQlg0jAIMaNAewFiJf/9N1lyOoz R7gJiozYJ2Iirk7ezptGPER3Izwy220BtLmtjW+445AGu+zYdCzL7T0r1B87IbSIfK+Q 4Svt+zHOP1sIT44/MRs4+GdLL9KtjNrX0JrskUjM72ckvgRYmo2H6gFzrqbaEIs1UYni vlHw== X-Gm-Message-State: AOJu0YzYqCEp+GkzSH/Hpw/1AQFZzLfcwltXm8XgsL6VUs+BxuMNucH+ Og794zLOG7+ddEgDuITXgEk= X-Google-Smtp-Source: AGHT+IG+PosLTFC/WuXxcQPX59iIK88iPx8jKXdJ2Reu1gJNxhJX7oPTHSzzmS6Op7WxrU2CkOXdOg== X-Received: by 2002:a05:600c:22d8:b0:40c:66a8:3567 with SMTP id 24-20020a05600c22d800b0040c66a83567mr1669571wmg.59.1702743135543; Sat, 16 Dec 2023 08:12:15 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id g9-20020a05600c310900b0040c3953cda5sm30818925wmo.45.2023.12.16.08.12.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 08:12:14 -0800 (PST) From: Christian Marangi To: Andrew Lunn , Heiner Kallweit , Russell King , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Christian Marangi Subject: [net-next PATCH v4 3/3] net: phy: led: dynamically allocate speed modes array Date: Sat, 16 Dec 2023 17:12:06 +0100 Message-Id: <20231216161206.1729-4-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231216161206.1729-1-ansuelsmth@gmail.com> References: <20231216161206.1729-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-Patchwork-Delegate: kuba@kernel.org Instead of defining a big enough array for speed modes use the newly introduced API to get the actual number of supported speed modes and dynamically allocate them. Allocated space is freed at the end of the LED register loop. Signed-off-by: Christian Marangi --- drivers/net/phy/phy_led_triggers.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/drivers/net/phy/phy_led_triggers.c b/drivers/net/phy/phy_led_triggers.c index f550576eb9da..3b962ec13d90 100644 --- a/drivers/net/phy/phy_led_triggers.c +++ b/drivers/net/phy/phy_led_triggers.c @@ -83,14 +83,21 @@ static void phy_led_trigger_unregister(struct phy_led_trigger *plt) int phy_led_triggers_register(struct phy_device *phy) { + unsigned int *speeds; int i, err; - unsigned int speeds[50]; - phy->phy_num_led_triggers = phy_supported_speeds(phy, speeds, - ARRAY_SIZE(speeds)); + phy->phy_num_led_triggers = phy_supported_speeds_num(phy); if (!phy->phy_num_led_triggers) return 0; + speeds = kmalloc_array(phy->phy_num_led_triggers, sizeof(*speeds), + GFP_KERNEL); + if (!speeds) + return -ENOMEM; + + /* Presence of speed modes already checked up */ + phy_supported_speeds(phy, speeds, phy->phy_num_led_triggers); + phy->led_link_trigger = devm_kzalloc(&phy->mdio.dev, sizeof(*phy->led_link_trigger), GFP_KERNEL); @@ -123,6 +130,8 @@ int phy_led_triggers_register(struct phy_device *phy) phy->last_triggered = NULL; phy_led_trigger_change_speed(phy); + kfree(speeds); + return 0; out_unreg: while (i--) @@ -134,6 +143,7 @@ int phy_led_triggers_register(struct phy_device *phy) devm_kfree(&phy->mdio.dev, phy->led_link_trigger); phy->led_link_trigger = NULL; out_clear: + kfree(speeds); phy->phy_num_led_triggers = 0; return err; }