xref: /freebsd/sys/compat/linuxkpi/common/src/linux_80211.c (revision 926905796749750da6464b97ec4f8eec0882cc0e)
1 /*-
2  * Copyright (c) 2020-2024 The FreeBSD Foundation
3  * Copyright (c) 2020-2024 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 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/kernel.h>
45 #include <sys/errno.h>
46 #include <sys/malloc.h>
47 #include <sys/module.h>
48 #include <sys/mutex.h>
49 #include <sys/socket.h>
50 #include <sys/sysctl.h>
51 #include <sys/queue.h>
52 #include <sys/taskqueue.h>
53 #include <sys/libkern.h>
54 
55 #include <net/if.h>
56 #include <net/if_var.h>
57 #include <net/if_media.h>
58 #include <net/ethernet.h>
59 
60 #include <net80211/ieee80211_var.h>
61 #include <net80211/ieee80211_proto.h>
62 #include <net80211/ieee80211_ratectl.h>
63 #include <net80211/ieee80211_radiotap.h>
64 #include <net80211/ieee80211_vht.h>
65 
66 #define	LINUXKPI_NET80211
67 #include <net/mac80211.h>
68 
69 #include <linux/workqueue.h>
70 #include "linux_80211.h"
71 
72 #define	LKPI_80211_WME
73 /* #define	LKPI_80211_HW_CRYPTO */
74 /* #define	LKPI_80211_VHT */
75 /* #define	LKPI_80211_HT */
76 #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
77 #define	LKPI_80211_HT
78 #endif
79 
80 static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
81 
82 /* XXX-BZ really want this and others in queue.h */
83 #define	TAILQ_ELEM_INIT(elm, field) do {				\
84 	(elm)->field.tqe_next = NULL;					\
85 	(elm)->field.tqe_prev = NULL;					\
86 } while (0)
87 
88 /* -------------------------------------------------------------------------- */
89 
90 /* Keep public for as long as header files are using it too. */
91 int linuxkpi_debug_80211;
92 
93 #ifdef LINUXKPI_DEBUG_80211
94 SYSCTL_DECL(_compat_linuxkpi);
95 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
96     "LinuxKPI 802.11 compatibility layer");
97 
98 SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
99     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
100 
101 #define	UNIMPLEMENTED		if (linuxkpi_debug_80211 & D80211_TODO)		\
102     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
103 #define	TRACEOK()		if (linuxkpi_debug_80211 & D80211_TRACEOK)	\
104     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
105 #else
106 #define	UNIMPLEMENTED		do { } while (0)
107 #define	TRACEOK()		do { } while (0)
108 #endif
109 
110 /* #define	PREP_TX_INFO_DURATION	(IEEE80211_TRANS_WAIT * 1000) */
111 #ifndef PREP_TX_INFO_DURATION
112 #define	PREP_TX_INFO_DURATION	0 /* Let the driver do its thing. */
113 #endif
114 
115 /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
116 const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
117 
118 /* IEEE 802.11-05/0257r1 */
119 const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
120 
121 /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
122 static const uint8_t ieee80211e_up_to_ac[] = {
123 	IEEE80211_AC_BE,
124 	IEEE80211_AC_BK,
125 	IEEE80211_AC_BK,
126 	IEEE80211_AC_BE,
127 	IEEE80211_AC_VI,
128 	IEEE80211_AC_VI,
129 	IEEE80211_AC_VO,
130 	IEEE80211_AC_VO,
131 #if 0
132 	IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
133 #endif
134 };
135 
136 const struct cfg80211_ops linuxkpi_mac80211cfgops = {
137 	/*
138 	 * XXX TODO need a "glue layer" to link cfg80211 ops to
139 	 * mac80211 and to the driver or net80211.
140 	 * Can we pass some on 1:1? Need to compare the (*f)().
141 	 */
142 };
143 
144 #if 0
145 static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
146     struct ieee80211_node *);
147 #endif
148 static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
149 static void lkpi_80211_txq_task(void *, int);
150 static void lkpi_80211_lhw_rxq_task(void *, int);
151 static void lkpi_ieee80211_free_skb_mbuf(void *);
152 #ifdef LKPI_80211_WME
153 static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
154 #endif
155 
156 #if defined(LKPI_80211_HT)
157 static void
lkpi_sta_sync_ht_from_ni(struct ieee80211_sta * sta,struct ieee80211_node * ni,int * ht_rx_nss)158 lkpi_sta_sync_ht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *ht_rx_nss)
159 {
160 	struct ieee80211vap *vap;
161 	uint8_t *ie;
162 	struct ieee80211_ht_cap *htcap;
163 	int i, rx_nss;
164 
165 	if ((ni->ni_flags & IEEE80211_NODE_HT) == 0)
166 		return;
167 
168 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan) &&
169 	    IEEE80211_IS_CHAN_HT40(ni->ni_chan))
170 		sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
171 
172 	sta->deflink.ht_cap.ht_supported = true;
173 
174 	/* htcap->ampdu_params_info */
175 	vap = ni->ni_vap;
176 	sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
177 	if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
178 		sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
179 	sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
180 	if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
181 		sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
182 
183 	ie = ni->ni_ies.htcap_ie;
184 	KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
185 	if (ie[0] == IEEE80211_ELEMID_VENDOR)
186 		ie += 4;
187 	ie += 2;
188 	htcap = (struct ieee80211_ht_cap *)ie;
189 	sta->deflink.ht_cap.cap = htcap->cap_info;
190 	sta->deflink.ht_cap.mcs = htcap->mcs;
191 
192 	rx_nss = 0;
193 	for (i = 0; i < nitems(htcap->mcs.rx_mask); i++) {
194 		if (htcap->mcs.rx_mask[i])
195 			rx_nss++;
196 	}
197 	if (ht_rx_nss != NULL)
198 		*ht_rx_nss = rx_nss;
199 
200 	IMPROVE("sta->wme, sta->deflink.agg.max*");
201 }
202 #endif
203 
204 #if defined(LKPI_80211_VHT)
205 static void
lkpi_sta_sync_vht_from_ni(struct ieee80211_sta * sta,struct ieee80211_node * ni,int * vht_rx_nss)206 lkpi_sta_sync_vht_from_ni(struct ieee80211_sta *sta, struct ieee80211_node *ni, int *vht_rx_nss)
207 {
208 
209 	if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0)
210 		return;
211 
212 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
213 #ifdef __notyet__
214 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
215 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160; /* XXX? */
216 		} else
217 #endif
218 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
219 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_160;
220 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
221 			sta->deflink.bandwidth = IEEE80211_STA_RX_BW_80;
222 	}
223 
224 	IMPROVE("VHT sync ni to sta");
225 	return;
226 }
227 #endif
228 
229 static void
lkpi_lsta_dump(struct lkpi_sta * lsta,struct ieee80211_node * ni,const char * _f,int _l)230 lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
231     const char *_f, int _l)
232 {
233 
234 #ifdef LINUXKPI_DEBUG_80211
235 	if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
236 		return;
237 	if (lsta == NULL)
238 		return;
239 
240 	printf("%s:%d lsta %p ni %p sta %p\n",
241 	    _f, _l, lsta, ni, &lsta->sta);
242 	if (ni != NULL)
243 		ieee80211_dump_node(NULL, ni);
244 	printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
245 	printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
246 		lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
247 #endif
248 }
249 
250 static void
lkpi_lsta_remove(struct lkpi_sta * lsta,struct lkpi_vif * lvif)251 lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
252 {
253 
254 
255 	LKPI_80211_LVIF_LOCK(lvif);
256 	KASSERT(lsta->lsta_entry.tqe_prev != NULL,
257 	    ("%s: lsta %p lsta_entry.tqe_prev %p ni %p\n", __func__,
258 	    lsta, lsta->lsta_entry.tqe_prev, lsta->ni));
259 	TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
260 	LKPI_80211_LVIF_UNLOCK(lvif);
261 }
262 
263 static struct lkpi_sta *
lkpi_lsta_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN],struct ieee80211_hw * hw,struct ieee80211_node * ni)264 lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
265     struct ieee80211_hw *hw, struct ieee80211_node *ni)
266 {
267 	struct lkpi_sta *lsta;
268 	struct lkpi_vif *lvif;
269 	struct ieee80211_vif *vif;
270 	struct ieee80211_sta *sta;
271 	int band, i, tid;
272 	int ht_rx_nss;
273 	int vht_rx_nss;
274 
275 	lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
276 	    M_NOWAIT | M_ZERO);
277 	if (lsta == NULL)
278 		return (NULL);
279 
280 	lsta->added_to_drv = false;
281 	lsta->state = IEEE80211_STA_NOTEXIST;
282 	/*
283 	 * Link the ni to the lsta here without taking a reference.
284 	 * For one we would have to take the reference in node_init()
285 	 * as ieee80211_alloc_node() will initialise the refcount after us.
286 	 * For the other a ni and an lsta are 1:1 mapped and always together
287 	 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
288 	 * using the ni references for the lsta as well despite it being
289 	 * two separate allocations.
290 	 */
291 	lsta->ni = ni;
292 	/* The back-pointer "drv_data" to net80211_node let's us get lsta. */
293 	ni->ni_drv_data = lsta;
294 
295 	lvif = VAP_TO_LVIF(vap);
296 	vif = LVIF_TO_VIF(lvif);
297 	sta = LSTA_TO_STA(lsta);
298 
299 	IEEE80211_ADDR_COPY(sta->addr, mac);
300 
301 	/* TXQ */
302 	for (tid = 0; tid < nitems(sta->txq); tid++) {
303 		struct lkpi_txq *ltxq;
304 
305 		/* We are not limiting ourselves to hw.queues here. */
306 		ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
307 		    M_LKPI80211, M_NOWAIT | M_ZERO);
308 		if (ltxq == NULL)
309 			goto cleanup;
310 		/* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
311 		if (tid == IEEE80211_NUM_TIDS) {
312 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
313 				free(ltxq, M_LKPI80211);
314 				continue;
315 			}
316 			IMPROVE("AP/if we support non-STA here too");
317 			ltxq->txq.ac = IEEE80211_AC_VO;
318 		} else {
319 			ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
320 		}
321 		ltxq->seen_dequeue = false;
322 		ltxq->stopped = false;
323 		ltxq->txq.vif = vif;
324 		ltxq->txq.tid = tid;
325 		ltxq->txq.sta = sta;
326 		TAILQ_ELEM_INIT(ltxq, txq_entry);
327 		skb_queue_head_init(&ltxq->skbq);
328 		LKPI_80211_LTXQ_LOCK_INIT(ltxq);
329 		sta->txq[tid] = &ltxq->txq;
330 	}
331 
332 	/* Deflink information. */
333 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
334 		struct ieee80211_supported_band *supband;
335 
336 		supband = hw->wiphy->bands[band];
337 		if (supband == NULL)
338 			continue;
339 
340 		for (i = 0; i < supband->n_bitrates; i++) {
341 
342 			IMPROVE("Further supband->bitrates[i]* checks?");
343 			/* or should we get them from the ni? */
344 			sta->deflink.supp_rates[band] |= BIT(i);
345 		}
346 	}
347 
348 	sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
349 	sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
350 	sta->deflink.rx_nss = 1;
351 
352 	ht_rx_nss = 0;
353 #if defined(LKPI_80211_HT)
354 	lkpi_sta_sync_ht_from_ni(sta, ni, &ht_rx_nss);
355 #endif
356 	vht_rx_nss = 0;
357 #if defined(LKPI_80211_VHT)
358 	lkpi_sta_sync_vht_from_ni(sta, ni, &vht_rx_nss);
359 #endif
360 
361 	sta->deflink.rx_nss = MAX(ht_rx_nss, sta->deflink.rx_nss);
362 	sta->deflink.rx_nss = MAX(vht_rx_nss, sta->deflink.rx_nss);
363 	IMPROVE("he, ... smps_mode, ..");
364 
365 	/* Link configuration. */
366 	IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
367 	sta->link[0] = &sta->deflink;
368 	for (i = 1; i < nitems(sta->link); i++) {
369 		IMPROVE("more links; only link[0] = deflink currently.");
370 	}
371 
372 	/* Deferred TX path. */
373 	LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
374 	TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
375 	mbufq_init(&lsta->txq, IFQ_MAXLEN);
376 	lsta->txq_ready = true;
377 
378 	return (lsta);
379 
380 cleanup:
381 	for (; tid >= 0; tid--) {
382 		struct lkpi_txq *ltxq;
383 
384 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
385 		LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
386 		free(sta->txq[tid], M_LKPI80211);
387 	}
388 	free(lsta, M_LKPI80211);
389 	return (NULL);
390 }
391 
392 static void
lkpi_lsta_free(struct lkpi_sta * lsta,struct ieee80211_node * ni)393 lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
394 {
395 	struct mbuf *m;
396 
397 	if (lsta->added_to_drv)
398 		panic("%s: Trying to free an lsta still known to firmware: "
399 		    "lsta %p ni %p added_to_drv %d\n",
400 		    __func__, lsta, ni, lsta->added_to_drv);
401 
402 	/* XXX-BZ free resources, ... */
403 	IMPROVE();
404 
405 	/* Drain sta->txq[] */
406 
407 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
408 	lsta->txq_ready = false;
409 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
410 
411 	/* Drain taskq, won't be restarted until added_to_drv is set again. */
412 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
413 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
414 
415 	/* Flush mbufq (make sure to release ni refs!). */
416 	m = mbufq_dequeue(&lsta->txq);
417 	while (m != NULL) {
418 		struct ieee80211_node *nim;
419 
420 		nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
421 		if (nim != NULL)
422 			ieee80211_free_node(nim);
423 		m_freem(m);
424 		m = mbufq_dequeue(&lsta->txq);
425 	}
426 	KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
427 	    __func__, lsta, mbufq_len(&lsta->txq)));
428 	LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
429 
430 	/* Remove lsta from vif; that is done by the state machine.  Should assert it? */
431 
432 	IMPROVE("Make sure everything is cleaned up.");
433 
434 	/* Free lsta. */
435 	lsta->ni = NULL;
436 	ni->ni_drv_data = NULL;
437 	free(lsta, M_LKPI80211);
438 }
439 
440 
441 static enum nl80211_band
lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel * c)442 lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
443 {
444 
445 	if (IEEE80211_IS_CHAN_2GHZ(c))
446 		return (NL80211_BAND_2GHZ);
447 	else if (IEEE80211_IS_CHAN_5GHZ(c))
448 		return (NL80211_BAND_5GHZ);
449 #ifdef __notyet__
450 	else if ()
451 		return (NL80211_BAND_6GHZ);
452 	else if ()
453 		return (NL80211_BAND_60GHZ);
454 	else if (IEEE80211_IS_CHAN_GSM(c))
455 		return (NL80211_BAND_XXX);
456 #endif
457 	else
458 		panic("%s: unsupported band. c %p flags %#x\n",
459 		    __func__, c, c->ic_flags);
460 }
461 
462 static uint32_t
lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)463 lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
464 {
465 
466 	/* XXX-BZ this is just silly; net80211 is too convoluted. */
467 	/* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
468 	switch (band) {
469 	case NL80211_BAND_2GHZ:
470 		return (IEEE80211_CHAN_2GHZ);
471 		break;
472 	case NL80211_BAND_5GHZ:
473 		return (IEEE80211_CHAN_5GHZ);
474 		break;
475 	case NL80211_BAND_60GHZ:
476 		break;
477 	case NL80211_BAND_6GHZ:
478 		break;
479 	default:
480 		panic("%s: unsupported band %u\n", __func__, band);
481 		break;
482 	}
483 
484 	IMPROVE();
485 	return (0x00);
486 }
487 
488 #if 0
489 static enum ieee80211_ac_numbers
490 lkpi_ac_net_to_l80211(int ac)
491 {
492 
493 	switch (ac) {
494 	case WME_AC_VO:
495 		return (IEEE80211_AC_VO);
496 	case WME_AC_VI:
497 		return (IEEE80211_AC_VI);
498 	case WME_AC_BE:
499 		return (IEEE80211_AC_BE);
500 	case WME_AC_BK:
501 		return (IEEE80211_AC_BK);
502 	default:
503 		printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
504 		return (IEEE80211_AC_BE);
505 	}
506 }
507 #endif
508 
509 static enum nl80211_iftype
lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)510 lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
511 {
512 
513 	switch (opmode) {
514 	case IEEE80211_M_IBSS:
515 		return (NL80211_IFTYPE_ADHOC);
516 		break;
517 	case IEEE80211_M_STA:
518 		return (NL80211_IFTYPE_STATION);
519 		break;
520 	case IEEE80211_M_WDS:
521 		return (NL80211_IFTYPE_WDS);
522 		break;
523 	case IEEE80211_M_HOSTAP:
524 		return (NL80211_IFTYPE_AP);
525 		break;
526 	case IEEE80211_M_MONITOR:
527 		return (NL80211_IFTYPE_MONITOR);
528 		break;
529 	case IEEE80211_M_MBSS:
530 		return (NL80211_IFTYPE_MESH_POINT);
531 		break;
532 	case IEEE80211_M_AHDEMO:
533 		/* FALLTHROUGH */
534 	default:
535 		printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
536 		/* FALLTHROUGH */
537 	}
538 	return (NL80211_IFTYPE_UNSPECIFIED);
539 }
540 
541 #ifdef LKPI_80211_HW_CRYPTO
542 static uint32_t
lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)543 lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
544 {
545 
546 	switch (wlan_cipher_suite) {
547 	case WLAN_CIPHER_SUITE_WEP40:
548 		return (IEEE80211_CRYPTO_WEP);
549 	case WLAN_CIPHER_SUITE_TKIP:
550 		return (IEEE80211_CRYPTO_TKIP);
551 	case WLAN_CIPHER_SUITE_CCMP:
552 		return (IEEE80211_CRYPTO_AES_CCM);
553 	case WLAN_CIPHER_SUITE_WEP104:
554 		return (IEEE80211_CRYPTO_WEP);
555 	case WLAN_CIPHER_SUITE_AES_CMAC:
556 	case WLAN_CIPHER_SUITE_GCMP:
557 	case WLAN_CIPHER_SUITE_GCMP_256:
558 	case WLAN_CIPHER_SUITE_CCMP_256:
559 	case WLAN_CIPHER_SUITE_BIP_GMAC_128:
560 	case WLAN_CIPHER_SUITE_BIP_GMAC_256:
561 	case WLAN_CIPHER_SUITE_BIP_CMAC_256:
562 		printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
563 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
564 		break;
565 	default:
566 		printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
567 		    wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
568 	}
569 
570 	return (0);
571 }
572 
573 static uint32_t
lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher,uint8_t keylen)574 lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
575 {
576 
577 	switch (cipher) {
578 	case IEEE80211_CIPHER_TKIP:
579 		return (WLAN_CIPHER_SUITE_TKIP);
580 	case IEEE80211_CIPHER_AES_CCM:
581 		return (WLAN_CIPHER_SUITE_CCMP);
582 	case IEEE80211_CIPHER_WEP:
583 		if (keylen < 8)
584 			return (WLAN_CIPHER_SUITE_WEP40);
585 		else
586 			return (WLAN_CIPHER_SUITE_WEP104);
587 		break;
588 	case IEEE80211_CIPHER_AES_OCB:
589 	case IEEE80211_CIPHER_TKIPMIC:
590 	case IEEE80211_CIPHER_CKIP:
591 	case IEEE80211_CIPHER_NONE:
592 		printf("%s: unsupported cipher %#010x\n", __func__, cipher);
593 		break;
594 	default:
595 		printf("%s: unknown cipher %#010x\n", __func__, cipher);
596 	};
597 	return (0);
598 }
599 #endif
600 
601 #ifdef __notyet__
602 static enum ieee80211_sta_state
lkpi_net80211_state_to_sta_state(enum ieee80211_state state)603 lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
604 {
605 
606 	/*
607 	 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
608 	 * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
609 	 */
610 	switch (state) {
611 	case IEEE80211_S_INIT:
612 		return (IEEE80211_STA_NOTEXIST);
613 	case IEEE80211_S_SCAN:
614 		return (IEEE80211_STA_NONE);
615 	case IEEE80211_S_AUTH:
616 		return (IEEE80211_STA_AUTH);
617 	case IEEE80211_S_ASSOC:
618 		return (IEEE80211_STA_ASSOC);
619 	case IEEE80211_S_RUN:
620 		return (IEEE80211_STA_AUTHORIZED);
621 	case IEEE80211_S_CAC:
622 	case IEEE80211_S_CSA:
623 	case IEEE80211_S_SLEEP:
624 	default:
625 		UNIMPLEMENTED;
626 	};
627 
628 	return (IEEE80211_STA_NOTEXIST);
629 }
630 #endif
631 
632 static struct linuxkpi_ieee80211_channel *
lkpi_find_lkpi80211_chan(struct lkpi_hw * lhw,struct ieee80211_channel * c)633 lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
634     struct ieee80211_channel *c)
635 {
636 	struct ieee80211_hw *hw;
637 	struct linuxkpi_ieee80211_channel *channels;
638 	enum nl80211_band band;
639 	int i, nchans;
640 
641 	hw = LHW_TO_HW(lhw);
642 	band = lkpi_net80211_chan_to_nl80211_band(c);
643 	if (hw->wiphy->bands[band] == NULL)
644 		return (NULL);
645 
646 	nchans = hw->wiphy->bands[band]->n_channels;
647 	if (nchans <= 0)
648 		return (NULL);
649 
650 	channels = hw->wiphy->bands[band]->channels;
651 	for (i = 0; i < nchans; i++) {
652 		if (channels[i].hw_value == c->ic_ieee)
653 			return (&channels[i]);
654 	}
655 
656 	return (NULL);
657 }
658 
659 #if 0
660 static struct linuxkpi_ieee80211_channel *
661 lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
662 {
663 	struct linuxkpi_ieee80211_channel *chan;
664 	struct ieee80211_channel *c;
665 	struct lkpi_hw *lhw;
666 
667 	chan = NULL;
668 	if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
669 		c = ni->ni_chan;
670 	else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
671 		c = ic->ic_bsschan;
672 	else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
673 		c = ic->ic_curchan;
674 	else
675 		c = NULL;
676 
677 	if (c != NULL && c != IEEE80211_CHAN_ANYC) {
678 		lhw = ic->ic_softc;
679 		chan = lkpi_find_lkpi80211_chan(lhw, c);
680 	}
681 
682 	return (chan);
683 }
684 #endif
685 
686 struct linuxkpi_ieee80211_channel *
linuxkpi_ieee80211_get_channel(struct wiphy * wiphy,uint32_t freq)687 linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
688 {
689 	enum nl80211_band band;
690 
691 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
692 		struct ieee80211_supported_band *supband;
693 		struct linuxkpi_ieee80211_channel *channels;
694 		int i;
695 
696 		supband = wiphy->bands[band];
697 		if (supband == NULL || supband->n_channels == 0)
698 			continue;
699 
700 		channels = supband->channels;
701 		for (i = 0; i < supband->n_channels; i++) {
702 			if (channels[i].center_freq == freq)
703 				return (&channels[i]);
704 		}
705 	}
706 
707 	return (NULL);
708 }
709 
710 #ifdef LKPI_80211_HW_CRYPTO
711 static int
_lkpi_iv_key_set_delete(struct ieee80211vap * vap,const struct ieee80211_key * k,enum set_key_cmd cmd)712 _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
713     enum set_key_cmd cmd)
714 {
715 	struct ieee80211com *ic;
716 	struct lkpi_hw *lhw;
717 	struct ieee80211_hw *hw;
718 	struct lkpi_vif *lvif;
719 	struct ieee80211_vif *vif;
720 	struct ieee80211_sta *sta;
721 	struct ieee80211_node *ni;
722 	struct ieee80211_key_conf *kc;
723 	int error;
724 
725 	/* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
726 
727 	ic = vap->iv_ic;
728 	lhw = ic->ic_softc;
729 	hw = LHW_TO_HW(lhw);
730 	lvif = VAP_TO_LVIF(vap);
731 	vif = LVIF_TO_VIF(lvif);
732 
733 	memset(&kc, 0, sizeof(kc));
734 	kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
735 	kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
736 	    k->wk_cipher->ic_cipher, k->wk_keylen);
737 	kc->keyidx = k->wk_keyix;
738 #if 0
739 	kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
740 #endif
741 	atomic64_set(&kc->tx_pn, k->wk_keytsc);
742 	kc->keylen = k->wk_keylen;
743 	memcpy(kc->key, k->wk_key, k->wk_keylen);
744 
745 	switch (kc->cipher) {
746 	case WLAN_CIPHER_SUITE_CCMP:
747 		kc->iv_len = k->wk_cipher->ic_header;
748 		kc->icv_len = k->wk_cipher->ic_trailer;
749 		break;
750 	case WLAN_CIPHER_SUITE_TKIP:
751 	default:
752 		IMPROVE();
753 		return (0);
754 	};
755 
756 	ni = vap->iv_bss;
757 	sta = ieee80211_find_sta(vif, ni->ni_bssid);
758 	if (sta != NULL) {
759 		struct lkpi_sta *lsta;
760 
761 		lsta = STA_TO_LSTA(sta);
762 		lsta->kc = kc;
763 	}
764 
765 	error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
766 	if (error != 0) {
767 		/* XXX-BZ leaking kc currently */
768 		ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
769 		return (0);
770 	} else {
771 		ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
772 		    "flags %#10x\n", __func__,
773 		    kc->keyidx, kc->hw_key_idx, kc->flags);
774 		return (1);
775 	}
776 }
777 
778 static int
lkpi_iv_key_delete(struct ieee80211vap * vap,const struct ieee80211_key * k)779 lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
780 {
781 
782 	/* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
783 	return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
784 }
785 static  int
lkpi_iv_key_set(struct ieee80211vap * vap,const struct ieee80211_key * k)786 lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
787 {
788 
789 	return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
790 }
791 #endif
792 
793 static u_int
lkpi_ic_update_mcast_copy(void * arg,struct sockaddr_dl * sdl,u_int cnt)794 lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
795 {
796 	struct netdev_hw_addr_list *mc_list;
797 	struct netdev_hw_addr *addr;
798 
799 	KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
800 	    __func__, arg, sdl, cnt));
801 
802 	mc_list = arg;
803 	/* If it is on the list already skip it. */
804 	netdev_hw_addr_list_for_each(addr, mc_list) {
805 		if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
806 			return (0);
807 	}
808 
809 	addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
810 	if (addr == NULL)
811 		return (0);
812 
813 	INIT_LIST_HEAD(&addr->addr_list);
814 	memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
815 	/* XXX this should be a netdev function? */
816 	list_add(&addr->addr_list, &mc_list->addr_list);
817 	mc_list->count++;
818 
819 #ifdef LINUXKPI_DEBUG_80211
820 	if (linuxkpi_debug_80211 & D80211_TRACE)
821 		printf("%s:%d: mc_list count %d: added %6D\n",
822 		    __func__, __LINE__, mc_list->count, addr->addr, ":");
823 #endif
824 
825 	return (1);
826 }
827 
828 static void
lkpi_update_mcast_filter(struct ieee80211com * ic,bool force)829 lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
830 {
831 	struct lkpi_hw *lhw;
832 	struct ieee80211_hw *hw;
833 	struct netdev_hw_addr_list mc_list;
834 	struct list_head *le, *next;
835 	struct netdev_hw_addr *addr;
836 	struct ieee80211vap *vap;
837 	u64 mc;
838 	unsigned int changed_flags, total_flags;
839 
840 	lhw = ic->ic_softc;
841 
842 	if (lhw->ops->prepare_multicast == NULL ||
843 	    lhw->ops->configure_filter == NULL)
844 		return;
845 
846 	if (!lhw->update_mc && !force)
847 		return;
848 
849 	changed_flags = total_flags = 0;
850 	mc_list.count = 0;
851 	INIT_LIST_HEAD(&mc_list.addr_list);
852 	if (ic->ic_allmulti == 0) {
853 		TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
854 			if_foreach_llmaddr(vap->iv_ifp,
855 			    lkpi_ic_update_mcast_copy, &mc_list);
856 	} else {
857 		changed_flags |= FIF_ALLMULTI;
858 	}
859 
860 	hw = LHW_TO_HW(lhw);
861 	mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
862 	/*
863 	 * XXX-BZ make sure to get this sorted what is a change,
864 	 * what gets all set; what was already set?
865 	 */
866 	total_flags = changed_flags;
867 	lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
868 
869 #ifdef LINUXKPI_DEBUG_80211
870 	if (linuxkpi_debug_80211 & D80211_TRACE)
871 		printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
872 		    __func__, changed_flags, mc_list.count, total_flags);
873 #endif
874 
875 	if (mc_list.count != 0) {
876 		list_for_each_safe(le, next, &mc_list.addr_list) {
877 			addr = list_entry(le, struct netdev_hw_addr, addr_list);
878 			free(addr, M_LKPI80211);
879 			mc_list.count--;
880 		}
881 	}
882 	KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
883 	    __func__, &mc_list, mc_list.count));
884 }
885 
886 static enum ieee80211_bss_changed
lkpi_update_dtim_tsf(struct ieee80211_vif * vif,struct ieee80211_node * ni,struct ieee80211vap * vap,const char * _f,int _l)887 lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
888     struct ieee80211vap *vap, const char *_f, int _l)
889 {
890 	enum ieee80211_bss_changed bss_changed;
891 
892 	bss_changed = 0;
893 
894 #ifdef LINUXKPI_DEBUG_80211
895 	if (linuxkpi_debug_80211 & D80211_TRACE)
896 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
897 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
898 		    "sync_device_ts %u bss_changed %#010jx\n",
899 			__func__, __LINE__, _f, _l,
900 			vif->cfg.assoc, vif->cfg.aid,
901 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
902 			vif->bss_conf.sync_dtim_count,
903 			(uintmax_t)vif->bss_conf.sync_tsf,
904 			vif->bss_conf.sync_device_ts,
905 			(uintmax_t)bss_changed);
906 #endif
907 
908 	if (vif->bss_conf.beacon_int != ni->ni_intval) {
909 		vif->bss_conf.beacon_int = ni->ni_intval;
910 		/* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
911 		if (vif->bss_conf.beacon_int < 16)
912 			vif->bss_conf.beacon_int = 16;
913 		bss_changed |= BSS_CHANGED_BEACON_INT;
914 	}
915 	if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
916 	    vap->iv_dtim_period > 0) {
917 		vif->bss_conf.dtim_period = vap->iv_dtim_period;
918 		bss_changed |= BSS_CHANGED_BEACON_INFO;
919 	}
920 
921 	vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
922 	vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
923 	/* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
924 
925 #ifdef LINUXKPI_DEBUG_80211
926 	if (linuxkpi_debug_80211 & D80211_TRACE)
927 		printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
928 		    "dtim_period %u sync_dtim_count %u sync_tsf %ju "
929 		    "sync_device_ts %u bss_changed %#010jx\n",
930 			__func__, __LINE__, _f, _l,
931 			vif->cfg.assoc, vif->cfg.aid,
932 			vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
933 			vif->bss_conf.sync_dtim_count,
934 			(uintmax_t)vif->bss_conf.sync_tsf,
935 			vif->bss_conf.sync_device_ts,
936 			(uintmax_t)bss_changed);
937 #endif
938 
939 	return (bss_changed);
940 }
941 
942 static void
lkpi_stop_hw_scan(struct lkpi_hw * lhw,struct ieee80211_vif * vif)943 lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
944 {
945 	struct ieee80211_hw *hw;
946 	int error;
947 	bool cancel;
948 
949 	LKPI_80211_LHW_SCAN_LOCK(lhw);
950 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
951 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
952 	if (!cancel)
953 		return;
954 
955 	hw = LHW_TO_HW(lhw);
956 
957 	IEEE80211_UNLOCK(lhw->ic);
958 	LKPI_80211_LHW_LOCK(lhw);
959 	/* Need to cancel the scan. */
960 	lkpi_80211_mo_cancel_hw_scan(hw, vif);
961 	LKPI_80211_LHW_UNLOCK(lhw);
962 
963 	/* Need to make sure we see ieee80211_scan_completed. */
964 	LKPI_80211_LHW_SCAN_LOCK(lhw);
965 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
966 		error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
967 	cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
968 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
969 
970 	IEEE80211_LOCK(lhw->ic);
971 
972 	if (cancel)
973 		ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
974 		    __func__, error, lhw, vif);
975 }
976 
977 static void
lkpi_hw_conf_idle(struct ieee80211_hw * hw,bool new)978 lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
979 {
980 	struct lkpi_hw *lhw;
981 	int error;
982 	bool old;
983 
984 	old = hw->conf.flags & IEEE80211_CONF_IDLE;
985 	if (old == new)
986 		return;
987 
988 	hw->conf.flags ^= IEEE80211_CONF_IDLE;
989 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
990 	if (error != 0 && error != EOPNOTSUPP) {
991 		lhw = HW_TO_LHW(hw);
992 		ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
993 		    __func__, IEEE80211_CONF_CHANGE_IDLE, error);
994 	}
995 }
996 
997 static enum ieee80211_bss_changed
lkpi_disassoc(struct ieee80211_sta * sta,struct ieee80211_vif * vif,struct lkpi_hw * lhw)998 lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
999     struct lkpi_hw *lhw)
1000 {
1001 	enum ieee80211_bss_changed changed;
1002 
1003 	changed = 0;
1004 	sta->aid = 0;
1005 	if (vif->cfg.assoc) {
1006 
1007 		lhw->update_mc = true;
1008 		lkpi_update_mcast_filter(lhw->ic, true);
1009 
1010 		vif->cfg.assoc = false;
1011 		vif->cfg.aid = 0;
1012 		changed |= BSS_CHANGED_ASSOC;
1013 		IMPROVE();
1014 
1015 		/*
1016 		 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
1017 		 * assoc = false right away here will remove the sta from
1018 		 * firmware for iwlwifi.
1019 		 * We no longer do this but only return the BSS_CHNAGED value.
1020 		 * The caller is responsible for removing the sta gong to
1021 		 * IEEE80211_STA_NOTEXIST and then executing the
1022 		 * bss_info_changed() update.
1023 		 * See lkpi_sta_run_to_init() for more detailed comment.
1024 		 */
1025 	}
1026 
1027 	return (changed);
1028 }
1029 
1030 static void
lkpi_wake_tx_queues(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool dequeue_seen,bool no_emptyq)1031 lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
1032     bool dequeue_seen, bool no_emptyq)
1033 {
1034 	struct lkpi_txq *ltxq;
1035 	int tid;
1036 	bool ltxq_empty;
1037 
1038 	/* Wake up all queues to know they are allocated in the driver. */
1039 	for (tid = 0; tid < nitems(sta->txq); tid++) {
1040 
1041 		if (tid == IEEE80211_NUM_TIDS) {
1042 			IMPROVE("station specific?");
1043 			if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
1044 				continue;
1045 		} else if (tid >= hw->queues)
1046 			continue;
1047 
1048 		if (sta->txq[tid] == NULL)
1049 			continue;
1050 
1051 		ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
1052 		if (dequeue_seen && !ltxq->seen_dequeue)
1053 			continue;
1054 
1055 		LKPI_80211_LTXQ_LOCK(ltxq);
1056 		ltxq_empty = skb_queue_empty(&ltxq->skbq);
1057 		LKPI_80211_LTXQ_UNLOCK(ltxq);
1058 		if (no_emptyq && ltxq_empty)
1059 			continue;
1060 
1061 		lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
1062 	}
1063 }
1064 
1065 /*
1066  * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
1067  * packet.  The problem is that the state machine functions tend to hold the
1068  * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
1069  * We call this after dropping the ic lock and before acquiring the LHW lock.
1070  * we make sure no further packets are queued and if they are queued the task
1071  * will finish or be cancelled.  At the end if a packet is left we manually
1072  * send it.  scan_to_auth() would re-enable sending if the lsta would be
1073  * re-used.
1074  */
1075 static void
lkpi_80211_flush_tx(struct lkpi_hw * lhw,struct lkpi_sta * lsta)1076 lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
1077 {
1078 	struct mbufq mq;
1079 	struct mbuf *m;
1080 	int len;
1081 
1082 	LKPI_80211_LHW_UNLOCK_ASSERT(lhw);
1083 
1084 	/* Do not accept any new packets until scan_to_auth or lsta_free(). */
1085 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
1086 	lsta->txq_ready = false;
1087 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
1088 
1089 	while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
1090 		taskqueue_drain(taskqueue_thread, &lsta->txq_task);
1091 
1092 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
1093 	len = mbufq_len(&lsta->txq);
1094 	if (len <= 0) {
1095 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
1096 		return;
1097 	}
1098 
1099 	mbufq_init(&mq, IFQ_MAXLEN);
1100 	mbufq_concat(&mq, &lsta->txq);
1101 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
1102 
1103 	m = mbufq_dequeue(&mq);
1104 	while (m != NULL) {
1105 		lkpi_80211_txq_tx_one(lsta, m);
1106 		m = mbufq_dequeue(&mq);
1107 	}
1108 }
1109 
1110 /* -------------------------------------------------------------------------- */
1111 
1112 static int
lkpi_sta_state_do_nada(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1113 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1114 {
1115 
1116 	return (0);
1117 }
1118 
1119 /* lkpi_iv_newstate() handles the stop scan case generally. */
1120 #define	lkpi_sta_scan_to_init(_v, _n, _a)	lkpi_sta_state_do_nada(_v, _n, _a)
1121 
1122 static int
lkpi_sta_scan_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1123 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1124 {
1125 	struct linuxkpi_ieee80211_channel *chan;
1126 	struct lkpi_chanctx *lchanctx;
1127 	struct ieee80211_chanctx_conf *chanctx_conf;
1128 	struct lkpi_hw *lhw;
1129 	struct ieee80211_hw *hw;
1130 	struct lkpi_vif *lvif;
1131 	struct ieee80211_vif *vif;
1132 	struct ieee80211_node *ni;
1133 	struct lkpi_sta *lsta;
1134 	enum ieee80211_bss_changed bss_changed;
1135 	struct ieee80211_prep_tx_info prep_tx_info;
1136 	uint32_t changed;
1137 	int error;
1138 
1139 	/*
1140 	 * In here we use vap->iv_bss until lvif->lvif_bss is set.
1141 	 * For all later (STATE >= AUTH) functions we need to use the lvif
1142 	 * cache which will be tracked even through (*iv_update_bss)().
1143 	 */
1144 
1145 	if (vap->iv_bss == NULL) {
1146 		ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
1147 		return (EINVAL);
1148 	}
1149 	/*
1150 	 * Keep the ni alive locally.  In theory (and practice) iv_bss can change
1151 	 * once we unlock here.  This is due to net80211 allowing state changes
1152 	 * and new join1() despite having an active node as well as due to
1153 	 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
1154 	 */
1155 	ni = ieee80211_ref_node(vap->iv_bss);
1156 	if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
1157 		ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
1158 		    "on vap %p\n", __func__, ni, vap);
1159 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
1160 		return (EINVAL);
1161 	}
1162 
1163 	lhw = vap->iv_ic->ic_softc;
1164 	chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
1165 	if (chan == NULL) {
1166 		ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
1167 		    "iv_bss ni %p on vap %p\n", __func__, ni, vap);
1168 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
1169 		return (ESRCH);
1170 	}
1171 
1172 	hw = LHW_TO_HW(lhw);
1173 	lvif = VAP_TO_LVIF(vap);
1174 	vif = LVIF_TO_VIF(lvif);
1175 
1176 	LKPI_80211_LVIF_LOCK(lvif);
1177 	/* XXX-BZ KASSERT later? */
1178 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
1179 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1180 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1181 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1182 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1183 		    lvif->lvif_bss_synched);
1184 		LKPI_80211_LVIF_UNLOCK(lvif);
1185 		ieee80211_free_node(ni);	/* Error handling for the local ni. */
1186 		return (EBUSY);
1187 	}
1188 	LKPI_80211_LVIF_UNLOCK(lvif);
1189 
1190 	IEEE80211_UNLOCK(vap->iv_ic);
1191 	LKPI_80211_LHW_LOCK(lhw);
1192 
1193 	/* Add chanctx (or if exists, change it). */
1194 	if (vif->chanctx_conf != NULL) {
1195 		chanctx_conf = vif->chanctx_conf;
1196 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1197 		IMPROVE("diff changes for changed, working on live copy, rcu");
1198 	} else {
1199 		/* Keep separate alloc as in Linux this is rcu managed? */
1200 		lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
1201 		    M_LKPI80211, M_WAITOK | M_ZERO);
1202 		chanctx_conf = &lchanctx->chanctx_conf;
1203 	}
1204 
1205 	chanctx_conf->rx_chains_dynamic = 1;
1206 	chanctx_conf->rx_chains_static = 1;
1207 	chanctx_conf->radar_enabled =
1208 	    (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
1209 	chanctx_conf->def.chan = chan;
1210 	chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
1211 	chanctx_conf->def.center_freq1 = chan->center_freq;
1212 	chanctx_conf->def.center_freq2 = 0;
1213 	IMPROVE("Check vht_cap from band not just chan?");
1214 	KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
1215 	   ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
1216 #ifdef LKPI_80211_HT
1217 	if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) {
1218 		if (IEEE80211_IS_CHAN_HT40(ni->ni_chan)) {
1219 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_40;
1220 		} else
1221 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_20;
1222 	}
1223 #endif
1224 #ifdef LKPI_80211_VHT
1225 	if (IEEE80211_IS_CHAN_VHT(ni->ni_chan)) {
1226 #ifdef __notyet__
1227 		if (IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan)) {
1228 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80P80;
1229 			chanctx_conf->def.center_freq2 = 0;	/* XXX */
1230 		} else
1231 #endif
1232 		if (IEEE80211_IS_CHAN_VHT160(ni->ni_chan))
1233 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_160;
1234 		else if (IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
1235 			chanctx_conf->def.width = NL80211_CHAN_WIDTH_80;
1236 	}
1237 #endif
1238 	/* Responder ... */
1239 	chanctx_conf->min_def.chan = chan;
1240 	chanctx_conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
1241 	chanctx_conf->min_def.center_freq1 = chan->center_freq;
1242 	chanctx_conf->min_def.center_freq2 = 0;
1243 	IMPROVE("currently 20_NOHT min_def only");
1244 
1245 	/* Set bss info (bss_info_changed). */
1246 	bss_changed = 0;
1247 	vif->bss_conf.bssid = ni->ni_bssid;
1248 	bss_changed |= BSS_CHANGED_BSSID;
1249 	vif->bss_conf.txpower = ni->ni_txpower;
1250 	bss_changed |= BSS_CHANGED_TXPOWER;
1251 	vif->cfg.idle = false;
1252 	bss_changed |= BSS_CHANGED_IDLE;
1253 
1254 	/* vif->bss_conf.basic_rates ? Where exactly? */
1255 
1256 	/* Should almost assert it is this. */
1257 	vif->cfg.assoc = false;
1258 	vif->cfg.aid = 0;
1259 
1260 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1261 
1262 	error = 0;
1263 	if (vif->chanctx_conf != NULL) {
1264 		changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
1265 		changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
1266 		changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
1267 		changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
1268 		lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
1269 	} else {
1270 		error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
1271 		if (error == 0 || error == EOPNOTSUPP) {
1272 			vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
1273 			vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
1274 			vif->bss_conf.chanreq.oper.center_freq1 =
1275 			    chanctx_conf->def.center_freq1;
1276 #ifdef LKPI_80211_HT
1277 			if (vif->bss_conf.chanreq.oper.width == NL80211_CHAN_WIDTH_40) {
1278 				/* Note: it is 10 not 20. */
1279 				if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan))
1280 					vif->bss_conf.chanreq.oper.center_freq1 += 10;
1281 				else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan))
1282 					vif->bss_conf.chanreq.oper.center_freq1 -= 10;
1283 			}
1284 #endif
1285 			vif->bss_conf.chanreq.oper.center_freq2 =
1286 			    chanctx_conf->def.center_freq2;
1287 		} else {
1288 			ic_printf(vap->iv_ic, "%s:%d: mo_add_chanctx "
1289 			    "failed: %d\n", __func__, __LINE__, error);
1290 			goto out;
1291 		}
1292 
1293 		vif->bss_conf.chanctx_conf = chanctx_conf;
1294 
1295 		/* Assign vif chanctx. */
1296 		if (error == 0)
1297 			error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
1298 			    &vif->bss_conf, chanctx_conf);
1299 		if (error == EOPNOTSUPP)
1300 			error = 0;
1301 		if (error != 0) {
1302 			ic_printf(vap->iv_ic, "%s:%d: mo_assign_vif_chanctx "
1303 			    "failed: %d\n", __func__, __LINE__, error);
1304 			lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1305 			lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1306 			free(lchanctx, M_LKPI80211);
1307 			goto out;
1308 		}
1309 	}
1310 	IMPROVE("update radiotap chan fields too");
1311 
1312 	/* RATES */
1313 	IMPROVE("bss info: not all needs to come now and rates are missing");
1314 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1315 
1316 	/*
1317 	 * Given ni and lsta are 1:1 from alloc to free we can assert that
1318 	 * ni always has lsta data attach despite net80211 node swapping
1319 	 * under the hoods.
1320 	 */
1321 	KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
1322 	    __func__, ni, ni->ni_drv_data));
1323 	lsta = ni->ni_drv_data;
1324 
1325 	/*
1326 	 * Make sure in case the sta did not change and we re-add it,
1327 	 * that we can tx again.
1328 	 */
1329 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
1330 	lsta->txq_ready = true;
1331 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
1332 
1333 	LKPI_80211_LVIF_LOCK(lvif);
1334 	/* Insert the [l]sta into the list of known stations. */
1335 	TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1336 	LKPI_80211_LVIF_UNLOCK(lvif);
1337 
1338 	/* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
1339 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1340 	KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
1341 	    "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1342 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1343 	if (error != 0) {
1344 		IMPROVE("do we need to undo the chan ctx?");
1345 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1346 		    "failed: %d\n", __func__, __LINE__, error);
1347 		goto out;
1348 	}
1349 #if 0
1350 	lsta->added_to_drv = true;	/* mo manages. */
1351 #endif
1352 
1353 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1354 
1355 #if 0
1356 	/*
1357 	 * Wakeup all queues now that sta is there so we have as much time to
1358 	 * possibly prepare the queue in the driver to be ready for the 1st
1359 	 * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
1360 	 * is no guarantee or way to check.
1361 	 * XXX-BZ and by now we know that this does not work on all drivers
1362 	 * for all queues.
1363 	 */
1364 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
1365 #endif
1366 
1367 	/* Start mgd_prepare_tx. */
1368 	memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1369 	prep_tx_info.duration = PREP_TX_INFO_DURATION;
1370 	lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1371 	lsta->in_mgd = true;
1372 
1373 	/*
1374 	 * What is going to happen next:
1375 	 * - <twiddle> .. we should end up in "auth_to_assoc"
1376 	 * - event_callback
1377 	 * - update sta_state (NONE to AUTH)
1378 	 * - mgd_complete_tx
1379 	 * (ideally we'd do that on a callback for something else ...)
1380 	 */
1381 
1382 	LKPI_80211_LHW_UNLOCK(lhw);
1383 	IEEE80211_LOCK(vap->iv_ic);
1384 
1385 	LKPI_80211_LVIF_LOCK(lvif);
1386 	/* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
1387 	if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
1388 	    lsta->ni != vap->iv_bss)
1389 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1390 		    "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
1391 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1392 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1393 		    lvif->lvif_bss_synched, ni, lsta);
1394 
1395 	/*
1396 	 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
1397 	 * Given we cache lsta we use lsta->ni instead of ni here (even though
1398 	 * lsta->ni == ni) to be distinct from the rest of the code where we do
1399 	 * assume that ni == vap->iv_bss which it may or may not be.
1400 	 * So do NOT use iv_bss here anymore as that may have diverged from our
1401 	 * function local ni already while ic was unlocked and would lead to
1402 	 * inconsistencies.  Go and see if we lost a race and do not update
1403 	 * lvif_bss_synched in that case.
1404 	 */
1405 	ieee80211_ref_node(lsta->ni);
1406 	lvif->lvif_bss = lsta;
1407 	if (lsta->ni == vap->iv_bss) {
1408 		lvif->lvif_bss_synched = true;
1409 	} else {
1410 		/* Set to un-synched no matter what. */
1411 		lvif->lvif_bss_synched = false;
1412 		/*
1413 		 * We do not error as someone has to take us down.
1414 		 * If we are followed by a 2nd, new net80211::join1() going to
1415 		 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
1416 		 * will take the lvif->lvif_bss node down eventually.
1417 		 * What happens with the vap->iv_bss node will entirely be up
1418 		 * to net80211 as we never used the node beyond alloc()/free()
1419 		 * and we do not hold an extra reference for that anymore given
1420 		 * ni : lsta == 1:1.
1421 		 */
1422 	}
1423 	LKPI_80211_LVIF_UNLOCK(lvif);
1424 	goto out_relocked;
1425 
1426 out:
1427 	LKPI_80211_LHW_UNLOCK(lhw);
1428 	IEEE80211_LOCK(vap->iv_ic);
1429 out_relocked:
1430 	/*
1431 	 * Release the reference that kept the ni stable locally
1432 	 * during the work of this function.
1433 	 */
1434 	if (ni != NULL)
1435 		ieee80211_free_node(ni);
1436 	return (error);
1437 }
1438 
1439 static int
lkpi_sta_auth_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1440 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1441 {
1442 	struct lkpi_hw *lhw;
1443 	struct ieee80211_hw *hw;
1444 	struct lkpi_vif *lvif;
1445 	struct ieee80211_vif *vif;
1446 	struct ieee80211_node *ni;
1447 	struct lkpi_sta *lsta;
1448 	struct ieee80211_sta *sta;
1449 	struct ieee80211_prep_tx_info prep_tx_info;
1450 	int error;
1451 
1452 	lhw = vap->iv_ic->ic_softc;
1453 	hw = LHW_TO_HW(lhw);
1454 	lvif = VAP_TO_LVIF(vap);
1455 	vif = LVIF_TO_VIF(lvif);
1456 
1457 	LKPI_80211_LVIF_LOCK(lvif);
1458 #ifdef LINUXKPI_DEBUG_80211
1459 	/* XXX-BZ KASSERT later; state going down so no action. */
1460 	if (lvif->lvif_bss == NULL)
1461 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1462 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1463 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1464 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1465 		    lvif->lvif_bss_synched);
1466 #endif
1467 
1468 	lsta = lvif->lvif_bss;
1469 	LKPI_80211_LVIF_UNLOCK(lvif);
1470 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1471 	    "lvif %p vap %p\n", __func__,
1472 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1473 	ni = lsta->ni;			/* Reference held for lvif_bss. */
1474 	sta = LSTA_TO_STA(lsta);
1475 
1476 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1477 
1478 	IEEE80211_UNLOCK(vap->iv_ic);
1479 	LKPI_80211_LHW_LOCK(lhw);
1480 
1481 	/* flush, drop. */
1482 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1483 
1484 	/* Wake tx queues to get packet(s) out. */
1485 	lkpi_wake_tx_queues(hw, sta, false, true);
1486 
1487 	/* flush, no drop */
1488 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1489 
1490 	/* End mgd_complete_tx. */
1491 	if (lsta->in_mgd) {
1492 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1493 		prep_tx_info.success = false;
1494 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1495 		lsta->in_mgd = false;
1496 	}
1497 
1498 	/* sync_rx_queues */
1499 	lkpi_80211_mo_sync_rx_queues(hw);
1500 
1501 	/* sta_pre_rcu_remove */
1502         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1503 
1504 	/* Take the station down. */
1505 
1506 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1507 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1508 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1509 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1510 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1511 	if (error != 0) {
1512 		IMPROVE("do we need to undo the chan ctx?");
1513 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1514 		    "failed: %d\n", __func__, __LINE__, error);
1515 		goto out;
1516 	}
1517 #if 0
1518 	lsta->added_to_drv = false;	/* mo manages. */
1519 #endif
1520 
1521 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1522 
1523 	LKPI_80211_LVIF_LOCK(lvif);
1524 	/* Remove ni reference for this cache of lsta. */
1525 	lvif->lvif_bss = NULL;
1526 	lvif->lvif_bss_synched = false;
1527 	LKPI_80211_LVIF_UNLOCK(lvif);
1528 	lkpi_lsta_remove(lsta, lvif);
1529 	/*
1530 	 * The very last release the reference on the ni for the ni/lsta on
1531 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
1532 	 * and potentially freed.
1533 	 */
1534 	ieee80211_free_node(ni);
1535 
1536 	/* conf_tx */
1537 
1538 	/* Take the chan ctx down. */
1539 	if (vif->chanctx_conf != NULL) {
1540 		struct lkpi_chanctx *lchanctx;
1541 		struct ieee80211_chanctx_conf *chanctx_conf;
1542 
1543 		chanctx_conf = vif->chanctx_conf;
1544 		/* Remove vif context. */
1545 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1546 		/* NB: vif->chanctx_conf is NULL now. */
1547 
1548 		lkpi_hw_conf_idle(hw, true);
1549 
1550 		/* Remove chan ctx. */
1551 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1552 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1553 		free(lchanctx, M_LKPI80211);
1554 	}
1555 
1556 out:
1557 	LKPI_80211_LHW_UNLOCK(lhw);
1558 	IEEE80211_LOCK(vap->iv_ic);
1559 	return (error);
1560 }
1561 
1562 static int
lkpi_sta_auth_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1563 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1564 {
1565 	int error;
1566 
1567 	error = lkpi_sta_auth_to_scan(vap, nstate, arg);
1568 	if (error == 0)
1569 		error = lkpi_sta_scan_to_init(vap, nstate, arg);
1570 	return (error);
1571 }
1572 
1573 static int
lkpi_sta_auth_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1574 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1575 {
1576 	struct lkpi_hw *lhw;
1577 	struct ieee80211_hw *hw;
1578 	struct lkpi_vif *lvif;
1579 	struct ieee80211_vif *vif;
1580 	struct lkpi_sta *lsta;
1581 	struct ieee80211_prep_tx_info prep_tx_info;
1582 	int error;
1583 
1584 	lhw = vap->iv_ic->ic_softc;
1585 	hw = LHW_TO_HW(lhw);
1586 	lvif = VAP_TO_LVIF(vap);
1587 	vif = LVIF_TO_VIF(lvif);
1588 
1589 	IEEE80211_UNLOCK(vap->iv_ic);
1590 	LKPI_80211_LHW_LOCK(lhw);
1591 
1592 	LKPI_80211_LVIF_LOCK(lvif);
1593 	/* XXX-BZ KASSERT later? */
1594 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
1595 #ifdef LINUXKPI_DEBUG_80211
1596 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1597 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1598 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1599 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1600 		    lvif->lvif_bss_synched);
1601 #endif
1602 		error = ENOTRECOVERABLE;
1603 		LKPI_80211_LVIF_UNLOCK(lvif);
1604 		goto out;
1605 	}
1606 	lsta = lvif->lvif_bss;
1607 	LKPI_80211_LVIF_UNLOCK(lvif);
1608 
1609 	KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
1610 
1611 	/* Finish auth. */
1612 	IMPROVE("event callback");
1613 
1614 	/* Update sta_state (NONE to AUTH). */
1615 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1616 	    "NONE: %#x\n", __func__, lsta, lsta->state));
1617 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
1618 	if (error != 0) {
1619 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
1620 		    "failed: %d\n", __func__, __LINE__, error);
1621 		goto out;
1622 	}
1623 
1624 	/* End mgd_complete_tx. */
1625 	if (lsta->in_mgd) {
1626 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1627 		prep_tx_info.success = true;
1628 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1629 		lsta->in_mgd = false;
1630 	}
1631 
1632 	/* Now start assoc. */
1633 
1634 	/* Start mgd_prepare_tx. */
1635 	if (!lsta->in_mgd) {
1636 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1637 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1638 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1639 		lsta->in_mgd = true;
1640 	}
1641 
1642 	/* Wake tx queue to get packet out. */
1643 	lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
1644 
1645 	/*
1646 	 * <twiddle> .. we end up in "assoc_to_run"
1647 	 * - update sta_state (AUTH to ASSOC)
1648 	 * - conf_tx [all]
1649 	 * - bss_info_changed (assoc, aid, ssid, ..)
1650 	 * - change_chanctx (if needed)
1651 	 * - event_callback
1652 	 * - mgd_complete_tx
1653 	 */
1654 
1655 out:
1656 	LKPI_80211_LHW_UNLOCK(lhw);
1657 	IEEE80211_LOCK(vap->iv_ic);
1658 	return (error);
1659 }
1660 
1661 /* auth_to_auth, assoc_to_assoc. */
1662 static int
lkpi_sta_a_to_a(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1663 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1664 {
1665 	struct lkpi_hw *lhw;
1666 	struct ieee80211_hw *hw;
1667 	struct lkpi_vif *lvif;
1668 	struct ieee80211_vif *vif;
1669 	struct lkpi_sta *lsta;
1670 	struct ieee80211_prep_tx_info prep_tx_info;
1671 	int error;
1672 
1673 	lhw = vap->iv_ic->ic_softc;
1674 	hw = LHW_TO_HW(lhw);
1675 	lvif = VAP_TO_LVIF(vap);
1676 	vif = LVIF_TO_VIF(lvif);
1677 
1678 	IEEE80211_UNLOCK(vap->iv_ic);
1679 	LKPI_80211_LHW_LOCK(lhw);
1680 
1681 	LKPI_80211_LVIF_LOCK(lvif);
1682 	/* XXX-BZ KASSERT later? */
1683 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
1684 #ifdef LINUXKPI_DEBUG_80211
1685 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1686 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1687 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1688 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1689 		    lvif->lvif_bss_synched);
1690 #endif
1691 		LKPI_80211_LVIF_UNLOCK(lvif);
1692 		error = ENOTRECOVERABLE;
1693 		goto out;
1694 	}
1695 	lsta = lvif->lvif_bss;
1696 	LKPI_80211_LVIF_UNLOCK(lvif);
1697 
1698 	KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
1699 	    lsta, lvif, vap));
1700 
1701 	IMPROVE("event callback?");
1702 
1703 	/* End mgd_complete_tx. */
1704 	if (lsta->in_mgd) {
1705 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1706 		prep_tx_info.success = false;
1707 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1708 		lsta->in_mgd = false;
1709 	}
1710 
1711 	/* Now start assoc. */
1712 
1713 	/* Start mgd_prepare_tx. */
1714 	if (!lsta->in_mgd) {
1715 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1716 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1717 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1718 		lsta->in_mgd = true;
1719 	}
1720 
1721 	error = 0;
1722 out:
1723 	LKPI_80211_LHW_UNLOCK(lhw);
1724 	IEEE80211_LOCK(vap->iv_ic);
1725 
1726 	return (error);
1727 }
1728 
1729 static int
_lkpi_sta_assoc_to_down(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1730 _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1731 {
1732 	struct lkpi_hw *lhw;
1733 	struct ieee80211_hw *hw;
1734 	struct lkpi_vif *lvif;
1735 	struct ieee80211_vif *vif;
1736 	struct ieee80211_node *ni;
1737 	struct lkpi_sta *lsta;
1738 	struct ieee80211_sta *sta;
1739 	struct ieee80211_prep_tx_info prep_tx_info;
1740 	enum ieee80211_bss_changed bss_changed;
1741 	int error;
1742 
1743 	lhw = vap->iv_ic->ic_softc;
1744 	hw = LHW_TO_HW(lhw);
1745 	lvif = VAP_TO_LVIF(vap);
1746 	vif = LVIF_TO_VIF(lvif);
1747 
1748 	IEEE80211_UNLOCK(vap->iv_ic);
1749 	LKPI_80211_LHW_LOCK(lhw);
1750 
1751 	LKPI_80211_LVIF_LOCK(lvif);
1752 #ifdef LINUXKPI_DEBUG_80211
1753 	/* XXX-BZ KASSERT later; state going down so no action. */
1754 	if (lvif->lvif_bss == NULL)
1755 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1756 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1757 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1758 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1759 		    lvif->lvif_bss_synched);
1760 #endif
1761 	lsta = lvif->lvif_bss;
1762 	LKPI_80211_LVIF_UNLOCK(lvif);
1763 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1764 	    "lvif %p vap %p\n", __func__,
1765 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1766 
1767 	ni = lsta->ni;		/* Reference held for lvif_bss. */
1768 	sta = LSTA_TO_STA(lsta);
1769 
1770 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1771 
1772 	/* flush, drop. */
1773 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1774 
1775 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1776 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1777 	    !lsta->in_mgd) {
1778 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1779 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
1780 		prep_tx_info.was_assoc = true;
1781 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1782 		lsta->in_mgd = true;
1783 	}
1784 
1785 	LKPI_80211_LHW_UNLOCK(lhw);
1786 	IEEE80211_LOCK(vap->iv_ic);
1787 
1788 	/* Call iv_newstate first so we get potential DEAUTH packet out. */
1789 	error = lvif->iv_newstate(vap, nstate, arg);
1790 	if (error != 0) {
1791 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
1792 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
1793 		goto outni;
1794 	}
1795 
1796 	IEEE80211_UNLOCK(vap->iv_ic);
1797 
1798 	/* Ensure the packets get out. */
1799 	lkpi_80211_flush_tx(lhw, lsta);
1800 
1801 	LKPI_80211_LHW_LOCK(lhw);
1802 
1803 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1804 
1805 	/* Wake tx queues to get packet(s) out. */
1806 	lkpi_wake_tx_queues(hw, sta, false, true);
1807 
1808 	/* flush, no drop */
1809 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1810 
1811 	/* End mgd_complete_tx. */
1812 	if (lsta->in_mgd) {
1813 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1814 		prep_tx_info.success = false;
1815 		prep_tx_info.was_assoc = true;
1816 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1817 		lsta->in_mgd = false;
1818 	}
1819 
1820 	/* sync_rx_queues */
1821 	lkpi_80211_mo_sync_rx_queues(hw);
1822 
1823 	/* sta_pre_rcu_remove */
1824         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1825 
1826 	/* Take the station down. */
1827 
1828 	/* Update sta and change state (from AUTH) to NONE. */
1829 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1830 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1831 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1832 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
1833 	if (error != 0) {
1834 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
1835 		    "failed: %d\n", __func__, __LINE__, error);
1836 		goto out;
1837 	}
1838 
1839 	/* See comment in lkpi_sta_run_to_init(). */
1840 	bss_changed = 0;
1841 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
1842 
1843 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1844 
1845 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
1846 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1847 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1848 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1849 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
1850 	if (error != 0) {
1851 		IMPROVE("do we need to undo the chan ctx?");
1852 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
1853 		    "failed: %d\n", __func__, __LINE__, error);
1854 		goto out;
1855 	}
1856 
1857 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
1858 
1859 	IMPROVE("Any bss_info changes to announce?");
1860 	vif->bss_conf.qos = 0;
1861 	bss_changed |= BSS_CHANGED_QOS;
1862 	vif->cfg.ssid_len = 0;
1863 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
1864 	bss_changed |= BSS_CHANGED_BSSID;
1865 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1866 
1867 	LKPI_80211_LVIF_LOCK(lvif);
1868 	/* Remove ni reference for this cache of lsta. */
1869 	lvif->lvif_bss = NULL;
1870 	lvif->lvif_bss_synched = false;
1871 	LKPI_80211_LVIF_UNLOCK(lvif);
1872 	lkpi_lsta_remove(lsta, lvif);
1873 	/*
1874 	 * The very last release the reference on the ni for the ni/lsta on
1875 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
1876 	 * and potentially freed.
1877 	 */
1878 	ieee80211_free_node(ni);
1879 
1880 	/* conf_tx */
1881 
1882 	/* Take the chan ctx down. */
1883 	if (vif->chanctx_conf != NULL) {
1884 		struct lkpi_chanctx *lchanctx;
1885 		struct ieee80211_chanctx_conf *chanctx_conf;
1886 
1887 		chanctx_conf = vif->chanctx_conf;
1888 		/* Remove vif context. */
1889 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
1890 		/* NB: vif->chanctx_conf is NULL now. */
1891 
1892 		lkpi_hw_conf_idle(hw, true);
1893 
1894 		/* Remove chan ctx. */
1895 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
1896 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
1897 		free(lchanctx, M_LKPI80211);
1898 	}
1899 
1900 	error = EALREADY;
1901 out:
1902 	LKPI_80211_LHW_UNLOCK(lhw);
1903 	IEEE80211_LOCK(vap->iv_ic);
1904 outni:
1905 	return (error);
1906 }
1907 
1908 static int
lkpi_sta_assoc_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1909 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1910 {
1911 	int error;
1912 
1913 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1914 	if (error != 0 && error != EALREADY)
1915 		return (error);
1916 
1917 	/* At this point iv_bss is long a new node! */
1918 
1919 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1920 	return (error);
1921 }
1922 
1923 static int
lkpi_sta_assoc_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1924 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1925 {
1926 	int error;
1927 
1928 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1929 	return (error);
1930 }
1931 
1932 static int
lkpi_sta_assoc_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1933 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1934 {
1935 	int error;
1936 
1937 	error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1938 	return (error);
1939 }
1940 
1941 static int
lkpi_sta_assoc_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)1942 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1943 {
1944 	struct lkpi_hw *lhw;
1945 	struct ieee80211_hw *hw;
1946 	struct lkpi_vif *lvif;
1947 	struct ieee80211_vif *vif;
1948 	struct ieee80211_node *ni;
1949 	struct lkpi_sta *lsta;
1950 	struct ieee80211_sta *sta;
1951 	struct ieee80211_prep_tx_info prep_tx_info;
1952 	enum ieee80211_bss_changed bss_changed;
1953 	int error;
1954 
1955 	lhw = vap->iv_ic->ic_softc;
1956 	hw = LHW_TO_HW(lhw);
1957 	lvif = VAP_TO_LVIF(vap);
1958 	vif = LVIF_TO_VIF(lvif);
1959 
1960 	IEEE80211_UNLOCK(vap->iv_ic);
1961 	LKPI_80211_LHW_LOCK(lhw);
1962 
1963 	LKPI_80211_LVIF_LOCK(lvif);
1964 	/* XXX-BZ KASSERT later? */
1965 	if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
1966 #ifdef LINUXKPI_DEBUG_80211
1967 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
1968 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
1969 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
1970 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
1971 		    lvif->lvif_bss_synched);
1972 #endif
1973 		LKPI_80211_LVIF_UNLOCK(lvif);
1974 		error = ENOTRECOVERABLE;
1975 		goto out;
1976 	}
1977 	lsta = lvif->lvif_bss;
1978 	LKPI_80211_LVIF_UNLOCK(lvif);
1979 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
1980 	    "lvif %p vap %p\n", __func__,
1981 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
1982 
1983 	ni = lsta->ni;		/* Reference held for lvif_bss. */
1984 
1985 	IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
1986 	    "and to lesser extend ieee80211_notify_node_join");
1987 
1988 	/* Finish assoc. */
1989 	/* Update sta_state (AUTH to ASSOC) and set aid. */
1990 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1991 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
1992 	sta = LSTA_TO_STA(lsta);
1993 	sta->aid = IEEE80211_NODE_AID(ni);
1994 #ifdef LKPI_80211_WME
1995 	if (vap->iv_flags & IEEE80211_F_WME)
1996 		sta->wme = true;
1997 #endif
1998 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
1999 	if (error != 0) {
2000 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2001 		    "failed: %d\n", __func__, __LINE__, error);
2002 		goto out;
2003 	}
2004 
2005 	IMPROVE("wme / conf_tx [all]");
2006 
2007 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2008 	bss_changed = 0;
2009 #ifdef LKPI_80211_WME
2010 	bss_changed |= lkpi_wme_update(lhw, vap, true);
2011 #endif
2012 	if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
2013 		vif->cfg.assoc = true;
2014 		vif->cfg.aid = IEEE80211_NODE_AID(ni);
2015 		bss_changed |= BSS_CHANGED_ASSOC;
2016 	}
2017 	/* We set SSID but this is not BSSID! */
2018 	vif->cfg.ssid_len = ni->ni_esslen;
2019 	memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
2020 	if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
2021 	    vif->bss_conf.use_short_preamble) {
2022 		vif->bss_conf.use_short_preamble ^= 1;
2023 		/* bss_changed |= BSS_CHANGED_??? */
2024 	}
2025 	if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
2026 	    vif->bss_conf.use_short_slot) {
2027 		vif->bss_conf.use_short_slot ^= 1;
2028 		/* bss_changed |= BSS_CHANGED_??? */
2029 	}
2030 	if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
2031 	    vif->bss_conf.qos) {
2032 		vif->bss_conf.qos ^= 1;
2033 		bss_changed |= BSS_CHANGED_QOS;
2034 	}
2035 
2036 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2037 
2038 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2039 
2040 	/* - change_chanctx (if needed)
2041 	 * - event_callback
2042 	 */
2043 
2044 	/* End mgd_complete_tx. */
2045 	if (lsta->in_mgd) {
2046 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2047 		prep_tx_info.success = true;
2048 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2049 		lsta->in_mgd = false;
2050 	}
2051 
2052 	lkpi_hw_conf_idle(hw, false);
2053 
2054 	/*
2055 	 * And then:
2056 	 * - (more packets)?
2057 	 * - set_key
2058 	 * - set_default_unicast_key
2059 	 * - set_key (?)
2060 	 * - ipv6_addr_change (?)
2061 	 */
2062 	/* Prepare_multicast && configure_filter. */
2063 	lhw->update_mc = true;
2064 	lkpi_update_mcast_filter(vap->iv_ic, true);
2065 
2066 	if (!ieee80211_node_is_authorized(ni)) {
2067 		IMPROVE("net80211 does not consider node authorized");
2068 	}
2069 
2070 #if defined(LKPI_80211_HT)
2071 	IMPROVE("Is this the right spot, has net80211 done all updates already?");
2072 	lkpi_sta_sync_ht_from_ni(sta, ni, NULL);
2073 #endif
2074 
2075 	/* Update sta_state (ASSOC to AUTHORIZED). */
2076 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2077 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2078 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2079 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
2080 	if (error != 0) {
2081 		IMPROVE("undo some changes?");
2082 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
2083 		    "failed: %d\n", __func__, __LINE__, error);
2084 		goto out;
2085 	}
2086 
2087 	/* - drv_config (?)
2088 	 * - bss_info_changed
2089 	 * - set_rekey_data (?)
2090 	 *
2091 	 * And now we should be passing packets.
2092 	 */
2093 	IMPROVE("Need that bssid setting, and the keys");
2094 
2095 	bss_changed = 0;
2096 	bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2097 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2098 
2099 out:
2100 	LKPI_80211_LHW_UNLOCK(lhw);
2101 	IEEE80211_LOCK(vap->iv_ic);
2102 	return (error);
2103 }
2104 
2105 static int
lkpi_sta_auth_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2106 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2107 {
2108 	int error;
2109 
2110 	error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
2111 	if (error == 0)
2112 		error = lkpi_sta_assoc_to_run(vap, nstate, arg);
2113 	return (error);
2114 }
2115 
2116 static int
lkpi_sta_run_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2117 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2118 {
2119 	struct lkpi_hw *lhw;
2120 	struct ieee80211_hw *hw;
2121 	struct lkpi_vif *lvif;
2122 	struct ieee80211_vif *vif;
2123 	struct ieee80211_node *ni;
2124 	struct lkpi_sta *lsta;
2125 	struct ieee80211_sta *sta;
2126 	struct ieee80211_prep_tx_info prep_tx_info;
2127 #if 0
2128 	enum ieee80211_bss_changed bss_changed;
2129 #endif
2130 	int error;
2131 
2132 	lhw = vap->iv_ic->ic_softc;
2133 	hw = LHW_TO_HW(lhw);
2134 	lvif = VAP_TO_LVIF(vap);
2135 	vif = LVIF_TO_VIF(lvif);
2136 
2137 	LKPI_80211_LVIF_LOCK(lvif);
2138 #ifdef LINUXKPI_DEBUG_80211
2139 	/* XXX-BZ KASSERT later; state going down so no action. */
2140 	if (lvif->lvif_bss == NULL)
2141 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2142 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2143 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2144 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2145 		    lvif->lvif_bss_synched);
2146 #endif
2147 	lsta = lvif->lvif_bss;
2148 	LKPI_80211_LVIF_UNLOCK(lvif);
2149 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2150 	    "lvif %p vap %p\n", __func__,
2151 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2152 
2153 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2154 	sta = LSTA_TO_STA(lsta);
2155 
2156 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2157 
2158 	IEEE80211_UNLOCK(vap->iv_ic);
2159 	LKPI_80211_LHW_LOCK(lhw);
2160 
2161 	/* flush, drop. */
2162 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2163 
2164 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2165 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2166 	    !lsta->in_mgd) {
2167 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2168 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2169 		prep_tx_info.was_assoc = true;
2170 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2171 		lsta->in_mgd = true;
2172 	}
2173 
2174 	LKPI_80211_LHW_UNLOCK(lhw);
2175 	IEEE80211_LOCK(vap->iv_ic);
2176 
2177 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2178 	error = lvif->iv_newstate(vap, nstate, arg);
2179 	if (error != 0) {
2180 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2181 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2182 		goto outni;
2183 	}
2184 
2185 	IEEE80211_UNLOCK(vap->iv_ic);
2186 
2187 	/* Ensure the packets get out. */
2188 	lkpi_80211_flush_tx(lhw, lsta);
2189 
2190 	LKPI_80211_LHW_LOCK(lhw);
2191 
2192 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2193 
2194 	/* Wake tx queues to get packet(s) out. */
2195 	lkpi_wake_tx_queues(hw, sta, false, true);
2196 
2197 	/* flush, no drop */
2198 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2199 
2200 	/* End mgd_complete_tx. */
2201 	if (lsta->in_mgd) {
2202 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2203 		prep_tx_info.success = false;
2204 		prep_tx_info.was_assoc = true;
2205 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2206 		lsta->in_mgd = false;
2207 	}
2208 
2209 #if 0
2210 	/* sync_rx_queues */
2211 	lkpi_80211_mo_sync_rx_queues(hw);
2212 
2213 	/* sta_pre_rcu_remove */
2214         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2215 #endif
2216 
2217 	/* Take the station down. */
2218 
2219 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2220 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2221 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2222 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2223 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2224 	if (error != 0) {
2225 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2226 		    "failed: %d\n", __func__, __LINE__, error);
2227 		goto out;
2228 	}
2229 
2230 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2231 
2232 	/* Update sta_state (ASSOC to AUTH). */
2233 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2234 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2235 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2236 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2237 	if (error != 0) {
2238 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2239 		    "failed: %d\n", __func__, __LINE__, error);
2240 		goto out;
2241 	}
2242 
2243 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2244 
2245 #if 0
2246 	/* Update bss info (bss_info_changed) (assoc, aid, ..). */
2247 	lkpi_disassoc(sta, vif, lhw);
2248 #endif
2249 
2250 	error = EALREADY;
2251 out:
2252 	LKPI_80211_LHW_UNLOCK(lhw);
2253 	IEEE80211_LOCK(vap->iv_ic);
2254 outni:
2255 	return (error);
2256 }
2257 
2258 static int
lkpi_sta_run_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2259 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2260 {
2261 	struct lkpi_hw *lhw;
2262 	struct ieee80211_hw *hw;
2263 	struct lkpi_vif *lvif;
2264 	struct ieee80211_vif *vif;
2265 	struct ieee80211_node *ni;
2266 	struct lkpi_sta *lsta;
2267 	struct ieee80211_sta *sta;
2268 	struct ieee80211_prep_tx_info prep_tx_info;
2269 	enum ieee80211_bss_changed bss_changed;
2270 	int error;
2271 
2272 	lhw = vap->iv_ic->ic_softc;
2273 	hw = LHW_TO_HW(lhw);
2274 	lvif = VAP_TO_LVIF(vap);
2275 	vif = LVIF_TO_VIF(lvif);
2276 
2277 	IEEE80211_UNLOCK(vap->iv_ic);
2278 	LKPI_80211_LHW_LOCK(lhw);
2279 
2280 	LKPI_80211_LVIF_LOCK(lvif);
2281 #ifdef LINUXKPI_DEBUG_80211
2282 	/* XXX-BZ KASSERT later; state going down so no action. */
2283 	if (lvif->lvif_bss == NULL)
2284 		ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2285 		    "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2286 		    lvif, vap, vap->iv_bss, lvif->lvif_bss,
2287 		    (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2288 		    lvif->lvif_bss_synched);
2289 #endif
2290 	lsta = lvif->lvif_bss;
2291 	LKPI_80211_LVIF_UNLOCK(lvif);
2292 	KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
2293 	    "lvif %p vap %p\n", __func__,
2294 	    lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
2295 
2296 	ni = lsta->ni;		/* Reference held for lvif_bss. */
2297 	sta = LSTA_TO_STA(lsta);
2298 
2299 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2300 
2301 	/* flush, drop. */
2302 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
2303 
2304 	IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
2305 	if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
2306 	    !lsta->in_mgd) {
2307 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2308 		prep_tx_info.duration = PREP_TX_INFO_DURATION;
2309 		prep_tx_info.was_assoc = true;
2310 		lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2311 		lsta->in_mgd = true;
2312 	}
2313 
2314 	LKPI_80211_LHW_UNLOCK(lhw);
2315 	IEEE80211_LOCK(vap->iv_ic);
2316 
2317 	/* Call iv_newstate first so we get potential DISASSOC packet out. */
2318 	error = lvif->iv_newstate(vap, nstate, arg);
2319 	if (error != 0) {
2320 		ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
2321 		    "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
2322 		goto outni;
2323 	}
2324 
2325 	IEEE80211_UNLOCK(vap->iv_ic);
2326 
2327 	/* Ensure the packets get out. */
2328 	lkpi_80211_flush_tx(lhw, lsta);
2329 
2330 	LKPI_80211_LHW_LOCK(lhw);
2331 
2332 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2333 
2334 	/* Wake tx queues to get packet(s) out. */
2335 	lkpi_wake_tx_queues(hw, sta, false, true);
2336 
2337 	/* flush, no drop */
2338 	lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
2339 
2340 	/* End mgd_complete_tx. */
2341 	if (lsta->in_mgd) {
2342 		memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2343 		prep_tx_info.success = false;
2344 		prep_tx_info.was_assoc = true;
2345 		lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
2346 		lsta->in_mgd = false;
2347 	}
2348 
2349 	/* sync_rx_queues */
2350 	lkpi_80211_mo_sync_rx_queues(hw);
2351 
2352 	/* sta_pre_rcu_remove */
2353         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
2354 
2355 	/* Take the station down. */
2356 
2357 	/* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
2358 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2359 	KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
2360 	    "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
2361 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
2362 	if (error != 0) {
2363 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
2364 		    "failed: %d\n", __func__, __LINE__, error);
2365 		goto out;
2366 	}
2367 
2368 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2369 
2370 	/* Update sta_state (ASSOC to AUTH). */
2371 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2372 	KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
2373 	    "ASSOC: %#x\n", __func__, lsta, lsta->state));
2374 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
2375 	if (error != 0) {
2376 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
2377 		    "failed: %d\n", __func__, __LINE__, error);
2378 		goto out;
2379 	}
2380 
2381 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2382 
2383 	/* Update sta and change state (from AUTH) to NONE. */
2384 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2385 	KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
2386 	    "AUTH: %#x\n", __func__, lsta, lsta->state));
2387 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2388 	if (error != 0) {
2389 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2390 		    "failed: %d\n", __func__, __LINE__, error);
2391 		goto out;
2392 	}
2393 
2394 	bss_changed = 0;
2395 	/*
2396 	 * Start updating bss info (bss_info_changed) (assoc, aid, ..).
2397 	 *
2398 	 * One would expect this to happen when going off AUTHORIZED.
2399 	 * See comment there; removes the sta from fw if not careful
2400 	 * (bss_info_changed() change is executed right away).
2401 	 *
2402 	 * We need to do this now, before sta changes to IEEE80211_STA_NOTEXIST
2403 	 * as otherwise drivers (iwlwifi at least) will silently not remove
2404 	 * the sta from the firmware and when we will add a new one trigger
2405 	 * a fw assert.
2406 	 *
2407 	 * The order which works best so far avoiding early removal or silent
2408 	 * non-removal seems to be (for iwlwifi::mld-mac80211.c cases;
2409 	 * the iwlwifi:mac80211.c case still to be tested):
2410 	 * 1) lkpi_disassoc(): set vif->cfg.assoc = false (aid=0 side effect here)
2411 	 * 2) call the last sta_state update -> IEEE80211_STA_NOTEXIST
2412 	 *    (removes the sta given assoc is false)
2413 	 * 3) add the remaining BSS_CHANGED changes and call bss_info_changed()
2414 	 * 4) call unassign_vif_chanctx
2415 	 * 5) call lkpi_hw_conf_idle
2416 	 * 6) call remove_chanctx
2417 	 */
2418 	bss_changed |= lkpi_disassoc(sta, vif, lhw);
2419 
2420 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2421 
2422 	/* Adjust sta and change state (from NONE) to NOTEXIST. */
2423 	KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2424 	KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
2425 	    "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
2426 	error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
2427 	if (error != 0) {
2428 		IMPROVE("do we need to undo the chan ctx?");
2429 		ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
2430 		    "failed: %d\n", __func__, __LINE__, error);
2431 		goto out;
2432 	}
2433 
2434 	lkpi_lsta_remove(lsta, lvif);
2435 
2436 	lkpi_lsta_dump(lsta, ni, __func__, __LINE__);	/* sta no longer save to use. */
2437 
2438 	IMPROVE("Any bss_info changes to announce?");
2439 	vif->bss_conf.qos = 0;
2440 	bss_changed |= BSS_CHANGED_QOS;
2441 	vif->cfg.ssid_len = 0;
2442 	memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
2443 	bss_changed |= BSS_CHANGED_BSSID;
2444 	vif->bss_conf.use_short_preamble = false;
2445 	vif->bss_conf.qos = false;
2446 	/* XXX BSS_CHANGED_???? */
2447 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2448 
2449 	LKPI_80211_LVIF_LOCK(lvif);
2450 	/* Remove ni reference for this cache of lsta. */
2451 	lvif->lvif_bss = NULL;
2452 	lvif->lvif_bss_synched = false;
2453 	LKPI_80211_LVIF_UNLOCK(lvif);
2454 	/*
2455 	 * The very last release the reference on the ni for the ni/lsta on
2456 	 * lvif->lvif_bss.  Upon return from this both ni and lsta are invalid
2457 	 * and potentially freed.
2458 	 */
2459 	ieee80211_free_node(ni);
2460 
2461 	/* conf_tx */
2462 
2463 	/* Take the chan ctx down. */
2464 	if (vif->chanctx_conf != NULL) {
2465 		struct lkpi_chanctx *lchanctx;
2466 		struct ieee80211_chanctx_conf *chanctx_conf;
2467 
2468 		chanctx_conf = vif->chanctx_conf;
2469 		/* Remove vif context. */
2470 		lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, &vif->chanctx_conf);
2471 		/* NB: vif->chanctx_conf is NULL now. */
2472 
2473 		lkpi_hw_conf_idle(hw, true);
2474 
2475 		/* Remove chan ctx. */
2476 		lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2477 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2478 		free(lchanctx, M_LKPI80211);
2479 	}
2480 
2481 	error = EALREADY;
2482 out:
2483 	LKPI_80211_LHW_UNLOCK(lhw);
2484 	IEEE80211_LOCK(vap->iv_ic);
2485 outni:
2486 	return (error);
2487 }
2488 
2489 static int
lkpi_sta_run_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2490 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2491 {
2492 
2493 	return (lkpi_sta_run_to_init(vap, nstate, arg));
2494 }
2495 
2496 static int
lkpi_sta_run_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2497 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2498 {
2499 	int error;
2500 
2501 	error = lkpi_sta_run_to_init(vap, nstate, arg);
2502 	if (error != 0 && error != EALREADY)
2503 		return (error);
2504 
2505 	/* At this point iv_bss is long a new node! */
2506 
2507 	error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
2508 	return (error);
2509 }
2510 
2511 /* -------------------------------------------------------------------------- */
2512 
2513 /*
2514  * The matches the documented state changes in net80211::sta_newstate().
2515  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
2516  * there are "invalid" (so there is a room for failure here).
2517  */
2518 struct fsm_state {
2519 	/* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
2520 	enum ieee80211_state ostate;
2521 	enum ieee80211_state nstate;
2522 	int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
2523 } sta_state_fsm[] = {
2524 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },
2525 	{ IEEE80211_S_SCAN,	IEEE80211_S_INIT, lkpi_sta_state_do_nada },	/* scan_to_init */
2526 	{ IEEE80211_S_AUTH,	IEEE80211_S_INIT, lkpi_sta_auth_to_init },	/* not explicitly in sta_newstate() */
2527 	{ IEEE80211_S_ASSOC,	IEEE80211_S_INIT, lkpi_sta_assoc_to_init },	/* Send DEAUTH. */
2528 	{ IEEE80211_S_RUN,	IEEE80211_S_INIT, lkpi_sta_run_to_init },	/* Send DISASSOC. */
2529 
2530 	{ IEEE80211_S_INIT,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
2531 	{ IEEE80211_S_SCAN,	IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
2532 	{ IEEE80211_S_AUTH,	IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
2533 	{ IEEE80211_S_ASSOC,	IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
2534 	{ IEEE80211_S_RUN,	IEEE80211_S_SCAN, lkpi_sta_run_to_scan },	/* Beacon miss. */
2535 
2536 	{ IEEE80211_S_INIT,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2537 	{ IEEE80211_S_SCAN,	IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },	/* Send AUTH. */
2538 	{ IEEE80211_S_AUTH,	IEEE80211_S_AUTH, lkpi_sta_a_to_a },		/* Send ?AUTH. */
2539 	{ IEEE80211_S_ASSOC,	IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },	/* Send ?AUTH. */
2540 	{ IEEE80211_S_RUN,	IEEE80211_S_AUTH, lkpi_sta_run_to_auth },	/* Send ?AUTH. */
2541 
2542 	{ IEEE80211_S_AUTH,	IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },	/* Send ASSOCREQ. */
2543 	{ IEEE80211_S_ASSOC,	IEEE80211_S_ASSOC, lkpi_sta_a_to_a },		/* Send ASSOCREQ. */
2544 	{ IEEE80211_S_RUN,	IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },	/* Send ASSOCREQ/REASSOCREQ. */
2545 
2546 	{ IEEE80211_S_AUTH,	IEEE80211_S_RUN, lkpi_sta_auth_to_run },
2547 	{ IEEE80211_S_ASSOC,	IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
2548 	{ IEEE80211_S_RUN,	IEEE80211_S_RUN, lkpi_sta_state_do_nada },
2549 
2550 	/* Dummy at the end without handler. */
2551 	{ IEEE80211_S_INIT,	IEEE80211_S_INIT, NULL },
2552 };
2553 
2554 static int
lkpi_iv_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2555 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2556 {
2557 	struct ieee80211com *ic;
2558 	struct lkpi_hw *lhw;
2559 	struct lkpi_vif *lvif;
2560 	struct ieee80211_vif *vif;
2561 	struct fsm_state *s;
2562 	enum ieee80211_state ostate;
2563 	int error;
2564 
2565 	ic = vap->iv_ic;
2566 	IEEE80211_LOCK_ASSERT(ic);
2567 	ostate = vap->iv_state;
2568 
2569 #ifdef LINUXKPI_DEBUG_80211
2570 	if (linuxkpi_debug_80211 & D80211_TRACE)
2571 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
2572 		    __func__, __LINE__, vap, nstate, arg);
2573 #endif
2574 
2575 	if (vap->iv_opmode == IEEE80211_M_STA) {
2576 
2577 		lhw = ic->ic_softc;
2578 		lvif = VAP_TO_LVIF(vap);
2579 		vif = LVIF_TO_VIF(lvif);
2580 
2581 		/* No need to replicate this in most state handlers. */
2582 		if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
2583 			lkpi_stop_hw_scan(lhw, vif);
2584 
2585 		s = sta_state_fsm;
2586 
2587 	} else {
2588 		ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
2589 		    "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
2590 		return (ENOSYS);
2591 	}
2592 
2593 	error = 0;
2594 	for (; s->handler != NULL; s++) {
2595 		if (ostate == s->ostate && nstate == s->nstate) {
2596 #ifdef LINUXKPI_DEBUG_80211
2597 			if (linuxkpi_debug_80211 & D80211_TRACE)
2598 				ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2599 				    " %d (%s): arg %d.\n", __func__,
2600 				    ostate, ieee80211_state_name[ostate],
2601 				    nstate, ieee80211_state_name[nstate], arg);
2602 #endif
2603 			error = s->handler(vap, nstate, arg);
2604 			break;
2605 		}
2606 	}
2607 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2608 
2609 	if (s->handler == NULL) {
2610 		IMPROVE("turn this into a KASSERT\n");
2611 		ic_printf(vap->iv_ic, "%s: unsupported state transition "
2612 		    "%d (%s) -> %d (%s)\n", __func__,
2613 		    ostate, ieee80211_state_name[ostate],
2614 		    nstate, ieee80211_state_name[nstate]);
2615 		return (ENOSYS);
2616 	}
2617 
2618 	if (error == EALREADY) {
2619 #ifdef LINUXKPI_DEBUG_80211
2620 		if (linuxkpi_debug_80211 & D80211_TRACE)
2621 			ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2622 			    "%d (%s): iv_newstate already handled: %d.\n",
2623 			    __func__, ostate, ieee80211_state_name[ostate],
2624 			    nstate, ieee80211_state_name[nstate], error);
2625 #endif
2626 		return (0);
2627 	}
2628 
2629 	if (error != 0) {
2630 		ic_printf(vap->iv_ic, "%s: error %d during state transition "
2631 		    "%d (%s) -> %d (%s)\n", __func__, error,
2632 		    ostate, ieee80211_state_name[ostate],
2633 		    nstate, ieee80211_state_name[nstate]);
2634 		return (error);
2635 	}
2636 
2637 #ifdef LINUXKPI_DEBUG_80211
2638 	if (linuxkpi_debug_80211 & D80211_TRACE)
2639 		ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2640 		    "calling net80211 parent\n",
2641 		    __func__, __LINE__, vap, nstate, arg);
2642 #endif
2643 
2644 	return (lvif->iv_newstate(vap, nstate, arg));
2645 }
2646 
2647 /* -------------------------------------------------------------------------- */
2648 
2649 /*
2650  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2651  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2652  * new node without us knowing and thus our ni/lsta are out of sync.
2653  */
2654 static struct ieee80211_node *
lkpi_iv_update_bss(struct ieee80211vap * vap,struct ieee80211_node * ni)2655 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2656 {
2657 	struct lkpi_vif *lvif;
2658 	struct ieee80211_node *rni;
2659 
2660 	IEEE80211_LOCK_ASSERT(vap->iv_ic);
2661 
2662 	lvif = VAP_TO_LVIF(vap);
2663 
2664 	LKPI_80211_LVIF_LOCK(lvif);
2665 	lvif->lvif_bss_synched = false;
2666 	LKPI_80211_LVIF_UNLOCK(lvif);
2667 
2668 	rni = lvif->iv_update_bss(vap, ni);
2669 	return (rni);
2670 }
2671 
2672 #ifdef LKPI_80211_WME
2673 static int
lkpi_wme_update(struct lkpi_hw * lhw,struct ieee80211vap * vap,bool planned)2674 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
2675 {
2676 	struct ieee80211com *ic;
2677 	struct ieee80211_hw *hw;
2678 	struct lkpi_vif *lvif;
2679 	struct ieee80211_vif *vif;
2680 	struct chanAccParams chp;
2681 	struct wmeParams wmeparr[WME_NUM_AC];
2682 	struct ieee80211_tx_queue_params txqp;
2683 	enum ieee80211_bss_changed changed;
2684 	int error;
2685 	uint16_t ac;
2686 
2687 	IMPROVE();
2688 	KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
2689 	    "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
2690 
2691 	if (vap == NULL)
2692 		return (0);
2693 
2694 	if ((vap->iv_flags & IEEE80211_F_WME) == 0)
2695 		return (0);
2696 
2697 	if (lhw->ops->conf_tx == NULL)
2698 		return (0);
2699 
2700 	if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
2701 		lhw->update_wme = true;
2702 		return (0);
2703 	}
2704 	lhw->update_wme = false;
2705 
2706 	ic = lhw->ic;
2707 	ieee80211_wme_ic_getparams(ic, &chp);
2708 	IEEE80211_LOCK(ic);
2709 	for (ac = 0; ac < WME_NUM_AC; ac++)
2710 		wmeparr[ac] = chp.cap_wmeParams[ac];
2711 	IEEE80211_UNLOCK(ic);
2712 
2713 	hw = LHW_TO_HW(lhw);
2714 	lvif = VAP_TO_LVIF(vap);
2715 	vif = LVIF_TO_VIF(lvif);
2716 
2717 	/* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
2718 	LKPI_80211_LHW_LOCK(lhw);
2719 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2720 		struct wmeParams *wmep;
2721 
2722 		wmep = &wmeparr[ac];
2723 		bzero(&txqp, sizeof(txqp));
2724 		txqp.cw_min = wmep->wmep_logcwmin;
2725 		txqp.cw_max = wmep->wmep_logcwmax;
2726 		txqp.txop = wmep->wmep_txopLimit;
2727 		txqp.aifs = wmep->wmep_aifsn;
2728 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2729 		if (error != 0)
2730 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2731 			    __func__, ac, error);
2732 	}
2733 	LKPI_80211_LHW_UNLOCK(lhw);
2734 	changed = BSS_CHANGED_QOS;
2735 	if (!planned)
2736 		lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2737 
2738 	return (changed);
2739 }
2740 #endif
2741 
2742 static int
lkpi_ic_wme_update(struct ieee80211com * ic)2743 lkpi_ic_wme_update(struct ieee80211com *ic)
2744 {
2745 #ifdef LKPI_80211_WME
2746 	struct ieee80211vap *vap;
2747 	struct lkpi_hw *lhw;
2748 
2749 	IMPROVE("Use the per-VAP callback in net80211.");
2750 	vap = TAILQ_FIRST(&ic->ic_vaps);
2751 	if (vap == NULL)
2752 		return (0);
2753 
2754 	lhw = ic->ic_softc;
2755 
2756 	lkpi_wme_update(lhw, vap, false);
2757 #endif
2758 	return (0);	/* unused */
2759 }
2760 
2761 /*
2762  * Change link-layer address on the vif (if the vap is not started/"UP").
2763  * This can happen if a user changes 'ether' using ifconfig.
2764  * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
2765  * we do use a per-[l]vif event handler to be sure we exist as we
2766  * cannot assume that from every vap derives a vif and we have a hard
2767  * time checking based on net80211 information.
2768  * Should this ever become a real problem we could add a callback function
2769  * to wlan_iflladdr() to be set optionally but that would be for a
2770  * single-consumer (or needs a list) -- was just too complicated for an
2771  * otherwise perfect mechanism FreeBSD already provides.
2772  */
2773 static void
lkpi_vif_iflladdr(void * arg,struct ifnet * ifp)2774 lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
2775 {
2776 	struct epoch_tracker et;
2777 	struct ieee80211_vif *vif;
2778 
2779 	NET_EPOCH_ENTER(et);
2780 	/* NB: identify vap's by if_transmit; left as an extra check. */
2781 	if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
2782 	    (if_getflags(ifp) & IFF_UP) != 0) {
2783 		NET_EPOCH_EXIT(et);
2784 		return;
2785 	}
2786 
2787 	vif = arg;
2788 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
2789 	NET_EPOCH_EXIT(et);
2790 }
2791 
2792 static struct ieee80211vap *
lkpi_ic_vap_create(struct ieee80211com * ic,const char name[IFNAMSIZ],int unit,enum ieee80211_opmode opmode,int flags,const uint8_t bssid[IEEE80211_ADDR_LEN],const uint8_t mac[IEEE80211_ADDR_LEN])2793 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
2794     int unit, enum ieee80211_opmode opmode, int flags,
2795     const uint8_t bssid[IEEE80211_ADDR_LEN],
2796     const uint8_t mac[IEEE80211_ADDR_LEN])
2797 {
2798 	struct lkpi_hw *lhw;
2799 	struct ieee80211_hw *hw;
2800 	struct lkpi_vif *lvif;
2801 	struct ieee80211vap *vap;
2802 	struct ieee80211_vif *vif;
2803 	struct ieee80211_tx_queue_params txqp;
2804 	enum ieee80211_bss_changed changed;
2805 	size_t len;
2806 	int error, i;
2807 	uint16_t ac;
2808 
2809 	if (!TAILQ_EMPTY(&ic->ic_vaps))	/* 1 so far. Add <n> once this works. */
2810 		return (NULL);
2811 
2812 	lhw = ic->ic_softc;
2813 	hw = LHW_TO_HW(lhw);
2814 
2815 	len = sizeof(*lvif);
2816 	len += hw->vif_data_size;	/* vif->drv_priv */
2817 
2818 	lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
2819 	mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
2820 	TAILQ_INIT(&lvif->lsta_head);
2821 	lvif->lvif_bss = NULL;
2822 	lvif->lvif_bss_synched = false;
2823 	vap = LVIF_TO_VAP(lvif);
2824 
2825 	vif = LVIF_TO_VIF(lvif);
2826 	memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
2827 	vif->p2p = false;
2828 	vif->probe_req_reg = false;
2829 	vif->type = lkpi_opmode_to_vif_type(opmode);
2830 	lvif->wdev.iftype = vif->type;
2831 	/* Need to fill in other fields as well. */
2832 	IMPROVE();
2833 
2834 	/* XXX-BZ hardcoded for now! */
2835 #if 1
2836 	vif->chanctx_conf = NULL;
2837 	vif->bss_conf.vif = vif;
2838 	/* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
2839 	IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
2840 	lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
2841 	    lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
2842 	vif->bss_conf.link_id = 0;	/* Non-MLO operation. */
2843 	vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
2844 	vif->bss_conf.use_short_preamble = false;	/* vap->iv_flags IEEE80211_F_SHPREAMBLE */
2845 	vif->bss_conf.use_short_slot = false;		/* vap->iv_flags IEEE80211_F_SHSLOT */
2846 	vif->bss_conf.qos = false;
2847 	vif->bss_conf.use_cts_prot = false;		/* vap->iv_protmode */
2848 	vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2849 	vif->cfg.aid = 0;
2850 	vif->cfg.assoc = false;
2851 	vif->cfg.idle = true;
2852 	vif->cfg.ps = false;
2853 	IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
2854 	/*
2855 	 * We need to initialize it to something as the bss_info_changed call
2856 	 * will try to copy from it in iwlwifi and NULL is a panic.
2857 	 * We will set the proper one in scan_to_auth() before being assoc.
2858 	 */
2859 	vif->bss_conf.bssid = ieee80211broadcastaddr;
2860 #endif
2861 #if 0
2862 	vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
2863 	IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
2864 	vif->bss_conf.beacon_int = ic->ic_bintval;
2865 	/* iwlwifi bug. */
2866 	if (vif->bss_conf.beacon_int < 16)
2867 		vif->bss_conf.beacon_int = 16;
2868 #endif
2869 
2870 	/* Link Config */
2871 	vif->link_conf[0] = &vif->bss_conf;
2872 	for (i = 0; i < nitems(vif->link_conf); i++) {
2873 		IMPROVE("more than 1 link one day");
2874 	}
2875 
2876 	/* Setup queue defaults; driver may override in (*add_interface). */
2877 	for (i = 0; i < IEEE80211_NUM_ACS; i++) {
2878 		if (ieee80211_hw_check(hw, QUEUE_CONTROL))
2879 			vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
2880 		else if (hw->queues >= IEEE80211_NUM_ACS)
2881 			vif->hw_queue[i] = i;
2882 		else
2883 			vif->hw_queue[i] = 0;
2884 
2885 		/* Initialize the queue to running. Stopped? */
2886 		lvif->hw_queue_stopped[i] = false;
2887 	}
2888 	vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
2889 
2890 	IMPROVE();
2891 
2892 	error = lkpi_80211_mo_start(hw);
2893 	if (error != 0) {
2894 		ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
2895 		mtx_destroy(&lvif->mtx);
2896 		free(lvif, M_80211_VAP);
2897 		return (NULL);
2898 	}
2899 
2900 	error = lkpi_80211_mo_add_interface(hw, vif);
2901 	if (error != 0) {
2902 		IMPROVE();	/* XXX-BZ mo_stop()? */
2903 		ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
2904 		mtx_destroy(&lvif->mtx);
2905 		free(lvif, M_80211_VAP);
2906 		return (NULL);
2907 	}
2908 
2909 	LKPI_80211_LHW_LVIF_LOCK(lhw);
2910 	TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
2911 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
2912 
2913 	/* Set bss_info. */
2914 	changed = 0;
2915 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2916 
2917 	/* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
2918 	IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
2919 	LKPI_80211_LHW_LOCK(lhw);
2920 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2921 
2922 		bzero(&txqp, sizeof(txqp));
2923 		txqp.cw_min = 15;
2924 		txqp.cw_max = 1023;
2925 		txqp.txop = 0;
2926 		txqp.aifs = 2;
2927 		error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
2928 		if (error != 0)
2929 			ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
2930 			    __func__, ac, error);
2931 	}
2932 	LKPI_80211_LHW_UNLOCK(lhw);
2933 	changed = BSS_CHANGED_QOS;
2934 	lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2935 
2936 	/* Force MC init. */
2937 	lkpi_update_mcast_filter(ic, true);
2938 
2939 	IMPROVE();
2940 
2941 	ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
2942 
2943 	/* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
2944 	lvif->iv_newstate = vap->iv_newstate;
2945 	vap->iv_newstate = lkpi_iv_newstate;
2946 	lvif->iv_update_bss = vap->iv_update_bss;
2947 	vap->iv_update_bss = lkpi_iv_update_bss;
2948 
2949 	/* Key management. */
2950 	if (lhw->ops->set_key != NULL) {
2951 #ifdef LKPI_80211_HW_CRYPTO
2952 		vap->iv_key_set = lkpi_iv_key_set;
2953 		vap->iv_key_delete = lkpi_iv_key_delete;
2954 #endif
2955 	}
2956 
2957 #ifdef LKPI_80211_HT
2958 	/* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
2959 #endif
2960 
2961 	ieee80211_ratectl_init(vap);
2962 
2963 	/* Complete setup. */
2964 	ieee80211_vap_attach(vap, ieee80211_media_change,
2965 	    ieee80211_media_status, mac);
2966 
2967 	if (hw->max_listen_interval == 0)
2968 		hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
2969 	hw->conf.listen_interval = hw->max_listen_interval;
2970 	ic->ic_set_channel(ic);
2971 
2972 	/* XXX-BZ do we need to be able to update these? */
2973 	hw->wiphy->frag_threshold = vap->iv_fragthreshold;
2974 	lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
2975 	hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
2976 	lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
2977 	/* any others? */
2978 	IMPROVE();
2979 
2980 	return (vap);
2981 }
2982 
2983 void
linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw * hw)2984 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
2985 {
2986 
2987 	wiphy_unregister(hw->wiphy);
2988 	linuxkpi_ieee80211_ifdetach(hw);
2989 
2990 	IMPROVE();
2991 }
2992 
2993 void
linuxkpi_ieee80211_restart_hw(struct ieee80211_hw * hw)2994 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
2995 {
2996 
2997 	TODO();
2998 }
2999 
3000 static void
lkpi_ic_vap_delete(struct ieee80211vap * vap)3001 lkpi_ic_vap_delete(struct ieee80211vap *vap)
3002 {
3003 	struct ieee80211com *ic;
3004 	struct lkpi_hw *lhw;
3005 	struct ieee80211_hw *hw;
3006 	struct lkpi_vif *lvif;
3007 	struct ieee80211_vif *vif;
3008 
3009 	lvif = VAP_TO_LVIF(vap);
3010 	vif = LVIF_TO_VIF(lvif);
3011 	ic = vap->iv_ic;
3012 	lhw = ic->ic_softc;
3013 	hw = LHW_TO_HW(lhw);
3014 
3015 	EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
3016 
3017 	LKPI_80211_LHW_LVIF_LOCK(lhw);
3018 	TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
3019 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
3020 
3021 	ieee80211_ratectl_deinit(vap);
3022 	ieee80211_vap_detach(vap);
3023 
3024 	IMPROVE("clear up other bits in this state");
3025 
3026 	lkpi_80211_mo_remove_interface(hw, vif);
3027 
3028 	/* Single VAP, so we can do this here. */
3029 	lkpi_80211_mo_stop(hw, false);			/* XXX SUSPEND */
3030 
3031 	mtx_destroy(&lvif->mtx);
3032 	free(lvif, M_80211_VAP);
3033 }
3034 
3035 static void
lkpi_ic_update_mcast(struct ieee80211com * ic)3036 lkpi_ic_update_mcast(struct ieee80211com *ic)
3037 {
3038 
3039 	lkpi_update_mcast_filter(ic, false);
3040 	TRACEOK();
3041 }
3042 
3043 static void
lkpi_ic_update_promisc(struct ieee80211com * ic)3044 lkpi_ic_update_promisc(struct ieee80211com *ic)
3045 {
3046 
3047 	UNIMPLEMENTED;
3048 }
3049 
3050 static void
lkpi_ic_update_chw(struct ieee80211com * ic)3051 lkpi_ic_update_chw(struct ieee80211com *ic)
3052 {
3053 
3054 	UNIMPLEMENTED;
3055 }
3056 
3057 /* Start / stop device. */
3058 static void
lkpi_ic_parent(struct ieee80211com * ic)3059 lkpi_ic_parent(struct ieee80211com *ic)
3060 {
3061 	struct lkpi_hw *lhw;
3062 #ifdef HW_START_STOP
3063 	struct ieee80211_hw *hw;
3064 	int error;
3065 #endif
3066 	bool start_all;
3067 
3068 	IMPROVE();
3069 
3070 	lhw = ic->ic_softc;
3071 #ifdef HW_START_STOP
3072 	hw = LHW_TO_HW(lhw);
3073 #endif
3074 	start_all = false;
3075 
3076 	/* IEEE80211_UNLOCK(ic); */
3077 	LKPI_80211_LHW_LOCK(lhw);
3078 	if (ic->ic_nrunning > 0) {
3079 #ifdef HW_START_STOP
3080 		error = lkpi_80211_mo_start(hw);
3081 		if (error == 0)
3082 #endif
3083 			start_all = true;
3084 	} else {
3085 #ifdef HW_START_STOP
3086 		lkpi_80211_mo_stop(hw, false);		/* XXX SUSPEND */
3087 #endif
3088 	}
3089 	LKPI_80211_LHW_UNLOCK(lhw);
3090 	/* IEEE80211_LOCK(ic); */
3091 
3092 	if (start_all)
3093 		ieee80211_start_all(ic);
3094 }
3095 
3096 bool
linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie,const u8 * ie_ids,size_t ie_ids_len)3097 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
3098     size_t ie_ids_len)
3099 {
3100 	int i;
3101 
3102 	for (i = 0; i < ie_ids_len; i++) {
3103 		if (ie == *ie_ids)
3104 			return (true);
3105 	}
3106 
3107 	return (false);
3108 }
3109 
3110 /* Return true if skipped; false if error. */
3111 bool
linuxkpi_ieee80211_ie_advance(size_t * xp,const u8 * ies,size_t ies_len)3112 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
3113 {
3114 	size_t x;
3115 	uint8_t l;
3116 
3117 	x = *xp;
3118 
3119 	KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
3120 	    __func__, x, ies_len, ies));
3121 	l = ies[x + 1];
3122 	x += 2 + l;
3123 
3124 	if (x > ies_len)
3125 		return (false);
3126 
3127 	*xp = x;
3128 	return (true);
3129 }
3130 
3131 static uint8_t *
lkpi_scan_ies_add(uint8_t * p,struct ieee80211_scan_ies * scan_ies,uint32_t band_mask,struct ieee80211vap * vap,struct ieee80211_hw * hw)3132 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
3133     uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
3134 {
3135 	struct ieee80211_supported_band *supband;
3136 	struct linuxkpi_ieee80211_channel *channels;
3137 	struct ieee80211com *ic;
3138 	const struct ieee80211_channel *chan;
3139 	const struct ieee80211_rateset *rs;
3140 	uint8_t *pb;
3141 	int band, i;
3142 
3143 	ic = vap->iv_ic;
3144 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
3145 		if ((band_mask & (1 << band)) == 0)
3146 			continue;
3147 
3148 		supband = hw->wiphy->bands[band];
3149 		/*
3150 		 * This should not happen;
3151 		 * band_mask is a bitmask of valid bands to scan on.
3152 		 */
3153 		if (supband == NULL || supband->n_channels == 0)
3154 			continue;
3155 
3156 		/* Find a first channel to get the mode and rates from. */
3157 		channels = supband->channels;
3158 		chan = NULL;
3159 		for (i = 0; i < supband->n_channels; i++) {
3160 
3161 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3162 				continue;
3163 
3164 			chan = ieee80211_find_channel(ic,
3165 			    channels[i].center_freq, 0);
3166 			if (chan != NULL)
3167 				break;
3168 		}
3169 
3170 		/* This really should not happen. */
3171 		if (chan == NULL)
3172 			continue;
3173 
3174 		pb = p;
3175 		rs = ieee80211_get_suprates(ic, chan);	/* calls chan2mode */
3176 		p = ieee80211_add_rates(p, rs);
3177 		p = ieee80211_add_xrates(p, rs);
3178 
3179 #if defined(LKPI_80211_HT)
3180 		if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
3181 			struct ieee80211_channel *c;
3182 
3183 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
3184 			    vap->iv_flags_ht);
3185 			p = ieee80211_add_htcap_ch(p, vap, c);
3186 		}
3187 #endif
3188 #if defined(LKPI_80211_VHT)
3189 		if ((vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
3190 			struct ieee80211_channel *c;
3191 
3192 			c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
3193 			    vap->iv_flags_ht);
3194 			c = ieee80211_vht_adjust_channel(ic, c,
3195 			    vap->iv_vht_flags);
3196 			p = ieee80211_add_vhtcap_ch(p, vap, c);
3197 		}
3198 #endif
3199 
3200 		scan_ies->ies[band] = pb;
3201 		scan_ies->len[band] = p - pb;
3202 	}
3203 
3204 	/* Add common_ies */
3205 	pb = p;
3206 	if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3207 	    vap->iv_wpa_ie != NULL) {
3208 		memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
3209 		p += 2 + vap->iv_wpa_ie[1];
3210 	}
3211 	if (vap->iv_appie_probereq != NULL) {
3212 		memcpy(p, vap->iv_appie_probereq->ie_data,
3213 		    vap->iv_appie_probereq->ie_len);
3214 		p += vap->iv_appie_probereq->ie_len;
3215 	}
3216 	scan_ies->common_ies = pb;
3217 	scan_ies->common_ie_len = p - pb;
3218 
3219 	return (p);
3220 }
3221 
3222 static void
lkpi_ic_scan_start(struct ieee80211com * ic)3223 lkpi_ic_scan_start(struct ieee80211com *ic)
3224 {
3225 	struct lkpi_hw *lhw;
3226 	struct ieee80211_hw *hw;
3227 	struct lkpi_vif *lvif;
3228 	struct ieee80211_vif *vif;
3229 	struct ieee80211_scan_state *ss;
3230 	struct ieee80211vap *vap;
3231 	int error;
3232 	bool is_hw_scan;
3233 
3234 	lhw = ic->ic_softc;
3235 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3236 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
3237 		/* A scan is still running. */
3238 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3239 		return;
3240 	}
3241 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3242 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3243 
3244 	ss = ic->ic_scan;
3245 	vap = ss->ss_vap;
3246 	if (vap->iv_state != IEEE80211_S_SCAN) {
3247 		IMPROVE("We need to be able to scan if not in S_SCAN");
3248 		return;
3249 	}
3250 
3251 	hw = LHW_TO_HW(lhw);
3252 	if (!is_hw_scan) {
3253 		/* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
3254 		vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3255 sw_scan:
3256 		lvif = VAP_TO_LVIF(vap);
3257 		vif = LVIF_TO_VIF(lvif);
3258 
3259 		if (vap->iv_state == IEEE80211_S_SCAN)
3260 			lkpi_hw_conf_idle(hw, false);
3261 
3262 		lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
3263 		/* net80211::scan_start() handled PS for us. */
3264 		IMPROVE();
3265 		/* XXX Also means it is too late to flush queues?
3266 		 * need to check iv_sta_ps or overload? */
3267 		/* XXX want to adjust ss end time/ maxdwell? */
3268 
3269 	} else {
3270 		struct ieee80211_channel *c;
3271 		struct ieee80211_scan_request *hw_req;
3272 		struct linuxkpi_ieee80211_channel *lc, **cpp;
3273 		struct cfg80211_ssid *ssids;
3274 		struct cfg80211_scan_6ghz_params *s6gp;
3275 		size_t chan_len, nchan, ssids_len, s6ghzlen;
3276 		int band, i, ssid_count, common_ie_len;
3277 		uint32_t band_mask;
3278 		uint8_t *ie, *ieend;
3279 		bool running;
3280 
3281 		ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
3282 		ssids_len = ssid_count * sizeof(*ssids);
3283 		s6ghzlen = 0 * (sizeof(*s6gp));			/* XXX-BZ */
3284 
3285 		band_mask = 0;
3286 		nchan = 0;
3287 		for (i = ss->ss_next; i < ss->ss_last; i++) {
3288 			nchan++;
3289 			band = lkpi_net80211_chan_to_nl80211_band(
3290 			    ss->ss_chans[ss->ss_next + i]);
3291 			band_mask |= (1 << band);
3292 		}
3293 
3294 		if (!ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
3295 			IMPROVE("individual band scans not yet supported, only scanning first band");
3296 			/* In theory net80211 should drive this. */
3297 			/* Probably we need to add local logic for now;
3298 			 * need to deal with scan_complete
3299 			 * and cancel_scan and keep local state.
3300 			 * Also cut the nchan down above.
3301 			 */
3302 			/* XXX-BZ ath10k does not set this but still does it? &$%^ */
3303 		}
3304 
3305 		chan_len = nchan * (sizeof(lc) + sizeof(*lc));
3306 
3307 		common_ie_len = 0;
3308 		if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
3309 		    vap->iv_wpa_ie != NULL)
3310 			common_ie_len += vap->iv_wpa_ie[1];
3311 		if (vap->iv_appie_probereq != NULL)
3312 			common_ie_len += vap->iv_appie_probereq->ie_len;
3313 
3314 		/* We would love to check this at an earlier stage... */
3315 		if (common_ie_len >  hw->wiphy->max_scan_ie_len) {
3316 			ic_printf(ic, "WARNING: %s: common_ie_len %d > "
3317 			    "wiphy->max_scan_ie_len %d\n", __func__,
3318 			    common_ie_len, hw->wiphy->max_scan_ie_len);
3319 		}
3320 
3321 		hw_req = malloc(sizeof(*hw_req) + ssids_len +
3322 		    s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
3323 		    common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
3324 
3325 		hw_req->req.flags = 0;			/* XXX ??? */
3326 		/* hw_req->req.wdev */
3327 		hw_req->req.wiphy = hw->wiphy;
3328 		hw_req->req.no_cck = false;		/* XXX */
3329 #if 0
3330 		/* This seems to pessimise default scanning behaviour. */
3331 		hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
3332 		hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
3333 #endif
3334 #ifdef __notyet__
3335 		hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
3336 		memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
3337 		memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
3338 #endif
3339 		eth_broadcast_addr(hw_req->req.bssid);
3340 
3341 		hw_req->req.n_channels = nchan;
3342 		cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
3343 		lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
3344 		for (i = 0; i < nchan; i++) {
3345 			*(cpp + i) =
3346 			    (struct linuxkpi_ieee80211_channel *)(lc + i);
3347 		}
3348 		for (i = 0; i < nchan; i++) {
3349 			c = ss->ss_chans[ss->ss_next + i];
3350 
3351 			lc->hw_value = c->ic_ieee;
3352 			lc->center_freq = c->ic_freq;	/* XXX */
3353 			/* lc->flags */
3354 			lc->band = lkpi_net80211_chan_to_nl80211_band(c);
3355 			lc->max_power = c->ic_maxpower;
3356 			/* lc-> ... */
3357 			lc++;
3358 		}
3359 
3360 		hw_req->req.n_ssids = ssid_count;
3361 		if (hw_req->req.n_ssids > 0) {
3362 			ssids = (struct cfg80211_ssid *)lc;
3363 			hw_req->req.ssids = ssids;
3364 			for (i = 0; i < ssid_count; i++) {
3365 				ssids->ssid_len = ss->ss_ssid[i].len;
3366 				memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
3367 				    ss->ss_ssid[i].len);
3368 				ssids++;
3369 			}
3370 			s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
3371 		} else {
3372 			s6gp = (struct cfg80211_scan_6ghz_params *)lc;
3373 		}
3374 
3375 		/* 6GHz one day. */
3376 		hw_req->req.n_6ghz_params = 0;
3377 		hw_req->req.scan_6ghz_params = NULL;
3378 		hw_req->req.scan_6ghz = false;	/* Weird boolean; not what you think. */
3379 		/* s6gp->... */
3380 
3381 		ie = ieend = (uint8_t *)s6gp;
3382 		/* Copy per-band IEs, copy common IEs */
3383 		ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
3384 		hw_req->req.ie = ie;
3385 		hw_req->req.ie_len = ieend - ie;
3386 
3387 		lvif = VAP_TO_LVIF(vap);
3388 		vif = LVIF_TO_VIF(lvif);
3389 
3390 		LKPI_80211_LHW_SCAN_LOCK(lhw);
3391 		/* Re-check under lock. */
3392 		running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
3393 		if (!running) {
3394 			KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
3395 			    "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
3396 
3397 			lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
3398 			lhw->hw_req = hw_req;
3399 		}
3400 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3401 		if (running) {
3402 			free(hw_req, M_LKPI80211);
3403 			return;
3404 		}
3405 
3406 		error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
3407 		if (error != 0) {
3408 			ieee80211_cancel_scan(vap);
3409 
3410 			/*
3411 			 * ieee80211_scan_completed must be called in either
3412 			 * case of error or none.  So let the free happen there
3413 			 * and only there.
3414 			 * That would be fine in theory but in practice drivers
3415 			 * behave differently:
3416 			 * ath10k does not return hw_scan until after scan_complete
3417 			 *        and can then still return an error.
3418 			 * rtw88 can return 1 or -EBUSY without scan_complete
3419 			 * iwlwifi can return various errors before scan starts
3420 			 * ...
3421 			 * So we cannot rely on that behaviour and have to check
3422 			 * and balance between both code paths.
3423 			 */
3424 			LKPI_80211_LHW_SCAN_LOCK(lhw);
3425 			if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
3426 				free(lhw->hw_req, M_LKPI80211);
3427 				lhw->hw_req = NULL;
3428 				lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
3429 			}
3430 			LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3431 
3432 			/*
3433 			 * XXX-SIGH magic number.
3434 			 * rtw88 has a magic "return 1" if offloading scan is
3435 			 * not possible.  Fall back to sw scan in that case.
3436 			 */
3437 			if (error == 1) {
3438 				LKPI_80211_LHW_SCAN_LOCK(lhw);
3439 				lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
3440 				LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3441 				/*
3442 				 * XXX If we clear this now and later a driver
3443 				 * thinks it * can do a hw_scan again, we will
3444 				 * currently not re-enable it?
3445 				 */
3446 				vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
3447 				ieee80211_start_scan(vap,
3448 				    IEEE80211_SCAN_ACTIVE |
3449 				    IEEE80211_SCAN_NOPICK |
3450 				    IEEE80211_SCAN_ONCE,
3451 				    IEEE80211_SCAN_FOREVER,
3452 				    ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
3453 				    ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
3454 				    vap->iv_des_nssid, vap->iv_des_ssid);
3455 				goto sw_scan;
3456 			}
3457 
3458 			ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
3459 			    __func__, error);
3460 		}
3461 	}
3462 }
3463 
3464 static void
lkpi_ic_scan_end(struct ieee80211com * ic)3465 lkpi_ic_scan_end(struct ieee80211com *ic)
3466 {
3467 	struct lkpi_hw *lhw;
3468 	bool is_hw_scan;
3469 
3470 	lhw = ic->ic_softc;
3471 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3472 	if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
3473 		LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3474 		return;
3475 	}
3476 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3477 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3478 
3479 	if (!is_hw_scan) {
3480 		struct ieee80211_scan_state *ss;
3481 		struct ieee80211vap *vap;
3482 		struct ieee80211_hw *hw;
3483 		struct lkpi_vif *lvif;
3484 		struct ieee80211_vif *vif;
3485 
3486 		ss = ic->ic_scan;
3487 		vap = ss->ss_vap;
3488 		hw = LHW_TO_HW(lhw);
3489 		lvif = VAP_TO_LVIF(vap);
3490 		vif = LVIF_TO_VIF(lvif);
3491 
3492 		lkpi_80211_mo_sw_scan_complete(hw, vif);
3493 
3494 		/* Send PS to stop buffering if n80211 does not for us? */
3495 
3496 		if (vap->iv_state == IEEE80211_S_SCAN)
3497 			lkpi_hw_conf_idle(hw, true);
3498 	}
3499 }
3500 
3501 static void
lkpi_ic_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)3502 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
3503     unsigned long maxdwell)
3504 {
3505 	struct lkpi_hw *lhw;
3506 	bool is_hw_scan;
3507 
3508 	lhw = ss->ss_ic->ic_softc;
3509 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3510 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3511 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3512 	if (!is_hw_scan)
3513 		lhw->ic_scan_curchan(ss, maxdwell);
3514 }
3515 
3516 static void
lkpi_ic_scan_mindwell(struct ieee80211_scan_state * ss)3517 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
3518 {
3519 	struct lkpi_hw *lhw;
3520 	bool is_hw_scan;
3521 
3522 	lhw = ss->ss_ic->ic_softc;
3523 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3524 	is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
3525 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3526 	if (!is_hw_scan)
3527 		lhw->ic_scan_mindwell(ss);
3528 }
3529 
3530 static void
lkpi_ic_set_channel(struct ieee80211com * ic)3531 lkpi_ic_set_channel(struct ieee80211com *ic)
3532 {
3533 	struct lkpi_hw *lhw;
3534 	struct ieee80211_hw *hw;
3535 	struct ieee80211_channel *c;
3536 	struct linuxkpi_ieee80211_channel *chan;
3537 	int error;
3538 	bool hw_scan_running;
3539 
3540 	lhw = ic->ic_softc;
3541 
3542 	/* If we do not support (*config)() save us the work. */
3543 	if (lhw->ops->config == NULL)
3544 		return;
3545 
3546 	/* If we have a hw_scan running do not switch channels. */
3547 	LKPI_80211_LHW_SCAN_LOCK(lhw);
3548 	hw_scan_running =
3549 	    (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) ==
3550 		(LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW);
3551 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
3552 	if (hw_scan_running)
3553 		return;
3554 
3555 	c = ic->ic_curchan;
3556 	if (c == NULL || c == IEEE80211_CHAN_ANYC) {
3557 		ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
3558 		    c, lhw->ops->config);
3559 		return;
3560 	}
3561 
3562 	chan = lkpi_find_lkpi80211_chan(lhw, c);
3563 	if (chan == NULL) {
3564 		ic_printf(ic, "%s: c %p chan %p\n", __func__,
3565 		    c, chan);
3566 		return;
3567 	}
3568 
3569 	/* XXX max power for scanning? */
3570 	IMPROVE();
3571 
3572 	hw = LHW_TO_HW(lhw);
3573 	cfg80211_chandef_create(&hw->conf.chandef, chan,
3574 #ifdef LKPI_80211_HT
3575 	    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
3576 #endif
3577 	    NL80211_CHAN_NO_HT);
3578 
3579 	error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
3580 	if (error != 0 && error != EOPNOTSUPP) {
3581 		ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
3582 		    __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
3583 		/* XXX should we unroll to the previous chandef? */
3584 		IMPROVE();
3585 	} else {
3586 		/* Update radiotap channels as well. */
3587 		lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
3588 		lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
3589 		lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
3590 		lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
3591 	}
3592 
3593 	/* Currently PS is hard coded off! Not sure it belongs here. */
3594 	IMPROVE();
3595 	if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
3596 	    (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
3597 		hw->conf.flags &= ~IEEE80211_CONF_PS;
3598 		error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
3599 		if (error != 0 && error != EOPNOTSUPP)
3600 			ic_printf(ic, "ERROR: %s: config %#0x returned "
3601 			    "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
3602 			    error);
3603 	}
3604 }
3605 
3606 static struct ieee80211_node *
lkpi_ic_node_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN])3607 lkpi_ic_node_alloc(struct ieee80211vap *vap,
3608     const uint8_t mac[IEEE80211_ADDR_LEN])
3609 {
3610 	struct ieee80211com *ic;
3611 	struct lkpi_hw *lhw;
3612 	struct ieee80211_node *ni;
3613 	struct ieee80211_hw *hw;
3614 	struct lkpi_sta *lsta;
3615 
3616 	ic = vap->iv_ic;
3617 	lhw = ic->ic_softc;
3618 
3619 	/* We keep allocations de-coupled so we can deal with the two worlds. */
3620 	if (lhw->ic_node_alloc == NULL)
3621 		return (NULL);
3622 
3623 	ni = lhw->ic_node_alloc(vap, mac);
3624 	if (ni == NULL)
3625 		return (NULL);
3626 
3627 	hw = LHW_TO_HW(lhw);
3628 	lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
3629 	if (lsta == NULL) {
3630 		if (lhw->ic_node_free != NULL)
3631 			lhw->ic_node_free(ni);
3632 		return (NULL);
3633 	}
3634 
3635 	return (ni);
3636 }
3637 
3638 static int
lkpi_ic_node_init(struct ieee80211_node * ni)3639 lkpi_ic_node_init(struct ieee80211_node *ni)
3640 {
3641 	struct ieee80211com *ic;
3642 	struct lkpi_hw *lhw;
3643 	int error;
3644 
3645 	ic = ni->ni_ic;
3646 	lhw = ic->ic_softc;
3647 
3648 	if (lhw->ic_node_init != NULL) {
3649 		error = lhw->ic_node_init(ni);
3650 		if (error != 0)
3651 			return (error);
3652 	}
3653 
3654 	/* XXX-BZ Sync other state over. */
3655 	IMPROVE();
3656 
3657 	return (0);
3658 }
3659 
3660 static void
lkpi_ic_node_cleanup(struct ieee80211_node * ni)3661 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
3662 {
3663 	struct ieee80211com *ic;
3664 	struct lkpi_hw *lhw;
3665 
3666 	ic = ni->ni_ic;
3667 	lhw = ic->ic_softc;
3668 
3669 	/* XXX-BZ remove from driver, ... */
3670 	IMPROVE();
3671 
3672 	if (lhw->ic_node_cleanup != NULL)
3673 		lhw->ic_node_cleanup(ni);
3674 }
3675 
3676 static void
lkpi_ic_node_free(struct ieee80211_node * ni)3677 lkpi_ic_node_free(struct ieee80211_node *ni)
3678 {
3679 	struct ieee80211com *ic;
3680 	struct lkpi_hw *lhw;
3681 	struct lkpi_sta *lsta;
3682 
3683 	ic = ni->ni_ic;
3684 	lhw = ic->ic_softc;
3685 	lsta = ni->ni_drv_data;
3686 
3687 	/* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
3688 
3689 	/*
3690 	 * Pass in the original ni just in case of error we could check that
3691 	 * it is the same as lsta->ni.
3692 	 */
3693 	lkpi_lsta_free(lsta, ni);
3694 
3695 	if (lhw->ic_node_free != NULL)
3696 		lhw->ic_node_free(ni);
3697 }
3698 
3699 static int
lkpi_ic_raw_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params __unused)3700 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
3701         const struct ieee80211_bpf_params *params __unused)
3702 {
3703 	struct lkpi_sta *lsta;
3704 
3705 	lsta = ni->ni_drv_data;
3706 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
3707 	if (!lsta->added_to_drv || !lsta->txq_ready) {
3708 		LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3709 		/*
3710 		 * Free the mbuf (do NOT release ni ref for the m_pkthdr.rcvif!
3711 		 * ieee80211_raw_output() does that in case of error).
3712 		 */
3713 		m_free(m);
3714 		return (ENETDOWN);
3715 	}
3716 
3717 	/* Queue the packet and enqueue the task to handle it. */
3718 	mbufq_enqueue(&lsta->txq, m);
3719 	taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
3720 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3721 
3722 #ifdef LINUXKPI_DEBUG_80211
3723 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3724 		printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
3725 		    __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
3726 		    mbufq_len(&lsta->txq));
3727 #endif
3728 
3729 	return (0);
3730 }
3731 
3732 static void
lkpi_80211_txq_tx_one(struct lkpi_sta * lsta,struct mbuf * m)3733 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
3734 {
3735 	struct ieee80211_node *ni;
3736 #ifndef LKPI_80211_HW_CRYPTO
3737 	struct ieee80211_frame *wh;
3738 #endif
3739 	struct ieee80211_key *k;
3740 	struct sk_buff *skb;
3741 	struct ieee80211com *ic;
3742 	struct lkpi_hw *lhw;
3743 	struct ieee80211_hw *hw;
3744 	struct lkpi_vif *lvif;
3745 	struct ieee80211_vif *vif;
3746 	struct ieee80211_channel *c;
3747 	struct ieee80211_tx_control control;
3748 	struct ieee80211_tx_info *info;
3749 	struct ieee80211_sta *sta;
3750 	struct ieee80211_hdr *hdr;
3751 	struct lkpi_txq *ltxq;
3752 	void *buf;
3753 	uint8_t ac, tid;
3754 
3755 	M_ASSERTPKTHDR(m);
3756 #ifdef LINUXKPI_DEBUG_80211
3757 	if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
3758 		hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
3759 #endif
3760 
3761 	ni = lsta->ni;
3762 	k = NULL;
3763 #ifndef LKPI_80211_HW_CRYPTO
3764 	/* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
3765 	wh = mtod(m, struct ieee80211_frame *);
3766 	if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
3767 		/* Retrieve key for TX && do software encryption. */
3768 		k = ieee80211_crypto_encap(ni, m);
3769 		if (k == NULL) {
3770 			ieee80211_free_node(ni);
3771 			m_freem(m);
3772 			return;
3773 		}
3774 	}
3775 #endif
3776 
3777 	ic = ni->ni_ic;
3778 	lhw = ic->ic_softc;
3779 	hw = LHW_TO_HW(lhw);
3780 	c = ni->ni_chan;
3781 
3782 	if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
3783 		struct lkpi_radiotap_tx_hdr *rtap;
3784 
3785 		rtap = &lhw->rtap_tx;
3786 		rtap->wt_flags = 0;
3787 		if (k != NULL)
3788 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
3789 		if (m->m_flags & M_FRAG)
3790 			rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
3791 		IMPROVE();
3792 		rtap->wt_rate = 0;
3793 		if (c != NULL && c != IEEE80211_CHAN_ANYC) {
3794 			rtap->wt_chan_freq = htole16(c->ic_freq);
3795 			rtap->wt_chan_flags = htole16(c->ic_flags);
3796 		}
3797 
3798 		ieee80211_radiotap_tx(ni->ni_vap, m);
3799 	}
3800 
3801 	/*
3802 	 * net80211 should handle hw->extra_tx_headroom.
3803 	 * Though for as long as we are copying we don't mind.
3804 	 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
3805 	 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
3806 	 */
3807 	skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
3808 	if (skb == NULL) {
3809 		ic_printf(ic, "ERROR %s: skb alloc failed\n", __func__);
3810 		ieee80211_free_node(ni);
3811 		m_freem(m);
3812 		return;
3813 	}
3814 	skb_reserve(skb, hw->extra_tx_headroom);
3815 
3816 	/* XXX-BZ we need a SKB version understanding mbuf. */
3817 	/* Save the mbuf for ieee80211_tx_complete(). */
3818 	skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
3819 	skb->m = m;
3820 #if 0
3821 	skb_put_data(skb, m->m_data, m->m_pkthdr.len);
3822 #else
3823 	buf = skb_put(skb, m->m_pkthdr.len);
3824 	m_copydata(m, 0, m->m_pkthdr.len, buf);
3825 #endif
3826 	/* Save the ni. */
3827 	m->m_pkthdr.PH_loc.ptr = ni;
3828 
3829 	lvif = VAP_TO_LVIF(ni->ni_vap);
3830 	vif = LVIF_TO_VIF(lvif);
3831 
3832 	hdr = (void *)skb->data;
3833 	tid = linuxkpi_ieee80211_get_tid(hdr, true);
3834 	if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
3835 		if (!ieee80211_is_data(hdr->frame_control)) {
3836 			/* MGMT and CTRL frames go on TID 7/VO. */
3837 			skb->priority = 7;
3838 			ac = IEEE80211_AC_VO;
3839 		} else {
3840 			/* Other non-QOS traffic goes to BE. */
3841 			/* Contrary to net80211 we MUST NOT promote M_EAPOL. */
3842 			skb->priority = 0;
3843 			ac = IEEE80211_AC_BE;
3844 		}
3845 	} else {
3846 		skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
3847 		ac = ieee80211e_up_to_ac[tid & 7];
3848 	}
3849 	skb_set_queue_mapping(skb, ac);
3850 
3851 	info = IEEE80211_SKB_CB(skb);
3852 	info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
3853 	/* Slight delay; probably only happens on scanning so fine? */
3854 	if (c == NULL || c == IEEE80211_CHAN_ANYC)
3855 		c = ic->ic_curchan;
3856 	info->band = lkpi_net80211_chan_to_nl80211_band(c);
3857 	info->hw_queue = vif->hw_queue[ac];
3858 	if (m->m_flags & M_EAPOL)
3859 		info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
3860 	info->control.vif = vif;
3861 	/* XXX-BZ info->control.rates */
3862 #ifdef __notyet__
3863 #ifdef LKPI_80211_HT
3864 	info->control.rts_cts_rate_idx=
3865 	info->control.use_rts= /* RTS */
3866 	info->control.use_cts_prot= /* RTS/CTS*/
3867 #endif
3868 #endif
3869 
3870 	sta = LSTA_TO_STA(lsta);
3871 #ifdef LKPI_80211_HW_CRYPTO
3872 	info->control.hw_key = lsta->kc;
3873 #endif
3874 
3875 	IMPROVE();
3876 
3877 	ltxq = NULL;
3878 	if (!ieee80211_is_data_present(hdr->frame_control)) {
3879 		if (vif->type == NL80211_IFTYPE_STATION &&
3880 		    lsta->added_to_drv &&
3881 		    sta->txq[IEEE80211_NUM_TIDS] != NULL)
3882 			ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
3883 	} else if (lsta->added_to_drv &&
3884 	    sta->txq[skb->priority] != NULL) {
3885 		ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
3886 	}
3887 	if (ltxq == NULL)
3888 		goto ops_tx;
3889 
3890 	KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
3891 	    "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
3892 
3893 	LKPI_80211_LTXQ_LOCK(ltxq);
3894 	skb_queue_tail(&ltxq->skbq, skb);
3895 #ifdef LINUXKPI_DEBUG_80211
3896 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3897 		printf("%s:%d mo_wake_tx_queue :: %d %u lsta %p sta %p "
3898 		    "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
3899 		    "WAKE_TX_Q ac %d prio %u qmap %u\n",
3900 		    __func__, __LINE__,
3901 		    curthread->td_tid, (unsigned int)ticks,
3902 		    lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
3903 		    skb_queue_len(&ltxq->skbq), ltxq->txq.ac,
3904 		    ltxq->txq.tid, ac, skb->priority, skb->qmap);
3905 #endif
3906 	LKPI_80211_LTXQ_UNLOCK(ltxq);
3907 	LKPI_80211_LHW_LOCK(lhw);
3908 	lkpi_80211_mo_wake_tx_queue(hw, &ltxq->txq);
3909 	LKPI_80211_LHW_UNLOCK(lhw);
3910 	return;
3911 
3912 ops_tx:
3913 #ifdef LINUXKPI_DEBUG_80211
3914 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3915 		printf("%s:%d mo_tx :: lsta %p sta %p ni %p %6D skb %p "
3916 		    "TX ac %d prio %u qmap %u\n",
3917 		    __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
3918 		    skb, ac, skb->priority, skb->qmap);
3919 #endif
3920 	memset(&control, 0, sizeof(control));
3921 	control.sta = sta;
3922 	LKPI_80211_LHW_LOCK(lhw);
3923 	lkpi_80211_mo_tx(hw, &control, skb);
3924 	LKPI_80211_LHW_UNLOCK(lhw);
3925 }
3926 
3927 static void
lkpi_80211_txq_task(void * ctx,int pending)3928 lkpi_80211_txq_task(void *ctx, int pending)
3929 {
3930 	struct lkpi_sta *lsta;
3931 	struct mbufq mq;
3932 	struct mbuf *m;
3933 	bool shall_tx;
3934 
3935 	lsta = ctx;
3936 
3937 #ifdef LINUXKPI_DEBUG_80211
3938 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3939 		printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
3940 		    __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
3941 		    pending, mbufq_len(&lsta->txq));
3942 #endif
3943 
3944 	mbufq_init(&mq, IFQ_MAXLEN);
3945 
3946 	LKPI_80211_LSTA_TXQ_LOCK(lsta);
3947 	/*
3948 	 * Do not re-check lsta->txq_ready here; we may have a pending
3949 	 * disassoc/deauth frame still.  On the contrary if txq_ready is
3950 	 * false we do not have a valid sta anymore in the firmware so no
3951 	 * point to try to TX.
3952 	 * We also use txq_ready as a semaphore and will drain the txq manually
3953 	 * if needed on our way towards SCAN/INIT in the state machine.
3954 	 */
3955 	shall_tx = lsta->added_to_drv && lsta->txq_ready;
3956 	if (__predict_true(shall_tx))
3957 		mbufq_concat(&mq, &lsta->txq);
3958 	/*
3959 	 * else a state change will push the packets out manually or
3960 	 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
3961 	 */
3962 	LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3963 
3964 	m = mbufq_dequeue(&mq);
3965 	while (m != NULL) {
3966 		lkpi_80211_txq_tx_one(lsta, m);
3967 		m = mbufq_dequeue(&mq);
3968 	}
3969 }
3970 
3971 static int
lkpi_ic_transmit(struct ieee80211com * ic,struct mbuf * m)3972 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
3973 {
3974 
3975 	/* XXX TODO */
3976 	IMPROVE();
3977 
3978 	/* Quick and dirty cheating hack. */
3979 	struct ieee80211_node *ni;
3980 
3981 	ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3982 	return (lkpi_ic_raw_xmit(ni, m, NULL));
3983 }
3984 
3985 #ifdef LKPI_80211_HT
3986 static int
lkpi_ic_recv_action(struct ieee80211_node * ni,const struct ieee80211_frame * wh,const uint8_t * frm,const uint8_t * efrm)3987 lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
3988     const uint8_t *frm, const uint8_t *efrm)
3989 {
3990 	struct ieee80211com *ic;
3991 	struct lkpi_hw *lhw;
3992 
3993 	ic = ni->ni_ic;
3994 	lhw = ic->ic_softc;
3995 
3996 	IMPROVE_HT("recv_action called; nothing to do in lkpi; make debugging");
3997 
3998 	return (lhw->ic_recv_action(ni, wh, frm, efrm));
3999 }
4000 
4001 static int
lkpi_ic_send_action(struct ieee80211_node * ni,int category,int action,void * sa)4002 lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
4003 {
4004 	struct ieee80211com *ic;
4005 	struct lkpi_hw *lhw;
4006 
4007 	ic = ni->ni_ic;
4008 	lhw = ic->ic_softc;
4009 
4010 	IMPROVE_HT("send_action called; nothing to do in lkpi; make debugging");
4011 
4012 	return (lhw->ic_send_action(ni, category, action, sa));
4013 }
4014 
4015 
4016 static int
lkpi_ic_ampdu_enable(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)4017 lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
4018 {
4019 	struct ieee80211com *ic;
4020 	struct lkpi_hw *lhw;
4021 
4022 	ic = ni->ni_ic;
4023 	lhw = ic->ic_softc;
4024 
4025 	IMPROVE_HT("ieee80211_ampdu_enable called; nothing to do in lkpi for now; make debugging");
4026 
4027 	return (lhw->ic_ampdu_enable(ni, tap));
4028 }
4029 
4030 /*
4031  * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
4032  * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
4033  *
4034  * NB: returns 0 on ERROR!
4035  */
4036 static int
lkpi_ic_addba_request(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int dialogtoken,int baparamset,int batimeout)4037 lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4038     int dialogtoken, int baparamset, int batimeout)
4039 {
4040 	struct ieee80211com *ic;
4041 	struct lkpi_hw *lhw;
4042 	struct ieee80211_hw *hw;
4043 	struct ieee80211vap *vap;
4044 	struct lkpi_vif *lvif;
4045 	struct ieee80211_vif *vif;
4046 	struct lkpi_sta *lsta;
4047 	struct ieee80211_sta *sta;
4048 	struct ieee80211_ampdu_params params = { };
4049 	int error;
4050 
4051 	ic = ni->ni_ic;
4052 	lhw = ic->ic_softc;
4053 	hw = LHW_TO_HW(lhw);
4054 	vap = ni->ni_vap;
4055 	lvif = VAP_TO_LVIF(vap);
4056 	vif = LVIF_TO_VIF(lvif);
4057 	lsta = ni->ni_drv_data;
4058 	sta = LSTA_TO_STA(lsta);
4059 
4060 	if (!lsta->added_to_drv) {
4061 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4062 		    __func__, lsta, ni, sta);
4063 		return (0);
4064 	}
4065 
4066 	params.sta = sta;
4067 	params.action = IEEE80211_AMPDU_TX_START;
4068 	/* Keep 0 here! */
4069 	params.buf_size = 0;
4070 	params.timeout = 0;
4071 	params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
4072 	params.tid = tap->txa_tid;
4073 	params.amsdu = false;
4074 
4075 	IEEE80211_UNLOCK(ic);
4076 	LKPI_80211_LHW_LOCK(lhw);
4077 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4078 	LKPI_80211_LHW_UNLOCK(lhw);
4079 	IEEE80211_LOCK(ic);
4080 	if (error != 0) {
4081 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4082 		    __func__, error, ni, tap);
4083 		return (0);
4084 	}
4085 
4086 	return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
4087 }
4088 
4089 /*
4090  * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
4091  * and calls the default ieee80211_addba_response() which always returns 1.
4092  *
4093  * NB: No error checking in net80211!
4094  * Staying with 0 is an error.
4095  */
4096 static int
lkpi_ic_addba_response(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int status,int baparamset,int batimeout)4097 lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4098     int status, int baparamset, int batimeout)
4099 {
4100 	struct ieee80211com *ic;
4101 	struct lkpi_hw *lhw;
4102 	struct ieee80211_hw *hw;
4103 	struct ieee80211vap *vap;
4104 	struct lkpi_vif *lvif;
4105 	struct ieee80211_vif *vif;
4106 	struct lkpi_sta *lsta;
4107 	struct ieee80211_sta *sta;
4108 	struct ieee80211_ampdu_params params = { };
4109 	int error;
4110 
4111 	ic = ni->ni_ic;
4112 	lhw = ic->ic_softc;
4113 	hw = LHW_TO_HW(lhw);
4114 	vap = ni->ni_vap;
4115 	lvif = VAP_TO_LVIF(vap);
4116 	vif = LVIF_TO_VIF(lvif);
4117 	lsta = ni->ni_drv_data;
4118 	sta = LSTA_TO_STA(lsta);
4119 
4120 	if (!lsta->added_to_drv) {
4121 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4122 		    __func__, lsta, ni, sta);
4123 		return (0);
4124 	}
4125 
4126 	if (status == IEEE80211_STATUS_SUCCESS) {
4127 		params.sta = sta;
4128 		params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
4129 		params.buf_size = tap->txa_wnd;
4130 		params.timeout = 0;
4131 		params.ssn = 0;
4132 		params.tid = tap->txa_tid;
4133 		if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
4134 			params.amsdu = true;
4135 		else
4136 			params.amsdu = false;
4137 	} else {
4138 		/* We need to free the allocated resources. */
4139 		params.sta = sta;
4140 		switch (status) {
4141 			/* params.action = FLUSH, FLUSH_CONT */
4142 		default:
4143 			params.action = IEEE80211_AMPDU_TX_STOP_CONT;
4144 			break;
4145 		}
4146 		params.buf_size = 0;
4147 		params.timeout = 0;
4148 		params.ssn = 0;
4149 		params.tid = tap->txa_tid;
4150 		params.amsdu = false;
4151 	}
4152 
4153 	IEEE80211_UNLOCK(ic);
4154 	LKPI_80211_LHW_LOCK(lhw);
4155 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4156 	LKPI_80211_LHW_UNLOCK(lhw);
4157 	IEEE80211_LOCK(ic);
4158 	if (error != 0) {
4159 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4160 		    __func__, error, ni, tap);
4161 		return (0);
4162 	}
4163 
4164 	IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
4165 
4166 	return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
4167 }
4168 
4169 /*
4170  * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
4171  * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
4172  */
4173 static void
lkpi_ic_addba_stop(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)4174 lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
4175 {
4176 	struct ieee80211com *ic;
4177 	struct lkpi_hw *lhw;
4178 	struct ieee80211_hw *hw;
4179 	struct ieee80211vap *vap;
4180 	struct lkpi_vif *lvif;
4181 	struct ieee80211_vif *vif;
4182 	struct lkpi_sta *lsta;
4183 	struct ieee80211_sta *sta;
4184 	struct ieee80211_ampdu_params params = { };
4185 	int error;
4186 
4187 	ic = ni->ni_ic;
4188 	lhw = ic->ic_softc;
4189 	hw = LHW_TO_HW(lhw);
4190 	vap = ni->ni_vap;
4191 	lvif = VAP_TO_LVIF(vap);
4192 	vif = LVIF_TO_VIF(lvif);
4193 	lsta = ni->ni_drv_data;
4194 	sta = LSTA_TO_STA(lsta);
4195 
4196 	if (!lsta->added_to_drv) {
4197 		ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
4198 		    __func__, lsta, ni, sta);
4199 		goto n80211;
4200 	}
4201 
4202 	/* We need to free the allocated resources. */
4203 	params.sta = sta;
4204 	IMPROVE("net80211 does not provide a reason to us");
4205 	params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
4206 	params.buf_size = 0;
4207 	params.timeout = 0;
4208 	params.ssn = 0;
4209 	params.tid = tap->txa_tid;
4210 	params.amsdu = false;
4211 
4212 	IEEE80211_UNLOCK(ic);
4213 	LKPI_80211_LHW_LOCK(lhw);
4214 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4215 	LKPI_80211_LHW_UNLOCK(lhw);
4216 	IEEE80211_LOCK(ic);
4217 	if (error != 0) {
4218 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
4219 		    __func__, error, ni, tap);
4220 		goto n80211;
4221 	}
4222 
4223 	IMPROVE_HT("anyting else?");
4224 
4225 n80211:
4226 	lhw->ic_addba_stop(ni, tap);
4227 }
4228 
4229 static void
lkpi_ic_addba_response_timeout(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap)4230 lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
4231 {
4232 	struct ieee80211com *ic;
4233 	struct lkpi_hw *lhw;
4234 
4235 	ic = ni->ni_ic;
4236 	lhw = ic->ic_softc;
4237 
4238 	IMPROVE_HT();
4239 
4240 	lhw->ic_addba_response_timeout(ni, tap);
4241 }
4242 
4243 static void
lkpi_ic_bar_response(struct ieee80211_node * ni,struct ieee80211_tx_ampdu * tap,int status)4244 lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
4245     int status)
4246 {
4247 	struct ieee80211com *ic;
4248 	struct lkpi_hw *lhw;
4249 
4250 	ic = ni->ni_ic;
4251 	lhw = ic->ic_softc;
4252 
4253 	IMPROVE_HT();
4254 
4255 	lhw->ic_bar_response(ni, tap, status);
4256 }
4257 
4258 static int
lkpi_ic_ampdu_rx_start(struct ieee80211_node * ni,struct ieee80211_rx_ampdu * rap,int baparamset,int batimeout,int baseqctl)4259 lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
4260     int baparamset, int batimeout, int baseqctl)
4261 {
4262 	struct ieee80211com *ic;
4263 	struct lkpi_hw *lhw;
4264 	struct ieee80211_hw *hw;
4265 	struct ieee80211vap *vap;
4266 	struct lkpi_vif *lvif;
4267 	struct ieee80211_vif *vif;
4268 	struct lkpi_sta *lsta;
4269 	struct ieee80211_sta *sta;
4270 	struct ieee80211_ampdu_params params = { };
4271 	int error;
4272 
4273 	ic = ni->ni_ic;
4274 	lhw = ic->ic_softc;
4275 	hw = LHW_TO_HW(lhw);
4276 	vap = ni->ni_vap;
4277 	lvif = VAP_TO_LVIF(vap);
4278 	vif = LVIF_TO_VIF(lvif);
4279 	lsta = ni->ni_drv_data;
4280 	sta = LSTA_TO_STA(lsta);
4281 
4282 	IEEE80211_UNLOCK_ASSERT(ic);
4283 
4284 	if (!lsta->added_to_drv) {
4285 		ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
4286 		    __func__, lsta, ni, vap, sta);
4287 		return (-ENXIO);
4288 	}
4289 
4290 	params.sta = sta;
4291 	params.action = IEEE80211_AMPDU_RX_START;
4292 	params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
4293 	if (params.buf_size == 0)
4294 		params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
4295 	else
4296 		params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
4297 	if (hw->max_rx_aggregation_subframes > 0 &&
4298 	    params.buf_size > hw->max_rx_aggregation_subframes)
4299 		params.buf_size = hw->max_rx_aggregation_subframes;
4300 	params.timeout = le16toh(batimeout);
4301 	params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
4302 	params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
4303 
4304 	/* Based on net80211::ampdu_rx_start(). */
4305 	if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
4306 	    (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
4307 		params.amsdu = true;
4308 	else
4309 		params.amsdu = false;
4310 
4311 	LKPI_80211_LHW_LOCK(lhw);
4312 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4313 	LKPI_80211_LHW_UNLOCK(lhw);
4314 	if (error != 0) {
4315 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
4316 		    __func__, error, ni, rap);
4317 		return (error);
4318 	}
4319 
4320 	if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
4321 		IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
4322 	}
4323 
4324 	IMPROVE_HT("net80211 is missing the error check on return and assumes success");
4325 
4326 	error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
4327 	return (error);
4328 }
4329 
4330 static void
lkpi_ic_ampdu_rx_stop(struct ieee80211_node * ni,struct ieee80211_rx_ampdu * rap)4331 lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
4332 {
4333 	struct ieee80211com *ic;
4334 	struct lkpi_hw *lhw;
4335 	struct ieee80211_hw *hw;
4336 	struct ieee80211vap *vap;
4337 	struct lkpi_vif *lvif;
4338 	struct ieee80211_vif *vif;
4339 	struct lkpi_sta *lsta;
4340 	struct ieee80211_sta *sta;
4341 	struct ieee80211_ampdu_params params = { };
4342 	int error;
4343 	uint8_t tid;
4344 
4345 	ic = ni->ni_ic;
4346 	lhw = ic->ic_softc;
4347 
4348 	/*
4349 	 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
4350 	 * we did not START.  Some drivers pass it down to firmware which will
4351 	 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
4352 	 * ieee80211_ht_node_init() amongst others which will iterate over all
4353 	 * tid and call ic_ampdu_rx_stop() unconditionally.
4354 	 * XXX net80211 should probably be more "gentle" in these cases and
4355 	 * track some state itself.
4356 	 */
4357 	if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
4358 		goto net80211_only;
4359 
4360 	hw = LHW_TO_HW(lhw);
4361 	vap = ni->ni_vap;
4362 	lvif = VAP_TO_LVIF(vap);
4363 	vif = LVIF_TO_VIF(lvif);
4364 	lsta = ni->ni_drv_data;
4365 	sta = LSTA_TO_STA(lsta);
4366 
4367 	IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
4368 	for (tid = 0; tid < WME_NUM_TID; tid++) {
4369 		if (&ni->ni_rx_ampdu[tid] == rap)
4370 			break;
4371 	}
4372 
4373 	params.sta = sta;
4374 	params.action = IEEE80211_AMPDU_RX_STOP;
4375 	params.buf_size = 0;
4376 	params.timeout = 0;
4377 	params.ssn = 0;
4378 	params.tid = tid;
4379 	params.amsdu = false;
4380 
4381 	// IEEE80211_UNLOCK(ic);
4382 	LKPI_80211_LHW_LOCK(lhw);
4383 	error = lkpi_80211_mo_ampdu_action(hw, vif, &params);
4384 	LKPI_80211_LHW_UNLOCK(lhw);
4385 	// IEEE80211_LOCK(ic);
4386 	if (error != 0)
4387 		ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
4388 		    __func__, error, ni, rap);
4389 
4390 net80211_only:
4391 	lhw->ic_ampdu_rx_stop(ni, rap);
4392 }
4393 #endif
4394 
4395 static void
lkpi_ic_getradiocaps_ht(struct ieee80211com * ic,struct ieee80211_hw * hw,uint8_t * bands,int * chan_flags,enum nl80211_band band)4396 lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
4397     uint8_t *bands, int *chan_flags, enum nl80211_band band)
4398 {
4399 #ifdef LKPI_80211_HT
4400 	struct ieee80211_sta_ht_cap *ht_cap;
4401 
4402 	ht_cap = &hw->wiphy->bands[band]->ht_cap;
4403 	if (!ht_cap->ht_supported)
4404 		return;
4405 
4406 	switch (band) {
4407 	case NL80211_BAND_2GHZ:
4408 		setbit(bands, IEEE80211_MODE_11NG);
4409 		break;
4410 	case NL80211_BAND_5GHZ:
4411 		setbit(bands, IEEE80211_MODE_11NA);
4412 		break;
4413 	default:
4414 		IMPROVE("Unsupported band %d", band);
4415 		return;
4416 	}
4417 
4418 	ic->ic_htcaps = IEEE80211_HTC_HT;	/* HT operation */
4419 
4420 	/*
4421 	 * Rather than manually checking each flag and
4422 	 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
4423 	 * simply copy the 16bits.
4424 	 */
4425 	ic->ic_htcaps |= ht_cap->cap;
4426 
4427 	/* Then deal with the other flags. */
4428 	if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
4429 		ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
4430 #ifdef __notyet__
4431 	if (ieee80211_hw_check(hw, TX_AMSDU))
4432 		ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
4433 	if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
4434 		ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
4435 		    IEEE80211_HTC_TX_AMSDU_AMPDU);
4436 #endif
4437 
4438 	IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
4439 	ic->ic_htcaps |= IEEE80211_HTCAP_SMPS_OFF;
4440 
4441 	/* Only add HT40 channels if supported. */
4442 	if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
4443 	    chan_flags != NULL)
4444 		*chan_flags |= NET80211_CBW_FLAG_HT40;
4445 #endif
4446 }
4447 
4448 static void
lkpi_ic_getradiocaps(struct ieee80211com * ic,int maxchan,int * n,struct ieee80211_channel * c)4449 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
4450     int *n, struct ieee80211_channel *c)
4451 {
4452 	struct lkpi_hw *lhw;
4453 	struct ieee80211_hw *hw;
4454 	struct linuxkpi_ieee80211_channel *channels;
4455 	uint8_t bands[IEEE80211_MODE_BYTES];
4456 	int chan_flags, error, i, nchans;
4457 
4458 	/* Channels */
4459 	lhw = ic->ic_softc;
4460 	hw = LHW_TO_HW(lhw);
4461 
4462 	/* NL80211_BAND_2GHZ */
4463 	nchans = 0;
4464 	if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
4465 		nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
4466 	if (nchans > 0) {
4467 		memset(bands, 0, sizeof(bands));
4468 		chan_flags = 0;
4469 		setbit(bands, IEEE80211_MODE_11B);
4470 		/* XXX-BZ unclear how to check for 11g. */
4471 
4472 		IMPROVE("the bitrates may have flags?");
4473 		setbit(bands, IEEE80211_MODE_11G);
4474 
4475 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
4476 		    NL80211_BAND_2GHZ);
4477 
4478 		channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
4479 		for (i = 0; i < nchans && *n < maxchan; i++) {
4480 			uint32_t nflags = 0;
4481 			int cflags = chan_flags;
4482 
4483 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
4484 				ic_printf(ic, "%s: Skipping disabled chan "
4485 				    "[%u/%u/%#x]\n", __func__,
4486 				    channels[i].hw_value,
4487 				    channels[i].center_freq, channels[i].flags);
4488 				continue;
4489 			}
4490 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
4491 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
4492 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
4493 				nflags |= IEEE80211_CHAN_DFS;
4494 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
4495 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
4496 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
4497 				cflags &= ~NET80211_CBW_FLAG_VHT80;
4498 			/* XXX how to map the remaining enum ieee80211_channel_flags? */
4499 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
4500 				cflags &= ~NET80211_CBW_FLAG_HT40;
4501 
4502 			error = ieee80211_add_channel_cbw(c, maxchan, n,
4503 			    channels[i].hw_value, channels[i].center_freq,
4504 			    channels[i].max_power,
4505 			    nflags, bands, cflags);
4506 			/* net80211::ENOBUFS: *n >= maxchans */
4507 			if (error != 0 && error != ENOBUFS)
4508 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
4509 				    "returned error %d\n",
4510 				    __func__, channels[i].hw_value,
4511 				    channels[i].center_freq, channels[i].flags,
4512 				    nflags, chan_flags, cflags, error);
4513 			if (error != 0)
4514 				break;
4515 		}
4516 	}
4517 
4518 	/* NL80211_BAND_5GHZ */
4519 	nchans = 0;
4520 	if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
4521 		nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
4522 	if (nchans > 0) {
4523 		memset(bands, 0, sizeof(bands));
4524 		chan_flags = 0;
4525 		setbit(bands, IEEE80211_MODE_11A);
4526 
4527 		lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
4528 		    NL80211_BAND_5GHZ);
4529 
4530 #ifdef LKPI_80211_VHT
4531 		if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
4532 
4533 			ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
4534 			ic->ic_vht_cap.vht_cap_info =
4535 			    hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
4536 
4537 			setbit(bands, IEEE80211_MODE_VHT_5GHZ);
4538 			chan_flags |= NET80211_CBW_FLAG_VHT80;
4539 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
4540 			    ic->ic_vht_cap.vht_cap_info))
4541 				chan_flags |= NET80211_CBW_FLAG_VHT160;
4542 			if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
4543 			    ic->ic_vht_cap.vht_cap_info))
4544 				chan_flags |= NET80211_CBW_FLAG_VHT80P80;
4545 		}
4546 #endif
4547 
4548 		channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
4549 		for (i = 0; i < nchans && *n < maxchan; i++) {
4550 			uint32_t nflags = 0;
4551 			int cflags = chan_flags;
4552 
4553 			if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
4554 				ic_printf(ic, "%s: Skipping disabled chan "
4555 				    "[%u/%u/%#x]\n", __func__,
4556 				    channels[i].hw_value,
4557 				    channels[i].center_freq, channels[i].flags);
4558 				continue;
4559 			}
4560 			if (channels[i].flags & IEEE80211_CHAN_NO_IR)
4561 				nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
4562 			if (channels[i].flags & IEEE80211_CHAN_RADAR)
4563 				nflags |= IEEE80211_CHAN_DFS;
4564 			if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
4565 				cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
4566 			if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
4567 				cflags &= ~NET80211_CBW_FLAG_VHT80;
4568 			/* XXX hwo to map the remaining enum ieee80211_channel_flags? */
4569 			if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
4570 				cflags &= ~NET80211_CBW_FLAG_HT40;
4571 
4572 			error = ieee80211_add_channel_cbw(c, maxchan, n,
4573 			    channels[i].hw_value, channels[i].center_freq,
4574 			    channels[i].max_power,
4575 			    nflags, bands, cflags);
4576 			/* net80211::ENOBUFS: *n >= maxchans */
4577 			if (error != 0 && error != ENOBUFS)
4578 				ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
4579 				    "returned error %d\n",
4580 				    __func__, channels[i].hw_value,
4581 				    channels[i].center_freq, channels[i].flags,
4582 				    nflags, chan_flags, cflags, error);
4583 			if (error != 0)
4584 				break;
4585 		}
4586 	}
4587 }
4588 
4589 static void *
lkpi_ieee80211_ifalloc(void)4590 lkpi_ieee80211_ifalloc(void)
4591 {
4592 	struct ieee80211com *ic;
4593 
4594 	ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
4595 
4596 	/* Setting these happens later when we have device information. */
4597 	ic->ic_softc = NULL;
4598 	ic->ic_name = "linuxkpi";
4599 
4600 	return (ic);
4601 }
4602 
4603 struct ieee80211_hw *
linuxkpi_ieee80211_alloc_hw(size_t priv_len,const struct ieee80211_ops * ops)4604 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
4605 {
4606 	struct ieee80211_hw *hw;
4607 	struct lkpi_hw *lhw;
4608 	struct wiphy *wiphy;
4609 	int ac;
4610 
4611 	/* Get us and the driver data also allocated. */
4612 	wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
4613 	if (wiphy == NULL)
4614 		return (NULL);
4615 
4616 	lhw = wiphy_priv(wiphy);
4617 	lhw->ops = ops;
4618 
4619 	LKPI_80211_LHW_LOCK_INIT(lhw);
4620 	LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
4621 	LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
4622 	sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
4623 	TAILQ_INIT(&lhw->lvif_head);
4624 	for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4625 		lhw->txq_generation[ac] = 1;
4626 		TAILQ_INIT(&lhw->scheduled_txqs[ac]);
4627 	}
4628 
4629 	/* Deferred RX path. */
4630 	LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
4631 	TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
4632 	mbufq_init(&lhw->rxq, IFQ_MAXLEN);
4633 	lhw->rxq_stopped = false;
4634 
4635 	/*
4636 	 * XXX-BZ TODO make sure there is a "_null" function to all ops
4637 	 * not initialized.
4638 	 */
4639 	hw = LHW_TO_HW(lhw);
4640 	hw->wiphy = wiphy;
4641 	hw->conf.flags |= IEEE80211_CONF_IDLE;
4642 	hw->priv = (void *)(lhw + 1);
4643 
4644 	/* BSD Specific. */
4645 	lhw->ic = lkpi_ieee80211_ifalloc();
4646 
4647 	IMPROVE();
4648 
4649 	return (hw);
4650 }
4651 
4652 void
linuxkpi_ieee80211_iffree(struct ieee80211_hw * hw)4653 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
4654 {
4655 	struct lkpi_hw *lhw;
4656 	struct mbuf *m;
4657 
4658 	lhw = HW_TO_LHW(hw);
4659 	free(lhw->ic, M_LKPI80211);
4660 	lhw->ic = NULL;
4661 
4662 	/*
4663 	 * Drain the deferred RX path.
4664 	 */
4665 	LKPI_80211_LHW_RXQ_LOCK(lhw);
4666 	lhw->rxq_stopped = true;
4667 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
4668 
4669 	/* Drain taskq, won't be restarted due to rxq_stopped being set. */
4670 	while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
4671 		taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
4672 
4673 	/* Flush mbufq (make sure to release ni refs!). */
4674 	m = mbufq_dequeue(&lhw->rxq);
4675 	while (m != NULL) {
4676 		struct m_tag *mtag;
4677 
4678 		mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
4679 		if (mtag != NULL) {
4680 			struct lkpi_80211_tag_rxni *rxni;
4681 
4682 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
4683 			ieee80211_free_node(rxni->ni);
4684 		}
4685 		m_freem(m);
4686 		m = mbufq_dequeue(&lhw->rxq);
4687 	}
4688 	KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
4689 	    __func__, lhw, mbufq_len(&lhw->rxq)));
4690 	LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
4691 
4692 	/* Cleanup more of lhw here or in wiphy_free()? */
4693 	LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
4694 	LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
4695 	LKPI_80211_LHW_LOCK_DESTROY(lhw);
4696 	sx_destroy(&lhw->lvif_sx);
4697 	IMPROVE();
4698 }
4699 
4700 void
linuxkpi_set_ieee80211_dev(struct ieee80211_hw * hw,char * name)4701 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
4702 {
4703 	struct lkpi_hw *lhw;
4704 	struct ieee80211com *ic;
4705 
4706 	lhw = HW_TO_LHW(hw);
4707 	ic = lhw->ic;
4708 
4709 	/* Now set a proper name before ieee80211_ifattach(). */
4710 	ic->ic_softc = lhw;
4711 	ic->ic_name = name;
4712 
4713 	/* XXX-BZ do we also need to set wiphy name? */
4714 }
4715 
4716 struct ieee80211_hw *
linuxkpi_wiphy_to_ieee80211_hw(struct wiphy * wiphy)4717 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
4718 {
4719 	struct lkpi_hw *lhw;
4720 
4721 	lhw = wiphy_priv(wiphy);
4722 	return (LHW_TO_HW(lhw));
4723 }
4724 
4725 static void
lkpi_radiotap_attach(struct lkpi_hw * lhw)4726 lkpi_radiotap_attach(struct lkpi_hw *lhw)
4727 {
4728 	struct ieee80211com *ic;
4729 
4730 	ic = lhw->ic;
4731 	ieee80211_radiotap_attach(ic,
4732 	    &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
4733 	    LKPI_RTAP_TX_FLAGS_PRESENT,
4734 	    &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
4735 	    LKPI_RTAP_RX_FLAGS_PRESENT);
4736 }
4737 
4738 int
linuxkpi_ieee80211_ifattach(struct ieee80211_hw * hw)4739 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
4740 {
4741 	struct ieee80211com *ic;
4742 	struct lkpi_hw *lhw;
4743 	int band, i;
4744 
4745 	lhw = HW_TO_LHW(hw);
4746 	ic = lhw->ic;
4747 
4748 	/* We do it this late as wiphy->dev should be set for the name. */
4749 	lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
4750 	if (lhw->workq == NULL)
4751 		return (-EAGAIN);
4752 
4753 	/* XXX-BZ figure this out how they count his... */
4754 	if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
4755 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
4756 		    hw->wiphy->perm_addr);
4757 	} else if (hw->wiphy->n_addresses > 0) {
4758 		/* We take the first one. */
4759 		IEEE80211_ADDR_COPY(ic->ic_macaddr,
4760 		    hw->wiphy->addresses[0].addr);
4761 	} else {
4762 		ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
4763 	}
4764 
4765 #ifdef __not_yet__
4766 	/* See comment in lkpi_80211_txq_tx_one(). */
4767 	ic->ic_headroom = hw->extra_tx_headroom;
4768 #endif
4769 
4770 	ic->ic_phytype = IEEE80211_T_OFDM;	/* not only, but not used */
4771 	ic->ic_opmode = IEEE80211_M_STA;
4772 
4773 	/* Set device capabilities. */
4774 	/* XXX-BZ we need to get these from linux80211/drivers and convert. */
4775 	ic->ic_caps =
4776 	    IEEE80211_C_STA |
4777 	    IEEE80211_C_MONITOR |
4778 	    IEEE80211_C_WPA |		/* WPA/RSN */
4779 #ifdef LKPI_80211_WME
4780 	    IEEE80211_C_WME |
4781 #endif
4782 #if 0
4783 	    IEEE80211_C_PMGT |
4784 #endif
4785 	    IEEE80211_C_SHSLOT |	/* short slot time supported */
4786 	    IEEE80211_C_SHPREAMBLE	/* short preamble supported */
4787 	    ;
4788 #if 0
4789 	/* Scanning is a different kind of beast to re-work. */
4790 	ic->ic_caps |= IEEE80211_C_BGSCAN;
4791 #endif
4792 	if (lhw->ops->hw_scan) {
4793 		/*
4794 		 * Advertise full-offload scanning.
4795 		 *
4796 		 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4797 		 * we essentially disable hw_scan for all drivers not setting
4798 		 * the flag.
4799 		 */
4800 		ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4801 		lhw->scan_flags |= LKPI_LHW_SCAN_HW;
4802 	}
4803 
4804 	/*
4805 	 * The wiphy variables report bitmasks of avail antennas.
4806 	 * (*get_antenna) get the current bitmask sets which can be
4807 	 * altered by (*set_antenna) for some drivers.
4808 	 * XXX-BZ will the count alone do us much good long-term in net80211?
4809 	 */
4810 	if (hw->wiphy->available_antennas_rx ||
4811 	    hw->wiphy->available_antennas_tx) {
4812 		uint32_t rxs, txs;
4813 
4814 		if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
4815 			ic->ic_rxstream = bitcount32(rxs);
4816 			ic->ic_txstream = bitcount32(txs);
4817 		}
4818 	}
4819 
4820 	ic->ic_cryptocaps = 0;
4821 #ifdef LKPI_80211_HW_CRYPTO
4822 	if (hw->wiphy->n_cipher_suites > 0) {
4823 		for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
4824 			ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
4825 			    hw->wiphy->cipher_suites[i]);
4826 	}
4827 #endif
4828 
4829 	lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
4830 	    ic->ic_channels);
4831 
4832 	ieee80211_ifattach(ic);
4833 
4834 	ic->ic_update_mcast = lkpi_ic_update_mcast;
4835 	ic->ic_update_promisc = lkpi_ic_update_promisc;
4836 	ic->ic_update_chw = lkpi_ic_update_chw;
4837 	ic->ic_parent = lkpi_ic_parent;
4838 	ic->ic_scan_start = lkpi_ic_scan_start;
4839 	ic->ic_scan_end = lkpi_ic_scan_end;
4840 	ic->ic_set_channel = lkpi_ic_set_channel;
4841 	ic->ic_transmit = lkpi_ic_transmit;
4842 	ic->ic_raw_xmit = lkpi_ic_raw_xmit;
4843 	ic->ic_vap_create = lkpi_ic_vap_create;
4844 	ic->ic_vap_delete = lkpi_ic_vap_delete;
4845 	ic->ic_getradiocaps = lkpi_ic_getradiocaps;
4846 	ic->ic_wme.wme_update = lkpi_ic_wme_update;
4847 
4848 	lhw->ic_scan_curchan = ic->ic_scan_curchan;
4849 	ic->ic_scan_curchan = lkpi_ic_scan_curchan;
4850 	lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
4851 	ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
4852 
4853 	lhw->ic_node_alloc = ic->ic_node_alloc;
4854 	ic->ic_node_alloc = lkpi_ic_node_alloc;
4855 	lhw->ic_node_init = ic->ic_node_init;
4856 	ic->ic_node_init = lkpi_ic_node_init;
4857 	lhw->ic_node_cleanup = ic->ic_node_cleanup;
4858 	ic->ic_node_cleanup = lkpi_ic_node_cleanup;
4859 	lhw->ic_node_free = ic->ic_node_free;
4860 	ic->ic_node_free = lkpi_ic_node_free;
4861 
4862 #ifdef LKPI_80211_HT
4863 	/*
4864 	 * Only attach if the driver/firmware supports (*ampdu_action)().
4865 	 * Otherwise it is in the hands of net80211.
4866 	 */
4867 	if (lhw->ops->ampdu_action != NULL) {
4868 		lhw->ic_recv_action = ic->ic_recv_action;
4869 		ic->ic_recv_action = lkpi_ic_recv_action;
4870 		lhw->ic_send_action = ic->ic_send_action;
4871 		ic->ic_send_action = lkpi_ic_send_action;
4872 
4873 		lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
4874 		ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
4875 
4876 		lhw->ic_addba_request = ic->ic_addba_request;
4877 		ic->ic_addba_request = lkpi_ic_addba_request;
4878 		lhw->ic_addba_response = ic->ic_addba_response;
4879 		ic->ic_addba_response = lkpi_ic_addba_response;
4880 		lhw->ic_addba_stop = ic->ic_addba_stop;
4881 		ic->ic_addba_stop = lkpi_ic_addba_stop;
4882 		lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
4883 		ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
4884 
4885 		lhw->ic_bar_response = ic->ic_bar_response;
4886 		ic->ic_bar_response = lkpi_ic_bar_response;
4887 
4888 		lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
4889 		ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
4890 		lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
4891 		ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
4892 	}
4893 #endif
4894 
4895 	lkpi_radiotap_attach(lhw);
4896 
4897 	/*
4898 	 * Assign the first possible channel for now;  seems Realtek drivers
4899 	 * expect one.
4900 	 * Also remember the amount of bands we support and the most rates
4901 	 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
4902 	 */
4903 	lhw->supbands = lhw->max_rates = 0;
4904 	for (band = 0; band < NUM_NL80211_BANDS; band++) {
4905 		struct ieee80211_supported_band *supband;
4906 		struct linuxkpi_ieee80211_channel *channels;
4907 
4908 		supband = hw->wiphy->bands[band];
4909 		if (supband == NULL || supband->n_channels == 0)
4910 			continue;
4911 
4912 		lhw->supbands++;
4913 		lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
4914 
4915 		/* If we have a channel, we need to keep counting supbands. */
4916 		if (hw->conf.chandef.chan != NULL)
4917 			continue;
4918 
4919 		channels = supband->channels;
4920 		for (i = 0; i < supband->n_channels; i++) {
4921 
4922 			if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4923 				continue;
4924 
4925 			cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
4926 #ifdef LKPI_80211_HT
4927 			    (ic->ic_htcaps & IEEE80211_HTC_HT) ? 0 :
4928 #endif
4929 			    NL80211_CHAN_NO_HT);
4930 			break;
4931 		}
4932 	}
4933 
4934 	IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
4935 
4936 	/* Make sure we do not support more than net80211 is willing to take. */
4937 	if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
4938 		ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
4939 		    lhw->max_rates, IEEE80211_RATE_MAXSIZE);
4940 		lhw->max_rates = IEEE80211_RATE_MAXSIZE;
4941 	}
4942 
4943 	/*
4944 	 * The maximum supported bitrates on any band + size for
4945 	 * DSSS Parameter Set give our per-band IE size.
4946 	 * SSID is the responsibility of the driver and goes on the side.
4947 	 * The user specified bits coming from the vap go into the
4948 	 * "common ies" fields.
4949 	 */
4950 	lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
4951 	if (lhw->max_rates > IEEE80211_RATE_SIZE)
4952 		lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
4953 
4954 	if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
4955 		/*
4956 		 * net80211 does not seem to support the DSSS Parameter Set but
4957 		 * some of the drivers insert it so calculate the extra fixed
4958 		 * space in.
4959 		 */
4960 		lhw->scan_ie_len += 2 + 1;
4961 	}
4962 
4963 #if defined(LKPI_80211_HT)
4964 	if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
4965 		lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
4966 #endif
4967 #if defined(LKPI_80211_VHT)
4968 	if ((ic->ic_flags_ext & IEEE80211_FEXT_VHT) != 0)
4969 		lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
4970 #endif
4971 
4972 	/* Reduce the max_scan_ie_len "left" by the amount we consume already. */
4973 	if (hw->wiphy->max_scan_ie_len > 0) {
4974 		if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
4975 			goto err;
4976 		hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
4977 	}
4978 
4979 	if (bootverbose)
4980 		ieee80211_announce(ic);
4981 
4982 	return (0);
4983 err:
4984 	IMPROVE("TODO FIXME CLEANUP");
4985 	return (-EAGAIN);
4986 }
4987 
4988 void
linuxkpi_ieee80211_ifdetach(struct ieee80211_hw * hw)4989 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
4990 {
4991 	struct lkpi_hw *lhw;
4992 	struct ieee80211com *ic;
4993 
4994 	lhw = HW_TO_LHW(hw);
4995 	ic = lhw->ic;
4996 	ieee80211_ifdetach(ic);
4997 }
4998 
4999 void
linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw * hw,enum ieee80211_iface_iter flags,void (* iterfunc)(void *,uint8_t *,struct ieee80211_vif *),void * arg)5000 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
5001     enum ieee80211_iface_iter flags,
5002     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
5003     void *arg)
5004 {
5005 	struct lkpi_hw *lhw;
5006 	struct lkpi_vif *lvif;
5007 	struct ieee80211_vif *vif;
5008 	bool active, atomic, nin_drv;
5009 
5010 	lhw = HW_TO_LHW(hw);
5011 
5012 	if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
5013 	    IEEE80211_IFACE_ITER_RESUME_ALL|
5014 	    IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
5015 	    IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
5016 		ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
5017 		    __func__, flags);
5018 	}
5019 
5020 	active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
5021 	atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
5022 	nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
5023 
5024 	if (atomic)
5025 		LKPI_80211_LHW_LVIF_LOCK(lhw);
5026 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5027 		struct ieee80211vap *vap;
5028 
5029 		vif = LVIF_TO_VIF(lvif);
5030 
5031 		/*
5032 		 * If we want "active" interfaces, we need to distinguish on
5033 		 * whether the driver knows about them or not to be able to
5034 		 * handle the "resume" case correctly.  Skip the ones the
5035 		 * driver does not know about.
5036 		 */
5037 		if (active && !lvif->added_to_drv &&
5038 		    (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
5039 			continue;
5040 
5041 		/*
5042 		 * If we shall skip interfaces not added to the driver do so
5043 		 * if we haven't yet.
5044 		 */
5045 		if (nin_drv && !lvif->added_to_drv)
5046 			continue;
5047 
5048 		/*
5049 		 * Run the iterator function if we are either not asking
5050 		 * asking for active only or if the VAP is "running".
5051 		 */
5052 		/* XXX-BZ probably should have state in the lvif as well. */
5053 		vap = LVIF_TO_VAP(lvif);
5054 		if (!active || (vap->iv_state != IEEE80211_S_INIT))
5055 			iterfunc(arg, vif->addr, vif);
5056 	}
5057 	if (atomic)
5058 		LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5059 }
5060 
5061 void
linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,void (* iterfunc)(struct ieee80211_hw *,struct ieee80211_vif *,struct ieee80211_sta *,struct ieee80211_key_conf *,void *),void * arg)5062 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
5063     struct ieee80211_vif *vif,
5064     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
5065         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
5066     void *arg)
5067 {
5068 
5069 	UNIMPLEMENTED;
5070 }
5071 
5072 void
linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw * hw,void (* iterfunc)(struct ieee80211_hw *,struct ieee80211_chanctx_conf *,void *),void * arg)5073 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
5074     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
5075 	void *),
5076     void *arg)
5077 {
5078 	struct lkpi_hw *lhw;
5079 	struct lkpi_vif *lvif;
5080 	struct ieee80211_vif *vif;
5081 	struct lkpi_chanctx *lchanctx;
5082 
5083 	KASSERT(hw != NULL && iterfunc != NULL,
5084 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5085 
5086 	lhw = HW_TO_LHW(hw);
5087 
5088 	IMPROVE("lchanctx should be its own list somewhere");
5089 
5090 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5091 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5092 
5093 		vif = LVIF_TO_VIF(lvif);
5094 		if (vif->chanctx_conf == NULL)
5095 			continue;
5096 
5097 		lchanctx = CHANCTX_CONF_TO_LCHANCTX(vif->chanctx_conf);
5098 		if (!lchanctx->added_to_drv)
5099 			continue;
5100 
5101 		iterfunc(hw, &lchanctx->chanctx_conf, arg);
5102 	}
5103 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5104 }
5105 
5106 void
linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw * hw,void (* iterfunc)(void *,struct ieee80211_sta *),void * arg)5107 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
5108    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
5109 {
5110 	struct lkpi_hw *lhw;
5111 	struct lkpi_vif *lvif;
5112 	struct lkpi_sta *lsta;
5113 	struct ieee80211_sta *sta;
5114 
5115 	KASSERT(hw != NULL && iterfunc != NULL,
5116 	    ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
5117 
5118 	lhw = HW_TO_LHW(hw);
5119 
5120 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5121 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5122 
5123 		LKPI_80211_LVIF_LOCK(lvif);
5124 		TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
5125 			if (!lsta->added_to_drv)
5126 				continue;
5127 			sta = LSTA_TO_STA(lsta);
5128 			iterfunc(arg, sta);
5129 		}
5130 		LKPI_80211_LVIF_UNLOCK(lvif);
5131 	}
5132 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5133 }
5134 
5135 struct linuxkpi_ieee80211_regdomain *
lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)5136 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
5137 {
5138 	struct linuxkpi_ieee80211_regdomain *regd;
5139 
5140 	regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
5141 	    GFP_KERNEL);
5142 	return (regd);
5143 }
5144 
5145 int
linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy * wiphy,struct linuxkpi_ieee80211_regdomain * regd)5146 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
5147     struct linuxkpi_ieee80211_regdomain *regd)
5148 {
5149 	struct lkpi_hw *lhw;
5150 	struct ieee80211com *ic;
5151 	struct ieee80211_regdomain *rd;
5152 
5153 	lhw = wiphy_priv(wiphy);
5154 	ic = lhw->ic;
5155 
5156 	rd = &ic->ic_regdomain;
5157 	if (rd->isocc[0] == '\0') {
5158 		rd->isocc[0] = regd->alpha2[0];
5159 		rd->isocc[1] = regd->alpha2[1];
5160 	}
5161 
5162 	TODO();
5163 	/* XXX-BZ finish the rest. */
5164 
5165 	return (0);
5166 }
5167 
5168 void
linuxkpi_ieee80211_scan_completed(struct ieee80211_hw * hw,struct cfg80211_scan_info * info)5169 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
5170     struct cfg80211_scan_info *info)
5171 {
5172 	struct lkpi_hw *lhw;
5173 	struct ieee80211com *ic;
5174 	struct ieee80211_scan_state *ss;
5175 
5176 	lhw = wiphy_priv(hw->wiphy);
5177 	ic = lhw->ic;
5178 	ss = ic->ic_scan;
5179 
5180 	ieee80211_scan_done(ss->ss_vap);
5181 
5182 	LKPI_80211_LHW_SCAN_LOCK(lhw);
5183 	free(lhw->hw_req, M_LKPI80211);
5184 	lhw->hw_req = NULL;
5185 	lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
5186 	wakeup(lhw);
5187 	LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5188 
5189 	return;
5190 }
5191 
5192 static void
lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw * lhw,struct mbuf * m)5193 lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
5194 {
5195 	struct ieee80211_node *ni;
5196 	struct m_tag *mtag;
5197 	int ok;
5198 
5199 	ni = NULL;
5200         mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
5201 	if (mtag != NULL) {
5202 		struct lkpi_80211_tag_rxni *rxni;
5203 
5204 		rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5205 		ni = rxni->ni;
5206 	}
5207 
5208 	if (ni != NULL) {
5209 		ok = ieee80211_input_mimo(ni, m);
5210 		ieee80211_free_node(ni);		/* Release the reference. */
5211 		if (ok < 0)
5212 			m_freem(m);
5213 	} else {
5214 		ok = ieee80211_input_mimo_all(lhw->ic, m);
5215 		/* mbuf got consumed. */
5216 	}
5217 
5218 #ifdef LINUXKPI_DEBUG_80211
5219 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5220 		printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
5221 #endif
5222 }
5223 
5224 static void
lkpi_80211_lhw_rxq_task(void * ctx,int pending)5225 lkpi_80211_lhw_rxq_task(void *ctx, int pending)
5226 {
5227 	struct lkpi_hw *lhw;
5228 	struct mbufq mq;
5229 	struct mbuf *m;
5230 
5231 	lhw = ctx;
5232 
5233 #ifdef LINUXKPI_DEBUG_80211
5234 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5235 		printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
5236 		    __func__, lhw, pending, mbufq_len(&lhw->rxq));
5237 #endif
5238 
5239 	mbufq_init(&mq, IFQ_MAXLEN);
5240 
5241 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5242 	mbufq_concat(&mq, &lhw->rxq);
5243 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5244 
5245 	m = mbufq_dequeue(&mq);
5246 	while (m != NULL) {
5247 		lkpi_80211_lhw_rxq_rx_one(lhw, m);
5248 		m = mbufq_dequeue(&mq);
5249 	}
5250 }
5251 
5252 /* For %list see comment towards the end of the function. */
5253 void
linuxkpi_ieee80211_rx(struct ieee80211_hw * hw,struct sk_buff * skb,struct ieee80211_sta * sta,struct napi_struct * napi __unused,struct list_head * list __unused)5254 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
5255     struct ieee80211_sta *sta, struct napi_struct *napi __unused,
5256     struct list_head *list __unused)
5257 {
5258 	struct lkpi_hw *lhw;
5259 	struct ieee80211com *ic;
5260 	struct mbuf *m;
5261 	struct skb_shared_info *shinfo;
5262 	struct ieee80211_rx_status *rx_status;
5263 	struct ieee80211_rx_stats rx_stats;
5264 	struct ieee80211_node *ni;
5265 	struct ieee80211vap *vap;
5266 	struct ieee80211_hdr *hdr;
5267 	struct lkpi_sta *lsta;
5268 	int i, offset, ok;
5269 	int8_t rssi;
5270 	bool is_beacon;
5271 
5272 	if (skb->len < 2) {
5273 		/* Need 80211 stats here. */
5274 		IMPROVE();
5275 		goto err;
5276 	}
5277 
5278 	/*
5279 	 * For now do the data copy; we can later improve things. Might even
5280 	 * have an mbuf backing the skb data then?
5281 	 */
5282 	m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
5283 	if (m == NULL)
5284 		goto err;
5285 	m_copyback(m, 0, skb->tail - skb->data, skb->data);
5286 
5287 	shinfo = skb_shinfo(skb);
5288 	offset = m->m_len;
5289 	for (i = 0; i < shinfo->nr_frags; i++) {
5290 		m_copyback(m, offset, shinfo->frags[i].size,
5291 		    (uint8_t *)linux_page_address(shinfo->frags[i].page) +
5292 		    shinfo->frags[i].offset);
5293 		offset += shinfo->frags[i].size;
5294 	}
5295 
5296 	rx_status = IEEE80211_SKB_RXCB(skb);
5297 
5298 	hdr = (void *)skb->data;
5299 	is_beacon = ieee80211_is_beacon(hdr->frame_control);
5300 
5301 #ifdef LINUXKPI_DEBUG_80211
5302 	if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
5303 		goto no_trace_beacons;
5304 
5305 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5306 		printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
5307 		    "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
5308 		    __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
5309 		    skb->truesize, skb->head, skb->data, skb->tail, skb->end,
5310 		    shinfo, shinfo->nr_frags,
5311 		    m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
5312 
5313 	if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
5314 		hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
5315 
5316 	/* Implement a dump_rxcb() !!! */
5317 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5318 		printf("TRACE-RX: %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
5319 		    "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
5320 			__func__,
5321 			(uintmax_t)rx_status->boottime_ns,
5322 			(uintmax_t)rx_status->mactime,
5323 			rx_status->device_timestamp,
5324 			rx_status->flag,
5325 			rx_status->freq,
5326 			rx_status->bw,
5327 			rx_status->encoding,
5328 			rx_status->ampdu_reference,
5329 			rx_status->band,
5330 			rx_status->chains,
5331 			rx_status->chain_signal[0],
5332 			rx_status->chain_signal[1],
5333 			rx_status->chain_signal[2],
5334 			rx_status->chain_signal[3],
5335 			rx_status->signal,
5336 			rx_status->enc_flags,
5337 			rx_status->he_dcm,
5338 			rx_status->he_gi,
5339 			rx_status->he_ru,
5340 			rx_status->zero_length_psdu_type,
5341 			rx_status->nss,
5342 			rx_status->rate_idx);
5343 no_trace_beacons:
5344 #endif
5345 
5346 	memset(&rx_stats, 0, sizeof(rx_stats));
5347 	rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
5348 	/* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
5349 	rx_stats.c_nf = -96;
5350 	if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
5351 	    !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
5352 		rssi = rx_status->signal;
5353 	else
5354 		rssi = rx_stats.c_nf;
5355 	/*
5356 	 * net80211 signal strength data are in .5 dBm units relative to
5357 	 * the current noise floor (see comment in ieee80211_node.h).
5358 	 */
5359 	rssi -= rx_stats.c_nf;
5360 	rx_stats.c_rssi = rssi * 2;
5361 	rx_stats.r_flags |= IEEE80211_R_BAND;
5362 	rx_stats.c_band =
5363 	    lkpi_nl80211_band_to_net80211_band(rx_status->band);
5364 	rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
5365 	rx_stats.c_freq = rx_status->freq;
5366 	rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
5367 
5368 	/* XXX (*sta_statistics)() to get to some of that? */
5369 	/* XXX-BZ dump the FreeBSD version of rx_stats as well! */
5370 
5371 	lhw = HW_TO_LHW(hw);
5372 	ic = lhw->ic;
5373 
5374 	ok = ieee80211_add_rx_params(m, &rx_stats);
5375 	if (ok == 0) {
5376 		m_freem(m);
5377 		counter_u64_add(ic->ic_ierrors, 1);
5378 		goto err;
5379 	}
5380 
5381 	lsta = NULL;
5382 	if (sta != NULL) {
5383 		lsta = STA_TO_LSTA(sta);
5384 		ni = ieee80211_ref_node(lsta->ni);
5385 	} else {
5386 		struct ieee80211_frame_min *wh;
5387 
5388 		wh = mtod(m, struct ieee80211_frame_min *);
5389 		ni = ieee80211_find_rxnode(ic, wh);
5390 		if (ni != NULL)
5391 			lsta = ni->ni_drv_data;
5392 	}
5393 
5394 	if (ni != NULL)
5395 		vap = ni->ni_vap;
5396 	else
5397 		/*
5398 		 * XXX-BZ can we improve this by looking at the frame hdr
5399 		 * or other meta-data passed up?
5400 		 */
5401 		vap = TAILQ_FIRST(&ic->ic_vaps);
5402 
5403 #ifdef LINUXKPI_DEBUG_80211
5404 	if (linuxkpi_debug_80211 & D80211_TRACE_RX)
5405 		printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
5406 		    __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
5407 		    ni, vap, is_beacon ? " beacon" : "");
5408 #endif
5409 
5410 	if (ni != NULL && vap != NULL && is_beacon &&
5411 	    rx_status->device_timestamp > 0 &&
5412 	    m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
5413 		struct lkpi_vif *lvif;
5414 		struct ieee80211_vif *vif;
5415 		struct ieee80211_frame *wh;
5416 
5417 		wh = mtod(m, struct ieee80211_frame *);
5418 		if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
5419 			goto skip_device_ts;
5420 
5421 		lvif = VAP_TO_LVIF(vap);
5422 		vif = LVIF_TO_VIF(lvif);
5423 
5424 		IMPROVE("TIMING_BEACON_ONLY?");
5425 		/* mac80211 specific (not net80211) so keep it here. */
5426 		vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
5427 		/*
5428 		 * net80211 should take care of the other information (sync_tsf,
5429 		 * sync_dtim_count) as otherwise we need to parse the beacon.
5430 		 */
5431 skip_device_ts:
5432 		;
5433 	}
5434 
5435 	if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
5436 	    ieee80211_radiotap_active_vap(vap)) {
5437 		struct lkpi_radiotap_rx_hdr *rtap;
5438 
5439 		rtap = &lhw->rtap_rx;
5440 		rtap->wr_tsft = rx_status->device_timestamp;
5441 		rtap->wr_flags = 0;
5442 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
5443 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
5444 		if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
5445 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
5446 #if 0	/* .. or it does not given we strip it below. */
5447 		if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
5448 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
5449 #endif
5450 		if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
5451 			rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
5452 		rtap->wr_rate = 0;
5453 		IMPROVE();
5454 		/* XXX TODO status->encoding / rate_index / bw */
5455 		rtap->wr_chan_freq = htole16(rx_stats.c_freq);
5456 		if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
5457 			rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
5458 		rtap->wr_dbm_antsignal = rssi;
5459 		rtap->wr_dbm_antnoise = rx_stats.c_nf;
5460 	}
5461 
5462 	if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
5463 		m_adj(m, -IEEE80211_CRC_LEN);
5464 
5465 #if 0
5466 	if (list != NULL) {
5467 		/*
5468 		* Normally this would be queued up and delivered by
5469 		* netif_receive_skb_list(), napi_gro_receive(), or the like.
5470 		* See mt76::mac80211.c as only current possible consumer.
5471 		*/
5472 		IMPROVE("we simply pass the packet to net80211 to deal with.");
5473 	}
5474 #endif
5475 
5476 	/*
5477 	 * Attach meta-information to the mbuf for the deferred RX path.
5478 	 * Currently this is best-effort.  Should we need to be hard,
5479 	 * drop the frame and goto err;
5480 	 */
5481 	if (ni != NULL) {
5482 		struct m_tag *mtag;
5483 		struct lkpi_80211_tag_rxni *rxni;
5484 
5485 		mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
5486 		    sizeof(*rxni), IEEE80211_M_NOWAIT);
5487 		if (mtag != NULL) {
5488 			rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
5489 			rxni->ni = ni;		/* We hold a reference. */
5490 			m_tag_prepend(m, mtag);
5491 		}
5492 	}
5493 
5494 	LKPI_80211_LHW_RXQ_LOCK(lhw);
5495 	if (lhw->rxq_stopped) {
5496 		LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5497 		m_freem(m);
5498 		goto err;
5499 	}
5500 
5501 	mbufq_enqueue(&lhw->rxq, m);
5502 	taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
5503 	LKPI_80211_LHW_RXQ_UNLOCK(lhw);
5504 
5505 	IMPROVE();
5506 
5507 err:
5508 	/* The skb is ours so we can free it :-) */
5509 	kfree_skb(skb);
5510 }
5511 
5512 uint8_t
linuxkpi_ieee80211_get_tid(struct ieee80211_hdr * hdr,bool nonqos_ok)5513 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
5514 {
5515 	const struct ieee80211_frame *wh;
5516 	uint8_t tid;
5517 
5518 	/* Linux seems to assume this is a QOS-Data-Frame */
5519 	KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
5520 	   ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
5521 	   hdr->frame_control));
5522 
5523 	wh = (const struct ieee80211_frame *)hdr;
5524 	tid = ieee80211_gettid(wh);
5525 	KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
5526 	   "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
5527 
5528 	return (tid);
5529 }
5530 
5531 /* -------------------------------------------------------------------------- */
5532 
5533 static void
lkpi_wiphy_work(struct work_struct * work)5534 lkpi_wiphy_work(struct work_struct *work)
5535 {
5536 	struct lkpi_wiphy *lwiphy;
5537 	struct wiphy *wiphy;
5538 	struct wiphy_work *wk;
5539 
5540 	lwiphy = container_of(work, struct lkpi_wiphy, wwk);
5541 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
5542 
5543 	wiphy_lock(wiphy);
5544 
5545 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5546 	wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
5547 	/* If there is nothing we do nothing. */
5548 	if (wk == NULL) {
5549 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5550 		wiphy_unlock(wiphy);
5551 		return;
5552 	}
5553 	list_del_init(&wk->entry);
5554 
5555 	/* More work to do? */
5556 	if (!list_empty(&lwiphy->wwk_list))
5557 		schedule_work(work);
5558 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5559 
5560 	/* Finally call the (*wiphy_work_fn)() function. */
5561 	wk->fn(wiphy, wk);
5562 
5563 	wiphy_unlock(wiphy);
5564 }
5565 
5566 void
linuxkpi_wiphy_work_queue(struct wiphy * wiphy,struct wiphy_work * wwk)5567 linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
5568 {
5569 	struct lkpi_wiphy *lwiphy;
5570 
5571 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5572 
5573 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5574 	/* Do not double-queue. */
5575 	if (list_empty(&wwk->entry))
5576 		list_add_tail(&wwk->entry, &lwiphy->wwk_list);
5577 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5578 
5579 	/*
5580 	 * See how ieee80211_queue_work() work continues in Linux or if things
5581 	 * migrate here over time?
5582 	 * Use a system queue from linux/workqueue.h for now.
5583 	 */
5584 	queue_work(system_wq, &lwiphy->wwk);
5585 }
5586 
5587 void
linuxkpi_wiphy_work_cancel(struct wiphy * wiphy,struct wiphy_work * wwk)5588 linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
5589 {
5590 	struct lkpi_wiphy *lwiphy;
5591 
5592 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5593 
5594 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5595 	/* Only cancel if queued. */
5596 	if (!list_empty(&wwk->entry))
5597 		list_del_init(&wwk->entry);
5598 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5599 }
5600 
5601 void
linuxkpi_wiphy_work_flush(struct wiphy * wiphy,struct wiphy_work * wwk)5602 linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
5603 {
5604 	struct lkpi_wiphy *lwiphy;
5605 	struct wiphy_work *wk;
5606 
5607 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5608 	LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5609 	/* If wwk is unset, flush everything; called when wiphy is shut down. */
5610 	if (wwk != NULL && list_empty(&wwk->entry)) {
5611 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5612 		return;
5613 	}
5614 
5615 	while (!list_empty(&lwiphy->wwk_list)) {
5616 
5617 		wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
5618 		    entry);
5619 		list_del_init(&wk->entry);
5620 		LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5621 		wk->fn(wiphy, wk);
5622 		LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
5623 		if (wk == wwk)
5624 			break;
5625 	}
5626 	LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
5627 }
5628 
5629 void
lkpi_wiphy_delayed_work_timer(struct timer_list * tl)5630 lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
5631 {
5632 	struct wiphy_delayed_work *wdwk;
5633 
5634 	wdwk = from_timer(wdwk, tl, timer);
5635         wiphy_work_queue(wdwk->wiphy, &wdwk->work);
5636 }
5637 
5638 void
linuxkpi_wiphy_delayed_work_queue(struct wiphy * wiphy,struct wiphy_delayed_work * wdwk,unsigned long delay)5639 linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
5640     struct wiphy_delayed_work *wdwk, unsigned long delay)
5641 {
5642 	if (delay == 0) {
5643 		/* Run right away. */
5644 		del_timer(&wdwk->timer);
5645 		wiphy_work_queue(wiphy, &wdwk->work);
5646 	} else {
5647 		wdwk->wiphy = wiphy;
5648 		mod_timer(&wdwk->timer, jiffies + delay);
5649 	}
5650 }
5651 
5652 void
linuxkpi_wiphy_delayed_work_cancel(struct wiphy * wiphy,struct wiphy_delayed_work * wdwk)5653 linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
5654     struct wiphy_delayed_work *wdwk)
5655 {
5656 	del_timer_sync(&wdwk->timer);
5657 	wiphy_work_cancel(wiphy, &wdwk->work);
5658 }
5659 
5660 /* -------------------------------------------------------------------------- */
5661 
5662 struct wiphy *
linuxkpi_wiphy_new(const struct cfg80211_ops * ops,size_t priv_len)5663 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
5664 {
5665 	struct lkpi_wiphy *lwiphy;
5666 	struct wiphy *wiphy;
5667 
5668 	lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
5669 	if (lwiphy == NULL)
5670 		return (NULL);
5671 	lwiphy->ops = ops;
5672 
5673 	LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
5674 	INIT_LIST_HEAD(&lwiphy->wwk_list);
5675 	INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
5676 
5677 	wiphy = LWIPHY_TO_WIPHY(lwiphy);
5678 
5679 	mutex_init(&wiphy->mtx);
5680 	TODO();
5681 
5682 	return (wiphy);
5683 }
5684 
5685 void
linuxkpi_wiphy_free(struct wiphy * wiphy)5686 linuxkpi_wiphy_free(struct wiphy *wiphy)
5687 {
5688 	struct lkpi_wiphy *lwiphy;
5689 
5690 	if (wiphy == NULL)
5691 		return;
5692 
5693 	linuxkpi_wiphy_work_flush(wiphy, NULL);
5694 	mutex_destroy(&wiphy->mtx);
5695 
5696 	lwiphy = WIPHY_TO_LWIPHY(wiphy);
5697 	LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
5698 
5699 	kfree(lwiphy);
5700 }
5701 
5702 uint32_t
linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,enum nl80211_band band)5703 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
5704     enum nl80211_band band)
5705 {
5706 
5707 	switch (band) {
5708 	case NL80211_BAND_2GHZ:
5709 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
5710 		break;
5711 	case NL80211_BAND_5GHZ:
5712 		return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
5713 		break;
5714 	default:
5715 		/* XXX abort, retry, error, panic? */
5716 		break;
5717 	}
5718 
5719 	return (0);
5720 }
5721 
5722 uint32_t
linuxkpi_ieee80211_frequency_to_channel(uint32_t freq,uint32_t flags __unused)5723 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
5724 {
5725 
5726 	return (ieee80211_mhz2ieee(freq, 0));
5727 }
5728 
5729 #if 0
5730 static struct lkpi_sta *
5731 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
5732 {
5733 	struct lkpi_sta *lsta, *temp;
5734 
5735 	LKPI_80211_LVIF_LOCK(lvif);
5736 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
5737 		if (lsta->ni == ni) {
5738 			LKPI_80211_LVIF_UNLOCK(lvif);
5739 			return (lsta);
5740 		}
5741 	}
5742 	LKPI_80211_LVIF_UNLOCK(lvif);
5743 
5744 	return (NULL);
5745 }
5746 #endif
5747 
5748 struct ieee80211_sta *
linuxkpi_ieee80211_find_sta(struct ieee80211_vif * vif,const u8 * peer)5749 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
5750 {
5751 	struct lkpi_vif *lvif;
5752 	struct lkpi_sta *lsta, *temp;
5753 	struct ieee80211_sta *sta;
5754 
5755 	lvif = VIF_TO_LVIF(vif);
5756 
5757 	LKPI_80211_LVIF_LOCK(lvif);
5758 	TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
5759 		sta = LSTA_TO_STA(lsta);
5760 		if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
5761 			LKPI_80211_LVIF_UNLOCK(lvif);
5762 			return (sta);
5763 		}
5764 	}
5765 	LKPI_80211_LVIF_UNLOCK(lvif);
5766 	return (NULL);
5767 }
5768 
5769 struct ieee80211_sta *
linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw * hw,const uint8_t * addr,const uint8_t * ourvifaddr)5770 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
5771     const uint8_t *addr, const uint8_t *ourvifaddr)
5772 {
5773 	struct lkpi_hw *lhw;
5774 	struct lkpi_vif *lvif;
5775 	struct lkpi_sta *lsta;
5776 	struct ieee80211_vif *vif;
5777 	struct ieee80211_sta *sta;
5778 
5779 	lhw = wiphy_priv(hw->wiphy);
5780 	sta = NULL;
5781 
5782 	LKPI_80211_LHW_LVIF_LOCK(lhw);
5783 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
5784 
5785 		/* XXX-BZ check our address from the vif. */
5786 
5787 		vif = LVIF_TO_VIF(lvif);
5788 		if (ourvifaddr != NULL &&
5789 		    !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
5790 			continue;
5791 		sta = linuxkpi_ieee80211_find_sta(vif, addr);
5792 		if (sta != NULL)
5793 			break;
5794 	}
5795 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
5796 
5797 	if (sta != NULL) {
5798 		lsta = STA_TO_LSTA(sta);
5799 		if (!lsta->added_to_drv)
5800 			return (NULL);
5801 	}
5802 
5803 	return (sta);
5804 }
5805 
5806 struct sk_buff *
linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)5807 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
5808     struct ieee80211_txq *txq)
5809 {
5810 	struct lkpi_txq *ltxq;
5811 	struct lkpi_vif *lvif;
5812 	struct sk_buff *skb;
5813 
5814 	skb = NULL;
5815 	ltxq = TXQ_TO_LTXQ(txq);
5816 	ltxq->seen_dequeue = true;
5817 
5818 	if (ltxq->stopped)
5819 		goto stopped;
5820 
5821 	lvif = VIF_TO_LVIF(ltxq->txq.vif);
5822 	if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
5823 		ltxq->stopped = true;
5824 		goto stopped;
5825 	}
5826 
5827 	IMPROVE("hw(TX_FRAG_LIST)");
5828 
5829 	LKPI_80211_LTXQ_LOCK(ltxq);
5830 	skb = skb_dequeue(&ltxq->skbq);
5831 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5832 
5833 stopped:
5834 	return (skb);
5835 }
5836 
5837 void
linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq * txq,unsigned long * frame_cnt,unsigned long * byte_cnt)5838 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
5839     unsigned long *frame_cnt, unsigned long *byte_cnt)
5840 {
5841 	struct lkpi_txq *ltxq;
5842 	struct sk_buff *skb;
5843 	unsigned long fc, bc;
5844 
5845 	ltxq = TXQ_TO_LTXQ(txq);
5846 
5847 	fc = bc = 0;
5848 	LKPI_80211_LTXQ_LOCK(ltxq);
5849 	skb_queue_walk(&ltxq->skbq, skb) {
5850 		fc++;
5851 		bc += skb->len;
5852 	}
5853 	LKPI_80211_LTXQ_UNLOCK(ltxq);
5854 	if (frame_cnt)
5855 		*frame_cnt = fc;
5856 	if (byte_cnt)
5857 		*byte_cnt = bc;
5858 
5859 	/* Validate that this is doing the correct thing. */
5860 	/* Should we keep track on en/dequeue? */
5861 	IMPROVE();
5862 }
5863 
5864 /*
5865  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
5866  * The latter tries to derive the success status from the info flags
5867  * passed back from the driver.  rawx_mit() saves the ni on the m and the
5868  * m on the skb for us to be able to give feedback to net80211.
5869  */
5870 static void
_lkpi_ieee80211_free_txskb(struct ieee80211_hw * hw,struct sk_buff * skb,int status)5871 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5872     int status)
5873 {
5874 	struct ieee80211_node *ni;
5875 	struct mbuf *m;
5876 
5877 	m = skb->m;
5878 	skb->m = NULL;
5879 
5880 	if (m != NULL) {
5881 		ni = m->m_pkthdr.PH_loc.ptr;
5882 		/* Status: 0 is ok, != 0 is error. */
5883 		ieee80211_tx_complete(ni, m, status);
5884 		/* ni & mbuf were consumed. */
5885 	}
5886 }
5887 
5888 void
linuxkpi_ieee80211_free_txskb(struct ieee80211_hw * hw,struct sk_buff * skb,int status)5889 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
5890     int status)
5891 {
5892 
5893 	_lkpi_ieee80211_free_txskb(hw, skb, status);
5894 	kfree_skb(skb);
5895 }
5896 
5897 void
linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw * hw,struct ieee80211_tx_status * txstat)5898 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
5899     struct ieee80211_tx_status *txstat)
5900 {
5901 	struct sk_buff *skb;
5902 	struct ieee80211_tx_info *info;
5903 	struct ieee80211_ratectl_tx_status txs;
5904 	struct ieee80211_node *ni;
5905 	int status;
5906 
5907 	skb = txstat->skb;
5908 	if (skb->m != NULL) {
5909 		struct mbuf *m;
5910 
5911 		m = skb->m;
5912 		ni = m->m_pkthdr.PH_loc.ptr;
5913 		memset(&txs, 0, sizeof(txs));
5914 	} else {
5915 		ni = NULL;
5916 	}
5917 
5918 	info = txstat->info;
5919 	if (info->flags & IEEE80211_TX_STAT_ACK) {
5920 		status = 0;	/* No error. */
5921 		txs.status = IEEE80211_RATECTL_TX_SUCCESS;
5922 	} else {
5923 		status = 1;
5924 		txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
5925 	}
5926 
5927 	if (ni != NULL) {
5928 		int ridx __unused;
5929 #ifdef LINUXKPI_DEBUG_80211
5930 		int old_rate;
5931 
5932 		old_rate = ni->ni_vap->iv_bss->ni_txrate;
5933 #endif
5934 		txs.pktlen = skb->len;
5935 		txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
5936 		if (info->status.rates[0].count > 1) {
5937 			txs.long_retries = info->status.rates[0].count - 1;	/* 1 + retries in drivers. */
5938 			txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
5939 		}
5940 #if 0		/* Unused in net80211 currently. */
5941 		/* XXX-BZ convert check .flags for MCS/VHT/.. */
5942 		txs.final_rate = info->status.rates[0].idx;
5943 		txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
5944 #endif
5945 		if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
5946 			txs.rssi = info->status.ack_signal;		/* XXX-BZ CONVERT? */
5947 			txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
5948 		}
5949 
5950 		IMPROVE("only update of rate matches but that requires us to get a proper rate");
5951 		ieee80211_ratectl_tx_complete(ni, &txs);
5952 		ridx = ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
5953 
5954 #ifdef LINUXKPI_DEBUG_80211
5955 		if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
5956 			printf("TX-RATE: %s: old %d new %d ridx %d, "
5957 			    "long_retries %d\n", __func__,
5958 			    old_rate, ni->ni_vap->iv_bss->ni_txrate,
5959 			    ridx, txs.long_retries);
5960 		}
5961 #endif
5962 	}
5963 
5964 #ifdef LINUXKPI_DEBUG_80211
5965 	if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5966 		printf("TX-STATUS: %s: hw %p skb %p status %d : flags %#x "
5967 		    "band %u hw_queue %u tx_time_est %d : "
5968 		    "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
5969 		    "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
5970 		    "tx_time %u flags %#x "
5971 		    "status_driver_data [ %p %p ]\n",
5972 		    __func__, hw, skb, status, info->flags,
5973 		    info->band, info->hw_queue, info->tx_time_est,
5974 		    info->status.rates[0].idx, info->status.rates[0].count,
5975 		    info->status.rates[0].flags,
5976 		    info->status.rates[1].idx, info->status.rates[1].count,
5977 		    info->status.rates[1].flags,
5978 		    info->status.rates[2].idx, info->status.rates[2].count,
5979 		    info->status.rates[2].flags,
5980 		    info->status.rates[3].idx, info->status.rates[3].count,
5981 		    info->status.rates[3].flags,
5982 		    info->status.ack_signal, info->status.ampdu_ack_len,
5983 		    info->status.ampdu_len, info->status.antenna,
5984 		    info->status.tx_time, info->status.flags,
5985 		    info->status.status_driver_data[0],
5986 		    info->status.status_driver_data[1]);
5987 #endif
5988 
5989 	if (txstat->free_list) {
5990 		_lkpi_ieee80211_free_txskb(hw, skb, status);
5991 		list_add_tail(&skb->list, txstat->free_list);
5992 	} else {
5993 		linuxkpi_ieee80211_free_txskb(hw, skb, status);
5994 	}
5995 }
5996 
5997 void
linuxkpi_ieee80211_tx_status(struct ieee80211_hw * hw,struct sk_buff * skb)5998 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
5999 {
6000 	struct ieee80211_tx_status status;
6001 
6002 	memset(&status, 0, sizeof(status));
6003 	status.info = IEEE80211_SKB_CB(skb);
6004 	status.skb = skb;
6005 	/* sta, n_rates, rates, free_list? */
6006 
6007 	ieee80211_tx_status_ext(hw, &status);
6008 }
6009 
6010 /*
6011  * This is an internal bandaid for the moment for the way we glue
6012  * skbs and mbufs together for TX.  Once we have skbs backed by
6013  * mbufs this should go away.
6014  * This is a public function but kept on the private KPI (lkpi_)
6015  * and is not exposed by a header file.
6016  */
6017 static void
lkpi_ieee80211_free_skb_mbuf(void * p)6018 lkpi_ieee80211_free_skb_mbuf(void *p)
6019 {
6020 	struct ieee80211_node *ni;
6021 	struct mbuf *m;
6022 
6023 	if (p == NULL)
6024 		return;
6025 
6026 	m = (struct mbuf *)p;
6027 	M_ASSERTPKTHDR(m);
6028 
6029 	ni = m->m_pkthdr.PH_loc.ptr;
6030 	m->m_pkthdr.PH_loc.ptr = NULL;
6031 	if (ni != NULL)
6032 		ieee80211_free_node(ni);
6033 	m_freem(m);
6034 }
6035 
6036 void
linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw * hw,struct delayed_work * w,int delay)6037 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
6038     struct delayed_work *w, int delay)
6039 {
6040 	struct lkpi_hw *lhw;
6041 
6042 	/* Need to make sure hw is in a stable (non-suspended) state. */
6043 	IMPROVE();
6044 
6045 	lhw = HW_TO_LHW(hw);
6046 	queue_delayed_work(lhw->workq, w, delay);
6047 }
6048 
6049 void
linuxkpi_ieee80211_queue_work(struct ieee80211_hw * hw,struct work_struct * w)6050 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
6051     struct work_struct *w)
6052 {
6053 	struct lkpi_hw *lhw;
6054 
6055 	/* Need to make sure hw is in a stable (non-suspended) state. */
6056 	IMPROVE();
6057 
6058 	lhw = HW_TO_LHW(hw);
6059 	queue_work(lhw->workq, w);
6060 }
6061 
6062 struct sk_buff *
linuxkpi_ieee80211_probereq_get(struct ieee80211_hw * hw,uint8_t * addr,uint8_t * ssid,size_t ssid_len,size_t tailroom)6063 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, uint8_t *addr,
6064     uint8_t *ssid, size_t ssid_len, size_t tailroom)
6065 {
6066 	struct sk_buff *skb;
6067 	struct ieee80211_frame *wh;
6068 	uint8_t *p;
6069 	size_t len;
6070 
6071 	len = sizeof(*wh);
6072 	len += 2 + ssid_len;
6073 
6074 	skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
6075 	if (skb == NULL)
6076 		return (NULL);
6077 
6078 	skb_reserve(skb, hw->extra_tx_headroom);
6079 
6080 	wh = skb_put_zero(skb, sizeof(*wh));
6081 	wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
6082 	wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
6083 	IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
6084 	IEEE80211_ADDR_COPY(wh->i_addr2, addr);
6085 	IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
6086 
6087 	p = skb_put(skb, 2 + ssid_len);
6088 	*p++ = IEEE80211_ELEMID_SSID;
6089 	*p++ = ssid_len;
6090 	if (ssid_len > 0)
6091 		memcpy(p, ssid, ssid_len);
6092 
6093 	return (skb);
6094 }
6095 
6096 struct sk_buff *
linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw * hw,struct ieee80211_vif * vif)6097 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
6098     struct ieee80211_vif *vif)
6099 {
6100 	struct lkpi_vif *lvif;
6101 	struct ieee80211vap *vap;
6102 	struct sk_buff *skb;
6103 	struct ieee80211_frame_pspoll *psp;
6104 	uint16_t v;
6105 
6106 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
6107 	if (skb == NULL)
6108 		return (NULL);
6109 
6110 	skb_reserve(skb, hw->extra_tx_headroom);
6111 
6112 	lvif = VIF_TO_LVIF(vif);
6113 	vap = LVIF_TO_VAP(lvif);
6114 
6115 	psp = skb_put_zero(skb, sizeof(*psp));
6116 	psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
6117 	psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
6118 	v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
6119 	memcpy(&psp->i_aid, &v, sizeof(v));
6120 	IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
6121 	IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
6122 
6123 	return (skb);
6124 }
6125 
6126 struct sk_buff *
linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw * hw,struct ieee80211_vif * vif,int linkid,bool qos)6127 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
6128     struct ieee80211_vif *vif, int linkid, bool qos)
6129 {
6130 	struct lkpi_vif *lvif;
6131 	struct ieee80211vap *vap;
6132 	struct sk_buff *skb;
6133 	struct ieee80211_frame *nullf;
6134 
6135 	IMPROVE("linkid");
6136 
6137 	skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
6138 	if (skb == NULL)
6139 		return (NULL);
6140 
6141 	skb_reserve(skb, hw->extra_tx_headroom);
6142 
6143 	lvif = VIF_TO_LVIF(vif);
6144 	vap = LVIF_TO_VAP(lvif);
6145 
6146 	nullf = skb_put_zero(skb, sizeof(*nullf));
6147 	nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
6148 	nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
6149 	nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
6150 
6151 	IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
6152 	IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
6153 	IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
6154 
6155 	return (skb);
6156 }
6157 
6158 struct wireless_dev *
linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif * vif)6159 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
6160 {
6161 	struct lkpi_vif *lvif;
6162 
6163 	lvif = VIF_TO_LVIF(vif);
6164 	return (&lvif->wdev);
6165 }
6166 
6167 void
linuxkpi_ieee80211_connection_loss(struct ieee80211_vif * vif)6168 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
6169 {
6170 	struct lkpi_vif *lvif;
6171 	struct ieee80211vap *vap;
6172 	enum ieee80211_state nstate;
6173 	int arg;
6174 
6175 	lvif = VIF_TO_LVIF(vif);
6176 	vap = LVIF_TO_VAP(lvif);
6177 
6178 	/*
6179 	 * Go to init; otherwise we need to elaborately check state and
6180 	 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
6181 	 * Let the statemachine handle all neccessary changes.
6182 	 */
6183 	nstate = IEEE80211_S_INIT;
6184 	arg = 0;	/* Not a valid reason. */
6185 
6186 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
6187 	    vif, vap, ieee80211_state_name[vap->iv_state]);
6188 	ieee80211_new_state(vap, nstate, arg);
6189 }
6190 
6191 void
linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif * vif)6192 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
6193 {
6194 	struct lkpi_vif *lvif;
6195 	struct ieee80211vap *vap;
6196 
6197 	lvif = VIF_TO_LVIF(vif);
6198 	vap = LVIF_TO_VAP(lvif);
6199 
6200 	ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
6201 	    vif, vap, ieee80211_state_name[vap->iv_state]);
6202 	ieee80211_beacon_miss(vap->iv_ic);
6203 }
6204 
6205 /* -------------------------------------------------------------------------- */
6206 
6207 void
linuxkpi_ieee80211_stop_queue(struct ieee80211_hw * hw,int qnum)6208 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
6209 {
6210 	struct lkpi_hw *lhw;
6211 	struct lkpi_vif *lvif;
6212 	struct ieee80211_vif *vif;
6213 	int ac_count, ac;
6214 
6215 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
6216 	    __func__, qnum, hw->queues, hw));
6217 
6218 	lhw = wiphy_priv(hw->wiphy);
6219 
6220 	/* See lkpi_ic_vap_create(). */
6221 	if (hw->queues >= IEEE80211_NUM_ACS)
6222 		ac_count = IEEE80211_NUM_ACS;
6223 	else
6224 		ac_count = 1;
6225 
6226 	LKPI_80211_LHW_LVIF_LOCK(lhw);
6227 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
6228 
6229 		vif = LVIF_TO_VIF(lvif);
6230 		for (ac = 0; ac < ac_count; ac++) {
6231 			IMPROVE_TXQ("LOCKING");
6232 			if (qnum == vif->hw_queue[ac]) {
6233 #ifdef LINUXKPI_DEBUG_80211
6234 				/*
6235 				 * For now log this to better understand
6236 				 * how this is supposed to work.
6237 				 */
6238 				if (lvif->hw_queue_stopped[ac] &&
6239 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
6240 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
6241 					    "lvif %p vif %p ac %d qnum %d already "
6242 					    "stopped\n", __func__, __LINE__,
6243 					    lhw, hw, lvif, vif, ac, qnum);
6244 #endif
6245 				lvif->hw_queue_stopped[ac] = true;
6246 			}
6247 		}
6248 	}
6249 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
6250 }
6251 
6252 void
linuxkpi_ieee80211_stop_queues(struct ieee80211_hw * hw)6253 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
6254 {
6255 	int i;
6256 
6257 	IMPROVE_TXQ("Locking; do we need further info?");
6258 	for (i = 0; i < hw->queues; i++)
6259 		linuxkpi_ieee80211_stop_queue(hw, i);
6260 }
6261 
6262 
6263 static void
lkpi_ieee80211_wake_queues(struct ieee80211_hw * hw,int hwq)6264 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
6265 {
6266 	struct lkpi_hw *lhw;
6267 	struct lkpi_vif *lvif;
6268 	struct lkpi_sta *lsta;
6269 	int ac_count, ac, tid;
6270 
6271 	/* See lkpi_ic_vap_create(). */
6272 	if (hw->queues >= IEEE80211_NUM_ACS)
6273 		ac_count = IEEE80211_NUM_ACS;
6274 	else
6275 		ac_count = 1;
6276 
6277 	lhw = wiphy_priv(hw->wiphy);
6278 
6279 	IMPROVE_TXQ("Locking");
6280 	LKPI_80211_LHW_LVIF_LOCK(lhw);
6281 	TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
6282 		struct ieee80211_vif *vif;
6283 
6284 		vif = LVIF_TO_VIF(lvif);
6285 		for (ac = 0; ac < ac_count; ac++) {
6286 
6287 			if (hwq == vif->hw_queue[ac]) {
6288 
6289 				/* XXX-BZ what about software scan? */
6290 
6291 #ifdef LINUXKPI_DEBUG_80211
6292 				/*
6293 				 * For now log this to better understand
6294 				 * how this is supposed to work.
6295 				 */
6296 				if (!lvif->hw_queue_stopped[ac] &&
6297 				    (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
6298 					ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
6299 					    "lvif %p vif %p ac %d hw_q not stopped\n",
6300 					    __func__, __LINE__,
6301 					    lhw, hw, lvif, vif, ac);
6302 #endif
6303 				lvif->hw_queue_stopped[ac] = false;
6304 
6305 				LKPI_80211_LVIF_LOCK(lvif);
6306 				TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
6307 					struct ieee80211_sta *sta;
6308 
6309 					sta = LSTA_TO_STA(lsta);
6310 					for (tid = 0; tid < nitems(sta->txq); tid++) {
6311 						struct lkpi_txq *ltxq;
6312 
6313 						if (sta->txq[tid] == NULL)
6314 							continue;
6315 
6316 						if (sta->txq[tid]->ac != ac)
6317 							continue;
6318 
6319 						ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
6320 						if (!ltxq->stopped)
6321 							continue;
6322 
6323 						ltxq->stopped = false;
6324 
6325 						/* XXX-BZ see when this explodes with all the locking. taskq? */
6326 						lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
6327 					}
6328 				}
6329 				LKPI_80211_LVIF_UNLOCK(lvif);
6330 			}
6331 		}
6332 	}
6333 	LKPI_80211_LHW_LVIF_UNLOCK(lhw);
6334 }
6335 
6336 void
linuxkpi_ieee80211_wake_queues(struct ieee80211_hw * hw)6337 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
6338 {
6339 	int i;
6340 
6341 	IMPROVE_TXQ("Is this all/enough here?");
6342 	for (i = 0; i < hw->queues; i++)
6343 		lkpi_ieee80211_wake_queues(hw, i);
6344 }
6345 
6346 void
linuxkpi_ieee80211_wake_queue(struct ieee80211_hw * hw,int qnum)6347 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
6348 {
6349 
6350 	KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
6351 	    __func__, qnum, hw->queues, hw));
6352 
6353 	lkpi_ieee80211_wake_queues(hw, qnum);
6354 }
6355 
6356 /* This is just hardware queues. */
6357 void
linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw * hw,uint8_t ac)6358 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
6359 {
6360 	struct lkpi_hw *lhw;
6361 
6362 	lhw = HW_TO_LHW(hw);
6363 
6364 	IMPROVE_TXQ("Are there reasons why we wouldn't schedule?");
6365 	IMPROVE_TXQ("LOCKING");
6366 	if (++lhw->txq_generation[ac] == 0)
6367 		lhw->txq_generation[ac]++;
6368 }
6369 
6370 struct ieee80211_txq *
linuxkpi_ieee80211_next_txq(struct ieee80211_hw * hw,uint8_t ac)6371 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
6372 {
6373 	struct lkpi_hw *lhw;
6374 	struct ieee80211_txq *txq;
6375 	struct lkpi_txq *ltxq;
6376 
6377 	lhw = HW_TO_LHW(hw);
6378 	txq = NULL;
6379 
6380 	IMPROVE_TXQ("LOCKING");
6381 
6382 	/* Check that we are scheduled. */
6383 	if (lhw->txq_generation[ac] == 0)
6384 		goto out;
6385 
6386 	ltxq = TAILQ_FIRST(&lhw->scheduled_txqs[ac]);
6387 	if (ltxq == NULL)
6388 		goto out;
6389 	if (ltxq->txq_generation == lhw->txq_generation[ac])
6390 		goto out;
6391 
6392 	ltxq->txq_generation = lhw->txq_generation[ac];
6393 	TAILQ_REMOVE(&lhw->scheduled_txqs[ac], ltxq, txq_entry);
6394 	txq = &ltxq->txq;
6395 	TAILQ_ELEM_INIT(ltxq, txq_entry);
6396 
6397 out:
6398 	return (txq);
6399 }
6400 
linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw * hw,struct ieee80211_txq * txq,bool withoutpkts)6401 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
6402     struct ieee80211_txq *txq, bool withoutpkts)
6403 {
6404 	struct lkpi_hw *lhw;
6405 	struct lkpi_txq *ltxq;
6406 	bool ltxq_empty;
6407 
6408 	ltxq = TXQ_TO_LTXQ(txq);
6409 
6410 	IMPROVE_TXQ("LOCKING");
6411 
6412 	/* Only schedule if work to do or asked to anyway. */
6413 	LKPI_80211_LTXQ_LOCK(ltxq);
6414 	ltxq_empty = skb_queue_empty(&ltxq->skbq);
6415 	LKPI_80211_LTXQ_UNLOCK(ltxq);
6416 	if (!withoutpkts && ltxq_empty)
6417 		goto out;
6418 
6419 	/*
6420 	 * Make sure we do not double-schedule. We do this by checking tqe_prev,
6421 	 * the previous entry in our tailq. tqe_prev is always valid if this entry
6422 	 * is queued, tqe_next may be NULL if this is the only element in the list.
6423 	 */
6424 	if (ltxq->txq_entry.tqe_prev != NULL)
6425 		goto out;
6426 
6427 	lhw = HW_TO_LHW(hw);
6428 	TAILQ_INSERT_TAIL(&lhw->scheduled_txqs[txq->ac], ltxq, txq_entry);
6429 out:
6430 	return;
6431 }
6432 
6433 void
linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw * hw,struct ieee80211_txq * txq)6434 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
6435     struct ieee80211_txq *txq)
6436 {
6437 	struct lkpi_hw *lhw;
6438 	struct ieee80211_txq *ntxq;
6439 	struct ieee80211_tx_control control;
6440         struct sk_buff *skb;
6441 
6442 	lhw = HW_TO_LHW(hw);
6443 
6444 	LKPI_80211_LHW_TXQ_LOCK(lhw);
6445 	ieee80211_txq_schedule_start(hw, txq->ac);
6446 	do {
6447 		ntxq = ieee80211_next_txq(hw, txq->ac);
6448 		if (ntxq == NULL)
6449 			break;
6450 
6451 		memset(&control, 0, sizeof(control));
6452 		control.sta = ntxq->sta;
6453 		do {
6454 			skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
6455 			if (skb == NULL)
6456 				break;
6457 			lkpi_80211_mo_tx(hw, &control, skb);
6458 		} while(1);
6459 
6460 		ieee80211_return_txq(hw, ntxq, false);
6461 	} while (1);
6462 	ieee80211_txq_schedule_end(hw, txq->ac);
6463 	LKPI_80211_LHW_TXQ_UNLOCK(lhw);
6464 }
6465 
6466 /* -------------------------------------------------------------------------- */
6467 
6468 struct lkpi_cfg80211_bss {
6469 	u_int refcnt;
6470 	struct cfg80211_bss bss;
6471 };
6472 
6473 struct lkpi_cfg80211_get_bss_iter_lookup {
6474 	struct wiphy *wiphy;
6475 	struct linuxkpi_ieee80211_channel *chan;
6476 	const uint8_t *bssid;
6477 	const uint8_t *ssid;
6478 	size_t ssid_len;
6479 	enum ieee80211_bss_type bss_type;
6480 	enum ieee80211_privacy privacy;
6481 
6482 	/*
6483 	 * Something to store a copy of the result as the net80211 scan cache
6484 	 * is not refoucnted so a scan entry might go away any time.
6485 	 */
6486 	bool match;
6487 	struct cfg80211_bss *bss;
6488 };
6489 
6490 static void
lkpi_cfg80211_get_bss_iterf(void * arg,const struct ieee80211_scan_entry * se)6491 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
6492 {
6493 	struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
6494 	size_t ielen;
6495 
6496 	lookup = arg;
6497 
6498 	/* Do not try to find another match. */
6499 	if (lookup->match)
6500 		return;
6501 
6502 	/* Nothing to store result. */
6503 	if (lookup->bss == NULL)
6504 		return;
6505 
6506 	if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
6507 		/* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
6508 		/* We have no idea what to compare to as the drivers only request ANY */
6509 		return;
6510 	}
6511 
6512 	if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
6513 		/* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
6514 		/* We have no idea what to compare to as the drivers only request ANY */
6515 		return;
6516 	}
6517 
6518 	if (lookup->chan != NULL) {
6519 		struct linuxkpi_ieee80211_channel *chan;
6520 
6521 		chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
6522 		    se->se_chan->ic_freq);
6523 		if (chan == NULL || chan != lookup->chan)
6524 			return;
6525 	}
6526 
6527 	if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
6528 		return;
6529 
6530 	if (lookup->ssid) {
6531 		if (lookup->ssid_len != se->se_ssid[1] ||
6532 		    se->se_ssid[1] == 0)
6533 			return;
6534 		if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
6535 			return;
6536 	}
6537 
6538 	ielen = se->se_ies.len;
6539 
6540 	lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
6541 	    M_LKPI80211, M_NOWAIT | M_ZERO);
6542 	if (lookup->bss->ies == NULL)
6543 		return;
6544 
6545 	lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
6546 	lookup->bss->ies->len = ielen;
6547 	if (ielen)
6548 		memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
6549 
6550 	lookup->match = true;
6551 }
6552 
6553 struct cfg80211_bss *
linuxkpi_cfg80211_get_bss(struct wiphy * wiphy,struct linuxkpi_ieee80211_channel * chan,const uint8_t * bssid,const uint8_t * ssid,size_t ssid_len,enum ieee80211_bss_type bss_type,enum ieee80211_privacy privacy)6554 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
6555     const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
6556     enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
6557 {
6558 	struct lkpi_cfg80211_bss *lbss;
6559 	struct lkpi_cfg80211_get_bss_iter_lookup lookup;
6560 	struct lkpi_hw *lhw;
6561 	struct ieee80211vap *vap;
6562 
6563 	lhw = wiphy_priv(wiphy);
6564 
6565 	/* Let's hope we can alloc. */
6566 	lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
6567 	if (lbss == NULL) {
6568 		ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
6569 		return (NULL);
6570 	}
6571 
6572 	lookup.wiphy = wiphy;
6573 	lookup.chan = chan;
6574 	lookup.bssid = bssid;
6575 	lookup.ssid = ssid;
6576 	lookup.ssid_len = ssid_len;
6577 	lookup.bss_type = bss_type;
6578 	lookup.privacy = privacy;
6579 	lookup.match = false;
6580 	lookup.bss = &lbss->bss;
6581 
6582 	IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
6583 	vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
6584 	ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
6585 	if (!lookup.match) {
6586 		free(lbss, M_LKPI80211);
6587 		return (NULL);
6588 	}
6589 
6590 	refcount_init(&lbss->refcnt, 1);
6591 	return (&lbss->bss);
6592 }
6593 
6594 void
linuxkpi_cfg80211_put_bss(struct wiphy * wiphy,struct cfg80211_bss * bss)6595 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
6596 {
6597 	struct lkpi_cfg80211_bss *lbss;
6598 
6599 	lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
6600 
6601 	/* Free everything again on refcount ... */
6602 	if (refcount_release(&lbss->refcnt)) {
6603 		free(lbss->bss.ies, M_LKPI80211);
6604 		free(lbss, M_LKPI80211);
6605 	}
6606 }
6607 
6608 void
linuxkpi_cfg80211_bss_flush(struct wiphy * wiphy)6609 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
6610 {
6611 	struct lkpi_hw *lhw;
6612 	struct ieee80211com *ic;
6613 	struct ieee80211vap *vap;
6614 
6615 	lhw = wiphy_priv(wiphy);
6616 	ic = lhw->ic;
6617 
6618 	/*
6619 	 * If we haven't called ieee80211_ifattach() yet
6620 	 * or there is no VAP, there are no scans to flush.
6621 	 */
6622 	if (ic == NULL ||
6623 	    (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
6624 		return;
6625 
6626 	/* Should only happen on the current one? Not seen it late enough. */
6627 	IEEE80211_LOCK(ic);
6628 	TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
6629 		ieee80211_scan_flush(vap);
6630 	IEEE80211_UNLOCK(ic);
6631 }
6632 
6633 /* -------------------------------------------------------------------------- */
6634 
6635 /*
6636  * hw->conf get initialized/set in various places for us:
6637  * - linuxkpi_ieee80211_alloc_hw(): flags
6638  * - linuxkpi_ieee80211_ifattach(): chandef
6639  * - lkpi_ic_vap_create(): listen_interval
6640  * - lkpi_ic_set_channel(): chandef, flags
6641  */
6642 
lkpi_80211_update_chandef(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * new)6643 int lkpi_80211_update_chandef(struct ieee80211_hw *hw,
6644     struct ieee80211_chanctx_conf *new)
6645 {
6646 	struct cfg80211_chan_def *cd;
6647 	uint32_t changed;
6648 	int error;
6649 
6650 	changed = 0;
6651 	if (new == NULL || new->def.chan == NULL)
6652 		cd = NULL;
6653 	else
6654 		cd = &new->def;
6655 
6656 	if (cd && cd->chan != hw->conf.chandef.chan) {
6657 		/* Copy; the chan pointer is fine and will stay valid. */
6658 		hw->conf.chandef = *cd;
6659 		changed |= IEEE80211_CONF_CHANGE_CHANNEL;
6660 	}
6661 	IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
6662 
6663 	if (changed == 0)
6664 		return (0);
6665 
6666 	error = lkpi_80211_mo_config(hw, changed);
6667 	return (error);
6668 }
6669 
6670 /* -------------------------------------------------------------------------- */
6671 
6672 MODULE_VERSION(linuxkpi_wlan, 1);
6673 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
6674 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
6675