18e99ea8dSJohannes Berg /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
28e99ea8dSJohannes Berg /*
370c9101dSJohannes Berg * Copyright (C) 2005-2014, 2018-2021 Intel Corporation
48e99ea8dSJohannes Berg * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
58e99ea8dSJohannes Berg * Copyright (C) 2015 Intel Deutschland GmbH
68e99ea8dSJohannes Berg */
7e705c121SKalle Valo #ifndef __iwl_op_mode_h__
8e705c121SKalle Valo #define __iwl_op_mode_h__
9e705c121SKalle Valo
10e705c121SKalle Valo #include <linux/netdevice.h>
11e705c121SKalle Valo #include <linux/debugfs.h>
12d0129315SMordechay Goodstein #include "iwl-dbg-tlv.h"
13e705c121SKalle Valo
14e705c121SKalle Valo struct iwl_op_mode;
15e705c121SKalle Valo struct iwl_trans;
16e705c121SKalle Valo struct sk_buff;
17e705c121SKalle Valo struct iwl_device_cmd;
18e705c121SKalle Valo struct iwl_rx_cmd_buffer;
19e705c121SKalle Valo struct iwl_fw;
20e705c121SKalle Valo struct iwl_cfg;
21e705c121SKalle Valo
22e705c121SKalle Valo /**
23e705c121SKalle Valo * DOC: Operational mode - what is it ?
24e705c121SKalle Valo *
25e705c121SKalle Valo * The operational mode (a.k.a. op_mode) is the layer that implements
26e705c121SKalle Valo * mac80211's handlers. It knows two APIs: mac80211's and the fw's. It uses
27e705c121SKalle Valo * the transport API to access the HW. The op_mode doesn't need to know how the
28e705c121SKalle Valo * underlying HW works, since the transport layer takes care of that.
29e705c121SKalle Valo *
30e705c121SKalle Valo * There can be several op_mode: i.e. different fw APIs will require two
31e705c121SKalle Valo * different op_modes. This is why the op_mode is virtualized.
32e705c121SKalle Valo */
33e705c121SKalle Valo
34e705c121SKalle Valo /**
35e705c121SKalle Valo * DOC: Life cycle of the Operational mode
36e705c121SKalle Valo *
37e705c121SKalle Valo * The operational mode has a very simple life cycle.
38e705c121SKalle Valo *
39e705c121SKalle Valo * 1) The driver layer (iwl-drv.c) chooses the op_mode based on the
40e705c121SKalle Valo * capabilities advertised by the fw file (in TLV format).
41e705c121SKalle Valo * 2) The driver layer starts the op_mode (ops->start)
42e705c121SKalle Valo * 3) The op_mode registers mac80211
43e705c121SKalle Valo * 4) The op_mode is governed by mac80211
44e705c121SKalle Valo * 5) The driver layer stops the op_mode
45e705c121SKalle Valo */
46e705c121SKalle Valo
47e705c121SKalle Valo /**
48e705c121SKalle Valo * struct iwl_op_mode_ops - op_mode specific operations
49e705c121SKalle Valo *
50e705c121SKalle Valo * The op_mode exports its ops so that external components can start it and
51e705c121SKalle Valo * interact with it. The driver layer typically calls the start and stop
52e705c121SKalle Valo * handlers, the transport layer calls the others.
53e705c121SKalle Valo *
54e705c121SKalle Valo * All the handlers MUST be implemented, except @rx_rss which can be left
55e705c121SKalle Valo * out *iff* the opmode will never run on hardware with multi-queue capability.
56e705c121SKalle Valo *
57e705c121SKalle Valo * @start: start the op_mode. The transport layer is already allocated.
58e705c121SKalle Valo * May sleep
59e705c121SKalle Valo * @stop: stop the op_mode. Must free all the memory allocated.
60e705c121SKalle Valo * May sleep
61e705c121SKalle Valo * @rx: Rx notification to the op_mode. rxb is the Rx buffer itself. Cmd is the
62e705c121SKalle Valo * HCMD this Rx responds to. Can't sleep.
63e705c121SKalle Valo * @rx_rss: data queue RX notification to the op_mode, for (data) notifications
64e705c121SKalle Valo * received on the RSS queue(s). The queue parameter indicates which of the
65e705c121SKalle Valo * RSS queues received this frame; it will always be non-zero.
66e705c121SKalle Valo * This method must not sleep.
67dcbb4746SEmmanuel Grumbach * @async_cb: called when an ASYNC command with CMD_WANT_ASYNC_CALLBACK set
68dcbb4746SEmmanuel Grumbach * completes. Must be atomic.
69e705c121SKalle Valo * @queue_full: notifies that a HW queue is full.
70e705c121SKalle Valo * Must be atomic and called with BH disabled.
71e705c121SKalle Valo * @queue_not_full: notifies that a HW queue is not full any more.
72e705c121SKalle Valo * Must be atomic and called with BH disabled.
73e705c121SKalle Valo * @hw_rf_kill:notifies of a change in the HW rf kill switch. True means that
74e705c121SKalle Valo * the radio is killed. Return %true if the device should be stopped by
75e705c121SKalle Valo * the transport immediately after the call. May sleep.
76e705c121SKalle Valo * @free_skb: allows the transport layer to free skbs that haven't been
77e705c121SKalle Valo * reclaimed by the op_mode. This can happen when the driver is freed and
78e705c121SKalle Valo * there are Tx packets pending in the transport layer.
79e705c121SKalle Valo * Must be atomic
80e705c121SKalle Valo * @nic_error: error notification. Must be atomic and must be called with BH
81*b8221b0fSJohannes Berg * disabled, unless the sync parameter is true.
82e705c121SKalle Valo * @cmd_queue_full: Called when the command queue gets full. Must be atomic and
83e705c121SKalle Valo * called with BH disabled.
84e705c121SKalle Valo * @nic_config: configure NIC, called before firmware is started.
85e705c121SKalle Valo * May sleep
86e705c121SKalle Valo * @wimax_active: invoked when WiMax becomes active. May sleep
87d0129315SMordechay Goodstein * @time_point: called when transport layer wants to collect debug data
88e705c121SKalle Valo */
89e705c121SKalle Valo struct iwl_op_mode_ops {
90e705c121SKalle Valo struct iwl_op_mode *(*start)(struct iwl_trans *trans,
91e705c121SKalle Valo const struct iwl_cfg *cfg,
92e705c121SKalle Valo const struct iwl_fw *fw,
93e705c121SKalle Valo struct dentry *dbgfs_dir);
94e705c121SKalle Valo void (*stop)(struct iwl_op_mode *op_mode);
95e705c121SKalle Valo void (*rx)(struct iwl_op_mode *op_mode, struct napi_struct *napi,
96e705c121SKalle Valo struct iwl_rx_cmd_buffer *rxb);
97e705c121SKalle Valo void (*rx_rss)(struct iwl_op_mode *op_mode, struct napi_struct *napi,
98e705c121SKalle Valo struct iwl_rx_cmd_buffer *rxb, unsigned int queue);
99dcbb4746SEmmanuel Grumbach void (*async_cb)(struct iwl_op_mode *op_mode,
100dcbb4746SEmmanuel Grumbach const struct iwl_device_cmd *cmd);
101e705c121SKalle Valo void (*queue_full)(struct iwl_op_mode *op_mode, int queue);
102e705c121SKalle Valo void (*queue_not_full)(struct iwl_op_mode *op_mode, int queue);
103e705c121SKalle Valo bool (*hw_rf_kill)(struct iwl_op_mode *op_mode, bool state);
104e705c121SKalle Valo void (*free_skb)(struct iwl_op_mode *op_mode, struct sk_buff *skb);
105*b8221b0fSJohannes Berg void (*nic_error)(struct iwl_op_mode *op_mode, bool sync);
106e705c121SKalle Valo void (*cmd_queue_full)(struct iwl_op_mode *op_mode);
107e705c121SKalle Valo void (*nic_config)(struct iwl_op_mode *op_mode);
108e705c121SKalle Valo void (*wimax_active)(struct iwl_op_mode *op_mode);
109d0129315SMordechay Goodstein void (*time_point)(struct iwl_op_mode *op_mode,
110d0129315SMordechay Goodstein enum iwl_fw_ini_time_point tp_id,
111d0129315SMordechay Goodstein union iwl_dbg_tlv_tp_data *tp_data);
112e705c121SKalle Valo };
113e705c121SKalle Valo
114e705c121SKalle Valo int iwl_opmode_register(const char *name, const struct iwl_op_mode_ops *ops);
115e705c121SKalle Valo void iwl_opmode_deregister(const char *name);
116e705c121SKalle Valo
117e705c121SKalle Valo /**
118e705c121SKalle Valo * struct iwl_op_mode - operational mode
119e705c121SKalle Valo * @ops: pointer to its own ops
120e705c121SKalle Valo *
121e705c121SKalle Valo * This holds an implementation of the mac80211 / fw API.
122e705c121SKalle Valo */
123e705c121SKalle Valo struct iwl_op_mode {
124e705c121SKalle Valo const struct iwl_op_mode_ops *ops;
125e705c121SKalle Valo
12645c21a0eSGustavo A. R. Silva char op_mode_specific[] __aligned(sizeof(void *));
127e705c121SKalle Valo };
128e705c121SKalle Valo
iwl_op_mode_stop(struct iwl_op_mode * op_mode)129e705c121SKalle Valo static inline void iwl_op_mode_stop(struct iwl_op_mode *op_mode)
130e705c121SKalle Valo {
131e705c121SKalle Valo might_sleep();
132e705c121SKalle Valo op_mode->ops->stop(op_mode);
133e705c121SKalle Valo }
134e705c121SKalle Valo
iwl_op_mode_rx(struct iwl_op_mode * op_mode,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb)135e705c121SKalle Valo static inline void iwl_op_mode_rx(struct iwl_op_mode *op_mode,
136e705c121SKalle Valo struct napi_struct *napi,
137e705c121SKalle Valo struct iwl_rx_cmd_buffer *rxb)
138e705c121SKalle Valo {
139e705c121SKalle Valo return op_mode->ops->rx(op_mode, napi, rxb);
140e705c121SKalle Valo }
141e705c121SKalle Valo
iwl_op_mode_rx_rss(struct iwl_op_mode * op_mode,struct napi_struct * napi,struct iwl_rx_cmd_buffer * rxb,unsigned int queue)142e705c121SKalle Valo static inline void iwl_op_mode_rx_rss(struct iwl_op_mode *op_mode,
143e705c121SKalle Valo struct napi_struct *napi,
144e705c121SKalle Valo struct iwl_rx_cmd_buffer *rxb,
145e705c121SKalle Valo unsigned int queue)
146e705c121SKalle Valo {
147e705c121SKalle Valo op_mode->ops->rx_rss(op_mode, napi, rxb, queue);
148e705c121SKalle Valo }
149e705c121SKalle Valo
iwl_op_mode_async_cb(struct iwl_op_mode * op_mode,const struct iwl_device_cmd * cmd)150dcbb4746SEmmanuel Grumbach static inline void iwl_op_mode_async_cb(struct iwl_op_mode *op_mode,
151dcbb4746SEmmanuel Grumbach const struct iwl_device_cmd *cmd)
152dcbb4746SEmmanuel Grumbach {
153dcbb4746SEmmanuel Grumbach if (op_mode->ops->async_cb)
154dcbb4746SEmmanuel Grumbach op_mode->ops->async_cb(op_mode, cmd);
155dcbb4746SEmmanuel Grumbach }
156dcbb4746SEmmanuel Grumbach
iwl_op_mode_queue_full(struct iwl_op_mode * op_mode,int queue)157e705c121SKalle Valo static inline void iwl_op_mode_queue_full(struct iwl_op_mode *op_mode,
158e705c121SKalle Valo int queue)
159e705c121SKalle Valo {
160e705c121SKalle Valo op_mode->ops->queue_full(op_mode, queue);
161e705c121SKalle Valo }
162e705c121SKalle Valo
iwl_op_mode_queue_not_full(struct iwl_op_mode * op_mode,int queue)163e705c121SKalle Valo static inline void iwl_op_mode_queue_not_full(struct iwl_op_mode *op_mode,
164e705c121SKalle Valo int queue)
165e705c121SKalle Valo {
166e705c121SKalle Valo op_mode->ops->queue_not_full(op_mode, queue);
167e705c121SKalle Valo }
168e705c121SKalle Valo
169e705c121SKalle Valo static inline bool __must_check
iwl_op_mode_hw_rf_kill(struct iwl_op_mode * op_mode,bool state)170e705c121SKalle Valo iwl_op_mode_hw_rf_kill(struct iwl_op_mode *op_mode, bool state)
171e705c121SKalle Valo {
172e705c121SKalle Valo might_sleep();
173e705c121SKalle Valo return op_mode->ops->hw_rf_kill(op_mode, state);
174e705c121SKalle Valo }
175e705c121SKalle Valo
iwl_op_mode_free_skb(struct iwl_op_mode * op_mode,struct sk_buff * skb)176e705c121SKalle Valo static inline void iwl_op_mode_free_skb(struct iwl_op_mode *op_mode,
177e705c121SKalle Valo struct sk_buff *skb)
178e705c121SKalle Valo {
17970c9101dSJohannes Berg if (WARN_ON_ONCE(!op_mode))
18070c9101dSJohannes Berg return;
181e705c121SKalle Valo op_mode->ops->free_skb(op_mode, skb);
182e705c121SKalle Valo }
183e705c121SKalle Valo
iwl_op_mode_nic_error(struct iwl_op_mode * op_mode,bool sync)184*b8221b0fSJohannes Berg static inline void iwl_op_mode_nic_error(struct iwl_op_mode *op_mode, bool sync)
185e705c121SKalle Valo {
186*b8221b0fSJohannes Berg op_mode->ops->nic_error(op_mode, sync);
187e705c121SKalle Valo }
188e705c121SKalle Valo
iwl_op_mode_cmd_queue_full(struct iwl_op_mode * op_mode)189e705c121SKalle Valo static inline void iwl_op_mode_cmd_queue_full(struct iwl_op_mode *op_mode)
190e705c121SKalle Valo {
191e705c121SKalle Valo op_mode->ops->cmd_queue_full(op_mode);
192e705c121SKalle Valo }
193e705c121SKalle Valo
iwl_op_mode_nic_config(struct iwl_op_mode * op_mode)194e705c121SKalle Valo static inline void iwl_op_mode_nic_config(struct iwl_op_mode *op_mode)
195e705c121SKalle Valo {
196e705c121SKalle Valo might_sleep();
197e705c121SKalle Valo op_mode->ops->nic_config(op_mode);
198e705c121SKalle Valo }
199e705c121SKalle Valo
iwl_op_mode_wimax_active(struct iwl_op_mode * op_mode)200e705c121SKalle Valo static inline void iwl_op_mode_wimax_active(struct iwl_op_mode *op_mode)
201e705c121SKalle Valo {
202e705c121SKalle Valo might_sleep();
203e705c121SKalle Valo op_mode->ops->wimax_active(op_mode);
204e705c121SKalle Valo }
205e705c121SKalle Valo
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)206d0129315SMordechay Goodstein static inline void iwl_op_mode_time_point(struct iwl_op_mode *op_mode,
207d0129315SMordechay Goodstein enum iwl_fw_ini_time_point tp_id,
208d0129315SMordechay Goodstein union iwl_dbg_tlv_tp_data *tp_data)
209d0129315SMordechay Goodstein {
2104538c5edSJohannes Berg if (!op_mode || !op_mode->ops || !op_mode->ops->time_point)
2114538c5edSJohannes Berg return;
212d0129315SMordechay Goodstein op_mode->ops->time_point(op_mode, tp_id, tp_data);
213d0129315SMordechay Goodstein }
214d0129315SMordechay Goodstein
215e705c121SKalle Valo #endif /* __iwl_op_mode_h__ */
216