From patchwork Thu Dec 14 15:49: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: 178946 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8897148dys; Thu, 14 Dec 2023 14:40:15 -0800 (PST) X-Google-Smtp-Source: AGHT+IFN9fjbL69gl1OQMHsNpWm7DYK8WkBSVNn00k73PnRtdlqVjFQxSvVklJEMU+KlZ/tt5iFS X-Received: by 2002:a05:6102:2b91:b0:466:6e68:157 with SMTP id ib17-20020a0561022b9100b004666e680157mr24540vsb.40.1702593614925; Thu, 14 Dec 2023 14:40:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702593614; cv=none; d=google.com; s=arc-20160816; b=uZXmfkTsAQYnGQRjRVMbSR/kl10g2I4ke//0eUTP8k/WNWVLCYF+idc9BLDEv/IEC6 pwNIYnxh9C+ySRzU/0Mj14zM7MWFbVvOodR+1JaA+IYrCEsLEfzoUpc50aRkU8tVXKzB 6OpFsqM7NNFRFOEZU+e2dilaCZ+dtsd++mMECknbOwTlyZQV4Akw3PphNyytoTq4764z Qzm9U0MixCURgc7yL3jP3+R8CcKIL0on8ozES51CxMt3dPvsgFX5Ea0eh7ZbjunVt0bF 5FLNu3wnzRNZ75EYyzWnCk+Viup2QCEvpnB6Vc0kaodNQ0QHp87uIhjwOE/ST9miQW5T zcSA== 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=PysnZlgIi6LjarqurpvkD4iKyhD/xp2NGOBU17rT3Ak=; fh=utmrxdkgikAlLxVfQI1kldUG+rNjhrLN+3fAlOYj01s=; b=wyT7g9VVvIQFZscsG7E6sDTFb7xsMMwZI3UYIOexwh6F4fpmRD9JDOVxtcU9iqKKxe fDQ/7L8Wldw7RlXGfcwc/4eYV/7cI/8BmuwI1yzXP92Y031zSKU9bDUT1njV/6OILD6S iJRg8XuffGInSBMqkmT3vIiVNNmGGCBqwSf30nGQ+Qhd6bEDi3bwuTckH2kCcdLkrIvn WgY96nrZYyfTrZKM97FecZQorv++UKOAYIY1ebwsxrKFQf1A35kKJZuFFJmlbJc8tWw+ ReSCPwZnCZyq6iH5jrDyp4YPVjOo71OdVw9U0z8MWDaHNjYvwDnR37Gvj04NthSrwvZ5 BOyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mC43kL+R; spf=pass (google.com: domain of linux-kernel+bounces-200-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id h26-20020ac846da000000b00425199e7e10si15443687qto.19.2023.12.14.14.40.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 14:40:14 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-200-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=mC43kL+R; spf=pass (google.com: domain of linux-kernel+bounces-200-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-200-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id B177D1C21014 for ; Thu, 14 Dec 2023 22:40:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 9B57B6E2DF; Thu, 14 Dec 2023 22:38:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="mC43kL+R" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f43.google.com (mail-wm1-f43.google.com [209.85.128.43]) (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 35F05697B1; Thu, 14 Dec 2023 22:38:35 +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-f43.google.com with SMTP id 5b1f17b1804b1-40c48d7a7a7so452265e9.3; Thu, 14 Dec 2023 14:38:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702593514; x=1703198314; 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=PysnZlgIi6LjarqurpvkD4iKyhD/xp2NGOBU17rT3Ak=; b=mC43kL+R6/vOPT2+r3M1kYbBsmolENg9LKxhrlVoDjkD2lSAyYjs+e2W8Uz6/6mSg4 X2tzOGUKENoqO88cQyrF3YamzVddHpgAwC3chAdzAB9+KBkUrOmgorZApj/zJk21c10F MTk4dPkPSdv5ROWd9/KNjm9bGFyLQox8MeHhg4BWCMsszhXPEHta0RyTlOawumOqmF8g kfjEdE94yIV2VOXxFyhFJTkG7QGxPFNCGwyg7s/LPv/2sMuMo3FGzGdZrf2yHqu4TE0d 1hkKfONvLevHe1Qy804euegk3RC1x9/Njm8mYk69hJoVrP5jJ8j0kP9NNi8x9JpOSigO GJMw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702593514; x=1703198314; 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=PysnZlgIi6LjarqurpvkD4iKyhD/xp2NGOBU17rT3Ak=; b=OOSUIa2JplBU2xETzN97Slgf13e9UtH+Y08Kt+7p0EkYYbRloN743vSCgVLXFH/X2L LrRFi+LGQZvVBW6IWSrC8eBQ8qrrXrJGvoQtlEFM/4f/0191kfvJmFt+TCZQ5t1jCsNG NrQUwHtqTUVjAeKThdk7/wqDKSAv8GiGmPm3nSMhe4/xMadOgctdgrt42HXtXP0TGQ6u 9vV4yC7t5GiuV1hhHrM1uRj9TdwyGvJmvxz6zTJmJaYO8hiutyPjmJdtNHieR9LzxxzK knGGsgE2PmRq0Oe/BFz2DzYqox03x4lz3+7anCQm8hgX4sStINYF8rvI09pIuiZK6v4C EW+A== X-Gm-Message-State: AOJu0YzQcvxGMlqcXsj+EV7hqL3FLG5QvWgER2cGoYc5Th98PZXBblqN NrCWbmzq3TkEubtz0rvSznA= X-Received: by 2002:a05:600c:2113:b0:40b:26f1:57a2 with SMTP id u19-20020a05600c211300b0040b26f157a2mr5276018wml.27.1702593513815; Thu, 14 Dec 2023 14:38:33 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id n41-20020a05600c3ba900b0040c61ee0816sm54746wms.0.2023.12.14.14.38.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 14:38:33 -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 v2 1/3] net: phy: refactor and better document phy_speeds function Date: Thu, 14 Dec 2023 16:49:04 +0100 Message-Id: <20231214154906.29436-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231214154906.29436-1-ansuelsmth@gmail.com> References: <20231214154906.29436-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: 1785298802367427217 X-GMAIL-MSGID: 1785298802367427217 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..9618d89458d1 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 Thu Dec 14 15:49: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: 178948 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8897232dys; Thu, 14 Dec 2023 14:40:24 -0800 (PST) X-Google-Smtp-Source: AGHT+IFVY2WS83TO2g6SV3kUDrvgfSi3gMviUDMG/MpBn5c4vYeXgwd/OKBFNZGEpGKKMSMdO/PI X-Received: by 2002:ac8:5913:0:b0:425:4043:762e with SMTP id 19-20020ac85913000000b004254043762emr15152796qty.86.1702593624180; Thu, 14 Dec 2023 14:40:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702593624; cv=none; d=google.com; s=arc-20160816; b=z9hZOd9C5bCkshuOOR+eZ4y4QOVnnLMVS3wM9xalbL2QIavACXYoWLJ/obSBM56Cg1 WcolMSRjjybIV5ZET64Qe60lFxoHjW1UKgtdDfflLW7c6TmqRtNN2rRchyB3qFMxT5Qy PXcqvj8NSy1ZqIN4QdLEDe34D2PCUHda9o3LO5NNzNbQGapvfRUax0FDxgZwNLEKb0q4 0PdkVnDjedqHPWWsXft0b9r4NumLH5iBvwZbSgYGDO8bcfiKpqo0VLbYuLq1ZhdGV12P UbN6y5+qp7NV4zzsp28clU1Lzlfh2UKy0vUo9txa+4286D9rOVzr2XG0zZlKkqi6H4Te 4ZlQ== 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=hAtxuDG+IL4Il9JbZckuESTLJk29St8uyddTrttAR4I=; fh=utmrxdkgikAlLxVfQI1kldUG+rNjhrLN+3fAlOYj01s=; b=KM6YT7j/TZ7LLNd/DPh9AQ1u+jY5umR0HHSMeAoD9ncsntWo6UbUoaawHDbm5/2GOr 01lRtOT18o5wkFuA2BArt7FCZHhAdNH4iSAb8p+uM8vudcF2QbyBhO/2+sPGbMUa/poe QNICBEql20HuGka/0rKm9QM4a8uvCI8sHpQp8vGxTSMKpsgR1L1y0pim9CFhFZXEgYuu nPp2TJYzHPtVQAo4ck/03BOWPCPdeJDsUu/5sMsPVqxbxiSrfcrdapVe22ZSd7YCehh6 tRk+rcuhtdV1O55oaGksnIr9Kqil4kE1lgjAHAOK0918BTOfDqy50b88tUDwYotRq6+Y UQBw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Zw+DbNAB; spf=pass (google.com: domain of linux-kernel+bounces-201-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id v5-20020a05622a130500b00425fe9c723dsi2422855qtk.351.2023.12.14.14.40.24 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 14:40:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-201-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Zw+DbNAB; spf=pass (google.com: domain of linux-kernel+bounces-201-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-201-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id EA56C1C21D86 for ; Thu, 14 Dec 2023 22:40:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 0B6E56E591; Thu, 14 Dec 2023 22:38:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Zw+DbNAB" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f44.google.com (mail-wm1-f44.google.com [209.85.128.44]) (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 DC2C36ABBE; Thu, 14 Dec 2023 22:38:36 +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-f44.google.com with SMTP id 5b1f17b1804b1-40c4846847eso628875e9.1; Thu, 14 Dec 2023 14:38:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702593515; x=1703198315; 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=hAtxuDG+IL4Il9JbZckuESTLJk29St8uyddTrttAR4I=; b=Zw+DbNABx8tUDhlvJpomxoamS1mGVUIcJBj6vD0+Y96eMfFml5U8Zc/JMYInK9Am+Z uzTI+8RNEagCrGS8Hu75yRhyFgi5ykTHuDHpqob3FrVlKlvcaLaIhWZWLVm6ytdt0LkN pLceZxKRHp0Etjo5NKMbgcwmdttx1cqZkL6aDnE9e+Re29+2QAMa5ebdWDxAiQxKrAxZ HZJnUTRADL+9q63Wzt5ltxuwu5j+w2afy9KTc6qBDTcQM1MQOvkUAR8VGxoRcYq9OMDs d7n/mgZzRviiQYO7xbuTTxuQMnR0cwiPni9K2rR4wVG5clCgQeq8SBUpJOLsCnsKL0B8 lzWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702593515; x=1703198315; 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=hAtxuDG+IL4Il9JbZckuESTLJk29St8uyddTrttAR4I=; b=ut5SwidPbSzZV7a5xbN5uQKGZ1aLYn4+rAa5f8/IJS8l9MRGP537wmkCuxMf41xibp gHQ7AH2/4VcCwCDW3ZaMbXLQ5FrPGyGZUy4U0MHb+vve9O2uak68KlzaAjFa0GUDaFmx xrzXxjbh5euXnL10ePjTnoQQPbmPjEccV5rpxKdZWpuP5BdFQspkqpU4fbV+8qLSTiBf 9UWAFK/cby+7geT3NCAtDeUF/gC1VeUJPOpWkehEV22gjp2Z5UrKH/GkuENINKob+e7U 9PlDj6rRKepSMUSlpdwStAE3FBuxmrEuzAMf7V5Mh4xsiMRhdY7LBL9IRxiSOf4v9HdS PjaA== X-Gm-Message-State: AOJu0YxwCiWGTkuxVINA8hpiAW3/UOYmEfMsQwAf/NV/STM9qegfdzpo st9epVjbFuU9G3UgyEKqNDk= X-Received: by 2002:a05:600c:1705:b0:40c:315e:9ec3 with SMTP id c5-20020a05600c170500b0040c315e9ec3mr5030141wmn.65.1702593515012; Thu, 14 Dec 2023 14:38:35 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id n41-20020a05600c3ba900b0040c61ee0816sm54746wms.0.2023.12.14.14.38.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 14:38:34 -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 v2 2/3] net: phy: add simple helper to return count of supported speeds Date: Thu, 14 Dec 2023 16:49:05 +0100 Message-Id: <20231214154906.29436-3-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231214154906.29436-1-ansuelsmth@gmail.com> References: <20231214154906.29436-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: 1785298812182016528 X-GMAIL-MSGID: 1785298812182016528 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 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/drivers/net/phy/phy.c b/drivers/net/phy/phy.c index a5fa077650e8..311560e72126 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. + * + * Description: 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. From patchwork Thu Dec 14 15:49: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: 178947 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp8897222dys; Thu, 14 Dec 2023 14:40:23 -0800 (PST) X-Google-Smtp-Source: AGHT+IGfKHX0OPKEm3bMTYOP7EcvrN21N//8frQD2LOD5O25jtOzsFv1sUNU/ZENjyC2TQ5XpI6Y X-Received: by 2002:ac8:5994:0:b0:423:7614:fbe8 with SMTP id e20-20020ac85994000000b004237614fbe8mr15950534qte.2.1702593623533; Thu, 14 Dec 2023 14:40:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702593623; cv=none; d=google.com; s=arc-20160816; b=Ov5X6vF/eYaGKrxGe3Av05nOesZh/5lOIKMcdnHLC8Z9gCJ/0JdXC3rX0187sZOQiV V9xS1uWh8w2SEjSglk8b3+v+7n/n2caNZ6vX5aWpL23XcWmxbHOHS4B7H1nf9M15ugt0 lo5GAEMoZ6qL4Cwqesinj/BtNHx2UC1LhZ9aGdApdZl3rxzE3YNNMRlfl+cLvLTmhWlj dYz2kFSZYUdF52QlioOfAlr9LO1SLrtpb4sUbg/6tFM2VJpek84ehdrOx6CM5EPEFris rK+IzVD4ckrWeMS400nca6YMJ+f87nKYEIF6RZME3oQ2QDElDOdS5ZEN5vxWdHvIppR8 LdCQ== 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=tI12aCWJQCHdnL9tpy1TZqFUoLiTbzcedlrkIrXRyzo=; fh=utmrxdkgikAlLxVfQI1kldUG+rNjhrLN+3fAlOYj01s=; b=u9QhZ+kt6AupYCt7EVD3CrTG0LMIXKHlE1XbAl49WR4Bd56nhZioVQy9Zn5KC224mR AK8gySGBt9xZK4r2WDFRCGeM+cMwFBhEJkMJAOwGiB8h1cIteW5JF91fg3eqf5rRnlBm nxCszc2jfX2egGGS+8Ef1dz1cR67J1Pdq0631//dwQL5EV/LvJo6nNqZ1rlV52Tri25A ZZ41TrbnU1i7Y7xlXeCLMmMzn12lqHRKEL/HdP9psVJJsWMEW2YJ8hE5Uhk3s+2SDpYM xRpD1v1RDaBAs6r5QhyD6nhpOAlyWEdCIGbzIE6LaBI6VmyJh6M9f2y6Gvs2IS/c1hJu 8qSQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gaM6CsDX; spf=pass (google.com: domain of linux-kernel+bounces-202-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id ew14-20020a05622a514e00b00423a25dd052si16644592qtb.87.2023.12.14.14.40.23 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 14:40:23 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-202-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=gaM6CsDX; spf=pass (google.com: domain of linux-kernel+bounces-202-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-202-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id 556681C21E0C for ; Thu, 14 Dec 2023 22:40:23 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E6B976EB59; Thu, 14 Dec 2023 22:38:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="gaM6CsDX" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f48.google.com (mail-wm1-f48.google.com [209.85.128.48]) (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 08DDE6A34D; Thu, 14 Dec 2023 22:38:37 +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-f48.google.com with SMTP id 5b1f17b1804b1-40c38de1ee4so720065e9.0; Thu, 14 Dec 2023 14:38:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702593516; x=1703198316; 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=tI12aCWJQCHdnL9tpy1TZqFUoLiTbzcedlrkIrXRyzo=; b=gaM6CsDXN9QTmZ+6tuMHtvQBtsZyL+6jo0kQtDGBFUdLQsHYspNnvIucVWR2cYaX9q 9BMSfEKtMMhg+cBnjZBzJ/vnLiiaXoIEYOGLQVYqFo0FcWbZphwQlvnih5TjuoYFf/ZF suyMFPhZHnTwYxft20sAb2hSkEebwkhHEKW0lBdKFcoVPyw/CSCdAzwoS64KvOqESTYR 8S1Lt9AvRaUvMbNHxWfVupBFDYBKCuQT21RW6D1K3c188LSe7vr6e2STNY8DNsFRsQOS uL06OXcXIH/30wN5WXFsh6nupeBNUgraWgX+HW/4RjbALScpIJPMAEvghrYCh1s2lmgk lzcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702593516; x=1703198316; 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=tI12aCWJQCHdnL9tpy1TZqFUoLiTbzcedlrkIrXRyzo=; b=jYCSJ5BmWj831aV+KylsBg6YH96KpPalN12AdU8BRbszXyGQuW4m/1SBUVz7IKulN4 C6wWxe4avsVJigSyaNbasgcPFhBJrrVk9G/FXVRfOboFLwb9bBPT9KqHXfTy20yEwF6J /hb5qQH400W3JVCliDFnWYke7x3sdVu/M0vTGAbJ0Gsrs5h9qfJl/JzTOfLP1lXA66zN 4NCkhYemF3GwfjSUr2u9SyQlNlVioOSXv7b8u6LAZfyQuOx/QfiS7mvpXXk2VF4QvQa8 lRUHS7+eays/zpagMfFZTcnd7i/2wG+mpxAx9YbcLv/Zc7ZK+t+udolOeC4o70dBm6bd Jb3g== X-Gm-Message-State: AOJu0YyEch4sL7Z7Ynd2rEm5tagj16eKR3L+VlIpY7vBmmd8Qq7IYTZ6 JxMhGMpVaPa8snRhcTzgxD4MTFlbGOE= X-Received: by 2002:a05:600c:1f1a:b0:40c:31f1:145c with SMTP id bd26-20020a05600c1f1a00b0040c31f1145cmr5011868wmb.169.1702593515923; Thu, 14 Dec 2023 14:38:35 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id n41-20020a05600c3ba900b0040c61ee0816sm54746wms.0.2023.12.14.14.38.35 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Dec 2023 14:38:35 -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 v2 3/3] net: phy: led: dynamically allocate speed modes array Date: Thu, 14 Dec 2023 16:49:06 +0100 Message-Id: <20231214154906.29436-4-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231214154906.29436-1-ansuelsmth@gmail.com> References: <20231214154906.29436-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: 1785298810944128475 X-GMAIL-MSGID: 1785298810944128475 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..2e41d10e323c 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); + free(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: + free(speeds); phy->phy_num_led_triggers = 0; return err; }