[RFC v2 PATCH 6/8] qaic: Implement PCI link status error handlers

From: Jeffrey Hugo
Date: Tue May 19 2020 - 10:15:04 EST


Whenever the link goes down, the device loses all state. Therefore, we
need to be aware of when the link goes down and comes back up so that
our state is kept in sync with the device. Implement the PCI error
handlers to be notified of links tate changes.

Signed-off-by: Jeffrey Hugo <jhugo@xxxxxxxxxxxxxx>
---
drivers/misc/qaic/mhi_controller.c | 16 ++++++++++++++++
drivers/misc/qaic/mhi_controller.h | 4 ++++
drivers/misc/qaic/qaic_drv.c | 29 +++++++++++++++++++++++++++++
3 files changed, 49 insertions(+)

diff --git a/drivers/misc/qaic/mhi_controller.c b/drivers/misc/qaic/mhi_controller.c
index a667a2f..9ba74fc 100644
--- a/drivers/misc/qaic/mhi_controller.c
+++ b/drivers/misc/qaic/mhi_controller.c
@@ -495,3 +495,19 @@ void qaic_mhi_free_controller(struct mhi_controller *mhi_cntl, bool link_up)
kfree(mhi_cntl->irq);
kfree(mhi_cntl);
}
+
+void qaic_mhi_link_down(struct mhi_controller *mhi_cntl)
+{
+ mhi_power_down(mhi_cntl, false);
+}
+
+void qaic_mhi_link_up(struct mhi_controller *mhi_cntl)
+{
+ struct pci_dev *pci_dev = container_of(mhi_cntl->cntrl_dev,
+ struct pci_dev, dev);
+ int ret;
+
+ ret = mhi_async_power_up(mhi_cntl);
+ if (ret)
+ pci_err(pci_dev, "mhi_async_power_up failed when link came back %d\n", ret);
+}
diff --git a/drivers/misc/qaic/mhi_controller.h b/drivers/misc/qaic/mhi_controller.h
index f5197f1..6dca59d 100644
--- a/drivers/misc/qaic/mhi_controller.h
+++ b/drivers/misc/qaic/mhi_controller.h
@@ -11,4 +11,8 @@ struct mhi_controller *qaic_mhi_register_controller(struct pci_dev *pci_dev,
int mhi_irq);

void qaic_mhi_free_controller(struct mhi_controller *mhi_cntl, bool link_up);
+
+void qaic_mhi_link_down(struct mhi_controller *mhi_cntl);
+void qaic_mhi_link_up(struct mhi_controller *mhi_cntl);
+
#endif /* MHICONTROLLERQAIC_H_ */
diff --git a/drivers/misc/qaic/qaic_drv.c b/drivers/misc/qaic/qaic_drv.c
index e548881..ac4315a 100644
--- a/drivers/misc/qaic/qaic_drv.c
+++ b/drivers/misc/qaic/qaic_drv.c
@@ -484,6 +484,28 @@ static void qaic_pci_remove(struct pci_dev *pdev)
kfree(qdev);
}

+static pci_ers_result_t qaic_pci_error_detected(struct pci_dev *pdev,
+ enum pci_channel_state error)
+{
+ return PCI_ERS_RESULT_NEED_RESET;
+}
+
+static void qaic_pci_reset_prepare(struct pci_dev *pdev)
+{
+ struct qaic_device *qdev = pci_get_drvdata(pdev);
+
+ qaic_dev_reset_clean_local_state(qdev);
+ qaic_mhi_link_down(qdev->mhi_cntl);
+}
+
+static void qaic_pci_reset_done(struct pci_dev *pdev)
+{
+ struct qaic_device *qdev = pci_get_drvdata(pdev);
+
+ qdev->in_reset = false;
+ qaic_mhi_link_up(qdev->mhi_cntl);
+}
+
static const struct mhi_device_id qaic_mhi_match_table[] = {
{ .chan = "QAIC_CONTROL", },
{},
@@ -507,11 +529,18 @@ static const struct pci_device_id ids[] = {
};
MODULE_DEVICE_TABLE(pci, ids);

+static const struct pci_error_handlers qaic_pci_err_handler = {
+ .error_detected = qaic_pci_error_detected,
+ .reset_prepare = qaic_pci_reset_prepare,
+ .reset_done = qaic_pci_reset_done,
+};
+
static struct pci_driver qaic_pci_driver = {
.name = KBUILD_MODNAME,
.id_table = ids,
.probe = qaic_pci_probe,
.remove = qaic_pci_remove,
+ .err_handler = &qaic_pci_err_handler,
};

static int __init qaic_init(void)
--
Qualcomm Technologies, Inc. is a member of the
Code Aurora Forum, a Linux Foundation Collaborative Project.