From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 83826C54FD2 for ; Thu, 30 Jul 2026 10:25:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:To:From:Reply-To: Cc:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=5M6b8h2Qx7uz2S7lrJX2+QvSSsq1tSsAAn6JVK5UB6A=; b=UA9p9mE0I2vb+NDt2sLSudAylJ aQZyNVtk5hf88vRc57K4TD47k4CQitGLaIl3blqDW7APZx8xJLH8QNTca1Zhn6cg2UNNdcmLOWul/ 8TtMFxl9fLWHru6cwDLuNKfu5WEDfxy5AUWfR/fzZeDii7InzJ04USuUXlO2qW7vdzPVqwyxxDQ44 O+zL1BX8tp4tiUI40A4mqKnEw0WFvp3Zb4cRBpkWBW0hrIKWambDtr5GERUYFhuvtALR9w92ZrCXM 7MQwyDzdjdjZWmC+ii4ophchcUukide0/PlARWif0hEW9bkY8L/lESSuQcEnG9+mzytxkf7GvTEkW 9laOMnBA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.99.1 #2 (Red Hat Linux)) id 1wpNxC-0000000ABqu-18OX; Thu, 30 Jul 2026 10:25:30 +0000 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]) by bombadil.infradead.org with esmtps (Exim 4.99.1 #2 (Red Hat Linux)) id 1wpNwp-0000000ABXB-40xs for linux-mediatek@lists.infradead.org; Thu, 30 Jul 2026 10:25:09 +0000 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-498028b3d5eso4407155e9.1 for ; Thu, 30 Jul 2026 03:25:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1785407106; x=1786011906; darn=lists.infradead.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:content-type; bh=5M6b8h2Qx7uz2S7lrJX2+QvSSsq1tSsAAn6JVK5UB6A=; b=ZuKAdO+WTem4dWtTzAnFOtlC6zLnxLkgME7UtGvDCuaNUUP221EN2siPuGvwO6a9N0 Q2S/0j++bwS7We6gLKrITe4r7cOGA6SqLASQ9idXDc8+6lJni7Ou8zIg4tUA3Jehfxeb JUEaJ26ME4fP9ZSJ4s/xE1mXWOuyjPyRRoU1/Xt2rLn6bcGzrCmwXmBtKGgLjtHo05YL kWl6L4sEVkqLaNX/R9bXbkG3Qs2BZl/FuEc5DVoUj8+RMk++I4uB0DdaLSvwesJFrpfJ npWILHXFwHtQpZH+GHmM/qnJg3wq20hQdu7X+juOzMHVNxVziouK9HNe1HeqTPKgl/jS nCEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1785407106; x=1786011906; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=5M6b8h2Qx7uz2S7lrJX2+QvSSsq1tSsAAn6JVK5UB6A=; b=dDIPqD5wAGnQAHv6+v13ymbjQ7LgxeasY2wpyq9LxHxVBou0QALsXNhbt3aj4uSUuS WKmY4b3bNEW3zOEdKmN6IHDpRFwbX9n1JtuSYNh0zLcvFLtMZmGd5J8Gspd9/t+osGc7 feA5YlpHT1Paq3MxsWrmMOXm5UO+4yVfzIlSvhiRSqI7crwJ5TQjfehjZMqwS5w9Hddf wdENG3SJtTkjHOiQZk92pfOiQwpbsaT7ICbpaMG9HngZPoqAcc2RTJU0vqH+SmuCWEH9 VNdJpXnelotzH7EFVRHPe4q1dXVeRckg7o1GwAuSMl40sQq+guboFguMmkQNPSvDiW4f UlYw== X-Forwarded-Encrypted: i=1; AHgh+RpK0jRuPssyb9ZtuYO0z/e+RTZP9Z45BbgYkmPwVJ93HrmF8pHJ68v5oHSTxuKjkySrysgTrCgxJ8x3KOLA2g==@lists.infradead.org X-Gm-Message-State: AOJu0YxOY2hant5wM7G/nnCeB2YiOTI13H2Vzu785P2jWR4feXLclZZl Pbu1xzzo9bFdm5dePfG3gy2c8ZRRyJ5FFvoq1Za6aGNReUUfda6moNWx X-Gm-Gg: AR+sD125OH6mcwVwlVE8H7IzOj3Y00TH6BAC3WtGLFJe3rMe78MXHJ2f/KdGlSd+zt0 wg9Qae5ujFN+ioJG6djf86unn31kGmT+Lgq2zbasOQ540nosBv8sZyiXTi87z/3TAxCApxRN4/C yCRXas7CMFdb52zsOQquIIBNQhRBUwXHGt/YsaTnRgz8dYlnGQx6widn5sHh+BZXu6njTPPAGD0 +x7+pB48sdBfPpnmkhgTw0ccXwZpWnD4SjRPv6+HjDkAsHi7B/jBgf8MONjLG3GNnIlAITrS5eG 8Srlg3TWRNgC2NRR/CEF723uQ+KY2hE6E1nv44ppcWbUs8ocUGV1U3kagenTMMfEsyBgEmG379s dpu/o2tbkAvJOeYtCeEl5O0oc0nvhr4Mpjpx0jLA4vEe2k0vU//OoYfQi19l8/TkXGe4MCDACmR /qMbr/8EnoYqZYTqGEqG6OGFxi5OBHUvGCUManNj+6KBFreQ6QkaW9pS83c3gpFvxPOIKhfn5oZ dQAqbxsajh2CwEvlpJS0GoeDKcQAHZ/RQCV4TWKJpam3IkRnxZ63g== X-Received: by 2002:a05:600c:1d8c:b0:495:6713:9a40 with SMTP id 5b1f17b1804b1-49800ea2aecmr25646785e9.18.1785407105759; Thu, 30 Jul 2026 03:25:05 -0700 (PDT) Received: from Ansuel-XPS24.localdomain (host-87-11-254-126.retail.telecomitalia.it. [87.11.254.126]) by smtp.googlemail.com with ESMTPSA id 5b1f17b1804b1-498011fd5fesm44720295e9.7.2026.07.30.03.25.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 30 Jul 2026 03:25:05 -0700 (PDT) From: Christian Marangi To: Maxime Chevallier , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Simon Horman , Jonathan Corbet , Shuah Khan , Christian Marangi , Lorenzo Bianconi , Heiner Kallweit , Russell King , Philipp Zabel , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, llvm@lists.linux.dev Subject: [PATCH net-next v10 05/11] net: phylink: support late PCS provider attach Date: Thu, 30 Jul 2026 12:24:16 +0200 Message-ID: <20260730102444.1967900-6-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20260730102444.1967900-1-ansuelsmth@gmail.com> References: <20260730102444.1967900-1-ansuelsmth@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20260730_032508_112793_99C5FDEE X-CRM114-Status: GOOD ( 31.64 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org 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 pcs-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 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 | 49 +++++++++++++++++++++ drivers/net/phy/phylink.c | 93 ++++++++++++++++++++++++++++++++++++--- include/linux/pcs/pcs.h | 65 +++++++++++++++++++++++++++ 3 files changed, 201 insertions(+), 6 deletions(-) diff --git a/drivers/net/pcs/pcs.c b/drivers/net/pcs/pcs.c index fa8ab67076e2..ec0409909955 100644 --- a/drivers/net/pcs/pcs.c +++ b/drivers/net/pcs/pcs.c @@ -22,6 +22,19 @@ struct fwnode_pcs_provider { 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); + +int unregister_fwnode_pcs_notifier(struct notifier_block *nb) +{ + return blocking_notifier_chain_unregister(&fwnode_pcs_notify_list, nb); +} +EXPORT_SYMBOL_GPL(unregister_fwnode_pcs_notifier); struct phylink_pcs *fwnode_pcs_simple_get(struct fwnode_reference_args *pcsspec, void *data) @@ -55,6 +68,10 @@ int fwnode_pcs_add_provider(struct fwnode_handle *fwnode, fwnode_dev_initialized(fwnode, true); + blocking_notifier_call_chain(&fwnode_pcs_notify_list, + FWNODE_PCS_PROVIDER_ADD, + fwnode); + return 0; } EXPORT_SYMBOL_GPL(fwnode_pcs_add_provider); @@ -150,6 +167,38 @@ struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, unsigned int in } EXPORT_SYMBOL_GPL(fwnode_pcs_get); +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 = 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 = 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 == pcs_fwnode) { + fwnode_handle_put(pcsspec.fwnode); + break; + } + + /* Check the next PCS reference */ + fwnode_handle_put(pcsspec.fwnode); + index++; + } + + return fwnode_pcs_get(fwnode, index); +} +EXPORT_SYMBOL_GPL(fwnode_phylink_pcs_get_from_fwnode); + unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode) { struct fwnode_reference_args out_args; diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index 562e09d1838b..d38820543ae4 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -62,6 +63,7 @@ struct phylink { /* List of available PCS */ struct list_head pcs_list; + struct notifier_block fwnode_pcs_nb; /* What interface are supported by the current link. * Can change on removal or addition of new PCS. @@ -1965,6 +1967,27 @@ int phylink_set_fixed_link(struct phylink *pl, } EXPORT_SYMBOL_GPL(phylink_set_fixed_link); +static void phylink_add_pcs(struct phylink *pl, struct phylink_pcs *pcs) +{ + struct phylink_pcs *tmp; + + /* + * Make sure state mutex is locked to protect concurrent + * access to phylink instance PCS list from + * initial fill_available_pcs and late PCS attach + */ + lockdep_assert_held(&pl->state_mutex); + + list_for_each_entry(tmp, &pl->pcs_list, list) + if (tmp == pcs) + return; + + list_add_tail(&pcs->list, &pl->pcs_list); + + /* Link PCS to phylink */ + pcs->phylink = pl; +} + static int phylink_fill_available_pcs(struct phylink *pl, struct phylink_config *config) { @@ -1988,21 +2011,70 @@ static int phylink_fill_available_pcs(struct phylink *pl, if (ret < 0) goto out; + mutex_lock(&pl->state_mutex); + for (i = 0; i < config->num_possible_pcs; i++) { struct phylink_pcs *pcs = pcss[i]; if (!pcs) continue; - list_add_tail(&pcs->list, &pl->pcs_list); + phylink_add_pcs(pl, pcs); } + mutex_unlock(&pl->state_mutex); + out: kfree(pcss); return ret; } +static int pcs_provider_notify(struct notifier_block *self, + unsigned long val, void *data) +{ + struct phylink *pl = container_of(self, struct phylink, fwnode_pcs_nb); + struct fwnode_handle *pcs_fwnode = data; + struct phylink_pcs *pcs; + + rtnl_lock(); + + /* Check if the just added PCS provider is + * in the phylink instance pcs-handle property. + */ + pcs = fwnode_phylink_pcs_get_from_fwnode(dev_fwnode(pl->config->dev), + pcs_fwnode); + if (IS_ERR(pcs)) { + rtnl_unlock(); + return NOTIFY_DONE; + } + + /* Add the PCS */ + mutex_lock(&pl->state_mutex); + + phylink_add_pcs(pl, pcs); + + /* 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); + + /* Force an interface reconfig if major config fail */ + if (pl->major_config_failed) + pl->force_major_config = 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 @@ -2047,15 +2119,21 @@ struct phylink *phylink_create(struct phylink_config *config, INIT_WORK(&pl->resolve, phylink_resolve); INIT_LIST_HEAD(&pl->pcs_list); + /* + * First register notifier for late PCS attach to catch + * any possible provider that registers while we fill + * the available PCS. + */ + if (!phy_interface_empty(config->pcs_interfaces)) { + pl->fwnode_pcs_nb.notifier_call = pcs_provider_notify; + register_fwnode_pcs_notifier(&pl->fwnode_pcs_nb); + } + /* Fill the PCS list with available PCS from phylink config */ ret = phylink_fill_available_pcs(pl, config); if (ret < 0) goto free_pl; - /* Link available PCS to phylink */ - list_for_each_entry(pcs, &pl->pcs_list, list) - pcs->phylink = pl; - phy_interface_copy(pl->supported_interfaces, config->supported_interfaces); @@ -2151,10 +2229,13 @@ void phylink_destroy(struct phylink *pl) if (pl->link_gpio) gpiod_put(pl->link_gpio); + /* Unregister notifier for late PCS attach */ + if (pl->fwnode_pcs_nb.notifier_call) + unregister_fwnode_pcs_notifier(&pl->fwnode_pcs_nb); + cancel_work_sync(&pl->resolve); /* Drop link between PCS and phylink */ - /* Remove every PCS from phylink PCS list */ list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list) { pcs->phylink = NULL; list_del(&pcs->list); diff --git a/include/linux/pcs/pcs.h b/include/linux/pcs/pcs.h index b7cfdd680b2a..45e8f96662db 100644 --- a/include/linux/pcs/pcs.h +++ b/include/linux/pcs/pcs.h @@ -4,7 +4,36 @@ #include +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); + +/** + * unregister_fwnode_pcs_notifier - Unregister a notifier block for fwnode + * PCS events + * @nb: pointer to the notifier block + * + * Unregisters a notifier block to the fwnode_pcs_notify_list blocking + * notifier chain. + * + * Returns: 0 or a negative error. + */ +int unregister_fwnode_pcs_notifier(struct notifier_block *nb); + /** * fwnode_pcs_get - Retrieves a PCS from a firmware node * @fwnode: firmware node @@ -20,6 +49,25 @@ struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, unsigned int index); +/** + * 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_count - count PCS entries described in firmware node * @fwnode: firmware node @@ -53,12 +101,29 @@ int fwnode_phylink_pcs_parse(struct fwnode_handle *fwnode, 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 int unregister_fwnode_pcs_notifier(struct notifier_block *nb) +{ + return -EOPNOTSUPP; +} + static inline struct phylink_pcs *fwnode_pcs_get(struct fwnode_handle *fwnode, unsigned int index) { return ERR_PTR(-ENOENT); } +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 unsigned int fwnode_phylink_pcs_count(struct fwnode_handle *fwnode) { return 0; -- 2.53.0