From patchwork Thu Dec 10 10:02:50 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Loic Poulain X-Patchwork-Id: 340978 Delivered-To: patch@linaro.org Received: by 2002:a02:85a7:0:0:0:0:0 with SMTP id d36csp5381860jai; Thu, 10 Dec 2020 01:57:22 -0800 (PST) X-Google-Smtp-Source: ABdhPJxIk+hbXXt8s9enMnuWXDCIvLb/wHxlQExIIOp+SfyuioYIj4uCsav9g4U7Wh6e35UHo4jS X-Received: by 2002:aa7:de0f:: with SMTP id h15mr6005521edv.110.1607594242180; Thu, 10 Dec 2020 01:57:22 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1607594242; cv=none; d=google.com; s=arc-20160816; b=AU13HgK2ZiLQvAd6ihRiIpu9l0HEzYvrzFGU+Z9dckXdWjxSe9MYcSQ8ue/GGwNTHY FJj1OSMU7hthfkbV7948wFHx9UXLRKDJF03NTPCgWDzlPXscd74YPRxsYZaxMnx2YWmd kOxn4NpS6+EiyEauRWBcy7c1MCQG+rwu1M81tsqqsl8He+KzJrXJEaYaDCwSFoFIeV6M 6H0c5N0/Ol65VaXUPq7chngsJvlLoXcmdoOF12KJhDRs5cwugRZ9VlxmCtoaDkd2GhMS CxKciBxbVl0u8aYH9qC4i2Yvfl6ApYMJQQamyHvnlBxRwYAmnkpbCqFQmibXrt5dN5sk n8xw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:references:in-reply-to:message-id:date:subject :cc:to:from:dkim-signature; bh=Fnby8SpxIcKgI7tsgg/Bq2XmX0mmwcu8tdwtOs7lw6I=; b=ZTr7Rkr8ucxc+OeqrFmRlv8pIusTrCrw9ofoOd61m6EKkpLo41DmjrJlViEtOpYVvN a4AIhukyxtPiQyc1oaK+d0XO5ZS6arWFRgAtK+mynpU1ycPc+HONdMv49rzO2m4Ry/Wy QdKa/6yApf5ZrQqawLuScu5TUHzW3zPfKRjsuZ01hoczJqiBjW2g73DRRS2IhDH1ZTaK FaLlf/VT9tB2OWRa9W9H+tbkU+nrWSYfYBDtmeYB78CMMOvAcBrDOjaAL1wKOvbEoFlM SnakVEjkJo7RAXfLSVveMosg1YqxQdxTkLFvAHcqQx0c8PsYj9t/GvxQ+NvmZLfnFzA4 eYSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VZqDePHx; spf=pass (google.com: domain of linux-arm-msm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-arm-msm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 11si2499658edv.53.2020.12.10.01.57.21; Thu, 10 Dec 2020 01:57:22 -0800 (PST) Received-SPF: pass (google.com: domain of linux-arm-msm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linaro.org header.s=google header.b=VZqDePHx; spf=pass (google.com: domain of linux-arm-msm-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-arm-msm-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728802AbgLJJ4x (ORCPT + 15 others); Thu, 10 Dec 2020 04:56:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728849AbgLJJ4u (ORCPT ); Thu, 10 Dec 2020 04:56:50 -0500 Received: from mail-wr1-x441.google.com (mail-wr1-x441.google.com [IPv6:2a00:1450:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C430CC061794 for ; Thu, 10 Dec 2020 01:56:00 -0800 (PST) Received: by mail-wr1-x441.google.com with SMTP id r14so4791607wrn.0 for ; Thu, 10 Dec 2020 01:56:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=Fnby8SpxIcKgI7tsgg/Bq2XmX0mmwcu8tdwtOs7lw6I=; b=VZqDePHxQvLFAhWaH7X+wXWURjqAyXEBgDZT1S2kFY85Gu/EvkwuKQSiZQKe+ieL62 bRiFHF3aP5mdEUSGcphKp4WNq2VtgKH5Jdd3lOHH59enIaSg6KVjREj/gGTWApdvldIZ N3U5Nl7NHBiL5NhIrDYxfYi3jTfNxMA0A2HVRrQ9U+CW7Suab6KzsPOIqWYqes1jvD7M ggDmUASu6sCGyrDnndTkVK89O+cvjQ3VRhXLqlFkkzcw2nIIuhTtXQ05A+M8RrRFXRaw btV9hdaD0Ns+4t8f741XGw4njIe2tppmMh4JHkRVujgzizrzoImNikfHFvgzczq+RFBY HpQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=Fnby8SpxIcKgI7tsgg/Bq2XmX0mmwcu8tdwtOs7lw6I=; b=dmxsWGbT3nGkw0E0tTRl0e+rKaqsmNHA0K5EvEvk7O/gWB+RLSbQufnBbOik48QKZt BUN3CBWLaCwlwaiJxCtOKF/J5QhrbCPLumoYGt2aFP5jS7D8Slg8BdrQ5+gBgyVH1ELv Vxu+P8/N8o5MhIpqFXZW7c++VvVsuOpUuxrmA/w7DCFlHcux5Y5lN9m6izUW8QYpFsh0 78HLpEFXr5bKLMXfiGyyKloPvBpH/zYxDIAIophJpUou74d837B3Rbh2LOlo9rYRRPw7 5vVEdgrXQ2kXgVRP10JztGMvgNqF6tbuXd6fjrhlzOZf+KXWpDR0Znp1L0siYd9MfhdI N41A== X-Gm-Message-State: AOAM532ZoeUnGmfFQfQRvYithdPVDxUHSPz0pJyzqdPrQiFF2Z+UReBW fwMtA93AXADGUxOBEONzRXOER+VRLkfXEifX X-Received: by 2002:a5d:4bc7:: with SMTP id l7mr6214894wrt.204.1607594159504; Thu, 10 Dec 2020 01:55:59 -0800 (PST) Received: from localhost.localdomain ([2a01:e0a:490:8730:4468:1cc2:be0c:233f]) by smtp.gmail.com with ESMTPSA id l16sm9043721wrx.5.2020.12.10.01.55.58 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Dec 2020 01:55:59 -0800 (PST) From: Loic Poulain To: manivannan.sadhasivam@linaro.org, hemantk@codeaurora.org Cc: linux-arm-msm@vger.kernel.org, jhugo@codeaurora.org, bbhatt@codeaurora.org, Loic Poulain Subject: [PATCH v4 05/10] mhi: pci_generic: Add support for reset Date: Thu, 10 Dec 2020 11:02:50 +0100 Message-Id: <1607594575-31590-6-git-send-email-loic.poulain@linaro.org> X-Mailer: git-send-email 2.7.4 In-Reply-To: <1607594575-31590-1-git-send-email-loic.poulain@linaro.org> References: <1607594575-31590-1-git-send-email-loic.poulain@linaro.org> Precedence: bulk List-ID: X-Mailing-List: linux-arm-msm@vger.kernel.org Add support for resetting the device, reset can be triggered in case of error or manually via sysfs (/sys/bus/pci/devices/*/reset). Signed-off-by: Loic Poulain --- drivers/bus/mhi/pci_generic.c | 120 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 107 insertions(+), 13 deletions(-) -- 2.7.4 diff --git a/drivers/bus/mhi/pci_generic.c b/drivers/bus/mhi/pci_generic.c index 077595c..d70d3ea 100644 --- a/drivers/bus/mhi/pci_generic.c +++ b/drivers/bus/mhi/pci_generic.c @@ -8,6 +8,7 @@ * Copyright (C) 2020 Linaro Ltd */ +#include #include #include #include @@ -177,6 +178,16 @@ static const struct pci_device_id mhi_pci_id_table[] = { }; MODULE_DEVICE_TABLE(pci, mhi_pci_id_table); +enum mhi_pci_device_status { + MHI_PCI_DEV_STARTED, +}; + +struct mhi_pci_device { + struct mhi_controller mhi_cntrl; + struct pci_saved_state *pci_state; + unsigned long status; +}; + static int mhi_pci_read_reg(struct mhi_controller *mhi_cntrl, void __iomem *addr, u32 *out) { @@ -196,6 +207,20 @@ static void mhi_pci_status_cb(struct mhi_controller *mhi_cntrl, /* Nothing to do for now */ } +static bool mhi_pci_is_alive(struct mhi_controller *mhi_cntrl) +{ + struct pci_dev *pdev = to_pci_dev(mhi_cntrl->cntrl_dev); + u16 vendor = 0; + + if (pci_read_config_word(pdev, PCI_VENDOR_ID, &vendor)) + return false; + + if (vendor == (u16) ~0 || vendor == 0) + return false; + + return true; +} + static int mhi_pci_claim(struct mhi_controller *mhi_cntrl, unsigned int bar_num, u64 dma_mask) { @@ -291,16 +316,20 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) { const struct mhi_pci_dev_info *info = (struct mhi_pci_dev_info *) id->driver_data; const struct mhi_controller_config *mhi_cntrl_config; + struct mhi_pci_device *mhi_pdev; struct mhi_controller *mhi_cntrl; int err; dev_dbg(&pdev->dev, "MHI PCI device found: %s\n", info->name); - mhi_cntrl = mhi_alloc_controller(); - if (!mhi_cntrl) + mhi_pdev = devm_kzalloc(&pdev->dev, sizeof(*mhi_pdev), GFP_KERNEL); + if (!mhi_pdev) return -ENOMEM; mhi_cntrl_config = info->config; + mhi_cntrl = &mhi_pdev->mhi_cntrl; + + mhi_initialize_controller(mhi_cntrl); mhi_cntrl->cntrl_dev = &pdev->dev; mhi_cntrl->iova_start = 0; mhi_cntrl->iova_stop = DMA_BIT_MASK(info->dma_data_width); @@ -315,17 +344,21 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) err = mhi_pci_claim(mhi_cntrl, info->bar_num, DMA_BIT_MASK(info->dma_data_width)); if (err) - goto err_release; + return err; err = mhi_pci_get_irqs(mhi_cntrl, mhi_cntrl_config); if (err) - goto err_release; + return err; + + pci_set_drvdata(pdev, mhi_pdev); - pci_set_drvdata(pdev, mhi_cntrl); + /* Have stored pci confspace at hand for restore in sudden PCI error */ + pci_save_state(pdev); + mhi_pdev->pci_state = pci_store_saved_state(pdev); err = mhi_register_controller(mhi_cntrl, mhi_cntrl_config); if (err) - goto err_release; + return err; /* MHI bus does not power up the controller by default */ err = mhi_prepare_for_power_up(mhi_cntrl); @@ -340,33 +373,94 @@ static int mhi_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id) goto err_unprepare; } + set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); + return 0; err_unprepare: mhi_unprepare_after_power_down(mhi_cntrl); err_unregister: mhi_unregister_controller(mhi_cntrl); -err_release: - mhi_free_controller(mhi_cntrl); return err; } static void mhi_pci_remove(struct pci_dev *pdev) { - struct mhi_controller *mhi_cntrl = pci_get_drvdata(pdev); + struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); + struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; + + if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { + mhi_power_down(mhi_cntrl, true); + mhi_unprepare_after_power_down(mhi_cntrl); + } - mhi_power_down(mhi_cntrl, true); - mhi_unprepare_after_power_down(mhi_cntrl); mhi_unregister_controller(mhi_cntrl); - mhi_free_controller(mhi_cntrl); } +void mhi_pci_reset_prepare(struct pci_dev *pdev) +{ + struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); + struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; + + dev_info(&pdev->dev, "reset\n"); + + /* Clean up MHI state */ + if (test_and_clear_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status)) { + mhi_power_down(mhi_cntrl, false); + mhi_unprepare_after_power_down(mhi_cntrl); + } + + /* cause internal device reset */ + mhi_reg_soc_reset(mhi_cntrl); + + /* Be sure device reset has been executed */ + msleep(500); +} + +void mhi_pci_reset_done(struct pci_dev *pdev) +{ + struct mhi_pci_device *mhi_pdev = pci_get_drvdata(pdev); + struct mhi_controller *mhi_cntrl = &mhi_pdev->mhi_cntrl; + int err; + + /* Restore initial known working PCI state */ + pci_load_saved_state(pdev, mhi_pdev->pci_state); + pci_restore_state(pdev); + + /* Is device status available ? */ + if (!mhi_pci_is_alive(mhi_cntrl)) { + dev_err(&pdev->dev, "reset failed\n"); + return; + } + + err = mhi_prepare_for_power_up(mhi_cntrl); + if (err) { + dev_err(&pdev->dev, "failed to prepare MHI controller\n"); + return; + } + + err = mhi_sync_power_up(mhi_cntrl); + if (err) { + dev_err(&pdev->dev, "failed to power up MHI controller\n"); + mhi_unprepare_after_power_down(mhi_cntrl); + return; + } + + set_bit(MHI_PCI_DEV_STARTED, &mhi_pdev->status); +} + +static const struct pci_error_handlers mhi_pci_err_handler = { + .reset_prepare = mhi_pci_reset_prepare, + .reset_done = mhi_pci_reset_done, +}; + static struct pci_driver mhi_pci_driver = { .name = "mhi-pci-generic", .id_table = mhi_pci_id_table, .probe = mhi_pci_probe, - .remove = mhi_pci_remove + .remove = mhi_pci_remove, + .err_handler = &mhi_pci_err_handler, }; module_pci_driver(mhi_pci_driver);