From 8b922418b18fefe2a60e122374b3bc8096672661 Mon Sep 17 00:00:00 2001
From: Chengchang Tang <tangchengchang@huawei.com>
Date: Tue, 26 Sep 2023 19:19:10 +0800
Subject: [PATCH 17/18] libhns: Fix missing reset notification.

driver inclusion
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/I98HQV

--------------------------------------------------------------------------

Currently, userspace driver get the reset notification by reading a
a shared variable which would be set to non-zero during reset. However,
if the user does not call driver's IO interface during reset, the reset
notification will be ignored. because this variable will be clear after
completes the reset.

This patch use a new reset flag to get whether the driver has been reset
at any time. A non-zero value will be assigned to this new reset
flag by default, which will permanently become 0 once a reset occurs.
During reset, the kernel space driver will assign 0 to this variable.
After reset, this variable will be remapped to a page of all zeros. The
userspace driver can judge whether the driver has been reset by whether
this variable is 0.

Fixes: 34f2ad8085c2 ("libhns: Add reset stop flow mechanism")
Signed-off-by: Chengchang Tang <tangchengchang@huawei.com>
---
 providers/hns/hns_roce_u.c       | 4 ++++
 providers/hns/hns_roce_u.h       | 2 ++
 providers/hns/hns_roce_u_hw_v2.c | 3 +++
 3 files changed, 9 insertions(+)

diff --git a/providers/hns/hns_roce_u.c b/providers/hns/hns_roce_u.c
index 0e4f4c1..810b650 100644
--- a/providers/hns/hns_roce_u.c
+++ b/providers/hns/hns_roce_u.c
@@ -105,6 +105,7 @@ static int init_reset_context(struct hns_roce_context *ctx, int cmd_fd,
 			int page_size)
 {
 	uint64_t reset_mmap_key = resp->reset_mmap_key;
+	struct hns_roce_v2_reset_state *state;
 
 	/* The reset mmap key is 0, which means it is not supported. */
 	if (reset_mmap_key == 0)
@@ -115,6 +116,9 @@ static int init_reset_context(struct hns_roce_context *ctx, int cmd_fd,
 	if (ctx->reset_state == MAP_FAILED)
 		return -ENOMEM;
 
+	state = ctx->reset_state;
+	ctx->use_new_reset_flag = state->hw_ready;
+
 	return 0;
 }
 
diff --git a/providers/hns/hns_roce_u.h b/providers/hns/hns_roce_u.h
index 5adf6bd..024932a 100644
--- a/providers/hns/hns_roce_u.h
+++ b/providers/hns/hns_roce_u.h
@@ -203,6 +203,7 @@ struct hns_roce_spinlock {
 
 struct hns_roce_v2_reset_state {
 	uint32_t is_reset;
+	uint32_t hw_ready;
 };
 
 struct hns_roce_context {
@@ -239,6 +240,7 @@ struct hns_roce_context {
 	uint32_t			config;
 	unsigned int			max_inline_data;
 
+	bool use_new_reset_flag;
 	bool reseted;
 };
 
diff --git a/providers/hns/hns_roce_u_hw_v2.c b/providers/hns/hns_roce_u_hw_v2.c
index fe22b43..a0dce1c 100644
--- a/providers/hns/hns_roce_u_hw_v2.c
+++ b/providers/hns/hns_roce_u_hw_v2.c
@@ -891,6 +891,9 @@ static bool hns_roce_reseted(struct hns_roce_context *ctx)
 {
 	struct hns_roce_v2_reset_state *state = ctx->reset_state;
 
+	if (ctx->use_new_reset_flag)
+		return !state->hw_ready;
+
 	if (state && state->is_reset)
 		ctx->reseted = true;
 
-- 
2.33.0

