From patchwork Fri Dec 15 13:29:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 179292 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9276117dys; Fri, 15 Dec 2023 05:33:04 -0800 (PST) X-Google-Smtp-Source: AGHT+IFIYzI/HZXgw/RzmELzUSJjkIan1SGXrRcKuqUb6eNf8TUpXVD9kGQWNCj2OfBAFmeVouTf X-Received: by 2002:a05:6a00:198f:b0:6ce:544f:f195 with SMTP id d15-20020a056a00198f00b006ce544ff195mr6694879pfl.60.1702647184585; Fri, 15 Dec 2023 05:33:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702647184; cv=none; d=google.com; s=arc-20160816; b=P8ZrAqGMs1wRyydAPPgjkoK1dl9ZJaq740mrZX9/+FkyrPlQzQVWq5nsYAxl7wqeT/ AiP2dtsmfuXU1y0tU5KHGwf4QC5ir+k9lyBMqFRGuQlyIn1LP+wkEynYq9ex2MZ9AAnz W0HKsWFbbJ6YcQEFLKKfS0zvs1UR9xJPhO2Z8EaqJbCtd1wuro6MUYv5tNQKi+t9XiNW KsoqyoQkYv5a36JexRQcF/RSY0fxZ9wXMwRPXikZu41suX2fKuqYg/MQ49n6FOrSJs+9 neMBj1CBGjt1WSVWKIC1EknWoUJGiv0zSOV2ctXWqeAy4+HI7tPU/+nxSKS75+FnZtLi 1l0A== 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=fxfZMEIz/bQsZCinbIake+5VfAhVP5RR8GN/J/19ivEaLKiMVlCqylB08J7dUwYDys Mpdar62gzeTKLPDqjNiv55HlQQ1nimgUZHyJp7SFuTeT8kY9FmlJKGWdVxryfER9yzCI YQCHS1iZHHz7liR1Cu7lj1Nm64aWxjeVn59O2ffcESd6K5yCF/luJJOsYnFHzDlT46UJ CWky9TITAvZ6cdL8cvZVwXVcZ7JfahARj5XDKE8EDmN8bfo1uNtjEOrL7uw2RFamS8UT YVC1fnmSCauugjxiAXKSjsNqtNwaek6+pColeTu/Klt9l8QcA1QZ6+XfgqJt62LmL1to Jvbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=f+XyXPKE; spf=pass (google.com: domain of linux-kernel+bounces-1016-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1016-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id t13-20020aa7938d000000b006cbb7b7bed0si13082464pfe.201.2023.12.15.05.33.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 05:33:04 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1016-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=f+XyXPKE; spf=pass (google.com: domain of linux-kernel+bounces-1016-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1016-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id E1D80B23E96 for ; Fri, 15 Dec 2023 13:31:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 3198C30CED; Fri, 15 Dec 2023 13:29:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="f+XyXPKE" 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 D06912DB6F; Fri, 15 Dec 2023 13:29:40 +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-40c3ceded81so6341655e9.1; Fri, 15 Dec 2023 05:29:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702646979; x=1703251779; 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=f+XyXPKEHed92YcUkw7rdBKvMZQjwTFLD+uLy3V81/9sByL4hsZA4M3RxzbtduEqMw XKwB2S8PxYPgcwnHLfA+ACm5MfPoSBFGqdTCaD1dn9TuH/XG+aDNp/G0oGZLUP091k0X QpjPHH97pZ0LBHIOD6iiI14RXGzUpbKSfzYzAf72AVeWW3CpEahXuIouCT0jToXPIQzE AaRGkAtwzxXTvl4ptzqiSUXKiuxftnrx2ancCnasJTOqVqIHUC9NwoKXblKCjO2LO13A m+2saBF3f2pPT4kuUZowq16zC2jqZSMMgLcWbF1VYfSBzYBlnI5T/G52PduMGMqj/1SP tQ4Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702646979; x=1703251779; 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=jWSx+aWyXXfNnpvx0AZpngqC2yhHPc83gVygJ3fLH5YBgjw4Z/SHUrHGCslCeRk6P1 rYUh3wH6DksUv0cRskl7+Z9/XGAYN37Uf84LA/yDu3BcV6imOa+cYKcxvINomGb4H2l7 UMZuvyL5yFA4d1+760Wdm4g9B2tsJyj0K/QDuntv5IRUwSvGqGTL4XvwXh9ibf8upVA0 zSnr6t52XbmRL8MB4dCWRF8w7oKMlst5XcRsTfK98bUK/oCoY6c3Bayskmzq4czJf1Hc ntg17AxoL2hCFsfhigJsq3AfJ0qCgxXy+zr+ksAK3Dw7L23anYtDGWxZW3+9DoszZZ4h MmPA== X-Gm-Message-State: AOJu0YxlmEb258lyd3QxyGur5sHRJ1yI1DmtADMx53JentGp3dihhuyl dqY1Qn7nDlZNH7cI1gHK/cY= X-Received: by 2002:a05:600c:3b9f:b0:40c:2878:35ec with SMTP id n31-20020a05600c3b9f00b0040c287835ecmr6193489wms.131.1702646978947; Fri, 15 Dec 2023 05:29:38 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id o3-20020a05600c4fc300b004042dbb8925sm31434543wmq.38.2023.12.15.05.29.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 05:29:38 -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 v3 1/3] net: phy: refactor and better document phy_speeds function Date: Fri, 15 Dec 2023 14:29:19 +0100 Message-Id: <20231215132921.16808-2-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231215132921.16808-1-ansuelsmth@gmail.com> References: <20231215132921.16808-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: 1785354974157897841 X-GMAIL-MSGID: 1785354974157897841 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 Fri Dec 15 13:29:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 179293 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9276371dys; Fri, 15 Dec 2023 05:33:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IECxZUWBOGz5kmQsLBNPD6mn3LxVslNDdmZ2oL02D9cARoq8n1d+9cZ4rqogtL7HMlfcAc7 X-Received: by 2002:a05:6a20:3942:b0:18f:97c:926c with SMTP id r2-20020a056a20394200b0018f097c926cmr7273888pzg.81.1702647209892; Fri, 15 Dec 2023 05:33:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702647209; cv=none; d=google.com; s=arc-20160816; b=pl+gjRR0RtqKwieVL776qFj8JKb/ZAjdQ9amMGCQZak1wj6Ym60p+ZbBU5SyT+kb9U 4xm7/CJz4h9Plb9854a9xgu2k6qwelEu6rCTNxz4uoJefJDJtqbCSTRYWXh/YyPzend8 w2Ju0PehblDAVZRinRSIvPKbEWLiFbU6P8mf6Ue5IjCAiAh2PoLVAU2f+sQUhCBag/Gj FLWau/D4AaACCt60OjEPtq8/OQWyYkSqUtv5ItAjAckFXA1Zha/XBOlg/9BDuqeTPxNH q0N8NIVdwa4q3R3r8HYIuQV8bMDsQmJQg+IFImnafrswiOso3uuasZny6b7ko6l5B7qa ZO+w== 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=8YCHaxtRcxT8du8yPrhEwLJqy0r1nUHAjMwSdxshw+8=; fh=utmrxdkgikAlLxVfQI1kldUG+rNjhrLN+3fAlOYj01s=; b=IJX84h9+1w/QoJ3IOTRyCcoUOII50FGZGFJWHWz2LqOGP8z3f3AHBao+ao4kcTTdUP wpYyjpFHbtD9kQ59V3+alV3UNB+Ck5xT06FzwU5t8MT4Fu/7h5EXlM8ZvuYPWOVeFCeE hVsGd41e7kxaHD5HZSuRM82hfV2ZqH9bnWOsCBG40ld3Ri+ZhNsG/wzl5Ax3ZCLHIIZc GSOeupaXKViCqi/uZ4yi4QiSiNmzQMW5Fb7DVeSCsmKqRmWHD3+PGXxEsgItorArpsjb 59kEPteubR6nsQm7rGqz7vc/JCCC7xoIsBF3WurLzDrP0hG2xXZezGdA/IagZHvhh6h7 jQ9Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Irkew6or; spf=pass (google.com: domain of linux-kernel+bounces-1017-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1017-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [147.75.48.161]) by mx.google.com with ESMTPS id s22-20020a056a0008d600b006d0aecafffasi5989914pfu.194.2023.12.15.05.33.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 05:33:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1017-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) client-ip=147.75.48.161; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Irkew6or; spf=pass (google.com: domain of linux-kernel+bounces-1017-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.48.161 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1017-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 0FAA3B23FFD for ; Fri, 15 Dec 2023 13:31:20 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 277BE30FAC; Fri, 15 Dec 2023 13:29:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Irkew6or" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f49.google.com (mail-wm1-f49.google.com [209.85.128.49]) (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 EAF902DF96; Fri, 15 Dec 2023 13:29:41 +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-f49.google.com with SMTP id 5b1f17b1804b1-40c69403b3eso6202995e9.3; Fri, 15 Dec 2023 05:29:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702646980; x=1703251780; 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=8YCHaxtRcxT8du8yPrhEwLJqy0r1nUHAjMwSdxshw+8=; b=Irkew6oravPVZnWKaGdknrLuBoeKehAeHPkABS8iUurJCwwcC28CNzf62OZrg1lR6a BcLIO2fP2VfEV7v8P6cKsd+FoJT9TqHyEbT/XOLIxbKkrWDoabU9uUkONqZZMRm6UOir vPQ/qQJS3+QJSp9M+erTBzDoVGzcaqNaYg6qaU5H4oneTrE4NsnvBkGOTBOmiYwRV5YZ Q3rlGUDNRzeA86LEIR4PKg+WHfPPBrLo6DwJguozy9rFy96PIBH3FWlWEfHx21dqeKQ/ mM3wT9s4km6VZWoM9Pky46JsYtzaz2aoI8rWOwwTCsBhKYSitupJVicIr59kJKPfs9q1 sq7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702646980; x=1703251780; 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=8YCHaxtRcxT8du8yPrhEwLJqy0r1nUHAjMwSdxshw+8=; b=YQpr5VXe88WQ3p2GDvEJEwzjsBWrZWEkJ8SPxAjYSy9YbdrnTjkYIJ6oobecV+VLMa w7gAf8h/N1MoLmoaT5FeCZeP8IE3OcN6Ocw/aB6FIt1Z126VKBNgkOCZhuADA1IJZ5aP AY4Fan74Sk+MWPfufavUr4kj9MFIy7HYwf52LJXCY1zfJYx4sR1fJIVWk07vAnT7yLzx h9vGqfgYi6aITGwAonKJVFJPucEMrJhcviu6KjDSatLkCNXOGcWud+GS3mQzX8vGRaA1 bYFlAf1fFG2MEpqjbORNMqJrOZSsikVvUoZKmhrr2FboMIBdc3FggIkMn42zAayWnE5G Ejvw== X-Gm-Message-State: AOJu0Ywg44beQoa8zPqjKK4ejRf9n7dVhFOa9AaKyv40i5lwzgc457oV SKTeg6QyhbKQWWrKMiYhAEA= X-Received: by 2002:a05:600c:4981:b0:402:f55c:faee with SMTP id h1-20020a05600c498100b00402f55cfaeemr6380105wmp.26.1702646980096; Fri, 15 Dec 2023 05:29:40 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id o3-20020a05600c4fc300b004042dbb8925sm31434543wmq.38.2023.12.15.05.29.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 05:29:39 -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 v3 2/3] net: phy: add simple helper to return count of supported speeds Date: Fri, 15 Dec 2023 14:29:20 +0100 Message-Id: <20231215132921.16808-3-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231215132921.16808-1-ansuelsmth@gmail.com> References: <20231215132921.16808-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: 1785355000845904287 X-GMAIL-MSGID: 1785355000845904287 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..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. 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 Fri Dec 15 13:29:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christian Marangi X-Patchwork-Id: 179289 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7300:3b04:b0:fb:cd0c:d3e with SMTP id c4csp9274945dys; Fri, 15 Dec 2023 05:31:28 -0800 (PST) X-Google-Smtp-Source: AGHT+IH1JGYMb1SNJfgFmwOW3AOjok1I3F76RvAMMNoERycYHKoadaL+ritDFjZnNFBySAcK9x4n X-Received: by 2002:a05:6808:3a09:b0:3a7:6213:6897 with SMTP id gr9-20020a0568083a0900b003a762136897mr11006519oib.11.1702647088231; Fri, 15 Dec 2023 05:31:28 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1702647088; cv=none; d=google.com; s=arc-20160816; b=S1IbG4aiMxLJEY5aDREvXqLGB1fMxDuHbhG7VYhFzjJWaj5g0jq2BQnO28pQAZ0AmD cg9sP/FuaPl3kUJ99YqD/pFvgU5T+pCOUyMpUWY0OFJWwbe/0YzGGHLzaQn8mVToYI0o kFWWDnFdtvLPHrjuXJ10gVUxySm8dTCco/FNKP1uoJ6nRdfzrXEvhVrNdz8RoTxM451L FeaDACj2DnlCZKREffxOXgv7qG+Y+1y0hRYyWODRpJ1sZO23fWHeWmF0Pp+nGtADk4zW LgdsMkeGo9qpfgkcfRGo/rrtZcOOngjacbsO5iY30PfrwujTHw+8bepdlNicojJ9VJxD bo7g== 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=RPn7V+M5t4ex/hmtUnSlSMjU8ayc/4TLTaX6CLO5JTrJvIJEnCncmPWN3/9liP8QhF ObYIBAYWMMIwdC+tK8g50hnu4+qWGq+iL/aq9iL13BFuuIQwikpyRveclQHL1BHrDXEl bgWl+Hzv6uj9Bf/N7ywG/k34zMNvYQNl85hKY79sqKgqq4pRoAHKhP32yxBF9vBU4ifd TTrKiLRjn1HM7y8IyPzKuQaauCScxw9l/hc2RZiM5Ww1LfROWJjEpFRf/55wazLJJ11/ JJ2PkLBc5f/ECIrcwd78AM8JFPBgWv2ZlslqgMGICPGng+gJBEiQwmkyR+hXIvQ6C3Ok hWRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=Mhix1SNK; spf=pass (google.com: domain of linux-kernel+bounces-1018-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1018-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 c19-20020a67f9d3000000b004647e3f7ec8si2969836vsq.610.2023.12.15.05.31.28 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 05:31:28 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-1018-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=Mhix1SNK; spf=pass (google.com: domain of linux-kernel+bounces-1018-ouuuleilei=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-1018-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 02DFE1C23BF2 for ; Fri, 15 Dec 2023 13:31:28 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 68F2E315AF; Fri, 15 Dec 2023 13:29:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="Mhix1SNK" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-wm1-f54.google.com (mail-wm1-f54.google.com [209.85.128.54]) (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 3F55530321; Fri, 15 Dec 2023 13:29:42 +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-f54.google.com with SMTP id 5b1f17b1804b1-40c38e292c8so3513915e9.0; Fri, 15 Dec 2023 05:29:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1702646981; x=1703251781; 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=Mhix1SNKzn25mpmaQJA/pZ7i75Y8TA9Q54sj3hZE+lCzV587iu5W4fwvNyq3kOvNhU bH5dvQDGMrQTx/fa/0kaIQqlLrH2m9ILUSTQlcUu/sQSGOE0Rlivj94r2CTGZmlTxoNp f+/cUzIRGXFNR4h13PjNVUINhYeRBx2SYs8tOF5UiewfQyosrWvwiVhPYglxHDAxLcP2 iavOVx+SOr5jiuamrUrfrZ8AOrSTlrwbsrgveafixUG98mJldQml6/PGSWxlHA9g3bUj 8rho2uZ6RojnpppcOaZ1MFeejOElKBuISSkfk11kkcmSsTVr7fPX/oKkgE5SbCfsRSRj ke5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702646981; x=1703251781; 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=wz5BdHf4G6FLU49Wvz3HVbqwvD3lmCf3oex13kj+XyGqq56eI0weCkTy/HoumxACni LxAigQF3TeKSLcVNZPbDfG9jXKcWKfdbmeYz0S9r8rRCKQ5ysz7SYddO9urE1h/mBBqg Tom97nMqI5MIE3LhqPHdMEoNRSx4WV2s2ts+LMU5UlcRLxyXtLU827sUw4uZcFWOnnL5 hcFQMfAFcJlIr9uXg57z3N71z2VVzRsqaSEG8CkeR2Q3dj8CoDeLnoTDmgPi+Y40Wvcz aU0HPBfPDYUsgAHvnbZ/bnB2CvtXbbmxJlUBeFllAmjjGWoOgXUDAI0S7MpasJwZR0ll KA6A== X-Gm-Message-State: AOJu0YwafsAroLj6iqnesHQlrsv5MGwNfsxbPUvETEZAKBYbSuLX/pGM u32gpRdt1T2m2e57rNZPxma6mfzNjpM= X-Received: by 2002:a05:600c:808e:b0:40b:47d0:cff with SMTP id ew14-20020a05600c808e00b0040b47d00cffmr6101245wmb.10.1702646981425; Fri, 15 Dec 2023 05:29:41 -0800 (PST) Received: from localhost.localdomain (93-34-89-13.ip49.fastwebnet.it. [93.34.89.13]) by smtp.googlemail.com with ESMTPSA id o3-20020a05600c4fc300b004042dbb8925sm31434543wmq.38.2023.12.15.05.29.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Dec 2023 05:29:41 -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 v3 3/3] net: phy: led: dynamically allocate speed modes array Date: Fri, 15 Dec 2023 14:29:21 +0100 Message-Id: <20231215132921.16808-4-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231215132921.16808-1-ansuelsmth@gmail.com> References: <20231215132921.16808-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: 1785354873563211261 X-GMAIL-MSGID: 1785354873563211261 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; }