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: 179865 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp306356dyi; Sat, 16 Dec 2023 08:13:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IFHUUfVps5dweaVoeBc0win02BeoXnjR5iu46EFjgSzVKNB0oW2LGQS1jis+hZiI0C64Qd/ X-Received: by 2002:a17:902:d492:b0:1d3:6a11:1fc6 with SMTP id c18-20020a170902d49200b001d36a111fc6mr5122201plg.10.1702743187042; Sat, 16 Dec 2023 08:13:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702743187; cv=none; d=google.com; s=arc-20160816; b=mREV1XQz7/fE9SM2486oq5+RBdBw8rGL1x8byDdontqWbVoHC3PJqLCWN2g5EAxjx+ xNDPRxyFqvML64cdb9OhrcOMbPHbXmBfuMM/KHWjZKRwIs8iaaiKllIjFR+JCQjMGpjN eTYvFQj+2EwONViY2lXpPhdNAaT1fR1u6+j1ZeKGWfLegwzo/IMXp1KRGC/VigAnE4ib 8wmfMtEGU1iFy4KKXMReuM8opUSzayJWt7iCzn+qcY6IgQW8UBtSBjY3Vmlf56PsSlTL xEBlWAhhMA/53FVNMWkOd7KRnxyb7dLSochtGZybXeyi5MNMvSr5mLBWzVO4zu12zsoQ qgVQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=cokl3LuwGBmLMkAOd5vR5wXsVLEjgGEmnr03uCIMe70=; fh=utmrxdkgikAlLxVfQI1kldUG+rNjhrLN+3fAlOYj01s=; b=k+qeqGUm4SqzUjAQtSp+qVm8OLDtjizTYbSSkl8oFSLF3Ldgciw8ZvIwa+XGketuxb qXgkgYTC0N7X5O/qYo69SN9L+3fxonGEaPg500pygpLmvjXaGYvURAX3VTMmLSgk1zFx MQDngnXX61nbPWjvuqJsMlE2Kpy8H/L0HBX10rk03nEeDo5LMU1LBmCSZcF1by2R5hKB cq0zEIPP7dN0sZoj+5G8rc2fAYu1nfP2xrFqNup+7D3yajXGyDf4mVcV9f1VjS5OVbKy kCzpJFA1LiBdBwizdO30Az4G4Cqx+/FW5tD84TiLDxr1vCpFkgMUK3PZ5Fx9QyUN0aJA HTUA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=DNUXbh4s; spf=pass (google.com: domain of linux-kernel+bounces-2242-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2242-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id h6-20020a170902704600b001d36d944044si4373618plt.119.2023.12.16.08.13.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 08:13:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2242-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=DNUXbh4s; spf=pass (google.com: domain of linux-kernel+bounces-2242-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2242-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id C9498284B1A for ; Sat, 16 Dec 2023 16:13:06 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 196D23172E; Sat, 16 Dec 2023 16:12:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="DNUXbh4s" X-Original-To: linux-kernel@vger.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 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-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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785455640492791229 X-GMAIL-MSGID: 1785455640492791229 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: 179866 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp306498dyi; Sat, 16 Dec 2023 08:13:26 -0800 (PST) X-Google-Smtp-Source: AGHT+IGFnIyu1TibygKv0resIP2Y+OHeT+/4TnYOSWPbygTzm9W38IwFaJcxUCxkaP/uVBB8Le8K X-Received: by 2002:a05:6602:641f:b0:7b4:28f8:df with SMTP id gn31-20020a056602641f00b007b428f800dfmr19898252iob.40.1702743206185; Sat, 16 Dec 2023 08:13:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702743206; cv=none; d=google.com; s=arc-20160816; b=kunsCin+t5odZDemJZeaWusfXddkMacpCIuY9d1m8X/5wk3e7NPPj9BfsaIQUsuE3S RRQsnbvxZPf8NSxdMl+VSHXD8eK6N8PIrkJsu8kUzg9GhGpj4I6Xkt8beGbPztEHIP6/ zL+x4LSk0Y6CEpCe0g7BT83vyDgTHQ2VLRprxLnX6x6nLvqIxJ0PuOKCzf9OAkH5j2zY JTyNhV6NpzhJe9XDCusinLHfiWkcMjh8O2qF1LPsMwOX3JdwLdQ4sGw5DbBYe2Gt5o1W bxASBsGUAYTRmbaefTj6bL0eqknvqLvb3FqxuFrc02de7BZvwYAhW4RlmGzOw4tTRcK+ FCJg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=vCjqm7RfhFeKXkonYo6dSLfvs7PV8vamdp/VJ5ZVACE=; fh=utmrxdkgikAlLxVfQI1kldUG+rNjhrLN+3fAlOYj01s=; b=pNQiF2u+eFRlEvwkbjRTNlrjwsK1ZjtdUUKmJ0MKQ6u43p3d8xx3I9iRM9JxePcWWC ylAUKelDSFieWw4OXER6bT23c11CRitaLg0jbECXXcPX/Qlrsm1xRtzTgb3G1YZIiDTk N/mAdDMEIY24sU4IeBkLXJaF6tx9Y0vmdquv1QRwXUOuELDarFXf/3ji+Qpyf6OuXSRr NzGVnQmHyiP/T9rZxmxp4gg9w0XYpmdINVvyr42O3UWtv/2eIB85738iVxHIz5rIq5nY vUb81nWORRxj3dDH1SWenNy8I7ul2PtcqdJ4GDO1tW4h//gx+IdC/TcWGwEwc1ecRt+R 8vLw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="TNJH/GEd"; spf=pass (google.com: domain of linux-kernel+bounces-2243-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2243-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id j8-20020a635948000000b00578ca217740si14390556pgm.711.2023.12.16.08.13.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 08:13:26 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2243-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b="TNJH/GEd"; spf=pass (google.com: domain of linux-kernel+bounces-2243-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2243-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id E6FE628514A for ; Sat, 16 Dec 2023 16:13:25 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 74BF831A94; Sat, 16 Dec 2023 16:12:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="TNJH/GEd" X-Original-To: linux-kernel@vger.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 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-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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785455659863556559 X-GMAIL-MSGID: 1785455659863556559 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: 179867 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:24d3:b0:fb:cd0c:d3e with SMTP id r19csp306561dyi; Sat, 16 Dec 2023 08:13:34 -0800 (PST) X-Google-Smtp-Source: AGHT+IFcDVddiz0nQ+6/Odu29ZFXvNYpxKwZHmSbIZ96VWlP5+al759MIISvMKdSNQ9PrJKB88eo X-Received: by 2002:a05:6a20:7f9b:b0:190:1b16:3558 with SMTP id d27-20020a056a207f9b00b001901b163558mr19532446pzj.2.1702743214235; Sat, 16 Dec 2023 08:13:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702743214; cv=none; d=google.com; s=arc-20160816; b=J4U9xKpTqxbw+EkmnvVtVZtHmfNXTyMK7HG2nhO+MbtddauoupEI8RxMSmcOsICQhj qAv6dScZn6TInPCneXy+D3D637EBKRh7eUPbQojSwRGfEigxu8+RPEDkFinCjw696Wj1 wbIjBHoXCf2qvH+YO4h2MiRV6YhxU+g1u18xPWm7zx6bjhqFREX2Uoqj+JydMCGJRya0 CWUsznIWeLtJd5G2SPEWHUA9rCPHgyrDHaIPhbaza+AU12h2TGOhV7Iz70s0C+EQ0Vwr CJeKIXwzpsYmKgSBSzKB1ae0YSsIJNyw+ljcPirbjT/VnhS6b6AHiZ9OoiLeUH7UTFwX qMeg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=v8KycpmoEY28RLHnfyVEt7HZvLA/Co1CVDklFA2THj0=; fh=utmrxdkgikAlLxVfQI1kldUG+rNjhrLN+3fAlOYj01s=; b=rTGKs+W6xfWOIG/jYlyeLu9z/UY2ogqFgvB1hj740UyT1nLnYHe8I8bCfu8kGih2RB cNAUqiJItyfeKdS9U2MCxxpT8UOsUW97sIzfKcHjGhzNSqKmRXb79nh9/TuYvaUuGmRS PpQWpwL/N0VTZOtGFV0PRhSuI6UEXRXdl4V4Lw3s0MO/6TgvcFk0ygXdJj1nXcAsrElm lrsERvR0N4FypQGQrg8SxL+VqC4/Mn9pHMVYEWtrLv3npzKZvCUEFLw1p0/M3hc65dDR yhMOtMqc3BNXWy4bVMYPBE475BApTnaqJpiskcuV3BDoW4ZqCRYnWq2KREcTasToAjR5 a4cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=NX2dreHZ; spf=pass (google.com: domain of linux-kernel+bounces-2244-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2244-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id f20-20020a056a00239400b006d0c02f0070si6970455pfc.353.2023.12.16.08.13.34 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Dec 2023 08:13:34 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-2244-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=NX2dreHZ; spf=pass (google.com: domain of linux-kernel+bounces-2244-ouuuleilei=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-2244-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sv.mirrors.kernel.org (Postfix) with ESMTPS id 09D7B285104 for ; Sat, 16 Dec 2023 16:13:34 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AEFFC321BB; Sat, 16 Dec 2023 16:12:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="NX2dreHZ" X-Original-To: linux-kernel@vger.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 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-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: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1785455668591689334 X-GMAIL-MSGID: 1785455668591689334 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; }