1 /*-
2  * Copyright (c) 2020-2022 The FreeBSD Foundation
3  * Copyright (c) 2020-2021 Bjoern A. Zeeb
4  *
5  * This software was developed by Björn Zeeb under sponsorship from
6  * the FreeBSD Foundation.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /*
31  * Public functions are called linuxkpi_*().
32  * Internal (static) functions are called lkpi_*().
33  *
34  * The internal structures holding metadata over public structures are also
35  * called lkpi_xxx (usually with a member at the end called xxx).
36  * Note: we do not replicate the structure names but the general variable names
37  * for these (e.g., struct hw -> struct lkpi_hw, struct sta -> struct lkpi_sta).
38  * There are macros to access one from the other.
39  * We call the internal versions lxxx (e.g., hw -> lhw, sta -> lsta).
40  */
41 
42 #ifndef _LKPI_SRC_LINUX_80211_H
43 #define _LKPI_SRC_LINUX_80211_H
44 
45 /* #define	LINUXKPI_DEBUG_80211 */
46 
47 #ifndef	D80211_TODO
48 #define	D80211_TODO		0x1
49 #endif
50 #ifndef D80211_IMPROVE
51 #define	D80211_IMPROVE		0x2
52 #endif
53 #define	D80211_TRACE		0x10
54 #define	D80211_TRACEOK		0x20
55 #define	D80211_TRACE_TX		0x100
56 #define	D80211_TRACE_TX_DUMP	0x200
57 #define	D80211_TRACE_RX		0x1000
58 #define	D80211_TRACE_RX_DUMP	0x2000
59 #define	D80211_TRACE_RX_BEACONS	0x4000
60 #define	D80211_TRACEX		(D80211_TRACE_TX|D80211_TRACE_RX)
61 #define	D80211_TRACEX_DUMP	(D80211_TRACE_TX_DUMP|D80211_TRACE_RX_DUMP)
62 #define	D80211_TRACE_STA	0x10000
63 #define	D80211_TRACE_MO		0x100000
64 
65 struct lkpi_radiotap_tx_hdr {
66 	struct ieee80211_radiotap_header wt_ihdr;
67 	uint8_t		wt_flags;
68 	uint8_t		wt_rate;
69 	uint16_t	wt_chan_freq;
70 	uint16_t	wt_chan_flags;
71 } __packed;
72 #define	LKPI_RTAP_TX_FLAGS_PRESENT					\
73 	((1 << IEEE80211_RADIOTAP_FLAGS) |				\
74 	 (1 << IEEE80211_RADIOTAP_RATE) |				\
75 	 (1 << IEEE80211_RADIOTAP_CHANNEL))
76 
77 struct lkpi_radiotap_rx_hdr {
78 	struct ieee80211_radiotap_header wr_ihdr;
79 	uint64_t	wr_tsft;
80 	uint8_t		wr_flags;
81 	uint8_t		wr_rate;
82 	uint16_t	wr_chan_freq;
83 	uint16_t	wr_chan_flags;
84 	int8_t		wr_dbm_antsignal;
85 	int8_t		wr_dbm_antnoise;
86 } __packed __aligned(8);
87 #define	LKPI_RTAP_RX_FLAGS_PRESENT					\
88 	((1 << IEEE80211_RADIOTAP_TSFT) |				\
89 	 (1 << IEEE80211_RADIOTAP_FLAGS) |				\
90 	 (1 << IEEE80211_RADIOTAP_RATE) |				\
91 	 (1 << IEEE80211_RADIOTAP_CHANNEL) |				\
92 	 (1 << IEEE80211_RADIOTAP_DBM_ANTSIGNAL) |			\
93 	 (1 << IEEE80211_RADIOTAP_DBM_ANTNOISE))
94 
95 struct lkpi_txq {
96 	bool			seen_dequeue;
97 	struct sk_buff_head	skbq;
98 
99 	/* Must be last! */
100 	struct ieee80211_txq	txq __aligned(CACHE_LINE_SIZE);
101 };
102 #define	TXQ_TO_LTXQ(_txq)	container_of(_txq, struct lkpi_txq, txq)
103 
104 
105 struct lkpi_sta {
106         TAILQ_ENTRY(lkpi_sta)	lsta_entry;
107 	struct ieee80211_node	*ni;
108 
109 	/* Deferred TX path. */
110 	/* Eventually we might want to migrate this into net80211 entirely. */
111 	/* XXX-BZ can we use sta->txq[] instead directly? */
112 	struct task		txq_task;
113 	struct mbufq		txq;
114 	struct mtx		txq_mtx;
115 
116 	struct ieee80211_key_conf *kc;
117 	enum ieee80211_sta_state state;
118 	bool			added_to_drv;			/* Driver knows; i.e. we called ...(). */
119 	bool			in_mgd;				/* XXX-BZ should this be per-vif? */
120 
121 	/* Must be last! */
122 	struct ieee80211_sta	sta __aligned(CACHE_LINE_SIZE);
123 };
124 #define	STA_TO_LSTA(_sta)	container_of(_sta, struct lkpi_sta, sta)
125 #define	LSTA_TO_STA(_lsta)	(&(_lsta)->sta)
126 
127 struct lkpi_vif {
128         TAILQ_ENTRY(lkpi_vif)	lvif_entry;
129 	struct ieee80211vap	iv_vap;
130 
131 	struct mtx		mtx;
132 	struct wireless_dev	wdev;
133 
134 	/* Other local stuff. */
135 	int			(*iv_newstate)(struct ieee80211vap *,
136 				    enum ieee80211_state, int);
137 	struct ieee80211_node *	(*iv_update_bss)(struct ieee80211vap *,
138 				    struct ieee80211_node *);
139 	TAILQ_HEAD(, lkpi_sta)	lsta_head;
140 	bool			added_to_drv;			/* Driver knows; i.e. we called add_interface(). */
141 
142 	/* Must be last! */
143 	struct ieee80211_vif	vif __aligned(CACHE_LINE_SIZE);
144 };
145 #define	VAP_TO_LVIF(_vap)	container_of(_vap, struct lkpi_vif, iv_vap)
146 #define	LVIF_TO_VAP(_lvif)	(&(_lvif)->iv_vap)
147 #define	VIF_TO_LVIF(_vif)	container_of(_vif, struct lkpi_vif, vif)
148 #define	LVIF_TO_VIF(_lvif)	(&(_lvif)->vif)
149 
150 
151 struct lkpi_hw {	/* name it mac80211_sc? */
152 	const struct ieee80211_ops	*ops;
153 	struct ieee80211_scan_request	*hw_req;
154 	struct workqueue_struct		*workq;
155 
156 	/* FreeBSD specific compat. */
157 	/* Linux device is in hw.wiphy->dev after SET_IEEE80211_DEV(). */
158 	struct ieee80211com		*ic;
159 	struct lkpi_radiotap_tx_hdr	rtap_tx;
160 	struct lkpi_radiotap_rx_hdr	rtap_rx;
161 
162 	TAILQ_HEAD(, lkpi_vif)		lvif_head;
163 	struct sx			lvif_sx;
164 
165 	struct mtx			mtx;
166 
167 	/* Scan functions we overload to handle depending on scan mode. */
168 	void                    (*ic_scan_curchan)(struct ieee80211_scan_state *,
169 				    unsigned long);
170 	void                    (*ic_scan_mindwell)(struct ieee80211_scan_state *);
171 
172 	/* Node functions we overload to sync state. */
173 	struct ieee80211_node *	(*ic_node_alloc)(struct ieee80211vap *,
174 				    const uint8_t [IEEE80211_ADDR_LEN]);
175 	int			(*ic_node_init)(struct ieee80211_node *);
176 	void			(*ic_node_cleanup)(struct ieee80211_node *);
177 	void			(*ic_node_free)(struct ieee80211_node *);
178 
179 #define	LKPI_MAC80211_DRV_STARTED	0x00000001
180 	uint32_t			sc_flags;
181 #define	LKPI_LHW_SCAN_RUNNING		0x00000001
182 #define	LKPI_LHW_SCAN_HW		0x00000002
183 	uint32_t			scan_flags;
184 
185 	int				supbands;	/* Number of supported bands. */
186 	int				max_rates;	/* Maximum number of bitrates supported in any channel. */
187 	int				scan_ie_len;	/* Length of common per-band scan IEs. */
188 
189 	bool				update_mc;
190 	bool				update_wme;
191 
192 	/* Must be last! */
193 	struct ieee80211_hw		hw __aligned(CACHE_LINE_SIZE);
194 };
195 #define	LHW_TO_HW(_lhw)		(&(_lhw)->hw)
196 #define	HW_TO_LHW(_hw)		container_of(_hw, struct lkpi_hw, hw)
197 
198 struct lkpi_wiphy {
199 	const struct cfg80211_ops	*ops;
200 
201 	/* Must be last! */
202 	struct wiphy			wiphy __aligned(CACHE_LINE_SIZE);
203 };
204 #define	WIPHY_TO_LWIPHY(_wiphy)	container_of(_wiphy, struct lkpi_wiphy, wiphy)
205 #define	LWIPHY_TO_WIPHY(_lwiphy)	(&(_lwiphy)->wiphy)
206 
207 
208 #define	LKPI_80211_LHW_LOCK(_lhw)	mtx_lock(&(_lhw)->mtx)
209 #define	LKPI_80211_LHW_UNLOCK(_lhw)	mtx_unlock(&(_lhw)->mtx)
210 #define	LKPI_80211_LHW_LOCK_ASSERT(_lhw) \
211     mtx_assert(&(_lhw)->mtx, MA_OWNED)
212 #define	LKPI_80211_LHW_UNLOCK_ASSERT(_lhw) \
213     mtx_assert(&(_lhw)->mtx, MA_NOTOWNED)
214 
215 #define	LKPI_80211_LHW_LVIF_LOCK(_lhw)	sx_xlock(&(_lhw)->lvif_sx)
216 #define	LKPI_80211_LHW_LVIF_UNLOCK(_lhw) sx_xunlock(&(_lhw)->lvif_sx)
217 
218 #define	LKPI_80211_LVIF_LOCK(_lvif)	mtx_lock(&(_lvif)->mtx)
219 #define	LKPI_80211_LVIF_UNLOCK(_lvif)	mtx_unlock(&(_lvif)->mtx)
220 
221 #define	LKPI_80211_LSTA_LOCK(_lsta)	mtx_lock(&(_lsta)->txq_mtx)
222 #define	LKPI_80211_LSTA_UNLOCK(_lsta)	mtx_unlock(&(_lsta)->txq_mtx)
223 
224 
225 int lkpi_80211_mo_start(struct ieee80211_hw *);
226 void lkpi_80211_mo_stop(struct ieee80211_hw *);
227 int lkpi_80211_mo_get_antenna(struct ieee80211_hw *, u32 *, u32 *);
228 int lkpi_80211_mo_set_frag_threshold(struct ieee80211_hw *, uint32_t);
229 int lkpi_80211_mo_set_rts_threshold(struct ieee80211_hw *, uint32_t);
230 int lkpi_80211_mo_add_interface(struct ieee80211_hw *, struct ieee80211_vif *);
231 void lkpi_80211_mo_remove_interface(struct ieee80211_hw *, struct ieee80211_vif *);
232 int lkpi_80211_mo_hw_scan(struct ieee80211_hw *, struct ieee80211_vif *,
233     struct ieee80211_scan_request *);
234 void lkpi_80211_mo_cancel_hw_scan(struct ieee80211_hw *, struct ieee80211_vif *);
235 void lkpi_80211_mo_sw_scan_complete(struct ieee80211_hw *, struct ieee80211_vif *);
236 void lkpi_80211_mo_sw_scan_start(struct ieee80211_hw *, struct ieee80211_vif *,
237     const u8 *);
238 u64 lkpi_80211_mo_prepare_multicast(struct ieee80211_hw *,
239     struct netdev_hw_addr_list *);
240 void lkpi_80211_mo_configure_filter(struct ieee80211_hw *, unsigned int,
241     unsigned int *, u64);
242 int lkpi_80211_mo_sta_state(struct ieee80211_hw *, struct ieee80211_vif *,
243     struct lkpi_sta *, enum ieee80211_sta_state);
244 int lkpi_80211_mo_config(struct ieee80211_hw *, uint32_t);
245 int lkpi_80211_mo_assign_vif_chanctx(struct ieee80211_hw *, struct ieee80211_vif *,
246     struct ieee80211_chanctx_conf *);
247 void lkpi_80211_mo_unassign_vif_chanctx(struct ieee80211_hw *, struct ieee80211_vif *,
248     struct ieee80211_chanctx_conf **);
249 int lkpi_80211_mo_add_chanctx(struct ieee80211_hw *, struct ieee80211_chanctx_conf *);
250 void lkpi_80211_mo_change_chanctx(struct ieee80211_hw *,
251     struct ieee80211_chanctx_conf *, uint32_t);
252 void lkpi_80211_mo_remove_chanctx(struct ieee80211_hw *,
253     struct ieee80211_chanctx_conf *);
254 void lkpi_80211_mo_bss_info_changed(struct ieee80211_hw *, struct ieee80211_vif *,
255     struct ieee80211_bss_conf *, uint64_t);
256 int lkpi_80211_mo_conf_tx(struct ieee80211_hw *, struct ieee80211_vif *,
257     uint16_t, const struct ieee80211_tx_queue_params *);
258 void lkpi_80211_mo_flush(struct ieee80211_hw *, struct ieee80211_vif *,
259     uint32_t, bool);
260 void lkpi_80211_mo_mgd_prepare_tx(struct ieee80211_hw *, struct ieee80211_vif *,
261     struct ieee80211_prep_tx_info *);
262 void lkpi_80211_mo_mgd_complete_tx(struct ieee80211_hw *, struct ieee80211_vif *,
263     struct ieee80211_prep_tx_info *);
264 void lkpi_80211_mo_tx(struct ieee80211_hw *, struct ieee80211_tx_control *,
265     struct sk_buff *);
266 void lkpi_80211_mo_wake_tx_queue(struct ieee80211_hw *, struct ieee80211_txq *);
267 void lkpi_80211_mo_sync_rx_queues(struct ieee80211_hw *);
268 void lkpi_80211_mo_sta_pre_rcu_remove(struct ieee80211_hw *,
269     struct ieee80211_vif *, struct ieee80211_sta *);
270 int lkpi_80211_mo_set_key(struct ieee80211_hw *, enum set_key_cmd,
271     struct ieee80211_vif *, struct ieee80211_sta *,
272     struct ieee80211_key_conf *);
273 
274 #endif	/* _LKPI_SRC_LINUX_80211_H */
275