Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp7381209rwl; Thu, 23 Mar 2023 03:39:29 -0700 (PDT) X-Google-Smtp-Source: AK7set9rwTu+PmR3Yc6t7weBrHx4EcjazTD1P7Cq103eaMlDRpRCSuk0bPGgsToshGW7SD9T36Ul X-Received: by 2002:aa7:d9cd:0:b0:4ac:bcf9:6d6c with SMTP id v13-20020aa7d9cd000000b004acbcf96d6cmr9134132eds.36.1679567969186; Thu, 23 Mar 2023 03:39:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679567969; cv=none; d=google.com; s=arc-20160816; b=aNgW7N4KdgF6wgKkGjTM1pUTLThzQKR2tB6BpSw5Qq8jwSXk+9WMhfTS+fLbACiPzc CmFImul2oGYgiWHLcaGioARBFiS+056thU3/xHjTQ4FPmwhaJAW6PKP9cuNciCTOKBw7 AfoW5Vbzi1GDFgc6HFqkSH3yhXv8zabirtjStvD8WJXfj0XsW27rX4pQhnjp8NqqXx55 Y3FY98fZYaAAGOPNjt0WBixAOxT3pIB3vXh4ve3PV6G95J0mV9a0+W/NIB24sGq+oHRo dh5ej1yHzRKOe7qqK5CZEa1GWv0adsrCXMIvk2sDrr8tJame47ea7kG0m3gNEL+3NOCM ogIw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=ZXghyFKbkBEH9/EKzDk+5w9Pp/aMwqnL/aCTRT1Laoo=; b=QWl2XgEstxdiRF46HNUt6YGxZSYGtohvyo4vFnc3eechggKTzvN7SrL0jdDXlRbqF7 NS0UuMHbvWaBpueJ7EdnGFq3Pqagi3oWjAA7bYnjzBnOwzDGeNJE/nDATWE6ql/CkClt lb2nZW4+I13k2JzoV/gP+GvttzXXktMES3nK3/tKNRuHB+neBjNTiaW0Txg0Vz30po2I kejd8sxPfFl/QxKvncByK+DTN8p/ps6s1MqR1uzAsRy3W4AxHwcjjyXMwErtKRYwbHBT SiYYsOJzwtjwh6WFB/dnpfLmw4HS59i3F08ReykvkHJopLatWSxXcHGCzU/JDy+y5msR LnTw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=LJTLbTwG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a1-20020aa7d901000000b00501e600c486si4989082edr.114.2023.03.23.03.39.05; Thu, 23 Mar 2023 03:39:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@bootlin.com header.s=gm1 header.b=LJTLbTwG; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=bootlin.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229885AbjCWKbB (ORCPT + 99 others); Thu, 23 Mar 2023 06:31:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42696 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231400AbjCWK3n (ORCPT ); Thu, 23 Mar 2023 06:29:43 -0400 Received: from relay2-d.mail.gandi.net (relay2-d.mail.gandi.net [217.70.183.194]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C78823803A; Thu, 23 Mar 2023 03:27:22 -0700 (PDT) Received: (Authenticated sender: herve.codina@bootlin.com) by mail.gandi.net (Postfix) with ESMTPA id C98CB40007; Thu, 23 Mar 2023 10:27:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bootlin.com; s=gm1; t=1679567231; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ZXghyFKbkBEH9/EKzDk+5w9Pp/aMwqnL/aCTRT1Laoo=; b=LJTLbTwGPnQSRj8spEu6q0fcfKe1kXQvZU5lYdeDWKD6iV27Fom4Zonxo1imQoDv0rd5uq claLSkqz5Om9N7BsV18vNOjhZeezZ7JBTNTdK6VUzQfkCsHKzSBcNFgdicF2SvAvfiof2b zA59gj8Qm0h/wvaNVklyIL+cCSu/Uynyu7m/mCiyykcUXoA5OHAKgar82eUKb7H+DGlqMV pjeCDKQ8us6XZPOeg+6efzlmiVn4ev0w06sYugUT7H7mHkXFqvVWL15xsh1powdkuCW7bF 4dQKnNjXDzoeAm8rjH7+CV+wtNN7rlCi1eBM3XuXTU+kOg8GyYusK8BEGCVn8w== From: Herve Codina To: Herve Codina , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Vinod Koul , Kishon Vijay Abraham I Cc: linux-kernel@vger.kernel.org, netdev@vger.kernel.org, linux-phy@lists.infradead.org, Christophe Leroy , Thomas Petazzoni Subject: [RFC PATCH 3/4] net: wan: fsl_qmc_hdlc: Add PHY support Date: Thu, 23 Mar 2023 11:26:54 +0100 Message-Id: <20230323102655.264115-4-herve.codina@bootlin.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230323102655.264115-1-herve.codina@bootlin.com> References: <20230323102655.264115-1-herve.codina@bootlin.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_LOW,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add PHY support in the fsl_qmc_hdlc driver in order to be able to signal carrier changes to the network stack based on the PHY status. Signed-off-by: Herve Codina --- drivers/net/wan/fsl_qmc_hdlc.c | 152 ++++++++++++++++++++++++++++++++- 1 file changed, 151 insertions(+), 1 deletion(-) diff --git a/drivers/net/wan/fsl_qmc_hdlc.c b/drivers/net/wan/fsl_qmc_hdlc.c index f12d00c78497..edea0f678ffe 100644 --- a/drivers/net/wan/fsl_qmc_hdlc.c +++ b/drivers/net/wan/fsl_qmc_hdlc.c @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -27,6 +28,11 @@ struct qmc_hdlc { struct device *dev; struct qmc_chan *qmc_chan; struct net_device *netdev; + struct phy *phy; + spinlock_t carrier_lock; /* Protect carrier detection */ + struct notifier_block nb; + bool is_phy_notifier; + struct delayed_work phy_poll_task; bool is_crc32; spinlock_t tx_lock; /* Protect tx descriptors */ struct qmc_hdlc_desc tx_descs[8]; @@ -39,6 +45,126 @@ static inline struct qmc_hdlc *netdev_to_qmc_hdlc(struct net_device *netdev) return (struct qmc_hdlc *)dev_to_hdlc(netdev)->priv; } +static int qmc_hdlc_phy_set_carrier(struct qmc_hdlc *qmc_hdlc) +{ + union phy_status phy_status; + unsigned long flags; + int ret; + + if (!qmc_hdlc->phy) + return 0; + + spin_lock_irqsave(&qmc_hdlc->carrier_lock, flags); + + ret = phy_get_status(qmc_hdlc->phy, &phy_status); + if (ret) { + dev_err(qmc_hdlc->dev, "get PHY status failed (%d)\n", ret); + goto end; + } + if (phy_status.basic.link_is_on) + netif_carrier_on(qmc_hdlc->netdev); + else + netif_carrier_off(qmc_hdlc->netdev); + +end: + spin_unlock_irqrestore(&qmc_hdlc->carrier_lock, flags); + return ret; +} + +static int qmc_hdlc_phy_notifier(struct notifier_block *nb, unsigned long action, + void *data) +{ + struct qmc_hdlc *qmc_hdlc = container_of(nb, struct qmc_hdlc, nb); + int ret; + + if (action != PHY_EVENT_STATUS) + return NOTIFY_DONE; + + ret = qmc_hdlc_phy_set_carrier(qmc_hdlc); + return ret ? NOTIFY_DONE : NOTIFY_OK; +} + +static void qmc_hdlc_phy_poll_task(struct work_struct *work) +{ + struct qmc_hdlc *qmc_hdlc = container_of(work, struct qmc_hdlc, phy_poll_task.work); + int ret; + + ret = qmc_hdlc_phy_set_carrier(qmc_hdlc); + if (ret) { + /* Should not happened but ... + * On error, force carrier on and stop scheduling this task + */ + dev_err(qmc_hdlc->dev, "set carrier failed (%d) -> force carrier on\n", + ret); + netif_carrier_on(qmc_hdlc->netdev); + return; + } + + /* Re-schedule task in 1 sec */ + queue_delayed_work(system_power_efficient_wq, &qmc_hdlc->phy_poll_task, 1 * HZ); +} + +static int qmc_hdlc_phy_init(struct qmc_hdlc *qmc_hdlc) +{ + union phy_status phy_status; + int ret; + + if (!qmc_hdlc->phy) + return 0; + + ret = phy_init(qmc_hdlc->phy); + if (ret) { + dev_err(qmc_hdlc->dev, "PHY init failed (%d)\n", ret); + return ret; + } + + ret = phy_power_on(qmc_hdlc->phy); + if (ret) { + dev_err(qmc_hdlc->dev, "PHY power-on failed (%d)\n", ret); + goto phy_exit; + } + + /* Be sure that get_status is supported */ + ret = phy_get_status(qmc_hdlc->phy, &phy_status); + if (ret) { + dev_err(qmc_hdlc->dev, "get PHY status failed (%d)\n", ret); + goto phy_power_off; + } + + qmc_hdlc->nb.notifier_call = qmc_hdlc_phy_notifier; + ret = phy_atomic_notifier_register(qmc_hdlc->phy, &qmc_hdlc->nb); + if (ret) { + qmc_hdlc->is_phy_notifier = false; + + /* Cannot register a PHY notifier -> Use polling */ + INIT_DELAYED_WORK(&qmc_hdlc->phy_poll_task, qmc_hdlc_phy_poll_task); + queue_delayed_work(system_power_efficient_wq, &qmc_hdlc->phy_poll_task, 1 * HZ); + } else { + qmc_hdlc->is_phy_notifier = true; + } + + return 0; + +phy_power_off: + phy_power_off(qmc_hdlc->phy); +phy_exit: + phy_exit(qmc_hdlc->phy); + return ret; +} + +static void qmc_hdlc_phy_exit(struct qmc_hdlc *qmc_hdlc) +{ + if (!qmc_hdlc->phy) + return; + + if (qmc_hdlc->is_phy_notifier) + phy_atomic_notifier_unregister(qmc_hdlc->phy, &qmc_hdlc->nb); + else + cancel_delayed_work_sync(&qmc_hdlc->phy_poll_task); + phy_power_off(qmc_hdlc->phy); + phy_exit(qmc_hdlc->phy); +} + static int qmc_hdlc_recv_queue(struct qmc_hdlc *qmc_hdlc, struct qmc_hdlc_desc *desc, size_t size); static void qmc_hcld_recv_complete(void *context, size_t length) @@ -192,10 +318,17 @@ static int qmc_hdlc_open(struct net_device *netdev) int ret; int i; - ret = hdlc_open(netdev); + ret = qmc_hdlc_phy_init(qmc_hdlc); if (ret) return ret; + ret = hdlc_open(netdev); + if (ret) + goto phy_exit; + + /* Update carrier */ + qmc_hdlc_phy_set_carrier(qmc_hdlc); + chan_param.mode = QMC_HDLC; /* HDLC_MAX_MRU + 4 for the CRC * HDLC_MAX_MRU + 4 + 8 for the CRC and some extraspace needed by the QMC @@ -245,6 +378,8 @@ static int qmc_hdlc_open(struct net_device *netdev) } hdlc_close: hdlc_close(netdev); +phy_exit: + qmc_hdlc_phy_exit(qmc_hdlc); return ret; } @@ -280,6 +415,7 @@ static int qmc_hdlc_close(struct net_device *netdev) } hdlc_close(netdev); + qmc_hdlc_phy_exit(qmc_hdlc); return 0; } @@ -318,6 +454,7 @@ static int qmc_hdlc_probe(struct platform_device *pdev) struct device_node *np = pdev->dev.of_node; struct qmc_hdlc *qmc_hdlc; struct qmc_chan_info info; + enum phy_mode phy_mode; hdlc_device *hdlc; int ret; @@ -327,6 +464,7 @@ static int qmc_hdlc_probe(struct platform_device *pdev) qmc_hdlc->dev = &pdev->dev; spin_lock_init(&qmc_hdlc->tx_lock); + spin_lock_init(&qmc_hdlc->carrier_lock); qmc_hdlc->qmc_chan = devm_qmc_chan_get_byphandle(qmc_hdlc->dev, np, "fsl,qmc-chan"); if (IS_ERR(qmc_hdlc->qmc_chan)) { @@ -348,6 +486,18 @@ static int qmc_hdlc_probe(struct platform_device *pdev) return -EINVAL; } + qmc_hdlc->phy = devm_of_phy_optional_get(qmc_hdlc->dev, np, NULL); + if (IS_ERR(qmc_hdlc->phy)) + return PTR_ERR(qmc_hdlc->phy); + if (qmc_hdlc->phy) { + phy_mode = phy_get_mode(qmc_hdlc->phy); + if (phy_mode != PHY_MODE_BASIC) { + dev_err(qmc_hdlc->dev, "Unsupported PHY mode (%d)\n", + phy_mode); + return -EINVAL; + } + } + qmc_hdlc->netdev = alloc_hdlcdev(qmc_hdlc); if (!qmc_hdlc->netdev) { dev_err(qmc_hdlc->dev, "failed to alloc hdlc dev\n"); -- 2.39.2