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