xref: /linux/drivers/net/wireless/intel/iwlwifi/iwl-op-mode.h (revision fb7399cf2d0b33825b8039f95c45395c7deba25c)
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /*
3  * Copyright (C) 2005-2014, 2018-2021, 2024-2025 Intel Corporation
4  * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5  * Copyright (C) 2015 Intel Deutschland GmbH
6  */
7 #ifndef __iwl_op_mode_h__
8 #define __iwl_op_mode_h__
9 
10 #include <linux/netdevice.h>
11 #include <linux/debugfs.h>
12 #include "iwl-dbg-tlv.h"
13 
14 struct iwl_op_mode;
15 struct iwl_trans;
16 struct sk_buff;
17 struct iwl_device_cmd;
18 struct iwl_rx_cmd_buffer;
19 struct iwl_fw;
20 struct iwl_rf_cfg;
21 
22 /**
23  * DOC: Operational mode - what is it ?
24  *
25  * The operational mode (a.k.a. op_mode) is the layer that implements
26  * mac80211's handlers. It knows two APIs: mac80211's and the fw's. It uses
27  * the transport API to access the HW. The op_mode doesn't need to know how the
28  * underlying HW works, since the transport layer takes care of that.
29  *
30  * There can be several op_mode: i.e. different fw APIs will require two
31  * different op_modes. This is why the op_mode is virtualized.
32  */
33 
34 /**
35  * DOC: Life cycle of the Operational mode
36  *
37  * The operational mode has a very simple life cycle.
38  *
39  *	1) The driver layer (iwl-drv.c) chooses the op_mode based on the
40  *	   capabilities advertised by the fw file (in TLV format).
41  *	2) The driver layer starts the op_mode (ops->start)
42  *	3) The op_mode registers mac80211
43  *	4) The op_mode is governed by mac80211
44  *	5) The driver layer stops the op_mode
45  */
46 
47 /**
48  * enum iwl_fw_error_type - FW error types/sources
49  * @IWL_ERR_TYPE_IRQ: "normal" FW error through an IRQ
50  * @IWL_ERR_TYPE_NMI_FORCED: NMI was forced by driver
51  * @IWL_ERR_TYPE_RESET_HS_TIMEOUT: reset handshake timed out,
52  *	any debug collection must happen synchronously as
53  *	the device will be shut down
54  * @IWL_ERR_TYPE_CMD_QUEUE_FULL: command queue was full
55  * @IWL_ERR_TYPE_TOP_RESET_BY_BT: TOP reset initiated by BT
56  * @IWL_ERR_TYPE_TOP_FATAL_ERROR: TOP fatal error
57  * @IWL_ERR_TYPE_TOP_RESET_FAILED: TOP reset failed
58  * @IWL_ERR_TYPE_DEBUGFS: error/reset indication from debugfs
59  */
60 enum iwl_fw_error_type {
61 	IWL_ERR_TYPE_IRQ,
62 	IWL_ERR_TYPE_NMI_FORCED,
63 	IWL_ERR_TYPE_RESET_HS_TIMEOUT,
64 	IWL_ERR_TYPE_CMD_QUEUE_FULL,
65 	IWL_ERR_TYPE_TOP_RESET_BY_BT,
66 	IWL_ERR_TYPE_TOP_FATAL_ERROR,
67 	IWL_ERR_TYPE_TOP_RESET_FAILED,
68 	IWL_ERR_TYPE_DEBUGFS,
69 };
70 
71 /**
72  * enum iwl_fw_error_context - error dump context
73  * @IWL_ERR_CONTEXT_WORKER: regular from worker context,
74  *	opmode must acquire locks and must also check
75  *	for @IWL_ERR_CONTEXT_ABORT after acquiring locks
76  * @IWL_ERR_CONTEXT_FROM_OPMODE: context is in a call
77  *	originating from the opmode, e.g. while resetting
78  *	or stopping the device, so opmode must not acquire
79  *	any locks
80  * @IWL_ERR_CONTEXT_ABORT: after lock acquisition, indicates
81  *	that the dump already happened via another callback
82  *	(currently only while stopping the device) via the
83  *	@IWL_ERR_CONTEXT_FROM_OPMODE context, and this call
84  *	must be aborted
85  */
86 enum iwl_fw_error_context {
87 	IWL_ERR_CONTEXT_WORKER,
88 	IWL_ERR_CONTEXT_FROM_OPMODE,
89 	IWL_ERR_CONTEXT_ABORT,
90 };
91 
92 /**
93  * struct iwl_fw_error_dump_mode - error dump mode for callback
94  * @type: The reason for the dump, per &enum iwl_fw_error_type.
95  * @context: The context for the dump, may also indicate this
96  *	call needs to be skipped. This MUST be checked before
97  *	and after acquiring any locks in the op-mode!
98  */
99 struct iwl_fw_error_dump_mode {
100 	enum iwl_fw_error_type type;
101 	enum iwl_fw_error_context context;
102 };
103 
104 /**
105  * struct iwl_op_mode_ops - op_mode specific operations
106  *
107  * The op_mode exports its ops so that external components can start it and
108  * interact with it. The driver layer typically calls the start and stop
109  * handlers, the transport layer calls the others.
110  *
111  * All the handlers MUST be implemented, except @rx_rss which can be left
112  * out *iff* the opmode will never run on hardware with multi-queue capability.
113  *
114  * @start: start the op_mode. The transport layer is already allocated.
115  *	May sleep
116  * @stop: stop the op_mode. Must free all the memory allocated.
117  *	May sleep
118  * @rx: Rx notification to the op_mode. rxb is the Rx buffer itself. Cmd is the
119  *	HCMD this Rx responds to. Can't sleep.
120  * @rx_rss: data queue RX notification to the op_mode, for (data) notifications
121  *	received on the RSS queue(s). The queue parameter indicates which of the
122  *	RSS queues received this frame; it will always be non-zero.
123  *	This method must not sleep.
124  * @queue_full: notifies that a HW queue is full.
125  *	Must be atomic and called with BH disabled.
126  * @queue_not_full: notifies that a HW queue is not full any more.
127  *	Must be atomic and called with BH disabled.
128  * @hw_rf_kill: notifies of a change in the HW rf kill switch. True means that
129  *	the radio is killed. Return %true if the device should be stopped by
130  *	the transport immediately after the call. May sleep.
131  *	Note that this must not return %true for newer devices using gen2 PCIe
132  *	transport.
133  * @free_skb: allows the transport layer to free skbs that haven't been
134  *	reclaimed by the op_mode. This can happen when the driver is freed and
135  *	there are Tx packets pending in the transport layer.
136  *	Must be atomic
137  * @nic_error: error notification. Must be atomic, the op mode should handle
138  *	the error (e.g. abort notification waiters) and print the error if
139  *	applicable
140  * @dump_error: NIC error dump collection (can sleep, synchronous)
141  * @sw_reset: (maybe) initiate a software reset, return %true if started
142  * @nic_config: configure NIC, called before firmware is started.
143  *	May sleep
144  * @wimax_active: invoked when WiMax becomes active. May sleep
145  * @time_point: called when transport layer wants to collect debug data
146  * @device_powered_off: called upon resume from hibernation but not only.
147  *	Op_mode needs to reset its internal state because the device did not
148  *	survive the system state transition. The firmware is no longer running,
149  *	etc...
150  */
151 struct iwl_op_mode_ops {
152 	struct iwl_op_mode *(*start)(struct iwl_trans *trans,
153 				     const struct iwl_rf_cfg *cfg,
154 				     const struct iwl_fw *fw,
155 				     struct dentry *dbgfs_dir);
156 	void (*stop)(struct iwl_op_mode *op_mode);
157 	void (*rx)(struct iwl_op_mode *op_mode, struct napi_struct *napi,
158 		   struct iwl_rx_cmd_buffer *rxb);
159 	void (*rx_rss)(struct iwl_op_mode *op_mode, struct napi_struct *napi,
160 		       struct iwl_rx_cmd_buffer *rxb, unsigned int queue);
161 	void (*queue_full)(struct iwl_op_mode *op_mode, int queue);
162 	void (*queue_not_full)(struct iwl_op_mode *op_mode, int queue);
163 	bool (*hw_rf_kill)(struct iwl_op_mode *op_mode, bool state);
164 	void (*free_skb)(struct iwl_op_mode *op_mode, struct sk_buff *skb);
165 	void (*nic_error)(struct iwl_op_mode *op_mode,
166 			  enum iwl_fw_error_type type);
167 	void (*dump_error)(struct iwl_op_mode *op_mode,
168 			   struct iwl_fw_error_dump_mode *mode);
169 	bool (*sw_reset)(struct iwl_op_mode *op_mode,
170 			 enum iwl_fw_error_type type);
171 	void (*nic_config)(struct iwl_op_mode *op_mode);
172 	void (*wimax_active)(struct iwl_op_mode *op_mode);
173 	void (*time_point)(struct iwl_op_mode *op_mode,
174 			   enum iwl_fw_ini_time_point tp_id,
175 			   union iwl_dbg_tlv_tp_data *tp_data);
176 	void (*device_powered_off)(struct iwl_op_mode *op_mode);
177 };
178 
179 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops);
180 void iwl_opmode_deregister(const char *name);
181 
182 /**
183  * struct iwl_op_mode - operational mode
184  * @ops: pointer to its own ops
185  *
186  * This holds an implementation of the mac80211 / fw API.
187  */
188 struct iwl_op_mode {
189 	const struct iwl_op_mode_ops *ops;
190 
191 	char op_mode_specific[] __aligned(sizeof(void *));
192 };
193 
194 static inline void iwl_op_mode_stop(struct iwl_op_mode *op_mode)
195 {
196 	might_sleep();
197 	op_mode->ops->stop(op_mode);
198 }
199 
200 static inline void iwl_op_mode_rx(struct iwl_op_mode *op_mode,
201 				  struct napi_struct *napi,
202 				  struct iwl_rx_cmd_buffer *rxb)
203 {
204 	return op_mode->ops->rx(op_mode, napi, rxb);
205 }
206 
207 static inline void iwl_op_mode_rx_rss(struct iwl_op_mode *op_mode,
208 				      struct napi_struct *napi,
209 				      struct iwl_rx_cmd_buffer *rxb,
210 				      unsigned int queue)
211 {
212 	op_mode->ops->rx_rss(op_mode, napi, rxb, queue);
213 }
214 
215 static inline void iwl_op_mode_queue_full(struct iwl_op_mode *op_mode,
216 					  int queue)
217 {
218 	op_mode->ops->queue_full(op_mode, queue);
219 }
220 
221 static inline void iwl_op_mode_queue_not_full(struct iwl_op_mode *op_mode,
222 					      int queue)
223 {
224 	op_mode->ops->queue_not_full(op_mode, queue);
225 }
226 
227 static inline bool __must_check
228 iwl_op_mode_hw_rf_kill(struct iwl_op_mode *op_mode, bool state)
229 {
230 	might_sleep();
231 	return op_mode->ops->hw_rf_kill(op_mode, state);
232 }
233 
234 static inline void iwl_op_mode_free_skb(struct iwl_op_mode *op_mode,
235 					struct sk_buff *skb)
236 {
237 	if (WARN_ON_ONCE(!op_mode))
238 		return;
239 	op_mode->ops->free_skb(op_mode, skb);
240 }
241 
242 static inline void iwl_op_mode_nic_error(struct iwl_op_mode *op_mode,
243 					 enum iwl_fw_error_type type)
244 {
245 	op_mode->ops->nic_error(op_mode, type);
246 }
247 
248 static inline void iwl_op_mode_dump_error(struct iwl_op_mode *op_mode,
249 					  struct iwl_fw_error_dump_mode *mode)
250 {
251 	might_sleep();
252 
253 	if (WARN_ON(mode->type == IWL_ERR_TYPE_TOP_RESET_BY_BT))
254 		return;
255 
256 	if (op_mode->ops->dump_error)
257 		op_mode->ops->dump_error(op_mode, mode);
258 }
259 
260 static inline void iwl_op_mode_nic_config(struct iwl_op_mode *op_mode)
261 {
262 	might_sleep();
263 	if (op_mode->ops->nic_config)
264 		op_mode->ops->nic_config(op_mode);
265 }
266 
267 static inline void iwl_op_mode_wimax_active(struct iwl_op_mode *op_mode)
268 {
269 	might_sleep();
270 	op_mode->ops->wimax_active(op_mode);
271 }
272 
273 static inline void iwl_op_mode_time_point(struct iwl_op_mode *op_mode,
274 					  enum iwl_fw_ini_time_point tp_id,
275 					  union iwl_dbg_tlv_tp_data *tp_data)
276 {
277 	if (!op_mode || !op_mode->ops || !op_mode->ops->time_point)
278 		return;
279 	op_mode->ops->time_point(op_mode, tp_id, tp_data);
280 }
281 
282 static inline void iwl_op_mode_device_powered_off(struct iwl_op_mode *op_mode)
283 {
284 	if (!op_mode || !op_mode->ops || !op_mode->ops->device_powered_off)
285 		return;
286 	op_mode->ops->device_powered_off(op_mode);
287 }
288 
289 #endif /* __iwl_op_mode_h__ */
290