diff mbox series

[for-next,7/8] RDMA/hns: Refactor the alloc_srqc()

Message ID 20220218110519.37375-8-liangwenpeng@huawei.com (mailing list archive)
State Superseded
Headers show
Series RDMA/hns: Clean up and refactor mailbox-related code | expand

Commit Message

Wenpeng Liang Feb. 18, 2022, 11:05 a.m. UTC
From: Chengchang Tang <tangchengchang@huawei.com>

Abstract the alloc_srqc() into several parts and separate the alloc_srqn()
from the alloc_srqc().

Signed-off-by: Chengchang Tang <tangchengchang@huawei.com>
Signed-off-by: Wenpeng Liang <liangwenpeng@huawei.com>
---
 drivers/infiniband/hw/hns/hns_roce_srq.c | 80 +++++++++++++++---------
 1 file changed, 52 insertions(+), 28 deletions(-)

Comments

Leon Romanovsky Feb. 24, 2022, 1:35 p.m. UTC | #1
On Fri, Feb 18, 2022 at 07:05:18PM +0800, Wenpeng Liang wrote:
> From: Chengchang Tang <tangchengchang@huawei.com>
> 
> Abstract the alloc_srqc() into several parts and separate the alloc_srqn()
> from the alloc_srqc().
> 
> Signed-off-by: Chengchang Tang <tangchengchang@huawei.com>
> Signed-off-by: Wenpeng Liang <liangwenpeng@huawei.com>
> ---
>  drivers/infiniband/hw/hns/hns_roce_srq.c | 80 +++++++++++++++---------
>  1 file changed, 52 insertions(+), 28 deletions(-)
> 
> diff --git a/drivers/infiniband/hw/hns/hns_roce_srq.c b/drivers/infiniband/hw/hns/hns_roce_srq.c
> index e316276e18c2..2613889a02ef 100644
> --- a/drivers/infiniband/hw/hns/hns_roce_srq.c
> +++ b/drivers/infiniband/hw/hns/hns_roce_srq.c
> @@ -59,40 +59,39 @@ static void hns_roce_ib_srq_event(struct hns_roce_srq *srq,
>  	}
>  }
>  
> -static int alloc_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
> +static int alloc_srqn(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
>  {
> -	struct hns_roce_srq_table *srq_table = &hr_dev->srq_table;
>  	struct hns_roce_ida *srq_ida = &hr_dev->srq_table.srq_ida;
> -	struct ib_device *ibdev = &hr_dev->ib_dev;
> -	struct hns_roce_cmd_mailbox *mailbox;
> -	int ret;
>  	int id;
>  
>  	id = ida_alloc_range(&srq_ida->ida, srq_ida->min, srq_ida->max,
>  			     GFP_KERNEL);
>  	if (id < 0) {
> -		ibdev_err(ibdev, "failed to alloc srq(%d).\n", id);
> +		ibdev_err(&hr_dev->ib_dev, "failed to alloc srq(%d).\n", id);
>  		return -ENOMEM;
>  	}
> -	srq->srqn = (unsigned long)id;
>  
> -	ret = hns_roce_table_get(hr_dev, &srq_table->table, srq->srqn);
> -	if (ret) {
> -		ibdev_err(ibdev, "failed to get SRQC table, ret = %d.\n", ret);
> -		goto err_out;
> -	}
> +	srq->srqn = id;
>  
> -	ret = xa_err(xa_store(&srq_table->xa, srq->srqn, srq, GFP_KERNEL));
> -	if (ret) {
> -		ibdev_err(ibdev, "failed to store SRQC, ret = %d.\n", ret);
> -		goto err_put;
> -	}
> +	return 0;
> +}
> +
> +static void free_srqn(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
> +{
> +	ida_free(&hr_dev->srq_table.srq_ida.ida, (int)srq->srqn);
> +}
> +
> +static int hns_roce_create_srqc(struct hns_roce_dev *hr_dev,
> +				struct hns_roce_srq *srq)
> +{
> +	struct ib_device *ibdev = &hr_dev->ib_dev;
> +	struct hns_roce_cmd_mailbox *mailbox;
> +	int ret;
>  
>  	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
>  	if (IS_ERR_OR_NULL(mailbox)) {

hns_roce_alloc_cmd_mailbox() never returns NULL, so the check should be IS_ERR()

Thanks

>  		ibdev_err(ibdev, "failed to alloc mailbox for SRQC.\n");
> -		ret = -ENOMEM;
> -		goto err_xa;
> +		return PTR_ERR(mailbox);
>  	}
>  
>  	ret = hr_dev->hw->write_srqc(srq, mailbox->buf);
> @@ -103,23 +102,42 @@ static int alloc_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
>  
>  	ret = hns_roce_create_hw_ctx(hr_dev, mailbox, HNS_ROCE_CMD_CREATE_SRQ,
>  				     srq->srqn);
> -	if (ret) {
> +	if (ret)
>  		ibdev_err(ibdev, "failed to config SRQC, ret = %d.\n", ret);
> -		goto err_mbox;
> -	}
>  
> +err_mbox:
>  	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
> +	return ret;
> +}
> +
> +static int alloc_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
> +{
> +	struct hns_roce_srq_table *srq_table = &hr_dev->srq_table;
> +	struct ib_device *ibdev = &hr_dev->ib_dev;
> +	int ret;
> +
> +	ret = hns_roce_table_get(hr_dev, &srq_table->table, srq->srqn);
> +	if (ret) {
> +		ibdev_err(ibdev, "failed to get SRQC table, ret = %d.\n", ret);
> +		return ret;
> +	}
> +
> +	ret = xa_err(xa_store(&srq_table->xa, srq->srqn, srq, GFP_KERNEL));
> +	if (ret) {
> +		ibdev_err(ibdev, "failed to store SRQC, ret = %d.\n", ret);
> +		goto err_put;
> +	}
> +
> +	ret = hns_roce_create_srqc(hr_dev, srq);
> +	if (ret)
> +		goto err_xa;
>  
>  	return 0;
>  
> -err_mbox:
> -	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
>  err_xa:
>  	xa_erase(&srq_table->xa, srq->srqn);
>  err_put:
>  	hns_roce_table_put(hr_dev, &srq_table->table, srq->srqn);
> -err_out:
> -	ida_free(&srq_ida->ida, id);
>  
>  	return ret;
>  }
> @@ -142,7 +160,6 @@ static void free_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
>  	wait_for_completion(&srq->free);
>  
>  	hns_roce_table_put(hr_dev, &srq_table->table, srq->srqn);
> -	ida_free(&srq_table->srq_ida.ida, (int)srq->srqn);
>  }
>  
>  static int alloc_srq_idx(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq,
> @@ -390,10 +407,14 @@ int hns_roce_create_srq(struct ib_srq *ib_srq,
>  	if (ret)
>  		return ret;
>  
> -	ret = alloc_srqc(hr_dev, srq);
> +	ret = alloc_srqn(hr_dev, srq);
>  	if (ret)
>  		goto err_srq_buf;
>  
> +	ret = alloc_srqc(hr_dev, srq);
> +	if (ret)
> +		goto err_srqn;
> +
>  	if (udata) {
>  		resp.srqn = srq->srqn;
>  		if (ib_copy_to_udata(udata, &resp,
> @@ -412,6 +433,8 @@ int hns_roce_create_srq(struct ib_srq *ib_srq,
>  
>  err_srqc:
>  	free_srqc(hr_dev, srq);
> +err_srqn:
> +	free_srqn(hr_dev, srq);
>  err_srq_buf:
>  	free_srq_buf(hr_dev, srq);
>  
> @@ -424,6 +447,7 @@ int hns_roce_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
>  	struct hns_roce_srq *srq = to_hr_srq(ibsrq);
>  
>  	free_srqc(hr_dev, srq);
> +	free_srqn(hr_dev, srq);
>  	free_srq_buf(hr_dev, srq);
>  	return 0;
>  }
> -- 
> 2.33.0
>
Wenpeng Liang Feb. 25, 2022, 10:53 a.m. UTC | #2
On 2022/2/24 21:35, Leon Romanovsky wrote:
>> +static int hns_roce_create_srqc(struct hns_roce_dev *hr_dev,
>> +				struct hns_roce_srq *srq)
>> +{
>> +	struct ib_device *ibdev = &hr_dev->ib_dev;
>> +	struct hns_roce_cmd_mailbox *mailbox;
>> +	int ret;
>>  
>>  	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
>>  	if (IS_ERR_OR_NULL(mailbox)) {
> hns_roce_alloc_cmd_mailbox() never returns NULL, so the check should be IS_ERR()
> 
> Thanks
> 

There are a few more hns_roce_alloc_cmd_mailbox() return values to deal with.
I will add a new patch in v2.

Thanks,
Wenpeng

>>  		ibdev_err(ibdev, "failed to alloc mailbox for SRQC.\n");
>> -		ret = -ENOMEM;
>> -		goto err_xa;
>> +		return PTR_ERR(mailbox);
>>  	}
diff mbox series

Patch

diff --git a/drivers/infiniband/hw/hns/hns_roce_srq.c b/drivers/infiniband/hw/hns/hns_roce_srq.c
index e316276e18c2..2613889a02ef 100644
--- a/drivers/infiniband/hw/hns/hns_roce_srq.c
+++ b/drivers/infiniband/hw/hns/hns_roce_srq.c
@@ -59,40 +59,39 @@  static void hns_roce_ib_srq_event(struct hns_roce_srq *srq,
 	}
 }
 
-static int alloc_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
+static int alloc_srqn(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
 {
-	struct hns_roce_srq_table *srq_table = &hr_dev->srq_table;
 	struct hns_roce_ida *srq_ida = &hr_dev->srq_table.srq_ida;
-	struct ib_device *ibdev = &hr_dev->ib_dev;
-	struct hns_roce_cmd_mailbox *mailbox;
-	int ret;
 	int id;
 
 	id = ida_alloc_range(&srq_ida->ida, srq_ida->min, srq_ida->max,
 			     GFP_KERNEL);
 	if (id < 0) {
-		ibdev_err(ibdev, "failed to alloc srq(%d).\n", id);
+		ibdev_err(&hr_dev->ib_dev, "failed to alloc srq(%d).\n", id);
 		return -ENOMEM;
 	}
-	srq->srqn = (unsigned long)id;
 
-	ret = hns_roce_table_get(hr_dev, &srq_table->table, srq->srqn);
-	if (ret) {
-		ibdev_err(ibdev, "failed to get SRQC table, ret = %d.\n", ret);
-		goto err_out;
-	}
+	srq->srqn = id;
 
-	ret = xa_err(xa_store(&srq_table->xa, srq->srqn, srq, GFP_KERNEL));
-	if (ret) {
-		ibdev_err(ibdev, "failed to store SRQC, ret = %d.\n", ret);
-		goto err_put;
-	}
+	return 0;
+}
+
+static void free_srqn(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
+{
+	ida_free(&hr_dev->srq_table.srq_ida.ida, (int)srq->srqn);
+}
+
+static int hns_roce_create_srqc(struct hns_roce_dev *hr_dev,
+				struct hns_roce_srq *srq)
+{
+	struct ib_device *ibdev = &hr_dev->ib_dev;
+	struct hns_roce_cmd_mailbox *mailbox;
+	int ret;
 
 	mailbox = hns_roce_alloc_cmd_mailbox(hr_dev);
 	if (IS_ERR_OR_NULL(mailbox)) {
 		ibdev_err(ibdev, "failed to alloc mailbox for SRQC.\n");
-		ret = -ENOMEM;
-		goto err_xa;
+		return PTR_ERR(mailbox);
 	}
 
 	ret = hr_dev->hw->write_srqc(srq, mailbox->buf);
@@ -103,23 +102,42 @@  static int alloc_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
 
 	ret = hns_roce_create_hw_ctx(hr_dev, mailbox, HNS_ROCE_CMD_CREATE_SRQ,
 				     srq->srqn);
-	if (ret) {
+	if (ret)
 		ibdev_err(ibdev, "failed to config SRQC, ret = %d.\n", ret);
-		goto err_mbox;
-	}
 
+err_mbox:
 	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
+	return ret;
+}
+
+static int alloc_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
+{
+	struct hns_roce_srq_table *srq_table = &hr_dev->srq_table;
+	struct ib_device *ibdev = &hr_dev->ib_dev;
+	int ret;
+
+	ret = hns_roce_table_get(hr_dev, &srq_table->table, srq->srqn);
+	if (ret) {
+		ibdev_err(ibdev, "failed to get SRQC table, ret = %d.\n", ret);
+		return ret;
+	}
+
+	ret = xa_err(xa_store(&srq_table->xa, srq->srqn, srq, GFP_KERNEL));
+	if (ret) {
+		ibdev_err(ibdev, "failed to store SRQC, ret = %d.\n", ret);
+		goto err_put;
+	}
+
+	ret = hns_roce_create_srqc(hr_dev, srq);
+	if (ret)
+		goto err_xa;
 
 	return 0;
 
-err_mbox:
-	hns_roce_free_cmd_mailbox(hr_dev, mailbox);
 err_xa:
 	xa_erase(&srq_table->xa, srq->srqn);
 err_put:
 	hns_roce_table_put(hr_dev, &srq_table->table, srq->srqn);
-err_out:
-	ida_free(&srq_ida->ida, id);
 
 	return ret;
 }
@@ -142,7 +160,6 @@  static void free_srqc(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq)
 	wait_for_completion(&srq->free);
 
 	hns_roce_table_put(hr_dev, &srq_table->table, srq->srqn);
-	ida_free(&srq_table->srq_ida.ida, (int)srq->srqn);
 }
 
 static int alloc_srq_idx(struct hns_roce_dev *hr_dev, struct hns_roce_srq *srq,
@@ -390,10 +407,14 @@  int hns_roce_create_srq(struct ib_srq *ib_srq,
 	if (ret)
 		return ret;
 
-	ret = alloc_srqc(hr_dev, srq);
+	ret = alloc_srqn(hr_dev, srq);
 	if (ret)
 		goto err_srq_buf;
 
+	ret = alloc_srqc(hr_dev, srq);
+	if (ret)
+		goto err_srqn;
+
 	if (udata) {
 		resp.srqn = srq->srqn;
 		if (ib_copy_to_udata(udata, &resp,
@@ -412,6 +433,8 @@  int hns_roce_create_srq(struct ib_srq *ib_srq,
 
 err_srqc:
 	free_srqc(hr_dev, srq);
+err_srqn:
+	free_srqn(hr_dev, srq);
 err_srq_buf:
 	free_srq_buf(hr_dev, srq);
 
@@ -424,6 +447,7 @@  int hns_roce_destroy_srq(struct ib_srq *ibsrq, struct ib_udata *udata)
 	struct hns_roce_srq *srq = to_hr_srq(ibsrq);
 
 	free_srqc(hr_dev, srq);
+	free_srqn(hr_dev, srq);
 	free_srq_buf(hr_dev, srq);
 	return 0;
 }