]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/compat/linuxkpi/common/src/linux_80211.c
LinuxKPI: 802.11: fix RSSI (and NF)
[FreeBSD/FreeBSD.git] / sys / compat / linuxkpi / common / src / linux_80211.c
1 /*-
2  * Copyright (c) 2020-2022 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/cdefs.h>
43 __FBSDID("$FreeBSD$");
44
45 #include <sys/param.h>
46 #include <sys/types.h>
47 #include <sys/kernel.h>
48 #include <sys/errno.h>
49 #include <sys/malloc.h>
50 #include <sys/module.h>
51 #include <sys/mutex.h>
52 #include <sys/socket.h>
53 #include <sys/sysctl.h>
54 #include <sys/queue.h>
55 #include <sys/taskqueue.h>
56
57 #include <net/if.h>
58 #include <net/if_var.h>
59 #include <net/if_media.h>
60 #include <net/ethernet.h>
61
62 #include <net80211/ieee80211_var.h>
63 #include <net80211/ieee80211_proto.h>
64 #include <net80211/ieee80211_ratectl.h>
65 #include <net80211/ieee80211_radiotap.h>
66
67 #define LINUXKPI_NET80211
68 #include <net/mac80211.h>
69
70 #include <linux/workqueue.h>
71 #include "linux_80211.h"
72
73 static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
74
75 /* -------------------------------------------------------------------------- */
76
77 /* Keep public for as long as header files are using it too. */
78 int linuxkpi_debug_80211;
79
80 #ifdef LINUXKPI_DEBUG_80211
81 SYSCTL_DECL(_compat_linuxkpi);
82 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
83     "LinuxKPI 802.11 compatibility layer");
84
85 SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
86     &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
87
88 #ifndef D80211_TODO
89 #define D80211_TODO             0x1
90 #endif
91 #ifndef D80211_IMPROVE
92 #define D80211_IMPROVE          0x2
93 #endif
94 #define D80211_TRACE            0x10
95 #define D80211_TRACEOK          0x20
96 #define D80211_TRACE_TX         0x100
97 #define D80211_TRACE_TX_DUMP    0x200
98 #define D80211_TRACE_RX         0x1000
99 #define D80211_TRACE_RX_DUMP    0x2000
100 #define D80211_TRACE_RX_BEACONS 0x4000
101 #define D80211_TRACEX           (D80211_TRACE_TX|D80211_TRACE_RX)
102 #define D80211_TRACEX_DUMP      (D80211_TRACE_TX_DUMP|D80211_TRACE_RX_DUMP)
103 #define D80211_TRACE_STA        0x10000
104 #define UNIMPLEMENTED           if (linuxkpi_debug_80211 & D80211_TODO)         \
105     printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
106 #define TRACEOK()               if (linuxkpi_debug_80211 & D80211_TRACEOK)      \
107     printf("XXX-TODO %s:%d: TRACEPOINT\n", __func__, __LINE__)
108 #else
109 #define UNIMPLEMENTED           do { } while (0)
110 #define TRACEOK()               do { } while (0)
111 #endif
112
113 /* #define      PREP_TX_INFO_DURATION   (IEEE80211_TRANS_WAIT * 1000) */
114 #ifndef PREP_TX_INFO_DURATION
115 #define PREP_TX_INFO_DURATION   0 /* Let the driver do its thing. */
116 #endif
117
118 /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
119 const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
120
121 const uint8_t tid_to_mac80211_ac[] = {
122         IEEE80211_AC_BE,
123         IEEE80211_AC_BK,
124         IEEE80211_AC_BK,
125         IEEE80211_AC_BE,
126         IEEE80211_AC_VI,
127         IEEE80211_AC_VI,
128         IEEE80211_AC_VO,
129         IEEE80211_AC_VO,
130 #if 0
131         IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
132 #endif
133 };
134
135 const struct cfg80211_ops linuxkpi_mac80211cfgops = {
136         /*
137          * XXX TODO need a "glue layer" to link cfg80211 ops to
138          * mac80211 and to the driver or net80211.
139          * Can we pass some on 1:1? Need to compare the (*f)().
140          */
141 };
142
143 static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
144     struct ieee80211_node *);
145 static void lkpi_80211_txq_task(void *, int);
146 static void lkpi_ieee80211_free_skb_mbuf(void *);
147
148 static void
149 lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
150     const char *_f, int _l)
151 {
152
153 #ifdef LINUXKPI_DEBUG_80211
154         if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
155                 return;
156         if (lsta == NULL)
157                 return;
158
159         printf("%s:%d lsta %p ni %p sta %p\n",
160             _f, _l, lsta, ni, &lsta->sta);
161         if (ni != NULL)
162                 ieee80211_dump_node(NULL, ni);
163         printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
164         printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
165                 lsta->kc, lsta->state, lsta->added_to_drv, lsta->in_mgd);
166 #endif
167 }
168
169 static void
170 lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
171 {
172         struct ieee80211_node *ni;
173
174         ni = lsta->ni;
175
176         LKPI_80211_LVIF_LOCK(lvif);
177         TAILQ_REMOVE(&lvif->lsta_head, lsta, lsta_entry);
178         LKPI_80211_LVIF_UNLOCK(lvif);
179
180         lsta->ni = NULL;
181         ni->ni_drv_data = NULL;
182         ieee80211_free_node(ni);
183
184         IMPROVE("free lsta here?  We won't have a pointer to it from the node anymore.");
185 }
186
187 static struct lkpi_sta *
188 lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
189     struct ieee80211_hw *hw, struct ieee80211_node *ni)
190 {
191         struct lkpi_sta *lsta;
192         struct lkpi_vif *lvif;
193         struct ieee80211_vif *vif;
194         struct ieee80211_sta *sta;
195         int tid;
196
197         lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
198             M_NOWAIT | M_ZERO);
199         if (lsta == NULL)
200                 return (NULL);
201
202         lsta->added_to_drv = false;
203         lsta->state = IEEE80211_STA_NOTEXIST;
204 #if 0
205         /*
206          * This needs to be done in node_init() as ieee80211_alloc_node()
207          * will initialise the refcount after us.
208          */
209         lsta->ni = ieee80211_ref_node(ni);
210 #endif
211         /* The back-pointer "drv_data" to net80211_node let's us get lsta. */
212         ni->ni_drv_data = lsta;
213
214         lvif = VAP_TO_LVIF(vap);
215         vif = LVIF_TO_VIF(lvif);
216         sta = LSTA_TO_STA(lsta);
217
218         IEEE80211_ADDR_COPY(sta->addr, mac);
219         for (tid = 0; tid < nitems(sta->txq); tid++) {
220                 struct lkpi_txq *ltxq;
221
222                 /*
223                  * We are neither limiting ourselves to hw.queues here,
224                  * nor do we check if driver wants IEEE80211_NUM_TIDS queue.
225                  */
226
227                 ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
228                     M_LKPI80211, M_NOWAIT | M_ZERO);
229                 if (ltxq == NULL)
230                         goto cleanup;
231                 ltxq->seen_dequeue = false;
232                 skb_queue_head_init(&ltxq->skbq);
233                 /* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
234                 if (tid == IEEE80211_NUM_TIDS) {
235                         IMPROVE();
236                         ltxq->txq.ac = IEEE80211_AC_VO;
237                 } else {
238                         ltxq->txq.ac = tid_to_mac80211_ac[tid & 7];
239                 }
240                 ltxq->txq.tid = tid;
241                 ltxq->txq.sta = sta;
242                 ltxq->txq.vif = vif;
243                 sta->txq[tid] = &ltxq->txq;
244         }
245
246         /* Deferred TX path. */
247         mtx_init(&lsta->txq_mtx, "lsta_txq", NULL, MTX_DEF);
248         TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
249         mbufq_init(&lsta->txq, IFQ_MAXLEN);
250
251         return (lsta);
252
253 cleanup:
254         for (; tid >= 0; tid--)
255                 free(sta->txq[tid], M_LKPI80211);
256         free(lsta, M_LKPI80211);
257         return (NULL);
258 }
259
260 static enum nl80211_band
261 lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
262 {
263
264         if (IEEE80211_IS_CHAN_2GHZ(c))
265                 return (NL80211_BAND_2GHZ);
266         else if (IEEE80211_IS_CHAN_5GHZ(c))
267                 return (NL80211_BAND_5GHZ);
268 #ifdef __notyet__
269         else if ()
270                 return (NL80211_BAND_6GHZ);
271         else if ()
272                 return (NL80211_BAND_60GHZ);
273         else if (IEEE80211_IS_CHAN_GSM(c))
274                 return (NL80211_BAND_XXX);
275 #endif
276         else
277                 panic("%s: unsupported band. c %p flags %#x\n",
278                     __func__, c, c->ic_flags);
279 }
280
281 static uint32_t
282 lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
283 {
284
285         /* XXX-BZ this is just silly; net80211 is too convoluted. */
286         /* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
287         switch (band) {
288         case NL80211_BAND_2GHZ:
289                 return (IEEE80211_CHAN_2GHZ);
290                 break;
291         case NL80211_BAND_5GHZ:
292                 return (IEEE80211_CHAN_5GHZ);
293                 break;
294         case NL80211_BAND_60GHZ:
295                 break;
296         case NL80211_BAND_6GHZ:
297                 break;
298         default:
299                 panic("%s: unsupported band %u\n", __func__, band);
300                 break;
301         }
302
303         IMPROVE();
304         return (0x00);
305 }
306
307 static enum ieee80211_ac_numbers
308 lkpi_ac_net_to_l80211(int ac)
309 {
310
311         switch (ac) {
312         case WME_AC_VO:
313                 return (IEEE80211_AC_VO);
314         case WME_AC_VI:
315                 return (IEEE80211_AC_VI);
316         case WME_AC_BE:
317                 return (IEEE80211_AC_BE);
318         case WME_AC_BK:
319                 return (IEEE80211_AC_BK);
320         default:
321                 printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
322                 return (IEEE80211_AC_BE);
323         }
324 }
325
326 static enum nl80211_iftype
327 lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
328 {
329
330         switch (opmode) {
331         case IEEE80211_M_IBSS:
332                 return (NL80211_IFTYPE_ADHOC);
333                 break;
334         case IEEE80211_M_STA:
335                 return (NL80211_IFTYPE_STATION);
336                 break;
337         case IEEE80211_M_WDS:
338                 return (NL80211_IFTYPE_WDS);
339                 break;
340         case IEEE80211_M_HOSTAP:
341                 return (NL80211_IFTYPE_AP);
342                 break;
343         case IEEE80211_M_MONITOR:
344                 return (NL80211_IFTYPE_MONITOR);
345                 break;
346         case IEEE80211_M_MBSS:
347                 return (NL80211_IFTYPE_MESH_POINT);
348                 break;
349         case IEEE80211_M_AHDEMO:
350                 /* FALLTHROUGH */
351         default:
352                 printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
353                 /* FALLTHROUGH */
354         }
355         return (NL80211_IFTYPE_UNSPECIFIED);
356 }
357
358 #ifdef __notyet__
359 static uint32_t
360 lkpi_l80211_to_net80211_cyphers(uint32_t wlan_cipher_suite)
361 {
362
363         switch (wlan_cipher_suite) {
364         case WLAN_CIPHER_SUITE_WEP40:
365                 return (IEEE80211_CRYPTO_WEP);
366         case WLAN_CIPHER_SUITE_TKIP:
367                 return (IEEE80211_CRYPTO_TKIP);
368         case WLAN_CIPHER_SUITE_CCMP:
369                 return (IEEE80211_CIPHER_AES_CCM);
370         case WLAN_CIPHER_SUITE_WEP104:
371                 return (IEEE80211_CRYPTO_WEP);
372         case WLAN_CIPHER_SUITE_AES_CMAC:
373         case WLAN_CIPHER_SUITE_GCMP:
374         case WLAN_CIPHER_SUITE_GCMP_256:
375         case WLAN_CIPHER_SUITE_CCMP_256:
376         case WLAN_CIPHER_SUITE_BIP_GMAC_128:
377         case WLAN_CIPHER_SUITE_BIP_GMAC_256:
378         case WLAN_CIPHER_SUITE_BIP_CMAC_256:
379                 printf("%s: unsupported WLAN Cipher Suite %#08x | %u\n", __func__,
380                     wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
381                 break;
382         default:
383                 printf("%s: unknown WLAN Cipher Suite %#08x | %u\n", __func__,
384                     wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff);
385         }
386
387         return (0);
388 }
389 #endif
390
391 #ifdef TRY_HW_CRYPTO
392 static uint32_t
393 lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
394 {
395
396         switch (cipher) {
397         case IEEE80211_CIPHER_TKIP:
398                 return (WLAN_CIPHER_SUITE_TKIP);
399         case IEEE80211_CIPHER_AES_CCM:
400                 return (WLAN_CIPHER_SUITE_CCMP);
401         case IEEE80211_CIPHER_WEP:
402                 if (keylen < 8)
403                         return (WLAN_CIPHER_SUITE_WEP40);
404                 else
405                         return (WLAN_CIPHER_SUITE_WEP104);
406                 break;
407         case IEEE80211_CIPHER_AES_OCB:
408         case IEEE80211_CIPHER_TKIPMIC:
409         case IEEE80211_CIPHER_CKIP:
410         case IEEE80211_CIPHER_NONE:
411                 printf("%s: unsupported cipher %#010x\n", __func__, cipher);
412                 break;
413         default:
414                 printf("%s: unknown cipher %#010x\n", __func__, cipher);
415         };
416         return (0);
417 }
418 #endif
419
420 #ifdef __notyet__
421 static enum ieee80211_sta_state
422 lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
423 {
424
425         /*
426          * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
427          * "done".  Also ASSOC/AUTHORIZED are both "RUN" then?
428          */
429         switch (state) {
430         case IEEE80211_S_INIT:
431                 return (IEEE80211_STA_NOTEXIST);
432         case IEEE80211_S_SCAN:
433                 return (IEEE80211_STA_NONE);
434         case IEEE80211_S_AUTH:
435                 return (IEEE80211_STA_AUTH);
436         case IEEE80211_S_ASSOC:
437                 return (IEEE80211_STA_ASSOC);
438         case IEEE80211_S_RUN:
439                 return (IEEE80211_STA_AUTHORIZED);
440         case IEEE80211_S_CAC:
441         case IEEE80211_S_CSA:
442         case IEEE80211_S_SLEEP:
443         default:
444                 UNIMPLEMENTED;
445         };
446
447         return (IEEE80211_STA_NOTEXIST);
448 }
449 #endif
450
451 static struct linuxkpi_ieee80211_channel *
452 lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
453     struct ieee80211_channel *c)
454 {
455         struct ieee80211_hw *hw;
456         struct linuxkpi_ieee80211_channel *channels;
457         enum nl80211_band band;
458         int i, nchans;
459
460         hw = LHW_TO_HW(lhw);
461         band = lkpi_net80211_chan_to_nl80211_band(c);
462         if (hw->wiphy->bands[band] == NULL)
463                 return (NULL);
464
465         nchans = hw->wiphy->bands[band]->n_channels;
466         if (nchans <= 0)
467                 return (NULL);
468
469         channels = hw->wiphy->bands[band]->channels;
470         for (i = 0; i < nchans; i++) {
471                 if (channels[i].hw_value == c->ic_ieee)
472                         return (&channels[i]);
473         }
474
475         return (NULL);
476 }
477
478 static struct linuxkpi_ieee80211_channel *
479 lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
480 {
481         struct linuxkpi_ieee80211_channel *chan;
482         struct ieee80211_channel *c;
483         struct lkpi_hw *lhw;
484
485         chan = NULL;
486         if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
487                 c = ni->ni_chan;
488         else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
489                 c = ic->ic_bsschan;
490         else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
491                 c = ic->ic_curchan;
492         else
493                 c = NULL;
494
495         if (c != NULL && c != IEEE80211_CHAN_ANYC) {
496                 lhw = ic->ic_softc;
497                 chan = lkpi_find_lkpi80211_chan(lhw, c);
498         }
499
500         return (chan);
501 }
502
503 struct linuxkpi_ieee80211_channel *
504 linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
505 {
506         enum nl80211_band band;
507
508         for (band = 0; band < NUM_NL80211_BANDS; band++) {
509                 struct ieee80211_supported_band *supband;
510                 struct linuxkpi_ieee80211_channel *channels;
511                 int i;
512
513                 supband = wiphy->bands[band];
514                 if (supband == NULL || supband->n_channels == 0)
515                         continue;
516
517                 channels = supband->channels;
518                 for (i = 0; i < supband->n_channels; i++) {
519                         if (channels[i].center_freq == freq)
520                                 return (&channels[i]);
521                 }
522         }
523
524         return (NULL);
525 }
526
527 #ifdef TRY_HW_CRYPTO
528 static int
529 _lkpi_iv_key_set_delete(struct ieee80211vap *vap, const struct ieee80211_key *k,
530     enum set_key_cmd cmd)
531 {
532         struct ieee80211com *ic;
533         struct lkpi_hw *lhw;
534         struct ieee80211_hw *hw;
535         struct lkpi_vif *lvif;
536         struct ieee80211_vif *vif;
537         struct ieee80211_sta *sta;
538         struct ieee80211_node *ni;
539         struct ieee80211_key_conf *kc;
540         int error;
541
542         /* XXX TODO Check (k->wk_flags & IEEE80211_KEY_SWENCRYPT) and don't upload to driver/hw? */
543
544         ic = vap->iv_ic;
545         lhw = ic->ic_softc;
546         hw = LHW_TO_HW(lhw);
547         lvif = VAP_TO_LVIF(vap);
548         vif = LVIF_TO_VIF(lvif);
549
550         memset(&kc, 0, sizeof(kc));
551         kc = malloc(sizeof(*kc) + k->wk_keylen, M_LKPI80211, M_WAITOK | M_ZERO);
552         kc->cipher = lkpi_net80211_to_l80211_cipher_suite(
553             k->wk_cipher->ic_cipher, k->wk_keylen);
554         kc->keyidx = k->wk_keyix;
555 #if 0
556         kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
557 #endif
558         atomic64_set(&kc->tx_pn, k->wk_keytsc);
559         kc->keylen = k->wk_keylen;
560         memcpy(kc->key, k->wk_key, k->wk_keylen);
561
562         switch (kc->cipher) {
563         case WLAN_CIPHER_SUITE_CCMP:
564                 kc->iv_len = k->wk_cipher->ic_header;
565                 kc->icv_len = k->wk_cipher->ic_trailer;
566                 break;
567         case WLAN_CIPHER_SUITE_TKIP:
568         default:
569                 IMPROVE();
570                 return (0);
571         };
572
573         ni = vap->iv_bss;
574         sta = ieee80211_find_sta(vif, ni->ni_bssid);
575         if (sta != NULL) {
576                 struct lkpi_sta *lsta;
577
578                 lsta = STA_TO_LSTA(sta);
579                 lsta->kc = kc;
580         }
581
582         error = lkpi_80211_mo_set_key(hw, cmd, vif, sta, kc);
583         if (error != 0) {
584                 /* XXX-BZ leaking kc currently */
585                 ic_printf(ic, "%s: set_key failed: %d\n", __func__, error);
586                 return (0);
587         } else {
588                 ic_printf(ic, "%s: set_key succeeded: keyidx %u hw_key_idx %u "
589                     "flags %#10x\n", __func__,
590                     kc->keyidx, kc->hw_key_idx, kc->flags);
591                 return (1);
592         }
593 }
594
595 static int
596 lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
597 {
598
599         /* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
600         return (_lkpi_iv_key_set_delete(vap, k, DISABLE_KEY));
601 }
602 static  int
603 lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
604 {
605
606         return (_lkpi_iv_key_set_delete(vap, k, SET_KEY));
607 }
608 #endif
609
610 static u_int
611 lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
612 {
613         struct netdev_hw_addr_list *mc_list;
614         struct netdev_hw_addr *addr;
615
616         KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
617             __func__, arg, sdl, cnt));
618
619         mc_list = arg;
620         /* If it is on the list already skip it. */
621         netdev_hw_addr_list_for_each(addr, mc_list) {
622                 if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
623                         return (0);
624         }
625
626         addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
627         if (addr == NULL)
628                 return (0);
629
630         INIT_LIST_HEAD(&addr->addr_list);
631         memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
632         /* XXX this should be a netdev function? */
633         list_add(&addr->addr_list, &mc_list->addr_list);
634         mc_list->count++;
635
636 #ifdef LINUXKPI_DEBUG_80211
637         if (linuxkpi_debug_80211 & D80211_TRACE)
638                 printf("%s:%d: mc_list count %d: added %6D\n",
639                     __func__, __LINE__, mc_list->count, addr->addr, ":");
640 #endif
641
642         return (1);
643 }
644
645 static void
646 lkpi_update_mcast_filter(struct ieee80211com *ic, bool force)
647 {
648         struct lkpi_hw *lhw;
649         struct ieee80211_hw *hw;
650         struct netdev_hw_addr_list mc_list;
651         struct list_head *le, *next;
652         struct netdev_hw_addr *addr;
653         struct ieee80211vap *vap;
654         u64 mc;
655         unsigned int changed_flags, total_flags;
656
657         lhw = ic->ic_softc;
658
659         if (lhw->ops->prepare_multicast == NULL ||
660             lhw->ops->configure_filter == NULL)
661                 return;
662
663         if (!lhw->update_mc && !force)
664                 return;
665
666         changed_flags = total_flags = 0;
667         mc_list.count = 0;
668         INIT_LIST_HEAD(&mc_list.addr_list);
669         if (ic->ic_allmulti == 0) {
670                 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
671                         if_foreach_llmaddr(vap->iv_ifp,
672                             lkpi_ic_update_mcast_copy, &mc_list);
673         } else {
674                 changed_flags |= FIF_ALLMULTI;
675         }
676
677         hw = LHW_TO_HW(lhw);
678         mc = lkpi_80211_mo_prepare_multicast(hw, &mc_list);
679         /*
680          * XXX-BZ make sure to get this sorted what is a change,
681          * what gets all set; what was already set?
682          */
683         total_flags = changed_flags;
684         lkpi_80211_mo_configure_filter(hw, changed_flags, &total_flags, mc);
685
686 #ifdef LINUXKPI_DEBUG_80211
687         if (linuxkpi_debug_80211 & D80211_TRACE)
688                 printf("%s: changed_flags %#06x count %d total_flags %#010x\n",
689                     __func__, changed_flags, mc_list.count, total_flags);
690 #endif
691
692         if (mc_list.count != 0) {
693                 list_for_each_safe(le, next, &mc_list.addr_list) {
694                         addr = list_entry(le, struct netdev_hw_addr, addr_list);
695                         free(addr, M_LKPI80211);
696                         mc_list.count--;
697                 }
698         }
699         KASSERT(mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
700             __func__, &mc_list, mc_list.count));
701 }
702
703 static enum ieee80211_bss_changed
704 lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
705     struct ieee80211vap *vap, const char *_f, int _l)
706 {
707         enum ieee80211_bss_changed bss_changed;
708
709         bss_changed = 0;
710
711 #ifdef LINUXKPI_DEBUG_80211
712         if (linuxkpi_debug_80211 & D80211_TRACE)
713                 printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
714                     "dtim_period %u sync_dtim_count %u sync_tsf %ju "
715                     "sync_device_ts %u bss_changed %#08x\n",
716                         __func__, __LINE__, _f, _l,
717                         vif->bss_conf.assoc, vif->bss_conf.aid,
718                         vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
719                         vif->bss_conf.sync_dtim_count,
720                         (uintmax_t)vif->bss_conf.sync_tsf,
721                         vif->bss_conf.sync_device_ts,
722                         bss_changed);
723 #endif
724
725         if (vif->bss_conf.beacon_int != ni->ni_intval) {
726                 vif->bss_conf.beacon_int = ni->ni_intval;
727                 /* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
728                 if (vif->bss_conf.beacon_int < 16)
729                         vif->bss_conf.beacon_int = 16;
730                 bss_changed |= BSS_CHANGED_BEACON_INT;
731         }
732         if (vif->bss_conf.dtim_period != vap->iv_dtim_period &&
733             vap->iv_dtim_period > 0) {
734                 vif->bss_conf.dtim_period = vap->iv_dtim_period;
735                 bss_changed |= BSS_CHANGED_BEACON_INFO;
736         }
737
738         vif->bss_conf.sync_dtim_count = vap->iv_dtim_count;
739         vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
740         /* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
741
742 #ifdef LINUXKPI_DEBUG_80211
743         if (linuxkpi_debug_80211 & D80211_TRACE)
744                 printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
745                     "dtim_period %u sync_dtim_count %u sync_tsf %ju "
746                     "sync_device_ts %u bss_changed %#08x\n",
747                         __func__, __LINE__, _f, _l,
748                         vif->bss_conf.assoc, vif->bss_conf.aid,
749                         vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
750                         vif->bss_conf.sync_dtim_count,
751                         (uintmax_t)vif->bss_conf.sync_tsf,
752                         vif->bss_conf.sync_device_ts,
753                         bss_changed);
754 #endif
755
756         return (bss_changed);
757 }
758
759 static void
760 lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
761 {
762         struct ieee80211_hw *hw;
763         int error;
764
765         if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0)
766                 return;
767
768         hw = LHW_TO_HW(lhw);
769
770         IEEE80211_UNLOCK(lhw->ic);
771         LKPI_80211_LHW_LOCK(lhw);
772         /* Need to cancel the scan. */
773         lkpi_80211_mo_cancel_hw_scan(hw, vif);
774
775         /* Need to make sure we see ieee80211_scan_completed. */
776         error = msleep(lhw, &lhw->mtx, 0, "lhwscanstop", hz/2);
777         LKPI_80211_LHW_UNLOCK(lhw);
778         IEEE80211_LOCK(lhw->ic);
779
780         if ((lhw->scan_flags & LKPI_SCAN_RUNNING) != 0)
781                 ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
782                     __func__, error, lhw, vif);
783 }
784
785 static void
786 lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
787 {
788         struct lkpi_hw *lhw;
789         int error;
790         bool old;
791
792         old = hw->conf.flags & IEEE80211_CONF_IDLE;
793         if (old == new)
794                 return;
795
796         hw->conf.flags ^= IEEE80211_CONF_IDLE;
797         error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
798         if (error != 0 && error != EOPNOTSUPP) {
799                 lhw = HW_TO_LHW(hw);
800                 ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
801                     __func__, IEEE80211_CONF_CHANGE_IDLE, error);
802         }
803 }
804
805 static void
806 lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
807     struct lkpi_hw *lhw)
808 {
809         sta->aid = 0;
810         if (vif->bss_conf.assoc) {
811                 struct ieee80211_hw *hw;
812                 enum ieee80211_bss_changed changed;
813
814                 lhw->update_mc = true;
815                 lkpi_update_mcast_filter(lhw->ic, true);
816
817                 changed = 0;
818                 vif->bss_conf.assoc = false;
819                 vif->bss_conf.aid = 0;
820                 changed |= BSS_CHANGED_ASSOC;
821                 /*
822                  * This will remove the sta from firmware for iwlwifi.
823                  * So confusing that they use state and flags and ... ^%$%#%$^.
824                  */
825                 IMPROVE();
826                 hw = LHW_TO_HW(lhw);
827                 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf,
828                     changed);
829
830                 lkpi_hw_conf_idle(hw, true);
831         }
832 }
833
834 static void
835 lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
836     bool dequeue_seen, bool no_emptyq)
837 {
838         struct lkpi_txq *ltxq;
839         int tid;
840
841         /* Wake up all queues to know they are allocated in the driver. */
842         for (tid = 0; tid < nitems(sta->txq); tid++) {
843
844                         if (tid == IEEE80211_NUM_TIDS) {
845                                 IMPROVE("station specific?");
846                                 if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
847                                         continue;
848                         } else if (tid >= hw->queues)
849                                 continue;
850
851                         if (sta->txq[tid] == NULL)
852                                 continue;
853
854                         ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
855                         if (dequeue_seen && !ltxq->seen_dequeue)
856                                 continue;
857
858                         if (no_emptyq && skb_queue_empty(&ltxq->skbq))
859                                 continue;
860
861                         lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid]);
862         }
863 }
864
865 /* -------------------------------------------------------------------------- */
866
867 static int
868 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
869 {
870
871         return (0);
872 }
873
874 /* lkpi_iv_newstate() handles the stop scan case generally. */
875 #define lkpi_sta_scan_to_init(_v, _n, _a)       lkpi_sta_state_do_nada(_v, _n, _a)
876
877 static int
878 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
879 {
880         struct linuxkpi_ieee80211_channel *chan;
881         struct ieee80211_chanctx_conf *conf;
882         struct lkpi_hw *lhw;
883         struct ieee80211_hw *hw;
884         struct lkpi_vif *lvif;
885         struct ieee80211_vif *vif;
886         struct ieee80211_node *ni;
887         struct lkpi_sta *lsta;
888         struct ieee80211_sta *sta;
889         enum ieee80211_bss_changed bss_changed;
890         struct ieee80211_prep_tx_info prep_tx_info;
891         uint32_t changed;
892         int error;
893
894         chan = lkpi_get_lkpi80211_chan(vap->iv_ic, vap->iv_bss);
895         if (chan == NULL) {
896                 ic_printf(vap->iv_ic, "%s: failed to get channel\n", __func__);
897                 return (ESRCH);
898         }
899
900         lhw = vap->iv_ic->ic_softc;
901         hw = LHW_TO_HW(lhw);
902         lvif = VAP_TO_LVIF(vap);
903         vif = LVIF_TO_VIF(lvif);
904
905         ni = ieee80211_ref_node(vap->iv_bss);
906
907         IEEE80211_UNLOCK(vap->iv_ic);
908
909         /* Add chanctx (or if exists, change it). */
910         if (vif->chanctx_conf != NULL) {
911                 conf = vif->chanctx_conf;
912                 IMPROVE("diff changes for changed, working on live copy, rcu");
913         } else {
914                 /* Keep separate alloc as in Linux this is rcu managed? */
915                 conf = malloc(sizeof(*conf) + hw->chanctx_data_size,
916                     M_LKPI80211, M_WAITOK | M_ZERO);
917         }
918
919         conf->rx_chains_dynamic = 1;
920         conf->rx_chains_static = 1;
921         conf->radar_enabled =
922             (chan->flags & IEEE80211_CHAN_RADAR) ? true : false;
923         conf->def.chan = chan;
924         conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
925         conf->def.center_freq1 = chan->center_freq;
926         conf->def.center_freq2 = 0;
927         /* Responder ... */
928         conf->min_def.chan = chan;
929         conf->min_def.width = NL80211_CHAN_WIDTH_20_NOHT;
930         conf->min_def.center_freq1 = chan->center_freq;
931         conf->min_def.center_freq2 = 0;
932         IMPROVE("currently 20_NOHT only");
933
934         error = 0;
935         if (vif->chanctx_conf != NULL) {
936                 changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
937                 changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
938                 changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
939                 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
940                 lkpi_80211_mo_change_chanctx(hw, conf, changed);
941         } else {
942                 error = lkpi_80211_mo_add_chanctx(hw, conf);
943                 if (error == 0 || error == EOPNOTSUPP) {
944                         vif->bss_conf.chandef.chan = conf->def.chan;
945                         vif->bss_conf.chandef.width = conf->def.width;
946                         vif->bss_conf.chandef.center_freq1 =
947                             conf->def.center_freq1;
948                         vif->bss_conf.chandef.center_freq2 =
949                             conf->def.center_freq2;
950                 } else {
951                         goto out;
952                 }
953                 /* Assign vif chanctx. */
954                 if (error == 0)
955                         error = lkpi_80211_mo_assign_vif_chanctx(hw, vif, conf);
956                 if (error == EOPNOTSUPP)
957                         error = 0;
958                 if (error != 0) {
959                         lkpi_80211_mo_remove_chanctx(hw, conf);
960                         free(conf, M_LKPI80211);
961                         goto out;
962                 }
963         }
964         IMPROVE("update radiotap chan fields too");
965
966         /* Set bss info (bss_info_changed). */
967         bss_changed = 0;
968         IEEE80211_ADDR_COPY(vif->bss_conf.bssid, ni->ni_bssid);
969         bss_changed |= BSS_CHANGED_BSSID;
970         vif->bss_conf.txpower = ni->ni_txpower;
971         bss_changed |= BSS_CHANGED_TXPOWER;
972         vif->bss_conf.idle = false;
973         bss_changed |= BSS_CHANGED_IDLE;
974
975         /* Should almost assert it is this. */
976         vif->bss_conf.assoc = false;
977         vif->bss_conf.aid = 0;
978
979         bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
980
981         /* RATES */
982         IMPROVE("bss info: not all needs to come now and rates are missing");
983         lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
984
985         /*
986          * This is a bandaid for now.  If we went through (*iv_update_bss)()
987          * and then removed the lsta we end up here without a lsta and have
988          * to manually allocate and link it in as lkpi_ic_node_alloc()/init()
989          * would normally do.
990          * XXX-BZ I do not like this but currently we have no good way of
991          * intercepting the bss swap and state changes and packets going out
992          * workflow so live with this.  It is a compat layer after all.
993          */
994         if (ni->ni_drv_data == NULL) {
995                 lsta = lkpi_lsta_alloc(vap, ni->ni_macaddr, hw, ni);
996                 if (lsta == NULL) {
997                         error = ENOMEM;
998                         goto out;
999                 }
1000                 lsta->ni = ieee80211_ref_node(ni);
1001                 LKPI_80211_LVIF_LOCK(lvif);
1002                 TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
1003                 LKPI_80211_LVIF_UNLOCK(lvif);
1004         } else {
1005                 lsta = ni->ni_drv_data;
1006         }
1007         /* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
1008         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1009         KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
1010             "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
1011         sta = LSTA_TO_STA(lsta);
1012         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
1013         if (error != 0) {
1014                 IMPROVE("do we need to undo the chan ctx?");
1015                 goto out;
1016         }
1017 #if 0
1018         lsta->added_to_drv = true;      /* mo manages. */
1019 #endif
1020
1021         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1022
1023         /*
1024          * Wakeup all queues now that sta is there so we have as much time to
1025          * possibly prepare the queue in the driver to be ready for the 1st
1026          * packet;  lkpi_80211_txq_tx_one() still has a workaround as there
1027          * is no guarantee or way to check.
1028          * XXX-BZ and by now we know that this does not work on all drivers
1029          * for all queues.
1030          */
1031         lkpi_wake_tx_queues(hw, sta, false, false);
1032
1033         {
1034                 int i, count;
1035
1036                 for (i = 3 * (hw->queues + 1); i > 0; i--) {
1037                         struct lkpi_txq *ltxq;
1038                         int tid;
1039
1040                         count = 0;
1041                         /* Wake up all queues to know they are allocated in the driver. */
1042                         for (tid = 0; tid < nitems(sta->txq); tid++) {
1043
1044                                 if (tid == IEEE80211_NUM_TIDS) {
1045                                         IMPROVE("station specific?");
1046                                         if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
1047                                                 continue;
1048                                 } else if (tid >= hw->queues)
1049                                         continue;
1050
1051                                 if (sta->txq[tid] == NULL)
1052                                         continue;
1053
1054                                 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
1055                                 if (!ltxq->seen_dequeue)
1056                                         count++;
1057                         }
1058                         if (count == 0)
1059                                 break;
1060 #ifdef LINUXKPI_DEBUG_80211
1061                         if (count > 0)
1062                                 ic_printf(vap->iv_ic, "%s: waiting for %d queues "
1063                                     "to be allocated by driver\n", __func__, count);
1064 #endif
1065                         pause("lkpi80211txq", hz/10);
1066                 }
1067 #ifdef LINUXKPI_DEBUG_80211
1068                 if (count > 0)
1069                         ic_printf(vap->iv_ic, "%s: %d queues still not "
1070                             "allocated by driver\n", __func__, count);
1071 #endif
1072         }
1073
1074         /* Start mgd_prepare_tx. */
1075         memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1076         prep_tx_info.duration = PREP_TX_INFO_DURATION;
1077         lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1078         lsta->in_mgd = true;
1079
1080         /*
1081          * What is going to happen next:
1082          * - <twiddle> .. we should end up in "auth_to_assoc"
1083          * - event_callback
1084          * - update sta_state (NONE to AUTH)
1085          * - mgd_complete_tx
1086          * (ideally we'd do that on a callback for something else ...)
1087          */
1088
1089 out:
1090         IEEE80211_LOCK(vap->iv_ic);
1091         if (ni != NULL)
1092                 ieee80211_free_node(ni);
1093         return (error);
1094 }
1095
1096 static int
1097 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1098 {
1099         struct lkpi_hw *lhw;
1100         struct ieee80211_hw *hw;
1101         struct lkpi_vif *lvif;
1102         struct ieee80211_vif *vif;
1103         struct ieee80211_node *ni;
1104         struct lkpi_sta *lsta;
1105         struct ieee80211_sta *sta;
1106         struct ieee80211_prep_tx_info prep_tx_info;
1107         int error;
1108
1109         lhw = vap->iv_ic->ic_softc;
1110         hw = LHW_TO_HW(lhw);
1111         lvif = VAP_TO_LVIF(vap);
1112         vif = LVIF_TO_VIF(lvif);
1113
1114         /* Keep ni around. */
1115         ni = ieee80211_ref_node(vap->iv_bss);
1116         lsta = ni->ni_drv_data;
1117         sta = LSTA_TO_STA(lsta);
1118
1119         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1120
1121         IEEE80211_UNLOCK(vap->iv_ic);
1122
1123         /* flush, drop. */
1124         lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1125
1126         /* Wake tx queues to get packet(s) out. */
1127         lkpi_wake_tx_queues(hw, sta, true, true);
1128
1129         /* flush, no drop */
1130         lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1131
1132         /* End mgd_complete_tx. */
1133         if (lsta->in_mgd) {
1134                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1135                 prep_tx_info.success = false;
1136                 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1137                 lsta->in_mgd = false;
1138         }
1139
1140         /* sync_rx_queues */
1141         lkpi_80211_mo_sync_rx_queues(hw);
1142
1143         /* sta_pre_rcu_remove */
1144         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1145
1146         /* Take the station down. */
1147
1148         /* Adjust sta and change state (from NONE) to NOTEXIST. */
1149         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1150         KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1151             "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1152         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST);
1153         if (error != 0) {
1154                 IMPROVE("do we need to undo the chan ctx?");
1155                 goto out;
1156         }
1157 #if 0
1158         lsta->added_to_drv = false;     /* mo manages. */
1159 #endif
1160
1161         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1162
1163         lkpi_lsta_remove(lsta, lvif);
1164
1165         /* conf_tx */
1166
1167         /* Take the chan ctx down. */
1168         if (vif->chanctx_conf != NULL) {
1169                 struct ieee80211_chanctx_conf *conf;
1170
1171                 conf = vif->chanctx_conf;
1172                 /* Remove vif context. */
1173                 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
1174                 /* NB: vif->chanctx_conf is NULL now. */
1175
1176                 /* Remove chan ctx. */
1177                 lkpi_80211_mo_remove_chanctx(hw, conf);
1178                 free(conf, M_LKPI80211);
1179         }
1180
1181 out:
1182         IEEE80211_LOCK(vap->iv_ic);
1183         if (ni != NULL)
1184                 ieee80211_free_node(ni);
1185         return (error);
1186 }
1187
1188 static int
1189 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1190 {
1191         int error;
1192
1193         error = lkpi_sta_auth_to_scan(vap, nstate, arg);
1194         if (error == 0)
1195                 error = lkpi_sta_scan_to_init(vap, nstate, arg);
1196         return (error);
1197 }
1198
1199 static int
1200 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1201 {
1202         struct lkpi_hw *lhw;
1203         struct ieee80211_hw *hw;
1204         struct lkpi_vif *lvif;
1205         struct ieee80211_vif *vif;
1206         struct ieee80211_node *ni;
1207         struct lkpi_sta *lsta;
1208         struct ieee80211_sta *sta;
1209         struct ieee80211_prep_tx_info prep_tx_info;
1210         int error;
1211
1212         lhw = vap->iv_ic->ic_softc;
1213         hw = LHW_TO_HW(lhw);
1214         lvif = VAP_TO_LVIF(vap);
1215         vif = LVIF_TO_VIF(lvif);
1216
1217         IEEE80211_UNLOCK(vap->iv_ic);
1218         ni = NULL;
1219
1220         /* Finish auth. */
1221         IMPROVE("event callback");
1222
1223         /* Update sta_state (NONE to AUTH). */
1224         ni = ieee80211_ref_node(vap->iv_bss);
1225         lsta = ni->ni_drv_data;
1226         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1227         KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1228             "NONE: %#x\n", __func__, lsta, lsta->state));
1229         sta = LSTA_TO_STA(lsta);
1230         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
1231         if (error != 0)
1232                 goto out;
1233
1234         /* End mgd_complete_tx. */
1235         if (lsta->in_mgd) {
1236                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1237                 prep_tx_info.success = true;
1238                 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1239                 lsta->in_mgd = false;
1240         }
1241
1242         /* Now start assoc. */
1243
1244         /* Start mgd_prepare_tx. */
1245         if (!lsta->in_mgd) {
1246                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1247                 prep_tx_info.duration = PREP_TX_INFO_DURATION;
1248                 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1249                 lsta->in_mgd = true;
1250         }
1251
1252         /* Wake tx queue to get packet out. */
1253         lkpi_wake_tx_queues(hw, sta, true, true);
1254
1255         /*
1256          * <twiddle> .. we end up in "assoc_to_run"
1257          * - update sta_state (AUTH to ASSOC)
1258          * - conf_tx [all]
1259          * - bss_info_changed (assoc, aid, ssid, ..)
1260          * - change_chanctx (if needed)
1261          * - event_callback
1262          * - mgd_complete_tx
1263          */
1264
1265 out:
1266         IEEE80211_LOCK(vap->iv_ic);
1267         if (ni != NULL)
1268                 ieee80211_free_node(ni);
1269         return (error);
1270 }
1271
1272 /* auth_to_auth, assoc_to_assoc. */
1273 static int
1274 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1275 {
1276         struct lkpi_hw *lhw;
1277         struct ieee80211_hw *hw;
1278         struct lkpi_vif *lvif;
1279         struct ieee80211_vif *vif;
1280         struct ieee80211_node *ni;
1281         struct lkpi_sta *lsta;
1282         struct ieee80211_prep_tx_info prep_tx_info;
1283
1284         lhw = vap->iv_ic->ic_softc;
1285         hw = LHW_TO_HW(lhw);
1286         lvif = VAP_TO_LVIF(vap);
1287         vif = LVIF_TO_VIF(lvif);
1288
1289         ni = ieee80211_ref_node(vap->iv_bss);
1290
1291         IEEE80211_UNLOCK(vap->iv_ic);
1292         lsta = ni->ni_drv_data;
1293
1294         IMPROVE("event callback?");
1295
1296         /* End mgd_complete_tx. */
1297         if (lsta->in_mgd) {
1298                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1299                 prep_tx_info.success = false;
1300                 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1301                 lsta->in_mgd = false;
1302         }
1303
1304         /* Now start assoc. */
1305
1306         /* Start mgd_prepare_tx. */
1307         if (!lsta->in_mgd) {
1308                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1309                 prep_tx_info.duration = PREP_TX_INFO_DURATION;
1310                 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1311                 lsta->in_mgd = true;
1312         }
1313
1314         IEEE80211_LOCK(vap->iv_ic);
1315         if (ni != NULL)
1316                 ieee80211_free_node(ni);
1317
1318         return (0);
1319 }
1320
1321 static int
1322 _lkpi_sta_assoc_to_down(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1323 {
1324         struct lkpi_hw *lhw;
1325         struct ieee80211_hw *hw;
1326         struct lkpi_vif *lvif;
1327         struct ieee80211_vif *vif;
1328         struct ieee80211_node *ni;
1329         struct lkpi_sta *lsta;
1330         struct ieee80211_sta *sta;
1331         struct ieee80211_prep_tx_info prep_tx_info;
1332         enum ieee80211_bss_changed bss_changed;
1333         int error;
1334
1335         lhw = vap->iv_ic->ic_softc;
1336         hw = LHW_TO_HW(lhw);
1337         lvif = VAP_TO_LVIF(vap);
1338         vif = LVIF_TO_VIF(lvif);
1339
1340         /* Keep ni around. */
1341         ni = ieee80211_ref_node(vap->iv_bss);
1342         lsta = ni->ni_drv_data;
1343         sta = LSTA_TO_STA(lsta);
1344
1345         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1346
1347         IEEE80211_UNLOCK(vap->iv_ic);
1348
1349         /* flush, drop. */
1350         lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1351
1352         IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1353         if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1354             !lsta->in_mgd) {
1355                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1356                 prep_tx_info.duration = PREP_TX_INFO_DURATION;
1357                 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1358                 lsta->in_mgd = true;
1359         }
1360
1361         IEEE80211_LOCK(vap->iv_ic);
1362
1363         /* Call iv_newstate first so we get potential DISASSOC packet out. */
1364         error = lvif->iv_newstate(vap, nstate, arg);
1365         if (error != 0)
1366                 goto outni;
1367
1368         IEEE80211_UNLOCK(vap->iv_ic);
1369
1370         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1371
1372         /* Wake tx queues to get packet(s) out. */
1373         lkpi_wake_tx_queues(hw, sta, true, true);
1374
1375         /* flush, no drop */
1376         lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1377
1378         /* End mgd_complete_tx. */
1379         if (lsta->in_mgd) {
1380                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1381                 prep_tx_info.success = false;
1382                 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1383                 lsta->in_mgd = false;
1384         }
1385
1386         /* sync_rx_queues */
1387         lkpi_80211_mo_sync_rx_queues(hw);
1388
1389         /* sta_pre_rcu_remove */
1390         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1391
1392         /* Take the station down. */
1393
1394         /* Update sta and change state (from AUTH) to NONE. */
1395         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1396         KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1397             "AUTH: %#x\n", __func__, lsta, lsta->state));
1398         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
1399         if (error != 0)
1400                 goto out;
1401
1402         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1403
1404         /* Adjust sta and change state (from NONE) to NOTEXIST. */
1405         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1406         KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1407             "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1408         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST);
1409         if (error != 0) {
1410                 IMPROVE("do we need to undo the chan ctx?");
1411                 goto out;
1412         }
1413 #if 0
1414         lsta->added_to_drv = false;     /* mo manages. */
1415 #endif
1416
1417         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1418
1419         /* Update bss info (bss_info_changed) (assoc, aid, ..). */
1420         /* We need to do this now, can only do after sta is IEEE80211_STA_NOTEXIST. */
1421         lkpi_disassoc(sta, vif, lhw);
1422
1423         IMPROVE("Any bss_info changes to announce?");
1424         bss_changed = 0;
1425         vif->bss_conf.qos = 0;
1426         bss_changed |= BSS_CHANGED_QOS;
1427         vif->bss_conf.ssid_len = 0;
1428         memset(vif->bss_conf.ssid, '\0', sizeof(vif->bss_conf.ssid));
1429         bss_changed |= BSS_CHANGED_BSSID;
1430         lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1431
1432         lkpi_lsta_remove(lsta, lvif);
1433
1434         /* conf_tx */
1435
1436         /* Take the chan ctx down. */
1437         if (vif->chanctx_conf != NULL) {
1438                 struct ieee80211_chanctx_conf *conf;
1439
1440                 conf = vif->chanctx_conf;
1441                 /* Remove vif context. */
1442                 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
1443                 /* NB: vif->chanctx_conf is NULL now. */
1444
1445                 /* Remove chan ctx. */
1446                 lkpi_80211_mo_remove_chanctx(hw, conf);
1447                 free(conf, M_LKPI80211);
1448         }
1449
1450         error = EALREADY;
1451 out:
1452         IEEE80211_LOCK(vap->iv_ic);
1453 outni:
1454         if (ni != NULL)
1455                 ieee80211_free_node(ni);
1456         return (error);
1457 }
1458
1459 static int
1460 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1461 {
1462         int error;
1463
1464         error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1465         if (error != 0 && error != EALREADY)
1466                 return (error);
1467
1468         /* At this point iv_bss is long a new node! */
1469
1470         error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1471         return (error);
1472 }
1473
1474 static int
1475 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1476 {
1477         int error;
1478
1479         error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1480         return (error);
1481 }
1482
1483 static int
1484 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1485 {
1486         int error;
1487
1488         error = _lkpi_sta_assoc_to_down(vap, nstate, arg);
1489         return (error);
1490 }
1491
1492 static int
1493 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1494 {
1495         struct lkpi_hw *lhw;
1496         struct ieee80211_hw *hw;
1497         struct lkpi_vif *lvif;
1498         struct ieee80211_vif *vif;
1499         struct ieee80211_node *ni;
1500         struct lkpi_sta *lsta;
1501         struct ieee80211_sta *sta;
1502         struct ieee80211_prep_tx_info prep_tx_info;
1503         enum ieee80211_bss_changed bss_changed;
1504         int error;
1505
1506         lhw = vap->iv_ic->ic_softc;
1507         hw = LHW_TO_HW(lhw);
1508         lvif = VAP_TO_LVIF(vap);
1509         vif = LVIF_TO_VIF(lvif);
1510
1511         IEEE80211_UNLOCK(vap->iv_ic);
1512         ni = NULL;
1513
1514         IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
1515             "and to lesser extend ieee80211_notify_node_join");
1516
1517         /* Finish assoc. */
1518         /* Update sta_state (AUTH to ASSOC) and set aid. */
1519         ni = ieee80211_ref_node(vap->iv_bss);
1520         lsta = ni->ni_drv_data;
1521         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1522         KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1523             "AUTH: %#x\n", __func__, lsta, lsta->state));
1524         sta = LSTA_TO_STA(lsta);
1525         sta->aid = IEEE80211_NODE_AID(ni);
1526         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
1527         if (error != 0)
1528                 goto out;
1529
1530         IMPROVE("wme / conf_tx [all]");
1531
1532         /* Update bss info (bss_info_changed) (assoc, aid, ..). */
1533         bss_changed = 0;
1534         if (!vif->bss_conf.assoc || vif->bss_conf.aid != IEEE80211_NODE_AID(ni)) {
1535                 vif->bss_conf.assoc = true;
1536                 vif->bss_conf.aid = IEEE80211_NODE_AID(ni);
1537                 bss_changed |= BSS_CHANGED_ASSOC;
1538         }
1539         /* We set SSID but this is not BSSID! */
1540         vif->bss_conf.ssid_len = ni->ni_esslen;
1541         memcpy(vif->bss_conf.ssid, ni->ni_essid, ni->ni_esslen);
1542         if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
1543             vif->bss_conf.use_short_preamble) {
1544                 vif->bss_conf.use_short_preamble ^= 1;
1545                 /* bss_changed |= BSS_CHANGED_??? */
1546         }
1547         if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
1548             vif->bss_conf.use_short_slot) {
1549                 vif->bss_conf.use_short_slot ^= 1;
1550                 /* bss_changed |= BSS_CHANGED_??? */
1551         }
1552         if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
1553             vif->bss_conf.qos) {
1554                 vif->bss_conf.qos ^= 1;
1555                 bss_changed |= BSS_CHANGED_QOS;
1556         }
1557
1558         bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1559
1560         lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1561
1562         /* - change_chanctx (if needed)
1563          * - event_callback
1564          */
1565
1566         /* End mgd_complete_tx. */
1567         if (lsta->in_mgd) {
1568                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1569                 prep_tx_info.success = true;
1570                 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1571                 lsta->in_mgd = false;
1572         }
1573
1574         lkpi_hw_conf_idle(hw, false);
1575
1576         /*
1577          * And then:
1578          * - (more packets)?
1579          * - set_key
1580          * - set_default_unicast_key
1581          * - set_key (?)
1582          * - ipv6_addr_change (?)
1583          */
1584         /* Prepare_multicast && configure_filter. */
1585         lhw->update_mc = true;
1586         lkpi_update_mcast_filter(vap->iv_ic, true);
1587
1588         if (!ieee80211_node_is_authorized(ni)) {
1589                 IMPROVE("net80211 does not consider node authorized");
1590         }
1591
1592         /* Update sta_state (ASSOC to AUTHORIZED). */
1593         ni = ieee80211_ref_node(vap->iv_bss);
1594         lsta = ni->ni_drv_data;
1595         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1596         KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1597             "ASSOC: %#x\n", __func__, lsta, lsta->state));
1598         sta = LSTA_TO_STA(lsta);
1599         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTHORIZED);
1600         if (error != 0) {
1601                 IMPROVE("undo some changes?");
1602                 goto out;
1603         }
1604
1605         /* - drv_config (?)
1606          * - bss_info_changed
1607          * - set_rekey_data (?)
1608          *
1609          * And now we should be passing packets.
1610          */
1611         IMPROVE("Need that bssid setting, and the keys");
1612
1613         bss_changed = 0;
1614         bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
1615         lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1616
1617 out:
1618         IEEE80211_LOCK(vap->iv_ic);
1619         if (ni != NULL)
1620                 ieee80211_free_node(ni);
1621         return (error);
1622 }
1623
1624 static int
1625 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1626 {
1627         int error;
1628
1629         error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
1630         if (error == 0)
1631                 error = lkpi_sta_assoc_to_run(vap, nstate, arg);
1632         return (error);
1633 }
1634
1635 static int
1636 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1637 {
1638         struct lkpi_hw *lhw;
1639         struct ieee80211_hw *hw;
1640         struct lkpi_vif *lvif;
1641         struct ieee80211_vif *vif;
1642         struct ieee80211_node *ni;
1643         struct lkpi_sta *lsta;
1644         struct ieee80211_sta *sta;
1645         struct ieee80211_prep_tx_info prep_tx_info;
1646 #if 0
1647         enum ieee80211_bss_changed bss_changed;
1648 #endif
1649         int error;
1650
1651         lhw = vap->iv_ic->ic_softc;
1652         hw = LHW_TO_HW(lhw);
1653         lvif = VAP_TO_LVIF(vap);
1654         vif = LVIF_TO_VIF(lvif);
1655
1656         /* Keep ni around. */
1657         ni = ieee80211_ref_node(vap->iv_bss);
1658         lsta = ni->ni_drv_data;
1659         sta = LSTA_TO_STA(lsta);
1660
1661         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1662
1663         IEEE80211_UNLOCK(vap->iv_ic);
1664
1665         /* flush, drop. */
1666         lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1667
1668         IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1669         if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1670             !lsta->in_mgd) {
1671                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1672                 prep_tx_info.duration = PREP_TX_INFO_DURATION;
1673                 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1674                 lsta->in_mgd = true;
1675         }
1676
1677         IEEE80211_LOCK(vap->iv_ic);
1678
1679         /* Call iv_newstate first so we get potential DISASSOC packet out. */
1680         error = lvif->iv_newstate(vap, nstate, arg);
1681         if (error != 0)
1682                 goto outni;
1683
1684         IEEE80211_UNLOCK(vap->iv_ic);
1685
1686         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1687
1688         /* Wake tx queues to get packet(s) out. */
1689         lkpi_wake_tx_queues(hw, sta, true, true);
1690
1691         /* flush, no drop */
1692         lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1693
1694         /* End mgd_complete_tx. */
1695         if (lsta->in_mgd) {
1696                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1697                 prep_tx_info.success = false;
1698                 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1699                 lsta->in_mgd = false;
1700         }
1701
1702 #if 0
1703         /* sync_rx_queues */
1704         lkpi_80211_mo_sync_rx_queues(hw);
1705
1706         /* sta_pre_rcu_remove */
1707         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1708 #endif
1709
1710         /* Take the station down. */
1711
1712         /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1713         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1714         KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1715             "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1716         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
1717         if (error != 0)
1718                 goto out;
1719
1720         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1721
1722         /* Update sta_state (ASSOC to AUTH). */
1723         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1724         KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1725             "ASSOC: %#x\n", __func__, lsta, lsta->state));
1726         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
1727         if (error != 0)
1728                 goto out;
1729
1730         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1731
1732 #if 0
1733         /* Update bss info (bss_info_changed) (assoc, aid, ..). */
1734         lkpi_disassoc(sta, vif, lhw);
1735 #endif
1736
1737         error = EALREADY;
1738 out:
1739         IEEE80211_LOCK(vap->iv_ic);
1740 outni:
1741         if (ni != NULL)
1742                 ieee80211_free_node(ni);
1743         return (error);
1744 }
1745
1746 static int
1747 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1748 {
1749         struct lkpi_hw *lhw;
1750         struct ieee80211_hw *hw;
1751         struct lkpi_vif *lvif;
1752         struct ieee80211_vif *vif;
1753         struct ieee80211_node *ni;
1754         struct lkpi_sta *lsta;
1755         struct ieee80211_sta *sta;
1756         struct ieee80211_prep_tx_info prep_tx_info;
1757         enum ieee80211_bss_changed bss_changed;
1758         int error;
1759
1760         lhw = vap->iv_ic->ic_softc;
1761         hw = LHW_TO_HW(lhw);
1762         lvif = VAP_TO_LVIF(vap);
1763         vif = LVIF_TO_VIF(lvif);
1764
1765         /* Keep ni around. */
1766         ni = ieee80211_ref_node(vap->iv_bss);
1767         lsta = ni->ni_drv_data;
1768         sta = LSTA_TO_STA(lsta);
1769
1770         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1771
1772         IEEE80211_UNLOCK(vap->iv_ic);
1773
1774         /* flush, drop. */
1775         lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), true);
1776
1777         IMPROVE("What are the proper conditions for DEAUTH_NEED_MGD_TX_PREP?");
1778         if (ieee80211_hw_check(hw, DEAUTH_NEED_MGD_TX_PREP) &&
1779             !lsta->in_mgd) {
1780                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1781                 prep_tx_info.duration = PREP_TX_INFO_DURATION;
1782                 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
1783                 lsta->in_mgd = true;
1784         }
1785
1786         IEEE80211_LOCK(vap->iv_ic);
1787
1788         /* Call iv_newstate first so we get potential DISASSOC packet out. */
1789         error = lvif->iv_newstate(vap, nstate, arg);
1790         if (error != 0)
1791                 goto outni;
1792
1793         IEEE80211_UNLOCK(vap->iv_ic);
1794
1795         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1796
1797         /* Wake tx queues to get packet(s) out. */
1798         lkpi_wake_tx_queues(hw, sta, true, true);
1799
1800         /* flush, no drop */
1801         lkpi_80211_mo_flush(hw, vif,  nitems(sta->txq), false);
1802
1803         /* End mgd_complete_tx. */
1804         if (lsta->in_mgd) {
1805                 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
1806                 prep_tx_info.success = false;
1807                 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
1808                 lsta->in_mgd = false;
1809         }
1810
1811         /* sync_rx_queues */
1812         lkpi_80211_mo_sync_rx_queues(hw);
1813
1814         /* sta_pre_rcu_remove */
1815         lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
1816
1817         /* Take the station down. */
1818
1819         /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
1820         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1821         KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
1822             "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
1823         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_ASSOC);
1824         if (error != 0)
1825                 goto out;
1826
1827         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1828
1829         /* Update sta_state (ASSOC to AUTH). */
1830         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1831         KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
1832             "ASSOC: %#x\n", __func__, lsta, lsta->state));
1833         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_AUTH);
1834         if (error != 0)
1835                 goto out;
1836
1837         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1838
1839         /* Update sta and change state (from AUTH) to NONE. */
1840         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1841         KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
1842             "AUTH: %#x\n", __func__, lsta, lsta->state));
1843         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NONE);
1844         if (error != 0)
1845                 goto out;
1846
1847         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1848
1849         /* Adjust sta and change state (from NONE) to NOTEXIST. */
1850         KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
1851         KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
1852             "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
1853         error = lkpi_80211_mo_sta_state(hw, vif, sta, IEEE80211_STA_NOTEXIST);
1854         if (error != 0) {
1855                 IMPROVE("do we need to undo the chan ctx?");
1856                 goto out;
1857         }
1858 #if 0
1859         lsta->added_to_drv = false;     /* mo manages. */
1860 #endif
1861
1862         lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
1863
1864         /* Update bss info (bss_info_changed) (assoc, aid, ..). */
1865         /*
1866          * One would expect this to happen when going off AUTHORIZED.
1867          * See comment there; removes the sta from fw.
1868          */
1869         lkpi_disassoc(sta, vif, lhw);
1870
1871         IMPROVE("Any bss_info changes to announce?");
1872         bss_changed = 0;
1873         vif->bss_conf.qos = 0;
1874         bss_changed |= BSS_CHANGED_QOS;
1875         vif->bss_conf.ssid_len = 0;
1876         memset(vif->bss_conf.ssid, '\0', sizeof(vif->bss_conf.ssid));
1877         bss_changed |= BSS_CHANGED_BSSID;
1878         lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
1879
1880         lkpi_lsta_remove(lsta, lvif);
1881
1882         /* conf_tx */
1883
1884         /* Take the chan ctx down. */
1885         if (vif->chanctx_conf != NULL) {
1886                 struct ieee80211_chanctx_conf *conf;
1887
1888                 conf = vif->chanctx_conf;
1889                 /* Remove vif context. */
1890                 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->chanctx_conf);
1891                 /* NB: vif->chanctx_conf is NULL now. */
1892
1893                 /* Remove chan ctx. */
1894                 lkpi_80211_mo_remove_chanctx(hw, conf);
1895                 free(conf, M_LKPI80211);
1896         }
1897
1898         error = EALREADY;
1899 out:
1900         IEEE80211_LOCK(vap->iv_ic);
1901 outni:
1902         if (ni != NULL)
1903                 ieee80211_free_node(ni);
1904         return (error);
1905 }
1906
1907 static int
1908 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1909 {
1910
1911         return (lkpi_sta_run_to_init(vap, nstate, arg));
1912 }
1913
1914 static int
1915 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1916 {
1917         int error;
1918
1919         error = lkpi_sta_run_to_init(vap, nstate, arg);
1920         if (error != 0 && error != EALREADY)
1921                 return (error);
1922
1923         /* At this point iv_bss is long a new node! */
1924
1925         error |= lkpi_sta_scan_to_auth(vap, nstate, 0);
1926         return (error);
1927 }
1928
1929 /* -------------------------------------------------------------------------- */
1930
1931 /*
1932  * The matches the documented state changes in net80211::sta_newstate().
1933  * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
1934  * there are "invalid" (so there is a room for failure here).
1935  */
1936 struct fsm_state {
1937         /* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
1938         enum ieee80211_state ostate;
1939         enum ieee80211_state nstate;
1940         int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
1941 } sta_state_fsm[] = {
1942         { IEEE80211_S_INIT,     IEEE80211_S_INIT, lkpi_sta_state_do_nada },
1943         { IEEE80211_S_SCAN,     IEEE80211_S_INIT, lkpi_sta_state_do_nada },     /* scan_to_init */
1944         { IEEE80211_S_AUTH,     IEEE80211_S_INIT, lkpi_sta_auth_to_init },      /* not explicitly in sta_newstate() */
1945         { IEEE80211_S_ASSOC,    IEEE80211_S_INIT, lkpi_sta_assoc_to_init },     /* Send DEAUTH. */
1946         { IEEE80211_S_RUN,      IEEE80211_S_INIT, lkpi_sta_run_to_init },       /* Send DISASSOC. */
1947
1948         { IEEE80211_S_INIT,     IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
1949         { IEEE80211_S_SCAN,     IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
1950         { IEEE80211_S_AUTH,     IEEE80211_S_SCAN, lkpi_sta_auth_to_scan },
1951         { IEEE80211_S_ASSOC,    IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
1952         { IEEE80211_S_RUN,      IEEE80211_S_SCAN, lkpi_sta_run_to_scan },       /* Beacon miss. */
1953
1954         { IEEE80211_S_INIT,     IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },      /* Send AUTH. */
1955         { IEEE80211_S_SCAN,     IEEE80211_S_AUTH, lkpi_sta_scan_to_auth },      /* Send AUTH. */
1956         { IEEE80211_S_AUTH,     IEEE80211_S_AUTH, lkpi_sta_a_to_a },            /* Send ?AUTH. */
1957         { IEEE80211_S_ASSOC,    IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth },     /* Send ?AUTH. */
1958         { IEEE80211_S_RUN,      IEEE80211_S_AUTH, lkpi_sta_run_to_auth },       /* Send ?AUTH. */
1959
1960         { IEEE80211_S_AUTH,     IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc },    /* Send ASSOCREQ. */
1961         { IEEE80211_S_ASSOC,    IEEE80211_S_ASSOC, lkpi_sta_a_to_a },           /* Send ASSOCREQ. */
1962         { IEEE80211_S_RUN,      IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc },     /* Send ASSOCREQ/REASSOCREQ. */
1963
1964         { IEEE80211_S_AUTH,     IEEE80211_S_RUN, lkpi_sta_auth_to_run },
1965         { IEEE80211_S_ASSOC,    IEEE80211_S_RUN, lkpi_sta_assoc_to_run },
1966         { IEEE80211_S_RUN,      IEEE80211_S_RUN, lkpi_sta_state_do_nada },
1967
1968         /* Dummy at the end without handler. */
1969         { IEEE80211_S_INIT,     IEEE80211_S_INIT, NULL },
1970 };
1971
1972 static int
1973 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
1974 {
1975         struct ieee80211com *ic;
1976         struct lkpi_hw *lhw;
1977         struct lkpi_vif *lvif;
1978         struct ieee80211_vif *vif;
1979         struct fsm_state *s;
1980         enum ieee80211_state ostate;
1981         int error;
1982
1983         ic = vap->iv_ic;
1984         IEEE80211_LOCK_ASSERT(ic);
1985         ostate = vap->iv_state;
1986
1987 #ifdef LINUXKPI_DEBUG_80211
1988         if (linuxkpi_debug_80211 & D80211_TRACE)
1989                 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
1990                     __func__, __LINE__, vap, nstate, arg);
1991 #endif
1992
1993         if (vap->iv_opmode == IEEE80211_M_STA) {
1994
1995                 lhw = ic->ic_softc;
1996                 lvif = VAP_TO_LVIF(vap);
1997                 vif = LVIF_TO_VIF(lvif);
1998
1999                 /* No need to replicate this in most state handlers. */
2000                 if (ostate == IEEE80211_S_SCAN && nstate != IEEE80211_S_SCAN)
2001                         lkpi_stop_hw_scan(lhw, vif);
2002
2003                 s = sta_state_fsm;
2004
2005         } else {
2006                 ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
2007                     "cap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
2008                 return (ENOSYS);
2009         }
2010
2011         error = 0;
2012         for (; s->handler != NULL; s++) {
2013                 if (ostate == s->ostate && nstate == s->nstate) {
2014 #ifdef LINUXKPI_DEBUG_80211
2015                         if (linuxkpi_debug_80211 & D80211_TRACE)
2016                                 ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
2017                                     " %d (%s): arg %d.\n", __func__,
2018                                     ostate, ieee80211_state_name[ostate],
2019                                     nstate, ieee80211_state_name[nstate], arg);
2020 #endif
2021                         error = s->handler(vap, nstate, arg);
2022                         break;
2023                 }
2024         }
2025         IEEE80211_LOCK_ASSERT(vap->iv_ic);
2026
2027         if (s->handler == NULL) {
2028                 IMPROVE("turn this into a KASSERT\n");
2029                 ic_printf(vap->iv_ic, "%s: unsupported state transition "
2030                     "%d (%s) -> %d (%s)\n", __func__,
2031                     ostate, ieee80211_state_name[ostate],
2032                     nstate, ieee80211_state_name[nstate]);
2033                 return (ENOSYS);
2034         }
2035
2036         if (error == EALREADY) {
2037 #ifdef LINUXKPI_DEBUG_80211
2038                 if (linuxkpi_debug_80211 & D80211_TRACE)
2039                         ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
2040                             "%d (%s): iv_newstate already handled: %d.\n",
2041                             __func__, ostate, ieee80211_state_name[ostate],
2042                             nstate, ieee80211_state_name[nstate], error);
2043 #endif
2044                 return (0);
2045         }
2046
2047         if (error != 0) {
2048                 /* XXX-BZ currently expected so ignore. */
2049                 ic_printf(vap->iv_ic, "%s: error %d during state transition "
2050                     "%d (%s) -> %d (%s)\n", __func__, error,
2051                     ostate, ieee80211_state_name[ostate],
2052                     nstate, ieee80211_state_name[nstate]);
2053                 /* return (error); */
2054         }
2055
2056 #ifdef LINUXKPI_DEBUG_80211
2057         if (linuxkpi_debug_80211 & D80211_TRACE)
2058                 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
2059                     "calling net80211 parent\n",
2060                     __func__, __LINE__, vap, nstate, arg);
2061 #endif
2062
2063         return (lvif->iv_newstate(vap, nstate, arg));
2064 }
2065
2066 /* -------------------------------------------------------------------------- */
2067
2068 /*
2069  * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
2070  * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
2071  * new node without us knowing and thus our ni/lsta are out of sync.
2072  */
2073 static struct ieee80211_node *
2074 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
2075 {
2076         struct lkpi_vif *lvif;
2077         struct ieee80211_node *obss;
2078         struct lkpi_sta *lsta;
2079
2080         lvif = VAP_TO_LVIF(vap);
2081         obss = vap->iv_bss;
2082
2083 #ifdef LINUXKPI_DEBUG_80211
2084         if (linuxkpi_debug_80211 & D80211_TRACE)
2085                 ic_printf(vap->iv_ic, "%s: obss %p ni_drv_data %p "
2086                     "ni %p ni_drv_data %p\n", __func__,
2087                     obss, (obss != NULL) ? obss->ni_drv_data : NULL,
2088                     ni, (ni != NULL) ? ni->ni_drv_data : NULL);
2089 #endif
2090
2091         /* Nothing to copy from.  Just return. */
2092         if (obss == NULL || obss->ni_drv_data == NULL)
2093                 goto out;
2094
2095         /* Nothing to copy to.  Just return. */
2096         IMPROVE("clearing the obss might still be needed?");
2097         if (ni == NULL)
2098                 goto out;
2099
2100         /* Nothing changed? panic? */
2101         if (obss == ni)
2102                 goto out;
2103
2104         lsta = obss->ni_drv_data;
2105         obss->ni_drv_data = ni->ni_drv_data;
2106         ni->ni_drv_data = lsta;
2107         if (lsta != NULL)
2108                 lsta->ni = ni;
2109         lsta = obss->ni_drv_data;
2110         if (lsta != NULL)
2111                 lsta->ni = obss;
2112
2113 out:
2114         return (lvif->iv_update_bss(vap, ni));
2115 }
2116
2117 static int
2118 lkpi_ic_wme_update(struct ieee80211com *ic)
2119 {
2120         /* This needs queuing and go at the right moment. */
2121 #ifdef WITH_WME_UPDATE
2122         struct ieee80211vap *vap;
2123         struct lkpi_hw *lhw;
2124         struct ieee80211_hw *hw;
2125         struct lkpi_vif *lvif;
2126         struct ieee80211_vif *vif;
2127         struct chanAccParams chp;
2128         struct wmeParams wmeparr[WME_NUM_AC];
2129         struct ieee80211_tx_queue_params txqp;
2130         enum ieee80211_bss_changed changed;
2131         int error;
2132         uint16_t ac;
2133 #endif
2134
2135         IMPROVE();
2136         KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
2137             "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
2138
2139 #ifdef WITH_WME_UPDATE
2140         vap = TAILQ_FIRST(&ic->ic_vaps);
2141         if (vap == NULL)
2142                 return (0);
2143
2144         /* We should factor this out into per-vap (*wme_update). */
2145         lhw = ic->ic_softc;
2146         if (lhw->ops->conf_tx == NULL)
2147                 return (0);
2148
2149         /* XXX-BZ check amount of hw queues */
2150         hw = LHW_TO_HW(lhw);
2151         lvif = VAP_TO_LVIF(vap);
2152         vif = LVIF_TO_VIF(lvif);
2153
2154         ieee80211_wme_ic_getparams(ic, &chp);
2155         IEEE80211_LOCK(ic);
2156         for (ac = 0; ac < WME_NUM_AC; ac++)
2157                 wmeparr[ac] = chp.cap_wmeParams[ac];
2158         IEEE80211_UNLOCK(ic);
2159
2160         /* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
2161         LKPI_80211_LHW_LOCK(lhw);
2162         for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
2163                 struct wmeParams *wmep;
2164
2165                 /* XXX-BZ should keep this in lvif? */
2166                 wmep = &wmeparr[ac];
2167                 bzero(&txqp, sizeof(txqp));
2168                 txqp.cw_min = wmep->wmep_logcwmin;
2169                 txqp.cw_max = wmep->wmep_logcwmax;
2170                 txqp.txop = wmep->wmep_txopLimit;
2171                 txqp.aifs = wmep->wmep_aifsn;
2172                 error = lkpi_80211_mo_conf_tx(hw, vif, ac, &txqp);
2173                 if (error != 0)
2174                         printf("%s: conf_tx ac %u failed %d\n",
2175                             __func__, ac, error);
2176         }
2177         LKPI_80211_LHW_UNLOCK(lhw);
2178         changed = BSS_CHANGED_QOS;
2179         lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2180 #endif
2181
2182         return (0);
2183 }
2184
2185 static struct ieee80211vap *
2186 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
2187     int unit, enum ieee80211_opmode opmode, int flags,
2188     const uint8_t bssid[IEEE80211_ADDR_LEN],
2189     const uint8_t mac[IEEE80211_ADDR_LEN])
2190 {
2191         struct lkpi_hw *lhw;
2192         struct ieee80211_hw *hw;
2193         struct lkpi_vif *lvif;
2194         struct ieee80211vap *vap;
2195         struct ieee80211_vif *vif;
2196         enum ieee80211_bss_changed changed;
2197         size_t len;
2198         int error;
2199
2200         if (!TAILQ_EMPTY(&ic->ic_vaps)) /* 1 so far. Add <n> once this works. */
2201                 return (NULL);
2202
2203         lhw = ic->ic_softc;
2204         hw = LHW_TO_HW(lhw);
2205
2206         len = sizeof(*lvif);
2207         len += hw->vif_data_size;       /* vif->drv_priv */
2208
2209         lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
2210         mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
2211         TAILQ_INIT(&lvif->lsta_head);
2212         vap = LVIF_TO_VAP(lvif);
2213
2214         vif = LVIF_TO_VIF(lvif);
2215         memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
2216         vif->p2p = false;
2217         vif->probe_req_reg = false;
2218         vif->type = lkpi_opmode_to_vif_type(opmode);
2219         lvif->wdev.iftype = vif->type;
2220         /* Need to fill in other fields as well. */
2221         IMPROVE();
2222
2223         /* XXX-BZ hardcoded for now! */
2224 #if 1
2225         vif->chanctx_conf = NULL;
2226         vif->bss_conf.idle = true;
2227         vif->bss_conf.ps = false;
2228         vif->bss_conf.chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
2229         vif->bss_conf.use_short_preamble = false;       /* vap->iv_flags IEEE80211_F_SHPREAMBLE */
2230         vif->bss_conf.use_short_slot = false;           /* vap->iv_flags IEEE80211_F_SHSLOT */
2231         vif->bss_conf.qos = false;
2232         vif->bss_conf.use_cts_prot = false;             /* vap->iv_protmode */
2233         vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
2234         vif->bss_conf.assoc = false;
2235         vif->bss_conf.aid = 0;
2236 #endif
2237 #if 0
2238         vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
2239         IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
2240         vif->bss_conf.beacon_int = ic->ic_bintval;
2241         /* iwlwifi bug. */
2242         if (vif->bss_conf.beacon_int < 16)
2243                 vif->bss_conf.beacon_int = 16;
2244 #endif
2245         IMPROVE();
2246
2247         error = lkpi_80211_mo_start(hw);
2248         if (error != 0) {
2249                 printf("%s: failed to start hw: %d\n", __func__, error);
2250                 mtx_destroy(&lvif->mtx);
2251                 free(lvif, M_80211_VAP);
2252                 return (NULL);
2253         }
2254
2255         error = lkpi_80211_mo_add_interface(hw, vif);
2256         if (error != 0) {
2257                 IMPROVE();      /* XXX-BZ mo_stop()? */
2258                 printf("%s: failed to add interface: %d\n", __func__, error);
2259                 mtx_destroy(&lvif->mtx);
2260                 free(lvif, M_80211_VAP);
2261                 return (NULL);
2262         }
2263
2264         LKPI_80211_LHW_LOCK(lhw);
2265         TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
2266         LKPI_80211_LHW_UNLOCK(lhw);
2267
2268         /* Set bss_info. */
2269         changed = 0;
2270         lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, changed);
2271
2272         /* conf_tx setup; default WME? */
2273
2274         /* Force MC init. */
2275         lkpi_update_mcast_filter(ic, true);
2276
2277         IMPROVE();
2278
2279         ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
2280
2281         /* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
2282         lvif->iv_newstate = vap->iv_newstate;
2283         vap->iv_newstate = lkpi_iv_newstate;
2284         lvif->iv_update_bss = vap->iv_update_bss;
2285         vap->iv_update_bss = lkpi_iv_update_bss;
2286
2287         /* Key management. */
2288         if (lhw->ops->set_key != NULL) {
2289 #ifdef TRY_HW_CRYPTO
2290                 vap->iv_key_set = lkpi_iv_key_set;
2291                 vap->iv_key_delete = lkpi_iv_key_delete;
2292 #endif
2293         }
2294
2295         ieee80211_ratectl_init(vap);
2296
2297         /* Complete setup. */
2298         ieee80211_vap_attach(vap, ieee80211_media_change,
2299             ieee80211_media_status, mac);
2300
2301         if (hw->max_listen_interval == 0)
2302                 hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
2303         hw->conf.listen_interval = hw->max_listen_interval;
2304         ic->ic_set_channel(ic);
2305
2306         /* XXX-BZ do we need to be able to update these? */
2307         hw->wiphy->frag_threshold =  vap->iv_fragthreshold;
2308         lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
2309         hw->wiphy->rts_threshold =  vap->iv_rtsthreshold;
2310         lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
2311         /* any others? */
2312         IMPROVE();
2313
2314         return (vap);
2315 }
2316
2317 static void
2318 lkpi_ic_vap_delete(struct ieee80211vap *vap)
2319 {
2320         struct ieee80211com *ic;
2321         struct lkpi_hw *lhw;
2322         struct ieee80211_hw *hw;
2323         struct lkpi_vif *lvif;
2324         struct ieee80211_vif *vif;
2325
2326         lvif = VAP_TO_LVIF(vap);
2327         vif = LVIF_TO_VIF(lvif);
2328         ic = vap->iv_ic;
2329         lhw = ic->ic_softc;
2330         hw = LHW_TO_HW(lhw);
2331
2332         LKPI_80211_LHW_LOCK(lhw);
2333         TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
2334         LKPI_80211_LHW_UNLOCK(lhw);
2335         lkpi_80211_mo_remove_interface(hw, vif);
2336
2337         ieee80211_ratectl_deinit(vap);
2338         ieee80211_vap_detach(vap);
2339         mtx_destroy(&lvif->mtx);
2340         free(lvif, M_80211_VAP);
2341 }
2342
2343 static void
2344 lkpi_ic_update_mcast(struct ieee80211com *ic)
2345 {
2346
2347         lkpi_update_mcast_filter(ic, false);
2348         TRACEOK();
2349 }
2350
2351 static void
2352 lkpi_ic_update_promisc(struct ieee80211com *ic)
2353 {
2354
2355         UNIMPLEMENTED;
2356 }
2357
2358 static void
2359 lkpi_ic_update_chw(struct ieee80211com *ic)
2360 {
2361
2362         UNIMPLEMENTED;
2363 }
2364
2365 /* Start / stop device. */
2366 static void
2367 lkpi_ic_parent(struct ieee80211com *ic)
2368 {
2369         struct lkpi_hw *lhw;
2370         struct ieee80211_hw *hw;
2371         int error;
2372         bool start_all;
2373
2374         IMPROVE();
2375
2376         lhw = ic->ic_softc;
2377         hw = LHW_TO_HW(lhw);
2378         start_all = false;
2379
2380         if (ic->ic_nrunning > 0) {
2381                 error = lkpi_80211_mo_start(hw);
2382                 if (error == 0)
2383                         start_all = true;
2384         } else {
2385                 lkpi_80211_mo_stop(hw);
2386         }
2387
2388         if (start_all)
2389                 ieee80211_start_all(ic);
2390 }
2391
2392 bool
2393 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
2394     size_t ie_ids_len)
2395 {
2396         int i;
2397
2398         for (i = 0; i < ie_ids_len; i++) {
2399                 if (ie == *ie_ids)
2400                         return (true);
2401         }
2402
2403         return (false);
2404 }
2405
2406 /* Return true if skipped; false if error. */
2407 bool
2408 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
2409 {
2410         size_t x;
2411         uint8_t l;
2412
2413         x = *xp;
2414
2415         KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
2416             __func__, x, ies_len, ies));
2417         l = ies[x + 1];
2418         x += 2 + l;
2419
2420         if (x > ies_len)
2421                 return (false);
2422
2423         *xp = x;
2424         return (true);
2425 }
2426
2427 static int
2428 lkpi_ieee80211_probereq_ie_alloc(struct ieee80211vap *vap,
2429     struct ieee80211com *ic, struct ieee80211_scan_ies *scan_ies,
2430     const uint8_t *ssid, size_t ssidlen)
2431 {
2432
2433         return (ieee80211_probereq_ie(vap, ic,
2434             &scan_ies->common_ies, &scan_ies->common_ie_len,
2435             ssid, ssidlen, true));
2436 }
2437
2438 static void
2439 lkpi_ic_scan_start(struct ieee80211com *ic)
2440 {
2441         struct lkpi_hw *lhw;
2442         struct ieee80211_hw *hw;
2443         struct lkpi_vif *lvif;
2444         struct ieee80211_vif *vif;
2445         struct ieee80211_scan_state *ss;
2446         struct ieee80211vap *vap;
2447         int error;
2448
2449         lhw = ic->ic_softc;
2450         if ((lhw->scan_flags & LKPI_SCAN_RUNNING) != 0) {
2451                 /* A scan is still running. */
2452                 return;
2453         }
2454
2455         ss = ic->ic_scan;
2456         vap = ss->ss_vap;
2457         if (vap->iv_state != IEEE80211_S_SCAN) {
2458                 IMPROVE("We need to be able to scan if not in S_SCAN");
2459                 return;
2460         }
2461
2462         hw = LHW_TO_HW(lhw);
2463         if ((vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) == 0) {
2464 sw_scan:
2465                 lvif = VAP_TO_LVIF(vap);
2466                 vif = LVIF_TO_VIF(lvif);
2467
2468                 if (vap->iv_state == IEEE80211_S_SCAN)
2469                         lkpi_hw_conf_idle(hw, false);
2470
2471                 lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
2472                 /* net80211::scan_start() handled PS for us. */
2473                 IMPROVE();
2474                 /* XXX Also means it is too late to flush queues?
2475                  * need to check iv_sta_ps or overload? */
2476                 /* XXX want to adjust ss end time/ maxdwell? */
2477
2478         } else {
2479                 struct ieee80211_channel *c;
2480                 struct ieee80211_scan_request *hw_req;
2481                 struct linuxkpi_ieee80211_channel *lc, **cpp;
2482                 struct cfg80211_ssid *ssids;
2483                 struct cfg80211_scan_6ghz_params *s6gp;
2484                 size_t chan_len, nchan, ssids_len, s6ghzlen;
2485                 int i;
2486
2487                 ssids_len = ss->ss_nssid * sizeof(*ssids);;
2488                 s6ghzlen = 0 * (sizeof(*s6gp));                 /* XXX-BZ */
2489
2490                 nchan = 0;
2491                 for (i = ss->ss_next; i < ss->ss_last; i++)
2492                         nchan++;
2493                 chan_len = nchan * (sizeof(lc) + sizeof(*lc));
2494
2495                 KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
2496                     "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
2497                 lhw->hw_req = hw_req = malloc(sizeof(*hw_req) + ssids_len +
2498                     s6ghzlen + chan_len, M_LKPI80211, M_WAITOK | M_ZERO);
2499
2500                 error = lkpi_ieee80211_probereq_ie_alloc(vap, ic,
2501                     &hw_req->ies, NULL, -1);
2502                 if (error != 0)
2503                         ic_printf(ic, "ERROR: %s: probereq_ie returned %d\n",
2504                             __func__, error);
2505
2506                 hw_req->req.flags = 0;                  /* XXX ??? */
2507                 /* hw_req->req.wdev */
2508                 hw_req->req.wiphy = hw->wiphy;
2509                 hw_req->req.no_cck = false;             /* XXX */
2510 #if 0
2511                 /* This seems to pessimise default scanning behaviour. */
2512                 hw_req->req.duration_mandatory = TICKS_2_USEC(ss->ss_mindwell);
2513                 hw_req->req.duration = TICKS_2_USEC(ss->ss_maxdwell);
2514 #endif
2515 #ifdef __notyet__
2516                 hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
2517                 memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
2518                 memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
2519 #endif
2520 #if 0
2521                 hw_req->req.ie_len = ;
2522                 hw_req->req.ie = ;
2523 #endif
2524 #if 0
2525                 hw->wiphy->max_scan_ie_len
2526                 hw->wiphy->max_scan_ssids
2527 #endif
2528
2529                 hw_req->req.n_channels = nchan;
2530                 cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
2531                 lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
2532                 for (i = 0; i < nchan; i++) {
2533                         *(cpp + i) =
2534                             (struct linuxkpi_ieee80211_channel *)(lc + i);
2535                 }
2536                 for (i = 0; i < nchan; i++) {
2537                         c = ss->ss_chans[ss->ss_next + i];
2538
2539                         lc->hw_value = c->ic_ieee;
2540                         lc->center_freq = c->ic_freq;
2541                         /* lc->flags */
2542                         lc->band = lkpi_net80211_chan_to_nl80211_band(c);
2543                         lc->max_power = c->ic_maxpower;
2544                         /* lc-> ... */
2545                         lc++;
2546                 }
2547
2548                 hw_req->req.n_ssids = ss->ss_nssid;
2549                 if (hw_req->req.n_ssids > 0) {
2550                         ssids = (struct cfg80211_ssid *)lc;
2551                         hw_req->req.ssids = ssids;
2552                         for (i = 0; i < ss->ss_nssid; i++) {
2553                                 ssids->ssid_len = ss->ss_ssid[i].len;
2554                                 memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
2555                                     ss->ss_ssid[i].len);
2556                                 ssids++;
2557                         }
2558                         s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
2559                 } else {
2560                         s6gp = (struct cfg80211_scan_6ghz_params *)lc;
2561                 }
2562
2563                 /* 6GHz one day. */
2564                 hw_req->req.n_6ghz_params = 0;
2565                 hw_req->req.scan_6ghz_params = NULL;
2566                 hw_req->req.scan_6ghz = false;  /* Weird boolean; not what you think. */
2567                 /* s6gp->... */
2568
2569                 lvif = VAP_TO_LVIF(vap);
2570                 vif = LVIF_TO_VIF(lvif);
2571                 error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
2572                 if (error != 0) {
2573                         free(hw_req->ies.common_ies, M_80211_VAP);
2574                         free(hw_req, M_LKPI80211);
2575                         lhw->hw_req = NULL;
2576
2577                         ieee80211_cancel_scan(vap);
2578                         /*
2579                          * XXX-SIGH magic number.
2580                          * rtw88 has a magic "return 1" if offloading scan is
2581                          * not possible.  Fall back to sw scan in that case.
2582                          */
2583                         if (error == 1) {
2584                                 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
2585                                 ieee80211_start_scan(vap,
2586                                     IEEE80211_SCAN_ACTIVE |
2587                                     IEEE80211_SCAN_NOPICK |
2588                                     IEEE80211_SCAN_ONCE,
2589                                     IEEE80211_SCAN_FOREVER,
2590                                     ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
2591                                     ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
2592                                     vap->iv_des_nssid, vap->iv_des_ssid);
2593                                 goto sw_scan;
2594                         }
2595
2596                         ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
2597                             __func__, error);
2598                 }
2599         }
2600 }
2601
2602 static void
2603 lkpi_ic_scan_end(struct ieee80211com *ic)
2604 {
2605         struct lkpi_hw *lhw;
2606         struct ieee80211_scan_state *ss;
2607         struct ieee80211vap *vap;
2608
2609         lhw = ic->ic_softc;
2610         if ((lhw->scan_flags & LKPI_SCAN_RUNNING) == 0) {
2611                 return;
2612         }
2613
2614         ss = ic->ic_scan;
2615         vap = ss->ss_vap;
2616         if (vap->iv_flags_ext & IEEE80211_FEXT_SCAN_OFFLOAD) {
2617                 /* Nothing to do. */
2618         } else {
2619                 struct ieee80211_hw *hw;
2620                 struct lkpi_vif *lvif;
2621                 struct ieee80211_vif *vif;
2622
2623                 hw = LHW_TO_HW(lhw);
2624                 lvif = VAP_TO_LVIF(vap);
2625                 vif = LVIF_TO_VIF(lvif);
2626                 lkpi_80211_mo_sw_scan_complete(hw, vif);
2627
2628                 /* Send PS to stop buffering if n80211 does not for us? */
2629
2630                 if (vap->iv_state == IEEE80211_S_SCAN)
2631                         lkpi_hw_conf_idle(hw, true);
2632         }
2633 }
2634
2635 static void
2636 lkpi_ic_set_channel(struct ieee80211com *ic)
2637 {
2638         struct lkpi_hw *lhw;
2639         struct ieee80211_hw *hw;
2640         struct ieee80211_channel *c;
2641         struct linuxkpi_ieee80211_channel *chan;
2642         int error;
2643
2644         lhw = ic->ic_softc;
2645
2646         /* If we do not support (*config)() save us the work. */
2647         if (lhw->ops->config == NULL)
2648                 return;
2649
2650         c = ic->ic_curchan;
2651         if (c == NULL || c == IEEE80211_CHAN_ANYC) {
2652                 ic_printf(ic, "%s: c %p ops->config %p\n", __func__,
2653                     c, lhw->ops->config);
2654                 return;
2655         }
2656
2657         chan = lkpi_find_lkpi80211_chan(lhw, c);
2658         if (chan == NULL) {
2659                 ic_printf(ic, "%s: c %p chan %p\n", __func__,
2660                     c, chan);
2661                 return;
2662         }
2663
2664         /* XXX max power for scanning? */
2665         IMPROVE();
2666
2667         hw = LHW_TO_HW(lhw);
2668         cfg80211_chandef_create(&hw->conf.chandef, chan,
2669             NL80211_CHAN_NO_HT);
2670
2671         error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_CHANNEL);
2672         if (error != 0 && error != EOPNOTSUPP) {
2673                 ic_printf(ic, "ERROR: %s: config %#0x returned %d\n",
2674                     __func__, IEEE80211_CONF_CHANGE_CHANNEL, error);
2675                 /* XXX should we unroll to the previous chandef? */
2676                 IMPROVE();
2677         } else {
2678                 /* Update radiotap channels as well. */
2679                 lhw->rtap_tx.wt_chan_freq = htole16(c->ic_freq);
2680                 lhw->rtap_tx.wt_chan_flags = htole16(c->ic_flags);
2681                 lhw->rtap_rx.wr_chan_freq = htole16(c->ic_freq);
2682                 lhw->rtap_rx.wr_chan_flags = htole16(c->ic_flags);
2683         }
2684
2685         /* Currently PS is hard coded off! Not sure it belongs here. */
2686         IMPROVE();
2687         if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
2688             (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
2689                 hw->conf.flags &= ~IEEE80211_CONF_PS;
2690                 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
2691                 if (error != 0 && error != EOPNOTSUPP)
2692                         ic_printf(ic, "ERROR: %s: config %#0x returned "
2693                             "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
2694                             error);
2695         }
2696 }
2697
2698 static struct ieee80211_node *
2699 lkpi_ic_node_alloc(struct ieee80211vap *vap,
2700     const uint8_t mac[IEEE80211_ADDR_LEN])
2701 {
2702         struct ieee80211com *ic;
2703         struct lkpi_hw *lhw;
2704         struct ieee80211_node *ni;
2705         struct ieee80211_hw *hw;
2706         struct lkpi_sta *lsta;
2707
2708         ic = vap->iv_ic;
2709         lhw = ic->ic_softc;
2710
2711         /* We keep allocations de-coupled so we can deal with the two worlds. */
2712         if (lhw->ic_node_alloc == NULL)
2713                 return (NULL);
2714
2715         ni = lhw->ic_node_alloc(vap, mac);
2716         if (ni == NULL)
2717                 return (NULL);
2718
2719         hw = LHW_TO_HW(lhw);
2720         lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
2721         if (lsta == NULL) {
2722                 if (lhw->ic_node_free != NULL)
2723                         lhw->ic_node_free(ni);
2724                 return (NULL);
2725         }
2726
2727         return (ni);
2728 }
2729
2730 static int
2731 lkpi_ic_node_init(struct ieee80211_node *ni)
2732 {
2733         struct ieee80211com *ic;
2734         struct lkpi_hw *lhw;
2735         struct lkpi_sta *lsta;
2736         struct lkpi_vif *lvif;
2737         int error;
2738
2739         ic = ni->ni_ic;
2740         lhw = ic->ic_softc;
2741
2742         if (lhw->ic_node_init != NULL) {
2743                 error = lhw->ic_node_init(ni);
2744                 if (error != 0)
2745                         return (error);
2746         }
2747
2748         lvif = VAP_TO_LVIF(ni->ni_vap);
2749
2750         lsta = ni->ni_drv_data;
2751
2752         /* Now take the reference before linking it to the table. */
2753         lsta->ni = ieee80211_ref_node(ni);
2754
2755         LKPI_80211_LVIF_LOCK(lvif);
2756         TAILQ_INSERT_TAIL(&lvif->lsta_head, lsta, lsta_entry);
2757         LKPI_80211_LVIF_UNLOCK(lvif);
2758
2759         /* XXX-BZ Sync other state over. */
2760         IMPROVE();
2761
2762         return (0);
2763 }
2764
2765 static void
2766 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
2767 {
2768         struct ieee80211com *ic;
2769         struct lkpi_hw *lhw;
2770
2771         ic = ni->ni_ic;
2772         lhw = ic->ic_softc;
2773
2774         /* XXX-BZ remove from driver, ... */
2775         IMPROVE();
2776
2777         if (lhw->ic_node_cleanup != NULL)
2778                 lhw->ic_node_cleanup(ni);
2779 }
2780
2781 static void
2782 lkpi_ic_node_free(struct ieee80211_node *ni)
2783 {
2784         struct ieee80211com *ic;
2785         struct lkpi_hw *lhw;
2786         struct lkpi_sta *lsta;
2787
2788         ic = ni->ni_ic;
2789         lhw = ic->ic_softc;
2790         lsta = ni->ni_drv_data;
2791         if (lsta == NULL)
2792                 goto out;
2793
2794         /* XXX-BZ free resources, ... */
2795         IMPROVE();
2796
2797         /* Flush mbufq (make sure to release ni refs!). */
2798 #ifdef __notyet__
2799         KASSERT(mbufq_len(&lsta->txq) == 0, ("%s: lsta %p has txq len %d != 0\n",
2800             __func__, lsta, mbufq_len(&lsta->txq)));
2801 #endif
2802         /* Drain taskq. */
2803
2804         /* Drain sta->txq[] */
2805         mtx_destroy(&lsta->txq_mtx);
2806
2807         /* Remove lsta if added_to_drv. */
2808         /* Remove lsta from vif */
2809
2810         /* remove ref from lsta node... */
2811
2812         /* Free lsta. */
2813
2814 out:
2815         if (lhw->ic_node_free != NULL)
2816                 lhw->ic_node_free(ni);
2817 }
2818
2819 static int
2820 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
2821         const struct ieee80211_bpf_params *params __unused)
2822 {
2823         struct lkpi_sta *lsta;
2824
2825         lsta = ni->ni_drv_data;
2826
2827         /* Queue the packet and enqueue the task to handle it. */
2828         LKPI_80211_LSTA_LOCK(lsta);
2829         mbufq_enqueue(&lsta->txq, m);
2830         LKPI_80211_LSTA_UNLOCK(lsta);
2831
2832 #ifdef LINUXKPI_DEBUG_80211
2833         if (linuxkpi_debug_80211 & D80211_TRACE_TX)
2834                 printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
2835                     __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
2836                     mbufq_len(&lsta->txq));
2837 #endif
2838
2839         taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
2840         return (0);
2841 }
2842
2843 static void
2844 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
2845 {
2846         struct ieee80211_node *ni;
2847         struct ieee80211_frame *wh;
2848         struct ieee80211_key *k;
2849         struct sk_buff *skb;
2850         struct ieee80211com *ic;
2851         struct lkpi_hw *lhw;
2852         struct ieee80211_hw *hw;
2853         struct lkpi_vif *lvif;
2854         struct ieee80211_vif *vif;
2855         struct ieee80211_channel *c;
2856         struct ieee80211_tx_control control;
2857         struct ieee80211_tx_info *info;
2858         struct ieee80211_sta *sta;
2859         void *buf;
2860         int ac;
2861
2862         M_ASSERTPKTHDR(m);
2863 #ifdef LINUXKPI_DEBUG_80211
2864         if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
2865                 hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
2866 #endif
2867
2868         ni = lsta->ni;
2869 #ifndef TRY_HW_CRYPTO
2870         /* Encrypt the frame if need be; XXX-BZ info->control.hw_key. */
2871         wh = mtod(m, struct ieee80211_frame *);
2872         if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
2873                 /* Retrieve key for TX && do software encryption. */
2874                 k = ieee80211_crypto_encap(ni, m);
2875                 if (k == NULL) {
2876                         ieee80211_free_node(ni);
2877                         m_freem(m);
2878                         return;
2879                 }
2880         }
2881 #endif
2882
2883         ic = ni->ni_ic;
2884         lhw = ic->ic_softc;
2885         hw = LHW_TO_HW(lhw);
2886         c = ni->ni_chan;
2887
2888         if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
2889                 struct lkpi_radiotap_tx_hdr *rtap;
2890
2891                 rtap = &lhw->rtap_tx;
2892                 rtap->wt_flags = 0;
2893                 if (k != NULL)
2894                         rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
2895                 if (m->m_flags & M_FRAG)
2896                         rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
2897                 IMPROVE();
2898                 rtap->wt_rate = 0;
2899                 if (c != NULL && c != IEEE80211_CHAN_ANYC) {
2900                         rtap->wt_chan_freq = htole16(c->ic_freq);
2901                         rtap->wt_chan_flags = htole16(c->ic_flags);
2902                 }
2903
2904                 ieee80211_radiotap_tx(ni->ni_vap, m);
2905         }
2906
2907         /*
2908          * net80211 should handle hw->extra_tx_headroom.
2909          * Though for as long as we are copying we don't mind.
2910          * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
2911          * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
2912          */
2913         skb = dev_alloc_skb(hw->extra_tx_headroom + m->m_pkthdr.len);
2914         if (skb == NULL) {
2915                 printf("XXX ERROR %s: skb alloc failed\n", __func__);
2916                 ieee80211_free_node(ni);
2917                 m_freem(m);
2918                 return;
2919         }
2920         skb_reserve(skb, hw->extra_tx_headroom);
2921
2922         /* XXX-BZ we need a SKB version understanding mbuf. */
2923         /* Save the mbuf for ieee80211_tx_complete(). */
2924         skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
2925         skb->m = m;
2926 #if 0
2927         skb_put_data(skb, m->m_data, m->m_pkthdr.len);
2928 #else
2929         buf = skb_put(skb, m->m_pkthdr.len);
2930         m_copydata(m, 0, m->m_pkthdr.len, buf);
2931 #endif
2932         /* Save the ni. */
2933         m->m_pkthdr.PH_loc.ptr = ni;
2934
2935         lvif = VAP_TO_LVIF(ni->ni_vap);
2936         vif = LVIF_TO_VIF(lvif);
2937
2938         /* XXX-BZ review this at some point [4] vs. [8] vs. [16](TID). */
2939         ac = M_WME_GETAC(m);
2940         skb->priority = WME_AC_TO_TID(ac);
2941         ac = lkpi_ac_net_to_l80211(ac);
2942         skb_set_queue_mapping(skb, ac);
2943
2944         info = IEEE80211_SKB_CB(skb);
2945         info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
2946         /* Slight delay; probably only happens on scanning so fine? */
2947         if (c == NULL || c == IEEE80211_CHAN_ANYC)
2948                 c = ic->ic_curchan;
2949         info->band = lkpi_net80211_chan_to_nl80211_band(c);
2950         info->hw_queue = ac;            /* XXX-BZ is this correct? */
2951         if (m->m_flags & M_EAPOL)
2952                 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
2953         info->control.vif = vif;
2954         /* XXX-BZ info->control.rates */
2955
2956         lsta = lkpi_find_lsta_by_ni(lvif, ni);
2957         if (lsta != NULL) {
2958                 sta = LSTA_TO_STA(lsta);
2959 #ifdef TRY_HW_CRYPTO
2960                 info->control.hw_key = lsta->kc;
2961 #endif
2962         } else {
2963                 sta = NULL;
2964         }
2965
2966         IMPROVE();
2967
2968         if (sta != NULL) {
2969                 struct lkpi_txq *ltxq;
2970
2971                 ltxq = TXQ_TO_LTXQ(sta->txq[ac]);       /* XXX-BZ re-check */
2972                 /*
2973                  * We currently do not use queues but do direct TX.
2974                  * The exception to the rule is initial packets, as we cannot
2975                  * TX until queues are allocated (at least for iwlwifi).
2976                  * So we wake_tx_queue in newstate and register any dequeue
2977                  * calls.  In the time until then we queue packets and
2978                  * let the driver deal with them.
2979                  */
2980                 if (!ltxq->seen_dequeue) {
2981
2982                         /* Prevent an ordering problem, likely other issues. */
2983                         while (!skb_queue_empty(&ltxq->skbq)) {
2984                                 struct sk_buff *skb2;
2985
2986                                 skb2 = skb_dequeue(&ltxq->skbq);
2987                                 if (skb2 != NULL) {
2988                                         memset(&control, 0, sizeof(control));
2989                                         control.sta = sta;
2990                                         lkpi_80211_mo_tx(hw, &control, skb2);
2991                                 }
2992                         }
2993                         goto ops_tx;
2994                 }
2995                 if (0 && ltxq->seen_dequeue && skb_queue_empty(&ltxq->skbq))
2996                         goto ops_tx;
2997
2998                 skb_queue_tail(&ltxq->skbq, skb);
2999 #ifdef LINUXKPI_DEBUG_80211
3000                 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3001                         printf("%s:%d lsta %p sta %p ni %p %6D skb %p lxtq %p "
3002                             "qlen %u WAKE_TX_Q ac %d prio %u qmap %u\n",
3003                             __func__, __LINE__, lsta, sta, ni,
3004                             ni->ni_macaddr, ":", skb, ltxq,
3005                             skb_queue_len(&ltxq->skbq), ac,
3006                             skb->priority, skb->qmap);
3007 #endif
3008                 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[ac]);  /* XXX-BZ */
3009                 return;
3010         }
3011
3012 ops_tx:
3013 #ifdef LINUXKPI_DEBUG_80211
3014         if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3015                 printf("%s:%d lsta %p sta %p ni %p %6D skb %p TX ac %d prio %u qmap %u\n",
3016                     __func__, __LINE__, lsta, sta, ni, ni->ni_macaddr, ":",
3017                     skb, ac, skb->priority, skb->qmap);
3018 #endif
3019         memset(&control, 0, sizeof(control));
3020         control.sta = sta;
3021
3022         lkpi_80211_mo_tx(hw, &control, skb);
3023         return;
3024 }
3025
3026 static void
3027 lkpi_80211_txq_task(void *ctx, int pending)
3028 {
3029         struct lkpi_sta *lsta;
3030         struct mbufq mq;
3031         struct mbuf *m;
3032
3033         lsta = ctx;
3034
3035 #ifdef LINUXKPI_DEBUG_80211
3036         if (linuxkpi_debug_80211 & D80211_TRACE_TX)
3037                 printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
3038                     __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
3039                     pending, mbufq_len(&lsta->txq));
3040 #endif
3041
3042         mbufq_init(&mq, IFQ_MAXLEN);
3043
3044         LKPI_80211_LSTA_LOCK(lsta);
3045         mbufq_concat(&mq, &lsta->txq);
3046         LKPI_80211_LSTA_UNLOCK(lsta);
3047
3048         m = mbufq_dequeue(&mq);
3049         while (m != NULL) {
3050                 lkpi_80211_txq_tx_one(lsta, m);
3051                 m = mbufq_dequeue(&mq);
3052         }
3053 }
3054
3055 static int
3056 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
3057 {
3058
3059         /* XXX TODO */
3060         IMPROVE();
3061
3062         /* Quick and dirty cheating hack. */
3063         struct ieee80211_node *ni;
3064
3065         ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
3066         return (lkpi_ic_raw_xmit(ni, m, NULL));
3067 }
3068
3069 static void
3070 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
3071     int *n, struct ieee80211_channel *c)
3072 {
3073         struct lkpi_hw *lhw;
3074         struct ieee80211_hw *hw;
3075         struct linuxkpi_ieee80211_channel *channels;
3076         uint8_t bands[IEEE80211_MODE_BYTES];
3077         int chan_flags, error, i, nchans;
3078
3079         /* Channels */
3080         lhw = ic->ic_softc;
3081         hw = LHW_TO_HW(lhw);
3082
3083         /* NL80211_BAND_2GHZ */
3084         nchans = 0;
3085         if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
3086                 nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
3087         if (nchans > 0) {
3088                 memset(bands, 0, sizeof(bands));
3089                 chan_flags = 0;
3090                 setbit(bands, IEEE80211_MODE_11B);
3091                 /* XXX-BZ unclear how to check for 11g. */
3092                 setbit(bands, IEEE80211_MODE_11G);
3093 #ifdef __notyet__
3094                 if (hw->wiphy->bands[NL80211_BAND_2GHZ]->ht_cap.ht_supported) {
3095                         setbit(bands, IEEE80211_MODE_11NG);
3096                         chan_flags |= NET80211_CBW_FLAG_HT40;
3097                 }
3098 #endif
3099
3100                 channels = hw->wiphy->bands[NL80211_BAND_2GHZ]->channels;
3101                 for (i = 0; i < nchans && *n < maxchan; i++) {
3102                         uint32_t nflags = 0;
3103                         int cflags = chan_flags;
3104
3105                         if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3106                                 printf("%s: %s: Skipping disabled chan "
3107                                     "[%u/%u/%#x]\n", ic->ic_name, __func__,
3108                                     channels[i].hw_value,
3109                                     channels[i].center_freq, channels[i].flags);
3110                                 continue;
3111                         }
3112                         if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3113                                 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3114                         if (channels[i].flags & IEEE80211_CHAN_RADAR)
3115                                 nflags |= IEEE80211_CHAN_DFS;
3116                         if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3117                                 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3118                         if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3119                                 cflags &= ~NET80211_CBW_FLAG_VHT80;
3120                         /* XXX how to map the remaining enum ieee80211_channel_flags? */
3121                         if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3122                                 cflags &= ~NET80211_CBW_FLAG_HT40;
3123
3124                         error = ieee80211_add_channel_cbw(c, maxchan, n,
3125                             channels[i].hw_value, channels[i].center_freq,
3126                             channels[i].max_power,
3127                             nflags, bands, chan_flags);
3128                         /* net80211::ENOBUFS: *n >= maxchans */
3129                         if (error != 0 && error != ENOBUFS)
3130                                 printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3131                                     "returned error %d\n", ic->ic_name,
3132                                     __func__, channels[i].hw_value,
3133                                     channels[i].center_freq, channels[i].flags,
3134                                     nflags, chan_flags, cflags, error);
3135                         if (error != 0)
3136                                 break;
3137                 }
3138         }
3139
3140         /* NL80211_BAND_5GHZ */
3141         nchans = 0;
3142         if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
3143                 nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
3144         if (nchans > 0) {
3145                 memset(bands, 0, sizeof(bands));
3146                 chan_flags = 0;
3147                 setbit(bands, IEEE80211_MODE_11A);
3148 #ifdef __not_yet__
3149                 if (hw->wiphy->bands[NL80211_BAND_5GHZ]->ht_cap.ht_supported) {
3150                         setbit(bands, IEEE80211_MODE_11NA);
3151                         chan_flags |= NET80211_CBW_FLAG_HT40;
3152                 }
3153                 if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported){
3154
3155                         ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
3156                         ic->ic_vhtcaps =
3157                             hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
3158
3159                         setbit(bands, IEEE80211_MODE_VHT_5GHZ);
3160                         chan_flags |= NET80211_CBW_FLAG_VHT80;
3161                         if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
3162                             ic->ic_vhtcaps))
3163                                 chan_flags |= NET80211_CBW_FLAG_VHT160;
3164                         if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
3165                             ic->ic_vhtcaps))
3166                                 chan_flags |= NET80211_CBW_FLAG_VHT80P80;
3167                 }
3168 #endif
3169
3170                 channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
3171                 for (i = 0; i < nchans && *n < maxchan; i++) {
3172                         uint32_t nflags = 0;
3173                         int cflags = chan_flags;
3174
3175                         if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
3176                                 printf("%s: %s: Skipping disabled chan "
3177                                     "[%u/%u/%#x]\n", ic->ic_name, __func__,
3178                                     channels[i].hw_value,
3179                                     channels[i].center_freq, channels[i].flags);
3180                                 continue;
3181                         }
3182                         if (channels[i].flags & IEEE80211_CHAN_NO_IR)
3183                                 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
3184                         if (channels[i].flags & IEEE80211_CHAN_RADAR)
3185                                 nflags |= IEEE80211_CHAN_DFS;
3186                         if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
3187                                 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
3188                         if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
3189                                 cflags &= ~NET80211_CBW_FLAG_VHT80;
3190                         /* XXX hwo to map the remaining enum ieee80211_channel_flags? */
3191                         if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
3192                                 cflags &= ~NET80211_CBW_FLAG_HT40;
3193
3194                         error = ieee80211_add_channel_cbw(c, maxchan, n,
3195                             channels[i].hw_value, channels[i].center_freq,
3196                             channels[i].max_power,
3197                             nflags, bands, chan_flags);
3198                         /* net80211::ENOBUFS: *n >= maxchans */
3199                         if (error != 0 && error != ENOBUFS)
3200                                 printf("%s: %s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
3201                                     "returned error %d\n", ic->ic_name,
3202                                     __func__, channels[i].hw_value,
3203                                     channels[i].center_freq, channels[i].flags,
3204                                     nflags, chan_flags, cflags, error);
3205                         if (error != 0)
3206                                 break;
3207                 }
3208         }
3209 }
3210
3211 static void *
3212 lkpi_ieee80211_ifalloc(void)
3213 {
3214         struct ieee80211com *ic;
3215
3216         ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
3217         if (ic == NULL)
3218                 return (NULL);
3219
3220         /* Setting these happens later when we have device information. */
3221         ic->ic_softc = NULL;
3222         ic->ic_name = "linuxkpi";
3223
3224         return (ic);
3225 }
3226
3227 struct ieee80211_hw *
3228 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
3229 {
3230         struct ieee80211_hw *hw;
3231         struct lkpi_hw *lhw;
3232         struct wiphy *wiphy;
3233
3234         /* Get us and the driver data also allocated. */
3235         wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
3236         if (wiphy == NULL)
3237                 return (NULL);
3238
3239         lhw = wiphy_priv(wiphy);
3240         lhw->ops = ops;
3241
3242         mtx_init(&lhw->mtx, "lhw", NULL, MTX_DEF | MTX_RECURSE);
3243         TAILQ_INIT(&lhw->lvif_head);
3244
3245         /*
3246          * XXX-BZ TODO make sure there is a "_null" function to all ops
3247          * not initialized.
3248          */
3249         hw = LHW_TO_HW(lhw);
3250         hw->wiphy = wiphy;
3251         hw->conf.flags |= IEEE80211_CONF_IDLE;
3252         hw->priv = (void *)(lhw + 1);
3253
3254         /* BSD Specific. */
3255         lhw->ic = lkpi_ieee80211_ifalloc();
3256         if (lhw->ic == NULL) {
3257                 ieee80211_free_hw(hw);
3258                 return (NULL);
3259         }
3260
3261         IMPROVE();
3262
3263         return (hw);
3264 }
3265
3266 void
3267 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
3268 {
3269         struct lkpi_hw *lhw;
3270
3271         lhw = HW_TO_LHW(hw);
3272         free(lhw->ic, M_LKPI80211);
3273         lhw->ic = NULL;
3274
3275         /* Cleanup more of lhw here or in wiphy_free()? */
3276         mtx_destroy(&lhw->mtx);
3277         IMPROVE();
3278 }
3279
3280 void
3281 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw, char *name)
3282 {
3283         struct lkpi_hw *lhw;
3284         struct ieee80211com *ic;
3285
3286         lhw = HW_TO_LHW(hw);
3287         ic = lhw->ic;
3288
3289         /* Now set a proper name before ieee80211_ifattach(). */
3290         ic->ic_softc = lhw;
3291         ic->ic_name = name;
3292
3293         /* XXX-BZ do we also need to set wiphy name? */
3294 }
3295
3296 struct ieee80211_hw *
3297 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
3298 {
3299         struct lkpi_hw *lhw;
3300
3301         lhw = wiphy_priv(wiphy);
3302         return (LHW_TO_HW(lhw));
3303 }
3304
3305 static void
3306 lkpi_radiotap_attach(struct lkpi_hw *lhw)
3307 {
3308         struct ieee80211com *ic;
3309
3310         ic = lhw->ic;
3311         ieee80211_radiotap_attach(ic,
3312             &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
3313             LKPI_RTAP_TX_FLAGS_PRESENT,
3314             &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
3315             LKPI_RTAP_RX_FLAGS_PRESENT);
3316 }
3317
3318 int
3319 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
3320 {
3321         struct ieee80211com *ic;
3322         struct lkpi_hw *lhw;
3323         int band, i;
3324
3325         lhw = HW_TO_LHW(hw);
3326         ic = lhw->ic;
3327
3328         /* We do it this late as wiphy->dev should be set for the name. */
3329         lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
3330         if (lhw->workq == NULL)
3331                 return (-EAGAIN);
3332
3333         /* XXX-BZ figure this out how they count his... */
3334         if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
3335                 IEEE80211_ADDR_COPY(ic->ic_macaddr,
3336                     hw->wiphy->perm_addr);
3337         } else if (hw->wiphy->n_addresses > 0) {
3338                 /* We take the first one. */
3339                 IEEE80211_ADDR_COPY(ic->ic_macaddr,
3340                     hw->wiphy->addresses[0].addr);
3341         } else {
3342                 ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
3343         }
3344
3345 #ifdef __not_yet__
3346         /* See comment in lkpi_80211_txq_tx_one(). */
3347         ic->ic_headroom = hw->extra_tx_headroom;
3348 #endif
3349
3350         ic->ic_phytype = IEEE80211_T_OFDM;      /* not only, but not used */
3351         ic->ic_opmode = IEEE80211_M_STA;
3352
3353         /* Set device capabilities. */
3354         /* XXX-BZ we need to get these from linux80211/drivers and convert. */
3355         ic->ic_caps =
3356             IEEE80211_C_STA |
3357             IEEE80211_C_MONITOR |
3358             IEEE80211_C_WPA |           /* WPA/RSN */
3359             IEEE80211_C_WME |
3360 #if 0
3361             IEEE80211_C_PMGT |
3362 #endif
3363             IEEE80211_C_SHSLOT |        /* short slot time supported */
3364             IEEE80211_C_SHPREAMBLE      /* short preamble supported */
3365             ;
3366 #if 0
3367         /* Scanning is a different kind of beast to re-work. */
3368         ic->ic_caps |= IEEE80211_C_BGSCAN;
3369 #endif
3370         if (lhw->ops->hw_scan) {
3371                 /*
3372                  * Advertise full-offload scanning.
3373                  *
3374                  * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
3375                  * we essentially disable hw_scan for all drivers not setting
3376                  * the flag.
3377                  */
3378                 ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
3379         }
3380
3381 #ifdef __notyet__
3382         ic->ic_htcaps = IEEE80211_HTC_HT        /* HT operation */
3383                     | IEEE80211_HTC_AMPDU       /* A-MPDU tx/rx */
3384                     | IEEE80211_HTC_AMSDU       /* A-MSDU tx/rx */
3385                     | IEEE80211_HTCAP_MAXAMSDU_3839
3386                                                 /* max A-MSDU length */
3387                     | IEEE80211_HTCAP_SMPS_OFF; /* SM power save off */
3388         ic->ic_htcaps |= IEEE80211_HTCAP_SHORTGI20;
3389         ic->ic_htcaps |= IEEE80211_HTCAP_CHWIDTH40 | IEEE80211_HTCAP_SHORTGI40;
3390         ic->ic_htcaps |= IEEE80211_HTCAP_TXSTBC;
3391 #endif
3392
3393         ic->ic_cryptocaps = 0;
3394 #ifdef TRY_HW_CRYPTO
3395         if (hw->wiphy->n_cipher_suites > 0) {
3396                 for (i = 0; i < hw->wiphy->n_cipher_suites; i++)
3397                         ic->ic_cryptocaps |= lkpi_l80211_to_net80211_cyphers(
3398                             hw->wiphy->cipher_suites[i]);
3399         }
3400 #endif
3401
3402         lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
3403             ic->ic_channels);
3404
3405         ieee80211_ifattach(ic);
3406
3407         ic->ic_update_mcast = lkpi_ic_update_mcast;
3408         ic->ic_update_promisc = lkpi_ic_update_promisc;
3409         ic->ic_update_chw = lkpi_ic_update_chw;
3410         ic->ic_parent = lkpi_ic_parent;
3411         ic->ic_scan_start = lkpi_ic_scan_start;
3412         ic->ic_scan_end = lkpi_ic_scan_end;
3413         ic->ic_set_channel = lkpi_ic_set_channel;
3414         ic->ic_transmit = lkpi_ic_transmit;
3415         ic->ic_raw_xmit = lkpi_ic_raw_xmit;
3416         ic->ic_vap_create = lkpi_ic_vap_create;
3417         ic->ic_vap_delete = lkpi_ic_vap_delete;
3418         ic->ic_getradiocaps = lkpi_ic_getradiocaps;
3419         ic->ic_wme.wme_update = lkpi_ic_wme_update;
3420
3421         lhw->ic_node_alloc = ic->ic_node_alloc;
3422         ic->ic_node_alloc = lkpi_ic_node_alloc;
3423         lhw->ic_node_init = ic->ic_node_init;
3424         ic->ic_node_init = lkpi_ic_node_init;
3425         lhw->ic_node_cleanup = ic->ic_node_cleanup;
3426         ic->ic_node_cleanup = lkpi_ic_node_cleanup;
3427         lhw->ic_node_free = ic->ic_node_free;
3428         ic->ic_node_free = lkpi_ic_node_free;
3429
3430         lkpi_radiotap_attach(lhw);
3431
3432         /*
3433          * Assign the first possible channel for now;  seems Realtek drivers
3434          * expect one.
3435          */
3436         for (band = 0; band < NUM_NL80211_BANDS &&
3437             hw->conf.chandef.chan == NULL; band++) {
3438                 struct ieee80211_supported_band *supband;
3439                 struct linuxkpi_ieee80211_channel *channels;
3440
3441                 supband = hw->wiphy->bands[band];
3442                 if (supband == NULL || supband->n_channels == 0)
3443                         continue;
3444
3445                 channels = supband->channels;
3446                 for (i = 0; i < supband->n_channels; i++) {
3447
3448                         if (channels[i].flags & IEEE80211_CHAN_DISABLED)
3449                                 continue;
3450
3451                         cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
3452                             NL80211_CHAN_NO_HT);
3453                         break;
3454                 }
3455         }
3456
3457         if (bootverbose)
3458                 ieee80211_announce(ic);
3459
3460         return (0);
3461 }
3462
3463 void
3464 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
3465 {
3466         struct lkpi_hw *lhw;
3467         struct ieee80211com *ic;
3468
3469         lhw = HW_TO_LHW(hw);
3470         ic = lhw->ic;
3471         ieee80211_ifdetach(ic);
3472 }
3473
3474 void
3475 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
3476     enum ieee80211_iface_iter flags,
3477     void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
3478     void *arg)
3479 {
3480         struct lkpi_hw *lhw;
3481         struct lkpi_vif *lvif;
3482         struct ieee80211_vif *vif;
3483         bool active, atomic, nin_drv;
3484
3485         lhw = HW_TO_LHW(hw);
3486
3487         if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
3488             IEEE80211_IFACE_ITER_RESUME_ALL|
3489             IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
3490             IEEE80211_IFACE_ITER__ACTIVE|IEEE80211_IFACE_ITER__ATOMIC)) {
3491                 ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
3492                     __func__, flags);
3493         }
3494
3495         active = (flags & IEEE80211_IFACE_ITER__ACTIVE) != 0;
3496         atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
3497         nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
3498
3499         if (atomic)
3500                 LKPI_80211_LHW_LOCK(lhw);
3501         TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3502                 struct ieee80211vap *vap;
3503
3504                 vif = LVIF_TO_VIF(lvif);
3505
3506                 /*
3507                  * If we want "active" interfaces, we need to distinguish on
3508                  * whether the driver knows about them or not to be able to
3509                  * handle the "resume" case correctly.  Skip the ones the
3510                  * driver does not know about.
3511                  */
3512                 if (active && !lvif->added_to_drv &&
3513                     (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
3514                         continue;
3515
3516                 /*
3517                  * If we shall skip interfaces not added to the driver do so
3518                  * if we haven't yet.
3519                  */
3520                 if (nin_drv && !lvif->added_to_drv)
3521                         continue;
3522
3523                 /*
3524                  * Run the iterator function if we are either not asking
3525                  * asking for active only or if the VAP is "running".
3526                  */
3527                 /* XXX-BZ probably should have state in the lvif as well. */
3528                 vap = LVIF_TO_VAP(lvif);
3529                 if (!active || (vap->iv_state != IEEE80211_S_INIT))
3530                         iterfunc(arg, vif->addr, vif);
3531         }
3532         if (atomic)
3533                 LKPI_80211_LHW_UNLOCK(lhw);
3534 }
3535
3536 void
3537 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
3538     struct ieee80211_vif *vif,
3539     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
3540         struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
3541     void *arg)
3542 {
3543
3544         UNIMPLEMENTED;
3545 }
3546
3547 void
3548 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
3549     void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
3550         void *),
3551     void *arg)
3552 {
3553
3554         UNIMPLEMENTED;
3555 }
3556
3557 void
3558 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
3559    void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
3560 {
3561         struct lkpi_hw *lhw;
3562         struct lkpi_vif *lvif;
3563         struct lkpi_sta *lsta;
3564         struct ieee80211_sta *sta;
3565
3566         KASSERT(hw != NULL && iterfunc != NULL,
3567             ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
3568
3569         lhw = HW_TO_LHW(hw);
3570
3571         LKPI_80211_LHW_LOCK(lhw);
3572         TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3573
3574                 LKPI_80211_LVIF_LOCK(lvif);
3575                 TAILQ_FOREACH(lsta, &lvif->lsta_head, lsta_entry) {
3576                         if (!lsta->added_to_drv)
3577                                 continue;
3578                         sta = LSTA_TO_STA(lsta);
3579                         iterfunc(arg, sta);
3580                 }
3581                 LKPI_80211_LVIF_UNLOCK(lvif);
3582         }
3583         LKPI_80211_LHW_UNLOCK(lhw);
3584 }
3585
3586 int
3587 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
3588     struct linuxkpi_ieee80211_regdomain *regd)
3589 {
3590         struct lkpi_hw *lhw;
3591         struct ieee80211com *ic;
3592         struct ieee80211_regdomain *rd;
3593
3594         lhw = wiphy_priv(wiphy);
3595         ic = lhw->ic;
3596
3597         rd = &ic->ic_regdomain;
3598         if (rd->isocc[0] == '\0') {
3599                 rd->isocc[0] = regd->alpha2[0];
3600                 rd->isocc[1] = regd->alpha2[1];
3601         }
3602
3603         TODO();
3604         /* XXX-BZ finish the rest. */
3605
3606         return (0);
3607 }
3608
3609 void
3610 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
3611     struct cfg80211_scan_info *info)
3612 {
3613         struct lkpi_hw *lhw;
3614         struct ieee80211com *ic;
3615         struct ieee80211_scan_state *ss;
3616
3617         lhw = wiphy_priv(hw->wiphy);
3618         ic = lhw->ic;
3619         ss = ic->ic_scan;
3620
3621         ieee80211_scan_done(ss->ss_vap);
3622
3623         LKPI_80211_LHW_LOCK(lhw);
3624         free(lhw->hw_req->ies.common_ies, M_80211_VAP);
3625         free(lhw->hw_req, M_LKPI80211);
3626         lhw->hw_req = NULL;
3627         lhw->scan_flags &= ~LKPI_SCAN_RUNNING;
3628         wakeup(lhw);
3629         LKPI_80211_LHW_UNLOCK(lhw);
3630
3631         return;
3632 }
3633
3634 void
3635 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
3636     struct ieee80211_sta *sta, struct napi_struct *napi __unused)
3637 {
3638         struct epoch_tracker et;
3639         struct lkpi_hw *lhw;
3640         struct ieee80211com *ic;
3641         struct mbuf *m;
3642         struct skb_shared_info *shinfo;
3643         struct ieee80211_rx_status *rx_status;
3644         struct ieee80211_rx_stats rx_stats;
3645         struct ieee80211_node *ni;
3646         struct ieee80211vap *vap;
3647         struct ieee80211_hdr *hdr;
3648         struct lkpi_sta *lsta;
3649         int i, offset, ok;
3650         bool is_beacon;
3651
3652         if (skb->len < 2) {
3653                 /* Need 80211 stats here. */
3654                 IMPROVE();
3655                 goto err;
3656         }
3657
3658         /*
3659          * For now do the data copy; we can later improve things. Might even
3660          * have an mbuf backing the skb data then?
3661          */
3662         m = m_get2(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
3663         if (m == NULL)
3664                 goto err;
3665         m_copyback(m, 0, skb->tail - skb->data, skb->data);
3666
3667         shinfo = skb_shinfo(skb);
3668         offset = m->m_len;
3669         for (i = 0; i < shinfo->nr_frags; i++) {
3670                 m_copyback(m, offset, shinfo->frags[i].size,
3671                     (uint8_t *)linux_page_address(shinfo->frags[i].page) +
3672                     shinfo->frags[i].offset);
3673                 offset += shinfo->frags[i].size;
3674         }
3675
3676         rx_status = IEEE80211_SKB_RXCB(skb);
3677
3678         hdr = (void *)skb->data;
3679         is_beacon = ieee80211_is_beacon(hdr->frame_control);
3680
3681 #ifdef LINUXKPI_DEBUG_80211
3682         if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0)
3683                 goto no_trace_beacons;
3684
3685         if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3686                 printf("TRACE-RX: %s: skb %p a/l/d/t-len (%u/%u/%u/%u) "
3687                     "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
3688                     __func__, skb, skb->_alloc_len, skb->len, skb->data_len,
3689                     skb->truesize, skb->head, skb->data, skb->tail, skb->end,
3690                     shinfo, shinfo->nr_frags,
3691                     m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
3692
3693         if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
3694                 hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
3695
3696         /* Implement a dump_rxcb() !!! */
3697         if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3698                 printf("TRACE %s: RXCB: %ju %ju %u, %#0x, %u, %#0x, %#0x, "
3699                     "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
3700                         __func__,
3701                         (uintmax_t)rx_status->boottime_ns,
3702                         (uintmax_t)rx_status->mactime,
3703                         rx_status->device_timestamp,
3704                         rx_status->flag,
3705                         rx_status->freq,
3706                         rx_status->bw,
3707                         rx_status->encoding,
3708                         rx_status->ampdu_reference,
3709                         rx_status->band,
3710                         rx_status->chains,
3711                         rx_status->chain_signal[0],
3712                         rx_status->chain_signal[1],
3713                         rx_status->chain_signal[2],
3714                         rx_status->chain_signal[3],
3715                         rx_status->signal,
3716                         rx_status->enc_flags,
3717                         rx_status->he_dcm,
3718                         rx_status->he_gi,
3719                         rx_status->he_ru,
3720                         rx_status->zero_length_psdu_type,
3721                         rx_status->nss,
3722                         rx_status->rate_idx);
3723 no_trace_beacons:
3724 #endif
3725
3726         memset(&rx_stats, 0, sizeof(rx_stats));
3727         rx_stats.r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
3728         /* XXX-BZ correct hardcoded rssi and noise floor, how? survey? */
3729         rx_stats.c_nf = -96;
3730         if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
3731             !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
3732                 rx_stats.c_rssi = rx_status->signal;
3733         else
3734                 rx_stats.c_rssi = 0;
3735         rx_stats.c_rssi -= rx_stats.c_nf;
3736         rx_stats.r_flags |= IEEE80211_R_BAND;
3737         rx_stats.c_band =
3738             lkpi_nl80211_band_to_net80211_band(rx_status->band);
3739         rx_stats.r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
3740         rx_stats.c_freq = rx_status->freq;
3741         rx_stats.c_ieee = ieee80211_mhz2ieee(rx_stats.c_freq, rx_stats.c_band);
3742
3743         /* XXX (*sta_statistics)() to get to some of that? */
3744         /* XXX-BZ dump the FreeBSD version of rx_stats as well! */
3745
3746         lhw = HW_TO_LHW(hw);
3747         ic = lhw->ic;
3748
3749         ok = ieee80211_add_rx_params(m, &rx_stats);
3750         if (ok == 0) {
3751                 counter_u64_add(ic->ic_ierrors, 1);
3752                 goto err;
3753         }
3754
3755         if (sta != NULL) {
3756                 lsta = STA_TO_LSTA(sta);
3757                 ni = ieee80211_ref_node(lsta->ni);
3758         } else {
3759                 struct ieee80211_frame_min *wh;
3760
3761                 wh = mtod(m, struct ieee80211_frame_min *);
3762                 ni = ieee80211_find_rxnode(ic, wh);
3763                 if (ni != NULL)
3764                         lsta = ni->ni_drv_data;
3765         }
3766
3767         if (ni != NULL)
3768                 vap = ni->ni_vap;
3769         else
3770                 /*
3771                  * XXX-BZ can we improve this by looking at the frame hdr
3772                  * or other meta-data passed up?
3773                  */
3774                 vap = TAILQ_FIRST(&ic->ic_vaps);
3775
3776 #ifdef LINUXKPI_DEBUG_80211
3777         if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3778                 printf("TRACE %s: sta %p lsta %p state %d ni %p vap %p%s\n",
3779                     __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
3780                     ni, vap, is_beacon ? " beacon" : "");
3781 #endif
3782
3783         if (ni != NULL && vap != NULL && is_beacon &&
3784             rx_status->device_timestamp > 0 &&
3785             m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
3786                 struct lkpi_vif *lvif;
3787                 struct ieee80211_vif *vif;
3788                 struct ieee80211_frame *wh;
3789
3790                 wh = mtod(m, struct ieee80211_frame *);
3791                 if (!IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_bssid))
3792                         goto skip_device_ts;
3793
3794                 lvif = VAP_TO_LVIF(vap);
3795                 vif = LVIF_TO_VIF(lvif);
3796
3797                 IMPROVE("TIMING_BEACON_ONLY?");
3798                 /* mac80211 specific (not net80211) so keep it here. */
3799                 vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
3800                 /*
3801                  * net80211 should take care of the other information (sync_tsf,
3802                  * sync_dtim_count) as otherwise we need to parse the beacon.
3803                  */
3804         }
3805 skip_device_ts:
3806
3807         if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
3808             ieee80211_radiotap_active_vap(vap)) {
3809                 struct lkpi_radiotap_rx_hdr *rtap;
3810
3811                 rtap = &lhw->rtap_rx;
3812                 rtap->wr_tsft = rx_status->device_timestamp;
3813                 rtap->wr_flags = 0;
3814                 if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
3815                         rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
3816                 if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
3817                         rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
3818 #if 0   /* .. or it does not given we strip it below. */
3819                 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
3820                         rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
3821 #endif
3822                 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
3823                         rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
3824                 rtap->wr_rate = 0;
3825                 IMPROVE();
3826                 /* XXX TODO status->encoding / rate_index / bw */
3827                 rtap->wr_chan_freq = htole16(rx_stats.c_freq);
3828                 if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
3829                         rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
3830                 rtap->wr_dbm_antsignal = rx_stats.c_rssi;
3831                 rtap->wr_dbm_antnoise = rx_stats.c_nf;
3832         }
3833
3834         if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
3835                 m_adj(m, -IEEE80211_CRC_LEN);
3836
3837         NET_EPOCH_ENTER(et);
3838         if (ni != NULL) {
3839                 ok = ieee80211_input_mimo(ni, m);
3840                 ieee80211_free_node(ni);
3841         } else {
3842                 ok = ieee80211_input_mimo_all(ic, m);
3843         }
3844         NET_EPOCH_EXIT(et);
3845
3846 #ifdef LINUXKPI_DEBUG_80211
3847         if (linuxkpi_debug_80211 & D80211_TRACE_RX)
3848                 printf("TRACE %s: handled frame type %#0x\n", __func__, ok);
3849 #endif
3850
3851         IMPROVE();
3852
3853 err:
3854         /* The skb is ours so we can free it :-) */
3855         kfree_skb(skb);
3856 }
3857
3858 uint8_t
3859 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr)
3860 {
3861         const struct ieee80211_frame *wh;
3862
3863         wh = (const struct ieee80211_frame *)hdr;
3864         return (ieee80211_gettid(wh));
3865 }
3866
3867 struct wiphy *
3868 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
3869 {
3870         struct lkpi_wiphy *lwiphy;
3871
3872         lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
3873         if (lwiphy == NULL)
3874                 return (NULL);
3875         lwiphy->ops = ops;
3876
3877         /* XXX TODO */
3878         return (LWIPHY_TO_WIPHY(lwiphy));
3879 }
3880
3881 void
3882 linuxkpi_wiphy_free(struct wiphy *wiphy)
3883 {
3884         struct lkpi_wiphy *lwiphy;
3885
3886         if (wiphy == NULL)
3887                 return;
3888
3889         lwiphy = WIPHY_TO_LWIPHY(wiphy);
3890         kfree(lwiphy);
3891 }
3892
3893 uint32_t
3894 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
3895     enum nl80211_band band)
3896 {
3897
3898         switch (band) {
3899         case NL80211_BAND_2GHZ:
3900                 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
3901                 break;
3902         case NL80211_BAND_5GHZ:
3903                 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
3904                 break;
3905         default:
3906                 /* XXX abort, retry, error, panic? */
3907                 break;
3908         }
3909
3910         return (0);
3911 }
3912
3913 uint32_t
3914 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
3915 {
3916
3917         return (ieee80211_mhz2ieee(freq, 0));
3918 }
3919
3920 static struct lkpi_sta *
3921 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
3922 {
3923         struct lkpi_sta *lsta, *temp;
3924
3925         LKPI_80211_LVIF_LOCK(lvif);
3926         TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
3927                 if (lsta->ni == ni) {
3928                         LKPI_80211_LVIF_UNLOCK(lvif);
3929                         return (lsta);
3930                 }
3931         }
3932         LKPI_80211_LVIF_UNLOCK(lvif);
3933
3934         return (NULL);
3935 }
3936
3937 struct ieee80211_sta *
3938 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
3939 {
3940         struct lkpi_vif *lvif;
3941         struct lkpi_sta *lsta, *temp;
3942         struct ieee80211_sta *sta;
3943
3944         lvif = VIF_TO_LVIF(vif);
3945
3946         LKPI_80211_LVIF_LOCK(lvif);
3947         TAILQ_FOREACH_SAFE(lsta, &lvif->lsta_head, lsta_entry, temp) {
3948                 sta = LSTA_TO_STA(lsta);
3949                 if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
3950                         LKPI_80211_LVIF_UNLOCK(lvif);
3951                         return (sta);
3952                 }
3953         }
3954         LKPI_80211_LVIF_UNLOCK(lvif);
3955         return (NULL);
3956 }
3957
3958 struct ieee80211_sta *
3959 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
3960     const uint8_t *addr, const uint8_t *ourvifaddr)
3961 {
3962         struct lkpi_hw *lhw;
3963         struct lkpi_vif *lvif;
3964         struct lkpi_sta *lsta;
3965         struct ieee80211_vif *vif;
3966         struct ieee80211_sta *sta;
3967
3968         lhw = wiphy_priv(hw->wiphy);
3969         sta = NULL;
3970
3971         LKPI_80211_LHW_LOCK(lhw);
3972         TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
3973
3974                 /* XXX-BZ check our address from the vif. */
3975
3976                 vif = LVIF_TO_VIF(lvif);
3977                 if (ourvifaddr != NULL &&
3978                     !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
3979                         continue;
3980                 sta = linuxkpi_ieee80211_find_sta(vif, addr);
3981                 if (sta != NULL)
3982                         break;
3983         }
3984         LKPI_80211_LHW_UNLOCK(lhw);
3985
3986         if (sta != NULL) {
3987                 lsta = STA_TO_LSTA(sta);
3988                 if (!lsta->added_to_drv)
3989                         return (NULL);
3990         }
3991
3992         return (sta);
3993 }
3994
3995 struct sk_buff *
3996 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
3997     struct ieee80211_txq *txq)
3998 {
3999         struct lkpi_txq *ltxq;
4000         struct sk_buff *skb;
4001
4002         ltxq = TXQ_TO_LTXQ(txq);
4003         ltxq->seen_dequeue = true;
4004
4005         skb = skb_dequeue(&ltxq->skbq);
4006
4007         return (skb);
4008 }
4009
4010 void
4011 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
4012     unsigned long *frame_cnt, unsigned long *byte_cnt)
4013 {
4014         struct lkpi_txq *ltxq;
4015         struct sk_buff *skb;
4016         unsigned long fc, bc;
4017
4018         ltxq = TXQ_TO_LTXQ(txq);
4019
4020         fc = bc = 0;
4021         skb_queue_walk(&ltxq->skbq, skb) {
4022                 fc++;
4023                 bc += skb->len;
4024         }
4025         if (frame_cnt)
4026                 *frame_cnt = fc;
4027         if (byte_cnt)
4028                 *byte_cnt = bc;
4029
4030         /* Validate that this is doing the correct thing. */
4031         /* Should we keep track on en/dequeue? */
4032         IMPROVE();
4033 }
4034
4035 /*
4036  * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
4037  * The latter tries to derive the success status from the info flags
4038  * passed back from the driver.  rawx_mit() saves the ni on the m and the
4039  * m on the skb for us to be able to give feedback to net80211.
4040  */
4041 void
4042 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
4043     int status)
4044 {
4045         struct ieee80211_node *ni;
4046         struct mbuf *m;
4047
4048         m = skb->m;
4049         skb->m = NULL;
4050
4051         if (m != NULL) {
4052                 ni = m->m_pkthdr.PH_loc.ptr;
4053                 /* Status: 0 is ok, != 0 is error. */
4054                 ieee80211_tx_complete(ni, m, status);
4055                 /* ni & mbuf were consumed. */
4056         }
4057
4058         kfree_skb(skb);
4059 }
4060
4061 /*
4062  * This is an internal bandaid for the moment for the way we glue
4063  * skbs and mbufs together for TX.  Once we have skbs backed by
4064  * mbufs this should go away.
4065  * This is a public function but kept on the private KPI (lkpi_)
4066  * and is not exposed by a header file.
4067  */
4068 static void
4069 lkpi_ieee80211_free_skb_mbuf(void *p)
4070 {
4071         struct ieee80211_node *ni;
4072         struct mbuf *m;
4073
4074         if (p == NULL)
4075                 return;
4076
4077         m = (struct mbuf *)p;
4078         M_ASSERTPKTHDR(m);
4079
4080         ni = m->m_pkthdr.PH_loc.ptr;
4081         m->m_pkthdr.PH_loc.ptr = NULL;
4082         if (ni != NULL)
4083                 ieee80211_free_node(ni);
4084         m_freem(m);
4085 }
4086
4087 void
4088 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
4089     struct delayed_work *w, int delay)
4090 {
4091         struct lkpi_hw *lhw;
4092
4093         /* Need to make sure hw is in a stable (non-suspended) state. */
4094         IMPROVE();
4095
4096         lhw = HW_TO_LHW(hw);
4097         queue_delayed_work(lhw->workq, w, delay);
4098 }
4099
4100 void
4101 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
4102     struct work_struct *w)
4103 {
4104         struct lkpi_hw *lhw;
4105
4106         /* Need to make sure hw is in a stable (non-suspended) state. */
4107         IMPROVE();
4108
4109         lhw = HW_TO_LHW(hw);
4110         queue_work(lhw->workq, w);
4111 }
4112
4113 struct sk_buff *
4114 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
4115     struct ieee80211_vif *vif)
4116 {
4117         struct lkpi_vif *lvif;
4118         struct ieee80211vap *vap;
4119         struct sk_buff *skb;
4120         struct ieee80211_frame_pspoll *psp;
4121         uint16_t v;
4122
4123         skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
4124         if (skb == NULL)
4125                 return (NULL);
4126
4127         skb_reserve(skb, hw->extra_tx_headroom);
4128
4129         lvif = VIF_TO_LVIF(vif);
4130         vap = LVIF_TO_VAP(lvif);
4131
4132         psp = skb_put_zero(skb, sizeof(*psp));
4133         psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
4134         psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
4135         v = htole16(vif->bss_conf.aid | 1<<15 | 1<<16);
4136         memcpy(&psp->i_aid, &v, sizeof(v));
4137         IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
4138         IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
4139
4140         return (skb);
4141 }
4142
4143 struct sk_buff *
4144 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
4145     struct ieee80211_vif *vif, bool qos)
4146 {
4147         struct lkpi_vif *lvif;
4148         struct ieee80211vap *vap;
4149         struct sk_buff *skb;
4150         struct ieee80211_frame *nullf;
4151
4152         skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
4153         if (skb == NULL)
4154                 return (NULL);
4155
4156         skb_reserve(skb, hw->extra_tx_headroom);
4157
4158         lvif = VIF_TO_LVIF(vif);
4159         vap = LVIF_TO_VAP(lvif);
4160
4161         nullf = skb_put_zero(skb, sizeof(*nullf));
4162         nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
4163         nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
4164         nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
4165
4166         IEEE80211_ADDR_COPY(nullf->i_addr1, vap->iv_bss->ni_bssid);
4167         IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
4168         IEEE80211_ADDR_COPY(nullf->i_addr3, vap->iv_bss->ni_macaddr);
4169
4170         return (skb);
4171 }
4172
4173 struct wireless_dev *
4174 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
4175 {
4176         struct lkpi_vif *lvif;
4177
4178         lvif = VIF_TO_LVIF(vif);
4179         return (&lvif->wdev);
4180 }
4181
4182 void
4183 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
4184 {
4185         struct lkpi_vif *lvif;
4186         struct ieee80211vap *vap;
4187         enum ieee80211_state nstate;
4188         int arg;
4189
4190         lvif = VIF_TO_LVIF(vif);
4191         vap = LVIF_TO_VAP(lvif);
4192
4193         /*
4194          * Go to init; otherwise we need to elaborately check state and
4195          * handle accordingly, e.g., if in RUN we could call iv_bmiss.
4196          * Let the statemachine handle all neccessary changes.
4197          */
4198         nstate = IEEE80211_S_INIT;
4199         arg = 0;        /* Not a valid reason. */
4200
4201 #ifdef LINUXKPI_DEBUG_80211
4202         if (linuxkpi_debug_80211 & D80211_TRACE)
4203                 ic_printf(vap->iv_ic, "%s: vif %p\n", __func__, vif);
4204 #endif
4205         ieee80211_new_state(vap, nstate, arg);
4206 }
4207
4208 void
4209 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
4210 {
4211         struct lkpi_vif *lvif;
4212         struct ieee80211vap *vap;
4213         enum ieee80211_state nstate;
4214         int arg;
4215
4216         lvif = VIF_TO_LVIF(vif);
4217         vap = LVIF_TO_VAP(lvif);
4218
4219         /*
4220          * Go to scan; otherwise we need to elaborately check state and
4221          * handle accordingly, e.g., if in RUN we could call iv_bmiss.
4222          * Let the statemachine handle all neccessary changes.
4223          */
4224         nstate = IEEE80211_S_SCAN;
4225         arg = 0;
4226
4227         /* We should be in RUN.  Can we assert that? */
4228 #ifdef LINUXKPI_DEBUG_80211
4229         if (linuxkpi_debug_80211 & D80211_TRACE || vap->iv_state != IEEE80211_S_RUN)
4230                 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s\n", __func__,
4231                     vif, vap, ieee80211_state_name[vap->iv_state]);
4232 #endif
4233         ieee80211_new_state(vap, nstate, arg);
4234 }
4235
4236 MODULE_VERSION(linuxkpi_wlan, 1);
4237 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
4238 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);