From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 995FC606A32D for ; Sun, 29 Nov 2026 23:06:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=dPam1ZyR; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=ZY8L9J/l 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-Unsubscribe-Post: List-Owner; bh=wDXryE2ji7A2ctWcQ25CCPZL0HaZhiVZi5mcTizyijs=; b=dPam1ZyRx6XKb8 oyfZGw1x45k+Wz8ENcFw4NjD0fbYVa+cd6coUSeeRdrUYWSb0RN21zgJ8Xby7131uflSRKxjqhgd1 eaQhNKTzlFAx0MecQ1EmeD8ysAB0kmGKsmVP4Zx33xcPwJwNjE05ZHXrHg91M3l7D0YUh6GcNIiQ2 o97vSHkOeF6uRHv6syZB+73rRnvf+rfS2n/8/kwvGJa780SHcsbFdy50CDWpwip2Cjw07YKIo9CxQ Mz1b2WFc1H84bHiS8p1qyC3wMYeP4cwKlLsr9rOyeW9OdPPxXoBXzSbV92Tid0WR/mvBrLgFh5zWU p24UiRww2eKpCJv4xzaw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXnyx-00000002ZhD-16WA; Sun, 29 Nov 2026 23:06:55 +0000 Received: from mail-wr1-x435.google.com ([2a00:1450:4864:20::435]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xXnyV-00000002ZOD-1cUE for linux-mediatek@lists.infradead.org; Sun, 29 Nov 2026 23:06:32 +0000 Received: by mail-wr1-x435.google.com with SMTP id ffacd0b85a97d-492f0a1b685so7096790f8f.0 for ; Sun, 29 Nov 2026 15:06:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795993586; x=1796598386; 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=wDXryE2ji7A2ctWcQ25CCPZL0HaZhiVZi5mcTizyijs=; b=ZY8L9J/lqZye242+Q/oRrP3szpSBgigg35sAp+kZcICUf4J2rVERRLI+L2MsJKanv0 XE8qcqgAtU/fEeIIfGFqKnccsy/we6xtDQ4XVS8aA3NzcufR94dvxi3O5qSsAriEr78F Sw5HbmWsRmLRZLGnKnhq9thB54WyGm4ezgBZhmSMQS26pv+742QvTEhXTiCaNuGOcVZP kPstTTs9fVsyjEvTUIx5eTZG6HNjoQweX27gOsYwNnPhz3lFpOe9R0qdZ62G+fB5sRnP 2yhttzLmtIiqlXmTQ3onHPMPJCGShLMdAuPJwkWb1aY/QGfo0gy7XFmU4+saMBDaL1W2 l20Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795993586; x=1796598386; 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=wDXryE2ji7A2ctWcQ25CCPZL0HaZhiVZi5mcTizyijs=; b=o1MHWZHgKnHPFmimk2DzXiGxRLxQaN7HVbq/4eKVZQnhuP9mcuRx8BdLs94nCdSpjk txMmDyeJwubzK/AEhZQ7eqxGk7Cv1ZhjbiPrt23TnPI7JA6zWi7vph+Rwy4V87LBRfmW NoGNeSx3Mej7jZmDl4X6jNUv3WE/miRDU0CsjWI+dwIlt/H4JZ/wXEk+2lqsQB/Js/Z3 xh87im+lSxm6z/uGRXl3qrwJ2Zb5xnopP/lVNEtgcMhOv5H+ccpktiNVlyAo/Vflo+rV NPu41VvL7bU3s6YBUYx72lDhXRvI9GfDEnI3jF94mqx66mgh4vNVqZB/uN6sPIDSu5CK pNhQ== X-Forwarded-Encrypted: i=1; AL+hErHjaPTJT6K0hPOGOp+sa/H5tACF/T7okidZ1hJQMBFBAFBXysog19DnUBHtAbxQExDa1lsaZ1pdQPrhNQhaPA==@lists.infradead.org X-Gm-Message-State: APuU4Z+K0ES2gTsHskFtOkoShHgy5pH8sAB3eJNgspOllqxqMtAqxyi1 xXPSoIaP7Rqz7WEec40poIq/QR3YG1pdGY53vbs1OghRjeSnK8wqfvbQ X-Gm-Gg: AZFdTZf507Vs/jeYH3iTCIBdk7ayVah/5zhmS/rKAQ/vykz23BxbJg+MzCwEnpd19X3 NXcBzA0HoOOZ6GPEmhkbv3rFrSxm4SDG1iNMzPKbIHvygEewA81ud2WsnwzAlR5k4qcA4uL0VTB 1ChIi1gO9N7udxZryZQa1EkYPBtxmD+IIup6S8iiOgfOE4o2glvILle++mdiX/i3JWfa9EtN8W5 7pyJfa6EdkfA1lSlKovx541lZ5eNaFOH0AriaVpdFfXgUL5dwX/FfGxVSZEw9I114tkjrdo+I0D ftp87X+xUMFE1yBYiP0HC72kkmARZ8cBBBFqsJh6fa+OelXiskvqIZAU8pBnCBtCdKWAzpbd3g6 +1DlLqY1hhhMaoWrq2ChJdXHDq8JrRf2qaFJi/EO0Fo4NSLhF5kidpNrc3oBeUiRfWIujrcsdjG CNcD4BqrOvuoVr7Gu8yxFe6q8XaIDG3VJURhJ0dDlZblXKV7IdWMZiJaP8nCooyDW3FoPkITkAM oG0HMe+6hTb1/98X8khPRZ/0xCUPFSmhVnhMe9Yyxb99LWtKzlzggqm299e9VdPmOg4FLMfSSIa SknRLE2zjYXj11ygFNqSXk1VlXnJf7x4KqeqfD9G1p5JafytJX8Dv6X83Qxn68CHMk6AJiIOD6T C7///4WAejoCijU2PTE+CoGT+59uZ86rVKqAZRnk= X-Received: by 2002:a05:6000:27d4:b0:4a6:d298:8328 with SMTP id ffacd0b85a97d-4abb5db7837mr39308648f8f.54.1795993585629; Sun, 29 Nov 2026 15:06:25 -0800 (PST) Received: from Ansuel-XPS24 (93-34-92-103.ip49.fastwebnet.it. [93.34.92.103]) by smtp.googlemail.com with ESMTPSA id ffacd0b85a97d-4ab6c4353d6sm58448894f8f.56.2026.11.29.15.06.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 29 Nov 2026 15:06:25 -0800 (PST) From: Christian Marangi To: Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Rob Herring , Krzysztof Kozlowski , Conor Dooley , Willem de Bruijn , Simon Horman , Jonathan Corbet , Shuah Khan , Randy Dunlap , Christian Marangi , Lorenzo Bianconi , Heiner Kallweit , Russell King , Maxime Chevallier , Philipp Zabel , Nathan Chancellor , Nick Desaulniers , Bill Wendling , Justin Stitt , "Russell King (Oracle)" , netdev@vger.kernel.org, devicetree@vger.kernel.org, linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, llvm@lists.linux.dev Subject: [PATCH net-next v23 03/15] net: phylink: introduce internal phylink PCS handling Date: Mon, 30 Nov 2026 00:05:33 +0100 Message-ID: <20261129230558.612526-4-ansuelsmth@gmail.com> X-Mailer: git-send-email 2.55.0 In-Reply-To: <20261129230558.612526-1-ansuelsmth@gmail.com> References: <20261129230558.612526-1-ansuelsmth@gmail.com> X-Archived-By: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.9.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20261129_150627_591683_3D783650 X-CRM114-Status: GOOD ( 38.63 ) X-Spam-Score: -2.1 (--) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" X-Spam-Status: No, score=-15.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 Introduce internal handling of PCS for phylink. This is an alternative way to .mac_select_pcs that moves the selection logic of the PCS entirely to phylink with the usage of the supported_interfaces bitmap in the PCS struct. A MAC using internal PCS handling should provide a callback to fill the available PCS in .fill_available_pcs in phylink_config and set .num_possible_pcs to the number of elements in the array. The MAC should also define a new bitmap, pcs_interfaces, in phylink_config to define which interface modes require a dedicated PCS. In phylink_create(), when .num_possible_pcs is nonzero, an array of PCS pointers is allocated with .num_possible_pcs entries and .fill_available_pcs is called with the phylink_config, the array and its size. The MAC will fill this array with pointers to the available PCS. This array is then parsed and a linked list of PCS is created based on the allocated PCS array filled by the MAC via .fill_available_pcs(), skipping empty entries and PCS instances that are already attached. Link each PCS to its phylink instance with rcu_assign_pointer(). Use a global mutex to serialize the ownership check and updates across phylink instances. Rebuild phylink's supported_interfaces from the MAC bitmap and the PCS modes that are also set in pcs_interfaces. Share PCS teardown between phylink_destroy() and the phylink_create() error path. Clear the PCS associations, unlink the list entries and wait for an RCU grace period before freeing phylink. phylink_validate_mac_and_pcs(), phylink_major_config() and phylink_inband_caps() are updated to support this new implementation with the PCS list stored in phylink. For interface modes set in pcs_interfaces, they walk the list and use phylink_validate_pcs_interface() to find the first PCS that supports the requested interface. phylink_major_config() fails if it cannot find a PCS for that interface. phylink_validate_pcs_interface() applies the same validation previously used for a PCS returned by .mac_select_pcs. An empty supported_interfaces bitmap in the PCS struct means that all interfaces are supported. A MAC using the PCS list must set pcs_interfaces, .num_possible_pcs and .fill_available_pcs together. phylink_create() returns -EINVAL if the configuration is incomplete or if the PCS list is combined with .mac_select_pcs. The phylink pointer in struct phylink_pcs tracks when a PCS is attached to a phylink instance. With internal PCS handling, the pointer stays set while the PCS is on the list, even when another PCS is selected. phylink_pcs_change() is also changed to verify that the PCS that triggered a link change is the one that is currently used by the phylink instance. The .mac_select_pcs implementation is not changed but it's expected that every MAC driver migrates to the new implementation to later deprecate and remove .mac_select_pcs. Signed-off-by: Christian Marangi --- drivers/net/phy/phylink.c | 259 +++++++++++++++++++++++++++++++++----- include/linux/phylink.h | 16 +++ 2 files changed, 244 insertions(+), 31 deletions(-) diff --git a/drivers/net/phy/phylink.c b/drivers/net/phy/phylink.c index 20759fe47eaa..35e82cccd39a 100644 --- a/drivers/net/phy/phylink.c +++ b/drivers/net/phy/phylink.c @@ -27,6 +27,9 @@ #include "sfp.h" #include "swphy.h" +/* Serializes changes to PCS list ownership across phylink instances */ +static DEFINE_MUTEX(phylink_pcs_ownership_mutex); + enum { PHYLINK_DISABLE_STOPPED, PHYLINK_DISABLE_LINK, @@ -63,6 +66,9 @@ struct phylink { /* The link configuration settings */ struct phylink_link_state link_config; + /* List of available PCS */ + struct list_head pcs_list; + /* * What interface are supported by the current link. * Can change on removal or addition of new PCS. @@ -523,6 +529,23 @@ static void phylink_validate_mask_caps(unsigned long *supported, linkmode_and(state->advertising, state->advertising, mask); } +static int phylink_validate_pcs_interface(struct phylink_pcs *pcs, + phy_interface_t interface) +{ + /* + * If PCS define an empty supported_interfaces value, assume + * all interface are supported. + */ + if (phy_interface_empty(pcs->supported_interfaces)) + return 0; + + /* Ensure that this PCS supports the interface mode */ + if (!test_bit(interface, pcs->supported_interfaces)) + return -EINVAL; + + return 0; +} + static int phylink_validate_mac_and_pcs(struct phylink *pl, unsigned long *supported, struct phylink_link_state *state) @@ -536,6 +559,21 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl, pcs = pl->mac_ops->mac_select_pcs(pl->config, state->interface); if (IS_ERR(pcs)) return PTR_ERR(pcs); + /* + * Find a PCS in available PCS list for the requested interface. + * + * Skip searching if the MAC doesn't require a dedicated PCS for + * the requested interface. + */ + } else if (test_bit(state->interface, pl->config->pcs_interfaces)) { + struct phylink_pcs *tmp; + + list_for_each_entry(tmp, &pl->pcs_list, list) { + if (!phylink_validate_pcs_interface(tmp, state->interface)) { + pcs = tmp; + break; + } + } } if (pcs) { @@ -550,13 +588,10 @@ static int phylink_validate_mac_and_pcs(struct phylink *pl, return -EINVAL; } - /* Ensure that this PCS supports the interface which the MAC - * returned it for. It is an error for the MAC to return a PCS - * that does not support the interface mode. - */ - if (!phy_interface_empty(pcs->supported_interfaces) && - !test_bit(state->interface, pcs->supported_interfaces)) { - phylink_err(pl, "MAC returned PCS which does not support %s\n", + /* Recheck PCS to handle legacy way for .mac_select_pcs */ + ret = phylink_validate_pcs_interface(pcs, state->interface); + if (ret) { + phylink_err(pl, "selected PCS does not support %s\n", phy_modes(state->interface)); return -EINVAL; } @@ -969,12 +1004,22 @@ static void phylink_pcs_enable_eee(struct phylink_pcs *pcs) static unsigned int phylink_inband_caps(struct phylink *pl, phy_interface_t interface) { - struct phylink_pcs *pcs; + struct phylink_pcs *pcs = NULL; - if (!pl->mac_ops->mac_select_pcs) - return 0; + if (pl->mac_ops->mac_select_pcs) { + pcs = pl->mac_ops->mac_select_pcs(pl->config, + interface); + } else if (test_bit(interface, pl->config->pcs_interfaces)) { + struct phylink_pcs *tmp; + + list_for_each_entry(tmp, &pl->pcs_list, list) { + if (!phylink_validate_pcs_interface(tmp, interface)) { + pcs = tmp; + break; + } + } + } - pcs = pl->mac_ops->mac_select_pcs(pl->config, interface); if (IS_ERR_OR_NULL(pcs)) return 0; @@ -1271,10 +1316,37 @@ static void phylink_major_config(struct phylink *pl, bool restart, pl->major_config_failed = true; return; } + /* + * Find a PCS in available PCS list for the requested interface. + * This doesn't overwrite the previous .mac_select_pcs as either + * .mac_select_pcs or PCS list implementation are permitted. + * + * Skip searching if the MAC doesn't require a dedicated PCS for + * the requested interface. + */ + } else if (test_bit(state->interface, pl->config->pcs_interfaces)) { + struct phylink_pcs *tmp; + + list_for_each_entry(tmp, &pl->pcs_list, list) { + if (!phylink_validate_pcs_interface(tmp, + state->interface)) { + pcs = tmp; + break; + } + } + + if (!pcs) { + phylink_err(pl, + "couldn't find a PCS for %s\n", + phy_modes(state->interface)); - pcs_changed = pl->pcs != pcs; + pl->major_config_failed = true; + return; + } } + pcs_changed = pl->pcs != pcs; + phylink_pcs_neg_mode(pl, pcs, state->interface, state->advertising); phylink_dbg(pl, "major config, active %s/%s/%s\n", @@ -1301,13 +1373,15 @@ static void phylink_major_config(struct phylink *pl, bool restart, if (pcs_changed) { phylink_pcs_disable(pl->pcs); - if (pl->pcs) - rcu_assign_pointer(pl->pcs->phylink, NULL); + if (pl->mac_ops->mac_select_pcs) { + if (pl->pcs) + rcu_assign_pointer(pl->pcs->phylink, NULL); - if (pcs) - rcu_assign_pointer(pcs->phylink, pl); + if (pcs) + rcu_assign_pointer(pcs->phylink, pl); + } - pl->pcs = pcs; + WRITE_ONCE(pl->pcs, pcs); } if (pl->pcs) @@ -1922,6 +1996,58 @@ int phylink_set_fixed_link(struct phylink *pl, } EXPORT_SYMBOL_GPL(phylink_set_fixed_link); +static bool phylink_add_pcs(struct phylink *pl, struct phylink_pcs *pcs) +{ + struct phylink *owner; + bool added = false; + + if (!pcs) + return false; + + mutex_lock(&phylink_pcs_ownership_mutex); + owner = rcu_access_pointer(pcs->phylink); + if (owner == pl) + goto out; + + if (WARN(owner, "%s: PCS already attached to a phylink instance\n", + __func__)) + goto out; + + list_add_tail(&pcs->list, &pl->pcs_list); + rcu_assign_pointer(pcs->phylink, pl); + added = true; + +out: + mutex_unlock(&phylink_pcs_ownership_mutex); + return added; +} + +static int phylink_fill_available_pcs(struct phylink *pl, + struct phylink_config *config) +{ + struct phylink_pcs **pcss; + int i, ret; + + if (!config->num_possible_pcs) + return 0; + + pcss = kzalloc_objs(*pcss, config->num_possible_pcs); + if (!pcss) + return -ENOMEM; + + ret = config->fill_available_pcs(config, pcss, config->num_possible_pcs); + if (ret < 0) + goto out; + + for (i = 0; i < config->num_possible_pcs; i++) + phylink_add_pcs(pl, pcss[i]); + +out: + kfree(pcss); + + return ret; +} + /** * phylink_update_pause_state() - Update the phylink pause frame configuration * @pl: a pointer to a &struct phylink instance @@ -2042,6 +2168,62 @@ void phylink_update_mac_pause_capabilities(struct phylink *pl, unsigned long mac } EXPORT_SYMBOL_GPL(phylink_update_mac_pause_capabilities); +static void phylink_parse_supported_interfaces(struct phylink *pl) +{ + struct phylink_pcs *pcs; + + phy_interface_copy(pl->supported_interfaces, + pl->config->supported_interfaces); + + /* + * Only add PCS-supported modes that phylink will select a PCS for. + * Otherwise, phylink may accept a PCS-only mode and configure it without + * a PCS. + */ + list_for_each_entry(pcs, &pl->pcs_list, list) { + DECLARE_PHY_INTERFACE_MASK(pcs_interfaces); + + phy_interface_copy(pcs_interfaces, pl->config->pcs_interfaces); + + if (!phy_interface_empty(pcs->supported_interfaces)) + phy_interface_and(pcs_interfaces, pcs_interfaces, + pcs->supported_interfaces); + + phy_interface_or(pl->supported_interfaces, + pl->supported_interfaces, + pcs_interfaces); + } +} + +static void phylink_detach_pcss(struct phylink *pl) +{ + struct phylink_pcs *pcs, *tmp; + + mutex_lock(&pl->state_mutex); + + mutex_lock(&phylink_pcs_ownership_mutex); + /* Remove every PCS from phylink PCS list */ + list_for_each_entry_safe(pcs, tmp, &pl->pcs_list, list) { + rcu_assign_pointer(pcs->phylink, NULL); + list_del(&pcs->list); + } + + if (pl->pcs) { + rcu_assign_pointer(pl->pcs->phylink, NULL); + WRITE_ONCE(pl->pcs, NULL); + } + mutex_unlock(&phylink_pcs_ownership_mutex); + + /* + * Unconditionally wait for readers that may have obtained this + * phylink through a PCS while phylink_major_config() was clearing + * the association. + */ + synchronize_rcu(); + + mutex_unlock(&pl->state_mutex); +} + /** * phylink_create() - create a phylink instance * @config: a pointer to the target &struct phylink_config @@ -2063,9 +2245,25 @@ struct phylink *phylink_create(struct phylink_config *config, phy_interface_t iface, const struct phylink_mac_ops *mac_ops) { + bool has_pcs_interfaces = !phy_interface_empty(config->pcs_interfaces); + bool has_pcs_callback = config->fill_available_pcs; + bool has_pcs_count = config->num_possible_pcs > 0; struct phylink *pl; int ret; + if (has_pcs_interfaces != has_pcs_callback || + has_pcs_interfaces != has_pcs_count) { + dev_err(config->dev, + "phylink: error: pcs_interfaces, num_possible_pcs and fill_available_pcs must be set together\n"); + return ERR_PTR(-EINVAL); + } + + if (mac_ops->mac_select_pcs && has_pcs_interfaces) { + dev_err(config->dev, + "phylink: error: either phylink_config .mac_select_pcs or pcs_interfaces must be used\n"); + return ERR_PTR(-EINVAL); + } + /* Validate the supplied configuration */ if (phy_interface_empty(config->supported_interfaces)) { dev_err(config->dev, @@ -2080,6 +2278,7 @@ struct phylink *phylink_create(struct phylink_config *config, mutex_init(&pl->phydev_mutex); mutex_init(&pl->state_mutex); INIT_WORK(&pl->resolve, phylink_resolve); + INIT_LIST_HEAD(&pl->pcs_list); pl->config = config; if (config->type == PHYLINK_NETDEV) { @@ -2117,8 +2316,12 @@ struct phylink *phylink_create(struct phylink_config *config, __set_bit(PHYLINK_DISABLE_STOPPED, &pl->phylink_disable_state); timer_setup(&pl->link_poll, phylink_fixed_poll, 0); - phy_interface_copy(pl->supported_interfaces, - pl->config->supported_interfaces); + /* Fill the PCS list with available PCS from phylink config */ + ret = phylink_fill_available_pcs(pl, config); + if (ret < 0) + goto free_pl; + + phylink_parse_supported_interfaces(pl); linkmode_fill(pl->supported); linkmode_copy(pl->link_config.advertising, pl->supported); @@ -2126,7 +2329,7 @@ struct phylink *phylink_create(struct phylink_config *config, ret = phylink_parse_mode(pl, fwnode); if (ret < 0) - goto free_pl; + goto unlink_pcs_list; if (pl->cfg_link_an_mode == MLO_AN_FIXED) { ret = phylink_parse_fixedlink(pl, fwnode); @@ -2145,6 +2348,8 @@ struct phylink *phylink_create(struct phylink_config *config, release_link_gpio: if (pl->link_gpio) gpiod_put(pl->link_gpio); +unlink_pcs_list: + phylink_detach_pcss(pl); free_pl: kfree(pl); return ERR_PTR(ret); @@ -2167,17 +2372,9 @@ void phylink_destroy(struct phylink *pl) if (pl->link_gpio) gpiod_put(pl->link_gpio); - if (pl->pcs) - rcu_assign_pointer(pl->pcs->phylink, NULL); - - /* - * Unconditionally wait for readers that may have obtained this - * phylink through a PCS while phylink_major_config() was clearing - * the association. - */ - synchronize_rcu(); - + phylink_detach_pcss(pl); cancel_work_sync(&pl->resolve); + kfree(pl); } EXPORT_SYMBOL_GPL(phylink_destroy); @@ -2646,7 +2843,7 @@ void phylink_pcs_change(struct phylink_pcs *pcs, bool up) rcu_read_lock(); pl = rcu_dereference(pcs->phylink); - if (pl) + if (pl && READ_ONCE(pl->pcs) == pcs) phylink_link_changed(pl, up, "pcs"); rcu_read_unlock(); diff --git a/include/linux/phylink.h b/include/linux/phylink.h index 27515d0bc57e..a56289ecee58 100644 --- a/include/linux/phylink.h +++ b/include/linux/phylink.h @@ -12,6 +12,7 @@ struct ethtool_cmd; struct fwnode_handle; struct net_device; struct phylink; +struct phylink_pcs; enum { MLO_PAUSE_NONE, @@ -151,6 +152,8 @@ enum phylink_op_type { * if MAC link is at %MLO_AN_FIXED mode. * @supported_interfaces: bitmap describing which PHY_INTERFACE_MODE_xxx * are supported by the MAC/PCS. + * @pcs_interfaces: bitmap describing for which PHY_INTERFACE_MODE_xxx a + * dedicated PCS is required. * @lpi_interfaces: bitmap describing which PHY interface modes can support * LPI signalling. * @mac_capabilities: MAC pause/speed/duplex capabilities. @@ -160,6 +163,10 @@ enum phylink_op_type { * @wol_phy_legacy: Use Wake-on-Lan with PHY even if phy_can_wakeup() is false * @wol_phy_speed_ctrl: Use phy speed control on suspend/resume * @wol_mac_support: Bitmask of MAC supported %WAKE_* options + * @num_possible_pcs: num of possible phylink_pcs PCS + * @fill_available_pcs: callback to fill the available PCS in the passed + * array struct of phylink_pcs PCS available_pcs up to + * num_possible_pcs. */ struct phylink_config { struct device *dev; @@ -172,6 +179,7 @@ struct phylink_config { void (*get_fixed_state)(struct phylink_config *config, struct phylink_link_state *state); DECLARE_PHY_INTERFACE_MASK(supported_interfaces); + DECLARE_PHY_INTERFACE_MASK(pcs_interfaces); DECLARE_PHY_INTERFACE_MASK(lpi_interfaces); unsigned long mac_capabilities; unsigned long lpi_capabilities; @@ -182,6 +190,11 @@ struct phylink_config { bool wol_phy_legacy; bool wol_phy_speed_ctrl; u32 wol_mac_support; + + unsigned int num_possible_pcs; + int (*fill_available_pcs)(struct phylink_config *config, + struct phylink_pcs **available_pcs, + unsigned int num_possible_pcs); }; void phylink_limit_mac_speed(struct phylink_config *config, u32 max_speed); @@ -497,6 +510,9 @@ struct phylink_pcs { struct phylink __rcu *phylink; bool poll; bool rxc_always_on; + + /* private: */ + struct list_head list; }; /** -- 2.55.0