xref: /linux/drivers/misc/issei/issei_dev.h (revision 65f0ecb9ec85cb3a2e2372a6427dcc100691fc57)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (C) 2023-2026 Intel Corporation */
3 #ifndef _ISSEI_DEV_H_
4 #define _ISSEI_DEV_H_
5 
6 #include <linux/device.h>
7 #include <linux/mutex.h>
8 #include <linux/sched.h>
9 #include <linux/time64.h>
10 #include <linux/types.h>
11 #include <linux/wait.h>
12 
13 #include "dma.h"
14 
15 struct cdev;
16 struct kset;
17 
18 struct issei_device;
19 struct issei_host_client;
20 
21 extern struct class *issei_class;
22 
23 #define ISSEI_HOST_CLIENTS_MAX		255
24 
25 #define ISSEI_SUPPORTED_PROTOCOL_VER	1
26 
27 #define ISSEI_MAX_CONSEC_RESET		3
28 
29 #define ISSEI_RST_HW_READY_TIMEOUT_MSEC (2 * MSEC_PER_SEC)
30 #define ISSEI_RST_STEP_TIMEOUT_MSEC     (2 * MSEC_PER_SEC)
31 #define ISSEI_STOP_TIMEOUT_MSEC         500
32 #define ISSEI_WRITE_TIMEOUT_MSEC        (MSEC_PER_SEC)
33 
34 /**
35  * struct issei_write_buf - write buffer object
36  * @list: linked list pointer
37  * @cl: host client that requested this write
38  * @data: data to write
39  * @data_size: data size
40  */
41 struct issei_write_buf {
42 	struct list_head list;
43 	struct issei_host_client *cl;
44 	const u8 *data;
45 	size_t data_size;
46 };
47 
48 /**
49  * struct issei_hw_ops - callbacks for hardware operations
50  * @irq_clear: clear irq
51  * @irq_enable: enable irq
52  * @irq_disable: disable irq
53  * @irq_sync: sync irq
54  * @hw_reset: initiate hardware reset
55  * @hw_config: initial hardware config
56  * @hw_is_ready: check if hardware is ready
57  * @hw_reset_release: release hardware from reset
58  * @host_set_ready: set host ready indicator
59  * @setup_message_send: send setup message
60  * @setup_message_recv: receive setup message
61  * @irq_write_generate: generate interrupt on write complete
62  */
63 struct issei_hw_ops {
64 	void (*irq_clear)(struct issei_device *idev);
65 	void (*irq_enable)(struct issei_device *idev);
66 	void (*irq_disable)(struct issei_device *idev);
67 	void (*irq_sync)(struct issei_device *idev);
68 	int (*hw_reset)(struct issei_device *idev, bool enable);
69 	int (*hw_config)(struct issei_device *idev);
70 	bool (*hw_is_ready)(struct issei_device *idev);
71 	void (*hw_reset_release)(struct issei_device *idev);
72 	void (*host_set_ready)(struct issei_device *idev);
73 	int (*setup_message_send)(struct issei_device *idev);
74 	int (*setup_message_recv)(struct issei_device *idev);
75 	int (*irq_write_generate)(struct issei_device *idev);
76 };
77 
78 /**
79  * enum issei_rst_state: driver reset flow states
80  * @ISSEI_RST_STATE_INIT: initial state
81  * @ISSEI_RST_STATE_HW_READY: waiting for HW to be ready
82  * @ISSEI_RST_STATE_SETUP: waiting for channel setup completion
83  * @ISSEI_RST_STATE_START: waiting for start handshake completion
84  * @ISSEI_RST_STATE_CLIENT_ENUM: waiting for client enumeration
85  * @ISSEI_RST_STATE_DONE: reset flow is done
86  * @ISSEI_RST_STATE_DISABLED: flow is disabled
87  */
88 enum issei_rst_state {
89 	ISSEI_RST_STATE_INIT,
90 	ISSEI_RST_STATE_HW_READY,
91 	ISSEI_RST_STATE_SETUP,
92 	ISSEI_RST_STATE_START,
93 	ISSEI_RST_STATE_CLIENT_ENUM,
94 	ISSEI_RST_STATE_DONE,
95 	ISSEI_RST_STATE_DISABLED,
96 };
97 
98 /**
99  * struct issei_device - issei device
100  * @parent: parent device object
101  * @dev: associated device object
102  * @cdev: character device
103  * @minor: allocated minor number
104  * @wait_has_data: wait queue for data
105  * @has_data: there are data to process
106  * @power_down: device is powering down
107  * @wait_rst_state: waitqueue for reset state processing
108  * @rst_state: reset state
109  * @fw_protocol_ver: protocol version
110  * @fw_version: firmware version
111  * @process_thread: worker thread
112  * @reset_count: number of consecutive link reset attempts
113  * @all_reset_count: cumilative number of link reset attempts
114  * @client_lock: mutex to protect client lists and write queue
115  * @host_client_list: host clients list
116  * @host_client_last_id: last allocated host client id
117  * @host_client_count: number of active host clients
118  * @fw_client_list: firmware clients list
119  * @write_queue: write queue
120  * @last_write_ts: last write timestamp
121  * @dma: DMA memory configuration
122  * @ops: hardware operations
123  * @hw: hw-specific data
124  */
125 struct issei_device {
126 	struct device *parent;
127 	struct device dev;
128 	struct cdev *cdev;
129 	u32 minor;
130 	wait_queue_head_t wait_has_data;
131 	bool has_data;
132 	bool power_down;
133 	wait_queue_head_t wait_rst_state;
134 	enum issei_rst_state rst_state;
135 	u16 fw_protocol_ver;
136 	u16 fw_version[4];
137 	/* reset flow */
138 	struct task_struct *process_thread;
139 	u8 reset_count;
140 	u8 all_reset_count;
141 	/* clients */
142 	struct mutex client_lock;
143 	struct list_head host_client_list;
144 	u16 host_client_last_id;
145 	u8 host_client_count;
146 	struct kset *fw_clients;
147 	struct list_head fw_client_list;
148 	struct list_head write_queue;
149 	ktime_t last_write_ts;
150 	struct issei_dma dma;
151 	const struct issei_hw_ops *ops;
152 	char hw[];
153 };
154 
155 static inline void issei_poke_process_thread(struct issei_device *idev)
156 {
157 	WRITE_ONCE(idev->has_data, true);
158 	wake_up_interruptible(&idev->wait_has_data);
159 }
160 #endif /* _ISSEI_DEV_H_ */
161