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