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