summaryrefslogtreecommitdiff
path: root/drivers/misc/issei/main.c
blob: 5987fb340250b8c30af1772dba4e9e9873a830c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// SPDX-License-Identifier: GPL-2.0
/* Copyright (C) 2023-2026 Intel Corporation */
#include <linux/atomic.h>
#include <linux/device.h>
#include <linux/dev_printk.h>
#include <linux/err.h>
#include <linux/export.h>
#include <linux/jiffies.h>
#include <linux/kthread.h>
#include <linux/slab.h>
#include <linux/wait.h>

#include "cdev.h"
#include "fw_client.h"
#include "host_client.h"
#include "ham.h"
#include "issei_dev.h"

static void issei_rst_state_set(struct issei_device *idev, enum issei_rst_state state)
{
	idev->rst_state = state;
	/* wake up the thread */
	if (waitqueue_active(&idev->wait_rst_state))
		wake_up(&idev->wait_rst_state);
}

static int issei_reset(struct issei_device *idev)
{
	int ret;

	idev->ops->irq_clear(idev);

	issei_cl_all_disconnect(idev);
	issei_fw_cl_remove_all(idev);
	/* No need to check for overflow here, the counter is used only for info */
	idev->all_reset_count++;
	ret = idev->ops->hw_reset(idev, !idev->power_down);
	issei_dmam_setup(idev);
	if (ret) {
		dev_err(&idev->dev, "hw_reset failed ret = %d\n", ret);
		return ret;
	}

	if (idev->power_down) {
		dev_dbg(&idev->dev, "powering down: end of reset\n");
		issei_rst_state_set(idev, ISSEI_RST_STATE_DISABLED);
		return -ENODEV;
	}
	return 0;
}

static int issei_process_read_msg(struct issei_device *idev)
{
	struct issei_dma_data data = {};
	int ret;

	ret = issei_dma_read(idev, &data);
	if (ret)
		return ret;

	dev_dbg(&idev->dev, "Processing response %u %u %u %u\n", data.fw_id, data.host_id,
		data.status, data.length);
	if (data.status != HAMS_SUCCESS) {
		dev_err(&idev->dev, "Command failed with status 0x%02X", data.status);
		kfree(data.buf);
		ret = -EIO;
	} else {
		if (issei_is_ham_rsp(data.fw_id, data.host_id))
			ret = issei_ham_process_ham_rsp(idev, data.buf, data.length);
		else
			ret = issei_cl_read_buf(idev, data.fw_id, data.host_id,
						data.buf, data.length);
	}
	idev->ops->irq_write_generate(idev);
	return ret;
}

static int issei_process_write_msg(struct issei_device *idev)
{
	if (idev->rst_state != ISSEI_RST_STATE_DONE)
		return 0;

	return issei_cl_write_from_queue(idev);
}

static int issei_process_thread(void *_dev)
{
	long timeout, old_timeout = MAX_SCHEDULE_TIMEOUT;
	struct issei_device *idev = _dev;
	int ret;

	while (!kthread_should_stop()) {
		dev_dbg(&idev->dev, "process_work in %d\n", idev->rst_state);
		if (!idev->ops->hw_is_ready(idev) && idev->rst_state > ISSEI_RST_STATE_HW_READY) {
			if (!idev->power_down)
				dev_dbg(&idev->dev, "HW not ready, resetting\n");
			idev->rst_state = ISSEI_RST_STATE_INIT;
		}
		if (idev->power_down)
			idev->rst_state = ISSEI_RST_STATE_INIT;
		WRITE_ONCE(idev->has_data, false);
		dev_dbg(&idev->dev, "reset_step in %d\n", idev->rst_state);
		timeout = MAX_SCHEDULE_TIMEOUT;
		ret = 0;
		switch (idev->rst_state) {
		case ISSEI_RST_STATE_DISABLED:
			if (idev->power_down) {
				dev_dbg(&idev->dev, "Interrupt in power down?\n");
				break;
			}
			idev->rst_state = ISSEI_RST_STATE_INIT;
			fallthrough;

		case ISSEI_RST_STATE_INIT:
			idev->ops->irq_clear(idev);
			idev->ops->irq_sync(idev);

			if (!idev->power_down) {
				idev->reset_count++;
				if (idev->reset_count > ISSEI_MAX_CONSEC_RESET) {
					dev_err(&idev->dev, "reset: reached maximal consecutive resets: disabling the device\n");
					issei_rst_state_set(idev, ISSEI_RST_STATE_DISABLED);
					break;
				}
			}

			ret = issei_reset(idev);
			if (idev->power_down) {
				dev_dbg(&idev->dev, "Powering down\n");
				return 0;
			}
			if (ret)
				break;

			idev->rst_state = ISSEI_RST_STATE_HW_READY;
			timeout = msecs_to_jiffies(ISSEI_RST_HW_READY_TIMEOUT_MSEC);
			break;

		case ISSEI_RST_STATE_HW_READY:
			if (!idev->ops->hw_is_ready(idev)) {
				dev_dbg(&idev->dev, "HW is not ready?\n");
				timeout = old_timeout;
				break;
			}

			dev_dbg(&idev->dev, "HW is ready\n");
			idev->ops->hw_reset_release(idev);
			idev->ops->host_set_ready(idev);
			ret = idev->ops->setup_message_send(idev);
			if (ret)
				break;

			idev->rst_state = ISSEI_RST_STATE_SETUP;
			timeout = msecs_to_jiffies(ISSEI_RST_STEP_TIMEOUT_MSEC);
			break;

		case ISSEI_RST_STATE_SETUP:
			ret = idev->ops->setup_message_recv(idev);
			if (ret) {
				if (ret == -ENODATA) {
					ret = 0;
					timeout = old_timeout;
				}
			} else {
				timeout = msecs_to_jiffies(ISSEI_RST_STEP_TIMEOUT_MSEC);
				ret = issei_ham_send_start_req(idev);
				idev->rst_state = ISSEI_RST_STATE_START;
			}
			break;

		case ISSEI_RST_STATE_START:
			ret = issei_process_read_msg(idev);
			if (!ret) {
				timeout = msecs_to_jiffies(ISSEI_RST_STEP_TIMEOUT_MSEC);
				idev->rst_state = ISSEI_RST_STATE_CLIENT_ENUM;
			} else if (ret == -ENODATA) {
				ret = 0;
				timeout = old_timeout;
			}
			break;

		case ISSEI_RST_STATE_CLIENT_ENUM:
			ret = issei_process_read_msg(idev);
			if (ret) {
				if (ret == -ENODATA) {
					ret = 0;
					timeout = old_timeout;
				}
			} else {
				idev->reset_count = 0;
				idev->rst_state = ISSEI_RST_STATE_DONE;
				dev_dbg(&idev->dev, "Reset finished successfully\n");
			}
			break;

		case ISSEI_RST_STATE_DONE:
			ret = issei_process_read_msg(idev);
			if (ret != 0 && ret != -ENODATA)
				break;

			ret = issei_process_write_msg(idev);
			break;
		}

		if (ret) {
			dev_warn(&idev->dev, "Process failed ret = %d\n", ret);
			idev->rst_state = ISSEI_RST_STATE_INIT;
			continue;
		}

		/*
		 * Every thread that has data to process sets the 'has_data' flag and
		 * triggers the wait queue.
		 * The processing thread, in each loop iteration, resets 'has_data'
		 * and processes all available data.
		 *
		 * After processing, the thread waits for 'has_data' to be set again.
		 *
		 * If the wait function times out but 'has_data' becomes 1 before
		 * the subsequent atomic read check, this is acceptable from a flow
		 * perspective - the thread will continue processing the data.
		 *
		 * The 'has_data' flag cannot become 0 between the wait function and
		 * the atomic read check, since only this thread is allowed to reset it to 0.
		 */

		old_timeout = wait_event_interruptible_timeout(idev->wait_has_data,
							       READ_ONCE(idev->has_data),
							       timeout);
		if (idev->rst_state == ISSEI_RST_STATE_DISABLED)
			continue;

		if (!READ_ONCE(idev->has_data)) {
			dev_warn(&idev->dev, "Timed out at state %d, resetting\n",
				 idev->rst_state);
			idev->rst_state = ISSEI_RST_STATE_INIT;
		}
	}

	return 0;
}

/**
 * issei_start - configure HW device and start processing thread.
 * @idev: the device structure
 *
 * Return: 0 on success, < 0 on failure
 */
int issei_start(struct issei_device *idev)
{
	int ret;

	idev->power_down = false;

	ret = issei_dmam_setup(idev);
	if (ret)
		return ret;

	idev->ops->irq_clear(idev);

	ret = idev->ops->hw_config(idev);
	if (ret)
		return ret;

	idev->process_thread = kthread_run(issei_process_thread, idev,
					   "kisseiprocess/%s", dev_name(&idev->dev));
	if (IS_ERR(idev->process_thread)) {
		ret = PTR_ERR(idev->process_thread);
		dev_err(&idev->dev, "unable to create process thread. ret = %d\n", ret);
		return ret;
	}

	issei_poke_process_thread(idev);
	return 0;
}
EXPORT_SYMBOL_GPL(issei_start);

/**
 * issei_stop - stop interrupts and processing thread.
 * @idev: the device structure
 */
void issei_stop(struct issei_device *idev)
{
	idev->power_down = true;

	idev->ops->irq_clear(idev);
	idev->ops->irq_sync(idev);

	issei_poke_process_thread(idev);

	wait_event_timeout(idev->wait_rst_state,
			   (idev->rst_state == ISSEI_RST_STATE_DISABLED),
			   msecs_to_jiffies(ISSEI_STOP_TIMEOUT_MSEC));
	kthread_stop(idev->process_thread);
}
EXPORT_SYMBOL_GPL(issei_stop);