From nobody Fri Sep 12 01:29:53 2025 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (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 16BC6264FBE; Sun, 11 May 2025 20:13:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746994414; cv=none; b=gxe7CB0CCo6wGuBJ4lFSekPJ/JXcohGf/j4NTEdoANS5easRnikfb6WF1gVsznA3Vko7CiTSFlFAm+s5ZnOzZdNG1WetlBqSWhexurH/FqEqBza1l4G+b9X9/mFfrtYW+2YCMRMKMgMk6PEI/qF8oQggz3xp8y7HV4e1gXej6yc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1746994414; c=relaxed/simple; bh=HekMUzVB4s8aLJa024ccjcOQFQtBjd3ldb6VfI+Y15Y=; h=From:To:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=avcH7sEve5cwRFdJyyMq62WS3KLlP5wmuG59NgW0TPFpldiyq1GZ8GtwPjipLgXR63MGE0c1icI8jY8cCTw3t1K9n4yoGswQSBZZGHc6amv4BWgTgExiKnGoz4lF0erxs71t5xi6ipjZYcHfv3Zt1md3yuVLDQI8wHNoUkQsfCM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=PORDEtT1; arc=none smtp.client-ip=209.85.128.50 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="PORDEtT1" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-43d04ea9d9aso19635255e9.3; Sun, 11 May 2025 13:13:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1746994410; x=1747599210; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to; bh=iQTsNqbFULQWL+wyDuQPJ1xAEm+cNteVGo/Ck4ylD84=; b=PORDEtT1um1gkClet1OWidEYYspy3H1fQQWK5lzR40BvqqC60V9tRYCaO0rLwlIFfQ K2n3ND8ypU/rTxWazeCZ3DERvTMcvwU5dn/xIec52e/xsSP4GEEKBaPxugkNh/e+U9wG npA61QuVkZ+yx4wyA84VHK9DrYykUhVt6+oDAak4f04FYu2tzoPKbNSlHdy1rzKbfLKh YL1lpAFa+txbFBzYRAXam/mz1GzR4K4Imr7id2OYF/8UyZqQTEbJJZh7toEF+plv8Jkw r2fi/VKezL1e35/FVGf8ZLtJthgtWpKRtA4ewhY5LSoXvn2C/gYLhWiGl89945d3huLK hQVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1746994410; x=1747599210; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=iQTsNqbFULQWL+wyDuQPJ1xAEm+cNteVGo/Ck4ylD84=; b=IxW5dvK8df1XbaU4BMaW8Ml5nP4T2Rqn/qRPXEGXerAbNzMnvuyssYsxRu+pFZbVzq JJy3uJIopmbowoiFR1nHIeZz09JdwBCXJenoIeTKS0+P+gubpotj2vtzg0WgQ6EFokuF 93SttxOSsrFZVpGFJ2AZXEzePlt3bOJNh9oZthP/g1+pPk1nFhG5LqvcIn8oqf0CvN/C zyLYYiLn2/Zl7G+So95K8PR9cRowJzU9P7zsrw+jSyVEPrNkds190+P6G/PMNbkL6IfR RsfCSMgM3JKbOH9bgB7OuXXEWn6BQr5KzYkK04ZLLW0wnFMywZervr69azhk+LPBOX/G OpNg== X-Forwarded-Encrypted: i=1; AJvYcCUO9AMQj16I7RQsjAKFAGjYliSzqMwZ5LbDzuvkrPwW7XTU1dwjEC73MQ1V+nfZWJue4me6dPeiEoD/fEjc@vger.kernel.org, AJvYcCVCF1j/SInojg+GnRk/vUZY3Dy1DUs4SBorFCEtcXWhkqURQw8Fsups/Fv9uCx9N+LUn9XRa0Tj@vger.kernel.org, AJvYcCWZB5v1KZRQd7keo9VnbIqQXuuayVABW2gHali3wBHCVptp0sd6aX9kiQYMJ28MAjDZkvM3O+tfMpwZ@vger.kernel.org X-Gm-Message-State: AOJu0Yzk5FYziQzDZi8wW3WMlrLFAwsR7U4lBI94/1aoNcVESK+xYAiC /2SlRXpLlYNXCytEwolvbBZOMcBLnUm8xSnYogfnN+/fWDGM2I9c X-Gm-Gg: ASbGncu9XsSPhXIK3U7k34cIdSbn4SiSzFy8lPmiSwMaWDbCpQ98LnXKJ2Ww2itMhNc 5vmH4E580YhQgbQ7mlMHZ/01aRETDo58S66yp1JGt/VNVsr2GqEzWXMecqNjOOKZ5ItU7RqFbWT K+Ej8cFHC6jUbtjDYdS8VsQMlhy3jriLk2C29/e+qLhRKNaG6CuR8Ym9aq/izuiFJ89jZKHbgGF zsglY1oy5zhyzta2kgPSKmIsfXEEa7MViisSzvT6VRIg13zDbeTZsAd4m5vGx/hx5ZNkX2S/dRv 1GqU3XHCru1t9YfyU+/tjy8AjlJcrmRX7OR6dclvUepej32GlhsjxM9TIcVY96lT/k2EuIvn/rT J66szzxvRtaJ/ZhBhEJ/E X-Google-Smtp-Source: AGHT+IEslwNZyPl+dAvJHlsPw5VASl6Qum9OSShnIlZFV8iIt385cjdglGV9PUctfP/VhgKE2nCnBA== X-Received: by 2002:a05:600c:4454:b0:440:9b1a:cd78 with SMTP id 5b1f17b1804b1-442d6d44aa7mr112880735e9.10.1746994410035; Sun, 11 May 2025 13:13:30 -0700 (PDT) Received: from localhost.localdomain (93-34-88-225.ip49.fastwebnet.it. [93.34.88.225]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-442d67ee275sm100615165e9.19.2025.05.11.13.13.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 11 May 2025 13:13:29 -0700 (PDT) From: Christian Marangi To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Lorenzo Bianconi , Heiner Kallweit , Russell King , Philipp Zabel , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , Christian Marangi , Daniel Golle , netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, llvm@lists.linux.dev Subject: [net-next PATCH v4 06/11] net: phylink: support late PCS provider attach Date: Sun, 11 May 2025 22:12:32 +0200 Message-ID: <20250511201250.3789083-7-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.48.1 In-Reply-To: <20250511201250.3789083-1-ansuelsmth@gmail.com> References: <20250511201250.3789083-1-ansuelsmth@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add support for late PCS provider attachment to a phylink instance. This works by creating a global notifier for the PCS provider and making each phylink instance that makes use of fwnode subscribe to this notifier. The PCS notifier will emit the event FWNODE_PCS_PROVIDER_ADD every time a new PCS provider is added. phylink will then react to this event and will call the new function fwnode_phylink_pcs_get_from_fwnode() that will check if the PCS fwnode provided by the event is present in the phy-handle property of the phylink instance. If a related PCS is found, then such PCS is added to the phylink instance PCS list. Then we link the PCS to the phylink instance if it's not disable and we refresh the supported interfaces of the phylink instance. Finally we check if we are in a major_config_failed scenario and trigger an interface reconfiguration in the next phylink resolve. In the example scenario where the link was previously torn down due to removal of PCS, the link will be established again as the PCS came back and is now available to phylink. Signed-off-by: Christian Marangi --- drivers/net/pcs/pcs.c | 40 ++++++++++++++++++++++++++++++ drivers/net/phy/phylink.c | 52 +++++++++++++++++++++++++++++++++++++++ include/linux/pcs/pcs.h | 48 ++++++++++++++++++++++++++++++++++++ 3 files changed, 140 insertions(+) diff --git a/drivers/net/pcs/pcs.c b/drivers/net/pcs/pcs.c index 26d07a2edfce..409d06658167 100644 --- a/drivers/net/pcs/pcs.c +++ b/drivers/net/pcs/pcs.c @@ -22,6 +22,13 @@ struct fwnode_pcs_provider { =20 static LIST_HEAD(fwnode_pcs_providers); static DEFINE_MUTEX(fwnode_pcs_mutex); +static BLOCKING_NOTIFIER_HEAD(fwnode_pcs_notify_list); + +int register_fwnode_pcs_notifier(struct notifier_block *nb) +{ + return blocking_notifier_chain_register(&fwnode_pcs_notify_list, nb); +} +EXPORT_SYMBOL_GPL(register_fwnode_pcs_notifier); =20 struct phylink_pcs *fwnode_pcs_simple_get(struct fwnode_reference_args *pc= sspec, void *data) @@ -55,6 +62,10 @@ int fwnode_pcs_add_provider(struct fwnode_handle *fwnode, =20 fwnode_dev_initialized(fwnode, true); =20 + blocking_notifier_call_chain(&fwnode_pcs_notify_list, + FWNODE_PCS_PROVIDER_ADD, + fwnode); + return 0; } EXPORT_SYMBOL_GPL(fwnode_pcs_add_provider); @@ -147,6 +158,35 @@ struct phylink_pcs *fwnode_pcs_get(struct fwnode_handl= e *fwnode, int index) } EXPORT_SYMBOL_GPL(fwnode_pcs_get); =20 +struct phylink_pcs * +fwnode_phylink_pcs_get_from_fwnode(struct fwnode_handle *fwnode, + struct fwnode_handle *pcs_fwnode) +{ + struct fwnode_reference_args pcsspec; + int index =3D 0; + int ret; + + /* Loop until we find a matching PCS node or + * fwnode_parse_pcsspec() returns error + * if we don't have any other PCS reference to check. + */ + while (true) { + ret =3D fwnode_parse_pcsspec(fwnode, index, NULL, &pcsspec); + if (ret) + return ERR_PTR(ret); + + /* Exit loop if we found the matching PCS node */ + if (pcsspec.fwnode =3D=3D pcs_fwnode) + break; + + /* Check the next PCS reference */ + index++; + } + + return fwnode_pcs_get(fwnode, index); +} +EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_get_from_fwnode); + static int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode, unsigned int *num_pcs) { diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index 2f28c4c83062..1a4df0d24aa2 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -61,6 +62,7 @@ struct phylink { =20 /* List of available PCS */ struct list_head pcs_list; + struct notifier_block fwnode_pcs_nb; =20 /* What interface are supported by the current link. * Can change on removal or addition of new PCS. @@ -1909,6 +1911,51 @@ int phylink_set_fixed_link(struct phylink *pl, } EXPORT_SYMBOL_GPL(phylink_set_fixed_link); =20 +static int pcs_provider_notify(struct notifier_block *self, + unsigned long val, void *data) +{ + struct phylink *pl =3D container_of(self, struct phylink, fwnode_pcs_nb); + struct fwnode_handle *pcs_fwnode =3D data; + struct phylink_pcs *pcs; + + /* Check if the just added PCS provider is + * in the phylink instance phy-handle property + */ + pcs =3D fwnode_phylink_pcs_get_from_fwnode(dev_fwnode(pl->config->dev), + pcs_fwnode); + if (IS_ERR(pcs)) + return NOTIFY_DONE; + + /* Add the PCS */ + rtnl_lock(); + + list_add(&pcs->list, &pl->pcs_list); + + /* Link phylink if we are started */ + if (!pl->phylink_disable_state) + pcs->phylink =3D pl; + + /* Refresh supported interfaces */ + phy_interface_copy(pl->supported_interfaces, + pl->config->supported_interfaces); + list_for_each_entry(pcs, &pl->pcs_list, list) + phy_interface_or(pl->supported_interfaces, + pl->supported_interfaces, + pcs->supported_interfaces); + + mutex_lock(&pl->state_mutex); + /* Force an interface reconfig if major config fail */ + if (pl->major_config_failed) + pl->reconfig_interface =3D true; + mutex_unlock(&pl->state_mutex); + + rtnl_unlock(); + + phylink_run_resolve(pl); + + return NOTIFY_OK; +} + /** * phylink_create() - create a phylink instance * @config: a pointer to the target &struct phylink_config @@ -1963,6 +2010,11 @@ struct phylink *phylink_create(struct phylink_config= *config, pl->supported_interfaces, pcs->supported_interfaces); =20 + if (!phy_interface_empty(config->pcs_interfaces)) { + pl->fwnode_pcs_nb.notifier_call =3D pcs_provider_notify; + register_fwnode_pcs_notifier(&pl->fwnode_pcs_nb); + } + pl->config =3D config; if (config->type =3D=3D PHYLINK_NETDEV) { pl->netdev =3D to_net_dev(config->dev); diff --git a/include/linux/pcs/pcs.h b/include/linux/pcs/pcs.h index 33244e3a442b..dfd3dc0f86f6 100644 --- a/include/linux/pcs/pcs.h +++ b/include/linux/pcs/pcs.h @@ -4,7 +4,24 @@ =20 #include =20 +enum fwnode_pcs_notify_event { + FWNODE_PCS_PROVIDER_ADD, +}; + #if IS_ENABLED(CONFIG_FWNODE_PCS) +/** + * register_fwnode_pcs_notifier - Register a notifier block for fwnode + * PCS events + * @nb: pointer to the notifier block + * + * Registers a notifier block to the fwnode_pcs_notify_list blocking + * notifier chain. This allows phylink instance to subscribe for + * PCS provider events. + * + * Returns: 0 or a negative error. + */ +int register_fwnode_pcs_notifier(struct notifier_block *nb); + /** * fwnode_pcs_get - Retrieves a PCS from a firmware node * @fwnode: firmware node @@ -20,6 +37,25 @@ struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, int index); =20 +/** + * fwnode_phylink_pcs_get_from_fwnode - Retrieves the PCS provided + * by the firmware node from a + * firmware node + * @fwnode: firmware node + * @pcs_fwnode: PCS firmware node + * + * Parse 'pcs-handle' in 'fwnode' and get the PCS that match + * 'pcs_fwnode' firmware node. + * + * Returns: a pointer to the phylink_pcs or a negative + * error pointer. Can return -EPROBE_DEFER if the PCS is not + * present in global providers list (either due to driver + * still needs to be probed or it failed to probe/removed) + */ +struct phylink_pcs * +fwnode_phylink_pcs_get_from_fwnode(struct fwnode_handle *fwnode, + struct fwnode_handle *pcs_fwnode); + /** * fwnode_phylink_pcs_parse - generic PCS parse for fwnode PCS provider * @fwnode: firmware node @@ -39,12 +75,24 @@ int fwnode_phylink_pcs_parse(struct fwnode_handle *fwno= de, struct phylink_pcs **available_pcs, unsigned int *num_pcs); #else +static inline int register_fwnode_pcs_notifier(struct notifier_block *nb) +{ + return -EOPNOTSUPP; +} + static inline struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwn= ode, int index) { return ERR_PTR(-ENOENT); } =20 +static inline struct phylink_pcs * +fwnode_phylink_pcs_get_from_fwnode(struct fwnode_handle *fwnode, + struct fwnode_handle *pcs_fwnode) +{ + return ERR_PTR(-ENOENT); +} + static inline int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode, struct phylink_pcs **available_pcs, unsigned int *num_pcs) --=20 2.48.1