1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /*
3  * Copyright (C) 2005-2014, 2018-2021, 2024 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_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  */
56 enum iwl_fw_error_type {
57 	IWL_ERR_TYPE_IRQ,
58 	IWL_ERR_TYPE_NMI_FORCED,
59 	IWL_ERR_TYPE_RESET_HS_TIMEOUT,
60 	IWL_ERR_TYPE_CMD_QUEUE_FULL,
61 };
62 
63 /**
64  * enum iwl_fw_error_context - error dump context
65  * @IWL_ERR_CONTEXT_WORKER: regular from worker context,
66  *	opmode must acquire locks and must also check
67  *	for @IWL_ERR_CONTEXT_ABORT after acquiring locks
68  * @IWL_ERR_CONTEXT_FROM_OPMODE: context is in a call
69  *	originating from the opmode, e.g. while resetting
70  *	or stopping the device, so opmode must not acquire
71  *	any locks
72  * @IWL_ERR_CONTEXT_ABORT: after lock acquisition, indicates
73  *	that the dump already happened via another callback
74  *	(currently only while stopping the device) via the
75  *	@IWL_ERR_CONTEXT_FROM_OPMODE context, and this call
76  *	must be aborted
77  */
78 enum iwl_fw_error_context {
79 	IWL_ERR_CONTEXT_WORKER,
80 	IWL_ERR_CONTEXT_FROM_OPMODE,
81 	IWL_ERR_CONTEXT_ABORT,
82 };
83 
84 /**
85  * struct iwl_fw_error_dump_mode - error dump mode for callback
86  * @type: The reason for the dump, per &enum iwl_fw_error_type.
87  * @context: The context for the dump, may also indicate this
88  *	call needs to be skipped. This MUST be checked before
89  *	and after acquiring any locks in the op-mode!
90  */
91 struct iwl_fw_error_dump_mode {
92 	enum iwl_fw_error_type type;
93 	enum iwl_fw_error_context context;
94 };
95 
96 /**
97  * struct iwl_op_mode_ops - op_mode specific operations
98  *
99  * The op_mode exports its ops so that external components can start it and
100  * interact with it. The driver layer typically calls the start and stop
101  * handlers, the transport layer calls the others.
102  *
103  * All the handlers MUST be implemented, except @rx_rss which can be left
104  * out *iff* the opmode will never run on hardware with multi-queue capability.
105  *
106  * @start: start the op_mode. The transport layer is already allocated.
107  *	May sleep
108  * @stop: stop the op_mode. Must free all the memory allocated.
109  *	May sleep
110  * @rx: Rx notification to the op_mode. rxb is the Rx buffer itself. Cmd is the
111  *	HCMD this Rx responds to. Can't sleep.
112  * @rx_rss: data queue RX notification to the op_mode, for (data) notifications
113  *	received on the RSS queue(s). The queue parameter indicates which of the
114  *	RSS queues received this frame; it will always be non-zero.
115  *	This method must not sleep.
116  * @queue_full: notifies that a HW queue is full.
117  *	Must be atomic and called with BH disabled.
118  * @queue_not_full: notifies that a HW queue is not full any more.
119  *	Must be atomic and called with BH disabled.
120  * @hw_rf_kill: notifies of a change in the HW rf kill switch. True means that
121  *	the radio is killed. Return %true if the device should be stopped by
122  *	the transport immediately after the call. May sleep.
123  *	Note that this must not return %true for newer devices using gen2 PCIe
124  *	transport.
125  * @free_skb: allows the transport layer to free skbs that haven't been
126  *	reclaimed by the op_mode. This can happen when the driver is freed and
127  *	there are Tx packets pending in the transport layer.
128  *	Must be atomic
129  * @nic_error: error notification. Must be atomic, the op mode should handle
130  *	the error (e.g. abort notification waiters) and print the error if
131  *	applicable
132  * @dump_error: NIC error dump collection (can sleep, synchronous)
133  * @sw_reset: (maybe) initiate a software reset, return %true if started
134  * @nic_config: configure NIC, called before firmware is started.
135  *	May sleep
136  * @wimax_active: invoked when WiMax becomes active. May sleep
137  * @time_point: called when transport layer wants to collect debug data
138  * @device_powered_off: called upon resume from hibernation but not only.
139  *	Op_mode needs to reset its internal state because the device did not
140  *	survive the system state transition. The firmware is no longer running,
141  *	etc...
142  */
143 struct iwl_op_mode_ops {
144 	struct iwl_op_mode *(*start)(struct iwl_trans *trans,
145 				     const struct iwl_cfg *cfg,
146 				     const struct iwl_fw *fw,
147 				     struct dentry *dbgfs_dir);
148 	void (*stop)(struct iwl_op_mode *op_mode);
149 	void (*rx)(struct iwl_op_mode *op_mode, struct napi_struct *napi,
150 		   struct iwl_rx_cmd_buffer *rxb);
151 	void (*rx_rss)(struct iwl_op_mode *op_mode, struct napi_struct *napi,
152 		       struct iwl_rx_cmd_buffer *rxb, unsigned int queue);
153 	void (*queue_full)(struct iwl_op_mode *op_mode, int queue);
154 	void (*queue_not_full)(struct iwl_op_mode *op_mode, int queue);
155 	bool (*hw_rf_kill)(struct iwl_op_mode *op_mode, bool state);
156 	void (*free_skb)(struct iwl_op_mode *op_mode, struct sk_buff *skb);
157 	void (*nic_error)(struct iwl_op_mode *op_mode,
158 			  enum iwl_fw_error_type type);
159 	void (*dump_error)(struct iwl_op_mode *op_mode,
160 			   struct iwl_fw_error_dump_mode *mode);
161 	bool (*sw_reset)(struct iwl_op_mode *op_mode,
162 			 enum iwl_fw_error_type type);
163 	void (*nic_config)(struct iwl_op_mode *op_mode);
164 	void (*wimax_active)(struct iwl_op_mode *op_mode);
165 	void (*time_point)(struct iwl_op_mode *op_mode,
166 			   enum iwl_fw_ini_time_point tp_id,
167 			   union iwl_dbg_tlv_tp_data *tp_data);
168 	void (*device_powered_off)(struct iwl_op_mode *op_mode);
169 };
170 
171 int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops);
172 void iwl_opmode_deregister(const char *name);
173 
174 /**
175  * struct iwl_op_mode - operational mode
176  * @ops: pointer to its own ops
177  *
178  * This holds an implementation of the mac80211 / fw API.
179  */
180 struct iwl_op_mode {
181 	const struct iwl_op_mode_ops *ops;
182 
183 	char op_mode_specific[] __aligned(sizeof(void *));
184 };
185 
iwl_op_mode_stop(struct iwl_op_mode * op_mode)186 static inline void iwl_op_mode_stop(struct iwl_op_mode *op_mode)
187 {
188 	might_sleep();
189 	op_mode->ops->stop(op_mode);
190 }
191 
iwl_op_mode_rx(struct iwl_op_mode * op_mode,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb)192 static inline void iwl_op_mode_rx(struct iwl_op_mode *op_mode,
193 				  struct napi_struct *napi,
194 				  struct iwl_rx_cmd_buffer *rxb)
195 {
196 	return op_mode->ops->rx(op_mode, napi, rxb);
197 }
198 
iwl_op_mode_rx_rss(struct iwl_op_mode * op_mode,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb,unsigned int queue)199 static inline void iwl_op_mode_rx_rss(struct iwl_op_mode *op_mode,
200 				      struct napi_struct *napi,
201 				      struct iwl_rx_cmd_buffer *rxb,
202 				      unsigned int queue)
203 {
204 	op_mode->ops->rx_rss(op_mode, napi, rxb, queue);
205 }
206 
iwl_op_mode_queue_full(struct iwl_op_mode * op_mode,int queue)207 static inline void iwl_op_mode_queue_full(struct iwl_op_mode *op_mode,
208 					  int queue)
209 {
210 	op_mode->ops->queue_full(op_mode, queue);
211 }
212 
iwl_op_mode_queue_not_full(struct iwl_op_mode * op_mode,int queue)213 static inline void iwl_op_mode_queue_not_full(struct iwl_op_mode *op_mode,
214 					      int queue)
215 {
216 	op_mode->ops->queue_not_full(op_mode, queue);
217 }
218 
219 static inline bool __must_check
iwl_op_mode_hw_rf_kill(struct iwl_op_mode * op_mode,bool state)220 iwl_op_mode_hw_rf_kill(struct iwl_op_mode *op_mode, bool state)
221 {
222 	might_sleep();
223 	return op_mode->ops->hw_rf_kill(op_mode, state);
224 }
225 
iwl_op_mode_free_skb(struct iwl_op_mode * op_mode,struct sk_buff * skb)226 static inline void iwl_op_mode_free_skb(struct iwl_op_mode *op_mode,
227 					struct sk_buff *skb)
228 {
229 	if (WARN_ON_ONCE(!op_mode))
230 		return;
231 	op_mode->ops->free_skb(op_mode, skb);
232 }
233 
iwl_op_mode_nic_error(struct iwl_op_mode * op_mode,enum iwl_fw_error_type type)234 static inline void iwl_op_mode_nic_error(struct iwl_op_mode *op_mode,
235 					 enum iwl_fw_error_type type)
236 {
237 	op_mode->ops->nic_error(op_mode, type);
238 }
239 
iwl_op_mode_dump_error(struct iwl_op_mode * op_mode,struct iwl_fw_error_dump_mode * mode)240 static inline void iwl_op_mode_dump_error(struct iwl_op_mode *op_mode,
241 					  struct iwl_fw_error_dump_mode *mode)
242 {
243 	might_sleep();
244 
245 	if (op_mode->ops->dump_error)
246 		op_mode->ops->dump_error(op_mode, mode);
247 }
248 
iwl_op_mode_nic_config(struct iwl_op_mode * op_mode)249 static inline void iwl_op_mode_nic_config(struct iwl_op_mode *op_mode)
250 {
251 	might_sleep();
252 	if (op_mode->ops->nic_config)
253 		op_mode->ops->nic_config(op_mode);
254 }
255 
iwl_op_mode_wimax_active(struct iwl_op_mode * op_mode)256 static inline void iwl_op_mode_wimax_active(struct iwl_op_mode *op_mode)
257 {
258 	might_sleep();
259 	op_mode->ops->wimax_active(op_mode);
260 }
261 
iwl_op_mode_time_point(struct iwl_op_mode * op_mode,enum iwl_fw_ini_time_point tp_id,union iwl_dbg_tlv_tp_data * tp_data)262 static inline void iwl_op_mode_time_point(struct iwl_op_mode *op_mode,
263 					  enum iwl_fw_ini_time_point tp_id,
264 					  union iwl_dbg_tlv_tp_data *tp_data)
265 {
266 	if (!op_mode || !op_mode->ops || !op_mode->ops->time_point)
267 		return;
268 	op_mode->ops->time_point(op_mode, tp_id, tp_data);
269 }
270 
iwl_op_mode_device_powered_off(struct iwl_op_mode * op_mode)271 static inline void iwl_op_mode_device_powered_off(struct iwl_op_mode *op_mode)
272 {
273 	if (!op_mode || !op_mode->ops || !op_mode->ops->device_powered_off)
274 		return;
275 	op_mode->ops->device_powered_off(op_mode);
276 }
277 
278 #endif /* __iwl_op_mode_h__ */
279