[PATCH v2 08/13] iommu/mtk: Constrain return value of ->attach_dev()

From: Nicolin Chen
Date: Wed Sep 14 2022 - 01:16:20 EST


Ensure attach_dev() callback functions only return errno values expected
from the attach_dev() op. In particular, only return -EINVAL when we are
sure that the device is incompatible with the domain.

Also drop any dev_err next to -EINVAL, following the attach_dev op kdocs.

Signed-off-by: Nicolin Chen <nicolinc@xxxxxxxxxx>
---
drivers/iommu/mtk_iommu.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/mtk_iommu.c b/drivers/iommu/mtk_iommu.c
index 7e363b1f24df..78c31b307ee1 100644
--- a/drivers/iommu/mtk_iommu.c
+++ b/drivers/iommu/mtk_iommu.c
@@ -655,7 +655,7 @@ static int mtk_iommu_attach_device(struct iommu_domain *domain,

region_id = mtk_iommu_get_iova_region_id(dev, data->plat_data);
if (region_id < 0)
- return region_id;
+ return -ENODEV;

bankid = mtk_iommu_get_bank_id(dev, data->plat_data);
mutex_lock(&dom->mutex);
@@ -678,12 +678,14 @@ static int mtk_iommu_attach_device(struct iommu_domain *domain,
ret = pm_runtime_resume_and_get(m4udev);
if (ret < 0) {
dev_err(m4udev, "pm get fail(%d) in attach.\n", ret);
+ ret = -ENODEV;
goto err_unlock;
}

ret = mtk_iommu_hw_init(data, bankid);
if (ret) {
pm_runtime_put(m4udev);
+ ret = -ENODEV;
goto err_unlock;
}
bank->m4u_dom = dom;
@@ -693,7 +695,10 @@ static int mtk_iommu_attach_device(struct iommu_domain *domain,
}
mutex_unlock(&data->mutex);

- return mtk_iommu_config(data, dev, true, region_id);
+ ret = mtk_iommu_config(data, dev, true, region_id);
+ if (ret)
+ ret = -ENODEV;
+ return ret;

err_unlock:
mutex_unlock(&data->mutex);
--
2.17.1