1 /*-
2 * Copyright (c) 2020-2026 The FreeBSD Foundation
3 * Copyright (c) 2020-2026 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 /*
43 * TODO:
44 * - lots :)
45 * - HW_CRYPTO: we need a "keystore" and an ordered list for suspend/resume.
46 */
47
48 #include <sys/param.h>
49 #include <sys/types.h>
50 #include <sys/kernel.h>
51 #include <sys/errno.h>
52 #include <sys/malloc.h>
53 #include <sys/module.h>
54 #include <sys/mutex.h>
55 #include <sys/sbuf.h>
56 #include <sys/socket.h>
57 #include <sys/sysctl.h>
58 #include <sys/queue.h>
59 #include <sys/taskqueue.h>
60 #include <sys/libkern.h>
61
62 #include <net/if.h>
63 #include <net/if_var.h>
64 #include <net/if_media.h>
65 #include <net/ethernet.h>
66
67 #include <net80211/ieee80211_var.h>
68 #include <net80211/ieee80211_proto.h>
69 #include <net80211/ieee80211_ratectl.h>
70 #include <net80211/ieee80211_radiotap.h>
71 #include <net80211/ieee80211_vht.h>
72
73 #define LINUXKPI_NET80211
74 #include <net/mac80211.h>
75
76 #include <linux/workqueue.h>
77 #include <linux/rculist.h>
78 #include "linux_80211.h"
79
80 /* #define LKPI_80211_USE_SCANLIST */
81 /* #define LKPI_80211_BGSCAN */
82 #define LKPI_80211_WME
83 #define LKPI_80211_HW_CRYPTO
84 #define LKPI_80211_HT
85 #define LKPI_80211_VHT
86
87 #if defined(LKPI_80211_VHT) && !defined(LKPI_80211_HT)
88 #define LKPI_80211_HT
89 #endif
90 #if defined(LKPI_80211_HT) && !defined(LKPI_80211_HW_CRYPTO)
91 #define LKPI_80211_HW_CRYPTO
92 #endif
93
94 static MALLOC_DEFINE(M_LKPI80211, "lkpi80211", "LinuxKPI 80211 compat");
95
96 /* XXX-BZ really want this and others in queue.h */
97 #define TAILQ_ELEM_INIT(elm, field) do { \
98 (elm)->field.tqe_next = NULL; \
99 (elm)->field.tqe_prev = NULL; \
100 } while (0)
101
102 /* -------------------------------------------------------------------------- */
103
104 SYSCTL_DECL(_compat_linuxkpi);
105 SYSCTL_NODE(_compat_linuxkpi, OID_AUTO, 80211, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
106 "LinuxKPI 802.11 compatibility layer");
107
108 static int lkpi_suspend_type = 1;
109 SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, suspend_type, CTLFLAG_RW,
110 &lkpi_suspend_type, 0,
111 "LinuxKPI 802.11 suspend type bitmask (0=off, 1=net80211, 2=wowlan");
112
113 static bool lkpi_order_scanlist = false;
114 SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, order_scanlist, CTLFLAG_RW,
115 &lkpi_order_scanlist, 0, "Enable LinuxKPI 802.11 scan list shuffeling");
116
117 #if defined(LKPI_80211_HW_CRYPTO)
118 static bool lkpi_hwcrypto = false;
119 SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, hw_crypto, CTLFLAG_RDTUN,
120 &lkpi_hwcrypto, 0, "Enable LinuxKPI 802.11 hardware crypto offload");
121
122 static bool lkpi_hwcrypto_tkip = false;
123 SYSCTL_BOOL(_compat_linuxkpi_80211, OID_AUTO, tkip, CTLFLAG_RDTUN,
124 &lkpi_hwcrypto_tkip, 0, "Enable LinuxKPI 802.11 TKIP crypto offload");
125 #endif
126
127 /* Keep public for as long as header files are using it too. */
128 int linuxkpi_debug_80211;
129
130 #ifdef LINUXKPI_DEBUG_80211
131 SYSCTL_INT(_compat_linuxkpi_80211, OID_AUTO, debug, CTLFLAG_RWTUN,
132 &linuxkpi_debug_80211, 0, "LinuxKPI 802.11 debug level");
133
134 #define UNIMPLEMENTED if (linuxkpi_debug_80211 & D80211_TODO) \
135 printf("XXX-TODO %s:%d: UNIMPLEMENTED\n", __func__, __LINE__)
136 #define TRACEOK(_fmt, ...) if (linuxkpi_debug_80211 & D80211_TRACEOK) \
137 printf("%s:%d: TRACEPOINT " _fmt "\n", __func__, __LINE__, ##__VA_ARGS__)
138 #else
139 #define UNIMPLEMENTED do { } while (0)
140 #define TRACEOK(...) do { } while (0)
141 #endif
142
143 /* #define PREP_TX_INFO_DURATION (IEEE80211_TRANS_WAIT * 1000) */
144 #ifndef PREP_TX_INFO_DURATION
145 #define PREP_TX_INFO_DURATION 0 /* Let the driver do its thing. */
146 #endif
147
148 /* This is DSAP | SSAP | CTRL | ProtoID/OrgCode{3}. */
149 const uint8_t rfc1042_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00 };
150
151 /* IEEE 802.11-05/0257r1 */
152 const uint8_t bridge_tunnel_header[6] = { 0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8 };
153
154 /* IEEE 802.11e Table 20i-UP-to-AC mappings. */
155 static const uint8_t ieee80211e_up_to_ac[] = {
156 IEEE80211_AC_BE,
157 IEEE80211_AC_BK,
158 IEEE80211_AC_BK,
159 IEEE80211_AC_BE,
160 IEEE80211_AC_VI,
161 IEEE80211_AC_VI,
162 IEEE80211_AC_VO,
163 IEEE80211_AC_VO,
164 #if 0
165 IEEE80211_AC_VO, /* We treat MGMT as TID 8, which is set as AC_VO */
166 #endif
167 };
168
169 const struct cfg80211_ops linuxkpi_mac80211cfgops = {
170 /*
171 * XXX TODO need a "glue layer" to link cfg80211 ops to
172 * mac80211 and to the driver or net80211.
173 * Can we pass some on 1:1? Need to compare the (*f)().
174 */
175 };
176
177 #if 0
178 static struct lkpi_sta *lkpi_find_lsta_by_ni(struct lkpi_vif *,
179 struct ieee80211_node *);
180 #endif
181 static void lkpi_sw_scan_task(void *, int);
182 static void lkpi_80211_txq_tx_one(struct lkpi_sta *, struct mbuf *);
183 static void lkpi_80211_txq_task(void *, int);
184 static void lkpi_80211_lhw_rxq_task(void *, int);
185 static void lkpi_ieee80211_free_skb_mbuf(void *);
186 #ifdef LKPI_80211_WME
187 static int lkpi_wme_update(struct lkpi_hw *, struct ieee80211vap *, bool);
188 #endif
189 static int lkpi_80211_update_chandef(struct ieee80211_hw *,
190 struct ieee80211_chanctx_conf *);
191 static void lkpi_ieee80211_wake_queues_locked(struct ieee80211_hw *);
192
193 static const char *
lkpi_rate_info_bw_to_str(enum rate_info_bw bw)194 lkpi_rate_info_bw_to_str(enum rate_info_bw bw)
195 {
196
197 switch (bw) {
198
199 case RATE_INFO_BW_20:
200 return ("20");
201 break;
202 case RATE_INFO_BW_5:
203 return ("5");
204 break;
205 case RATE_INFO_BW_10:
206 return ("10");
207 break;
208 case RATE_INFO_BW_40:
209 return ("40");
210 break;
211 case RATE_INFO_BW_80:
212 return ("80");
213 break;
214 case RATE_INFO_BW_160:
215 return ("160");
216 break;
217 case RATE_INFO_BW_HE_RU:
218 IMPROVE("nl80211_he_ru_alloc");
219 return ("HE_RU");
220 break;
221 case RATE_INFO_BW_320:
222 return ("320");
223 break;
224 case RATE_INFO_BW_EHT_RU:
225 IMPROVE("nl80211_eht_ru_alloc");
226 return ("EHT_RU");
227 break;
228 default:
229 return ("?");
230 break;
231 }
232 }
233
234 static void
lkpi_nl80211_sta_info_to_str(struct sbuf * s,const char * prefix,const uint64_t flags)235 lkpi_nl80211_sta_info_to_str(struct sbuf *s, const char *prefix,
236 const uint64_t flags)
237 {
238 int bit, i;
239
240 sbuf_printf(s, "%s %#010jx", prefix, flags);
241
242 i = 0;
243 for (bit = 0; bit < BITS_PER_TYPE(flags); bit++) {
244
245 if ((flags & BIT_ULL(bit)) == 0)
246 continue;
247
248 #define EXPAND_CASE(_flag) \
249 case NL80211_STA_INFO_ ## _flag: \
250 sbuf_printf(s, "%c%s", (i == 0) ? '<' : ',', #_flag); \
251 i++; \
252 break;
253
254 switch (bit) {
255 EXPAND_CASE(BEACON_RX)
256 EXPAND_CASE(BEACON_SIGNAL_AVG)
257 EXPAND_CASE(BSS_PARAM)
258 EXPAND_CASE(CHAIN_SIGNAL)
259 EXPAND_CASE(CHAIN_SIGNAL_AVG)
260 EXPAND_CASE(CONNECTED_TIME)
261 EXPAND_CASE(INACTIVE_TIME)
262 EXPAND_CASE(SIGNAL)
263 EXPAND_CASE(SIGNAL_AVG)
264 EXPAND_CASE(STA_FLAGS)
265 EXPAND_CASE(RX_BITRATE)
266 EXPAND_CASE(RX_PACKETS)
267 EXPAND_CASE(RX_BYTES)
268 EXPAND_CASE(RX_DROP_MISC)
269 EXPAND_CASE(TX_BITRATE)
270 EXPAND_CASE(TX_PACKETS)
271 EXPAND_CASE(TX_BYTES)
272 EXPAND_CASE(TX_BYTES64)
273 EXPAND_CASE(RX_BYTES64)
274 EXPAND_CASE(TX_FAILED)
275 EXPAND_CASE(TX_RETRIES)
276 EXPAND_CASE(RX_DURATION)
277 EXPAND_CASE(TX_DURATION)
278 EXPAND_CASE(ACK_SIGNAL)
279 EXPAND_CASE(ACK_SIGNAL_AVG)
280 default:
281 sbuf_printf(s, "%c?%d", (i == 0) ? '<' : ',', bit);
282 break;
283 }
284 }
285 #undef EXPAND_CASE
286 if (i > 0)
287 sbuf_printf(s, ">");
288 sbuf_printf(s, "\n");
289 }
290
291 static void
lkpi_80211_dump_lvif_stas(struct lkpi_vif * lvif,struct sbuf * s,bool dump_queues)292 lkpi_80211_dump_lvif_stas(struct lkpi_vif *lvif, struct sbuf *s, bool dump_queues)
293 {
294 struct lkpi_hw *lhw;
295 struct ieee80211_hw *hw;
296 struct ieee80211vap *vap;
297 struct ieee80211_vif *vif;
298 struct lkpi_sta *lsta;
299 struct ieee80211_sta *sta;
300 struct station_info sinfo;
301 int error;
302 uint8_t tid;
303
304 vif = LVIF_TO_VIF(lvif);
305 vap = LVIF_TO_VAP(lvif);
306 lhw = vap->iv_ic->ic_softc;
307 hw = LHW_TO_HW(lhw);
308
309 wiphy_lock(hw->wiphy);
310 list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
311 sta = LSTA_TO_STA(lsta);
312
313 sbuf_putc(s, '\n');
314 sbuf_printf(s, "lsta %p sta %p added_to_drv %d\n", lsta, sta, lsta->added_to_drv);
315
316 memset(&sinfo, 0, sizeof(sinfo));
317 error = lkpi_80211_mo_sta_statistics(hw, vif, sta, &sinfo);
318 if (error == EEXIST) /* Not added to driver. */
319 continue;
320 if (error == ENOTSUPP) {
321 sbuf_printf(s, " sta_statistics not supported\n");
322 continue;
323 }
324 if (error != 0) {
325 sbuf_printf(s, " sta_statistics failed: %d\n", error);
326 continue;
327 }
328
329 /* If no RX_BITRATE is reported, try to fill it in from the lsta sinfo. */
330 if ((sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) == 0 &&
331 (lsta->sinfo.filled & BIT_ULL(NL80211_STA_INFO_RX_BITRATE)) != 0) {
332 memcpy(&sinfo.rxrate, &lsta->sinfo.rxrate, sizeof(sinfo.rxrate));
333 sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
334 }
335 /* If no CHAIN_SIGNAL is reported, try to fill it in from the lsta sinfo. */
336 if ((sinfo.filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) == 0 &&
337 (lsta->sinfo.filled & BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL)) != 0) {
338 sinfo.chains = lsta->sinfo.chains;
339 memcpy(sinfo.chain_signal, lsta->sinfo.chain_signal,
340 sizeof(sinfo.chain_signal));
341 sinfo.filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
342 }
343
344 lkpi_nl80211_sta_info_to_str(s, " nl80211_sta_info (valid fields)", sinfo.filled);
345 sbuf_printf(s, " connected_time %u inactive_time %u\n",
346 sinfo.connected_time, sinfo.inactive_time);
347 sbuf_printf(s, " rx_bytes %ju rx_packets %u rx_dropped_misc %u\n",
348 (uintmax_t)sinfo.rx_bytes, sinfo.rx_packets, sinfo.rx_dropped_misc);
349 sbuf_printf(s, " rx_duration %ju rx_beacon %u rx_beacon_signal_avg %d\n",
350 (uintmax_t)sinfo.rx_duration, sinfo.rx_beacon, (int8_t)sinfo.rx_beacon_signal_avg);
351
352 sbuf_printf(s, " tx_bytes %ju tx_packets %u tx_failed %u\n",
353 (uintmax_t)sinfo.tx_bytes, sinfo.tx_packets, sinfo.tx_failed);
354 sbuf_printf(s, " tx_duration %ju tx_retries %u\n",
355 (uintmax_t)sinfo.tx_duration, sinfo.tx_retries);
356
357 sbuf_printf(s, " signal %d signal_avg %d ack_signal %d avg_ack_signal %d\n",
358 sinfo.signal, sinfo.signal_avg, sinfo.ack_signal, sinfo.avg_ack_signal);
359 sbuf_printf(s, " generation %d assoc_req_ies_len %zu chains %#04x\n",
360 sinfo.generation, sinfo.assoc_req_ies_len, sinfo.chains);
361
362 for (int i = 0; i < nitems(sinfo.chain_signal) && i < IEEE80211_MAX_CHAINS; i++) {
363 if (!(sinfo.chains & BIT(i)))
364 continue;
365 sbuf_printf(s, " chain[%d] signal %d signal_avg %d\n",
366 i, (int8_t)sinfo.chain_signal[i], (int8_t)sinfo.chain_signal_avg[i]);
367 }
368
369 /* assoc_req_ies, bss_param, sta_flags */
370
371 sbuf_printf(s, " rxrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
372 sinfo.rxrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
373 sinfo.rxrate.bw, lkpi_rate_info_bw_to_str(sinfo.rxrate.bw),
374 sinfo.rxrate.legacy * 100,
375 sinfo.rxrate.mcs, sinfo.rxrate.nss);
376 sbuf_printf(s, " he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
377 sinfo.rxrate.he_dcm, sinfo.rxrate.he_gi, sinfo.rxrate.he_ru_alloc,
378 sinfo.rxrate.eht_gi);
379 sbuf_printf(s, " txrate: flags %b bw %u(%s) legacy %u kbit/s mcs %u nss %u\n",
380 sinfo.txrate.flags, CFG80211_RATE_INFO_FLAGS_BITS,
381 sinfo.txrate.bw, lkpi_rate_info_bw_to_str(sinfo.txrate.bw),
382 sinfo.txrate.legacy * 100,
383 sinfo.txrate.mcs, sinfo.txrate.nss);
384 sbuf_printf(s, " he_dcm %u he_gi %u he_ru_alloc %u eht_gi %u\n",
385 sinfo.txrate.he_dcm, sinfo.txrate.he_gi, sinfo.txrate.he_ru_alloc,
386 sinfo.txrate.eht_gi);
387
388 if (!dump_queues)
389 continue;
390
391 /* Dump queue information. */
392 sbuf_printf(s, " Queue information:\n");
393 sbuf_printf(s, " frms direct tx %ju\n", lsta->frms_tx);
394 for (tid = 0; tid <= IEEE80211_NUM_TIDS; tid++) {
395 struct lkpi_txq *ltxq;
396
397 if (sta->txq[tid] == NULL) {
398 sbuf_printf(s, " tid %-2u NOQ\n", tid);
399 continue;
400 }
401
402 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
403 #ifdef __notyet__
404 sbuf_printf(s, " tid %-2u flags: %b "
405 "txq_generation %u skbq len %d\n",
406 tid, ltxq->flags, LKPI_TXQ_FLAGS_BITS,
407 ltxq->txq_generation,
408 skb_queue_len_lockless(<xq->skbq));
409 #else
410 sbuf_printf(s, " tid %-2u "
411 "txq_generation %u skbq len %d\n",
412 tid,
413 ltxq->txq_generation,
414 skb_queue_len_lockless(<xq->skbq));
415 #endif
416 sbuf_printf(s, " frms_enqueued %ju frms_dequeued %ju "
417 "frms_tx %ju\n",
418 ltxq->frms_enqueued, ltxq->frms_dequeued, ltxq->frms_tx);
419 }
420 }
421 wiphy_unlock(hw->wiphy);
422 }
423
424 static int
lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)425 lkpi_80211_dump_stas(SYSCTL_HANDLER_ARGS)
426 {
427 struct lkpi_vif *lvif;
428 struct sbuf s;
429
430 if (req->newptr)
431 return (EPERM);
432
433 lvif = (struct lkpi_vif *)arg1;
434
435 sbuf_new_for_sysctl(&s, NULL, 1024, req);
436
437 lkpi_80211_dump_lvif_stas(lvif, &s, false);
438
439 sbuf_finish(&s);
440 sbuf_delete(&s);
441
442 return (0);
443 }
444
445 static int
lkpi_80211_dump_sta_queues(SYSCTL_HANDLER_ARGS)446 lkpi_80211_dump_sta_queues(SYSCTL_HANDLER_ARGS)
447 {
448 struct lkpi_vif *lvif;
449 struct sbuf s;
450
451 if (req->newptr)
452 return (EPERM);
453
454 lvif = (struct lkpi_vif *)arg1;
455
456 sbuf_new_for_sysctl(&s, NULL, 1024, req);
457
458 lkpi_80211_dump_lvif_stas(lvif, &s, true);
459
460 sbuf_finish(&s);
461 sbuf_delete(&s);
462
463 return (0);
464 }
465
466 static enum ieee80211_sta_rx_bandwidth
lkpi_cw_to_rx_bw(enum nl80211_chan_width cw)467 lkpi_cw_to_rx_bw(enum nl80211_chan_width cw)
468 {
469 switch (cw) {
470 case NL80211_CHAN_WIDTH_320:
471 return (IEEE80211_STA_RX_BW_320);
472 case NL80211_CHAN_WIDTH_160:
473 case NL80211_CHAN_WIDTH_80P80:
474 return (IEEE80211_STA_RX_BW_160);
475 case NL80211_CHAN_WIDTH_80:
476 return (IEEE80211_STA_RX_BW_80);
477 case NL80211_CHAN_WIDTH_40:
478 return (IEEE80211_STA_RX_BW_40);
479 case NL80211_CHAN_WIDTH_20:
480 case NL80211_CHAN_WIDTH_20_NOHT:
481 return (IEEE80211_STA_RX_BW_20);
482 case NL80211_CHAN_WIDTH_5:
483 case NL80211_CHAN_WIDTH_10:
484 /* Unsupported input. */
485 return (IEEE80211_STA_RX_BW_20);
486 }
487 }
488
489 static enum nl80211_chan_width
lkpi_rx_bw_to_cw(enum ieee80211_sta_rx_bandwidth rx_bw)490 lkpi_rx_bw_to_cw(enum ieee80211_sta_rx_bandwidth rx_bw)
491 {
492 switch (rx_bw) {
493 case IEEE80211_STA_RX_BW_20:
494 return (NL80211_CHAN_WIDTH_20); /* _NOHT */
495 case IEEE80211_STA_RX_BW_40:
496 return (NL80211_CHAN_WIDTH_40);
497 case IEEE80211_STA_RX_BW_80:
498 return (NL80211_CHAN_WIDTH_80);
499 case IEEE80211_STA_RX_BW_160:
500 return (NL80211_CHAN_WIDTH_160); /* 80P80 */
501 case IEEE80211_STA_RX_BW_320:
502 return (NL80211_CHAN_WIDTH_320);
503 }
504 }
505
506 static enum ieee80211_bss_changed
lkpi_sta_supp_rates(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_node * ni,enum ieee80211_rate_control_changed_flags * rc_changed)507 lkpi_sta_supp_rates(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
508 struct ieee80211_node *ni,
509 enum ieee80211_rate_control_changed_flags *rc_changed)
510 {
511 struct lkpi_vif *lvif;
512 struct lkpi_sta *lsta;
513 struct ieee80211_sta *sta;
514 enum ieee80211_bss_changed bss_changed;
515 struct ieee80211_supported_band *supband;
516 uint32_t supp_rates, basic_rates;
517 int band, i, n;
518
519 bss_changed = 0;
520
521 band = vif->bss_conf.chanreq.oper.chan->band;
522 supband = hw->wiphy->bands[band];
523 if (supband == NULL)
524 return (bss_changed);
525
526 lvif = VIF_TO_LVIF(vif);
527 lsta = ni->ni_drv_data;
528 sta = LSTA_TO_STA(lsta);
529
530 supp_rates = 0;
531 basic_rates = 0;
532
533 for (n = 0; n < ni->ni_rates.rs_nrates; n++) {
534 uint8_t supp_rate;
535 uint16_t bitr;
536 bool basic;
537
538 /* Note: net80211 rates are in 0.5Mbit/s, e.g., 108 is 54Mbit/s. */
539 supp_rate = ni->ni_rates.rs_rates[n] & (~IEEE80211_RATE_BASIC);
540 basic = (ni->ni_rates.rs_rates[n] & IEEE80211_RATE_BASIC) != 0;
541
542 for (i = 0; i < supband->n_bitrates; i++) {
543 /* Band bitrates are * 10 so, e.g., 55 is 5.5Mbit/s. */
544 /* To match net80211 rates we need to do a DIV5. */
545 bitr = howmany(supband->bitrates[i].bitrate, 5);
546 if (supp_rate == bitr) {
547 supp_rates |= BIT(i);
548 if (basic)
549 basic_rates |= BIT(i);
550 }
551 /*
552 * We are not checking if we are doing 11b or 11g and
553 * if the rate is fine for each.
554 */
555 }
556 TRACE_RATES("supp_rate %u basic %d supp_rates %#010x basic_rates %#010x",
557 supp_rate, basic, supp_rates, basic_rates);
558 }
559 if (basic_rates != 0 &&
560 vif->bss_conf.basic_rates != basic_rates) {
561 TRACE_RATES("vif bss_conf basic_rates %#010x update to %#010x",
562 vif->bss_conf.basic_rates, basic_rates);
563 vif->bss_conf.basic_rates = basic_rates;
564 bss_changed |= BSS_CHANGED_BASIC_RATES;
565 }
566 /* Guard against net80211 not having any rates set when we get here. */
567 if (supp_rates == 0)
568 supp_rates = vif->bss_conf.basic_rates;
569 if (sta->deflink.supp_rates[band] != supp_rates) {
570 TRACE_RATES("band %d supp_rates %#010x update to %#010x",
571 band, sta->deflink.supp_rates[band], supp_rates);
572 sta->deflink.supp_rates[band] = supp_rates;
573 if (rc_changed != NULL)
574 *rc_changed |= IEEE80211_RC_SUPP_RATES_CHANGED;
575 }
576
577 /*
578 * br_mask got initialized in lkpi_ic_vap_create().
579 * Do a basic rates check against it for the current band if we are
580 * in a state to have all the above information.
581 */
582 TRACE_RATES("band %d br_mask legacy %#010x & basic_rates %#010x != 0?",
583 band, lvif->br_mask.control[band].legacy, vif->bss_conf.basic_rates);
584 if (band == vif->bss_conf.chanreq.oper.chan->band &&
585 (lvif->br_mask.control[band].legacy & vif->bss_conf.basic_rates) == 0) {
586 /* In our setup this should never happen. */
587 printf("%s: WARNING: no acceptable basic rate %#010x & %#010x\n",
588 __func__, lvif->br_mask.control[band].legacy, vif->bss_conf.basic_rates);
589 }
590
591 /*
592 * XXX-BZ we should track changes here as well and call or let the
593 * caller call lkpi_80211_mo_set_bitrate_mask() if needed.
594 * Note: the call in lkpi_sta_scan_to_auth() still have to
595 * happen unconditionally for the initial setting.
596 */
597 #if defined(LKPI_80211_HT)
598 if (supband->ht_cap.ht_supported) {
599 memcpy(lvif->br_mask.control[band].ht_mcs,
600 supband->ht_cap.mcs.rx_mask,
601 sizeof(lvif->br_mask.control[band].ht_mcs));
602 #if defined(LINUXKPI_DEBUG_80211)
603 for (int i = 0; i < IEEE80211_HT_MCS_MASK_LEN; i++) {
604 TRACE_RATES("band %d ht_mcs[%d] %#010x",
605 band, i, lvif->br_mask.control[band].ht_mcs[i]);
606 }
607 #endif
608 }
609 #endif /* LKPI_80211_HT */
610 #if defined(LKPI_80211_VHT)
611 if (supband->vht_cap.vht_supported) {
612 uint16_t mcs_map, val;
613 uint8_t nss;
614
615 mcs_map = supband->vht_cap.vht_mcs.tx_mcs_map;
616 for (nss = 0; nss < NL80211_VHT_NSS_MAX; nss++) {
617 TRACE_RATES("band %d nss %d vht_mcs.tx_mcs_map %#06x mcs_map %#06x & 0x3 = %#06x",
618 band, nss, supband->vht_cap.vht_mcs.tx_mcs_map, mcs_map, mcs_map & 0x3);
619 switch (mcs_map & 0x3) {
620 case IEEE80211_VHT_MCS_SUPPORT_0_7:
621 val = 0x00ff;
622 break;
623 case IEEE80211_VHT_MCS_SUPPORT_0_8:
624 val = 0x01ff;
625 break;
626 case IEEE80211_VHT_MCS_SUPPORT_0_9:
627 val = 0x03ff;
628 break;
629 case IEEE80211_VHT_MCS_NOT_SUPPORTED:
630 val = 0;
631 break;
632 }
633 lvif->br_mask.control[band].vht_mcs[nss] = val;
634 TRACE_RATES("band %d nss %d vht_mcs %#06x",
635 band, nss, lvif->br_mask.control[band].vht_mcs[nss]);
636 mcs_map >>= 2;
637 }
638 }
639 #endif /* LKPI_80211_VHT */
640
641 return (bss_changed);
642 }
643
644 static void
lkpi_sync_chanctx_cw_from_rx_bw(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta)645 lkpi_sync_chanctx_cw_from_rx_bw(struct ieee80211_hw *hw,
646 struct ieee80211_vif *vif, struct ieee80211_sta *sta)
647 {
648 struct lkpi_hw *lhw;
649 struct ieee80211_chanctx_conf *chanctx_conf;
650 enum ieee80211_sta_rx_bandwidth old_bw;
651 uint32_t changed;
652
653 lockdep_assert_wiphy(hw->wiphy);
654
655 chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
656 lockdep_is_held(&hw->wiphy->mtx));
657 if (chanctx_conf == NULL)
658 return;
659
660 old_bw = lkpi_cw_to_rx_bw(chanctx_conf->def.width);
661 TRACE_RATES("old_bw %d sta->deflink.bandwidth %d hw->conf.chandef.width %d",
662 old_bw, sta->deflink.bandwidth, lkpi_cw_to_rx_bw(hw->conf.chandef.width));
663
664 lhw = HW_TO_LHW(hw);
665 if (old_bw == sta->deflink.bandwidth &&
666 (!lhw->emulate_chanctx || old_bw == lkpi_cw_to_rx_bw(hw->conf.chandef.width)))
667 return;
668
669 chanctx_conf->def.width = lkpi_rx_bw_to_cw(sta->deflink.bandwidth);
670 if (chanctx_conf->def.width == NL80211_CHAN_WIDTH_20 &&
671 !sta->deflink.ht_cap.ht_supported)
672 chanctx_conf->def.width = NL80211_CHAN_WIDTH_20_NOHT;
673
674 chanctx_conf->min_def = chanctx_conf->def;
675
676 vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
677
678 TRACE_RATES("chanctx_conf %p def.width %d sta->deflink.bandwidth %d "
679 "ht_supported %d vht_supported %d",
680 chanctx_conf, chanctx_conf->def.width, sta->deflink.bandwidth,
681 sta->deflink.ht_cap.ht_supported, sta->deflink.vht_cap.vht_supported);
682
683 changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
684 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
685 lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
686 }
687
688 #if defined(LKPI_80211_HT)
689 static void
lkpi_sta_sync_ht_from_ni(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_node * ni)690 lkpi_sta_sync_ht_from_ni(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
691 struct ieee80211_sta *sta, struct ieee80211_node *ni)
692 {
693 struct ieee80211vap *vap;
694 uint8_t *ie;
695 struct ieee80211_ht_cap *htcap;
696 struct ieee80211_sta_ht_cap *ht_cap, *sta_ht_cap;
697 enum nl80211_band band;
698 int i, rx_nss;
699
700 if ((ni->ni_flags & IEEE80211_NODE_HT) == 0) {
701 sta->deflink.ht_cap.ht_supported = false;
702 TRACE_RATES("HT ht_supported %d", sta->deflink.ht_cap.ht_supported);
703 return;
704 }
705
706 sta->deflink.ht_cap.ht_supported = true;
707
708 /* htcap->ampdu_params_info */
709 vap = ni->ni_vap;
710 sta->deflink.ht_cap.ampdu_density = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY);
711 if (sta->deflink.ht_cap.ampdu_density > vap->iv_ampdu_density)
712 sta->deflink.ht_cap.ampdu_density = vap->iv_ampdu_density;
713 sta->deflink.ht_cap.ampdu_factor = _IEEE80211_MASKSHIFT(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU);
714 if (sta->deflink.ht_cap.ampdu_factor > vap->iv_ampdu_rxmax)
715 sta->deflink.ht_cap.ampdu_factor = vap->iv_ampdu_rxmax;
716
717 ie = ni->ni_ies.htcap_ie;
718 KASSERT(ie != NULL, ("%s: HT but no htcap_ie on ni %p\n", __func__, ni));
719 if (ie[0] == IEEE80211_ELEMID_VENDOR)
720 ie += 4;
721 ie += 2;
722 htcap = (struct ieee80211_ht_cap *)ie;
723 sta->deflink.ht_cap.cap = htcap->cap_info;
724 sta->deflink.ht_cap.mcs = htcap->mcs;
725
726 /*
727 * 802.11n-2009 20.6 Parameters for HT MCSs gives the mandatory/
728 * optional MCS for Nss=1..4. We need to check the first four
729 * MCS sets from the Rx MCS Bitmask; then there is MCS 32 and
730 * MCS33.. is UEQM.
731 */
732 band = vif->bss_conf.chanctx_conf->def.chan->band;
733 ht_cap = &hw->wiphy->bands[band]->ht_cap;
734 sta_ht_cap = &sta->deflink.ht_cap;
735 rx_nss = 0;
736 for (i = 0; i < 4; i++) {
737 TRACE_RATES("HT rx_mask[%d] sta %#04x & hw %#04x", i,
738 sta_ht_cap->mcs.rx_mask[i], ht_cap->mcs.rx_mask[i]);
739 sta_ht_cap->mcs.rx_mask[i] =
740 sta_ht_cap->mcs.rx_mask[i] & ht_cap->mcs.rx_mask[i];
741 /* XXX-BZ masking unequal modulation? */
742
743 if (sta_ht_cap->mcs.rx_mask[i] != 0)
744 rx_nss++;
745 }
746 if (rx_nss > 0) {
747 TRACE_RATES("HT rx_nss = max(%d, %d)", rx_nss, sta->deflink.rx_nss);
748 sta->deflink.rx_nss = MAX(rx_nss, sta->deflink.rx_nss);
749 } else {
750 sta->deflink.ht_cap.ht_supported = false;
751 TRACE_RATES("HT ht_supported %d", sta->deflink.ht_cap.ht_supported);
752 return;
753 }
754
755 if ((sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_SUP_WIDTH_20_40) != 0 &&
756 IEEE80211_IS_CHAN_HT40(ni->ni_chan))
757 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_40;
758 else
759 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
760
761 IMPROVE("sta->wme");
762
763 if (sta->deflink.ht_cap.cap & IEEE80211_HT_CAP_MAX_AMSDU)
764 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_7935;
765 else
766 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_3839;
767 sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
768 #ifdef __handled_by_driver__ /* iwlwifi only? actually unused? */
769 for (i = 0; i < nitems(sta.deflink.agg.max_tid_amsdu_len); i++) {
770 sta->deflink.agg.max_tid_amsdu_len[j] = ;
771 }
772 #endif
773 TRACE_RATES("HT ht_supported %d", sta->deflink.ht_cap.ht_supported);
774 }
775 #endif
776
777 #if defined(LKPI_80211_VHT)
778 static void
lkpi_sta_sync_vht_from_ni(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_node * ni)779 lkpi_sta_sync_vht_from_ni(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
780 struct ieee80211_sta *sta, struct ieee80211_node *ni)
781 {
782 struct ieee80211_sta_vht_cap *vht_cap, *sta_vht_cap;;
783 enum ieee80211_sta_rx_bandwidth bw;
784 enum nl80211_band band;
785 uint32_t width;
786 int rx_nss;
787 uint16_t rx_map, tx_map;
788
789 if ((ni->ni_flags & IEEE80211_NODE_VHT) == 0 ||
790 !IEEE80211_IS_CHAN_VHT_5GHZ(ni->ni_chan)) {
791 sta->deflink.vht_cap.vht_supported = false;
792 TRACE_RATES("VHT vht_supported %d", sta->deflink.vht_cap.vht_supported);
793 return;
794 }
795
796 sta->deflink.vht_cap.vht_supported = true;
797
798 sta->deflink.vht_cap.cap = ni->ni_vhtcap;
799 sta->deflink.vht_cap.vht_mcs = ni->ni_vht_mcsinfo;
800
801 /*
802 * If VHT20/40 are selected do not update the bandwidth
803 * from HT but stya on VHT.
804 */
805 if (ni->ni_vht_chanwidth == IEEE80211_VHT_CHANWIDTH_USE_HT)
806 goto skip_bw;
807
808 bw = sta->deflink.bandwidth;
809 width = (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_MASK);
810 switch (width) {
811 /* Deprecated. */
812 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160MHZ:
813 case IEEE80211_VHT_CAP_SUPP_CHAN_WIDTH_160_80PLUS80MHZ:
814 bw = IEEE80211_STA_RX_BW_160;
815 break;
816 default:
817 /* Check if we do support 160Mhz somehow after all. */
818 if ((sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_EXT_NSS_BW_MASK) != 0)
819 bw = IEEE80211_STA_RX_BW_160;
820 else
821 bw = IEEE80211_STA_RX_BW_80;
822 }
823 /*
824 * While we can set what is possibly supported we also need to be
825 * on a channel which supports that bandwidth; e.g., we can support
826 * VHT160 but the AP only does VHT80.
827 * Further ni_chan will also have filtered out what we disabled
828 * by configuration.
829 * Once net80211 channel selection is fixed for 802.11-2020 and
830 * VHT160 we can possibly spare ourselves the above.
831 */
832 if (bw == IEEE80211_STA_RX_BW_160 &&
833 !IEEE80211_IS_CHAN_VHT160(ni->ni_chan) &&
834 !IEEE80211_IS_CHAN_VHT80P80(ni->ni_chan))
835 bw = IEEE80211_STA_RX_BW_80;
836 if (bw == IEEE80211_STA_RX_BW_80 &&
837 !IEEE80211_IS_CHAN_VHT80(ni->ni_chan))
838 bw = sta->deflink.bandwidth;
839 sta->deflink.bandwidth = bw;
840 skip_bw:
841
842 band = vif->bss_conf.chanctx_conf->def.chan->band;
843 vht_cap = &hw->wiphy->bands[band]->vht_cap;
844 sta_vht_cap = &sta->deflink.vht_cap;
845
846 rx_nss = 0;
847 rx_map = tx_map = 0;
848 for (int i = 7; i >= 0; i--) {
849 uint8_t card, sta;
850
851 card = (vht_cap->vht_mcs.rx_mcs_map >> (2 * i)) & 0x3;
852 sta = (sta_vht_cap->vht_mcs.rx_mcs_map >> (2 * i)) & 0x3;
853 if (sta != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
854 if (card == IEEE80211_VHT_MCS_NOT_SUPPORTED)
855 sta = IEEE80211_VHT_MCS_NOT_SUPPORTED;
856 else {
857 sta = MIN(sta, card);
858 if (rx_nss == 0)
859 rx_nss = i + 1;
860 }
861 }
862 rx_map |= (sta << (2 * i));
863
864 card = (vht_cap->vht_mcs.tx_mcs_map >> (2 * i)) & 0x3;
865 sta = (sta_vht_cap->vht_mcs.tx_mcs_map >> (2 * i)) & 0x3;
866 if (sta != IEEE80211_VHT_MCS_NOT_SUPPORTED) {
867 if (card == IEEE80211_VHT_MCS_NOT_SUPPORTED)
868 sta = IEEE80211_VHT_MCS_NOT_SUPPORTED;
869 else
870 sta = MIN(sta, card);
871 }
872 tx_map |= (sta << (2 * i));
873 }
874 TRACE_RATES("VHT rx_mcs_map %#010x->%#010x, tx_mcs_map %#010x->%#010x, rx_nss = %d",
875 sta_vht_cap->vht_mcs.rx_mcs_map, rx_map,
876 sta_vht_cap->vht_mcs.tx_mcs_map, tx_map, rx_nss);
877 sta_vht_cap->vht_mcs.rx_mcs_map = rx_map;
878 sta_vht_cap->vht_mcs.tx_mcs_map = tx_map;
879 if (rx_nss > 0) {
880 TRACE_RATES("VHT rx_nss = max(%d, %d)", rx_nss, sta->deflink.rx_nss);
881 sta->deflink.rx_nss = MAX(rx_nss, sta->deflink.rx_nss);
882 } else {
883 sta->deflink.vht_cap.vht_supported = false;
884 TRACE_RATES("VHT vht_supported %d", sta->deflink.vht_cap.vht_supported);
885 return;
886 }
887
888 switch (sta->deflink.vht_cap.cap & IEEE80211_VHT_CAP_MAX_MPDU_MASK) {
889 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_11454:
890 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_11454;
891 break;
892 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_7991:
893 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_7991;
894 break;
895 case IEEE80211_VHT_CAP_MAX_MPDU_LENGTH_3895:
896 default:
897 sta->deflink.agg.max_amsdu_len = IEEE80211_MAX_MPDU_LEN_VHT_3895;
898 break;
899 }
900
901 TRACE_RATES("VHT vht_supported %d", sta->deflink.vht_cap.vht_supported);
902 }
903 #endif
904
905 static enum ieee80211_bss_changed
lkpi_sta_sync_from_ni(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_sta * sta,struct ieee80211_node * ni,bool updchnctx)906 lkpi_sta_sync_from_ni(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
907 struct ieee80211_sta *sta, struct ieee80211_node *ni, bool updchnctx)
908 {
909 enum ieee80211_bss_changed bss_changed;
910 enum ieee80211_rate_control_changed_flags rc_changed;
911 enum ieee80211_sta_rx_bandwidth bandwidth;
912 uint8_t rx_nss;
913
914 if (updchnctx)
915 lockdep_assert_wiphy(hw->wiphy);
916
917 bss_changed = 0;
918 rc_changed = 0;
919
920 bandwidth = sta->deflink.bandwidth;
921 rx_nss = sta->deflink.rx_nss;
922
923 TRACE_RATES("updchnctx %d bandwidth %d rx_nss %u",
924 updchnctx, bandwidth, rx_nss);
925
926 /*
927 * Ensure rx_nss is at least 1 as otherwise drivers run into
928 * unexpected problems.
929 */
930 sta->deflink.rx_nss = 1;
931
932 #if defined(LKPI_80211_HT)
933 lkpi_sta_sync_ht_from_ni(hw, vif, sta, ni);
934 #endif
935 #if defined(LKPI_80211_VHT)
936 lkpi_sta_sync_vht_from_ni(hw, vif, sta, ni);
937 #endif
938
939 /*
940 * We are also called from node allocation which net80211
941 * can do even on `ifconfig down`; in that case the chanctx
942 * may still be valid and we get a discrepancy between
943 * sta and chanctx. Thus do not try to update the chanctx
944 * when called from lkpi_lsta_alloc().
945 */
946 if (updchnctx)
947 lkpi_sync_chanctx_cw_from_rx_bw(hw, vif, sta);
948
949 bss_changed |= lkpi_sta_supp_rates(hw, vif, ni, &rc_changed);
950
951 if (sta->deflink.bandwidth != bandwidth)
952 rc_changed |= IEEE80211_RC_BW_CHANGED;
953 if (sta->deflink.rx_nss != rx_nss)
954 rc_changed |= IEEE80211_RC_NSS_CHANGED;
955
956 TRACE_RATES("updchnctx %d rc_change %#010x bss_changed %#010jx "
957 "bandwidth %d rx_nss %u",
958 updchnctx, rc_changed, (uintmax_t)bss_changed,
959 sta->deflink.bandwidth, sta->deflink.rx_nss);
960
961 if (rc_changed != 0)
962 lkpi_80211_mo_link_sta_rc_update(hw, vif, &sta->deflink, rc_changed);
963
964 return (bss_changed);
965 }
966
967 #if 0
968 static uint8_t
969 lkpi_get_max_rx_chains(struct ieee80211_node *ni)
970 {
971 uint8_t chains;
972 #if defined(LKPI_80211_HT) || defined(LKPI_80211_VHT)
973 struct lkpi_sta *lsta;
974 struct ieee80211_sta *sta;
975
976 lsta = ni->ni_drv_data;
977 sta = LSTA_TO_STA(lsta);
978 #endif
979
980 chains = 1;
981 #if defined(LKPI_80211_HT)
982 IMPROVE("We should factor counting MCS/NSS out for sync and here");
983 if (sta->deflink.ht_cap.ht_supported)
984 chains = MAX(chains, sta->deflink.rx_nss);
985 #endif
986
987 #if defined(LKPI_80211_VHT)
988 if (sta->deflink.vht_cap.vht_supported)
989 chains = MAX(chains, sta->deflink.rx_nss);
990 #endif
991
992 return (chains);
993 }
994 #endif
995
996 static void
lkpi_lsta_dump(struct lkpi_sta * lsta,struct ieee80211_node * ni,const char * _f,int _l)997 lkpi_lsta_dump(struct lkpi_sta *lsta, struct ieee80211_node *ni,
998 const char *_f, int _l)
999 {
1000
1001 #ifdef LINUXKPI_DEBUG_80211
1002 if ((linuxkpi_debug_80211 & D80211_TRACE_STA) == 0)
1003 return;
1004 if (lsta == NULL)
1005 return;
1006
1007 printf("%s:%d lsta %p ni %p sta %p\n",
1008 _f, _l, lsta, ni, &lsta->sta);
1009 if (ni != NULL)
1010 ieee80211_dump_node(NULL, ni);
1011 printf("\ttxq_task txq len %d mtx\n", mbufq_len(&lsta->txq));
1012 printf("\tkc %p state %d added_to_drv %d in_mgd %d\n",
1013 &lsta->kc[0], lsta->state, lsta->added_to_drv, lsta->in_mgd);
1014 #endif
1015 }
1016
1017 static void
lkpi_lsta_remove(struct lkpi_sta * lsta,struct lkpi_vif * lvif)1018 lkpi_lsta_remove(struct lkpi_sta *lsta, struct lkpi_vif *lvif)
1019 {
1020
1021 lockdep_assert_wiphy(lsta->hw->wiphy);
1022
1023 KASSERT(!list_empty(&lsta->lsta_list),
1024 ("%s: lsta %p ni %p\n", __func__, lsta, lsta->ni));
1025 list_del_init(&lsta->lsta_list);
1026 }
1027
1028 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)1029 lkpi_lsta_alloc(struct ieee80211vap *vap, const uint8_t mac[IEEE80211_ADDR_LEN],
1030 struct ieee80211_hw *hw, struct ieee80211_node *ni)
1031 {
1032 struct lkpi_sta *lsta;
1033 struct lkpi_vif *lvif;
1034 struct ieee80211_vif *vif;
1035 struct ieee80211_sta *sta;
1036 int band, i, tid;
1037
1038 lsta = malloc(sizeof(*lsta) + hw->sta_data_size, M_LKPI80211,
1039 M_NOWAIT | M_ZERO);
1040 if (lsta == NULL)
1041 return (NULL);
1042
1043 lsta->hw = hw;
1044 lsta->added_to_drv = false;
1045 lsta->state = IEEE80211_STA_NOTEXIST;
1046 /*
1047 * Link the ni to the lsta here without taking a reference.
1048 * For one we would have to take the reference in node_init()
1049 * as ieee80211_alloc_node() will initialise the refcount after us.
1050 * For the other a ni and an lsta are 1:1 mapped and always together
1051 * from [ic_]node_alloc() to [ic_]node_free() so we are essentally
1052 * using the ni references for the lsta as well despite it being
1053 * two separate allocations.
1054 */
1055 lsta->ni = ni;
1056 /* The back-pointer "drv_data" to net80211_node let's us get lsta. */
1057 ni->ni_drv_data = lsta;
1058
1059 lvif = VAP_TO_LVIF(vap);
1060 vif = LVIF_TO_VIF(lvif);
1061 sta = LSTA_TO_STA(lsta);
1062
1063 IEEE80211_ADDR_COPY(sta->addr, mac);
1064
1065 /* TXQ */
1066 for (tid = 0; tid < nitems(sta->txq); tid++) {
1067 struct lkpi_txq *ltxq;
1068
1069 /* We are not limiting ourselves to hw.queues here. */
1070 ltxq = malloc(sizeof(*ltxq) + hw->txq_data_size,
1071 M_LKPI80211, M_NOWAIT | M_ZERO);
1072 if (ltxq == NULL)
1073 goto cleanup;
1074 /* iwlwifi//mvm/sta.c::tid_to_mac80211_ac[] */
1075 if (tid == IEEE80211_NUM_TIDS) {
1076 if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ)) {
1077 free(ltxq, M_LKPI80211);
1078 continue;
1079 }
1080 IMPROVE("AP/if we support non-STA here too");
1081 ltxq->txq.ac = IEEE80211_AC_VO;
1082 } else {
1083 ltxq->txq.ac = ieee80211e_up_to_ac[tid & 7];
1084 }
1085 ltxq->flags = 0;
1086 ltxq->txq.vif = vif;
1087 ltxq->txq.tid = tid;
1088 ltxq->txq.sta = sta;
1089 TAILQ_ELEM_INIT(ltxq, txq_entry);
1090 skb_queue_head_init(<xq->skbq);
1091 LKPI_80211_LTXQ_LOCK_INIT(ltxq);
1092 sta->txq[tid] = <xq->txq;
1093 }
1094
1095 /* Deflink information. */
1096 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1097 struct ieee80211_supported_band *supband;
1098 uint32_t rate_mandatory;;
1099
1100 supband = hw->wiphy->bands[band];
1101 if (supband == NULL)
1102 continue;
1103
1104 switch (band) {
1105 case NL80211_BAND_2GHZ:
1106 /* We have to assume 11g support here. */
1107 rate_mandatory = IEEE80211_RATE_MANDATORY_G |
1108 IEEE80211_RATE_MANDATORY_B;
1109 break;
1110 case NL80211_BAND_5GHZ:
1111 rate_mandatory = IEEE80211_RATE_MANDATORY_A;
1112 break;
1113 default:
1114 continue;
1115 }
1116
1117 for (i = 0; i < supband->n_bitrates; i++) {
1118 if ((supband->bitrates[i].flags & rate_mandatory) != 0)
1119 sta->deflink.supp_rates[band] |= BIT(i);
1120 }
1121 }
1122
1123 sta->deflink.smps_mode = IEEE80211_SMPS_OFF;
1124 sta->deflink.bandwidth = IEEE80211_STA_RX_BW_20;
1125 sta->deflink.agg.max_rc_amsdu_len = IEEE80211_MAX_MPDU_LEN_HT_BA;
1126 sta->deflink.rx_nss = 1;
1127 sta->deflink.sta = sta;
1128
1129 (void)lkpi_sta_sync_from_ni(hw, vif, sta, ni, false);
1130
1131 IMPROVE("he, eht, bw_320, ... smps_mode, ..");
1132
1133 /* Link configuration. */
1134 IEEE80211_ADDR_COPY(sta->deflink.addr, sta->addr);
1135 sta->link[0] = &sta->deflink;
1136 for (i = 1; i < nitems(sta->link); i++) {
1137 IMPROVE("more links; only link[0] = deflink currently.");
1138 }
1139 IMPROVE("11be");
1140 sta->mlo = false;
1141
1142 /* Deferred TX path. */
1143 LKPI_80211_LSTA_TXQ_LOCK_INIT(lsta);
1144 TASK_INIT(&lsta->txq_task, 0, lkpi_80211_txq_task, lsta);
1145 mbufq_init(&lsta->txq, 32 * NAPI_POLL_WEIGHT);
1146 lsta->txq_ready = true;
1147
1148 return (lsta);
1149
1150 cleanup:
1151 for (; tid >= 0; tid--) {
1152 struct lkpi_txq *ltxq;
1153
1154 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
1155 LKPI_80211_LTXQ_LOCK_DESTROY(ltxq);
1156 free(sta->txq[tid], M_LKPI80211);
1157 }
1158 free(lsta, M_LKPI80211);
1159 return (NULL);
1160 }
1161
1162 static void
lkpi_lsta_free(struct lkpi_sta * lsta,struct ieee80211_node * ni)1163 lkpi_lsta_free(struct lkpi_sta *lsta, struct ieee80211_node *ni)
1164 {
1165 struct mbuf *m;
1166
1167 if (lsta->added_to_drv)
1168 panic("%s: Trying to free an lsta still known to firmware: "
1169 "lsta %p ni %p added_to_drv %d\n",
1170 __func__, lsta, ni, lsta->added_to_drv);
1171
1172 /* XXX-BZ free resources, ... */
1173 IMPROVE();
1174
1175 /* Drain sta->txq[] */
1176
1177 LKPI_80211_LSTA_TXQ_LOCK(lsta);
1178 lsta->txq_ready = false;
1179 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
1180
1181 /* Drain taskq, won't be restarted until added_to_drv is set again. */
1182 while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
1183 taskqueue_drain(taskqueue_thread, &lsta->txq_task);
1184
1185 /* Flush mbufq (make sure to release ni refs!). */
1186 m = mbufq_dequeue(&lsta->txq);
1187 while (m != NULL) {
1188 struct ieee80211_node *nim;
1189
1190 nim = (struct ieee80211_node *)m->m_pkthdr.rcvif;
1191 if (nim != NULL)
1192 ieee80211_free_node(nim);
1193 m_freem(m);
1194 m = mbufq_dequeue(&lsta->txq);
1195 }
1196 KASSERT(mbufq_empty(&lsta->txq), ("%s: lsta %p has txq len %d != 0\n",
1197 __func__, lsta, mbufq_len(&lsta->txq)));
1198 LKPI_80211_LSTA_TXQ_LOCK_DESTROY(lsta);
1199
1200 /* Remove lsta from vif; that is done by the state machine. Should assert it? */
1201
1202 IMPROVE("Make sure everything is cleaned up.");
1203
1204 /* Free lsta. */
1205 lsta->ni = NULL;
1206 ni->ni_drv_data = NULL;
1207 free(lsta, M_LKPI80211);
1208 }
1209
1210
1211 static enum nl80211_band
lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel * c)1212 lkpi_net80211_chan_to_nl80211_band(struct ieee80211_channel *c)
1213 {
1214
1215 if (IEEE80211_IS_CHAN_2GHZ(c))
1216 return (NL80211_BAND_2GHZ);
1217 else if (IEEE80211_IS_CHAN_5GHZ(c))
1218 return (NL80211_BAND_5GHZ);
1219 #ifdef __notyet__
1220 else if ()
1221 return (NL80211_BAND_6GHZ);
1222 else if ()
1223 return (NL80211_BAND_60GHZ);
1224 else if (IEEE80211_IS_CHAN_GSM(c))
1225 return (NL80211_BAND_XXX);
1226 #endif
1227 else
1228 panic("%s: unsupported band. c %p flags %#x\n",
1229 __func__, c, c->ic_flags);
1230 }
1231
1232 static uint32_t
lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)1233 lkpi_nl80211_band_to_net80211_band(enum nl80211_band band)
1234 {
1235
1236 /* XXX-BZ this is just silly; net80211 is too convoluted. */
1237 /* IEEE80211_CHAN_A / _G / .. doesn't really work either. */
1238 switch (band) {
1239 case NL80211_BAND_2GHZ:
1240 return (IEEE80211_CHAN_2GHZ);
1241 break;
1242 case NL80211_BAND_5GHZ:
1243 return (IEEE80211_CHAN_5GHZ);
1244 break;
1245 case NL80211_BAND_60GHZ:
1246 break;
1247 case NL80211_BAND_6GHZ:
1248 break;
1249 default:
1250 panic("%s: unsupported band %u\n", __func__, band);
1251 break;
1252 }
1253
1254 IMPROVE();
1255 return (0x00);
1256 }
1257
1258 #ifdef LINUXKPI_DEBUG_80211
1259 static const char *
lkpi_nl80211_band_name(enum nl80211_band band)1260 lkpi_nl80211_band_name(enum nl80211_band band)
1261 {
1262 switch (band) {
1263 case NL80211_BAND_2GHZ:
1264 return "2Ghz";
1265 break;
1266 case NL80211_BAND_5GHZ:
1267 return "5Ghz";
1268 break;
1269 case NL80211_BAND_60GHZ:
1270 return "60Ghz";
1271 break;
1272 case NL80211_BAND_6GHZ:
1273 return "6Ghz";
1274 break;
1275 default:
1276 panic("%s: unsupported band %u\n", __func__, band);
1277 break;
1278 }
1279 }
1280 #endif
1281
1282 #if 0
1283 static enum ieee80211_ac_numbers
1284 lkpi_ac_net_to_l80211(int ac)
1285 {
1286
1287 switch (ac) {
1288 case WME_AC_VO:
1289 return (IEEE80211_AC_VO);
1290 case WME_AC_VI:
1291 return (IEEE80211_AC_VI);
1292 case WME_AC_BE:
1293 return (IEEE80211_AC_BE);
1294 case WME_AC_BK:
1295 return (IEEE80211_AC_BK);
1296 default:
1297 printf("%s: invalid WME_AC_* input: ac = %d\n", __func__, ac);
1298 return (IEEE80211_AC_BE);
1299 }
1300 }
1301 #endif
1302
1303 static enum nl80211_iftype
lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)1304 lkpi_opmode_to_vif_type(enum ieee80211_opmode opmode)
1305 {
1306
1307 switch (opmode) {
1308 case IEEE80211_M_IBSS:
1309 return (NL80211_IFTYPE_ADHOC);
1310 break;
1311 case IEEE80211_M_STA:
1312 return (NL80211_IFTYPE_STATION);
1313 break;
1314 case IEEE80211_M_WDS:
1315 return (NL80211_IFTYPE_WDS);
1316 break;
1317 case IEEE80211_M_HOSTAP:
1318 return (NL80211_IFTYPE_AP);
1319 break;
1320 case IEEE80211_M_MONITOR:
1321 return (NL80211_IFTYPE_MONITOR);
1322 break;
1323 case IEEE80211_M_MBSS:
1324 return (NL80211_IFTYPE_MESH_POINT);
1325 break;
1326 case IEEE80211_M_AHDEMO:
1327 /* FALLTHROUGH */
1328 default:
1329 printf("ERROR: %s: unsupported opmode %d\n", __func__, opmode);
1330 /* FALLTHROUGH */
1331 }
1332 return (NL80211_IFTYPE_UNSPECIFIED);
1333 }
1334
1335 #ifdef LKPI_80211_HW_CRYPTO
1336 static const char *
lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)1337 lkpi_cipher_suite_to_name(uint32_t wlan_cipher_suite)
1338 {
1339 switch (wlan_cipher_suite) {
1340 case WLAN_CIPHER_SUITE_WEP40:
1341 return ("WEP40");
1342 case WLAN_CIPHER_SUITE_WEP104:
1343 return ("WEP104");
1344 case WLAN_CIPHER_SUITE_TKIP:
1345 return ("TKIP");
1346 case WLAN_CIPHER_SUITE_CCMP:
1347 return ("CCMP");
1348 case WLAN_CIPHER_SUITE_CCMP_256:
1349 return ("CCMP_256");
1350 case WLAN_CIPHER_SUITE_GCMP:
1351 return ("GCMP");
1352 case WLAN_CIPHER_SUITE_GCMP_256:
1353 return ("GCMP_256");
1354 case WLAN_CIPHER_SUITE_AES_CMAC:
1355 return ("AES_CMAC");
1356 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1357 return ("BIP_CMAC_256");
1358 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1359 return ("BIP_GMAC_128");
1360 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1361 return ("BIP_GMAC_256");
1362 case WLAN_CIPHER_SUITE_SMS4:
1363 return ("WPI-SMS4");
1364 default:
1365 return ("??");
1366 }
1367 }
1368
1369 static uint32_t
lkpi_l80211_to_net80211_cyphers(struct ieee80211com * ic,uint32_t wlan_cipher_suite)1370 lkpi_l80211_to_net80211_cyphers(struct ieee80211com *ic,
1371 uint32_t wlan_cipher_suite)
1372 {
1373 switch (wlan_cipher_suite) {
1374 case WLAN_CIPHER_SUITE_WEP40:
1375 return (IEEE80211_CRYPTO_WEP);
1376 case WLAN_CIPHER_SUITE_WEP104:
1377 return (IEEE80211_CRYPTO_WEP);
1378 case WLAN_CIPHER_SUITE_TKIP:
1379 return (IEEE80211_CRYPTO_TKIP);
1380 case WLAN_CIPHER_SUITE_CCMP:
1381 return (IEEE80211_CRYPTO_AES_CCM);
1382 case WLAN_CIPHER_SUITE_CCMP_256:
1383 return (IEEE80211_CRYPTO_AES_CCM_256);
1384 case WLAN_CIPHER_SUITE_GCMP:
1385 return (IEEE80211_CRYPTO_AES_GCM_128);
1386 case WLAN_CIPHER_SUITE_GCMP_256:
1387 return (IEEE80211_CRYPTO_AES_GCM_256);
1388 case WLAN_CIPHER_SUITE_AES_CMAC:
1389 return (IEEE80211_CRYPTO_BIP_CMAC_128);
1390 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
1391 return (IEEE80211_CRYPTO_BIP_CMAC_256);
1392 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
1393 return (IEEE80211_CRYPTO_BIP_GMAC_128);
1394 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
1395 return (IEEE80211_CRYPTO_BIP_GMAC_256);
1396 default:
1397 ic_printf(ic, "%s: unknown/unsupported WLAN Cipher Suite %#08x | %u (%s)\n",
1398 __func__,
1399 wlan_cipher_suite >> 8, wlan_cipher_suite & 0xff,
1400 lkpi_cipher_suite_to_name(wlan_cipher_suite));
1401 return (0);
1402 }
1403 }
1404
1405 static uint32_t
lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher,uint8_t keylen)1406 lkpi_net80211_to_l80211_cipher_suite(uint32_t cipher, uint8_t keylen)
1407 {
1408
1409 switch (cipher) {
1410 case IEEE80211_CIPHER_WEP:
1411 if (keylen == (40/NBBY))
1412 return (WLAN_CIPHER_SUITE_WEP40);
1413 else if (keylen == (104/NBBY))
1414 return (WLAN_CIPHER_SUITE_WEP104);
1415 else {
1416 printf("%s: WEP with unsupported keylen %d\n",
1417 __func__, keylen * NBBY);
1418 return (0);
1419 }
1420 break;
1421 case IEEE80211_CIPHER_TKIP:
1422 return (WLAN_CIPHER_SUITE_TKIP);
1423 case IEEE80211_CIPHER_AES_CCM:
1424 return (WLAN_CIPHER_SUITE_CCMP);
1425 case IEEE80211_CIPHER_AES_CCM_256:
1426 return (WLAN_CIPHER_SUITE_CCMP_256);
1427 case IEEE80211_CIPHER_AES_GCM_128:
1428 return (WLAN_CIPHER_SUITE_GCMP);
1429 case IEEE80211_CIPHER_AES_GCM_256:
1430 return (WLAN_CIPHER_SUITE_GCMP_256);
1431 case IEEE80211_CIPHER_BIP_CMAC_128:
1432 return (WLAN_CIPHER_SUITE_AES_CMAC);
1433 case IEEE80211_CIPHER_BIP_CMAC_256:
1434 return (WLAN_CIPHER_SUITE_BIP_CMAC_256);
1435 case IEEE80211_CIPHER_BIP_GMAC_128:
1436 return (WLAN_CIPHER_SUITE_BIP_GMAC_128);
1437 case IEEE80211_CIPHER_BIP_GMAC_256:
1438 return (WLAN_CIPHER_SUITE_BIP_GMAC_256);
1439
1440 case IEEE80211_CIPHER_AES_OCB:
1441 case IEEE80211_CIPHER_TKIPMIC:
1442 /*
1443 * TKIP w/ hw MIC support
1444 * (gone wrong; should really be a crypto flag in net80211).
1445 */
1446 case IEEE80211_CIPHER_CKIP:
1447 case IEEE80211_CIPHER_NONE:
1448 printf("%s: unsupported cipher %#010x\n", __func__, cipher);
1449 break;
1450 default:
1451 printf("%s: unknown cipher %#010x\n", __func__, cipher);
1452 };
1453 return (0);
1454 }
1455 #endif
1456
1457 #ifdef __notyet__
1458 static enum ieee80211_sta_state
lkpi_net80211_state_to_sta_state(enum ieee80211_state state)1459 lkpi_net80211_state_to_sta_state(enum ieee80211_state state)
1460 {
1461
1462 /*
1463 * XXX-BZ The net80211 states are "try to ..", the lkpi8011 states are
1464 * "done". Also ASSOC/AUTHORIZED are both "RUN" then?
1465 */
1466 switch (state) {
1467 case IEEE80211_S_INIT:
1468 return (IEEE80211_STA_NOTEXIST);
1469 case IEEE80211_S_SCAN:
1470 return (IEEE80211_STA_NONE);
1471 case IEEE80211_S_AUTH:
1472 return (IEEE80211_STA_AUTH);
1473 case IEEE80211_S_ASSOC:
1474 return (IEEE80211_STA_ASSOC);
1475 case IEEE80211_S_RUN:
1476 return (IEEE80211_STA_AUTHORIZED);
1477 case IEEE80211_S_CAC:
1478 case IEEE80211_S_CSA:
1479 case IEEE80211_S_SLEEP:
1480 default:
1481 UNIMPLEMENTED;
1482 };
1483
1484 return (IEEE80211_STA_NOTEXIST);
1485 }
1486 #endif
1487
1488 static struct linuxkpi_ieee80211_channel *
lkpi_find_lkpi80211_chan(struct lkpi_hw * lhw,struct ieee80211_channel * c)1489 lkpi_find_lkpi80211_chan(struct lkpi_hw *lhw,
1490 struct ieee80211_channel *c)
1491 {
1492 struct ieee80211_hw *hw;
1493 struct linuxkpi_ieee80211_channel *channels;
1494 enum nl80211_band band;
1495 int i, nchans;
1496
1497 hw = LHW_TO_HW(lhw);
1498 band = lkpi_net80211_chan_to_nl80211_band(c);
1499 if (hw->wiphy->bands[band] == NULL)
1500 return (NULL);
1501
1502 nchans = hw->wiphy->bands[band]->n_channels;
1503 if (nchans <= 0)
1504 return (NULL);
1505
1506 channels = hw->wiphy->bands[band]->channels;
1507 for (i = 0; i < nchans; i++) {
1508 if (channels[i].center_freq == c->ic_freq)
1509 return (&channels[i]);
1510 }
1511
1512 return (NULL);
1513 }
1514
1515 #if 0
1516 static struct linuxkpi_ieee80211_channel *
1517 lkpi_get_lkpi80211_chan(struct ieee80211com *ic, struct ieee80211_node *ni)
1518 {
1519 struct linuxkpi_ieee80211_channel *chan;
1520 struct ieee80211_channel *c;
1521 struct lkpi_hw *lhw;
1522
1523 chan = NULL;
1524 if (ni != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC)
1525 c = ni->ni_chan;
1526 else if (ic->ic_bsschan != IEEE80211_CHAN_ANYC)
1527 c = ic->ic_bsschan;
1528 else if (ic->ic_curchan != IEEE80211_CHAN_ANYC)
1529 c = ic->ic_curchan;
1530 else
1531 c = NULL;
1532
1533 if (c != NULL && c != IEEE80211_CHAN_ANYC) {
1534 lhw = ic->ic_softc;
1535 chan = lkpi_find_lkpi80211_chan(lhw, c);
1536 }
1537
1538 return (chan);
1539 }
1540 #endif
1541
1542 struct linuxkpi_ieee80211_channel *
linuxkpi_ieee80211_get_channel(struct wiphy * wiphy,uint32_t freq)1543 linuxkpi_ieee80211_get_channel(struct wiphy *wiphy, uint32_t freq)
1544 {
1545 enum nl80211_band band;
1546
1547 for (band = 0; band < NUM_NL80211_BANDS; band++) {
1548 struct ieee80211_supported_band *supband;
1549 struct linuxkpi_ieee80211_channel *channels;
1550 int i;
1551
1552 supband = wiphy->bands[band];
1553 if (supband == NULL || supband->n_channels == 0)
1554 continue;
1555
1556 channels = supband->channels;
1557 for (i = 0; i < supband->n_channels; i++) {
1558 if (channels[i].center_freq == freq)
1559 return (&channels[i]);
1560 }
1561 }
1562
1563 return (NULL);
1564 }
1565
1566 #ifdef LKPI_80211_HW_CRYPTO
1567 static int
lkpi_sta_del_keys(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct lkpi_sta * lsta)1568 lkpi_sta_del_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
1569 struct lkpi_sta *lsta)
1570 {
1571 int error;
1572
1573 if (!lkpi_hwcrypto)
1574 return (0);
1575
1576 lockdep_assert_wiphy(hw->wiphy);
1577
1578 if (vif->cfg.assoc && lsta->state == IEEE80211_STA_AUTHORIZED) {
1579 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1580 ic_printf(lsta->ni->ni_ic,
1581 "%d %lu %s: vif still assoc; not deleting keys\n",
1582 curthread->td_tid, jiffies, __func__);
1583 return (0);
1584 }
1585
1586 ieee80211_ref_node(lsta->ni);
1587
1588 error = 0;
1589 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc); keyix++) {
1590 struct ieee80211_key_conf *kc;
1591 int err;
1592
1593 if (lsta->kc[keyix] == NULL)
1594 continue;
1595 kc = lsta->kc[keyix];
1596
1597 #ifdef LINUXKPI_DEBUG_80211
1598 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1599 ic_printf(lsta->ni->ni_ic, "%d %lu %s: running set_key cmd %d(%s) for "
1600 "sta %6D: keyidx %u hw_key_idx %u flags %b\n",
1601 curthread->td_tid, jiffies, __func__,
1602 DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1603 kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1604 #endif
1605
1606 err = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif,
1607 LSTA_TO_STA(lsta), kc);
1608 if (err != 0) {
1609 ic_printf(lsta->ni->ni_ic, "%d %lu %s: set_key cmd %d(%s) for "
1610 "sta %6D failed: %d\n", curthread->td_tid, jiffies, __func__,
1611 DISABLE_KEY, "DISABLE", lsta->sta.addr, ":", err);
1612 error++;
1613
1614 /*
1615 * If we free the key here we will never be able to get it
1616 * removed from the driver/fw which will likely make us
1617 * crash (firmware).
1618 */
1619 continue;
1620 }
1621 #ifdef LINUXKPI_DEBUG_80211
1622 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1623 ic_printf(lsta->ni->ni_ic, "%d %lu %s: set_key cmd %d(%s) for "
1624 "sta %6D succeeded: keyidx %u hw_key_idx %u flags %b\n",
1625 curthread->td_tid, jiffies, __func__,
1626 DISABLE_KEY, "DISABLE", lsta->sta.addr, ":",
1627 kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1628 #endif
1629
1630 lsta->kc[keyix] = NULL;
1631 free(kc, M_LKPI80211);
1632 }
1633 ieee80211_free_node(lsta->ni);
1634 return (error);
1635 }
1636
1637 /* XXX-BZ one day we should replace this iterating over VIFs, or node list? */
1638 /* See also lkpi_sta_del_keys() these days. */
1639 static int
lkpi_iv_key_delete(struct ieee80211vap * vap,const struct ieee80211_key * k)1640 lkpi_iv_key_delete(struct ieee80211vap *vap, const struct ieee80211_key *k)
1641 {
1642 struct ieee80211com *ic;
1643 struct lkpi_hw *lhw;
1644 struct ieee80211_hw *hw;
1645 struct lkpi_vif *lvif;
1646 struct lkpi_sta *lsta;
1647 struct ieee80211_vif *vif;
1648 struct ieee80211_sta *sta;
1649 struct ieee80211_node *ni;
1650 struct ieee80211_key_conf *kc;
1651 int error;
1652
1653 ic = vap->iv_ic;
1654 lhw = ic->ic_softc;
1655 hw = LHW_TO_HW(lhw);
1656 lvif = VAP_TO_LVIF(vap);
1657 vif = LVIF_TO_VIF(lvif);
1658
1659 /*
1660 * Make sure we do not make it here without going through
1661 * lkpi_iv_key_update_begin() first.
1662 */
1663 lockdep_assert_wiphy(hw->wiphy);
1664
1665 ni = ieee80211_ref_node(vap->iv_bss);
1666 lsta = ni->ni_drv_data;
1667 if (lsta == NULL) {
1668 ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
1669 __func__, ni, ni->ni_bssid, ":");
1670 ieee80211_free_node(ni);
1671 return (0);
1672 }
1673
1674 /*
1675 * While we are assoc we may still send packets. We cannot delete the
1676 * keys as otherwise packets could go out unencrypted. Some firmware
1677 * does not like this and will fire an assert.
1678 * net80211 needs to drive this better but given we want the disassoc
1679 * frame out and have to unlock we are open to a race currently.
1680 * This check should prevent problems.
1681 * How to test: run 800Mbit/s UDP traffic and during that restart your
1682 * supplicant. You want to survive that.
1683 */
1684 if (vif->cfg.assoc && lsta->state == IEEE80211_STA_AUTHORIZED) {
1685 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1686 ic_printf(ic, "%d %lu %s: vif still assoc; not deleting keys\n",
1687 curthread->td_tid, jiffies, __func__);
1688 ieee80211_free_node(ni);
1689 return (0);
1690 }
1691
1692 if (IEEE80211_KEY_UNDEFINED(k)) {
1693 ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1694 __func__, vap, k, k->wk_cipher, k->wk_keyix);
1695 ieee80211_free_node(ni);
1696 return (0);
1697 }
1698
1699 if (vap->iv_bss == NULL) {
1700 ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
1701 __func__, vap->iv_bss, vap);
1702 ieee80211_free_node(ni);
1703 return (0);
1704 }
1705 sta = LSTA_TO_STA(lsta);
1706
1707 if (lsta->kc[k->wk_keyix] == NULL) {
1708 #ifdef LINUXKPI_DEBUG_80211
1709 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1710 ic_printf(ic, "%d %lu %s: sta %6D and no key information, "
1711 "keyidx %u wk_macaddr %6D; returning success\n",
1712 curthread->td_tid, jiffies, __func__, sta->addr, ":",
1713 k->wk_keyix, k->wk_macaddr, ":");
1714 #endif
1715 ieee80211_free_node(ni);
1716 return (1);
1717 }
1718 kc = lsta->kc[k->wk_keyix];
1719
1720 #ifdef LINUXKPI_DEBUG_80211
1721 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1722 ic_printf(ic, "%d %lu %s: running set_key cmd %d(%s) for sta %6D: "
1723 "keyidx %u hw_key_idx %u flags %b\n",
1724 curthread->td_tid, jiffies, __func__,
1725 DISABLE_KEY, "DISABLE", sta->addr, ":",
1726 kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1727 #endif
1728
1729 error = lkpi_80211_mo_set_key(hw, DISABLE_KEY, vif, sta, kc);
1730 if (error != 0) {
1731 ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D failed: %d\n",
1732 curthread->td_tid, jiffies, __func__,
1733 DISABLE_KEY, "DISABLE", sta->addr, ":", error);
1734 error = 0;
1735 goto out;
1736 }
1737
1738 #ifdef LINUXKPI_DEBUG_80211
1739 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1740 ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D succeeded: "
1741 "keyidx %u hw_key_idx %u flags %b\n",
1742 curthread->td_tid, jiffies, __func__,
1743 DISABLE_KEY, "DISABLE", sta->addr, ":",
1744 kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1745 #endif
1746 lsta->kc[k->wk_keyix] = NULL;
1747 free(kc, M_LKPI80211);
1748 error = 1;
1749 out:
1750 ieee80211_free_node(ni);
1751 return (error);
1752 }
1753
1754 static int
lkpi_iv_key_set(struct ieee80211vap * vap,const struct ieee80211_key * k)1755 lkpi_iv_key_set(struct ieee80211vap *vap, const struct ieee80211_key *k)
1756 {
1757 struct ieee80211com *ic;
1758 struct lkpi_hw *lhw;
1759 struct ieee80211_hw *hw;
1760 struct lkpi_vif *lvif;
1761 struct lkpi_sta *lsta;
1762 struct ieee80211_vif *vif;
1763 struct ieee80211_sta *sta;
1764 struct ieee80211_node *ni;
1765 struct ieee80211_key_conf *kc;
1766 struct ieee80211_key *wk;
1767 uint32_t lcipher;
1768 uint16_t exp_flags;
1769 uint8_t keylen;
1770 int error;
1771
1772 ic = vap->iv_ic;
1773 lhw = ic->ic_softc;
1774 hw = LHW_TO_HW(lhw);
1775
1776 /*
1777 * Make sure we do not make it here without going through
1778 * lkpi_iv_key_update_begin() first.
1779 */
1780 lockdep_assert_wiphy(hw->wiphy);
1781
1782 if (IEEE80211_KEY_UNDEFINED(k)) {
1783 ic_printf(ic, "%s: vap %p key %p is undefined: %p %u\n",
1784 __func__, vap, k, k->wk_cipher, k->wk_keyix);
1785 return (0);
1786 }
1787
1788 if (vap->iv_bss == NULL) {
1789 ic_printf(ic, "%s: iv_bss %p for vap %p is NULL\n",
1790 __func__, vap->iv_bss, vap);
1791 return (0);
1792 }
1793 ni = ieee80211_ref_node(vap->iv_bss);
1794 lsta = ni->ni_drv_data;
1795 if (lsta == NULL) {
1796 ic_printf(ic, "%s: ni %p (%6D) with lsta NULL\n",
1797 __func__, ni, ni->ni_bssid, ":");
1798 ieee80211_free_node(ni);
1799 return (0);
1800 }
1801 sta = LSTA_TO_STA(lsta);
1802
1803 keylen = ieee80211_crypto_get_key_len(k);
1804 lcipher = lkpi_net80211_to_l80211_cipher_suite(
1805 k->wk_cipher->ic_cipher, ieee80211_crypto_get_key_len(k));
1806 switch (lcipher) {
1807 case WLAN_CIPHER_SUITE_TKIP:
1808 keylen += ieee80211_crypto_get_key_txmic_len(k);
1809 keylen += ieee80211_crypto_get_key_rxmic_len(k);
1810 break;
1811 case WLAN_CIPHER_SUITE_CCMP:
1812 case WLAN_CIPHER_SUITE_GCMP:
1813 break;
1814 default:
1815 ic_printf(ic, "%s: CIPHER SUITE %#x (%s) not supported\n",
1816 __func__, lcipher, lkpi_cipher_suite_to_name(lcipher));
1817 IMPROVE();
1818 ieee80211_free_node(ni);
1819 return (0);
1820 }
1821
1822 if (lsta->kc[k->wk_keyix] != NULL) {
1823 IMPROVE("Still in firmware? Del first. Can we assert this cannot happen?");
1824 ic_printf(ic, "%s: sta %6D found with key information\n",
1825 __func__, sta->addr, ":");
1826 kc = lsta->kc[k->wk_keyix];
1827 lsta->kc[k->wk_keyix] = NULL;
1828 free(kc, M_LKPI80211);
1829 kc = NULL; /* safeguard */
1830 }
1831
1832 kc = malloc(sizeof(*kc) + keylen, M_LKPI80211, M_WAITOK | M_ZERO);
1833 kc->_k = k; /* Save the pointer to net80211. */
1834 kc->cipher = lcipher;
1835 kc->keyidx = k->wk_keyix;
1836 #if 0
1837 kc->hw_key_idx = /* set by hw and needs to be passed for TX */;
1838 #endif
1839 atomic64_set(&kc->tx_pn, k->wk_keytsc);
1840 kc->keylen = ieee80211_crypto_get_key_len(k);
1841 memcpy(kc->key, ieee80211_crypto_get_key_data(k),
1842 ieee80211_crypto_get_key_len(k));
1843
1844 if (k->wk_flags & (IEEE80211_KEY_XMIT | IEEE80211_KEY_RECV))
1845 kc->flags |= IEEE80211_KEY_FLAG_PAIRWISE;
1846 if (k->wk_flags & IEEE80211_KEY_GROUP)
1847 kc->flags &= ~IEEE80211_KEY_FLAG_PAIRWISE;
1848
1849 kc->iv_len = k->wk_cipher->ic_header;
1850 kc->icv_len = k->wk_cipher->ic_trailer;
1851
1852 switch (kc->cipher) {
1853 case WLAN_CIPHER_SUITE_TKIP:
1854 memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY,
1855 ieee80211_crypto_get_key_txmic_data(k),
1856 ieee80211_crypto_get_key_txmic_len(k));
1857 memcpy(kc->key + NL80211_TKIP_DATA_OFFSET_RX_MIC_KEY,
1858 ieee80211_crypto_get_key_rxmic_data(k),
1859 ieee80211_crypto_get_key_rxmic_len(k));
1860 break;
1861 case WLAN_CIPHER_SUITE_CCMP:
1862 case WLAN_CIPHER_SUITE_GCMP:
1863 break;
1864 default:
1865 /* currently UNREACH */
1866 IMPROVE();
1867 break;
1868 };
1869 lsta->kc[k->wk_keyix] = kc;
1870
1871 #ifdef LINUXKPI_DEBUG_80211
1872 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1873 ic_printf(ic, "%d %lu %s: running set_key cmd %d(%s) for sta %6D: "
1874 "kc %p keyidx %u hw_key_idx %u keylen %u flags %b\n",
1875 curthread->td_tid, jiffies, __func__,
1876 SET_KEY, "SET", sta->addr, ":", kc, kc->keyidx, kc->hw_key_idx,
1877 kc->keylen, kc->flags, IEEE80211_KEY_FLAG_BITS);
1878 #endif
1879
1880 lvif = VAP_TO_LVIF(vap);
1881 vif = LVIF_TO_VIF(lvif);
1882 error = lkpi_80211_mo_set_key(hw, SET_KEY, vif, sta, kc);
1883 if (error != 0) {
1884 ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D failed: %d\n",
1885 curthread->td_tid, jiffies, __func__,
1886 SET_KEY, "SET", sta->addr, ":", error);
1887 lsta->kc[k->wk_keyix] = NULL;
1888 free(kc, M_LKPI80211);
1889 ieee80211_free_node(ni);
1890 return (0);
1891 }
1892
1893 #ifdef LINUXKPI_DEBUG_80211
1894 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1895 ic_printf(ic, "%d %lu %s: set_key cmd %d(%s) for sta %6D succeeded: "
1896 "kc %p keyidx %u hw_key_idx %u flags %b\n",
1897 curthread->td_tid, jiffies, __func__,
1898 SET_KEY, "SET", sta->addr, ":",
1899 kc, kc->keyidx, kc->hw_key_idx, kc->flags, IEEE80211_KEY_FLAG_BITS);
1900 #endif
1901
1902 /*
1903 * Getting here means we support HW crypto offload.
1904 * Some drivers do not set the wiphy [n_]cipher_suites and thus we
1905 * never populate ic_cryptocaps. which means SWCRYPT will be set and we
1906 * should disable this now (before possibly setting other SW flags
1907 * again for when we need partial SW support).
1908 */
1909 wk = __DECONST(struct ieee80211_key *, k);
1910 wk->wk_flags &= ~IEEE80211_KEY_SWCRYPT;
1911
1912 exp_flags = 0;
1913 switch (kc->cipher) {
1914 case WLAN_CIPHER_SUITE_TKIP:
1915 exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
1916 IEEE80211_KEY_FLAG_PUT_IV_SPACE |
1917 IEEE80211_KEY_FLAG_GENERATE_MMIC |
1918 IEEE80211_KEY_FLAG_PUT_MIC_SPACE);
1919 #define TKIP_INVAL_COMBINATION \
1920 (IEEE80211_KEY_FLAG_PUT_MIC_SPACE|IEEE80211_KEY_FLAG_GENERATE_MMIC)
1921 if ((kc->flags & TKIP_INVAL_COMBINATION) == TKIP_INVAL_COMBINATION) {
1922 ic_printf(ic, "%s: SET_KEY for %s returned invalid "
1923 "combination %b\n", __func__,
1924 lkpi_cipher_suite_to_name(kc->cipher),
1925 kc->flags, IEEE80211_KEY_FLAG_BITS);
1926 }
1927 #undef TKIP_INVAL_COMBINATION
1928 #ifdef __notyet__
1929 /* Do flags surgery; special see linuxkpi_ieee80211_ifattach(). */
1930 if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_MMIC) != 0) {
1931 wk->wk_flags &= ~(IEEE80211_KEY_NOMICMGT|IEEE80211_KEY_NOMIC);
1932 wk->wk_flags |= IEEE80211_KEY_SWMIC;
1933 ic->ic_cryptocaps &= ~IEEE80211_CRYPTO_TKIPMIC
1934 }
1935 #endif
1936 break;
1937 case WLAN_CIPHER_SUITE_CCMP:
1938 case WLAN_CIPHER_SUITE_GCMP:
1939 exp_flags = (IEEE80211_KEY_FLAG_PAIRWISE |
1940 IEEE80211_KEY_FLAG_PUT_IV_SPACE |
1941 IEEE80211_KEY_FLAG_GENERATE_IV |
1942 IEEE80211_KEY_FLAG_GENERATE_IV_MGMT | /* Only needs IV geeration for MGMT frames. */
1943 IEEE80211_KEY_FLAG_SW_MGMT_TX); /* MFP in software */
1944 break;
1945 }
1946 if ((kc->flags & ~exp_flags) != 0)
1947 ic_printf(ic, "%s: SET_KEY for %s returned unexpected key flags: "
1948 " %#06x & ~%#06x = %b\n", __func__,
1949 lkpi_cipher_suite_to_name(kc->cipher), kc->flags, exp_flags,
1950 (kc->flags & ~exp_flags), IEEE80211_KEY_FLAG_BITS);
1951
1952 #ifdef __notyet__
1953 /* Do flags surgery. */
1954 if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) == 0)
1955 wk->wk_flags |= IEEE80211_KEY_NOIVMGT;
1956 if ((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
1957 wk->wk_flags |= IEEE80211_KEY_NOIV;
1958 #endif
1959
1960 ieee80211_free_node(ni);
1961 return (1);
1962 }
1963
1964 static void
lkpi_iv_key_update_begin(struct ieee80211vap * vap)1965 lkpi_iv_key_update_begin(struct ieee80211vap *vap)
1966 {
1967 struct ieee80211_node_table *nt;
1968 struct ieee80211com *ic;
1969 struct lkpi_hw *lhw;
1970 struct ieee80211_hw *hw;
1971 struct lkpi_vif *lvif;
1972 struct ieee80211_node *ni;
1973 bool icislocked, ntislocked;
1974
1975 ic = vap->iv_ic;
1976 lhw = ic->ic_softc;
1977 hw = LHW_TO_HW(lhw);
1978 lvif = VAP_TO_LVIF(vap);
1979 nt = &ic->ic_sta;
1980
1981 icislocked = IEEE80211_IS_LOCKED(ic);
1982 ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
1983
1984 #ifdef LINUXKPI_DEBUG_80211
1985 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
1986 ic_printf(ic, "%d %lu %s: vap %p ic %p %slocked nt %p %slocked "
1987 "lvif ic_unlocked %d nt_unlocked %d\n",
1988 curthread->td_tid, jiffies, __func__, vap,
1989 ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
1990 lvif->ic_unlocked, lvif->nt_unlocked);
1991 #endif
1992
1993 /*
1994 * This is inconsistent net80211 locking to be fixed one day.
1995 */
1996 /* Try to make sure the node does not go away while possibly unlocked. */
1997 ni = NULL;
1998 if (icislocked || ntislocked) {
1999 if (vap->iv_bss != NULL)
2000 ni = ieee80211_ref_node(vap->iv_bss);
2001 }
2002
2003 if (icislocked)
2004 IEEE80211_UNLOCK(ic);
2005 if (ntislocked)
2006 IEEE80211_NODE_UNLOCK(nt);
2007
2008 wiphy_lock(hw->wiphy);
2009
2010 KASSERT(lvif->key_update_iv_bss == NULL, ("%s: key_update_iv_bss not NULL %p",
2011 __func__, lvif->key_update_iv_bss));
2012 lvif->key_update_iv_bss = ni;
2013
2014 /*
2015 * ic/nt_unlocked could be a bool given we are under the lock and there
2016 * must only be a single thread.
2017 * In case anything in the future disturbs the order the refcnt will
2018 * help us catching problems a lot easier.
2019 */
2020 if (icislocked)
2021 refcount_acquire(&lvif->ic_unlocked);
2022 if (ntislocked)
2023 refcount_acquire(&lvif->nt_unlocked);
2024
2025 /*
2026 * Stop the queues while doing key updates.
2027 */
2028 ieee80211_stop_queues(hw);
2029 }
2030
2031 static void
lkpi_iv_key_update_end(struct ieee80211vap * vap)2032 lkpi_iv_key_update_end(struct ieee80211vap *vap)
2033 {
2034 struct ieee80211_node_table *nt;
2035 struct ieee80211com *ic;
2036 struct lkpi_hw *lhw;
2037 struct ieee80211_hw *hw;
2038 struct lkpi_vif *lvif;
2039 bool icislocked, ntislocked;
2040
2041 ic = vap->iv_ic;
2042 lhw = ic->ic_softc;
2043 hw = LHW_TO_HW(lhw);
2044 lvif = VAP_TO_LVIF(vap);
2045 nt = &ic->ic_sta;
2046
2047 /*
2048 * Re-enabled the queues after the key update.
2049 */
2050 lkpi_ieee80211_wake_queues_locked(hw);
2051
2052 icislocked = IEEE80211_IS_LOCKED(ic);
2053 MPASS(!icislocked);
2054 ntislocked = IEEE80211_NODE_IS_LOCKED(nt);
2055 MPASS(!ntislocked);
2056
2057 #ifdef LINUXKPI_DEBUG_80211
2058 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
2059 ic_printf(ic, "%d %lu %s: vap %p ic %p %slocked nt %p %slocked "
2060 "lvif ic_unlocked %d nt_unlocked %d\n",
2061 curthread->td_tid, jiffies, __func__, vap,
2062 ic, icislocked ? "" : "un", nt, ntislocked ? "" : "un",
2063 lvif->ic_unlocked, lvif->nt_unlocked);
2064 #endif
2065
2066 /*
2067 * Check under lock; see comment in lkpi_iv_key_update_begin().
2068 * In case the refcnt gets out of sync locking in net80211 will
2069 * quickly barf as well (trying to unlock a lock not held).
2070 */
2071 icislocked = refcount_release_if_last(&lvif->ic_unlocked);
2072 ntislocked = refcount_release_if_last(&lvif->nt_unlocked);
2073
2074 if (lvif->key_update_iv_bss != NULL) {
2075 ieee80211_free_node(lvif->key_update_iv_bss);
2076 lvif->key_update_iv_bss = NULL;
2077 }
2078
2079 wiphy_unlock(hw->wiphy);
2080
2081 /*
2082 * This is inconsistent net80211 locking to be fixed one day.
2083 * ic before nt to avoid a LOR.
2084 */
2085 if (icislocked)
2086 IEEE80211_LOCK(ic);
2087 if (ntislocked)
2088 IEEE80211_NODE_LOCK(nt);
2089 }
2090 #endif
2091
2092 static void
lkpi_cleanup_mcast_list_locked(struct lkpi_hw * lhw)2093 lkpi_cleanup_mcast_list_locked(struct lkpi_hw *lhw)
2094 {
2095 struct list_head *le, *next;
2096 struct netdev_hw_addr *addr;
2097
2098 if (lhw->mc_list.count != 0) {
2099 list_for_each_safe(le, next, &lhw->mc_list.addr_list) {
2100 addr = list_entry(le, struct netdev_hw_addr, addr_list);
2101 list_del(le);
2102 lhw->mc_list.count--;
2103 free(addr, M_LKPI80211);
2104 }
2105 }
2106 KASSERT(lhw->mc_list.count == 0, ("%s: mc_list %p count %d != 0\n",
2107 __func__, &lhw->mc_list, lhw->mc_list.count));
2108 }
2109
2110 static u_int
lkpi_ic_update_mcast_copy(void * arg,struct sockaddr_dl * sdl,u_int cnt)2111 lkpi_ic_update_mcast_copy(void *arg, struct sockaddr_dl *sdl, u_int cnt)
2112 {
2113 struct netdev_hw_addr_list *mc_list;
2114 struct netdev_hw_addr *addr;
2115
2116 KASSERT(arg != NULL && sdl != NULL, ("%s: arg %p sdl %p cnt %u\n",
2117 __func__, arg, sdl, cnt));
2118
2119 mc_list = arg;
2120 /* If it is on the list already skip it. */
2121 netdev_hw_addr_list_for_each(addr, mc_list) {
2122 if (!memcmp(addr->addr, LLADDR(sdl), sdl->sdl_alen))
2123 return (0);
2124 }
2125
2126 addr = malloc(sizeof(*addr), M_LKPI80211, M_NOWAIT | M_ZERO);
2127 if (addr == NULL)
2128 return (0);
2129
2130 INIT_LIST_HEAD(&addr->addr_list);
2131 memcpy(addr->addr, LLADDR(sdl), sdl->sdl_alen);
2132 /* XXX this should be a netdev function? */
2133 list_add(&addr->addr_list, &mc_list->addr_list);
2134 mc_list->count++;
2135
2136 #ifdef LINUXKPI_DEBUG_80211
2137 if (linuxkpi_debug_80211 & D80211_TRACE)
2138 printf("%s:%d: mc_list count %d: added %6D\n",
2139 __func__, __LINE__, mc_list->count, addr->addr, ":");
2140 #endif
2141
2142 return (1);
2143 }
2144
2145 static void
lkpi_update_mcast_filter_locked(struct ieee80211com * ic)2146 lkpi_update_mcast_filter_locked(struct ieee80211com *ic)
2147 {
2148 struct lkpi_hw *lhw;
2149 struct ieee80211_hw *hw;
2150 u64 mc;
2151 unsigned int changed_flags, flags;
2152 bool scanning;
2153
2154 lhw = ic->ic_softc;
2155 hw = LHW_TO_HW(lhw);
2156
2157 lockdep_assert_wiphy(hw->wiphy);
2158
2159 LKPI_80211_LHW_SCAN_LOCK(lhw);
2160 scanning = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
2161 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2162
2163 LKPI_80211_LHW_MC_LOCK(lhw);
2164
2165 flags = 0;
2166 if (scanning)
2167 flags |= FIF_BCN_PRBRESP_PROMISC;
2168 /* The latter condition may not be as expected but seems wise. */
2169 if (lhw->mc_all_multi || lhw->ops->prepare_multicast == NULL)
2170 flags |= FIF_ALLMULTI;
2171
2172 mc = lkpi_80211_mo_prepare_multicast(hw, &lhw->mc_list);
2173
2174 changed_flags = (lhw->mc_flags ^ flags) & FIF_FLAGS_MASK;
2175 lkpi_80211_mo_configure_filter(hw, changed_flags, &flags, mc);
2176 lhw->mc_flags = flags;
2177
2178 #ifdef LINUXKPI_DEBUG_80211
2179 if (linuxkpi_debug_80211 & D80211_TRACE)
2180 printf("%s: changed_flags %#06x count %d mc_flags %#010x\n",
2181 __func__, changed_flags, lhw->mc_list.count, lhw->mc_flags);
2182 #endif
2183
2184 LKPI_80211_LHW_MC_UNLOCK(lhw);
2185 }
2186
2187 static void
lkpi_update_mcast_filter(struct ieee80211com * ic)2188 lkpi_update_mcast_filter(struct ieee80211com *ic)
2189 {
2190 struct lkpi_hw *lhw;
2191 struct ieee80211_hw *hw;
2192
2193 lhw = ic->ic_softc;
2194 hw = LHW_TO_HW(lhw);
2195
2196 wiphy_lock(hw->wiphy);
2197 lkpi_update_mcast_filter_locked(ic);
2198 wiphy_unlock(hw->wiphy);
2199 }
2200
2201 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)2202 lkpi_update_dtim_tsf(struct ieee80211_vif *vif, struct ieee80211_node *ni,
2203 struct ieee80211vap *vap, const char *_f, int _l)
2204 {
2205 enum ieee80211_bss_changed bss_changed;
2206
2207 bss_changed = 0;
2208
2209 #ifdef LINUXKPI_DEBUG_80211
2210 if (linuxkpi_debug_80211 & D80211_TRACE)
2211 printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
2212 "dtim_period %u sync_dtim_count %u sync_tsf %ju "
2213 "sync_device_ts %u bss_changed %#010jx\n",
2214 __func__, __LINE__, _f, _l,
2215 vif->cfg.assoc, vif->cfg.aid,
2216 vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
2217 vif->bss_conf.sync_dtim_count,
2218 (uintmax_t)vif->bss_conf.sync_tsf,
2219 vif->bss_conf.sync_device_ts,
2220 (uintmax_t)bss_changed);
2221 #endif
2222
2223 if (vif->bss_conf.beacon_int != ni->ni_intval) {
2224 vif->bss_conf.beacon_int = ni->ni_intval;
2225 /* iwlwifi FW bug workaround; iwl_mvm_mac_sta_state. */
2226 if (vif->bss_conf.beacon_int < 16)
2227 vif->bss_conf.beacon_int = 16;
2228 bss_changed |= BSS_CHANGED_BEACON_INT;
2229 }
2230
2231 /*
2232 * lkpi_iv_sta_recv_mgmt() will directly call into this function.
2233 * iwlwifi(4) in iwl_mvm_bss_info_changed_station_common() will
2234 * stop seesion protection the moment it sees
2235 * BSS_CHANGED_BEACON_INFO (with the expectations that it was
2236 * "a beacon from the associated AP"). It will also update
2237 * the beacon filter in that case. This is the only place
2238 * we set the BSS_CHANGED_BEACON_INFO on the non-teardown
2239 * path so make sure we only do run this check once we are
2240 * assoc. (*iv_recv_mgmt)() will be called before we enter
2241 * here so the ni will be updated with information from the
2242 * beacon via net80211::sta_recv_mgmt(). We also need to
2243 * make sure we do not do it on every beacon we still may
2244 * get so only do if something changed. vif->bss_conf.dtim_period
2245 * should be 0 as we start up (we also reset it on teardown).
2246 *
2247 * If we are assoc we need to make sure dtim_period is non-0.
2248 * 0 is a reserved value and drivers assume they can DIV by it.
2249 * In theory this means we need to wait for the first beacon
2250 * before we finalize the vif being assoc. In practise that
2251 * is harder until net80211 learns how to. Work around like
2252 * this for the moment.
2253 */
2254 if (vif->cfg.assoc) {
2255 if (vif->bss_conf.dtim_period != ni->ni_dtim_period &&
2256 ni->ni_dtim_period > 0) {
2257 vif->bss_conf.dtim_period = ni->ni_dtim_period;
2258 bss_changed |= BSS_CHANGED_BEACON_INFO;
2259 } else if (vif->bss_conf.dtim_period == 0) {
2260 vif->bss_conf.dtim_period = 1;
2261 bss_changed |= BSS_CHANGED_BEACON_INFO;
2262 }
2263 }
2264
2265 vif->bss_conf.sync_dtim_count = ni->ni_dtim_count;
2266 vif->bss_conf.sync_tsf = le64toh(ni->ni_tstamp.tsf);
2267 /* vif->bss_conf.sync_device_ts = set in linuxkpi_ieee80211_rx. */
2268
2269 #ifdef LINUXKPI_DEBUG_80211
2270 if (linuxkpi_debug_80211 & D80211_TRACE)
2271 printf("%s:%d [%s:%d] assoc %d aid %d beacon_int %u "
2272 "dtim_period %u sync_dtim_count %u sync_tsf %ju "
2273 "sync_device_ts %u bss_changed %#010jx\n",
2274 __func__, __LINE__, _f, _l,
2275 vif->cfg.assoc, vif->cfg.aid,
2276 vif->bss_conf.beacon_int, vif->bss_conf.dtim_period,
2277 vif->bss_conf.sync_dtim_count,
2278 (uintmax_t)vif->bss_conf.sync_tsf,
2279 vif->bss_conf.sync_device_ts,
2280 (uintmax_t)bss_changed);
2281 #endif
2282
2283 return (bss_changed);
2284 }
2285
2286 static void
lkpi_stop_hw_scan(struct lkpi_hw * lhw,struct ieee80211_vif * vif)2287 lkpi_stop_hw_scan(struct lkpi_hw *lhw, struct ieee80211_vif *vif)
2288 {
2289 struct ieee80211_hw *hw;
2290 int error;
2291 bool cancel;
2292
2293 TRACE_SCAN(lhw->ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
2294
2295 LKPI_80211_LHW_SCAN_LOCK(lhw);
2296 cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
2297 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2298 if (!cancel)
2299 return;
2300
2301 hw = LHW_TO_HW(lhw);
2302
2303 IEEE80211_UNLOCK(lhw->ic);
2304 wiphy_lock(hw->wiphy);
2305 /* Need to cancel the scan. */
2306 lkpi_80211_mo_cancel_hw_scan(hw, vif);
2307 wiphy_unlock(hw->wiphy);
2308
2309 /* Need to make sure we see ieee80211_scan_completed. */
2310 LKPI_80211_LHW_SCAN_LOCK(lhw);
2311 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0)
2312 error = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz/2);
2313 cancel = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
2314 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
2315
2316 IEEE80211_LOCK(lhw->ic);
2317
2318 if (cancel)
2319 ic_printf(lhw->ic, "%s: failed to cancel scan: %d (%p, %p)\n",
2320 __func__, error, lhw, vif);
2321 }
2322
2323 static void
lkpi_hw_conf_idle(struct ieee80211_hw * hw,bool new)2324 lkpi_hw_conf_idle(struct ieee80211_hw *hw, bool new)
2325 {
2326 struct lkpi_hw *lhw;
2327 int error;
2328 bool old;
2329
2330 lockdep_assert_wiphy(hw->wiphy);
2331
2332 old = hw->conf.flags & IEEE80211_CONF_IDLE;
2333 if (old == new)
2334 return;
2335
2336 hw->conf.flags ^= IEEE80211_CONF_IDLE;
2337 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_IDLE);
2338 if (error != 0 && error != EOPNOTSUPP) {
2339 lhw = HW_TO_LHW(hw);
2340 ic_printf(lhw->ic, "ERROR: %s: config %#0x returned %d\n",
2341 __func__, IEEE80211_CONF_CHANGE_IDLE, error);
2342 }
2343 }
2344
2345 static enum ieee80211_bss_changed
lkpi_disassoc(struct ieee80211_sta * sta,struct ieee80211_vif * vif,struct lkpi_hw * lhw)2346 lkpi_disassoc(struct ieee80211_sta *sta, struct ieee80211_vif *vif,
2347 struct lkpi_hw *lhw)
2348 {
2349 struct ieee80211_hw *hw;
2350 struct lkpi_vif *lvif;
2351 enum ieee80211_bss_changed changed;
2352
2353 hw = LHW_TO_HW(lhw);
2354 lockdep_assert_wiphy(hw->wiphy);
2355
2356 changed = 0;
2357 sta->aid = 0;
2358 if (vif->cfg.assoc) {
2359
2360 vif->cfg.assoc = false;
2361 vif->cfg.aid = 0;
2362 changed |= BSS_CHANGED_ASSOC;
2363 IMPROVE();
2364
2365 lkpi_update_mcast_filter_locked(lhw->ic);
2366
2367 /*
2368 * Executing the bss_info_changed(BSS_CHANGED_ASSOC) with
2369 * assoc = false right away here will remove the sta from
2370 * firmware for iwlwifi.
2371 * We no longer do this but only return the BSS_CHNAGED value.
2372 * The caller is responsible for removing the sta gong to
2373 * IEEE80211_STA_NOTEXIST and then executing the
2374 * bss_info_changed() update.
2375 * See DOWN4 for more detailed comment.
2376 */
2377
2378 lvif = VIF_TO_LVIF(vif);
2379 lvif->beacons = 0;
2380 }
2381
2382 return (changed);
2383 }
2384
2385 static void
lkpi_wake_tx_queues(struct ieee80211_hw * hw,struct ieee80211_sta * sta,bool dequeue_seen,bool no_emptyq)2386 lkpi_wake_tx_queues(struct ieee80211_hw *hw, struct ieee80211_sta *sta,
2387 bool dequeue_seen, bool no_emptyq)
2388 {
2389 struct lkpi_txq *ltxq;
2390 int tid;
2391 bool ltxq_empty;
2392
2393 /* Wake up all queues to know they are allocated in the driver. */
2394 for (tid = 0; tid < nitems(sta->txq); tid++) {
2395
2396 if (tid == IEEE80211_NUM_TIDS) {
2397 IMPROVE("station specific?");
2398 if (!ieee80211_hw_check(hw, STA_MMPDU_TXQ))
2399 continue;
2400 } else if (tid >= hw->queues)
2401 continue;
2402
2403 if (sta->txq[tid] == NULL)
2404 continue;
2405
2406 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
2407 if (dequeue_seen && (ltxq->flags & LKPI_TXQ_SEEN_DEQUEUE) == 0)
2408 continue;
2409
2410 LKPI_80211_LTXQ_LOCK(ltxq);
2411 ltxq_empty = skb_queue_empty(<xq->skbq);
2412 LKPI_80211_LTXQ_UNLOCK(ltxq);
2413 if (no_emptyq && ltxq_empty)
2414 continue;
2415
2416 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid], false);
2417 }
2418 }
2419
2420 /*
2421 * On the way down from RUN -> ASSOC -> AUTH we may send a DISASSOC or DEAUTH
2422 * packet. The problem is that the state machine functions tend to hold the
2423 * LHW lock which will prevent lkpi_80211_txq_tx_one() from sending the packet.
2424 * We call this after dropping the ic lock and before acquiring the LHW lock.
2425 * we make sure no further packets are queued and if they are queued the task
2426 * will finish or be cancelled. At the end if a packet is left we manually
2427 * send it. scan_to_auth() would re-enable sending if the lsta would be
2428 * re-used.
2429 */
2430 static void
lkpi_80211_flush_tx(struct lkpi_hw * lhw,struct lkpi_sta * lsta)2431 lkpi_80211_flush_tx(struct lkpi_hw *lhw, struct lkpi_sta *lsta)
2432 {
2433 struct ieee80211_hw *hw;
2434 struct mbufq mq;
2435 struct mbuf *m;
2436 int len;
2437
2438 /* There is no lockdep_assert_not_held_wiphy(). */
2439 hw = LHW_TO_HW(lhw);
2440 lockdep_assert_not_held(&hw->wiphy->mtx);
2441
2442 /* Do not accept any new packets until scan_to_auth or lsta_free(). */
2443 LKPI_80211_LSTA_TXQ_LOCK(lsta);
2444 lsta->txq_ready = false;
2445 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2446
2447 while (taskqueue_cancel(taskqueue_thread, &lsta->txq_task, NULL) != 0)
2448 taskqueue_drain(taskqueue_thread, &lsta->txq_task);
2449
2450 LKPI_80211_LSTA_TXQ_LOCK(lsta);
2451 len = mbufq_len(&lsta->txq);
2452 if (len <= 0) {
2453 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2454 return;
2455 }
2456
2457 mbufq_init(&mq, IFQ_MAXLEN);
2458 mbufq_concat(&mq, &lsta->txq);
2459 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
2460
2461 m = mbufq_dequeue(&mq);
2462 while (m != NULL) {
2463 lkpi_80211_txq_tx_one(lsta, m);
2464 m = mbufq_dequeue(&mq);
2465 }
2466 }
2467
2468 static void
lkpi_init_chandef(struct ieee80211com * ic __unused,struct cfg80211_chan_def * chandef,struct linuxkpi_ieee80211_channel * chan,struct ieee80211_channel * c,bool can_ht)2469 lkpi_init_chandef(struct ieee80211com *ic __unused,
2470 struct cfg80211_chan_def *chandef,
2471 struct linuxkpi_ieee80211_channel *chan, struct ieee80211_channel *c,
2472 bool can_ht)
2473 {
2474
2475 cfg80211_chandef_create(chandef, chan,
2476 (can_ht) ? NL80211_CHAN_HT20 : NL80211_CHAN_NO_HT);
2477 chandef->center_freq1 = ieee80211_get_channel_center_freq1(c);
2478 chandef->center_freq2 = ieee80211_get_channel_center_freq2(c);
2479
2480 IMPROVE("Check ht/vht_cap from band not just chan? See lkpi_sta_sync_from_ni...");
2481 #ifdef LKPI_80211_HT
2482 if (IEEE80211_IS_CHAN_HT(c)) {
2483 if (IEEE80211_IS_CHAN_HT40(c))
2484 chandef->width = NL80211_CHAN_WIDTH_40;
2485 else
2486 chandef->width = NL80211_CHAN_WIDTH_20;
2487 }
2488 #endif
2489 #ifdef LKPI_80211_VHT
2490 if (IEEE80211_IS_CHAN_VHT_5GHZ(c)) {
2491 if (IEEE80211_IS_CHAN_VHT80P80(c))
2492 chandef->width = NL80211_CHAN_WIDTH_80P80;
2493 else if (IEEE80211_IS_CHAN_VHT160(c))
2494 chandef->width = NL80211_CHAN_WIDTH_160;
2495 else if (IEEE80211_IS_CHAN_VHT80(c))
2496 chandef->width = NL80211_CHAN_WIDTH_80;
2497 }
2498 #endif
2499
2500 #ifdef LINUXKPI_DEBUG_80211
2501 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
2502 ic_printf(ic, "%s:%d: chandef %p { chan %p { %u }, "
2503 "width %d cfreq1 %u cfreq2 %u punctured %u }\n",
2504 __func__, __LINE__, chandef,
2505 chandef->chan, chandef->chan->center_freq,
2506 chandef->width,
2507 chandef->center_freq1, chandef->center_freq2,
2508 chandef->punctured);
2509 #endif
2510 }
2511
2512 static uint32_t
lkpi_init_chanctx_conf(struct ieee80211_hw * hw,struct cfg80211_chan_def * chandef,struct ieee80211_chanctx_conf * chanctx_conf)2513 lkpi_init_chanctx_conf(struct ieee80211_hw *hw,
2514 struct cfg80211_chan_def *chandef,
2515 struct ieee80211_chanctx_conf *chanctx_conf)
2516 {
2517 uint32_t changed;
2518
2519 lockdep_assert_wiphy(hw->wiphy);
2520
2521 changed = 0;
2522
2523 chanctx_conf->rx_chains_static = 1;
2524 chanctx_conf->rx_chains_dynamic = 1;
2525 changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
2526
2527 if (chanctx_conf->radar_enabled != hw->conf.radar_enabled) {
2528 chanctx_conf->radar_enabled = hw->conf.radar_enabled;
2529 changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
2530 }
2531
2532 chanctx_conf->def = *chandef;
2533 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2534
2535 /* One day we should figure this out; is for iwlwifi-only. */
2536 chanctx_conf->min_def = chanctx_conf->def;
2537 changed |= IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
2538
2539 /* chanctx_conf->ap = */
2540
2541 return (changed);
2542 }
2543
2544 static struct lkpi_chanctx *
lkpi_alloc_lchanctx(struct ieee80211_hw * hw,struct lkpi_vif * lvif)2545 lkpi_alloc_lchanctx(struct ieee80211_hw *hw, struct lkpi_vif *lvif)
2546 {
2547 struct lkpi_chanctx *lchanctx;
2548
2549 lchanctx = malloc(sizeof(*lchanctx) + hw->chanctx_data_size,
2550 M_LKPI80211, M_WAITOK | M_ZERO);
2551 lchanctx->lvif = lvif;
2552
2553 return (lchanctx);
2554 }
2555
2556 static struct lkpi_chanctx *
lkpi_find_lchanctx_reserved(struct ieee80211_hw * hw,struct lkpi_vif * lvif)2557 lkpi_find_lchanctx_reserved(struct ieee80211_hw *hw, struct lkpi_vif *lvif)
2558 {
2559 struct lkpi_hw *lhw;
2560 struct lkpi_chanctx *lchanctx;
2561 bool found;
2562
2563 lhw = HW_TO_LHW(hw);
2564
2565 found = false;
2566 rcu_read_lock();
2567 list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list_reserved, entry) {
2568 if (lchanctx->lvif == lvif) {
2569 found = true;
2570 break;
2571 }
2572 }
2573 rcu_read_unlock();
2574
2575 if (!found) {
2576 lchanctx = lkpi_alloc_lchanctx(hw, lvif);
2577 list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list_reserved);
2578 }
2579
2580 return (lchanctx);
2581 }
2582
2583 static struct ieee80211_chanctx_conf *
lkpi_get_chanctx_conf(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2584 lkpi_get_chanctx_conf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2585 {
2586 struct ieee80211_chanctx_conf *chanctx_conf;
2587
2588 chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2589 lockdep_is_held(&hw->wiphy->mtx));
2590 if (chanctx_conf == NULL) {
2591 struct lkpi_chanctx *lchanctx;
2592 struct lkpi_vif *lvif;
2593
2594 lvif = VIF_TO_LVIF(vif);
2595 lchanctx = lkpi_find_lchanctx_reserved(hw, lvif);
2596 KASSERT(lchanctx != NULL, ("%s: hw %p, vif %p no lchanctx\n",
2597 __func__, hw, vif));
2598 list_del(&lchanctx->entry);
2599 chanctx_conf = &lchanctx->chanctx_conf;
2600 }
2601 /* else { IMPROVE("diff changes for changed, working on live copy, rcu"); } */
2602
2603 return (chanctx_conf);
2604 }
2605
2606 static int
lkpi_set_chanctx_conf(struct ieee80211_hw * hw,struct ieee80211_vif * vif,struct ieee80211_chanctx_conf * chanctx_conf,uint32_t changed,bool changed_set)2607 lkpi_set_chanctx_conf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2608 struct ieee80211_chanctx_conf *chanctx_conf,
2609 uint32_t changed, bool changed_set)
2610 {
2611 struct lkpi_hw *lhw;
2612 struct lkpi_chanctx *lchanctx;
2613 int error;
2614
2615 lockdep_assert_wiphy(hw->wiphy);
2616
2617 if (vif->bss_conf.chanctx_conf == chanctx_conf) {
2618 if (!changed_set) {
2619 IMPROVE("OBSOLETE?");
2620 changed = IEEE80211_CHANCTX_CHANGE_MIN_WIDTH;
2621 changed |= IEEE80211_CHANCTX_CHANGE_RADAR;
2622 changed |= IEEE80211_CHANCTX_CHANGE_RX_CHAINS;
2623 changed |= IEEE80211_CHANCTX_CHANGE_WIDTH;
2624 }
2625 lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
2626
2627 return (0);
2628 }
2629
2630 lhw = HW_TO_LHW(hw);
2631
2632 /* The device is no longer idle. */
2633 IMPROVE("Once we do multi-vif, only do for 1st chanctx");
2634 lkpi_hw_conf_idle(hw, false);
2635
2636 error = lkpi_80211_mo_add_chanctx(hw, chanctx_conf);
2637 if (error != 0 && error != EOPNOTSUPP) {
2638 ic_printf(lhw->ic, "%s:%d: mo_add_chanctx "
2639 "failed: %d\n", __func__, __LINE__, error);
2640 return (error);
2641 }
2642
2643 vif->bss_conf.chanreq.oper.chan = chanctx_conf->def.chan;
2644 vif->bss_conf.chanreq.oper.width = chanctx_conf->def.width;
2645 vif->bss_conf.chanreq.oper.center_freq1 =
2646 chanctx_conf->def.center_freq1;
2647 vif->bss_conf.chanreq.oper.center_freq2 =
2648 chanctx_conf->def.center_freq2;
2649
2650 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2651 list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list);
2652 rcu_assign_pointer(vif->bss_conf.chanctx_conf, chanctx_conf);
2653
2654 /* Assign vif chanctx. */
2655 if (error == 0)
2656 error = lkpi_80211_mo_assign_vif_chanctx(hw, vif,
2657 &vif->bss_conf, chanctx_conf);
2658 if (error == EOPNOTSUPP)
2659 error = 0;
2660 if (error != 0) {
2661 ic_printf(lhw->ic, "%s:%d: mo_assign_vif_chanctx "
2662 "failed: %d\n", __func__, __LINE__, error);
2663 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2664 rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2665 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2666 list_del(&lchanctx->entry);
2667 memset(lchanctx, 0, sizeof(*lchanctx));
2668 lchanctx->lvif = VIF_TO_LVIF(vif);
2669 list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list_reserved);
2670 }
2671
2672 return (error);
2673 }
2674
2675 static void
lkpi_remove_chanctx(struct ieee80211_hw * hw,struct ieee80211_vif * vif)2676 lkpi_remove_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
2677 {
2678 struct lkpi_hw *lhw;
2679 struct ieee80211_chanctx_conf *chanctx_conf;
2680 struct lkpi_chanctx *lchanctx;
2681
2682 lockdep_assert_wiphy(hw->wiphy);
2683
2684 chanctx_conf = rcu_dereference_protected(vif->bss_conf.chanctx_conf,
2685 lockdep_is_held(&hw->wiphy->mtx));
2686
2687 if (chanctx_conf == NULL)
2688 return;
2689
2690 /* Remove vif context. */
2691 lkpi_80211_mo_unassign_vif_chanctx(hw, vif, &vif->bss_conf, chanctx_conf);
2692
2693 lkpi_hw_conf_idle(hw, true);
2694
2695 /* Remove chan ctx. */
2696 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
2697
2698 /* Cleanup. */
2699 rcu_assign_pointer(vif->bss_conf.chanctx_conf, NULL);
2700 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
2701 list_del(&lchanctx->entry);
2702 lhw = HW_TO_LHW(hw);
2703 memset(lchanctx, 0, sizeof(*lchanctx));
2704 lchanctx->lvif = VIF_TO_LVIF(vif);
2705 list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list_reserved);
2706 }
2707
2708 /* -------------------------------------------------------------------------- */
2709
2710 /* Any other options belong here? Check more drivers. */
2711 #define BSS_CHANGED_VIF_CFG_BITS \
2712 (BSS_CHANGED_SSID | BSS_CHANGED_IDLE | BSS_CHANGED_PS | BSS_CHANGED_ASSOC | \
2713 BSS_CHANGED_ARP_FILTER | BSS_CHANGED_MLD_VALID_LINKS | BSS_CHANGED_MLD_TTLM)
2714
2715 static void
lkpi_bss_info_change(struct ieee80211_hw * hw,struct ieee80211_vif * vif,enum ieee80211_bss_changed bss_changed)2716 lkpi_bss_info_change(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
2717 enum ieee80211_bss_changed bss_changed)
2718 {
2719 struct lkpi_vif *lvif;
2720 enum ieee80211_bss_changed vif_cfg_bits, link_info_bits;
2721
2722 if (ieee80211_vif_is_mld(vif)) {
2723 TODO("This likely needs a subset only; split up into 3 parts.");
2724 }
2725
2726 /* Nothing to do? */
2727 if (bss_changed == 0)
2728 return;
2729
2730 /*
2731 * If the vif is not known to the driver there is nothing to notifiy for.
2732 * We MUST NOT check for !lvif_bss_synched here (the reasonable it seems)
2733 * as we need to execute the update(s) or we will have follow-up issues.
2734 */
2735 lvif = VIF_TO_LVIF(vif);
2736 if (!lvif->added_to_drv)
2737 return;
2738
2739 /*
2740 * With the advent of MLO bss_conf got split up into vif and link
2741 * change notfications, while historically it was one.
2742 * We now need to support all possible models.
2743 */
2744 vif_cfg_bits = bss_changed & BSS_CHANGED_VIF_CFG_BITS;
2745 if (vif_cfg_bits != 0)
2746 lkpi_80211_mo_vif_cfg_changed(hw, vif, vif_cfg_bits, false);
2747
2748 link_info_bits = bss_changed & ~(BSS_CHANGED_VIF_CFG_BITS);
2749 if (link_info_bits != 0)
2750 lkpi_80211_mo_link_info_changed(hw, vif, &vif->bss_conf,
2751 link_info_bits, 0, false);
2752
2753 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2754 }
2755
2756 /* -------------------------------------------------------------------------- */
2757
2758 static int
lkpi_sta_state_do_nada(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2759 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2760 {
2761 return (0);
2762 }
2763
2764 /* UP1 */
2765 static int
lkpi_sta_init_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2766 lkpi_sta_init_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2767 {
2768 return (lkpi_sta_state_do_nada(vap, nstate, arg));
2769 }
2770
2771 /* UP2 */
2772 static int
lkpi_sta_scan_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2773 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2774 {
2775 struct linuxkpi_ieee80211_channel *chan;
2776 struct cfg80211_chan_def chandef;
2777 struct ieee80211_chanctx_conf *chanctx_conf;
2778 struct lkpi_hw *lhw;
2779 struct ieee80211_hw *hw;
2780 struct lkpi_vif *lvif;
2781 struct ieee80211_vif *vif;
2782 struct ieee80211_node *ni;
2783 struct lkpi_sta *lsta;
2784 enum ieee80211_bss_changed bss_changed;
2785 struct ieee80211_prep_tx_info prep_tx_info;
2786 uint32_t changed;
2787 int error;
2788 bool synched, can_ht;
2789
2790 /*
2791 * In here we use vap->iv_bss until lvif->lvif_bss is set.
2792 * For all later (STATE >= AUTH) functions we need to use the lvif
2793 * cache which will be tracked even through (*iv_update_bss)().
2794 */
2795
2796 if (vap->iv_bss == NULL) {
2797 ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
2798 return (EINVAL);
2799 }
2800 /*
2801 * Keep the ni alive locally. In theory (and practice) iv_bss can change
2802 * once we unlock here. This is due to net80211 allowing state changes
2803 * and new join1() despite having an active node as well as due to
2804 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
2805 */
2806 ni = ieee80211_ref_node(vap->iv_bss);
2807 if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
2808 ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
2809 "on vap %p\n", __func__, ni, vap);
2810 ieee80211_free_node(ni); /* Error handling for the local ni. */
2811 return (EINVAL);
2812 }
2813
2814 lhw = vap->iv_ic->ic_softc;
2815 chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
2816 if (chan == NULL) {
2817 ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
2818 "iv_bss ni %p on vap %p\n", __func__, ni, vap);
2819 ieee80211_free_node(ni); /* Error handling for the local ni. */
2820 return (ESRCH);
2821 }
2822
2823 hw = LHW_TO_HW(lhw);
2824 lvif = VAP_TO_LVIF(vap);
2825 vif = LVIF_TO_VIF(lvif);
2826
2827 LKPI_80211_LVIF_LOCK(lvif);
2828 /* XXX-BZ KASSERT later? */
2829 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
2830 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2831 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2832 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2833 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2834 lvif->lvif_bss_synched);
2835 LKPI_80211_LVIF_UNLOCK(lvif);
2836 ieee80211_free_node(ni); /* Error handling for the local ni. */
2837 return (EBUSY);
2838 }
2839 LKPI_80211_LVIF_UNLOCK(lvif);
2840
2841 IEEE80211_UNLOCK(vap->iv_ic);
2842 wiphy_lock(hw->wiphy);
2843
2844 /* Add chanctx (or if exists, change it). */
2845 chanctx_conf = lkpi_get_chanctx_conf(hw, vif);
2846
2847 KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
2848 ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
2849
2850 #ifdef LKPI_80211_HT
2851 can_ht = (vap->iv_ic->ic_flags_ht & IEEE80211_FHT_HT) != 0;
2852 #else
2853 can_ht = false;
2854 #endif
2855 lkpi_init_chandef(vap->iv_ic, &chandef, chan, ni->ni_chan, can_ht);
2856 hw->conf.radar_enabled =
2857 ((chan->flags & IEEE80211_CHAN_RADAR) != 0) ? true : false;
2858 hw->conf.chandef = chandef;
2859 vif->bss_conf.chanreq.oper = hw->conf.chandef;
2860 #ifdef LINUXKPI_DEBUG_80211
2861 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
2862 ic_printf(vap->iv_ic, "%s:%d: hw->conf.chandef %p = chandef %p = "
2863 "vif->bss_conf.chanreq.oper %p\n", __func__, __LINE__,
2864 &hw->conf.chandef, &chandef, &vif->bss_conf.chanreq.oper);
2865 #endif
2866
2867 changed = lkpi_init_chanctx_conf(hw, &chandef, chanctx_conf);
2868
2869 /* Responder ... */
2870
2871 /* Set bss info (bss_info_changed). */
2872 bss_changed = 0;
2873 IEEE80211_ADDR_COPY(vif->cfg.ap_addr, ni->ni_bssid);
2874 vif->bss_conf.bssid = ni->ni_bssid;
2875 bss_changed |= BSS_CHANGED_BSSID;
2876 vif->bss_conf.txpower = ni->ni_txpower;
2877 bss_changed |= BSS_CHANGED_TXPOWER;
2878 vif->cfg.idle = false;
2879 bss_changed |= BSS_CHANGED_IDLE;
2880
2881 lvif->beacons = 0;
2882 /* Should almost assert it is this. */
2883 vif->cfg.assoc = false;
2884 vif->cfg.aid = 0;
2885
2886 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2887
2888 error = lkpi_set_chanctx_conf(hw, vif, chanctx_conf, changed, true);
2889 if (error != 0)
2890 goto out;
2891
2892 IMPROVE("update radiotap chan fields too");
2893
2894 /* RATES */
2895 IMPROVE("bss info: not all needs to come now and rates are missing");
2896 bss_changed |= lkpi_sta_supp_rates(hw, vif, ni, NULL);
2897 if (ieee80211_hw_check(hw, HAS_RATE_CONTROL))
2898 lkpi_80211_mo_set_bitrate_mask(hw, vif, &lvif->br_mask);
2899 TODO("cfg80211_tid_config WHERE?");
2900
2901 lkpi_bss_info_change(hw, vif, bss_changed);
2902
2903 /*
2904 * Given ni and lsta are 1:1 from alloc to free we can assert that
2905 * ni always has lsta data attach despite net80211 node swapping
2906 * under the hoods.
2907 */
2908 KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
2909 __func__, ni, ni->ni_drv_data));
2910 lsta = ni->ni_drv_data;
2911
2912 /* Insert the [l]sta into the list of known stations. */
2913 list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2914
2915 /* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
2916 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2917 KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
2918 "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2919 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2920 if (error != 0) {
2921 IMPROVE("do we need to undo the chan ctx?");
2922 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2923 "failed: %d\n", __func__, __LINE__, error);
2924 goto out;
2925 }
2926 #if 0
2927 lsta->added_to_drv = true; /* mo manages. */
2928 #endif
2929
2930 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2931
2932 #if 0
2933 /*
2934 * Wakeup all queues now that sta is there so we have as much time to
2935 * possibly prepare the queue in the driver to be ready for the 1st
2936 * packet; lkpi_80211_txq_tx_one() still has a workaround as there
2937 * is no guarantee or way to check.
2938 * XXX-BZ and by now we know that this does not work on all drivers
2939 * for all queues.
2940 */
2941 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
2942 #endif
2943
2944 /* Start mgd_prepare_tx. */
2945 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2946 prep_tx_info.duration = PREP_TX_INFO_DURATION; /* SAE */
2947 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
2948 prep_tx_info.link_id = 0;
2949 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2950 lsta->in_mgd = true;
2951
2952 /*
2953 * What is going to happen next:
2954 * - <twiddle> .. we should end up in "auth_to_assoc"
2955 * - event_callback
2956 * - update sta_state (NONE to AUTH)
2957 * - mgd_complete_tx
2958 * (ideally we'd do that on a callback for something else ...)
2959 */
2960
2961 wiphy_unlock(hw->wiphy);
2962 IEEE80211_LOCK(vap->iv_ic);
2963
2964 LKPI_80211_LVIF_LOCK(lvif);
2965 /* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2966 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2967 lsta->ni != vap->iv_bss)
2968 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2969 "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2970 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2971 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2972 lvif->lvif_bss_synched, ni, lsta);
2973
2974 /*
2975 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2976 * Given we cache lsta we use lsta->ni instead of ni here (even though
2977 * lsta->ni == ni) to be distinct from the rest of the code where we do
2978 * assume that ni == vap->iv_bss which it may or may not be.
2979 * So do NOT use iv_bss here anymore as that may have diverged from our
2980 * function local ni already while ic was unlocked and would lead to
2981 * inconsistencies. Go and see if we lost a race and do not update
2982 * lvif_bss_synched in that case.
2983 */
2984 ieee80211_ref_node(lsta->ni);
2985 lvif->lvif_bss = lsta;
2986 if (lsta->ni == vap->iv_bss) {
2987 lvif->lvif_bss_synched = synched = true;
2988 } else {
2989 /* Set to un-synched no matter what. */
2990 lvif->lvif_bss_synched = synched = false;
2991 /*
2992 * We do not error as someone has to take us down.
2993 * If we are followed by a 2nd, new net80211::join1() going to
2994 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2995 * will take the lvif->lvif_bss node down eventually.
2996 * What happens with the vap->iv_bss node will entirely be up
2997 * to net80211 as we never used the node beyond alloc()/free()
2998 * and we do not hold an extra reference for that anymore given
2999 * ni : lsta == 1:1.
3000 * Problem is if we do not error a MGMT/AUTH frame will be
3001 * sent from net80211::sta_newstate(); disable lsta queue below.
3002 */
3003 }
3004 LKPI_80211_LVIF_UNLOCK(lvif);
3005 /*
3006 * Make sure in case the sta did not change and we re-added it,
3007 * that we can tx again but only if the vif/iv_bss are in sync.
3008 * Otherwise this should prevent the MGMT/AUTH frame from being
3009 * sent triggering a warning in iwlwifi.
3010 */
3011 LKPI_80211_LSTA_TXQ_LOCK(lsta);
3012 lsta->txq_ready = synched;
3013 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3014 goto out_relocked;
3015
3016 out:
3017 wiphy_unlock(hw->wiphy);
3018 IEEE80211_LOCK(vap->iv_ic);
3019 out_relocked:
3020 /*
3021 * Release the reference that kept the ni stable locally
3022 * during the work of this function.
3023 */
3024 if (ni != NULL)
3025 ieee80211_free_node(ni);
3026 return (error);
3027 }
3028
3029 /* UP3.1 */
3030 static int
lkpi_sta_auth_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3031 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3032 {
3033 struct lkpi_hw *lhw;
3034 struct ieee80211_hw *hw;
3035 struct lkpi_vif *lvif;
3036 struct ieee80211_vif *vif;
3037 struct lkpi_sta *lsta;
3038 struct ieee80211_prep_tx_info prep_tx_info;
3039 int error;
3040
3041 lhw = vap->iv_ic->ic_softc;
3042 hw = LHW_TO_HW(lhw);
3043 lvif = VAP_TO_LVIF(vap);
3044 vif = LVIF_TO_VIF(lvif);
3045
3046 IEEE80211_UNLOCK(vap->iv_ic);
3047 wiphy_lock(hw->wiphy);
3048
3049 LKPI_80211_LVIF_LOCK(lvif);
3050 /* XXX-BZ KASSERT later? */
3051 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
3052 #ifdef LINUXKPI_DEBUG_80211
3053 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3054 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3055 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3056 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3057 lvif->lvif_bss_synched);
3058 #endif
3059 error = ENOTRECOVERABLE;
3060 LKPI_80211_LVIF_UNLOCK(lvif);
3061 goto out;
3062 }
3063 lsta = lvif->lvif_bss;
3064 LKPI_80211_LVIF_UNLOCK(lvif);
3065
3066 KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
3067
3068 /* Finish auth. */
3069 IMPROVE("event callback");
3070
3071 /* Update sta_state (NONE to AUTH). */
3072 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3073 "NONE: %#x\n", __func__, lsta, lsta->state));
3074 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3075 if (error != 0) {
3076 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3077 "failed: %d\n", __func__, __LINE__, error);
3078 goto out;
3079 }
3080
3081 /* End mgd_complete_tx. */
3082 if (lsta->in_mgd) {
3083 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3084 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
3085 prep_tx_info.success = true;
3086 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3087 lsta->in_mgd = false;
3088 }
3089
3090 /* Now start assoc. unless nstate=RUN (auth_to_run). */
3091
3092 /* Start mgd_prepare_tx. */
3093 if (nstate == IEEE80211_S_ASSOC && !lsta->in_mgd) {
3094 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3095 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3096 prep_tx_info.link_id = 0;
3097 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3098 lsta->in_mgd = true;
3099 }
3100
3101 #if 0
3102 /* We do not yet have a packet to go out. */
3103 /* Wake tx queue to get packet out. */
3104 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
3105 #endif
3106
3107 /*
3108 * <twiddle> .. we end up in "assoc_to_run"
3109 * - update sta_state (AUTH to ASSOC)
3110 * - conf_tx [all]
3111 * - bss_info_changed (assoc, aid, ssid, ..)
3112 * - change_chanctx (if needed)
3113 * - event_callback
3114 * - mgd_complete_tx
3115 */
3116
3117 out:
3118 wiphy_unlock(hw->wiphy);
3119 IEEE80211_LOCK(vap->iv_ic);
3120 return (error);
3121 }
3122
3123 static int lkpi_sta_assoc_to_run(struct ieee80211vap *, enum ieee80211_state, int);
3124
3125 /* UP3.2 */
3126 static int
lkpi_sta_auth_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3127 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3128 {
3129 int error;
3130
3131 error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
3132 if (error == 0)
3133 error = lkpi_sta_assoc_to_run(vap, nstate, arg);
3134 return (error);
3135 }
3136
3137 /* UP4 */
3138 static int
lkpi_sta_assoc_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3139 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3140 {
3141 struct lkpi_hw *lhw;
3142 struct ieee80211_hw *hw;
3143 struct lkpi_vif *lvif;
3144 struct ieee80211_vif *vif;
3145 struct ieee80211_node *ni;
3146 struct lkpi_sta *lsta;
3147 struct ieee80211_sta *sta;
3148 struct ieee80211_prep_tx_info prep_tx_info;
3149 enum ieee80211_bss_changed bss_changed;
3150 int error;
3151
3152 lhw = vap->iv_ic->ic_softc;
3153 hw = LHW_TO_HW(lhw);
3154 lvif = VAP_TO_LVIF(vap);
3155 vif = LVIF_TO_VIF(lvif);
3156
3157 IEEE80211_UNLOCK(vap->iv_ic);
3158 wiphy_lock(hw->wiphy);
3159
3160 LKPI_80211_LVIF_LOCK(lvif);
3161 /* XXX-BZ KASSERT later? */
3162 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
3163 #ifdef LINUXKPI_DEBUG_80211
3164 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3165 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3166 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3167 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3168 lvif->lvif_bss_synched);
3169 #endif
3170 LKPI_80211_LVIF_UNLOCK(lvif);
3171 error = ENOTRECOVERABLE;
3172 goto out;
3173 }
3174 lsta = lvif->lvif_bss;
3175 LKPI_80211_LVIF_UNLOCK(lvif);
3176 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3177 "lvif %p vap %p\n", __func__,
3178 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3179
3180 ni = lsta->ni; /* Reference held for lvif_bss. */
3181
3182 IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
3183 "and to lesser extend ieee80211_notify_node_join");
3184
3185 /* Finish assoc. */
3186 /* Update sta_state (AUTH to ASSOC) and set aid. */
3187 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3188 "AUTH: %#x\n", __func__, lsta, lsta->state));
3189 sta = LSTA_TO_STA(lsta);
3190 sta->aid = IEEE80211_NODE_AID(ni);
3191 #ifdef LKPI_80211_WME
3192 if (vap->iv_flags & IEEE80211_F_WME)
3193 sta->wme = true;
3194 #endif
3195 bss_changed = 0;
3196 /*
3197 * This sync needs to happen before the sta_state change to ASSOC.
3198 * At least mt7921 (likely all drivers) rely on, e.g., ht_cap, vht_cap,
3199 * .. to be set at the point we go to assoc.
3200 */
3201 bss_changed |= lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
3202 if (ieee80211_hw_check(hw, HAS_RATE_CONTROL))
3203 lkpi_80211_mo_set_bitrate_mask(hw, vif, &lvif->br_mask);
3204
3205 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3206 if (error != 0) {
3207 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3208 "failed: %d\n", __func__, __LINE__, error);
3209 goto out;
3210 }
3211
3212 IMPROVE("wme / conf_tx [all]");
3213
3214 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
3215 #ifdef LKPI_80211_WME
3216 bss_changed |= lkpi_wme_update(lhw, vap, true);
3217 #endif
3218 if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
3219 lvif->beacons = 0;
3220 vif->cfg.assoc = true;
3221 vif->cfg.aid = IEEE80211_NODE_AID(ni);
3222 bss_changed |= BSS_CHANGED_ASSOC;
3223 }
3224 /* We set SSID but this is not BSSID! */
3225 vif->cfg.ssid_len = ni->ni_esslen;
3226 memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
3227 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
3228 vif->bss_conf.use_short_preamble) {
3229 vif->bss_conf.use_short_preamble ^= 1;
3230 /* bss_changed |= BSS_CHANGED_??? */
3231 }
3232 if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
3233 vif->bss_conf.use_short_slot) {
3234 vif->bss_conf.use_short_slot ^= 1;
3235 /* bss_changed |= BSS_CHANGED_??? */
3236 }
3237 if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
3238 vif->bss_conf.qos) {
3239 vif->bss_conf.qos ^= 1;
3240 bss_changed |= BSS_CHANGED_QOS;
3241 }
3242
3243 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
3244 lkpi_bss_info_change(hw, vif, bss_changed);
3245
3246 /* - change_chanctx (if needed)
3247 * - event_callback
3248 */
3249
3250 /* End mgd_complete_tx. (we do not have to check ostate == IEEE80211_S_ASSOC). */
3251 if (lsta->in_mgd) {
3252 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3253 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3254 prep_tx_info.success = true; /* Needs vif->cfg.assoc set! */
3255 prep_tx_info.link_id = 0;
3256 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3257 lsta->in_mgd = false;
3258 }
3259
3260 /*
3261 * And then:
3262 * - (more packets)?
3263 * - set_key
3264 * - set_default_unicast_key
3265 * - set_key (?)
3266 * - ipv6_addr_change (?)
3267 */
3268
3269 if (!ieee80211_node_is_authorized(ni)) {
3270 IMPROVE("net80211 does not consider node authorized");
3271 }
3272
3273 bss_changed = 0;
3274 IMPROVE("Is this the right spot, has net80211 done all updates already?");
3275
3276 /* Update thresholds. */
3277 hw->wiphy->frag_threshold = vap->iv_fragthreshold;
3278 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
3279 hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
3280 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
3281
3282 /* Update sta_state (ASSOC to AUTHORIZED). */
3283 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3284 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3285 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3286 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
3287 if (error != 0) {
3288 IMPROVE("undo some changes?");
3289 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
3290 "failed: %d\n", __func__, __LINE__, error);
3291 goto out;
3292 }
3293
3294 /* - drv_config (?)
3295 * - bss_info_changed
3296 * - set_rekey_data (?)
3297 *
3298 * And now we should be passing packets.
3299 */
3300 IMPROVE("Need that bssid setting, and the keys");
3301
3302 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
3303 lkpi_bss_info_change(hw, vif, bss_changed);
3304
3305 /* Prepare_multicast && configure_filter. */
3306 lkpi_update_mcast_filter_locked(vap->iv_ic);
3307
3308 out:
3309 wiphy_unlock(hw->wiphy);
3310 IEEE80211_LOCK(vap->iv_ic);
3311 return (error);
3312 }
3313
3314 /*
3315 * DOWN1
3316 * "to assoc" means we are going back to State 2 from State 4[/3].
3317 * This means ni still is authenticated, so we keep sta, chanctx, ..
3318 * We will send a (Re)Assoc Request in case net80211 handles roadming.
3319 * Note: this can be called as part of a DEAUTH going to State 1 as well,
3320 * so for RoC prep_tx_info we need to check nstate (see run_to_{auth,scan,init}).
3321 */
3322 static int
lkpi_sta_run_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3323 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3324 {
3325 struct lkpi_hw *lhw;
3326 struct ieee80211_hw *hw;
3327 struct lkpi_vif *lvif;
3328 struct ieee80211_vif *vif;
3329 struct ieee80211_node *ni;
3330 struct lkpi_sta *lsta;
3331 struct ieee80211_sta *sta;
3332 struct ieee80211_prep_tx_info prep_tx_info;
3333 #if 0
3334 enum ieee80211_bss_changed bss_changed;
3335 #endif
3336 struct ieee80211_rx_ampdu *rap;
3337 int error;
3338
3339 lhw = vap->iv_ic->ic_softc;
3340 hw = LHW_TO_HW(lhw);
3341 lvif = VAP_TO_LVIF(vap);
3342 vif = LVIF_TO_VIF(lvif);
3343
3344 IEEE80211_UNLOCK(vap->iv_ic);
3345 wiphy_lock(hw->wiphy);
3346
3347 LKPI_80211_LVIF_LOCK(lvif);
3348 #ifdef LINUXKPI_DEBUG_80211
3349 /* XXX-BZ KASSERT later; state going down so no action. */
3350 if (lvif->lvif_bss == NULL)
3351 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3352 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3353 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3354 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3355 lvif->lvif_bss_synched);
3356 #endif
3357 lsta = lvif->lvif_bss;
3358 LKPI_80211_LVIF_UNLOCK(lvif);
3359 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3360 "lvif %p vap %p\n", __func__,
3361 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3362
3363 ni = lsta->ni; /* Reference held for lvif_bss. */
3364 sta = LSTA_TO_STA(lsta);
3365
3366 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3367
3368 /* flush, drop. */
3369 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
3370
3371 /* We should make this a KASSERT. */
3372 if (lsta->in_mgd) {
3373 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p lsta %p in_mgd\n",
3374 __func__, __LINE__, lvif, vap, lsta);
3375 }
3376 /*
3377 * Problem is that we should hook into the tx/rx flow and not
3378 * try to re-model the state machine parts. We may miss a SME
3379 * triggered frame this way.
3380 */
3381 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3382 if (nstate == IEEE80211_S_ASSOC) {
3383 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
3384 if (arg)
3385 prep_tx_info.subtype = IEEE80211_STYPE_REASSOC_REQ;
3386 else
3387 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3388 } else {
3389 /* wpa_supplicant upon RTM_IEEE80211_LEAVE. */
3390 prep_tx_info.subtype = IEEE80211_STYPE_DISASSOC;
3391 }
3392 } else
3393 prep_tx_info.subtype = IEEE80211_STYPE_DEAUTH;
3394 prep_tx_info.was_assoc = true;
3395 prep_tx_info.link_id = 0;
3396 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3397 lsta->in_mgd = true;
3398
3399 wiphy_unlock(hw->wiphy);
3400 IEEE80211_LOCK(vap->iv_ic);
3401
3402 /* Call iv_newstate first so we get potential (RE-)ASSOC/DEAUTH? packet out. */
3403 error = lvif->iv_newstate(vap, nstate, arg);
3404 if (error != 0) {
3405 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3406 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3407 goto outni;
3408 }
3409
3410 /* Stop any BA sessions if still active. */
3411 for (int rapn = 0; rapn < WME_NUM_TID; rapn++) {
3412 rap = &ni->ni_rx_ampdu[rapn];
3413
3414 if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
3415 continue;
3416
3417 vap->iv_ic->ic_ampdu_rx_stop(ni, rap);
3418 }
3419
3420 IEEE80211_UNLOCK(vap->iv_ic);
3421
3422 /* Ensure the packets get out. */
3423 lkpi_80211_flush_tx(lhw, lsta);
3424
3425 wiphy_lock(hw->wiphy);
3426
3427 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3428
3429 /* Wake tx queues to get packet(s) out. */
3430 lkpi_wake_tx_queues(hw, sta, false, true);
3431
3432 /* flush, no drop */
3433 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
3434
3435 /* End mgd_complete_tx. */
3436 /* We should make this a KASSERT. */
3437 if (!lsta->in_mgd) {
3438 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p lsta %p !in_mgd\n",
3439 __func__, __LINE__, lvif, vap, lsta);
3440 }
3441 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3442 lsta->in_mgd = false;
3443
3444 #if 0
3445 /* sync_rx_queues */
3446 lkpi_80211_mo_sync_rx_queues(hw);
3447
3448 /* sta_pre_rcu_remove */
3449 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3450 #endif
3451
3452 /* Take the station down. */
3453
3454 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3455 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3456 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3457 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3458 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3459 if (error != 0) {
3460 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3461 "failed: %d\n", __func__, __LINE__, error);
3462 goto out;
3463 }
3464
3465 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3466
3467 #ifdef LKPI_80211_HW_CRYPTO
3468 if (lkpi_hwcrypto) {
3469 error = lkpi_sta_del_keys(hw, vif, lsta);
3470 if (error != 0) {
3471 ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3472 "failed: %d\n", __func__, __LINE__, error);
3473 /*
3474 * Either drv/fw will crash or cleanup itself,
3475 * otherwise net80211 will delete the keys (at a
3476 * less appropriate time).
3477 */
3478 /* goto out; */
3479 }
3480 }
3481 #endif
3482
3483 /* Update sta_state (ASSOC to AUTH). */
3484 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3485 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3486 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3487 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3488 if (error != 0) {
3489 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3490 "failed: %d\n", __func__, __LINE__, error);
3491 goto out;
3492 }
3493
3494 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3495
3496 #if 0
3497 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
3498 /* See comment in DOWN4. */
3499 lkpi_disassoc(sta, vif, lhw);
3500 #endif
3501
3502 error = EALREADY;
3503 out:
3504 wiphy_unlock(hw->wiphy);
3505 IEEE80211_LOCK(vap->iv_ic);
3506 outni:
3507 return (error);
3508 }
3509
3510 /*
3511 * DOWN2
3512 * We are in state 2 and go back to state 1 and will try to auth again
3513 * (to IEEE80211_S_AUTH in FreeBSD means "try to auth"). This should be
3514 * like scan_to_auth but that we keep the "ni" and with that chanctx/bssid,
3515 * which essentially makes this "a_to_a" in LinuxKPI.
3516 */
3517 static int
lkpi_sta_assoc_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3518 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3519 {
3520 struct lkpi_hw *lhw;
3521 struct ieee80211_hw *hw;
3522 struct lkpi_vif *lvif;
3523 struct ieee80211_vif *vif;
3524 struct ieee80211_node *ni;
3525 struct lkpi_sta *lsta;
3526 struct ieee80211_prep_tx_info prep_tx_info;
3527 int error;
3528
3529 lhw = vap->iv_ic->ic_softc;
3530 hw = LHW_TO_HW(lhw);
3531 lvif = VAP_TO_LVIF(vap);
3532 vif = LVIF_TO_VIF(lvif);
3533
3534 IEEE80211_UNLOCK(vap->iv_ic);
3535 wiphy_lock(hw->wiphy);
3536
3537 LKPI_80211_LVIF_LOCK(lvif);
3538 #ifdef LINUXKPI_DEBUG_80211
3539 /* XXX-BZ KASSERT later; state going down so no action. */
3540 if (lvif->lvif_bss == NULL)
3541 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3542 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3543 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3544 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3545 lvif->lvif_bss_synched);
3546 #endif
3547 lsta = lvif->lvif_bss;
3548 LKPI_80211_LVIF_UNLOCK(lvif);
3549 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3550 "lvif %p vap %p\n", __func__,
3551 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3552
3553 ni = lsta->ni; /* Reference held for lvif_bss. */
3554
3555 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3556
3557 /* End mgd_complete_tx. */
3558 if (lsta->in_mgd && vap->iv_state == IEEE80211_S_ASSOC) {
3559 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3560 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3561 prep_tx_info.link_id = 0;
3562 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3563 lsta->in_mgd = false;
3564 } else if (lsta->in_mgd) {
3565 ic_printf(vap->iv_ic, "%s:%d: in_mgd %d (%s) -> %d (%s) %d\n",
3566 __func__, __LINE__,
3567 vap->iv_state, ieee80211_state_name[vap->iv_state],
3568 nstate, ieee80211_state_name[nstate], arg);
3569 }
3570
3571 /* Take the station down. */
3572 /* Update sta_state (AUTH to NONE). */
3573 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3574 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3575 "AUTH: %#x\n", __func__, lsta, lsta->state));
3576 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3577 if (error != 0) {
3578 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3579 "failed: %d\n", __func__, __LINE__, error);
3580 goto out;
3581 }
3582
3583 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3584
3585 out:
3586 wiphy_unlock(hw->wiphy);
3587 IEEE80211_LOCK(vap->iv_ic);
3588 return (error);
3589 }
3590
3591 /*
3592 * DOWN3
3593 * We are in state 1. Either auth timed out (arg != 0) or we have an internal
3594 * state change forcing us to give up trying to authenticate.
3595 * Cleanup and remove chanctx, sta, ...
3596 */
3597 static int
lkpi_sta_auth_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3598 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3599 {
3600 struct lkpi_hw *lhw;
3601 struct ieee80211_hw *hw;
3602 struct lkpi_vif *lvif;
3603 struct ieee80211_vif *vif;
3604 struct ieee80211_node *ni;
3605 struct lkpi_sta *lsta;
3606 struct ieee80211_sta *sta;
3607 struct ieee80211_prep_tx_info prep_tx_info;
3608 enum ieee80211_bss_changed bss_changed;
3609 int error;
3610
3611 lhw = vap->iv_ic->ic_softc;
3612 hw = LHW_TO_HW(lhw);
3613 lvif = VAP_TO_LVIF(vap);
3614 vif = LVIF_TO_VIF(lvif);
3615
3616 IEEE80211_UNLOCK(vap->iv_ic);
3617 wiphy_lock(hw->wiphy);
3618
3619 LKPI_80211_LVIF_LOCK(lvif);
3620 /*
3621 * XXX-BZ KASSERT later; state going down so no action in theory
3622 * but try to avoid a NULL-pointer derref for now and gracefully
3623 * fail for non-debug kernels.
3624 */
3625 if (lvif->lvif_bss == NULL) {
3626 ic_printf(vap->iv_ic, "%s:%d: ERROR: lvif %p vap %p iv_bss %p "
3627 "lvif_bss %p lvif_bss->ni %p synched %d; "
3628 "expect follow-up problems\n", __func__, __LINE__,
3629 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3630 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3631 lvif->lvif_bss_synched);
3632 LKPI_80211_LVIF_UNLOCK(lvif);
3633 /*
3634 * This will likely lead to a firmware crash (if there
3635 * was not one before already) and need a
3636 * ieee80211_restart_hw() but still better than a panic
3637 * for users as they can at least recover.
3638 */
3639 error = ENOTRECOVERABLE;
3640 goto out;
3641 }
3642 lsta = lvif->lvif_bss;
3643 LKPI_80211_LVIF_UNLOCK(lvif);
3644 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3645 "lvif %p vap %p\n", __func__,
3646 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3647 ni = lsta->ni; /* Reference held for lvif_bss. */
3648 sta = LSTA_TO_STA(lsta);
3649
3650 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3651
3652 /* flush, drop. */
3653 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
3654
3655 /* Wake tx queues to get packet(s) out. */
3656 lkpi_wake_tx_queues(hw, sta, false, true);
3657
3658 /* flush, no drop */
3659 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
3660
3661 /* End mgd_complete_tx. */
3662 if (lsta->in_mgd) {
3663 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3664 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
3665 prep_tx_info.link_id = 0;
3666 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3667 lsta->in_mgd = false;
3668 }
3669
3670 /* sync_rx_queues */
3671 lkpi_80211_mo_sync_rx_queues(hw);
3672
3673 #ifdef LKPI_80211_HW_CRYPTO
3674 if (lkpi_hwcrypto) {
3675 error = lkpi_sta_del_keys(hw, vif, lsta);
3676 if (error != 0) {
3677 ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3678 "failed: %d\n", __func__, __LINE__, error);
3679 /*
3680 * Either drv/fw will crash or cleanup itself,
3681 * otherwise net80211 will delete the keys (at a
3682 * less appropriate time).
3683 */
3684 /* goto out; */
3685 }
3686 }
3687 #endif
3688
3689 /* sta_pre_rcu_remove */
3690 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3691
3692 synchronize_net();
3693
3694 /* Take the station down. */
3695
3696 bss_changed = 0;
3697 /*
3698 * Start updating bss info (*bss_info_changed) (assoc, aid, ..).
3699 *
3700 * One would expect this to happen when going off AUTHORIZED but
3701 * not so.
3702 *
3703 * Immediately issuing the (*bss_info_changed) used to also remove the
3704 * sta from firmware for iwlwifi; or we have problems with the sta
3705 * silently not being removed and then crash upon the next sta add.
3706 * Neither seems to be the case or a problem still.
3707 *
3708 * Contrary for BE200 (iwlwifi/mld) if we do not issue the
3709 * (*vif_cfg_change) to tell FW that we are no longer assoc
3710 * it will crash now upon sta rm. So the order now is as we once
3711 * expected it:
3712 *
3713 * 1) lkpi_disassoc(): set vif->cfg.assoc = false and .aid=0
3714 * 2) add the remaining BSS_CHANGED changes and call (*bss_info_changed)
3715 * (which may be split up into (*vif_cfg_change) and
3716 * (*link_info_changed) for more modern drivers).
3717 * 3) call the last sta_state update -> IEEE80211_STA_NOTEXIST
3718 * (removes the sta given assoc is false) and tidy up our lists.
3719 * 4) call unassign_vif_chanctx
3720 * 5) call lkpi_hw_conf_idle
3721 * 6) call remove_chanctx
3722 *
3723 * Note: vif->driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC
3724 * might change this.
3725 */
3726 bss_changed |= lkpi_disassoc(sta, vif, lhw);
3727
3728 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3729
3730 IMPROVE("Any bss_info changes to announce?");
3731 vif->bss_conf.qos = false;
3732 bss_changed |= BSS_CHANGED_QOS;
3733 vif->cfg.ssid_len = 0;
3734 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3735 IEEE80211_ADDR_COPY(vif->cfg.ap_addr, ieee80211broadcastaddr);
3736 bss_changed |= BSS_CHANGED_BSSID;
3737 vif->bss_conf.use_short_preamble = false;
3738 /* XXX BSS_CHANGED_???? */
3739 vif->bss_conf.dtim_period = 0; /* go back to 0. */
3740 bss_changed |= BSS_CHANGED_BEACON_INFO;
3741 lkpi_bss_info_change(hw, vif, bss_changed);
3742
3743 /* Adjust sta and change state (from NONE) to NOTEXIST. */
3744 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3745 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3746 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3747 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3748 if (error != 0) {
3749 IMPROVE("do we need to undo the chan ctx?");
3750 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3751 "failed: %d\n", __func__, __LINE__, error);
3752 goto out;
3753 }
3754
3755 lkpi_lsta_remove(lsta, lvif);
3756
3757 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3758
3759 LKPI_80211_LVIF_LOCK(lvif);
3760 /* Remove ni reference for this cache of lsta. */
3761 lvif->lvif_bss = NULL;
3762 lvif->lvif_bss_synched = false;
3763 LKPI_80211_LVIF_UNLOCK(lvif);
3764
3765 /* conf_tx */
3766
3767 lkpi_remove_chanctx(hw, vif);
3768
3769 out:
3770 wiphy_unlock(hw->wiphy);
3771 IEEE80211_LOCK(vap->iv_ic);
3772 if (error == 0) {
3773 /*
3774 * We do this outside the wiphy lock as net80211::node_free() may call
3775 * into crypto code to delete keys and we have a recursed on
3776 * non-recursive sx panic. Also only do this if we get here w/o error.
3777 *
3778 * The very last release the reference on the ni for the ni/lsta on
3779 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
3780 * and potentially freed.
3781 */
3782 ieee80211_free_node(ni);
3783 }
3784 return (error);
3785 }
3786
3787 /* DOWN4 */
3788 static int
lkpi_sta_scan_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3789 lkpi_sta_scan_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3790 {
3791 /* lkpi_iv_newstate() handles the stop scan case in common code. */
3792 return (lkpi_sta_state_do_nada(vap, nstate, arg));
3793 }
3794
3795 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
3796
3797 static int
lkpi_sta_auth_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3798 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3799 {
3800 int error;
3801
3802 error = lkpi_sta_auth_to_scan(vap, nstate, arg);
3803 if (error == 0)
3804 error = lkpi_sta_scan_to_init(vap, nstate, arg);
3805 return (error);
3806 }
3807
3808 /* auth_to_auth, assoc_to_assoc. */
3809 static int
lkpi_sta_a_to_a(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3810 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3811 {
3812 struct lkpi_hw *lhw;
3813 struct ieee80211_hw *hw;
3814 struct lkpi_vif *lvif;
3815 struct ieee80211_vif *vif;
3816 struct lkpi_sta *lsta;
3817 struct ieee80211_prep_tx_info prep_tx_info;
3818 int error;
3819
3820 lhw = vap->iv_ic->ic_softc;
3821 hw = LHW_TO_HW(lhw);
3822 lvif = VAP_TO_LVIF(vap);
3823 vif = LVIF_TO_VIF(lvif);
3824
3825 IEEE80211_UNLOCK(vap->iv_ic);
3826 wiphy_lock(hw->wiphy);
3827
3828 LKPI_80211_LVIF_LOCK(lvif);
3829 /* XXX-BZ KASSERT later? */
3830 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
3831 #ifdef LINUXKPI_DEBUG_80211
3832 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3833 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3834 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3835 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3836 lvif->lvif_bss_synched);
3837 #endif
3838 LKPI_80211_LVIF_UNLOCK(lvif);
3839 error = ENOTRECOVERABLE;
3840 goto out;
3841 }
3842 lsta = lvif->lvif_bss;
3843 LKPI_80211_LVIF_UNLOCK(lvif);
3844
3845 KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
3846 lsta, lvif, vap));
3847
3848 IMPROVE("event callback?");
3849
3850 /* End mgd_complete_tx. */
3851 if (lsta->in_mgd) {
3852 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3853 if (vap->iv_state == IEEE80211_S_AUTH)
3854 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
3855 else
3856 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3857 prep_tx_info.link_id = 0;
3858 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3859 lsta->in_mgd = false;
3860 }
3861
3862 /* Now start auth/assoc. */
3863
3864 /* Start mgd_prepare_tx. */
3865 if (!lsta->in_mgd) {
3866 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3867 if (nstate == IEEE80211_S_AUTH)
3868 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
3869 else
3870 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3871 prep_tx_info.link_id = 0;
3872 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3873 lsta->in_mgd = true;
3874 }
3875
3876 error = 0;
3877 out:
3878 wiphy_unlock(hw->wiphy);
3879 IEEE80211_LOCK(vap->iv_ic);
3880
3881 return (error);
3882 }
3883
3884 static int
lkpi_sta_assoc_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3885 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3886 {
3887 int error;
3888
3889 error = lkpi_sta_assoc_to_auth(vap, nstate, arg);
3890 if (error != 0 && error != EALREADY)
3891 return (error);
3892
3893 error = lkpi_sta_auth_to_scan(vap, nstate, arg);
3894 return (error);
3895 }
3896
3897 static int
lkpi_sta_assoc_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3898 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3899 {
3900 int error;
3901
3902 error = lkpi_sta_assoc_to_scan(vap, nstate, arg);
3903 if (error != 0 && error != EALREADY)
3904 return (error);
3905
3906 error = lkpi_sta_scan_to_init(vap, nstate, arg); /* do_nada */
3907 return (error);
3908 }
3909
3910 static int
lkpi_sta_run_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3911 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3912 {
3913 int error;
3914
3915 error = lkpi_sta_run_to_assoc(vap, nstate, arg);
3916 if (error != 0 && error != EALREADY)
3917 return (error);
3918
3919 error = lkpi_sta_assoc_to_init(vap, nstate, arg);
3920 return (error);
3921 }
3922
3923 static int
lkpi_sta_run_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3924 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3925 {
3926 int error;
3927
3928 error = lkpi_sta_run_to_assoc(vap, nstate, arg);
3929 if (error != 0 && error != EALREADY)
3930 return (error);
3931
3932 error = lkpi_sta_assoc_to_scan(vap, nstate, arg);
3933 return (error);
3934 }
3935
3936 static int
lkpi_sta_run_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3937 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3938 {
3939 int error;
3940
3941 error = lkpi_sta_run_to_assoc(vap, nstate, arg);
3942 if (error != 0 && error != EALREADY)
3943 return (error);
3944
3945 error = lkpi_sta_assoc_to_auth(vap, nstate, arg);
3946 return (error);
3947 }
3948
3949 /* -------------------------------------------------------------------------- */
3950
3951 /*
3952 * The matches the documented state changes in net80211::sta_newstate().
3953 * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
3954 * there are "invalid" (so there is a room for failure here).
3955 */
3956 struct fsm_state {
3957 /* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
3958 enum ieee80211_state ostate;
3959 enum ieee80211_state nstate;
3960 int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
3961 } sta_state_fsm[] = {
3962 { IEEE80211_S_INIT, IEEE80211_S_INIT, lkpi_sta_state_do_nada },
3963 { IEEE80211_S_SCAN, IEEE80211_S_INIT, lkpi_sta_state_do_nada }, /* DOWN4 scan_to_init */
3964 { IEEE80211_S_AUTH, IEEE80211_S_INIT, lkpi_sta_auth_to_init }, /* not explicitly in sta_newstate() */
3965 { IEEE80211_S_ASSOC, IEEE80211_S_INIT, lkpi_sta_assoc_to_init }, /* Send DEAUTH. */
3966 { IEEE80211_S_RUN, IEEE80211_S_INIT, lkpi_sta_run_to_init }, /* Send DISASSOC. */
3967
3968 { IEEE80211_S_INIT, IEEE80211_S_SCAN, lkpi_sta_init_to_scan }, /* UP1 */
3969 { IEEE80211_S_SCAN, IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
3970 { IEEE80211_S_AUTH, IEEE80211_S_SCAN, lkpi_sta_auth_to_scan }, /* DOWN3 */
3971 { IEEE80211_S_ASSOC, IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3972 { IEEE80211_S_RUN, IEEE80211_S_SCAN, lkpi_sta_run_to_scan }, /* Beacon miss. */
3973
3974 { IEEE80211_S_INIT, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */
3975 { IEEE80211_S_SCAN, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* UP2 Send AUTH. */
3976 { IEEE80211_S_AUTH, IEEE80211_S_AUTH, lkpi_sta_a_to_a }, /* Send ?AUTH. */
3977 { IEEE80211_S_ASSOC, IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth }, /* DOWN2 Send ?AUTH. */
3978 { IEEE80211_S_RUN, IEEE80211_S_AUTH, lkpi_sta_run_to_auth }, /* Send ?AUTH. */
3979
3980 { IEEE80211_S_AUTH, IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc }, /* UP3.1 Send ASSOCREQ. */
3981 { IEEE80211_S_ASSOC, IEEE80211_S_ASSOC, lkpi_sta_a_to_a }, /* Send ASSOCREQ. */
3982 { IEEE80211_S_RUN, IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc }, /* DOWN1 Send ASSOCREQ/REASSOCREQ. */
3983
3984 { IEEE80211_S_AUTH, IEEE80211_S_RUN, lkpi_sta_auth_to_run }, /* UP3.2 */
3985 { IEEE80211_S_ASSOC, IEEE80211_S_RUN, lkpi_sta_assoc_to_run }, /* UP4 */
3986 { IEEE80211_S_RUN, IEEE80211_S_RUN, lkpi_sta_state_do_nada },
3987
3988 /* Dummy at the end without handler. */
3989 { IEEE80211_S_INIT, IEEE80211_S_INIT, NULL },
3990 };
3991
3992 static int
lkpi_iv_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3993 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3994 {
3995 struct ieee80211com *ic;
3996 struct lkpi_hw *lhw;
3997 struct lkpi_vif *lvif;
3998 struct ieee80211_vif *vif;
3999 struct fsm_state *s;
4000 enum ieee80211_state ostate;
4001 int error;
4002
4003 ic = vap->iv_ic;
4004 IEEE80211_LOCK_ASSERT(ic);
4005 ostate = vap->iv_state;
4006
4007 #ifdef LINUXKPI_DEBUG_80211
4008 if (linuxkpi_debug_80211 & D80211_TRACE)
4009 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
4010 __func__, __LINE__, vap, nstate, arg);
4011 #endif
4012
4013 if (vap->iv_opmode == IEEE80211_M_STA) {
4014
4015 lhw = ic->ic_softc;
4016 lvif = VAP_TO_LVIF(vap);
4017 vif = LVIF_TO_VIF(lvif);
4018
4019 /* No need to replicate this in most state handlers. */
4020 if (nstate > IEEE80211_S_SCAN)
4021 lkpi_stop_hw_scan(lhw, vif);
4022
4023 s = sta_state_fsm;
4024
4025 } else {
4026 ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
4027 "vap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
4028 return (ENOSYS);
4029 }
4030
4031 error = 0;
4032 for (; s->handler != NULL; s++) {
4033 if (ostate == s->ostate && nstate == s->nstate) {
4034 #ifdef LINUXKPI_DEBUG_80211
4035 if (linuxkpi_debug_80211 & D80211_TRACE)
4036 ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
4037 " %d (%s): arg %d.\n", __func__,
4038 ostate, ieee80211_state_name[ostate],
4039 nstate, ieee80211_state_name[nstate], arg);
4040 #endif
4041 error = s->handler(vap, nstate, arg);
4042 break;
4043 }
4044 }
4045 IEEE80211_LOCK_ASSERT(vap->iv_ic);
4046
4047 if (s->handler == NULL) {
4048 IMPROVE("turn this into a KASSERT\n");
4049 ic_printf(vap->iv_ic, "%s: unsupported state transition "
4050 "%d (%s) -> %d (%s)\n", __func__,
4051 ostate, ieee80211_state_name[ostate],
4052 nstate, ieee80211_state_name[nstate]);
4053 return (ENOSYS);
4054 }
4055
4056 if (error == EALREADY) {
4057 #ifdef LINUXKPI_DEBUG_80211
4058 if (linuxkpi_debug_80211 & D80211_TRACE)
4059 ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
4060 "%d (%s): iv_newstate already handled: %d.\n",
4061 __func__, ostate, ieee80211_state_name[ostate],
4062 nstate, ieee80211_state_name[nstate], error);
4063 #endif
4064 return (0);
4065 }
4066
4067 if (error != 0) {
4068 ic_printf(vap->iv_ic, "%s: error %d during state transition "
4069 "%d (%s) -> %d (%s)\n", __func__, error,
4070 ostate, ieee80211_state_name[ostate],
4071 nstate, ieee80211_state_name[nstate]);
4072 return (error);
4073 }
4074
4075 #ifdef LINUXKPI_DEBUG_80211
4076 if (linuxkpi_debug_80211 & D80211_TRACE)
4077 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
4078 "calling net80211 parent\n",
4079 __func__, __LINE__, vap, nstate, arg);
4080 #endif
4081
4082 return (lvif->iv_newstate(vap, nstate, arg));
4083 }
4084
4085 /* -------------------------------------------------------------------------- */
4086
4087 /*
4088 * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
4089 * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
4090 * new node without us knowing and thus our ni/lsta are out of sync.
4091 */
4092 static struct ieee80211_node *
lkpi_iv_update_bss(struct ieee80211vap * vap,struct ieee80211_node * ni)4093 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
4094 {
4095 struct lkpi_vif *lvif;
4096 struct ieee80211_node *rni;
4097
4098 IEEE80211_LOCK_ASSERT(vap->iv_ic);
4099
4100 lvif = VAP_TO_LVIF(vap);
4101
4102 LKPI_80211_LVIF_LOCK(lvif);
4103 lvif->lvif_bss_synched = false;
4104 LKPI_80211_LVIF_UNLOCK(lvif);
4105
4106 rni = lvif->iv_update_bss(vap, ni);
4107 return (rni);
4108 }
4109
4110 #ifdef LKPI_80211_WME
4111 static int
lkpi_wme_update(struct lkpi_hw * lhw,struct ieee80211vap * vap,bool planned)4112 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
4113 {
4114 struct ieee80211com *ic;
4115 struct ieee80211_hw *hw;
4116 struct lkpi_vif *lvif;
4117 struct ieee80211_vif *vif;
4118 struct chanAccParams chp;
4119 struct wmeParams wmeparr[WME_NUM_AC];
4120 struct ieee80211_tx_queue_params txqp;
4121 enum ieee80211_bss_changed bss_changed;
4122 int error;
4123 uint16_t ac;
4124
4125 hw = LHW_TO_HW(lhw);
4126 lockdep_assert_wiphy(hw->wiphy);
4127
4128 IMPROVE();
4129 KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
4130 "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
4131
4132 if (vap == NULL)
4133 return (0);
4134
4135 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
4136 return (0);
4137
4138 if (lhw->ops->conf_tx == NULL)
4139 return (0);
4140
4141 if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
4142 lhw->update_wme = true;
4143 return (0);
4144 }
4145 lhw->update_wme = false;
4146
4147 ic = lhw->ic;
4148 ieee80211_wme_ic_getparams(ic, &chp);
4149 IEEE80211_LOCK(ic);
4150 for (ac = 0; ac < WME_NUM_AC; ac++)
4151 wmeparr[ac] = chp.cap_wmeParams[ac];
4152 IEEE80211_UNLOCK(ic);
4153
4154 lvif = VAP_TO_LVIF(vap);
4155 vif = LVIF_TO_VIF(lvif);
4156
4157 /* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
4158 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4159 struct wmeParams *wmep;
4160
4161 wmep = &wmeparr[ac];
4162 bzero(&txqp, sizeof(txqp));
4163 txqp.cw_min = wmep->wmep_logcwmin;
4164 txqp.cw_max = wmep->wmep_logcwmax;
4165 txqp.txop = wmep->wmep_txopLimit;
4166 txqp.aifs = wmep->wmep_aifsn;
4167 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
4168 if (error != 0)
4169 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
4170 __func__, ac, error);
4171 }
4172 bss_changed = BSS_CHANGED_QOS;
4173 if (!planned)
4174 lkpi_bss_info_change(hw, vif, bss_changed);
4175
4176 return (bss_changed);
4177 }
4178 #endif
4179
4180 static int
lkpi_ic_wme_update(struct ieee80211com * ic)4181 lkpi_ic_wme_update(struct ieee80211com *ic)
4182 {
4183 #ifdef LKPI_80211_WME
4184 struct ieee80211vap *vap;
4185 struct lkpi_hw *lhw;
4186 struct ieee80211_hw *hw;
4187
4188 IMPROVE("Use the per-VAP callback in net80211.");
4189 vap = TAILQ_FIRST(&ic->ic_vaps);
4190 if (vap == NULL)
4191 return (0);
4192
4193 lhw = ic->ic_softc;
4194 hw = LHW_TO_HW(lhw);
4195
4196 wiphy_lock(hw->wiphy);
4197 lkpi_wme_update(lhw, vap, false);
4198 wiphy_unlock(hw->wiphy);
4199 #endif
4200 return (0); /* unused */
4201 }
4202
4203 static void
lkpi_iv_sta_recv_mgmt(struct ieee80211_node * ni,struct mbuf * m0,int subtype,const struct ieee80211_rx_stats * rxs,int rssi,int nf)4204 lkpi_iv_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
4205 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
4206 {
4207 struct lkpi_hw *lhw;
4208 struct ieee80211_hw *hw;
4209 struct lkpi_vif *lvif;
4210 struct ieee80211_vif *vif;
4211 enum ieee80211_bss_changed bss_changed;
4212
4213 lvif = VAP_TO_LVIF(ni->ni_vap);
4214 vif = LVIF_TO_VIF(lvif);
4215
4216 lvif->iv_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
4217
4218 switch (subtype) {
4219 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
4220 break;
4221 case IEEE80211_FC0_SUBTYPE_BEACON:
4222 /*
4223 * Only count beacons when assoc. SCAN has its own logging.
4224 * This is for connection/beacon loss/session protection almost
4225 * over debugging when trying to get into a stable RUN state.
4226 */
4227 if (vif->cfg.assoc)
4228 lvif->beacons++;
4229 break;
4230 default:
4231 return;
4232 }
4233
4234 lhw = ni->ni_ic->ic_softc;
4235 hw = LHW_TO_HW(lhw);
4236
4237 /*
4238 * If this direct call to mo_bss_info_changed will not work due to
4239 * locking, see if queue_work() is fast enough.
4240 */
4241 bss_changed = lkpi_update_dtim_tsf(vif, ni, ni->ni_vap, __func__, __LINE__);
4242 lkpi_bss_info_change(hw, vif, bss_changed);
4243 }
4244
4245 /*
4246 * Change link-layer address on the vif (if the vap is not started/"UP").
4247 * This can happen if a user changes 'ether' using ifconfig.
4248 * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
4249 * we do use a per-[l]vif event handler to be sure we exist as we
4250 * cannot assume that from every vap derives a vif and we have a hard
4251 * time checking based on net80211 information.
4252 * Should this ever become a real problem we could add a callback function
4253 * to wlan_iflladdr() to be set optionally but that would be for a
4254 * single-consumer (or needs a list) -- was just too complicated for an
4255 * otherwise perfect mechanism FreeBSD already provides.
4256 */
4257 static void
lkpi_vif_iflladdr(void * arg,struct ifnet * ifp)4258 lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
4259 {
4260 struct epoch_tracker et;
4261 struct ieee80211_vif *vif;
4262
4263 NET_EPOCH_ENTER(et);
4264 /* NB: identify vap's by if_transmit; left as an extra check. */
4265 if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
4266 (if_getflags(ifp) & IFF_UP) != 0) {
4267 NET_EPOCH_EXIT(et);
4268 return;
4269 }
4270
4271 vif = arg;
4272 IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
4273 NET_EPOCH_EXIT(et);
4274 }
4275
4276 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])4277 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
4278 int unit, enum ieee80211_opmode opmode, int flags,
4279 const uint8_t bssid[IEEE80211_ADDR_LEN],
4280 const uint8_t mac[IEEE80211_ADDR_LEN])
4281 {
4282 struct lkpi_hw *lhw;
4283 struct ieee80211_hw *hw;
4284 struct lkpi_vif *lvif;
4285 struct ieee80211vap *vap;
4286 struct ieee80211_vif *vif;
4287 struct ieee80211_tx_queue_params txqp;
4288 enum ieee80211_bss_changed bss_changed;
4289 enum nl80211_band band;
4290 struct sysctl_oid *node;
4291 size_t len;
4292 int error, i;
4293 uint16_t ac;
4294
4295 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* 1 so far. Add <n> once this works. */
4296 return (NULL);
4297
4298 lhw = ic->ic_softc;
4299 hw = LHW_TO_HW(lhw);
4300
4301 len = sizeof(*lvif);
4302 len += hw->vif_data_size; /* vif->drv_priv */
4303
4304 lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
4305 mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
4306 TASK_INIT(&lvif->sw_scan_task, 0, lkpi_sw_scan_task, lvif);
4307 INIT_LIST_HEAD(&lvif->lsta_list);
4308 lvif->lvif_bss = NULL;
4309 refcount_init(&lvif->nt_unlocked, 0);
4310 lvif->lvif_bss_synched = false;
4311 vap = LVIF_TO_VAP(lvif);
4312 vif = LVIF_TO_VIF(lvif);
4313
4314 /*
4315 * Setup legacy br_mask here. We will call (*set_bitrate_mask)
4316 * elsewhere to announce it to the driver but it is a static
4317 * setup.
4318 * Also setup basic_rates with just the mandatory rates for the
4319 * current band (if avail).
4320 */
4321 for (band = 0; band < NUM_NL80211_BANDS; band++) {
4322 struct ieee80211_supported_band *supband;
4323 uint32_t rate_mandatory;;
4324
4325 supband = hw->wiphy->bands[band];
4326 if (supband == NULL || supband->n_bitrates == 0)
4327 continue;
4328
4329 /* Per-band legacy br_mask. */
4330 lvif->br_mask.control[band].legacy = (1 << supband->n_bitrates) - 1;
4331
4332 /* basic_rates for the current band. */
4333 if (hw->conf.chandef.chan == NULL ||
4334 hw->conf.chandef.chan->band != band)
4335 continue;
4336
4337 switch (band) {
4338 case NL80211_BAND_2GHZ:
4339 /* We have to assume 11g support here. */
4340 rate_mandatory = IEEE80211_RATE_MANDATORY_G |
4341 IEEE80211_RATE_MANDATORY_B;
4342 break;
4343 case NL80211_BAND_5GHZ:
4344 rate_mandatory = IEEE80211_RATE_MANDATORY_A;
4345 break;
4346 default:
4347 continue;
4348 }
4349
4350 for (i = 0; i < supband->n_bitrates; i++) {
4351 if ((supband->bitrates[i].flags & rate_mandatory) != 0)
4352 vif->bss_conf.basic_rates |= BIT(i);
4353 }
4354 }
4355
4356 memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
4357 vif->p2p = false;
4358 vif->probe_req_reg = false;
4359 vif->type = lkpi_opmode_to_vif_type(opmode);
4360 lvif->wdev.wiphy = hw->wiphy;
4361 lvif->wdev.iftype = vif->type;
4362 wiphy_lock(hw->wiphy);
4363 list_add_rcu(&lvif->wdev.list, &hw->wiphy->wdev_list);
4364 wiphy_unlock(hw->wiphy);
4365 memcpy(lvif->wdev.address, mac, IEEE80211_ADDR_LEN);
4366 #if 0
4367 /* Need to fill in other fields as well. */
4368 struct net_device *netdev; /* When do we create this? */
4369 uint32_t radio_mask;
4370 #endif
4371 IMPROVE("wdev");
4372
4373 /* Create a chanctx to be used later. */
4374 IMPROVE("lkpi_alloc_lchanctx reserved as many as can be");
4375 (void) lkpi_find_lchanctx_reserved(hw, lvif);
4376
4377 /* XXX-BZ hardcoded for now! */
4378 #if 1
4379 RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
4380 vif->bss_conf.vif = vif;
4381 /* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
4382 IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
4383 lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
4384 lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
4385 vif->bss_conf.link_id = 0; /* Non-MLO operation. */
4386 vif->bss_conf.chanreq.oper.chan = lhw->dflt_chandef.chan;
4387 vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
4388 vif->bss_conf.use_short_preamble = false; /* vap->iv_flags IEEE80211_F_SHPREAMBLE */
4389 vif->bss_conf.use_short_slot = false; /* vap->iv_flags IEEE80211_F_SHSLOT */
4390 vif->bss_conf.qos = false;
4391 vif->bss_conf.use_cts_prot = false; /* vap->iv_protmode */
4392 vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
4393 IEEE80211_ADDR_COPY(vif->cfg.ap_addr, ieee80211broadcastaddr);
4394 vif->cfg.aid = 0;
4395 vif->cfg.assoc = false;
4396 vif->cfg.idle = true;
4397 vif->cfg.ps = false;
4398 IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
4399 /*
4400 * We need to initialize it to something as the bss_info_changed call
4401 * will try to copy from it in iwlwifi and NULL is a panic.
4402 * We will set the proper one in scan_to_auth() before being assoc.
4403 */
4404 vif->bss_conf.bssid = ieee80211broadcastaddr;
4405 #endif
4406 #if 0
4407 vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
4408 IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
4409 vif->bss_conf.beacon_int = ic->ic_bintval;
4410 /* iwlwifi bug. */
4411 if (vif->bss_conf.beacon_int < 16)
4412 vif->bss_conf.beacon_int = 16;
4413 #endif
4414
4415 /* Link Config */
4416 vif->link_conf[0] = &vif->bss_conf;
4417 for (i = 0; i < nitems(vif->link_conf); i++) {
4418 IMPROVE("more than 1 link one day");
4419 }
4420
4421 /* Setup queue defaults; driver may override in (*add_interface). */
4422 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
4423 if (ieee80211_hw_check(hw, QUEUE_CONTROL))
4424 vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
4425 else if (hw->queues >= IEEE80211_NUM_ACS)
4426 vif->hw_queue[i] = i;
4427 else
4428 vif->hw_queue[i] = 0;
4429
4430 /* Initialize the queue to running. Stopped? */
4431 lvif->hw_queue_stopped[i] = false;
4432 }
4433 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
4434
4435 IMPROVE();
4436
4437 wiphy_lock(hw->wiphy);
4438 error = lkpi_80211_mo_start(hw);
4439 if (error != 0) {
4440 wiphy_unlock(hw->wiphy);
4441 ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
4442 mtx_destroy(&lvif->mtx);
4443 free(lvif, M_80211_VAP);
4444 return (NULL);
4445 }
4446
4447 error = lkpi_80211_mo_add_interface(hw, vif);
4448 if (error != 0) {
4449 IMPROVE(); /* XXX-BZ mo_stop()? */
4450 wiphy_unlock(hw->wiphy);
4451 ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
4452 mtx_destroy(&lvif->mtx);
4453 free(lvif, M_80211_VAP);
4454 return (NULL);
4455 }
4456 wiphy_unlock(hw->wiphy);
4457
4458 LKPI_80211_LHW_LVIF_LOCK(lhw);
4459 TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
4460 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4461
4462 /* Set bss_info. */
4463 bss_changed = 0;
4464 lkpi_bss_info_change(hw, vif, bss_changed);
4465
4466 /* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
4467 IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
4468 wiphy_lock(hw->wiphy);
4469 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4470
4471 bzero(&txqp, sizeof(txqp));
4472 txqp.cw_min = 15;
4473 txqp.cw_max = 1023;
4474 txqp.txop = 0;
4475 txqp.aifs = 2;
4476 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
4477 if (error != 0)
4478 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
4479 __func__, ac, error);
4480 }
4481 bss_changed = BSS_CHANGED_QOS;
4482 lkpi_bss_info_change(hw, vif, bss_changed);
4483
4484 /* Force MC init. */
4485 lkpi_update_mcast_filter_locked(ic);
4486
4487 wiphy_unlock(hw->wiphy);
4488
4489 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
4490
4491 /* Now we have a valid vap->iv_ifp. Any checksum offloading goes below. */
4492
4493 IMPROVE();
4494
4495 /* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
4496 lvif->iv_newstate = vap->iv_newstate;
4497 vap->iv_newstate = lkpi_iv_newstate;
4498 lvif->iv_update_bss = vap->iv_update_bss;
4499 vap->iv_update_bss = lkpi_iv_update_bss;
4500 lvif->iv_recv_mgmt = vap->iv_recv_mgmt;
4501 vap->iv_recv_mgmt = lkpi_iv_sta_recv_mgmt;
4502
4503 #ifdef LKPI_80211_HW_CRYPTO
4504 /* Key management. */
4505 if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
4506 vap->iv_key_set = lkpi_iv_key_set;
4507 vap->iv_key_delete = lkpi_iv_key_delete;
4508 vap->iv_key_update_begin = lkpi_iv_key_update_begin;
4509 vap->iv_key_update_end = lkpi_iv_key_update_end;
4510 }
4511 #endif
4512
4513 #ifdef LKPI_80211_HT
4514 /* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
4515 #endif
4516
4517 ieee80211_ratectl_init(vap);
4518
4519 /* Complete setup. */
4520 ieee80211_vap_attach(vap, ieee80211_media_change,
4521 ieee80211_media_status, mac);
4522
4523 #ifdef LKPI_80211_HT
4524 /*
4525 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
4526 * to do so if they cannot do the crypto too.
4527 */
4528 if (!lkpi_hwcrypto && IEEE80211_CONF_AMPDU_OFFLOAD(ic))
4529 vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
4530 #endif
4531
4532 if (hw->max_listen_interval == 0)
4533 hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
4534 hw->conf.listen_interval = hw->max_listen_interval;
4535
4536 wiphy_lock(hw->wiphy);
4537 /* XXX-BZ do we need to be able to update these? */
4538 hw->wiphy->frag_threshold = vap->iv_fragthreshold;
4539 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
4540 hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
4541 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
4542 wiphy_unlock(hw->wiphy);
4543 /* any others? */
4544
4545 /* Add per-VIF/VAP sysctls. */
4546 sysctl_ctx_init(&lvif->sysctl_ctx);
4547
4548 node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
4549 SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
4550 OID_AUTO, if_name(vap->iv_ifp),
4551 CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
4552
4553 SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
4554 SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
4555 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
4556 lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
4557 SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
4558 SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas_queues",
4559 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_SKIP, lvif, 0,
4560 lkpi_80211_dump_sta_queues, "A",
4561 "Dump queue statistics for any sta of this vif");
4562
4563 IMPROVE();
4564
4565 return (vap);
4566 }
4567
4568 void
linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw * hw)4569 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
4570 {
4571
4572 wiphy_unregister(hw->wiphy);
4573 linuxkpi_ieee80211_ifdetach(hw);
4574
4575 IMPROVE();
4576 }
4577
4578 void
linuxkpi_ieee80211_restart_hw(struct ieee80211_hw * hw)4579 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
4580 {
4581
4582 TODO();
4583 }
4584
4585 static void
lkpi_ic_vap_delete(struct ieee80211vap * vap)4586 lkpi_ic_vap_delete(struct ieee80211vap *vap)
4587 {
4588 struct ieee80211com *ic;
4589 struct lkpi_hw *lhw;
4590 struct ieee80211_hw *hw;
4591 struct lkpi_vif *lvif;
4592 struct ieee80211_vif *vif;
4593
4594 lvif = VAP_TO_LVIF(vap);
4595 vif = LVIF_TO_VIF(lvif);
4596 ic = vap->iv_ic;
4597 lhw = ic->ic_softc;
4598 hw = LHW_TO_HW(lhw);
4599
4600 EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
4601
4602 /* Clear up per-VIF/VAP sysctls. */
4603 sysctl_ctx_free(&lvif->sysctl_ctx);
4604
4605 ieee80211_draintask(ic, &lvif->sw_scan_task);
4606
4607 LKPI_80211_LHW_LVIF_LOCK(lhw);
4608 TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
4609 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4610
4611 wiphy_lock(hw->wiphy);
4612 list_del_rcu(&lvif->wdev.list);
4613 wiphy_unlock(hw->wiphy);
4614
4615 ieee80211_ratectl_deinit(vap);
4616 ieee80211_vap_detach(vap);
4617
4618 IMPROVE("clear up other bits in this state");
4619
4620 lkpi_80211_mo_remove_interface(hw, vif);
4621
4622 /* Single VAP, so we can do this here. */
4623 lkpi_80211_mo_stop(hw, false); /* XXX SUSPEND */
4624
4625 mtx_destroy(&lvif->mtx);
4626 free(lvif, M_80211_VAP);
4627 }
4628
4629 static void
lkpi_ic_update_mcast(struct ieee80211com * ic)4630 lkpi_ic_update_mcast(struct ieee80211com *ic)
4631 {
4632 struct ieee80211vap *vap;
4633 struct lkpi_hw *lhw;
4634
4635 lhw = ic->ic_softc;
4636
4637 LKPI_80211_LHW_MC_LOCK(lhw);
4638 /* Cleanup anything on the current list. */
4639 lkpi_cleanup_mcast_list_locked(lhw);
4640
4641 /* Build up the new list (or allmulti). */
4642 if (ic->ic_allmulti == 0) {
4643 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
4644 if_foreach_llmaddr(vap->iv_ifp,
4645 lkpi_ic_update_mcast_copy, &lhw->mc_list);
4646 lhw->mc_all_multi = false;
4647 } else {
4648 lhw->mc_all_multi = true;
4649 }
4650 LKPI_80211_LHW_MC_UNLOCK(lhw);
4651
4652 lkpi_update_mcast_filter(ic);
4653 TRACEOK();
4654 }
4655
4656 static void
lkpi_ic_update_promisc(struct ieee80211com * ic)4657 lkpi_ic_update_promisc(struct ieee80211com *ic)
4658 {
4659
4660 UNIMPLEMENTED;
4661 }
4662
4663 static void
lkpi_ic_update_chw(struct ieee80211com * ic)4664 lkpi_ic_update_chw(struct ieee80211com *ic)
4665 {
4666
4667 UNIMPLEMENTED;
4668 }
4669
4670 /* Start / stop device. */
4671 static void
lkpi_ic_parent(struct ieee80211com * ic)4672 lkpi_ic_parent(struct ieee80211com *ic)
4673 {
4674 struct lkpi_hw *lhw;
4675 struct ieee80211_hw *hw;
4676 #ifdef HW_START_STOP
4677 int error;
4678 #endif
4679 bool start_all;
4680
4681 IMPROVE();
4682
4683 lhw = ic->ic_softc;
4684 hw = LHW_TO_HW(lhw);
4685 start_all = false;
4686
4687 /* IEEE80211_UNLOCK(ic); */
4688 wiphy_lock(hw->wiphy);
4689 if (ic->ic_nrunning > 0) {
4690 #ifdef HW_START_STOP
4691 error = lkpi_80211_mo_start(hw);
4692 if (error == 0)
4693 #endif
4694 start_all = true;
4695 } else {
4696 #ifdef HW_START_STOP
4697 lkpi_80211_mo_stop(hw, false); /* XXX SUSPEND */
4698 #endif
4699 }
4700 wiphy_unlock(hw->wiphy);
4701 /* IEEE80211_LOCK(ic); */
4702
4703 if (start_all)
4704 ieee80211_start_all(ic);
4705 }
4706
4707 bool
linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie,const u8 * ie_ids,size_t ie_ids_len)4708 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
4709 size_t ie_ids_len)
4710 {
4711 int i;
4712
4713 for (i = 0; i < ie_ids_len; i++) {
4714 if (ie == *ie_ids)
4715 return (true);
4716 }
4717
4718 return (false);
4719 }
4720
4721 /* Return true if skipped; false if error. */
4722 bool
linuxkpi_ieee80211_ie_advance(size_t * xp,const u8 * ies,size_t ies_len)4723 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
4724 {
4725 size_t x;
4726 uint8_t l;
4727
4728 x = *xp;
4729
4730 KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
4731 __func__, x, ies_len, ies));
4732 l = ies[x + 1];
4733 x += 2 + l;
4734
4735 if (x > ies_len)
4736 return (false);
4737
4738 *xp = x;
4739 return (true);
4740 }
4741
4742 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)4743 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
4744 uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
4745 {
4746 struct ieee80211_supported_band *supband;
4747 struct linuxkpi_ieee80211_channel *channels;
4748 struct ieee80211com *ic;
4749 const struct ieee80211_channel *chan;
4750 const struct ieee80211_rateset *rs;
4751 uint8_t *pb;
4752 int band, i;
4753
4754 ic = vap->iv_ic;
4755 for (band = 0; band < NUM_NL80211_BANDS; band++) {
4756 if ((band_mask & (1 << band)) == 0)
4757 continue;
4758
4759 supband = hw->wiphy->bands[band];
4760 /*
4761 * This should not happen;
4762 * band_mask is a bitmask of valid bands to scan on.
4763 */
4764 if (supband == NULL || supband->n_channels == 0)
4765 continue;
4766
4767 /* Find a first channel to get the mode and rates from. */
4768 channels = supband->channels;
4769 chan = NULL;
4770 for (i = 0; i < supband->n_channels; i++) {
4771 uint32_t flags;
4772
4773 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4774 continue;
4775
4776 flags = 0;
4777 switch (band) {
4778 case NL80211_BAND_2GHZ:
4779 flags |= IEEE80211_CHAN_G;
4780 break;
4781 case NL80211_BAND_5GHZ:
4782 flags |= IEEE80211_CHAN_A;
4783 break;
4784 default:
4785 panic("%s:%d: unupported band %d\n",
4786 __func__, __LINE__, band);
4787 }
4788
4789 chan = ieee80211_find_channel(ic,
4790 channels[i].center_freq, flags);
4791 if (chan != NULL)
4792 break;
4793 }
4794
4795 /* This really should not happen. */
4796 if (chan == NULL)
4797 continue;
4798
4799 pb = p;
4800 rs = ieee80211_get_suprates(ic, chan); /* calls chan2mode */
4801 p = ieee80211_add_rates(p, rs);
4802 p = ieee80211_add_xrates(p, rs);
4803
4804 #if defined(LKPI_80211_HT)
4805 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
4806 struct ieee80211_channel *c;
4807
4808 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4809 vap->iv_flags_ht);
4810 p = ieee80211_add_htcap_ch(p, vap, c);
4811 }
4812 #endif
4813 #if defined(LKPI_80211_VHT)
4814 if (band == NL80211_BAND_5GHZ &&
4815 (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
4816 struct ieee80211_channel *c;
4817
4818 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4819 vap->iv_flags_ht);
4820 c = ieee80211_vht_adjust_channel(ic, c,
4821 vap->iv_vht_flags);
4822 p = ieee80211_add_vhtcap_ch(p, vap, c);
4823 }
4824 #endif
4825
4826 scan_ies->ies[band] = pb;
4827 scan_ies->len[band] = p - pb;
4828 }
4829
4830 /* Add common_ies */
4831 pb = p;
4832 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4833 vap->iv_wpa_ie != NULL) {
4834 memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
4835 p += 2 + vap->iv_wpa_ie[1];
4836 }
4837 if (vap->iv_appie_probereq != NULL) {
4838 memcpy(p, vap->iv_appie_probereq->ie_data,
4839 vap->iv_appie_probereq->ie_len);
4840 p += vap->iv_appie_probereq->ie_len;
4841 }
4842 scan_ies->common_ies = pb;
4843 scan_ies->common_ie_len = p - pb;
4844
4845 return (p);
4846 }
4847
4848 static void
lkpi_enable_hw_scan(struct lkpi_hw * lhw)4849 lkpi_enable_hw_scan(struct lkpi_hw *lhw)
4850 {
4851
4852 if (lhw->ops->hw_scan) {
4853 /*
4854 * Advertise full-offload scanning.
4855 *
4856 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4857 * we essentially disable hw_scan for all drivers not setting
4858 * the flag.
4859 */
4860 lhw->ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4861 lhw->scan_flags |= LKPI_LHW_SCAN_HW;
4862 }
4863 }
4864
4865 #ifndef LKPI_80211_USE_SCANLIST
4866 static const uint32_t chan_pri[] = {
4867 5180, 5500, 5745,
4868 5260, 5580, 5660, 5825,
4869 5220, 5300, 5540, 5620, 5700, 5785, 5865,
4870 2437, 2412, 2422, 2462, 2472, 2432, 2452
4871 };
4872
4873 static int
lkpi_scan_chan_list_idx(const struct linuxkpi_ieee80211_channel * lc)4874 lkpi_scan_chan_list_idx(const struct linuxkpi_ieee80211_channel *lc)
4875 {
4876 int i;
4877
4878 for (i = 0; i < nitems(chan_pri); i++) {
4879 if (lc->center_freq == chan_pri[i])
4880 return (i);
4881 }
4882
4883 return (-1);
4884 }
4885
4886 static int
lkpi_scan_chan_list_comp(const struct linuxkpi_ieee80211_channel * lc1,const struct linuxkpi_ieee80211_channel * lc2)4887 lkpi_scan_chan_list_comp(const struct linuxkpi_ieee80211_channel *lc1,
4888 const struct linuxkpi_ieee80211_channel *lc2)
4889 {
4890 int idx1, idx2;
4891
4892 /* Find index in list. */
4893 idx1 = lkpi_scan_chan_list_idx(lc1);
4894 idx2 = lkpi_scan_chan_list_idx(lc2);
4895
4896 if (idx1 == -1 && idx2 != -1)
4897 return (1);
4898 if (idx1 != -1 && idx2 == -1)
4899 return (-1);
4900
4901 /* Neither on the list, use center_freq. */
4902 if (idx1 == -1 && idx2 == -1)
4903 return (lc1->center_freq - lc2->center_freq);
4904
4905 /* Whichever is first in the list. */
4906 return (idx1 - idx2);
4907 }
4908
4909 static void
lkpi_scan_chan_list_resort(struct linuxkpi_ieee80211_channel ** cpp,size_t nchan)4910 lkpi_scan_chan_list_resort(struct linuxkpi_ieee80211_channel **cpp, size_t nchan)
4911 {
4912 struct linuxkpi_ieee80211_channel *lc, *nc;
4913 size_t i, j;
4914 int rc;
4915
4916 for (i = (nchan - 1); i > 0; i--) {
4917 for (j = i; j > 0 ; j--) {
4918 lc = *(cpp + j);
4919 nc = *(cpp + j - 1);
4920 rc = lkpi_scan_chan_list_comp(lc, nc);
4921 if (rc < 0) {
4922 *(cpp + j) = nc;
4923 *(cpp + j - 1) = lc;
4924 }
4925 }
4926 }
4927 }
4928
4929 static bool
lkpi_scan_chan(struct linuxkpi_ieee80211_channel * c,struct ieee80211com * ic,bool log)4930 lkpi_scan_chan(struct linuxkpi_ieee80211_channel *c,
4931 struct ieee80211com *ic, bool log)
4932 {
4933
4934 if ((c->flags & IEEE80211_CHAN_DISABLED) != 0) {
4935 if (log)
4936 TRACE_SCAN(ic, "Skipping disabled chan "
4937 "on band %s [%#x/%u/%#x]",
4938 lkpi_nl80211_band_name(c->band), c->hw_value,
4939 c->center_freq, c->flags);
4940 return (false);
4941 }
4942 if (isclr(ic->ic_chan_active, ieee80211_mhz2ieee(c->center_freq,
4943 lkpi_nl80211_band_to_net80211_band(c->band)))) {
4944 if (log)
4945 TRACE_SCAN(ic, "Skipping !active chan "
4946 "on band %s [%#x/%u/%#x]",
4947 lkpi_nl80211_band_name(c->band), c->hw_value,
4948 c->center_freq, c->flags);
4949 return (false);
4950 }
4951 return (true);
4952 }
4953 #endif
4954
4955 static void
lkpi_ic_scan_start(struct ieee80211com * ic)4956 lkpi_ic_scan_start(struct ieee80211com *ic)
4957 {
4958 struct lkpi_hw *lhw;
4959 struct ieee80211_hw *hw;
4960 struct lkpi_vif *lvif;
4961 struct ieee80211_vif *vif;
4962 struct ieee80211_scan_state *ss;
4963 struct ieee80211vap *vap;
4964 int error;
4965 bool is_hw_scan;
4966
4967 lhw = ic->ic_softc;
4968 ss = ic->ic_scan;
4969 vap = ss->ss_vap;
4970 TRACE_SCAN(ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4971
4972 LKPI_80211_LHW_SCAN_LOCK(lhw);
4973 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
4974 /* A scan is still running. */
4975 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4976 TRACE_SCAN(ic, "Trying to start new scan while still running; "
4977 "cancelling new net80211 scan; scan_flags %b",
4978 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4979 ieee80211_cancel_scan(vap);
4980 return;
4981 }
4982 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4983 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4984
4985 #if 0
4986 if (vap->iv_state != IEEE80211_S_SCAN) {
4987 TODO("We need to be able to scan if not in S_SCAN");
4988 TRACE_SCAN(ic, "scan_flags %b iv_state %d",
4989 lhw->scan_flags, LKPI_LHW_SCAN_BITS, vap->iv_state);
4990 ieee80211_cancel_scan(vap);
4991 return;
4992 }
4993 #endif
4994
4995 hw = LHW_TO_HW(lhw);
4996 if (!is_hw_scan) {
4997 /* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
4998 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
4999
5000 lvif = VAP_TO_LVIF(vap);
5001 vif = LVIF_TO_VIF(lvif);
5002
5003 if (vap->iv_state == IEEE80211_S_SCAN) {
5004 wiphy_lock(hw->wiphy);
5005 lkpi_hw_conf_idle(hw, false);
5006 wiphy_unlock(hw->wiphy);
5007 }
5008
5009 LKPI_80211_LHW_SCAN_LOCK(lhw);
5010 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
5011 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5012
5013 lkpi_update_mcast_filter(ic);
5014
5015 TRACE_SCAN(vap->iv_ic, "Starting SW_SCAN: scan_flags %b",
5016 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
5017 lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
5018 /* net80211::scan_start() handled PS for us. */
5019 IMPROVE();
5020 /* XXX Also means it is too late to flush queues?
5021 * need to check iv_sta_ps or overload? */
5022 /* XXX want to adjust ss end time/ maxdwell? */
5023
5024 } else {
5025 struct ieee80211_scan_request *hw_req;
5026 struct linuxkpi_ieee80211_channel *lc, **cpp;
5027 struct cfg80211_ssid *ssids;
5028 struct cfg80211_scan_6ghz_params *s6gp;
5029 size_t chan_len, nchan, ssids_len, s6ghzlen;
5030 int band, i, ssid_count, common_ie_len;
5031 #ifndef LKPI_80211_USE_SCANLIST
5032 int n;
5033 #endif
5034 uint32_t band_mask;
5035 uint8_t *ie, *ieend;
5036 bool running;
5037
5038 ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
5039 ssids_len = ssid_count * sizeof(*ssids);
5040 s6ghzlen = 0 * (sizeof(*s6gp)); /* XXX-BZ */
5041
5042 band_mask = 0;
5043 nchan = 0;
5044 if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
5045 #ifdef LKPI_80211_USE_SCANLIST
5046 /* Avoid net80211 scan lists until it has proper scan offload support. */
5047 for (i = ss->ss_next; i < ss->ss_last; i++) {
5048 nchan++;
5049 band = lkpi_net80211_chan_to_nl80211_band(
5050 ss->ss_chans[ss->ss_next + i]);
5051 band_mask |= (1 << band);
5052 }
5053 #else
5054 /* Instead we scan for all channels all the time. */
5055 for (band = 0; band < NUM_NL80211_BANDS; band++) {
5056 switch (band) {
5057 case NL80211_BAND_2GHZ:
5058 case NL80211_BAND_5GHZ:
5059 break;
5060 default:
5061 continue;
5062 }
5063 if (hw->wiphy->bands[band] != NULL) {
5064 struct linuxkpi_ieee80211_channel *channels;
5065 int n;
5066
5067 band_mask |= (1 << band);
5068
5069 channels = hw->wiphy->bands[band]->channels;
5070 n = hw->wiphy->bands[band]->n_channels;
5071 for (i = 0; i < n; i++) {
5072 if (lkpi_scan_chan(&channels[i], ic, true))
5073 nchan++;
5074 }
5075 }
5076 }
5077 #endif
5078 } else {
5079 IMPROVE("individual band scans not yet supported, only scanning first band");
5080 /* In theory net80211 should drive this. */
5081 /* Probably we need to add local logic for now;
5082 * need to deal with scan_complete
5083 * and cancel_scan and keep local state.
5084 * Also cut the nchan down above.
5085 */
5086 /* XXX-BZ ath10k does not set this but still does it? &$%^ */
5087 }
5088
5089 chan_len = nchan * (sizeof(lc) + sizeof(*lc));
5090
5091 common_ie_len = 0;
5092 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
5093 vap->iv_wpa_ie != NULL)
5094 common_ie_len += vap->iv_wpa_ie[1];
5095 if (vap->iv_appie_probereq != NULL)
5096 common_ie_len += vap->iv_appie_probereq->ie_len;
5097
5098 /* We would love to check this at an earlier stage... */
5099 if (common_ie_len > hw->wiphy->max_scan_ie_len) {
5100 ic_printf(ic, "WARNING: %s: common_ie_len %d > "
5101 "wiphy->max_scan_ie_len %d\n", __func__,
5102 common_ie_len, hw->wiphy->max_scan_ie_len);
5103 }
5104
5105 lvif = VAP_TO_LVIF(vap);
5106
5107 hw_req = malloc(sizeof(*hw_req) + ssids_len +
5108 s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
5109 common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
5110
5111 hw_req->req.flags = 0; /* XXX ??? */
5112 hw_req->req.wdev = &lvif->wdev;
5113 hw_req->req.wiphy = hw->wiphy;
5114 hw_req->req.no_cck = false; /* XXX */
5115
5116 /*
5117 * In general setting duration[_mandatory] seems to pessimise
5118 * default scanning behaviour. We only use it for BGSCANnig
5119 * to keep the dwell times small.
5120 * Setting duration_mandatory makes this the maximum dwell
5121 * time (otherwise may be shorter). Duration is in TU.
5122 */
5123 if ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) != 0) {
5124 unsigned long dwell;
5125
5126 if ((ic->ic_caps & IEEE80211_C_BGSCAN) == 0 ||
5127 (vap->iv_flags & IEEE80211_F_BGSCAN) == 0)
5128 ic_printf(ic, "BGSCAN despite off: %b, %b, %b\n",
5129 ic->ic_flags_ext, IEEE80211_FEXT_BITS,
5130 vap->iv_flags, IEEE80211_F_BITS,
5131 ic->ic_caps, IEEE80211_C_BITS);
5132
5133 dwell = ss->ss_mindwell;
5134 if (dwell == 0)
5135 dwell = msecs_to_ticks(20);
5136
5137 hw_req->req.duration_mandatory = true;
5138 hw_req->req.duration = TICKS_2_USEC(dwell) / 1024;
5139 }
5140
5141 #ifdef __notyet__
5142 hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
5143 memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
5144 memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
5145 #endif
5146 eth_broadcast_addr(hw_req->req.bssid);
5147
5148 hw_req->req.n_channels = nchan;
5149 cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
5150 lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
5151 #ifdef LKPI_80211_USE_SCANLIST
5152 for (i = 0; i < nchan; i++) {
5153 *(cpp + i) =
5154 (struct linuxkpi_ieee80211_channel *)(lc + i);
5155 }
5156 /* Avoid net80211 scan lists until it has proper scan offload support. */
5157 for (i = 0; i < nchan; i++) {
5158 struct ieee80211_channel *c;
5159
5160 c = ss->ss_chans[ss->ss_next + i];
5161 lc->center_freq = c->ic_freq; /* XXX */
5162 /* lc->flags */
5163 lc->band = lkpi_net80211_chan_to_nl80211_band(c);
5164 lc->max_power = c->ic_maxpower;
5165 /* lc-> ... */
5166 lc++;
5167 }
5168 #else
5169 /* Add bands in reverse order for scanning. */
5170 n = 0;
5171 for (band = NUM_NL80211_BANDS - 1; band >= 0; band--) {
5172 struct ieee80211_supported_band *supband;
5173 struct linuxkpi_ieee80211_channel *channels;
5174
5175 /* Band disabled for scanning? */
5176 if ((band_mask & (1 << band)) == 0)
5177 continue;
5178
5179 /* Nothing to scan in band? */
5180 supband = hw->wiphy->bands[band];
5181 if (supband == NULL || supband->n_channels == 0)
5182 continue;
5183
5184 channels = supband->channels;
5185 for (i = 0; i < supband->n_channels; i++) {
5186 if (lkpi_scan_chan(&channels[i], ic, false))
5187 *(cpp + n++) = &channels[i];
5188 }
5189 }
5190 if (lkpi_order_scanlist)
5191 lkpi_scan_chan_list_resort(cpp, nchan);
5192
5193 if ((linuxkpi_debug_80211 & D80211_SCAN) != 0) {
5194 printf("%s:%d: %s SCAN Channel List (nchan=%zu): ",
5195 __func__, __LINE__, ic->ic_name, nchan);
5196 for (i = 0; i < nchan; i++) {
5197 struct linuxkpi_ieee80211_channel *xc;
5198
5199 xc = *(cpp + i);
5200 printf(" %d(%d)",
5201 ieee80211_mhz2ieee(xc->center_freq,
5202 lkpi_nl80211_band_to_net80211_band(
5203 xc->band)),
5204 xc->center_freq);
5205 }
5206 printf("\n");
5207 }
5208 #endif
5209
5210 hw_req->req.n_ssids = ssid_count;
5211 if (hw_req->req.n_ssids > 0) {
5212 ssids = (struct cfg80211_ssid *)lc;
5213 hw_req->req.ssids = ssids;
5214 for (i = 0; i < ssid_count; i++) {
5215 ssids->ssid_len = ss->ss_ssid[i].len;
5216 memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
5217 ss->ss_ssid[i].len);
5218 ssids++;
5219 }
5220 s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
5221 } else {
5222 s6gp = (struct cfg80211_scan_6ghz_params *)lc;
5223 }
5224
5225 /* 6GHz one day. */
5226 hw_req->req.n_6ghz_params = 0;
5227 hw_req->req.scan_6ghz_params = NULL;
5228 hw_req->req.scan_6ghz = false; /* Weird boolean; not what you think. */
5229 /* s6gp->... */
5230
5231 ie = ieend = (uint8_t *)s6gp;
5232 /* Copy per-band IEs, copy common IEs */
5233 ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
5234 hw_req->req.ie = ie;
5235 hw_req->req.ie_len = ieend - ie;
5236 hw_req->req.scan_start = jiffies;
5237
5238 vif = LVIF_TO_VIF(lvif);
5239
5240 LKPI_80211_LHW_SCAN_LOCK(lhw);
5241 /* Re-check under lock. */
5242 running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
5243 if (!running) {
5244 KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
5245 "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
5246
5247 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
5248 lhw->hw_req = hw_req;
5249 }
5250 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5251 if (running) {
5252 free(hw_req, M_LKPI80211);
5253 TRACE_SCAN(ic, "Trying to start new scan while still "
5254 "running (2); cancelling new net80211 scan; "
5255 "scan_flags %b",
5256 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
5257 ieee80211_cancel_scan(vap);
5258 return;
5259 }
5260
5261 wiphy_lock(hw->wiphy);
5262 lkpi_update_mcast_filter_locked(ic);
5263 TRACE_SCAN(ic, "Starting HW_SCAN: scan_flags %b, "
5264 "ie_len %d, n_ssids %d, n_chan %d, common_ie_len %d [%d, %d]",
5265 lhw->scan_flags, LKPI_LHW_SCAN_BITS, hw_req->req.ie_len,
5266 hw_req->req.n_ssids, hw_req->req.n_channels,
5267 hw_req->ies.common_ie_len,
5268 hw_req->ies.len[NL80211_BAND_2GHZ],
5269 hw_req->ies.len[NL80211_BAND_5GHZ]);
5270
5271 error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
5272 wiphy_unlock(hw->wiphy);
5273 if (error != 0) {
5274 bool scan_done;
5275 int e;
5276
5277 TRACE_SCAN(ic, "hw_scan failed; scan_flags %b, error %d",
5278 lhw->scan_flags, LKPI_LHW_SCAN_BITS, error);
5279 ieee80211_cancel_scan(vap);
5280
5281 /*
5282 * ieee80211_scan_completed must be called in either
5283 * case of error or none. So let the free happen there
5284 * and only there.
5285 * That would be fine in theory but in practice drivers
5286 * behave differently:
5287 * ath10k does not return hw_scan until after scan_complete
5288 * and can then still return an error.
5289 * rtw88 can return 1 or -EBUSY without scan_complete
5290 * iwlwifi can return various errors before scan starts
5291 * ...
5292 * So we cannot rely on that behaviour and have to check
5293 * and balance between both code paths.
5294 */
5295 e = 0;
5296 scan_done = true;
5297 LKPI_80211_LHW_SCAN_LOCK(lhw);
5298 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
5299
5300 free(lhw->hw_req, M_LKPI80211);
5301 lhw->hw_req = NULL;
5302 /*
5303 * The ieee80211_cancel_scan() above runs in a
5304 * taskq and it may take ages for the previous
5305 * scan to clear; starting a new one right away
5306 * we run into the problem that the old one is
5307 * still active.
5308 */
5309 e = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz);
5310 scan_done = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
5311
5312 /*
5313 * Now we can clear running if no one else did.
5314 */
5315 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
5316 }
5317 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5318 lkpi_update_mcast_filter(ic);
5319 if (!scan_done) {
5320 ic_printf(ic, "ERROR: %s: timeout/error to wait "
5321 "for ieee80211_cancel_scan: %d\n", __func__, e);
5322 return;
5323 }
5324
5325 /*
5326 * XXX-SIGH magic number.
5327 * rtw88 has a magic "return 1" if offloading scan is
5328 * not possible. Fall back to sw scan in that case.
5329 */
5330 if (error == 1) {
5331 /*
5332 * We need to put this into some defered context
5333 * the net80211 scan may not be done yet
5334 * (ic_flags & IEEE80211_F_SCAN) and we cannot
5335 * wait here; if we do scan_curchan_task always
5336 * runs after our timeout to finalize the scan.
5337 */
5338 ieee80211_runtask(ic, &lvif->sw_scan_task);
5339 return;
5340 }
5341
5342 ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
5343 __func__, error);
5344 }
5345 }
5346 }
5347
5348 static void
lkpi_sw_scan_task(void * arg,int pending __unused)5349 lkpi_sw_scan_task(void *arg, int pending __unused)
5350 {
5351 struct lkpi_hw *lhw;
5352 struct lkpi_vif *lvif;
5353 struct ieee80211vap *vap;
5354 struct ieee80211_scan_state *ss;
5355
5356 lvif = arg;
5357 vap = LVIF_TO_VAP(lvif);
5358 lhw = vap->iv_ic->ic_softc;
5359 ss = vap->iv_ic->ic_scan;
5360
5361 LKPI_80211_LHW_SCAN_LOCK(lhw);
5362 /*
5363 * We will re-enable this at scan_end calling lkpi_enable_hw_scan().
5364 * IEEE80211_FEXT_SCAN_OFFLOAD will be cleared by lkpi_ic_scan_start.
5365 */
5366 lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
5367 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5368
5369 TRACE_SCAN(vap->iv_ic, "Triggering SW_SCAN: pending %d, scan_flags %b",
5370 pending, lhw->scan_flags, LKPI_LHW_SCAN_BITS);
5371
5372 /*
5373 * This will call ic_scan_start() and we will get into the right path
5374 * unless other scans started in between.
5375 */
5376 ieee80211_start_scan(vap,
5377 IEEE80211_SCAN_ONCE,
5378 msecs_to_ticks(10000), /* 10000 ms (=~ 50 chan * 200 ms) */
5379 ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
5380 ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
5381 vap->iv_des_nssid, vap->iv_des_ssid);
5382 }
5383
5384 static void
lkpi_ic_scan_end(struct ieee80211com * ic)5385 lkpi_ic_scan_end(struct ieee80211com *ic)
5386 {
5387 struct lkpi_hw *lhw;
5388 bool is_hw_scan;
5389
5390 lhw = ic->ic_softc;
5391 TRACE_SCAN(ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
5392
5393 LKPI_80211_LHW_SCAN_LOCK(lhw);
5394 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
5395 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5396 return;
5397 }
5398 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
5399 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5400
5401 if (!is_hw_scan) {
5402 struct ieee80211_scan_state *ss;
5403 struct ieee80211vap *vap;
5404 struct ieee80211_hw *hw;
5405 struct lkpi_vif *lvif;
5406 struct ieee80211_vif *vif;
5407
5408 ss = ic->ic_scan;
5409 vap = ss->ss_vap;
5410 hw = LHW_TO_HW(lhw);
5411 lvif = VAP_TO_LVIF(vap);
5412 vif = LVIF_TO_VIF(lvif);
5413
5414 lkpi_80211_mo_sw_scan_complete(hw, vif);
5415
5416 /* Send PS to stop buffering if n80211 does not for us? */
5417
5418 if (vap->iv_state == IEEE80211_S_SCAN) {
5419 wiphy_lock(hw->wiphy);
5420 lkpi_hw_conf_idle(hw, true);
5421 wiphy_unlock(hw->wiphy);
5422 }
5423 }
5424
5425 /*
5426 * In case we disabled the hw_scan in lkpi_ic_scan_start() and
5427 * switched to swscan, re-enable hw_scan if available.
5428 */
5429 lkpi_enable_hw_scan(lhw);
5430
5431 /* Clear the scanning chandef. */
5432 memset(&lhw->scan_chandef, 0, sizeof(lhw->scan_chandef));
5433
5434 LKPI_80211_LHW_SCAN_LOCK(lhw);
5435 wakeup(lhw);
5436 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5437 }
5438
5439 static void
lkpi_ic_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)5440 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
5441 unsigned long maxdwell)
5442 {
5443 struct lkpi_hw *lhw;
5444 bool is_hw_scan;
5445
5446 lhw = ss->ss_ic->ic_softc;
5447 TRACE_SCAN(ss->ss_ic, "scan_flags %b chan %d maxdwell %lu",
5448 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
5449 ss->ss_ic->ic_curchan->ic_ieee, maxdwell);
5450
5451 LKPI_80211_LHW_SCAN_LOCK(lhw);
5452 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
5453 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5454 if (!is_hw_scan)
5455 lhw->ic_scan_curchan(ss, maxdwell);
5456 }
5457
5458 static void
lkpi_ic_scan_mindwell(struct ieee80211_scan_state * ss)5459 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
5460 {
5461 struct lkpi_hw *lhw;
5462 bool is_hw_scan;
5463
5464 lhw = ss->ss_ic->ic_softc;
5465 TRACE_SCAN(ss->ss_ic, "scan_flags %b chan %d mindwell %lu",
5466 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
5467 ss->ss_ic->ic_curchan->ic_ieee, ss->ss_mindwell);
5468
5469 LKPI_80211_LHW_SCAN_LOCK(lhw);
5470 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
5471 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5472 if (!is_hw_scan)
5473 lhw->ic_scan_mindwell(ss);
5474 }
5475
5476 struct lkpi_ic_set_channel_iter_arg {
5477 struct linuxkpi_ieee80211_channel *chan;
5478 struct ieee80211_chanctx_conf *chanctx_conf;
5479 };
5480
5481 static void
lkpi_ic_set_channel_chanctx_iterf(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * chanctx_conf,void * arg)5482 lkpi_ic_set_channel_chanctx_iterf(struct ieee80211_hw *hw,
5483 struct ieee80211_chanctx_conf *chanctx_conf, void *arg)
5484 {
5485 struct lkpi_ic_set_channel_iter_arg *chanctx_iter_arg;
5486
5487 chanctx_iter_arg = arg;
5488 if (chanctx_iter_arg->chanctx_conf != NULL)
5489 return;
5490
5491 if (chanctx_iter_arg->chan == chanctx_conf->def.chan)
5492 chanctx_iter_arg->chanctx_conf = chanctx_conf;
5493 }
5494
5495 static void
lkpi_ic_set_channel(struct ieee80211com * ic)5496 lkpi_ic_set_channel(struct ieee80211com *ic)
5497 {
5498 struct lkpi_hw *lhw;
5499 struct ieee80211_hw *hw;
5500 struct ieee80211_channel *c;
5501 struct linuxkpi_ieee80211_channel *chan;
5502 struct ieee80211_chanctx_conf *chanctx_conf;
5503 uint32_t changed;
5504 int error;
5505 bool hw_scan, scan_running;
5506
5507 IEEE80211_UNLOCK_ASSERT(ic);
5508
5509 lhw = ic->ic_softc;
5510
5511 c = ic->ic_curchan;
5512 if (c == NULL || c == IEEE80211_CHAN_ANYC) {
5513 ic_printf(ic, "%s: Unset channel: c %p, ignoring update\n",
5514 __func__, c);
5515 return;
5516 }
5517
5518 chan = lkpi_find_lkpi80211_chan(lhw, c);
5519 if (chan == NULL) {
5520 ic_printf(ic, "%s: No channel found for c %p(%d) chan %p\n",
5521 __func__, c, c->ic_ieee, chan);
5522 return;
5523 }
5524
5525 /*
5526 * All net80211 callers call ieee80211_radiotap_chan_change().
5527 * That means we have nothing to do ourselves.
5528 */
5529
5530 /* If we have a hw_scan running do not switch channels. */
5531 LKPI_80211_LHW_SCAN_LOCK(lhw);
5532 scan_running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
5533 hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
5534 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5535 if (scan_running && hw_scan) {
5536 TRACE_SCAN(ic, "scan_flags %b chan %d nothing to do.",
5537 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
5538 c->ic_ieee);
5539 /* Let us hope we set tx power levels elsewhere. */
5540 return;
5541 }
5542
5543 hw = LHW_TO_HW(lhw);
5544 wiphy_lock(hw->wiphy);
5545 if (scan_running) {
5546 struct ieee80211vap *vap;
5547 struct lkpi_vif *lvif;
5548 struct ieee80211_vif *vif;
5549
5550 /*
5551 * For now and for scanning just pick the first VIF.
5552 * net80211 will need to grow DBDC/link_id support
5553 * for us to find the vif/chanctx otherwise.
5554 */
5555 vap = TAILQ_FIRST(&ic->ic_vaps);
5556 lvif = VAP_TO_LVIF(vap);
5557 vif = LVIF_TO_VIF(lvif);
5558
5559 /* We always set the chandef to no-HT for scanning. */
5560 cfg80211_chandef_create(&lhw->scan_chandef, chan,
5561 NL80211_CHAN_NO_HT);
5562 #ifdef LINUXKPI_DEBUG_80211
5563 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
5564 ic_printf(ic, "%s:%d: initialized lhw->scan_chandef\n",
5565 __func__, __LINE__);
5566 #endif
5567
5568 /*
5569 * This works for as long as we do not do BGSCANs; otherwise
5570 * it'll have to be offchan work.
5571 */
5572 chanctx_conf = lkpi_get_chanctx_conf(hw, vif);
5573 changed = lkpi_init_chanctx_conf(hw, &lhw->scan_chandef, chanctx_conf);
5574 error = lkpi_set_chanctx_conf(hw, vif, chanctx_conf, changed, true);
5575
5576 TRACE_SCAN(ic, "scan_flags %b chan %d ???, error %d",
5577 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
5578 c->ic_ieee, error);
5579
5580 IMPROVE("max power for scanning; TODO in lkpi_80211_update_chandef");
5581
5582 } else if (lhw->emulate_chanctx) {
5583 /*
5584 * We do not set the channel here for normal chanctx operation.
5585 * That's just a setup to fail. scan_to_auth will setup all the
5586 * other neccessary options for this to work.
5587 */
5588 struct lkpi_ic_set_channel_iter_arg chanctx_iter_arg = {
5589 .chan = chan,
5590 .chanctx_conf = NULL,
5591 };
5592 struct cfg80211_chan_def chandef;
5593
5594 lkpi_init_chandef(ic, &chandef, chan, c, false);
5595
5596 ieee80211_iter_chan_contexts_mtx(hw,
5597 lkpi_ic_set_channel_chanctx_iterf, &chanctx_iter_arg);
5598
5599 if (chanctx_iter_arg.chanctx_conf == NULL) {
5600 /* No chanctx found for this channel. */
5601 struct ieee80211vap *vap;
5602 struct lkpi_vif *lvif;
5603 struct ieee80211_vif *vif;
5604
5605 /*
5606 * For now just pick the first VIF.
5607 * net80211 will need to grow DBDC/link_id support
5608 * for us to find the vif/chanctx otherwise.
5609 */
5610 vap = TAILQ_FIRST(&ic->ic_vaps);
5611 lvif = VAP_TO_LVIF(vap);
5612 vif = LVIF_TO_VIF(lvif);
5613
5614 #ifdef LINUXKPI_DEBUG_80211
5615 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
5616 ic_printf(ic, "%s:%d: using on stack chandef\n",
5617 __func__, __LINE__);
5618 #endif
5619 chanctx_conf = lkpi_get_chanctx_conf(hw, vif);
5620 changed = lkpi_init_chanctx_conf(hw, &chandef, chanctx_conf);
5621 IMPROVE("update HT, VHT, bw, ...");
5622 error = lkpi_set_chanctx_conf(hw, vif, chanctx_conf, changed, true);
5623
5624 } else {
5625 /*
5626 * We know we are on the same channel.
5627 * Do we really have to reset everything?
5628 */
5629 IMPROVE("update HT, VHT, bw, ...");
5630
5631 #ifdef LINUXKPI_DEBUG_80211
5632 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
5633 ic_printf(ic, "%s:%d: using on stack chandef\n",
5634 __func__, __LINE__);
5635 #endif
5636
5637 chanctx_conf = chanctx_iter_arg.chanctx_conf;
5638 changed = lkpi_init_chanctx_conf(hw, &chandef, chanctx_conf);
5639 lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
5640 }
5641 }
5642
5643 /* Currently PS is hard coded off! Not sure it belongs here. */
5644 IMPROVE("PS");
5645 if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
5646 (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
5647 hw->conf.flags &= ~IEEE80211_CONF_PS;
5648 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
5649 if (error != 0 && error != EOPNOTSUPP)
5650 ic_printf(ic, "ERROR: %s: config %#0x returned "
5651 "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
5652 error);
5653 }
5654
5655 wiphy_unlock(hw->wiphy);
5656 }
5657
5658 static struct ieee80211_node *
lkpi_ic_node_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN])5659 lkpi_ic_node_alloc(struct ieee80211vap *vap,
5660 const uint8_t mac[IEEE80211_ADDR_LEN])
5661 {
5662 struct ieee80211com *ic;
5663 struct lkpi_hw *lhw;
5664 struct ieee80211_node *ni;
5665 struct ieee80211_hw *hw;
5666 struct lkpi_sta *lsta;
5667
5668 ic = vap->iv_ic;
5669 lhw = ic->ic_softc;
5670
5671 /* We keep allocations de-coupled so we can deal with the two worlds. */
5672 if (lhw->ic_node_alloc == NULL)
5673 return (NULL);
5674
5675 ni = lhw->ic_node_alloc(vap, mac);
5676 if (ni == NULL)
5677 return (NULL);
5678
5679 hw = LHW_TO_HW(lhw);
5680 lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
5681 if (lsta == NULL) {
5682 if (lhw->ic_node_free != NULL)
5683 lhw->ic_node_free(ni);
5684 return (NULL);
5685 }
5686
5687 return (ni);
5688 }
5689
5690 static int
lkpi_ic_node_init(struct ieee80211_node * ni)5691 lkpi_ic_node_init(struct ieee80211_node *ni)
5692 {
5693 struct ieee80211com *ic;
5694 struct lkpi_hw *lhw;
5695 int error;
5696
5697 ic = ni->ni_ic;
5698 lhw = ic->ic_softc;
5699
5700 if (lhw->ic_node_init != NULL) {
5701 error = lhw->ic_node_init(ni);
5702 if (error != 0)
5703 return (error);
5704 }
5705
5706 /* XXX-BZ Sync other state over. */
5707 IMPROVE();
5708
5709 return (0);
5710 }
5711
5712 static void
lkpi_ic_node_cleanup(struct ieee80211_node * ni)5713 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
5714 {
5715 struct ieee80211com *ic;
5716 struct lkpi_hw *lhw;
5717
5718 ic = ni->ni_ic;
5719 lhw = ic->ic_softc;
5720
5721 /* XXX-BZ remove from driver, ... */
5722 IMPROVE();
5723
5724 if (lhw->ic_node_cleanup != NULL)
5725 lhw->ic_node_cleanup(ni);
5726 }
5727
5728 static void
lkpi_ic_node_free(struct ieee80211_node * ni)5729 lkpi_ic_node_free(struct ieee80211_node *ni)
5730 {
5731 struct ieee80211com *ic;
5732 struct lkpi_hw *lhw;
5733 struct lkpi_sta *lsta;
5734
5735 ic = ni->ni_ic;
5736 lhw = ic->ic_softc;
5737 lsta = ni->ni_drv_data;
5738
5739 /* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
5740
5741 /*
5742 * Pass in the original ni just in case of error we could check that
5743 * it is the same as lsta->ni.
5744 */
5745 lkpi_lsta_free(lsta, ni);
5746
5747 if (lhw->ic_node_free != NULL)
5748 lhw->ic_node_free(ni);
5749 }
5750
5751 /*
5752 * lkpi_xmit() called from both the (*ic_raw_xmit) as well as the (*ic_transmit)
5753 * call path.
5754 * Unfortunately they have slightly different invariants. See
5755 * ieee80211_raw_output() and ieee80211_parent_xmitpkt().
5756 * Both take care of the ni reference in case of error, and otherwise during
5757 * the callback after transmit.
5758 * The difference is that in case of error (*ic_raw_xmit) needs us to release
5759 * the mbuf, while (*ic_transmit) will free the mbuf itself.
5760 */
5761 static int
lkpi_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params __unused,bool freem)5762 lkpi_xmit(struct ieee80211_node *ni, struct mbuf *m,
5763 const struct ieee80211_bpf_params *params __unused,
5764 bool freem)
5765 {
5766 struct lkpi_sta *lsta;
5767 int error;
5768
5769 lsta = ni->ni_drv_data;
5770 LKPI_80211_LSTA_TXQ_LOCK(lsta);
5771 #if 0
5772 if (!lsta->added_to_drv || !lsta->txq_ready) {
5773 #else
5774 /*
5775 * Backout this part of 886653492945f which breaks rtw88 or
5776 * in general drivers without (*sta_state)() but only the
5777 * legacy fallback to (*sta_add)().
5778 */
5779 if (!lsta->txq_ready) {
5780 #endif
5781 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5782 if (freem)
5783 m_freem(m);
5784 return (ENETDOWN);
5785 }
5786
5787 /* Queue the packet and enqueue the task to handle it. */
5788 error = mbufq_enqueue(&lsta->txq, m);
5789 if (error != 0) {
5790 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5791 if (freem)
5792 m_freem(m);
5793 #ifdef LINUXKPI_DEBUG_80211
5794 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5795 ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
5796 __func__, error);
5797 #endif
5798 return (ENETDOWN);
5799 }
5800 taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
5801 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5802
5803 #ifdef LINUXKPI_DEBUG_80211
5804 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5805 printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
5806 __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
5807 mbufq_len(&lsta->txq));
5808 #endif
5809
5810 return (0);
5811 }
5812
5813 static int
5814 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
5815 const struct ieee80211_bpf_params *params __unused)
5816 {
5817 return (lkpi_xmit(ni, m, NULL, true));
5818 }
5819
5820 #ifdef LKPI_80211_HW_CRYPTO
5821 /*
5822 * This is a bit of a hack given we know we are operating on a
5823 * single frame and we know that hardware will deal with it.
5824 * But otherwise the enmic bit and the encrypt bit need to be
5825 * decoupled.
5826 */
5827 static int
5828 lkpi_hw_crypto_prepare_tkip(struct ieee80211_key *k,
5829 struct ieee80211_key_conf *kc, struct sk_buff *skb)
5830 {
5831 struct ieee80211_hdr *hdr;
5832 uint32_t hlen, hdrlen;
5833 uint8_t *p;
5834
5835 /*
5836 * TKIP only happens on data.
5837 */
5838 hdr = (void *)skb->data;
5839 if (!ieee80211_is_data_present(hdr->frame_control))
5840 return (0);
5841
5842 /*
5843 * "enmic" (though we do not do that).
5844 */
5845 /* any conditions to not apply this? */
5846 if (skb_tailroom(skb) < ieee80211_crypto_get_key_txmic_len(k))
5847 return (ENOBUFS);
5848
5849 p = skb_put(skb, ieee80211_crypto_get_key_txmic_len(k));
5850 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) != 0)
5851 goto encrypt;
5852
5853 /*
5854 * (*enmic) which we hopefully do not have to do with hw accel.
5855 * That means if we make it here we have a problem.
5856 */
5857 TODO("(*enmic)");
5858 return (ENXIO);
5859
5860 encrypt:
5861 /*
5862 * "encrypt" (though we do not do that).
5863 */
5864 /*
5865 * Check if we have anything to do as requested by driver
5866 * or if we are done?
5867 */
5868 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
5869 (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
5870 return (0);
5871
5872 hlen = k->wk_cipher->ic_header;
5873 if (skb_headroom(skb) < hlen)
5874 return (ENOBUFS);
5875
5876 hdr = (void *)skb->data;
5877 hdrlen = ieee80211_hdrlen(hdr->frame_control);
5878 p = skb_push(skb, hlen);
5879 memmove(p, p + hlen, hdrlen);
5880
5881 /* If driver request space only we are done. */
5882 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
5883 return (0);
5884
5885 p += hdrlen;
5886 k->wk_cipher->ic_setiv(k, p);
5887
5888 /* If we make it hear we do sw encryption. */
5889 TODO("sw encrypt");
5890 return (ENXIO);
5891 }
5892
5893 static int
5894 lkpi_hw_crypto_prepare_ccmp(struct ieee80211_key *k,
5895 struct ieee80211_key_conf *kc, struct sk_buff *skb)
5896 {
5897 struct ieee80211_hdr *hdr;
5898 uint32_t hlen, hdrlen;
5899 uint8_t *p;
5900
5901 hdr = (void *)skb->data;
5902
5903 /*
5904 * Check if we have anythig to do as requested by driver
5905 * or if we are done?
5906 */
5907 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
5908 (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
5909 /* MFP */
5910 !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
5911 ieee80211_is_mgmt(hdr->frame_control)))
5912 return (0);
5913
5914 hlen = k->wk_cipher->ic_header;
5915 if (skb_headroom(skb) < hlen)
5916 return (ENOBUFS);
5917
5918 hdrlen = ieee80211_hdrlen(hdr->frame_control);
5919 p = skb_push(skb, hlen);
5920 memmove(p, p + hlen, hdrlen);
5921
5922 /* If driver request space only we are done. */
5923 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
5924 return (0);
5925
5926 p += hdrlen;
5927 k->wk_cipher->ic_setiv(k, p);
5928
5929 return (0);
5930 }
5931
5932 static int
5933 lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
5934 struct sk_buff *skb)
5935 {
5936 struct ieee80211_tx_info *info;
5937 struct ieee80211_key_conf *kc;
5938
5939 KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
5940 KASSERT(k != NULL, ("%s: key is NULL", __func__));
5941 KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
5942
5943 kc = lsta->kc[k->wk_keyix];
5944
5945 info = IEEE80211_SKB_CB(skb);
5946 info->control.hw_key = kc;
5947
5948 /* MUST NOT happen. KASSERT? */
5949 if (kc == NULL) {
5950 ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
5951 "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
5952 return (ENXIO);
5953 }
5954
5955 switch (kc->cipher) {
5956 case WLAN_CIPHER_SUITE_TKIP:
5957 return (lkpi_hw_crypto_prepare_tkip(k, kc, skb));
5958 case WLAN_CIPHER_SUITE_CCMP:
5959 return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5960 case WLAN_CIPHER_SUITE_GCMP:
5961 return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5962 case WLAN_CIPHER_SUITE_WEP40:
5963 case WLAN_CIPHER_SUITE_WEP104:
5964 case WLAN_CIPHER_SUITE_CCMP_256:
5965 case WLAN_CIPHER_SUITE_GCMP_256:
5966 case WLAN_CIPHER_SUITE_AES_CMAC:
5967 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5968 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5969 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5970 default:
5971 ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p kc %p skb %p, "
5972 "unsupported cipher suite %u (%s)\n", __func__, lsta, k, kc,
5973 skb, kc->cipher, lkpi_cipher_suite_to_name(kc->cipher));
5974 return (EOPNOTSUPP);
5975 }
5976 }
5977
5978 static uint8_t
5979 lkpi_hw_crypto_tailroom(struct lkpi_sta *lsta, struct ieee80211_key *k)
5980 {
5981 struct ieee80211_key_conf *kc;
5982
5983 kc = lsta->kc[k->wk_keyix];
5984 if (kc == NULL)
5985 return (0);
5986
5987 IMPROVE("which other flags need tailroom?");
5988 if (kc->flags & (IEEE80211_KEY_FLAG_PUT_MIC_SPACE))
5989 return (32); /* Large enough to hold everything and pow2. */
5990
5991 return (0);
5992 }
5993 #endif
5994
5995 static void
5996 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
5997 {
5998 struct ieee80211_node *ni;
5999 struct ieee80211_frame *wh;
6000 struct ieee80211_key *k;
6001 struct sk_buff *skb;
6002 struct ieee80211com *ic;
6003 struct lkpi_hw *lhw;
6004 struct ieee80211_hw *hw;
6005 struct lkpi_vif *lvif;
6006 struct ieee80211_vif *vif;
6007 struct ieee80211_channel *c;
6008 struct ieee80211_tx_control control;
6009 struct ieee80211_tx_info *info;
6010 struct ieee80211_sta *sta;
6011 struct ieee80211_hdr *hdr;
6012 struct lkpi_txq *ltxq;
6013 void *buf;
6014 ieee80211_keyix keyix;
6015 uint8_t ac, tid, tailroom;
6016
6017 M_ASSERTPKTHDR(m);
6018 #ifdef LINUXKPI_DEBUG_80211
6019 if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
6020 hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
6021 #endif
6022
6023 ni = lsta->ni;
6024 ieee80211_output_seqno_assign(ni, -1, m);
6025
6026 k = NULL;
6027 keyix = IEEE80211_KEYIX_NONE;
6028 wh = mtod(m, struct ieee80211_frame *);
6029 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
6030
6031 #ifdef LKPI_80211_HW_CRYPTO
6032 if (lkpi_hwcrypto) {
6033 k = ieee80211_crypto_get_txkey(ni, m);
6034 if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
6035 keyix = k->wk_keyix;
6036 }
6037 #endif
6038
6039 /* Encrypt the frame if need be. */
6040 if (keyix == IEEE80211_KEYIX_NONE) {
6041 /* Retrieve key for TX && do software encryption. */
6042 k = ieee80211_crypto_encap(ni, m);
6043 if (k == NULL) {
6044 ieee80211_free_node(ni);
6045 m_freem(m);
6046 return;
6047 }
6048 }
6049 }
6050
6051 ic = ni->ni_ic;
6052 lhw = ic->ic_softc;
6053 hw = LHW_TO_HW(lhw);
6054 c = ni->ni_chan;
6055
6056 if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
6057 struct lkpi_radiotap_tx_hdr *rtap;
6058
6059 rtap = &lhw->rtap_tx;
6060 rtap->wt_flags = 0;
6061 if (k != NULL)
6062 rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
6063 if (m->m_flags & M_FRAG)
6064 rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
6065 IMPROVE();
6066 rtap->wt_rate = 0;
6067 if (c != NULL && c != IEEE80211_CHAN_ANYC) {
6068 rtap->wt_chan_freq = htole16(c->ic_freq);
6069 rtap->wt_chan_flags = htole16(c->ic_flags);
6070 }
6071
6072 ieee80211_radiotap_tx(ni->ni_vap, m);
6073 }
6074
6075 #ifdef LKPI_80211_HW_CRYPTO
6076 if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE)
6077 tailroom = lkpi_hw_crypto_tailroom(lsta, k);
6078 else
6079 #endif
6080 tailroom = 0;
6081
6082 /*
6083 * net80211 should handle hw->extra_tx_headroom.
6084 * Though for as long as we are copying we don't mind.
6085 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
6086 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
6087 */
6088 skb = dev_alloc_skb(hw->extra_tx_headroom + tailroom + m->m_pkthdr.len);
6089 if (skb == NULL) {
6090 static uint8_t skb_alloc_failures = 0;
6091
6092 if (skb_alloc_failures++ == 0) {
6093 int tid;
6094
6095 sta = LSTA_TO_STA(lsta);
6096 ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
6097 __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
6098 for (tid = 0; tid < nitems(sta->txq); tid++) {
6099 if (sta->txq[tid] == NULL)
6100 continue;
6101 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
6102 ic_printf(ic, " tid %d ltxq %p flags %b skb_queue_len %u\n",
6103 tid, ltxq, ltxq->flags, LKPI_TXQ_FLAGS_BITS, skb_queue_len(<xq->skbq));
6104 }
6105 }
6106 ieee80211_free_node(ni);
6107 m_freem(m);
6108 return;
6109 }
6110 skb_reserve(skb, hw->extra_tx_headroom);
6111
6112 /* XXX-BZ we need a SKB version understanding mbuf. */
6113 /* Save the mbuf for ieee80211_tx_complete(). */
6114 skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
6115 skb->m = m;
6116 #if 0
6117 skb_put_data(skb, m->m_data, m->m_pkthdr.len);
6118 #else
6119 buf = skb_put(skb, m->m_pkthdr.len);
6120 m_copydata(m, 0, m->m_pkthdr.len, buf);
6121 #endif
6122 /* Save the ni. */
6123 m->m_pkthdr.PH_loc.ptr = ni;
6124
6125 lvif = VAP_TO_LVIF(ni->ni_vap);
6126 vif = LVIF_TO_VIF(lvif);
6127
6128 hdr = (void *)skb->data;
6129 tid = linuxkpi_ieee80211_get_tid(hdr, true);
6130 if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
6131 if (!ieee80211_is_data(hdr->frame_control)) {
6132 /* MGMT and CTRL frames go on TID 7/VO. */
6133 skb->priority = 7;
6134 ac = IEEE80211_AC_VO;
6135 } else {
6136 /* Other non-QOS traffic goes to BE. */
6137 /* Contrary to net80211 we MUST NOT promote M_EAPOL. */
6138 skb->priority = 0;
6139 ac = IEEE80211_AC_BE;
6140 }
6141 } else {
6142 skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
6143 ac = ieee80211e_up_to_ac[tid & 7];
6144 }
6145 skb_set_queue_mapping(skb, ac);
6146
6147 info = IEEE80211_SKB_CB(skb);
6148 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
6149 /* Slight delay; probably only happens on scanning so fine? */
6150 if (c == NULL || c == IEEE80211_CHAN_ANYC)
6151 c = ic->ic_curchan;
6152 info->band = lkpi_net80211_chan_to_nl80211_band(c);
6153 info->hw_queue = vif->hw_queue[ac];
6154 if ((m->m_flags & M_EAPOL) != 0) {
6155 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
6156 info->flags |= IEEE80211_TX_CTL_USE_MINRATE; /* mt76 */
6157 TRACE_RATES("M_EAPOL -> TX_CTL_USE_MINRATE");
6158 }
6159 info->control.vif = vif;
6160
6161 /* IMPROVE("MLO"); */
6162 info->control.flags |=
6163 u32_encode_bits(IEEE80211_LINK_UNSPECIFIED, IEEE80211_TX_CTRL_MLO_LINK);
6164
6165 if (tid != IEEE80211_NONQOS_TID) {
6166 struct ieee80211_tx_ampdu *tap;
6167
6168 tap = &ni->ni_tx_ampdu[tid];
6169 if (ieee80211_is_data_qos(hdr->frame_control) &&
6170 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
6171 !is_multicast_ether_addr(hdr->addr1) &&
6172 IEEE80211_AMPDU_RUNNING(tap))
6173 info->flags |= IEEE80211_TX_CTL_AMPDU;
6174 }
6175
6176 /* XXX-BZ info->control.rates for non-HW rate control and injected packets. */
6177 #ifdef __notyet__
6178 #ifdef LKPI_80211_HT
6179 info->control.rts_cts_rate_idx=
6180 info->control.use_rts= /* RTS */
6181 info->control.use_cts_prot= /* RTS/CTS*/
6182 #endif
6183 #endif
6184
6185 sta = LSTA_TO_STA(lsta);
6186 #ifdef LKPI_80211_HW_CRYPTO
6187 if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
6188 int error;
6189
6190 error = lkpi_hw_crypto_prepare(lsta, k, skb);
6191 if (error != 0) {
6192 /*
6193 * We only have to free the skb which will free the
6194 * mbuf and release the reference on the ni.
6195 */
6196 dev_kfree_skb(skb);
6197 return;
6198 }
6199 /* Reset header as data might have moved. */
6200 hdr = (void *)skb->data;
6201 }
6202 #endif
6203
6204 IMPROVE();
6205
6206 ltxq = NULL;
6207 if (!ieee80211_is_data_present(hdr->frame_control)) {
6208 if (vif->type == NL80211_IFTYPE_STATION &&
6209 lsta->added_to_drv &&
6210 sta->txq[IEEE80211_NUM_TIDS] != NULL)
6211 ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
6212 } else if (lsta->added_to_drv &&
6213 sta->txq[skb->priority] != NULL) {
6214 ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
6215 }
6216 if (ltxq == NULL)
6217 goto ops_tx;
6218
6219 KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
6220 "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
6221
6222 LKPI_80211_LTXQ_LOCK(ltxq);
6223 skb_queue_tail(<xq->skbq, skb);
6224 ltxq->frms_enqueued++;
6225 #ifdef LINUXKPI_DEBUG_80211
6226 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6227 printf("%s:%d mo_wake_tx_queue :: %d %lu lsta %p sta %p "
6228 "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
6229 "WAKE_TX_Q ac %d prio %u qmap %u\n",
6230 __func__, __LINE__,
6231 curthread->td_tid, jiffies,
6232 lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
6233 skb_queue_len(<xq->skbq), ltxq->txq.ac,
6234 ltxq->txq.tid, ac, skb->priority, skb->qmap);
6235 #endif
6236 LKPI_80211_LTXQ_UNLOCK(ltxq);
6237 wiphy_lock(hw->wiphy);
6238 lkpi_80211_mo_wake_tx_queue(hw, <xq->txq, true);
6239 wiphy_unlock(hw->wiphy);
6240 return;
6241
6242 ops_tx:
6243 #ifdef LINUXKPI_DEBUG_80211
6244 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6245 printf("%s:%d mo_tx :: lsta %p { added_to_drv %d } sta %p "
6246 "ni %p %6D skb %p TX ac %d prio %u qmap %u\n",
6247 __func__, __LINE__, lsta, lsta->added_to_drv, sta,
6248 ni, ni->ni_macaddr, ":", skb, ac, skb->priority, skb->qmap);
6249 #endif
6250 memset(&control, 0, sizeof(control));
6251 if (lsta->added_to_drv)
6252 control.sta = sta;
6253 wiphy_lock(hw->wiphy);
6254 lkpi_80211_mo_tx(hw, &control, skb);
6255 lsta->frms_tx++;
6256 wiphy_unlock(hw->wiphy);
6257 }
6258
6259 static void
6260 lkpi_80211_txq_task(void *ctx, int pending)
6261 {
6262 struct lkpi_sta *lsta;
6263 struct mbufq mq;
6264 struct mbuf *m;
6265 bool shall_tx;
6266
6267 lsta = ctx;
6268
6269 #ifdef LINUXKPI_DEBUG_80211
6270 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6271 printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
6272 __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
6273 pending, mbufq_len(&lsta->txq));
6274 #endif
6275
6276 mbufq_init(&mq, IFQ_MAXLEN);
6277
6278 LKPI_80211_LSTA_TXQ_LOCK(lsta);
6279 /*
6280 * Do not re-check lsta->txq_ready here; we may have a pending
6281 * disassoc/deauth frame still. On the contrary if txq_ready is
6282 * false we do not have a valid sta anymore in the firmware so no
6283 * point to try to TX.
6284 * We also use txq_ready as a semaphore and will drain the txq manually
6285 * if needed on our way towards SCAN/INIT in the state machine.
6286 */
6287 #if 0
6288 shall_tx = lsta->added_to_drv && lsta->txq_ready;
6289 #else
6290 /*
6291 * Backout this part of 886653492945f which breaks rtw88 or
6292 * in general drivers without (*sta_state)() but only the
6293 * legacy fallback to (*sta_add)().
6294 */
6295 shall_tx = lsta->txq_ready;
6296 #endif
6297 if (__predict_true(shall_tx))
6298 mbufq_concat(&mq, &lsta->txq);
6299 /*
6300 * else a state change will push the packets out manually or
6301 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
6302 */
6303 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
6304
6305 m = mbufq_dequeue(&mq);
6306 while (m != NULL) {
6307 lkpi_80211_txq_tx_one(lsta, m);
6308 m = mbufq_dequeue(&mq);
6309 }
6310 }
6311
6312 static int
6313 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
6314 {
6315
6316 /* XXX TODO */
6317 IMPROVE();
6318
6319 /* Quick and dirty cheating hack. */
6320 struct ieee80211_node *ni;
6321
6322 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
6323 return (lkpi_xmit(ni, m, NULL, false));
6324 }
6325
6326 #ifdef LKPI_80211_HT
6327 static int
6328 lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
6329 const uint8_t *frm, const uint8_t *efrm)
6330 {
6331 struct ieee80211com *ic;
6332 struct lkpi_hw *lhw;
6333
6334 ic = ni->ni_ic;
6335 lhw = ic->ic_softc;
6336
6337 TRACEOK("recv_action called");
6338
6339 return (lhw->ic_recv_action(ni, wh, frm, efrm));
6340 }
6341
6342 static int
6343 lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
6344 {
6345 struct ieee80211com *ic;
6346 struct lkpi_hw *lhw;
6347
6348 ic = ni->ni_ic;
6349 lhw = ic->ic_softc;
6350
6351 TRACEOK("send_action with action %d called", action);
6352
6353 return (lhw->ic_send_action(ni, category, action, sa));
6354 }
6355
6356
6357 static int
6358 lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
6359 {
6360 struct ieee80211com *ic;
6361 struct lkpi_hw *lhw;
6362
6363 ic = ni->ni_ic;
6364 lhw = ic->ic_softc;
6365
6366 TRACEOK("ieee80211_ampdu_enable called");
6367
6368 return (lhw->ic_ampdu_enable(ni, tap));
6369 }
6370
6371 /*
6372 * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
6373 * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
6374 *
6375 * NB: returns 0 on ERROR!
6376 */
6377 static int
6378 lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
6379 int dialogtoken, int baparamset, int batimeout)
6380 {
6381 struct ieee80211com *ic;
6382 struct lkpi_hw *lhw;
6383 struct ieee80211_hw *hw;
6384 struct ieee80211vap *vap;
6385 struct lkpi_vif *lvif;
6386 struct ieee80211_vif *vif;
6387 struct lkpi_sta *lsta;
6388 struct ieee80211_sta *sta;
6389 struct ieee80211_ampdu_params params = { };
6390 int error;
6391
6392 ic = ni->ni_ic;
6393 lhw = ic->ic_softc;
6394 hw = LHW_TO_HW(lhw);
6395 vap = ni->ni_vap;
6396 lvif = VAP_TO_LVIF(vap);
6397 vif = LVIF_TO_VIF(lvif);
6398 lsta = ni->ni_drv_data;
6399 sta = LSTA_TO_STA(lsta);
6400
6401 TRACEOK("ADDBA REQ tid %u", tap->txa_tid);
6402
6403 if (!lsta->added_to_drv) {
6404 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
6405 __func__, lsta, ni, sta);
6406 return (0);
6407 }
6408
6409 params.sta = sta;
6410 params.action = IEEE80211_AMPDU_TX_START;
6411 /* Keep 0 here! */
6412 params.buf_size = 0;
6413 params.timeout = 0;
6414 params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
6415 params.tid = tap->txa_tid;
6416 params.amsdu = false;
6417
6418 /* We get called from if_transmit all the way up unlocked in net80211. */
6419 IEEE80211_UNLOCK_ASSERT(ic);
6420 wiphy_lock(hw->wiphy);
6421 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6422 wiphy_unlock(hw->wiphy);
6423 if (error == IEEE80211_AMPDU_TX_START_IMMEDIATE) {
6424 ic_printf(ic, "%s: mo_ampdu_action returned AMPDU_TX_START_IMMEDIATE. "
6425 "ni %p tap %p\n", __func__, ni, tap);
6426 } else if (error != 0) {
6427 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
6428 __func__, error, ni, tap);
6429 return (0);
6430 }
6431
6432 if (sta->txq[tap->txa_tid] != NULL) {
6433 struct lkpi_txq *ltxq;
6434
6435 ltxq = TXQ_TO_LTXQ(sta->txq[tap->txa_tid]);
6436 TRACEOK("ADDBA REQ ltxq tid %u flags %b qlen %d", tap->txa_tid,
6437 ltxq->flags, LKPI_TXQ_FLAGS_BITS, skb_queue_len(<xq->skbq));
6438 ltxq->flags |= LKPI_TXQ_STOPPED_BA;
6439 }
6440
6441 return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
6442 }
6443
6444 /*
6445 * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
6446 * and calls the default ieee80211_addba_response() which always returns 1.
6447 *
6448 * NB: No error checking in net80211!
6449 * Staying with 0 is an error.
6450 */
6451 static int
6452 lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
6453 int status, int baparamset, int batimeout)
6454 {
6455 struct ieee80211com *ic;
6456 struct lkpi_hw *lhw;
6457 struct ieee80211_hw *hw;
6458 struct ieee80211vap *vap;
6459 struct lkpi_vif *lvif;
6460 struct ieee80211_vif *vif;
6461 struct lkpi_sta *lsta;
6462 struct ieee80211_sta *sta;
6463 struct ieee80211_ampdu_params params = { };
6464 int error;
6465
6466 ic = ni->ni_ic;
6467 lhw = ic->ic_softc;
6468 hw = LHW_TO_HW(lhw);
6469 vap = ni->ni_vap;
6470 lvif = VAP_TO_LVIF(vap);
6471 vif = LVIF_TO_VIF(lvif);
6472 lsta = ni->ni_drv_data;
6473 sta = LSTA_TO_STA(lsta);
6474
6475 TRACEOK("ADDBA RESP status %d (0 == SUCCESS) tid %u", status, tap->txa_tid);
6476
6477 if (!lsta->added_to_drv) {
6478 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
6479 __func__, lsta, ni, sta);
6480 return (0);
6481 }
6482
6483 if (status == IEEE80211_STATUS_SUCCESS) {
6484 params.sta = sta;
6485 params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
6486 params.buf_size = tap->txa_wnd;
6487 params.timeout = 0;
6488 params.ssn = 0;
6489 params.tid = tap->txa_tid;
6490 if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
6491 params.amsdu = true;
6492 else
6493 params.amsdu = false;
6494 } else {
6495 /* We need to free the allocated resources. */
6496 params.sta = sta;
6497 switch (status) {
6498 /* params.action = FLUSH, FLUSH_CONT */
6499 default:
6500 params.action = IEEE80211_AMPDU_TX_STOP_CONT;
6501 break;
6502 }
6503 params.buf_size = 0;
6504 params.timeout = 0;
6505 params.ssn = 0;
6506 params.tid = tap->txa_tid;
6507 params.amsdu = false;
6508 }
6509
6510 /* We are called all they way up from ieee80211_input* without lock. */
6511 wiphy_lock(hw->wiphy);
6512 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6513 wiphy_unlock(hw->wiphy);
6514 if (error != 0) {
6515 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
6516 __func__, error, ni, tap);
6517 return (0);
6518 }
6519
6520 if (sta->txq[tap->txa_tid] != NULL) {
6521 struct lkpi_txq *ltxq;
6522
6523 ltxq = TXQ_TO_LTXQ(sta->txq[tap->txa_tid]);
6524 TRACEOK("ADDBA RESP ltxq tid %u flags %b qlen %d", tap->txa_tid,
6525 ltxq->flags, LKPI_TXQ_FLAGS_BITS, skb_queue_len(<xq->skbq));
6526 ltxq->flags &= ~LKPI_TXQ_STOPPED_BA;
6527
6528 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tap->txa_tid], true);
6529 }
6530
6531 IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
6532
6533 return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
6534 }
6535
6536 /*
6537 * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
6538 * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
6539 */
6540 static void
6541 lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
6542 {
6543 struct ieee80211com *ic;
6544 struct lkpi_hw *lhw;
6545 struct ieee80211_hw *hw;
6546 struct ieee80211vap *vap;
6547 struct lkpi_vif *lvif;
6548 struct ieee80211_vif *vif;
6549 struct lkpi_sta *lsta;
6550 struct ieee80211_sta *sta;
6551 struct ieee80211_ampdu_params params = { };
6552 int error;
6553
6554 ic = ni->ni_ic;
6555 lhw = ic->ic_softc;
6556 hw = LHW_TO_HW(lhw);
6557 vap = ni->ni_vap;
6558 lvif = VAP_TO_LVIF(vap);
6559 vif = LVIF_TO_VIF(lvif);
6560 lsta = ni->ni_drv_data;
6561 sta = LSTA_TO_STA(lsta);
6562
6563 if (!lsta->added_to_drv) {
6564 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
6565 __func__, lsta, ni, sta);
6566 goto n80211;
6567 }
6568
6569 /* We need to free the allocated resources. */
6570 params.sta = sta;
6571 IMPROVE("net80211 does not provide a reason to us");
6572 params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
6573 params.buf_size = 0;
6574 params.timeout = 0;
6575 params.ssn = 0;
6576 params.tid = tap->txa_tid;
6577 params.amsdu = false;
6578
6579 IEEE80211_UNLOCK(ic);
6580 wiphy_lock(hw->wiphy);
6581 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6582 wiphy_unlock(hw->wiphy);
6583 IEEE80211_LOCK(ic);
6584 if (error != 0) {
6585 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
6586 __func__, error, ni, tap);
6587 goto n80211;
6588 }
6589
6590 IMPROVE_HT("anyting else?");
6591
6592 n80211:
6593 lhw->ic_addba_stop(ni, tap);
6594 }
6595
6596 static void
6597 lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
6598 {
6599 struct ieee80211com *ic;
6600 struct lkpi_hw *lhw;
6601 struct lkpi_sta *lsta;
6602 struct ieee80211_sta *sta;
6603
6604 ic = ni->ni_ic;
6605 lhw = ic->ic_softc;
6606 lsta = ni->ni_drv_data;
6607 sta = LSTA_TO_STA(lsta);
6608
6609 TRACEOK("ADDBA RESP TIMEO tid %u", tap->txa_tid);
6610
6611 IMPROVE_HT();
6612
6613 if (!lsta->added_to_drv) {
6614 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
6615 __func__, lsta, ni, sta);
6616 goto n80211;
6617 }
6618
6619 /* We need to re-enable the txq and get packets out. */
6620 if (sta->txq[tap->txa_tid] != NULL) {
6621 struct lkpi_txq *ltxq;
6622 struct ieee80211_hw *hw;
6623
6624 ltxq = TXQ_TO_LTXQ(sta->txq[tap->txa_tid]);
6625 TRACEOK("ADDBA RESP TIMEO ltxq tid %u flags %b qlen %d",
6626 tap->txa_tid, ltxq->flags, LKPI_TXQ_FLAGS_BITS,
6627 skb_queue_len(<xq->skbq));
6628 ltxq->flags &= ~LKPI_TXQ_STOPPED_BA;
6629
6630 hw = LHW_TO_HW(lhw);
6631 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tap->txa_tid], true);
6632 }
6633
6634 n80211:
6635 lhw->ic_addba_response_timeout(ni, tap);
6636 }
6637
6638 static void
6639 lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
6640 int status)
6641 {
6642 struct ieee80211com *ic;
6643 struct lkpi_hw *lhw;
6644
6645 ic = ni->ni_ic;
6646 lhw = ic->ic_softc;
6647
6648 IMPROVE_HT();
6649
6650 lhw->ic_bar_response(ni, tap, status);
6651 }
6652
6653 static int
6654 lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
6655 int baparamset, int batimeout, int baseqctl)
6656 {
6657 struct ieee80211com *ic;
6658 struct lkpi_hw *lhw;
6659 struct ieee80211_hw *hw;
6660 struct ieee80211vap *vap;
6661 struct lkpi_vif *lvif;
6662 struct ieee80211_vif *vif;
6663 struct lkpi_sta *lsta;
6664 struct ieee80211_sta *sta;
6665 struct ieee80211_ampdu_params params = { };
6666 int error;
6667
6668 ic = ni->ni_ic;
6669 lhw = ic->ic_softc;
6670 hw = LHW_TO_HW(lhw);
6671 vap = ni->ni_vap;
6672 lvif = VAP_TO_LVIF(vap);
6673 vif = LVIF_TO_VIF(lvif);
6674 lsta = ni->ni_drv_data;
6675 sta = LSTA_TO_STA(lsta);
6676
6677 IEEE80211_UNLOCK_ASSERT(ic);
6678
6679 if (!lsta->added_to_drv) {
6680 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
6681 __func__, lsta, ni, vap, sta);
6682 return (-ENXIO);
6683 }
6684
6685 if (lsta->state != IEEE80211_STA_AUTHORIZED) {
6686 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
6687 __func__, lsta, ni, vap, sta, lsta->state);
6688 return (-ENXIO);
6689 }
6690
6691 params.sta = sta;
6692 params.action = IEEE80211_AMPDU_RX_START;
6693 params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
6694 if (params.buf_size == 0)
6695 params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
6696 else
6697 params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
6698 if (hw->max_rx_aggregation_subframes > 0 &&
6699 params.buf_size > hw->max_rx_aggregation_subframes)
6700 params.buf_size = hw->max_rx_aggregation_subframes;
6701 params.timeout = le16toh(batimeout);
6702 params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
6703 params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
6704
6705 /* Based on net80211::ampdu_rx_start(). */
6706 if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
6707 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
6708 params.amsdu = true;
6709 else
6710 params.amsdu = false;
6711
6712 wiphy_lock(hw->wiphy);
6713 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6714 wiphy_unlock(hw->wiphy);
6715 if (error != 0) {
6716 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
6717 __func__, error, ni, rap);
6718 return (error);
6719 }
6720
6721 if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
6722 IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
6723 }
6724
6725 IMPROVE_HT("net80211 is missing the error check on return and assumes success");
6726
6727 error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
6728 return (error);
6729 }
6730
6731 static void
6732 lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
6733 {
6734 struct ieee80211com *ic;
6735 struct lkpi_hw *lhw;
6736 struct ieee80211_hw *hw;
6737 struct ieee80211vap *vap;
6738 struct lkpi_vif *lvif;
6739 struct ieee80211_vif *vif;
6740 struct lkpi_sta *lsta;
6741 struct ieee80211_sta *sta;
6742 struct ieee80211_ampdu_params params = { };
6743 int error;
6744 uint8_t tid;
6745 bool ic_locked;
6746
6747 ic = ni->ni_ic;
6748 lhw = ic->ic_softc;
6749
6750 /*
6751 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
6752 * we did not START. Some drivers pass it down to firmware which will
6753 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
6754 * ieee80211_ht_node_init() amongst others which will iterate over all
6755 * tid and call ic_ampdu_rx_stop() unconditionally.
6756 * XXX net80211 should probably be more "gentle" in these cases and
6757 * track some state itself.
6758 */
6759 if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
6760 goto net80211_only;
6761
6762 hw = LHW_TO_HW(lhw);
6763 vap = ni->ni_vap;
6764 lvif = VAP_TO_LVIF(vap);
6765 vif = LVIF_TO_VIF(lvif);
6766 lsta = ni->ni_drv_data;
6767 if (lsta == NULL) {
6768 ic_printf(ic, "%s: lsta %p ni %p vap %p, lsta is NULL\n",
6769 __func__, lsta, ni, vap);
6770 goto net80211_only;
6771 }
6772 sta = LSTA_TO_STA(lsta);
6773
6774 if (!lsta->added_to_drv) {
6775 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
6776 __func__, lsta, ni, vap, sta);
6777 goto net80211_only;
6778 }
6779
6780 if (lsta->state != IEEE80211_STA_AUTHORIZED) {
6781 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
6782 __func__, lsta, ni, vap, sta, lsta->state);
6783 goto net80211_only;
6784 }
6785
6786 IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
6787 for (tid = 0; tid < WME_NUM_TID; tid++) {
6788 if (&ni->ni_rx_ampdu[tid] == rap)
6789 break;
6790 }
6791 if (tid == WME_NUM_TID) {
6792 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p TID not found\n",
6793 __func__, lsta, ni, vap, sta);
6794 goto net80211_only;
6795 }
6796
6797 params.sta = sta;
6798 params.action = IEEE80211_AMPDU_RX_STOP;
6799 params.buf_size = 0;
6800 params.timeout = 0;
6801 params.ssn = 0;
6802 params.tid = tid;
6803 params.amsdu = false;
6804
6805 ic_locked = IEEE80211_IS_LOCKED(ic);
6806 if (ic_locked)
6807 IEEE80211_UNLOCK(ic);
6808 wiphy_lock(hw->wiphy);
6809 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6810 wiphy_unlock(hw->wiphy);
6811 if (ic_locked)
6812 IEEE80211_LOCK(ic);
6813 if (error != 0)
6814 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
6815 __func__, error, ni, rap);
6816
6817 net80211_only:
6818 lhw->ic_ampdu_rx_stop(ni, rap);
6819 }
6820 #endif
6821
6822 int
6823 linuxkpi_ieee80211_start_tx_ba_session(struct ieee80211_sta *sta, uint8_t tid,
6824 int timeout)
6825 {
6826 struct lkpi_sta *lsta;
6827 struct ieee80211_hw *hw;
6828 struct lkpi_hw *lhw;
6829 struct ieee80211_tx_ampdu *tap;
6830 int worked;
6831
6832 lsta = STA_TO_LSTA(sta);
6833
6834 /* If tid is out of range, fail gracefully. */
6835 /* XXX-BZ are we limited to 8? */
6836 if (tid >= IEEE80211_NUM_TIDS) {
6837 net80211_vap_printf(lsta->ni->ni_vap, "%s: tid %u out of range "
6838 ">= %u\n", __func__, tid, IEEE80211_NUM_TIDS);
6839 return (-EINVAL);
6840 }
6841
6842 hw = lsta->hw;
6843 lhw = HW_TO_LHW(hw);
6844
6845 /* No ampdu_action support, just error. */
6846 if (lhw->ops->ampdu_action == NULL) {
6847 net80211_vap_printf(lsta->ni->ni_vap, "%s: (*ampdu_action) "
6848 "not supported\n", __func__);
6849 return (-ENOTSUPP);
6850 }
6851
6852 /* Does HW allow us to set this up? */
6853 if (!ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
6854 net80211_vap_printf(lsta->ni->ni_vap, "%s: !AMPDU_AGGREGATION\n",
6855 __func__);
6856 return (-ENOTSUPP);
6857 }
6858 if (ieee80211_hw_check(hw, TX_AMPDU_SETUP_IN_HW)) {
6859 net80211_vap_printf(lsta->ni->ni_vap, "%s: TX_AMPDU_SETUP_IN_HW\n",
6860 __func__);
6861 return (-EPERM);
6862 }
6863
6864 /* We need at least HT or higher support enabled. */
6865 if (!sta->deflink.ht_cap.ht_supported &&
6866 !sta->deflink.vht_cap.vht_supported &&
6867 !sta->deflink.he_cap.has_he &&
6868 !sta->deflink.eht_cap.has_eht) {
6869 net80211_vap_printf(lsta->ni->ni_vap, "%s: HT or later not "
6870 "supported\n", __func__);
6871 return (-EINVAL);
6872 }
6873
6874 #ifdef __notyet__
6875 /*
6876 * We need some rate limiting/disabling in case we try too hard and
6877 * get NACKed over and over.
6878 * XXX-BZ This check should likely go to addba_req along with a counter.
6879 */
6880 if (lsta->block_ba)
6881 return (-EACCESS);
6882 #endif
6883
6884 /* XXX-BZ locking? */
6885
6886 /* Do we have a running session already? */
6887 tap = &lsta->ni->ni_tx_ampdu[tid];
6888 if (IEEE80211_AMPDU_REQUESTED(tap)) {
6889 net80211_vap_printf(lsta->ni->ni_vap, "%s: "
6890 "AMPDU requested/running\n", __func__);
6891 return (-EINPROGRESS);
6892 }
6893
6894 /* Tell net80211 to setup an aggr sessions. */
6895 /* XXX-BZ we have no way to carry the timeout forward easily. */
6896 worked = ieee80211_ampdu_tx_request_ext(lsta->ni, tid);
6897 TRACEOK("ieee80211_ampdu_tx_request_ext %d", worked);
6898
6899 if (worked != 1) {
6900 net80211_vap_printf(lsta->ni->ni_vap, "%s: "
6901 "ieee80211_ampdu_tx_request_ext returned %d != 1\n",
6902 __func__, worked);
6903 return (-EINVAL);
6904 }
6905
6906 /*
6907 * How do we make sure the EAPOL handshake has completed?
6908 * Let ieee80211_output do it.
6909 */
6910 if (1) {
6911 /* Immediately trigger the setup and output of the action frame. */
6912 worked = ieee80211_ampdu_request(lsta->ni, tap);
6913 if (worked != 1) {
6914 net80211_vap_printf(lsta->ni->ni_vap, "%s: "
6915 "ieee80211_ampdu_request returned %d != 1\n",
6916 __func__, worked);
6917 return (-EAGAIN);
6918 }
6919 }
6920
6921 return (0);
6922 }
6923
6924 static void
6925 lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
6926 uint8_t *bands, int *chan_flags, enum nl80211_band band)
6927 {
6928 #ifdef LKPI_80211_HT
6929 struct ieee80211_sta_ht_cap *ht_cap;
6930
6931 ht_cap = &hw->wiphy->bands[band]->ht_cap;
6932 if (!ht_cap->ht_supported)
6933 return;
6934
6935 switch (band) {
6936 case NL80211_BAND_2GHZ:
6937 setbit(bands, IEEE80211_MODE_11NG);
6938 break;
6939 case NL80211_BAND_5GHZ:
6940 setbit(bands, IEEE80211_MODE_11NA);
6941 break;
6942 default:
6943 IMPROVE("Unsupported band %d", band);
6944 return;
6945 }
6946
6947 ic->ic_htcaps = IEEE80211_HTC_HT; /* HT operation */
6948
6949 /*
6950 * Rather than manually checking each flag and
6951 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
6952 * simply copy the 16bits.
6953 */
6954 ic->ic_htcaps |= ht_cap->cap;
6955
6956 /* Then deal with the other flags. */
6957 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
6958 ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
6959 #ifdef __notyet__
6960 if (ieee80211_hw_check(hw, TX_AMSDU))
6961 ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
6962 if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
6963 ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
6964 IEEE80211_HTC_TX_AMSDU_AMPDU);
6965 #endif
6966
6967 IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
6968
6969 /* Only add HT40 channels if supported. */
6970 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
6971 chan_flags != NULL)
6972 *chan_flags |= NET80211_CBW_FLAG_HT40;
6973 #endif
6974 }
6975
6976 static void
6977 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
6978 int *n, struct ieee80211_channel *c)
6979 {
6980 struct lkpi_hw *lhw;
6981 struct ieee80211_hw *hw;
6982 struct linuxkpi_ieee80211_channel *channels;
6983 uint8_t bands[IEEE80211_MODE_BYTES];
6984 int chan_flags, error, i, nchans;
6985
6986 /* Channels */
6987 lhw = ic->ic_softc;
6988 hw = LHW_TO_HW(lhw);
6989
6990 /* NL80211_BAND_2GHZ */
6991 nchans = 0;
6992 if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
6993 nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
6994 if (nchans > 0) {
6995 struct ieee80211_supported_band *supband;
6996
6997 memset(bands, 0, sizeof(bands));
6998 chan_flags = 0;
6999 setbit(bands, IEEE80211_MODE_11B);
7000
7001 /* Check for 11g (simplified). */
7002 supband = hw->wiphy->bands[NL80211_BAND_2GHZ];
7003 for (i = 0; i < supband->n_bitrates; i++) {
7004 if ((supband->bitrates[i].flags &
7005 IEEE80211_RATE_MANDATORY_G) != 0) {
7006 setbit(bands, IEEE80211_MODE_11G);
7007 break;
7008 }
7009 }
7010
7011 IMPROVE("the bitrates may have flags?");
7012
7013 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
7014 NL80211_BAND_2GHZ);
7015
7016 channels = supband->channels;
7017 for (i = 0; i < nchans && *n < maxchan; i++) {
7018 uint32_t nflags = 0;
7019 int cflags = chan_flags;
7020
7021 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
7022 ic_printf(ic, "%s: Skipping disabled chan "
7023 "[%u/%u/%#x]\n", __func__,
7024 channels[i].hw_value,
7025 channels[i].center_freq, channels[i].flags);
7026 continue;
7027 }
7028 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
7029 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
7030 if (channels[i].flags & IEEE80211_CHAN_RADAR)
7031 nflags |= IEEE80211_CHAN_DFS;
7032 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
7033 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
7034 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
7035 cflags &= ~NET80211_CBW_FLAG_VHT80;
7036 /* XXX how to map the remaining enum ieee80211_channel_flags? */
7037 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
7038 cflags &= ~NET80211_CBW_FLAG_HT40;
7039
7040 error = ieee80211_add_channel_cbw(c, maxchan, n,
7041 ieee80211_mhz2ieee(channels[i].center_freq,
7042 lkpi_nl80211_band_to_net80211_band(channels[i].band)),
7043 channels[i].center_freq, channels[i].max_power,
7044 nflags, bands, cflags);
7045 /* net80211::ENOBUFS: *n >= maxchans */
7046 if (error != 0 && error != ENOBUFS)
7047 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
7048 "returned error %d\n",
7049 __func__, channels[i].hw_value,
7050 channels[i].center_freq, channels[i].flags,
7051 nflags, chan_flags, cflags, error);
7052 if (error != 0)
7053 break;
7054 }
7055 }
7056
7057 /* NL80211_BAND_5GHZ */
7058 nchans = 0;
7059 if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
7060 nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
7061 if (nchans > 0) {
7062 memset(bands, 0, sizeof(bands));
7063 chan_flags = 0;
7064 setbit(bands, IEEE80211_MODE_11A);
7065
7066 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
7067 NL80211_BAND_5GHZ);
7068
7069 #ifdef LKPI_80211_VHT
7070 if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
7071
7072 ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
7073 ic->ic_vht_cap.vht_cap_info =
7074 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
7075 ic->ic_vht_cap.supp_mcs =
7076 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
7077
7078 setbit(bands, IEEE80211_MODE_VHT_5GHZ);
7079 chan_flags |= NET80211_CBW_FLAG_VHT80;
7080 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
7081 ic->ic_vht_cap.vht_cap_info))
7082 chan_flags |= NET80211_CBW_FLAG_VHT160;
7083 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
7084 ic->ic_vht_cap.vht_cap_info))
7085 chan_flags |= NET80211_CBW_FLAG_VHT80P80;
7086 }
7087 #endif
7088
7089 channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
7090 for (i = 0; i < nchans && *n < maxchan; i++) {
7091 uint32_t nflags = 0;
7092 int cflags = chan_flags;
7093
7094 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
7095 ic_printf(ic, "%s: Skipping disabled chan "
7096 "[%u/%u/%#x]\n", __func__,
7097 channels[i].hw_value,
7098 channels[i].center_freq, channels[i].flags);
7099 continue;
7100 }
7101 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
7102 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
7103 if (channels[i].flags & IEEE80211_CHAN_RADAR)
7104 nflags |= IEEE80211_CHAN_DFS;
7105 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
7106 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
7107 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
7108 cflags &= ~NET80211_CBW_FLAG_VHT80;
7109 /* XXX hwo to map the remaining enum ieee80211_channel_flags? */
7110 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
7111 cflags &= ~NET80211_CBW_FLAG_HT40;
7112
7113 error = ieee80211_add_channel_cbw(c, maxchan, n,
7114 ieee80211_mhz2ieee(channels[i].center_freq,
7115 lkpi_nl80211_band_to_net80211_band(channels[i].band)),
7116 channels[i].center_freq, channels[i].max_power,
7117 nflags, bands, cflags);
7118 /* net80211::ENOBUFS: *n >= maxchans */
7119 if (error != 0 && error != ENOBUFS)
7120 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
7121 "returned error %d\n",
7122 __func__, channels[i].hw_value,
7123 channels[i].center_freq, channels[i].flags,
7124 nflags, chan_flags, cflags, error);
7125 if (error != 0)
7126 break;
7127 }
7128 }
7129 }
7130
7131 static void *
7132 lkpi_ieee80211_ifalloc(void)
7133 {
7134 struct ieee80211com *ic;
7135
7136 ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
7137
7138 /* Setting these happens later when we have device information. */
7139 ic->ic_softc = NULL;
7140 ic->ic_name = "linuxkpi";
7141
7142 return (ic);
7143 }
7144
7145 struct ieee80211_hw *
7146 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
7147 {
7148 struct ieee80211_hw *hw;
7149 struct lkpi_hw *lhw;
7150 struct wiphy *wiphy;
7151 int ac;
7152 bool emuchanctx;
7153
7154 /*
7155 * Do certain checks before starting to allocate resources.
7156 * Store results in temporary variables.
7157 */
7158
7159 /* ac1d519c01ca introduced emulating chanctx changes. */
7160 emuchanctx = false;
7161 if (ops->add_chanctx == ieee80211_emulate_add_chanctx &&
7162 ops->change_chanctx == ieee80211_emulate_change_chanctx &&
7163 ops->remove_chanctx == ieee80211_emulate_remove_chanctx) {
7164 /*
7165 * If we emulate the chanctx ops, we must not have
7166 * assign_vif_chanctx and unassign_vif_chanctx.
7167 */
7168 if (ops->assign_vif_chanctx != NULL ||
7169 ops->unassign_vif_chanctx != NULL) {
7170 /* Fail gracefully. */
7171 printf("%s: emulate_chanctx but "
7172 "assign_vif_chanctx %p != NULL || "
7173 "unassign_vif_chanctx %p != NULL\n", __func__,
7174 ops->assign_vif_chanctx, ops->unassign_vif_chanctx);
7175 return (NULL);
7176 }
7177 emuchanctx = true;
7178 }
7179 if (!emuchanctx && (ops->add_chanctx == ieee80211_emulate_add_chanctx ||
7180 ops->change_chanctx == ieee80211_emulate_change_chanctx ||
7181 ops->remove_chanctx == ieee80211_emulate_remove_chanctx)) {
7182 printf("%s: not emulating chanctx changes but emulating "
7183 "function set: %d/%d/%d\n", __func__,
7184 ops->add_chanctx == ieee80211_emulate_add_chanctx,
7185 ops->change_chanctx == ieee80211_emulate_change_chanctx,
7186 ops->remove_chanctx == ieee80211_emulate_remove_chanctx);
7187 return (NULL);
7188 }
7189 if (!emuchanctx && (ops->add_chanctx == NULL || ops->change_chanctx == NULL ||
7190 ops->remove_chanctx == NULL || ops->assign_vif_chanctx == NULL ||
7191 ops->unassign_vif_chanctx == NULL)) {
7192 printf("%s: not all functions set for chanctx operations "
7193 "(emulating chanctx %d): %p/%p/%p %p/%p\n",
7194 __func__, emuchanctx,
7195 ops->add_chanctx, ops->change_chanctx, ops->remove_chanctx,
7196 ops->assign_vif_chanctx, ops->unassign_vif_chanctx);
7197 return (NULL);
7198 }
7199
7200 /* Get us and the driver data also allocated. */
7201 wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
7202 if (wiphy == NULL)
7203 return (NULL);
7204
7205 lhw = wiphy_priv(wiphy);
7206 lhw->ops = ops;
7207
7208 LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
7209 LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
7210 spin_lock_init(&lhw->txq_lock);
7211 sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
7212 LKPI_80211_LHW_MC_LOCK_INIT(lhw);
7213 TAILQ_INIT(&lhw->lvif_head);
7214 __hw_addr_init(&lhw->mc_list);
7215 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
7216 spin_lock_init(&lhw->txq_scheduled_lock[ac]);
7217 lhw->txq_generation[ac] = 1;
7218 TAILQ_INIT(&lhw->txq_scheduled[ac]);
7219 }
7220
7221 /* Chanctx_conf */
7222 INIT_LIST_HEAD(&lhw->lchanctx_list);
7223 INIT_LIST_HEAD(&lhw->lchanctx_list_reserved);
7224 lhw->emulate_chanctx = emuchanctx;
7225
7226 /* Deferred RX path. */
7227 LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
7228 TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
7229 mbufq_init(&lhw->rxq, 32 * NAPI_POLL_WEIGHT);
7230 lhw->rxq_stopped = false;
7231
7232 /*
7233 * XXX-BZ TODO make sure there is a "_null" function to all ops
7234 * not initialized.
7235 */
7236 hw = LHW_TO_HW(lhw);
7237 hw->wiphy = wiphy;
7238 hw->conf.flags |= IEEE80211_CONF_IDLE;
7239 hw->priv = (void *)(lhw + 1);
7240
7241 /* BSD Specific. */
7242 lhw->ic = lkpi_ieee80211_ifalloc();
7243
7244 if (lhw->emulate_chanctx)
7245 ic_printf(lhw->ic, "Using chanctx emulation.\n");
7246 IMPROVE();
7247
7248 return (hw);
7249 }
7250
7251 void
7252 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
7253 {
7254 struct lkpi_hw *lhw;
7255 struct mbuf *m;
7256 int ac;
7257
7258 lhw = HW_TO_LHW(hw);
7259 free(lhw->ic, M_LKPI80211);
7260 lhw->ic = NULL;
7261
7262 /*
7263 * Drain the deferred RX path.
7264 */
7265 LKPI_80211_LHW_RXQ_LOCK(lhw);
7266 lhw->rxq_stopped = true;
7267 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7268
7269 /* Drain taskq, won't be restarted due to rxq_stopped being set. */
7270 while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
7271 taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
7272
7273 /* Flush mbufq (make sure to release ni refs!). */
7274 m = mbufq_dequeue(&lhw->rxq);
7275 while (m != NULL) {
7276 #ifdef LKPI_80211_USE_MTAG
7277 struct m_tag *mtag;
7278
7279 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
7280 if (mtag != NULL) {
7281 struct lkpi_80211_tag_rxni *rxni;
7282
7283 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
7284 ieee80211_free_node(rxni->ni);
7285 }
7286 #else
7287 if (m->m_pkthdr.PH_loc.ptr != NULL) {
7288 struct ieee80211_node *ni;
7289
7290 ni = m->m_pkthdr.PH_loc.ptr;
7291 ieee80211_free_node(ni);
7292 }
7293 #endif
7294 m_freem(m);
7295 m = mbufq_dequeue(&lhw->rxq);
7296 }
7297 KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
7298 __func__, lhw, mbufq_len(&lhw->rxq)));
7299 LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
7300
7301 wiphy_lock(hw->wiphy);
7302 /* Chanctx_conf. */
7303 if (!list_empty_careful(&lhw->lchanctx_list)) {
7304 struct lkpi_chanctx *lchanctx, *next;
7305 struct ieee80211_chanctx_conf *chanctx_conf;
7306
7307 list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
7308 if (lchanctx->added_to_drv) {
7309 /* In reality we should panic? */
7310 chanctx_conf = &lchanctx->chanctx_conf;
7311 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
7312 }
7313 list_del(&lchanctx->entry);
7314 /* No need to reset the lchanctx here as we will free it below. */
7315 list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list_reserved);
7316 }
7317 }
7318 if (!list_empty_careful(&lhw->lchanctx_list_reserved)) {
7319 struct lkpi_chanctx *lchanctx, *next;
7320
7321 list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list_reserved, entry) {
7322 list_del(&lchanctx->entry);
7323 if (lchanctx->added_to_drv)
7324 panic("%s: lchanctx %p on reserved list still added_to_drv\n",
7325 __func__, lchanctx);
7326 free(lchanctx, M_LKPI80211);
7327 }
7328 }
7329 wiphy_unlock(hw->wiphy);
7330
7331 LKPI_80211_LHW_MC_LOCK(lhw);
7332 lkpi_cleanup_mcast_list_locked(lhw);
7333 LKPI_80211_LHW_MC_UNLOCK(lhw);
7334
7335 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
7336 spin_lock_destroy(&lhw->txq_scheduled_lock[ac]);
7337
7338 /* Cleanup more of lhw here or in wiphy_free()? */
7339 spin_lock_destroy(&lhw->txq_lock);
7340 LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
7341 LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
7342 sx_destroy(&lhw->lvif_sx);
7343 LKPI_80211_LHW_MC_LOCK_DESTROY(lhw)
7344 IMPROVE();
7345 }
7346
7347 void
7348 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw)
7349 {
7350 struct lkpi_hw *lhw;
7351 struct ieee80211com *ic;
7352 struct device *dev;
7353
7354 lhw = HW_TO_LHW(hw);
7355 ic = lhw->ic;
7356
7357 /* Save the backpointer from net80211 to LinuxKPI. */
7358 ic->ic_softc = lhw;
7359
7360 /*
7361 * Set a proper name before ieee80211_ifattach() if dev is set.
7362 * ath1xk also unset the dev so we need to check.
7363 * Also we will (ab)use this opportunity to register the
7364 * power management sub-children if thay exist (for suspend/resume).
7365 */
7366 dev = wiphy_dev(hw->wiphy);
7367 if (dev != NULL) {
7368 ic->ic_name = dev_name(dev);
7369 if (dev->bsddev != NULL) {
7370 bus_identify_children(dev->bsddev);
7371 bus_enumerate_hinted_children(dev->bsddev);
7372 bus_topo_lock();
7373 bus_attach_children(dev->bsddev);
7374 bus_topo_unlock();
7375 }
7376 } else {
7377 TODO("adjust arguments to still have the old dev or go through "
7378 "the hoops of getting the bsddev from hw and detach; "
7379 "or do in XXX; check ath1kx drivers");
7380 }
7381
7382 /* XXX-BZ do we also need to set wiphy name? */
7383 }
7384
7385 struct ieee80211_hw *
7386 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
7387 {
7388 struct lkpi_hw *lhw;
7389
7390 lhw = wiphy_priv(wiphy);
7391 return (LHW_TO_HW(lhw));
7392 }
7393
7394 static void
7395 lkpi_radiotap_attach(struct lkpi_hw *lhw)
7396 {
7397 struct ieee80211com *ic;
7398
7399 ic = lhw->ic;
7400 ieee80211_radiotap_attach(ic,
7401 &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
7402 LKPI_RTAP_TX_FLAGS_PRESENT,
7403 &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
7404 LKPI_RTAP_RX_FLAGS_PRESENT);
7405 }
7406
7407 int
7408 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
7409 {
7410 struct ieee80211com *ic;
7411 struct lkpi_hw *lhw;
7412 int band, i;
7413
7414 lhw = HW_TO_LHW(hw);
7415 ic = lhw->ic;
7416
7417 /* We do it this late as wiphy->dev should be set for the name. */
7418 lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
7419 if (lhw->workq == NULL)
7420 return (-EAGAIN);
7421
7422 /* XXX-BZ figure this out how they count his... */
7423 if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
7424 IEEE80211_ADDR_COPY(ic->ic_macaddr,
7425 hw->wiphy->perm_addr);
7426 } else if (hw->wiphy->n_addresses > 0) {
7427 /* We take the first one. */
7428 IEEE80211_ADDR_COPY(ic->ic_macaddr,
7429 hw->wiphy->addresses[0].addr);
7430 } else {
7431 ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
7432 }
7433
7434 #ifdef __not_yet__
7435 /* See comment in lkpi_80211_txq_tx_one(). */
7436 ic->ic_headroom = hw->extra_tx_headroom;
7437 #endif
7438
7439 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
7440 ic->ic_opmode = IEEE80211_M_STA;
7441
7442 /* Set device capabilities. */
7443 /* XXX-BZ we need to get these from linux80211/drivers and convert. */
7444 ic->ic_caps =
7445 IEEE80211_C_STA |
7446 IEEE80211_C_MONITOR |
7447 IEEE80211_C_WPA | /* WPA/RSN */
7448 #ifdef LKPI_80211_WME
7449 IEEE80211_C_WME |
7450 #endif
7451 #if 0
7452 IEEE80211_C_PMGT |
7453 #endif
7454 IEEE80211_C_SHSLOT | /* short slot time supported */
7455 IEEE80211_C_SHPREAMBLE /* short preamble supported */
7456 ;
7457
7458 #ifdef LKPI_80211_BGSCAN
7459 if (lhw->ops->hw_scan)
7460 ic->ic_caps |= IEEE80211_C_BGSCAN;
7461 #endif
7462
7463 lkpi_enable_hw_scan(lhw);
7464
7465 /* Does the driver/firmware handle rate countrol? */
7466 /* Currently only older iwlwifi mvm devices are in this category. */
7467 if (bootverbose && !ieee80211_hw_check(hw, HAS_RATE_CONTROL))
7468 ic_printf(ic, "NOTE: rate control not supported by LinuxKPI; "
7469 "expect low rates only\n");
7470
7471 /* Does HW support Fragmentation offload? */
7472 if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
7473 ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
7474
7475 /* Does HW support full AMPDU[-TX] offload? */
7476 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
7477 ic->ic_flags_ext |= IEEE80211_FEXT_AMPDU_OFFLOAD;
7478 #ifdef __notyet__
7479 if (ieee80211_hw_check(hw, TX_AMSDU))
7480 if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
7481 #endif
7482
7483 /*
7484 * The wiphy variables report bitmasks of avail antennas.
7485 * (*get_antenna) get the current bitmask sets which can be
7486 * altered by (*set_antenna) for some drivers.
7487 * XXX-BZ will the count alone do us much good long-term in net80211?
7488 */
7489 if (hw->wiphy->available_antennas_rx ||
7490 hw->wiphy->available_antennas_tx) {
7491 uint32_t rxs, txs;
7492
7493 if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
7494 ic->ic_rxstream = bitcount32(rxs);
7495 ic->ic_txstream = bitcount32(txs);
7496 }
7497 }
7498
7499 ic->ic_cryptocaps = 0;
7500 #ifdef LKPI_80211_HW_CRYPTO
7501 if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
7502 uint32_t hwciphers;
7503
7504 hwciphers = 0;
7505 for (i = 0; i < hw->wiphy->n_cipher_suites; i++) {
7506 uint32_t cs;
7507
7508 cs = lkpi_l80211_to_net80211_cyphers(
7509 ic, hw->wiphy->cipher_suites[i]);
7510 if (cs == IEEE80211_CRYPTO_TKIP) {
7511 /*
7512 * We do set this here. We will only find out
7513 * when doing a SET_KEY operation depending on
7514 * what the driver returns.
7515 * net80211::ieee80211_crypto_newkey()
7516 * checks this so we will have to do flags
7517 * surgery later.
7518 */
7519 cs |= IEEE80211_CRYPTO_TKIPMIC;
7520 }
7521 hwciphers |= cs;
7522 }
7523 /*
7524 * (20250415) nothing anywhere in the path checks we actually
7525 * support all these in net80211.
7526 * net80211 supports _256 variants but the ioctl does not.
7527 */
7528 IMPROVE("as net80211 grows more support, enable them");
7529 hwciphers &= (IEEE80211_CRYPTO_WEP |
7530 IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_TKIPMIC |
7531 IEEE80211_CRYPTO_AES_CCM | IEEE80211_CRYPTO_AES_GCM_128);
7532 /*
7533 * We only support CCMP here, so further filter.
7534 * Also permit TKIP if turned on.
7535 */
7536 hwciphers &= (IEEE80211_CRYPTO_AES_CCM |
7537 IEEE80211_CRYPTO_AES_GCM_128 |
7538 (lkpi_hwcrypto_tkip ? (IEEE80211_CRYPTO_TKIP |
7539 IEEE80211_CRYPTO_TKIPMIC) : 0));
7540 ieee80211_set_hardware_ciphers(ic, hwciphers);
7541 }
7542 #endif
7543
7544 lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
7545 ic->ic_channels);
7546
7547 ieee80211_ifattach(ic);
7548
7549 ic->ic_update_mcast = lkpi_ic_update_mcast;
7550 ic->ic_update_promisc = lkpi_ic_update_promisc;
7551 ic->ic_update_chw = lkpi_ic_update_chw;
7552 ic->ic_parent = lkpi_ic_parent;
7553 ic->ic_scan_start = lkpi_ic_scan_start;
7554 ic->ic_scan_end = lkpi_ic_scan_end;
7555 ic->ic_set_channel = lkpi_ic_set_channel;
7556 ic->ic_transmit = lkpi_ic_transmit;
7557 ic->ic_raw_xmit = lkpi_ic_raw_xmit;
7558 ic->ic_vap_create = lkpi_ic_vap_create;
7559 ic->ic_vap_delete = lkpi_ic_vap_delete;
7560 ic->ic_getradiocaps = lkpi_ic_getradiocaps;
7561 ic->ic_wme.wme_update = lkpi_ic_wme_update;
7562
7563 lhw->ic_scan_curchan = ic->ic_scan_curchan;
7564 ic->ic_scan_curchan = lkpi_ic_scan_curchan;
7565 lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
7566 ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
7567
7568 lhw->ic_node_alloc = ic->ic_node_alloc;
7569 ic->ic_node_alloc = lkpi_ic_node_alloc;
7570 lhw->ic_node_init = ic->ic_node_init;
7571 ic->ic_node_init = lkpi_ic_node_init;
7572 lhw->ic_node_cleanup = ic->ic_node_cleanup;
7573 ic->ic_node_cleanup = lkpi_ic_node_cleanup;
7574 lhw->ic_node_free = ic->ic_node_free;
7575 ic->ic_node_free = lkpi_ic_node_free;
7576
7577 #ifdef LKPI_80211_HT
7578 /*
7579 * Only attach if the driver/firmware supports (*ampdu_action)().
7580 * Otherwise it is in the hands of net80211.
7581 */
7582 if (lhw->ops->ampdu_action != NULL) {
7583 lhw->ic_recv_action = ic->ic_recv_action;
7584 ic->ic_recv_action = lkpi_ic_recv_action;
7585 lhw->ic_send_action = ic->ic_send_action;
7586 ic->ic_send_action = lkpi_ic_send_action;
7587
7588 lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
7589 ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
7590
7591 lhw->ic_addba_request = ic->ic_addba_request;
7592 ic->ic_addba_request = lkpi_ic_addba_request;
7593 lhw->ic_addba_response = ic->ic_addba_response;
7594 ic->ic_addba_response = lkpi_ic_addba_response;
7595 lhw->ic_addba_stop = ic->ic_addba_stop;
7596 ic->ic_addba_stop = lkpi_ic_addba_stop;
7597 lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
7598 ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
7599
7600 lhw->ic_bar_response = ic->ic_bar_response;
7601 ic->ic_bar_response = lkpi_ic_bar_response;
7602
7603 lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
7604 ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
7605 lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
7606 ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
7607 }
7608 #endif
7609
7610 lkpi_radiotap_attach(lhw);
7611
7612 /*
7613 * Assign the first possible channel for now; seems Realtek drivers
7614 * expect one.
7615 * Also remember the amount of bands we support and the most rates
7616 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
7617 */
7618 lhw->supbands = lhw->max_rates = 0;
7619 for (band = 0; band < NUM_NL80211_BANDS; band++) {
7620 struct ieee80211_supported_band *supband;
7621 struct linuxkpi_ieee80211_channel *channels;
7622
7623 supband = hw->wiphy->bands[band];
7624 if (supband == NULL || supband->n_channels == 0)
7625 continue;
7626
7627 lhw->supbands++;
7628 lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
7629
7630 /* If we have a channel, we need to keep counting supbands. */
7631 if (hw->conf.chandef.chan != NULL)
7632 continue;
7633
7634 channels = supband->channels;
7635 for (i = 0; i < supband->n_channels; i++) {
7636
7637 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
7638 continue;
7639
7640 cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
7641 #ifdef LKPI_80211_HT
7642 (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
7643 #endif
7644 NL80211_CHAN_NO_HT);
7645 lhw->dflt_chandef = hw->conf.chandef;
7646 #ifdef LINUXKPI_DEBUG_80211
7647 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
7648 ic_printf(ic, "%s:%d: initialized "
7649 "hw->conf.chandef and dflt_chandef to %p\n",
7650 __func__, __LINE__, &lhw->dflt_chandef);
7651 #endif
7652 break;
7653 }
7654 }
7655
7656 IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
7657
7658 /* Make sure we do not support more than net80211 is willing to take. */
7659 if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
7660 ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
7661 lhw->max_rates, IEEE80211_RATE_MAXSIZE);
7662 lhw->max_rates = IEEE80211_RATE_MAXSIZE;
7663 }
7664
7665 /*
7666 * The maximum supported bitrates on any band + size for
7667 * DSSS Parameter Set give our per-band IE size.
7668 * SSID is the responsibility of the driver and goes on the side.
7669 * The user specified bits coming from the vap go into the
7670 * "common ies" fields.
7671 */
7672 lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
7673 if (lhw->max_rates > IEEE80211_RATE_SIZE)
7674 lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
7675
7676 if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
7677 /*
7678 * net80211 does not seem to support the DSSS Parameter Set but
7679 * some of the drivers insert it so calculate the extra fixed
7680 * space in.
7681 */
7682 lhw->scan_ie_len += 2 + 1;
7683 }
7684
7685 #if defined(LKPI_80211_HT)
7686 if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
7687 lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
7688 #endif
7689 #if defined(LKPI_80211_VHT)
7690 if (IEEE80211_CONF_VHT(ic))
7691 lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
7692 #endif
7693
7694 /* Reduce the max_scan_ie_len "left" by the amount we consume already. */
7695 if (hw->wiphy->max_scan_ie_len > 0) {
7696 if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
7697 goto err;
7698 hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
7699 }
7700
7701 if (bootverbose) {
7702 if (hw->netdev_features != 0)
7703 ic_printf(ic, "netdev_features %b\n",
7704 hw->netdev_features, NETIF_F_BITS);
7705 ieee80211_announce(ic);
7706 }
7707
7708 return (0);
7709 err:
7710 IMPROVE("TODO FIXME CLEANUP");
7711 return (-EAGAIN);
7712 }
7713
7714 void
7715 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
7716 {
7717 struct lkpi_hw *lhw;
7718 struct ieee80211com *ic;
7719
7720 lhw = HW_TO_LHW(hw);
7721 ic = lhw->ic;
7722 ieee80211_ifdetach(ic);
7723 }
7724
7725 void
7726 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
7727 enum ieee80211_iface_iter flags,
7728 void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
7729 void *arg)
7730 {
7731 struct lkpi_hw *lhw;
7732 struct lkpi_vif *lvif;
7733 struct ieee80211_vif *vif;
7734 bool active, atomic, nin_drv;
7735
7736 lhw = HW_TO_LHW(hw);
7737
7738 if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
7739 IEEE80211_IFACE_ITER_RESUME_ALL|
7740 IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
7741 IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC|
7742 IEEE80211_IFACE_ITER__MTX)) {
7743 ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
7744 __func__, flags);
7745 }
7746
7747 if ((flags & IEEE80211_IFACE_ITER__MTX) != 0)
7748 lockdep_assert_wiphy(hw->wiphy);
7749
7750 active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
7751 atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
7752 nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
7753
7754 if (atomic) {
7755 IMPROVE("LKPI_80211_LHW_LVIF_LOCK atomic assume to be rcu?");
7756 LKPI_80211_LHW_LVIF_LOCK(lhw);
7757 }
7758 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
7759 struct ieee80211vap *vap;
7760
7761 vif = LVIF_TO_VIF(lvif);
7762
7763 /*
7764 * If we want "active" interfaces, we need to distinguish on
7765 * whether the driver knows about them or not to be able to
7766 * handle the "resume" case correctly. Skip the ones the
7767 * driver does not know about.
7768 */
7769 if (active && !lvif->added_to_drv &&
7770 (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
7771 continue;
7772
7773 /*
7774 * If we shall skip interfaces not added to the driver do so
7775 * if we haven't yet.
7776 */
7777 if (nin_drv && !lvif->added_to_drv)
7778 continue;
7779
7780 /*
7781 * Run the iterator function if we are either not asking
7782 * asking for active only or if the VAP is "running".
7783 */
7784 /* XXX-BZ probably should have state in the lvif as well. */
7785 vap = LVIF_TO_VAP(lvif);
7786 if (!active || (vap->iv_state != IEEE80211_S_INIT))
7787 iterfunc(arg, vif->addr, vif);
7788 }
7789 if (atomic)
7790 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
7791 }
7792
7793 static void
7794 lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7795 ieee80211_keyix keyix, struct lkpi_sta *lsta,
7796 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
7797 struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
7798 void *arg)
7799 {
7800 #ifdef LINUXKPI_DEBUG_80211
7801 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
7802 net80211_vap_printf(LVIF_TO_VAP(VIF_TO_LVIF(vif)),
7803 "%s:%d: lsta %6D added_to_drv %d kc[keyix %u] %p\n",
7804 __func__, __LINE__, LSTA_TO_STA(lsta)->addr, ":",
7805 lsta->added_to_drv, keyix, lsta->kc[keyix]);
7806 #endif
7807
7808 if (!lsta->added_to_drv)
7809 return;
7810
7811 if (lsta->kc[keyix] == NULL)
7812 return;
7813
7814 iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
7815 }
7816
7817 void
7818 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
7819 struct ieee80211_vif *vif,
7820 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
7821 struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
7822 void *arg, bool rcu)
7823 {
7824 struct lkpi_sta *lsta;
7825 struct lkpi_vif *lvif;
7826
7827 lvif = VIF_TO_LVIF(vif);
7828
7829 if (rcu) {
7830 rcu_read_lock_held(); /* XXX-BZ is this correct? */
7831
7832 if (vif == NULL) {
7833 TODO();
7834 } else {
7835 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7836 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
7837 keyix++)
7838 lkpi_ieee80211_iterate_keys(hw, vif,
7839 keyix, lsta, iterfunc, arg);
7840 }
7841 }
7842 } else {
7843 TODO("Used by suspend/resume; order of keys as installed to "
7844 "firmware is important; we'll need to rewrite some code for that");
7845 lockdep_assert_wiphy(hw->wiphy);
7846
7847 if (vif == NULL) {
7848 TODO();
7849 } else {
7850 list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
7851 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
7852 keyix++)
7853 lkpi_ieee80211_iterate_keys(hw, vif,
7854 keyix, lsta, iterfunc, arg);
7855 }
7856 }
7857 }
7858 }
7859
7860 void
7861 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
7862 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
7863 void *),
7864 void *arg)
7865 {
7866 struct lkpi_hw *lhw;
7867 struct lkpi_chanctx *lchanctx;
7868
7869 KASSERT(hw != NULL && iterfunc != NULL,
7870 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
7871
7872 lhw = HW_TO_LHW(hw);
7873
7874 rcu_read_lock();
7875 list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
7876 if (!lchanctx->added_to_drv)
7877 continue;
7878 iterfunc(hw, &lchanctx->chanctx_conf, arg);
7879 }
7880 rcu_read_unlock();
7881 }
7882
7883 void
7884 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
7885 void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
7886 {
7887 struct lkpi_hw *lhw;
7888 struct lkpi_vif *lvif;
7889 struct lkpi_sta *lsta;
7890 struct ieee80211_sta *sta;
7891
7892 KASSERT(hw != NULL && iterfunc != NULL,
7893 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
7894
7895 lhw = HW_TO_LHW(hw);
7896
7897 LKPI_80211_LHW_LVIF_LOCK(lhw);
7898 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
7899
7900 rcu_read_lock();
7901 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7902 if (!lsta->added_to_drv)
7903 continue;
7904 sta = LSTA_TO_STA(lsta);
7905 iterfunc(arg, sta);
7906 }
7907 rcu_read_unlock();
7908 }
7909 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
7910 }
7911
7912 struct linuxkpi_ieee80211_regdomain *
7913 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
7914 {
7915 struct linuxkpi_ieee80211_regdomain *regd;
7916
7917 regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
7918 GFP_KERNEL);
7919 return (regd);
7920 }
7921
7922 int
7923 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
7924 struct linuxkpi_ieee80211_regdomain *regd)
7925 {
7926 struct lkpi_hw *lhw;
7927 struct ieee80211com *ic;
7928 struct ieee80211_regdomain *rd;
7929
7930 lhw = wiphy_priv(wiphy);
7931 ic = lhw->ic;
7932
7933 rd = &ic->ic_regdomain;
7934 if (rd->isocc[0] == '\0') {
7935 rd->isocc[0] = regd->alpha2[0];
7936 rd->isocc[1] = regd->alpha2[1];
7937 }
7938
7939 TODO();
7940 /* XXX-BZ finish the rest. */
7941
7942 return (0);
7943 }
7944
7945 void
7946 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
7947 struct cfg80211_scan_info *info)
7948 {
7949 struct lkpi_hw *lhw;
7950 struct ieee80211com *ic;
7951 struct ieee80211_scan_state *ss;
7952
7953 lhw = wiphy_priv(hw->wiphy);
7954 ic = lhw->ic;
7955 ss = ic->ic_scan;
7956
7957 TRACE_SCAN(ic, "scan_flags %b info { %ju, %6D, aborted %d }",
7958 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
7959 (uintmax_t)info->scan_start_tsf, info->tsf_bssid, ":",
7960 info->aborted);
7961
7962 ieee80211_scan_done(ss->ss_vap);
7963
7964 LKPI_80211_LHW_SCAN_LOCK(lhw);
7965 free(lhw->hw_req, M_LKPI80211);
7966 lhw->hw_req = NULL;
7967 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
7968 /* The wakeup(lhw) will be called from lkpi_ic_scan_end(). */
7969 /* wakeup(lhw); */
7970 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
7971
7972 return;
7973 }
7974
7975 static void
7976 lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
7977 {
7978 struct ieee80211_node *ni;
7979 #ifdef LKPI_80211_USE_MTAG
7980 struct m_tag *mtag;
7981 #endif
7982 int ok;
7983
7984 ni = NULL;
7985 #ifdef LKPI_80211_USE_MTAG
7986 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
7987 if (mtag != NULL) {
7988 struct lkpi_80211_tag_rxni *rxni;
7989
7990 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
7991 ni = rxni->ni;
7992 }
7993 #else
7994 if (m->m_pkthdr.PH_loc.ptr != NULL) {
7995 ni = m->m_pkthdr.PH_loc.ptr;
7996 m->m_pkthdr.PH_loc.ptr = NULL;
7997 }
7998 #endif
7999
8000 if (ni != NULL) {
8001 ok = ieee80211_input_mimo(ni, m);
8002 ieee80211_free_node(ni); /* Release the reference. */
8003 if (ok < 0)
8004 m_freem(m);
8005 } else {
8006 ok = ieee80211_input_mimo_all(lhw->ic, m);
8007 /* mbuf got consumed. */
8008 }
8009
8010 #ifdef LINUXKPI_DEBUG_80211
8011 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8012 printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
8013 #endif
8014 }
8015
8016 static void
8017 lkpi_80211_lhw_rxq_task(void *ctx, int pending)
8018 {
8019 struct lkpi_hw *lhw;
8020 struct mbufq mq;
8021 struct mbuf *m;
8022
8023 lhw = ctx;
8024
8025 #ifdef LINUXKPI_DEBUG_80211
8026 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8027 printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
8028 __func__, lhw, pending, mbufq_len(&lhw->rxq));
8029 #endif
8030
8031 mbufq_init(&mq, IFQ_MAXLEN);
8032
8033 LKPI_80211_LHW_RXQ_LOCK(lhw);
8034 mbufq_concat(&mq, &lhw->rxq);
8035 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
8036
8037 m = mbufq_dequeue(&mq);
8038 while (m != NULL) {
8039 lkpi_80211_lhw_rxq_rx_one(lhw, m);
8040 m = mbufq_dequeue(&mq);
8041 }
8042 }
8043
8044 static void
8045 lkpi_convert_rx_status(struct ieee80211_hw *hw, struct lkpi_sta *lsta,
8046 struct ieee80211_rx_status *rx_status,
8047 struct ieee80211_rx_stats *rx_stats,
8048 uint8_t *rssip)
8049 {
8050 struct ieee80211_supported_band *supband;
8051 struct rate_info rxrate;
8052 int i;
8053 uint8_t rssi;
8054
8055 memset(&rxrate, 0, sizeof(rxrate));
8056 memset(rx_stats, 0, sizeof(*rx_stats));
8057 rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
8058 /* XXX-BZ correct hardcoded noise floor, survey data? */
8059 rx_stats->c_nf = -96;
8060 if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
8061 !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
8062 rssi = rx_status->signal;
8063 else
8064 rssi = rx_stats->c_nf;
8065 /*
8066 * net80211 signal strength data are in .5 dBm units relative to
8067 * the current noise floor (see comment in ieee80211_node.h).
8068 */
8069 rssi -= rx_stats->c_nf;
8070 if (rssip != NULL)
8071 *rssip = rssi;
8072 rx_stats->c_rssi = rssi * 2;
8073 rx_stats->r_flags |= IEEE80211_R_BAND;
8074 rx_stats->c_band =
8075 lkpi_nl80211_band_to_net80211_band(rx_status->band);
8076 rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
8077 rx_stats->c_freq = rx_status->freq;
8078 rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
8079
8080 rx_stats->c_rx_tsf = rx_status->mactime;
8081
8082 /* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
8083 if ((rx_status->flag & RX_FLAG_MACTIME) ==
8084 (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
8085 rx_stats->r_flags |= IEEE80211_R_TSF64;
8086 /* XXX RX_FLAG_MACTIME_PLCP_START ? */
8087 if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
8088 rx_stats->r_flags |= IEEE80211_R_TSF_START;
8089 if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
8090 rx_stats->r_flags |= IEEE80211_R_TSF_END;
8091 /* XXX-BZ if TSF_END will net80211 do the unwind of time? */
8092 }
8093
8094 if (rx_status->chains != 0) {
8095 int cc;
8096 int8_t crssi;
8097
8098 rx_stats->c_chain = rx_status->chains;
8099 rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
8100
8101 cc = 0;
8102 for (i = 0; i < nitems(rx_status->chain_signal); i++) {
8103 if (!(rx_status->chains & BIT(i)))
8104 continue;
8105 crssi = rx_status->chain_signal[i];
8106 crssi -= rx_stats->c_nf;
8107 rx_stats->c_rssi_ctl[i] = crssi * 2;
8108 rx_stats->c_rssi_ext[i] = crssi * 2; /* XXX _ext ??? ATH thing? */
8109 /* We currently only have the global noise floor value. */
8110 rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
8111 rx_stats->c_nf_ext[i] = rx_stats->c_nf;
8112 cc++;
8113 }
8114 if (cc > 0)
8115 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
8116 }
8117
8118 /* rx_status->antenna */
8119
8120 /* XXX-NET80211 We are not going to populate c_phytype! */
8121
8122 switch (rx_status->encoding) {
8123 case RX_ENC_LEGACY:
8124 {
8125 uint32_t legacy = 0;
8126
8127 supband = hw->wiphy->bands[rx_status->band];
8128 if (supband != NULL)
8129 legacy = supband->bitrates[rx_status->rate_idx].bitrate;
8130 rx_stats->c_rate = legacy;
8131 rxrate.legacy = legacy;
8132 /* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
8133 break;
8134 }
8135 case RX_ENC_HT:
8136 rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
8137 rx_stats->c_rate = rx_status->rate_idx; /* mcs */
8138 rxrate.flags |= RATE_INFO_FLAGS_MCS;
8139 rxrate.mcs = rx_status->rate_idx;
8140 if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
8141 rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
8142 rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
8143 }
8144 break;
8145 case RX_ENC_VHT:
8146 rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
8147 rx_stats->c_rate = rx_status->rate_idx; /* mcs */
8148 rx_stats->c_vhtnss = rx_status->nss;
8149 rxrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
8150 rxrate.mcs = rx_status->rate_idx;
8151 rxrate.nss = rx_status->nss;
8152 if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
8153 rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
8154 rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
8155 }
8156 break;
8157 case RX_ENC_HE:
8158 rxrate.flags |= RATE_INFO_FLAGS_HE_MCS;
8159 rxrate.mcs = rx_status->rate_idx;
8160 rxrate.nss = rx_status->nss;
8161 /* XXX TODO */
8162 TODO("net80211 has not matching encoding for %u", rx_status->encoding);
8163 break;
8164 case RX_ENC_EHT:
8165 rxrate.flags |= RATE_INFO_FLAGS_EHT_MCS;
8166 rxrate.mcs = rx_status->rate_idx;
8167 rxrate.nss = rx_status->nss;
8168 /* XXX TODO */
8169 TODO("net80211 has not matching encoding for %u", rx_status->encoding);
8170 break;
8171 }
8172
8173 rxrate.bw = rx_status->bw;
8174 switch (rx_status->bw) {
8175 case RATE_INFO_BW_20:
8176 rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
8177 break;
8178 case RATE_INFO_BW_40:
8179 rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
8180 break;
8181 case RATE_INFO_BW_80:
8182 rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
8183 break;
8184 case RATE_INFO_BW_160:
8185 rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
8186 break;
8187 case RATE_INFO_BW_320:
8188 case RATE_INFO_BW_HE_RU:
8189 case RATE_INFO_BW_EHT_RU:
8190 case RATE_INFO_BW_5:
8191 case RATE_INFO_BW_10:
8192 TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
8193 break;
8194 }
8195
8196 if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
8197 rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
8198 if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
8199 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
8200
8201 /*
8202 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
8203 * case the hardware does something we do not expect always leave
8204 * these enabled. Leaving this commant as documentation for the || 1.
8205 */
8206 #if defined(LKPI_80211_HW_CRYPTO) || 1
8207 if (rx_status->flag & RX_FLAG_DECRYPTED) {
8208 rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
8209 /* Only valid if decrypted is set. */
8210 if (rx_status->flag & RX_FLAG_PN_VALIDATED)
8211 rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
8212 }
8213 if (rx_status->flag & RX_FLAG_IV_STRIPPED)
8214 rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
8215 if (rx_status->flag & RX_FLAG_ICV_STRIPPED)
8216 rx_stats->c_pktflags |= IEEE80211_RX_F_ICV_STRIP;
8217 if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
8218 rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
8219 if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
8220 rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
8221 if (rx_status->flag & RX_FLAG_MMIC_ERROR)
8222 rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
8223 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
8224 rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
8225 #endif
8226
8227 /* Fill in some sinfo bits to fill gaps not reported byt the driver. */
8228 if (lsta != NULL) {
8229 memcpy(&lsta->sinfo.rxrate, &rxrate, sizeof(rxrate));
8230 lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
8231
8232 if (rx_status->chains != 0) {
8233 lsta->sinfo.chains = rx_status->chains;
8234 memcpy(lsta->sinfo.chain_signal, rx_status->chain_signal,
8235 sizeof(lsta->sinfo.chain_signal));
8236 lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
8237 }
8238 }
8239 }
8240
8241 #ifdef LINUXKPI_DEBUG_80211
8242 static void
8243 lkpi_rx_log_beacon(struct mbuf *m, struct lkpi_hw *lhw,
8244 struct ieee80211_rx_status *rx_status)
8245 {
8246 struct ieee80211_mgmt *f;
8247 uint8_t *e;
8248 char ssid[IEEE80211_NWID_LEN * 4 + 1];
8249
8250 memset(ssid, '\0', sizeof(ssid));
8251
8252 f = mtod(m, struct ieee80211_mgmt *);
8253 e = f->u.beacon.variable;
8254 /*
8255 * Usually SSID is right after the fixed part and for debugging we will
8256 * be fine should we miss it if it is not.
8257 */
8258 while ((e - (uint8_t *)f) < m->m_len) {
8259 if (*e == IEEE80211_ELEMID_SSID)
8260 break;
8261 e += (2 + *(e + 1));
8262 }
8263 if (*e == IEEE80211_ELEMID_SSID) {
8264 int i, len;
8265 char *p;
8266
8267 p = ssid;
8268 len = m->m_len - ((e + 2) - (uint8_t *)f);
8269 if (len > *(e + 1))
8270 len = *(e + 1);
8271 e += 2;
8272 for (i = 0; i < len; i++) {
8273 /* Printable character? */
8274 if (*e >= 0x20 && *e < 0x7f) {
8275 *p++ = *e++;
8276 } else {
8277 snprintf(p, 5, "%#04x", *e++);
8278 p += 4;
8279 }
8280 }
8281 *p = '\0';
8282 }
8283
8284 /* We print skb, skb->data, m as we are seeing 'ghost beacons'. */
8285 TRACE_SCAN_BEACON(lhw->ic, "Beacon: scan_flags %b, band %s freq %u chan %-4d "
8286 "len %d { %#06x %#06x %6D %6D %6D %#06x %ju %u %#06x SSID '%s' }",
8287 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
8288 lkpi_nl80211_band_name(rx_status->band), rx_status->freq,
8289 linuxkpi_ieee80211_frequency_to_channel(rx_status->freq, 0),
8290 m->m_pkthdr.len, f->frame_control, f->duration_id,
8291 f->da, ":", f->sa, ":", f->bssid, ":", f->seq_ctrl,
8292 (uintmax_t)le64_to_cpu(f->u.beacon.timestamp),
8293 le16_to_cpu(f->u.beacon.beacon_int),
8294 le16_to_cpu(f->u.beacon.capab_info), ssid);
8295 }
8296 #endif
8297
8298 /* For %list see comment towards the end of the function. */
8299 void
8300 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
8301 struct ieee80211_sta *sta, struct napi_struct *napi __unused,
8302 struct list_head *list __unused)
8303 {
8304 struct lkpi_hw *lhw;
8305 struct ieee80211com *ic;
8306 struct mbuf *m;
8307 struct skb_shared_info *shinfo;
8308 struct ieee80211_rx_status *rx_status;
8309 struct ieee80211_rx_stats rx_stats;
8310 struct ieee80211_node *ni;
8311 struct ieee80211vap *vap;
8312 struct ieee80211_hdr *hdr;
8313 struct lkpi_sta *lsta;
8314 int i, offset, ok, error;
8315 uint8_t rssi;
8316 bool is_beacon;
8317
8318 lhw = HW_TO_LHW(hw);
8319 ic = lhw->ic;
8320
8321 if (skb->len < 2) {
8322 /* Need 80211 stats here. */
8323 counter_u64_add(ic->ic_ierrors, 1);
8324 IMPROVE();
8325 goto err;
8326 }
8327
8328 /*
8329 * For now do the data copy; we can later improve things. Might even
8330 * have an mbuf backing the skb data then?
8331 */
8332 m = m_get3(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
8333 if (m == NULL) {
8334 counter_u64_add(ic->ic_ierrors, 1);
8335 goto err;
8336 }
8337 m_copyback(m, 0, skb->tail - skb->data, skb->data);
8338
8339 shinfo = skb_shinfo(skb);
8340 offset = m->m_len;
8341 for (i = 0; i < shinfo->nr_frags; i++) {
8342 m_copyback(m, offset, shinfo->frags[i].size,
8343 (uint8_t *)linux_page_address(shinfo->frags[i].page) +
8344 shinfo->frags[i].offset);
8345 offset += shinfo->frags[i].size;
8346 }
8347
8348 rx_status = IEEE80211_SKB_RXCB(skb);
8349
8350 hdr = (void *)skb->data;
8351 is_beacon = ieee80211_is_beacon(hdr->frame_control);
8352
8353 #ifdef LINUXKPI_DEBUG_80211
8354 /*
8355 * We use the mbuf here as otherwise the variable part might
8356 * be in skb frags.
8357 */
8358 if (is_beacon && ((linuxkpi_debug_80211 & D80211_SCAN_BEACON) != 0))
8359 lkpi_rx_log_beacon(m, lhw, rx_status);
8360
8361 if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0 &&
8362 (linuxkpi_debug_80211 & D80211_SCAN_BEACON) == 0)
8363 goto no_trace_beacons;
8364
8365 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8366 printf("TRACE-RX: %s: skb %p l/d/t-len (%u/%u/%u) "
8367 "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
8368 __func__, skb, skb->len, skb->data_len,
8369 skb->truesize, skb->head, skb->data, skb->tail, skb->end,
8370 shinfo, shinfo->nr_frags,
8371 m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
8372
8373 if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
8374 hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
8375
8376 /* Implement a dump_rxcb() !!! */
8377 if ((linuxkpi_debug_80211 & D80211_TRACE_RX) != 0 ||
8378 (linuxkpi_debug_80211 & D80211_SCAN_BEACON) != 0)
8379 printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
8380 "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
8381 __func__,
8382 (uintmax_t)rx_status->boottime_ns,
8383 (uintmax_t)rx_status->mactime,
8384 rx_status->device_timestamp,
8385 rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
8386 rx_status->freq,
8387 rx_status->bw,
8388 rx_status->encoding,
8389 rx_status->ampdu_reference,
8390 rx_status->band,
8391 rx_status->chains,
8392 rx_status->chain_signal[0],
8393 rx_status->chain_signal[1],
8394 rx_status->chain_signal[2],
8395 rx_status->chain_signal[3],
8396 rx_status->signal,
8397 rx_status->enc_flags,
8398 rx_status->he_dcm,
8399 rx_status->he_gi,
8400 rx_status->he_ru,
8401 rx_status->zero_length_psdu_type,
8402 rx_status->nss,
8403 rx_status->rate_idx);
8404 no_trace_beacons:
8405 #endif
8406
8407 lsta = NULL;
8408 if (sta != NULL) {
8409 lsta = STA_TO_LSTA(sta);
8410 ni = ieee80211_ref_node(lsta->ni);
8411 } else {
8412 struct ieee80211_frame_min *wh;
8413
8414 wh = mtod(m, struct ieee80211_frame_min *);
8415 ni = ieee80211_find_rxnode(ic, wh);
8416 if (ni != NULL)
8417 lsta = ni->ni_drv_data;
8418 }
8419
8420 rssi = 0;
8421 lkpi_convert_rx_status(hw, lsta, rx_status, &rx_stats, &rssi);
8422
8423 ok = ieee80211_add_rx_params(m, &rx_stats);
8424 if (ok == 0) {
8425 m_freem(m);
8426 counter_u64_add(ic->ic_ierrors, 1);
8427 goto err;
8428 }
8429
8430 if (ni != NULL)
8431 vap = ni->ni_vap;
8432 else
8433 /*
8434 * XXX-BZ can we improve this by looking at the frame hdr
8435 * or other meta-data passed up?
8436 */
8437 vap = TAILQ_FIRST(&ic->ic_vaps);
8438
8439 #ifdef LINUXKPI_DEBUG_80211
8440 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8441 printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
8442 __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
8443 ni, vap, is_beacon ? " beacon" : "");
8444 #endif
8445
8446 if (ni != NULL && vap != NULL && is_beacon &&
8447 rx_status->device_timestamp > 0 &&
8448 m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
8449 struct lkpi_vif *lvif;
8450 struct ieee80211_vif *vif;
8451 struct ieee80211_frame *wh;
8452
8453 lvif = VAP_TO_LVIF(vap);
8454 vif = LVIF_TO_VIF(lvif);
8455
8456 wh = mtod(m, struct ieee80211_frame *);
8457 if (!IEEE80211_ADDR_EQ(wh->i_addr2, vif->cfg.ap_addr))
8458 goto skip_device_ts;
8459
8460 IMPROVE("TIMING_BEACON_ONLY?");
8461 /* mac80211 specific (not net80211) so keep it here. */
8462 vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
8463 /*
8464 * net80211 should take care of the other information (sync_tsf,
8465 * sync_dtim_count) as otherwise we need to parse the beacon.
8466 */
8467 skip_device_ts:
8468 ;
8469 }
8470
8471 if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
8472 ieee80211_radiotap_active_vap(vap)) {
8473 struct lkpi_radiotap_rx_hdr *rtap;
8474
8475 rtap = &lhw->rtap_rx;
8476 rtap->wr_tsft = rx_status->device_timestamp;
8477 rtap->wr_flags = 0;
8478 if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
8479 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
8480 if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
8481 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
8482 #if 0 /* .. or it does not given we strip it below. */
8483 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
8484 rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
8485 #endif
8486 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
8487 rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
8488 rtap->wr_rate = 0;
8489 IMPROVE();
8490 /* XXX TODO status->encoding / rate_index / bw */
8491 rtap->wr_chan_freq = htole16(rx_stats.c_freq);
8492 if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
8493 rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
8494 rtap->wr_dbm_antsignal = rssi;
8495 rtap->wr_dbm_antnoise = rx_stats.c_nf;
8496 }
8497
8498 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
8499 m_adj(m, -IEEE80211_CRC_LEN);
8500
8501 #if 0
8502 if (list != NULL) {
8503 /*
8504 * Normally this would be queued up and delivered by
8505 * netif_receive_skb_list(), napi_gro_receive(), or the like.
8506 * See mt76::mac80211.c as only current possible consumer.
8507 */
8508 IMPROVE("we simply pass the packet to net80211 to deal with.");
8509 }
8510 #endif
8511
8512 /* Attach meta-information to the mbuf for the deferred RX path. */
8513 if (ni != NULL) {
8514 #ifdef LKPI_80211_USE_MTAG
8515 struct m_tag *mtag;
8516 struct lkpi_80211_tag_rxni *rxni;
8517
8518 mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
8519 sizeof(*rxni), IEEE80211_M_NOWAIT);
8520 if (mtag == NULL) {
8521 m_freem(m);
8522 counter_u64_add(ic->ic_ierrors, 1);
8523 goto err;
8524 }
8525 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
8526 rxni->ni = ni; /* We hold a reference. */
8527 m_tag_prepend(m, mtag);
8528 #else
8529 m->m_pkthdr.PH_loc.ptr = ni; /* We hold a reference. */
8530 #endif
8531 }
8532
8533 LKPI_80211_LHW_RXQ_LOCK(lhw);
8534 if (lhw->rxq_stopped) {
8535 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
8536 m_freem(m);
8537 counter_u64_add(ic->ic_ierrors, 1);
8538 goto err;
8539 }
8540
8541 error = mbufq_enqueue(&lhw->rxq, m);
8542 if (error != 0) {
8543 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
8544 m_freem(m);
8545 counter_u64_add(ic->ic_ierrors, 1);
8546 #ifdef LINUXKPI_DEBUG_80211
8547 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8548 ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
8549 __func__, error);
8550 #endif
8551 goto err;
8552 }
8553 taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
8554 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
8555
8556 IMPROVE();
8557
8558 err:
8559 /* The skb is ours so we can free it :-) */
8560 kfree_skb(skb);
8561 }
8562
8563 uint8_t
8564 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
8565 {
8566 const struct ieee80211_frame *wh;
8567 uint8_t tid;
8568
8569 /* Linux seems to assume this is a QOS-Data-Frame */
8570 KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
8571 ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
8572 hdr->frame_control));
8573
8574 wh = (const struct ieee80211_frame *)hdr;
8575 tid = ieee80211_gettid(wh);
8576 KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
8577 "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
8578
8579 return (tid);
8580 }
8581
8582 /* -------------------------------------------------------------------------- */
8583
8584 static void
8585 lkpi_wiphy_work(struct work_struct *work)
8586 {
8587 struct lkpi_wiphy *lwiphy;
8588 struct wiphy *wiphy;
8589 struct wiphy_work *wk;
8590
8591 lwiphy = container_of(work, struct lkpi_wiphy, wwk);
8592 wiphy = LWIPHY_TO_WIPHY(lwiphy);
8593
8594 wiphy_lock(wiphy);
8595
8596 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8597 wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
8598 /* If there is nothing we do nothing. */
8599 if (wk == NULL) {
8600 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8601 wiphy_unlock(wiphy);
8602 return;
8603 }
8604 list_del_init(&wk->entry);
8605
8606 /* More work to do? */
8607 if (!list_empty(&lwiphy->wwk_list))
8608 schedule_work(work);
8609 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8610
8611 /* Finally call the (*wiphy_work_fn)() function. */
8612 wk->fn(wiphy, wk);
8613
8614 wiphy_unlock(wiphy);
8615 }
8616
8617 void
8618 linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
8619 {
8620 struct lkpi_wiphy *lwiphy;
8621
8622 lwiphy = WIPHY_TO_LWIPHY(wiphy);
8623
8624 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8625 /* Do not double-queue. */
8626 if (list_empty(&wwk->entry))
8627 list_add_tail(&wwk->entry, &lwiphy->wwk_list);
8628 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8629
8630 /*
8631 * See how ieee80211_queue_work() work continues in Linux or if things
8632 * migrate here over time?
8633 * Use a system queue from linux/workqueue.h for now.
8634 */
8635 queue_work(system_wq, &lwiphy->wwk);
8636 }
8637
8638 void
8639 linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
8640 {
8641 struct lkpi_wiphy *lwiphy;
8642
8643 lwiphy = WIPHY_TO_LWIPHY(wiphy);
8644
8645 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8646 /* Only cancel if queued. */
8647 if (!list_empty(&wwk->entry))
8648 list_del_init(&wwk->entry);
8649 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8650 }
8651
8652 void
8653 linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
8654 {
8655 struct lkpi_wiphy *lwiphy;
8656 struct wiphy_work *wk;
8657
8658 lwiphy = WIPHY_TO_LWIPHY(wiphy);
8659 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8660 /* If wwk is unset, flush everything; called when wiphy is shut down. */
8661 if (wwk != NULL && list_empty(&wwk->entry)) {
8662 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8663 return;
8664 }
8665
8666 while (!list_empty(&lwiphy->wwk_list)) {
8667
8668 wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
8669 entry);
8670 list_del_init(&wk->entry);
8671 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8672 wk->fn(wiphy, wk);
8673 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8674 if (wk == wwk)
8675 break;
8676 }
8677 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8678 }
8679
8680 void
8681 lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
8682 {
8683 struct wiphy_delayed_work *wdwk;
8684
8685 wdwk = timer_container_of(wdwk, tl, timer);
8686 wiphy_work_queue(wdwk->wiphy, &wdwk->work);
8687 }
8688
8689 void
8690 linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
8691 struct wiphy_delayed_work *wdwk, unsigned long delay)
8692 {
8693 if (delay == 0) {
8694 /* Run right away. */
8695 del_timer(&wdwk->timer);
8696 wiphy_work_queue(wiphy, &wdwk->work);
8697 } else {
8698 wdwk->wiphy = wiphy;
8699 mod_timer(&wdwk->timer, jiffies + delay);
8700 }
8701 }
8702
8703 void
8704 linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
8705 struct wiphy_delayed_work *wdwk)
8706 {
8707 del_timer_sync(&wdwk->timer);
8708 wiphy_work_cancel(wiphy, &wdwk->work);
8709 }
8710
8711 void
8712 linuxkpi_wiphy_delayed_work_flush(struct wiphy *wiphy,
8713 struct wiphy_delayed_work *wdwk)
8714 {
8715 lockdep_assert_held(&wiphy->mtx);
8716
8717 del_timer_sync(&wdwk->timer);
8718 wiphy_work_flush(wiphy, &wdwk->work);
8719 }
8720
8721 /* -------------------------------------------------------------------------- */
8722
8723 struct wiphy *
8724 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
8725 {
8726 struct lkpi_wiphy *lwiphy;
8727 struct wiphy *wiphy;
8728
8729 lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
8730 if (lwiphy == NULL)
8731 return (NULL);
8732 lwiphy->ops = ops;
8733
8734 LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
8735 INIT_LIST_HEAD(&lwiphy->wwk_list);
8736 INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
8737
8738 wiphy = LWIPHY_TO_WIPHY(lwiphy);
8739
8740 INIT_LIST_HEAD(&wiphy->wdev_list);
8741 mutex_init(&wiphy->mtx);
8742 TODO();
8743
8744 return (wiphy);
8745 }
8746
8747 void
8748 linuxkpi_wiphy_free(struct wiphy *wiphy)
8749 {
8750 struct lkpi_wiphy *lwiphy;
8751
8752 if (wiphy == NULL)
8753 return;
8754
8755 linuxkpi_wiphy_work_flush(wiphy, NULL);
8756 mutex_destroy(&wiphy->mtx);
8757
8758 lwiphy = WIPHY_TO_LWIPHY(wiphy);
8759 LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
8760
8761 kfree(lwiphy);
8762 }
8763
8764 static void
8765 lkpi_wiphy_band_annotate(struct wiphy *wiphy)
8766 {
8767 int band;
8768
8769 for (band = 0; band < NUM_NL80211_BANDS; band++) {
8770 struct ieee80211_supported_band *supband;
8771 int i;
8772
8773 supband = wiphy->bands[band];
8774 if (supband == NULL)
8775 continue;
8776
8777 switch (band) {
8778 case NL80211_BAND_2GHZ:
8779 case NL80211_BAND_5GHZ:
8780 break;
8781 default:
8782 #ifdef LINUXKPI_DEBUG_80211
8783 IMPROVE("band %d(%s) not yet supported",
8784 band, lkpi_nl80211_band_name(band));
8785 /* For bands added here, also check lkpi_lsta_alloc(). */
8786 #endif
8787 continue;
8788 }
8789
8790 /* Band bitrates are times 10; e.g., 55 is 5.5Mbit/s. */
8791 for (i = 0; i < supband->n_bitrates; i++) {
8792 switch (band) {
8793 case NL80211_BAND_2GHZ:
8794 switch (supband->bitrates[i].bitrate) {
8795 case 110:
8796 case 55:
8797 case 20:
8798 case 10:
8799 supband->bitrates[i].flags |=
8800 IEEE80211_RATE_MANDATORY_B;
8801 /* FALLTHROUGH */
8802 /* 11g only */
8803 case 240:
8804 case 120:
8805 case 60:
8806 supband->bitrates[i].flags |=
8807 IEEE80211_RATE_MANDATORY_G;
8808 break;
8809 }
8810 break;
8811 case NL80211_BAND_5GHZ:
8812 switch (supband->bitrates[i].bitrate) {
8813 case 240:
8814 case 120:
8815 case 60:
8816 supband->bitrates[i].flags |=
8817 IEEE80211_RATE_MANDATORY_A;
8818 break;
8819 }
8820 break;
8821 }
8822 TRACE_RATES("band %d bitrate[%d/%u] %u flags %#010x",
8823 band, i, supband->n_bitrates,
8824 supband->bitrates[i].bitrate,
8825 supband->bitrates[i].flags);
8826 }
8827 }
8828 }
8829
8830 int
8831 linuxkpi_80211_wiphy_register(struct wiphy *wiphy)
8832 {
8833 TODO("Lots of checks and initialization");
8834
8835 lkpi_wiphy_band_annotate(wiphy);
8836
8837 return (0);
8838 }
8839
8840 static uint32_t
8841 lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
8842 {
8843 TODO("cfg80211_calculate_bitrate_ht");
8844 return (rate->legacy);
8845 }
8846
8847 static uint32_t
8848 lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
8849 {
8850 TODO("cfg80211_calculate_bitrate_vht");
8851 return (rate->legacy);
8852 }
8853
8854 uint32_t
8855 linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
8856 {
8857
8858 /* Beware: order! */
8859 if (rate->flags & RATE_INFO_FLAGS_MCS)
8860 return (lkpi_cfg80211_calculate_bitrate_ht(rate));
8861
8862 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
8863 return (lkpi_cfg80211_calculate_bitrate_vht(rate));
8864
8865 IMPROVE("HE/EHT/...");
8866
8867 return (rate->legacy);
8868 }
8869
8870 uint32_t
8871 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
8872 enum nl80211_band band)
8873 {
8874
8875 switch (band) {
8876 case NL80211_BAND_2GHZ:
8877 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
8878 break;
8879 case NL80211_BAND_5GHZ:
8880 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
8881 break;
8882 default:
8883 /* XXX abort, retry, error, panic? */
8884 break;
8885 }
8886
8887 return (0);
8888 }
8889
8890 uint32_t
8891 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
8892 {
8893
8894 return (ieee80211_mhz2ieee(freq, 0));
8895 }
8896
8897 #if 0
8898 static struct lkpi_sta *
8899 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
8900 {
8901 struct lkpi_sta *lsta, *temp;
8902
8903 rcu_read_lock();
8904 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
8905 if (lsta->ni == ni) {
8906 rcu_read_unlock();
8907 return (lsta);
8908 }
8909 }
8910 rcu_read_unlock();
8911
8912 return (NULL);
8913 }
8914 #endif
8915
8916 struct ieee80211_sta *
8917 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
8918 {
8919 struct lkpi_vif *lvif;
8920 struct lkpi_sta *lsta;
8921 struct ieee80211_sta *sta;
8922
8923 lvif = VIF_TO_LVIF(vif);
8924
8925 rcu_read_lock();
8926 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
8927 sta = LSTA_TO_STA(lsta);
8928 if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
8929 rcu_read_unlock();
8930 return (sta);
8931 }
8932 }
8933 rcu_read_unlock();
8934 return (NULL);
8935 }
8936
8937 struct ieee80211_sta *
8938 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
8939 const uint8_t *addr, const uint8_t *ourvifaddr)
8940 {
8941 struct lkpi_hw *lhw;
8942 struct lkpi_vif *lvif;
8943 struct lkpi_sta *lsta;
8944 struct ieee80211_vif *vif;
8945 struct ieee80211_sta *sta;
8946
8947 lhw = wiphy_priv(hw->wiphy);
8948 sta = NULL;
8949
8950 LKPI_80211_LHW_LVIF_LOCK(lhw);
8951 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
8952
8953 /* XXX-BZ check our address from the vif. */
8954
8955 vif = LVIF_TO_VIF(lvif);
8956 if (ourvifaddr != NULL &&
8957 !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
8958 continue;
8959 sta = linuxkpi_ieee80211_find_sta(vif, addr);
8960 if (sta != NULL)
8961 break;
8962 }
8963 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
8964
8965 if (sta != NULL) {
8966 lsta = STA_TO_LSTA(sta);
8967 if (!lsta->added_to_drv)
8968 return (NULL);
8969 }
8970
8971 return (sta);
8972 }
8973
8974 struct sk_buff *
8975 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
8976 struct ieee80211_txq *txq)
8977 {
8978 struct lkpi_txq *ltxq;
8979 struct lkpi_vif *lvif;
8980 struct sk_buff *skb;
8981
8982 IMPROVE("wiphy_lock? or assert?");
8983 skb = NULL;
8984 ltxq = TXQ_TO_LTXQ(txq);
8985 ltxq->flags |= LKPI_TXQ_SEEN_DEQUEUE;
8986
8987 if ((ltxq->flags & (LKPI_TXQ_STOPPED|LKPI_TXQ_STOPPED_BA)) != 0)
8988 goto stopped;
8989
8990 lvif = VIF_TO_LVIF(ltxq->txq.vif);
8991 if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
8992 ltxq->flags |= LKPI_TXQ_STOPPED;
8993 goto stopped;
8994 }
8995
8996 IMPROVE("hw(TX_FRAG_LIST)");
8997
8998 LKPI_80211_LTXQ_LOCK(ltxq);
8999 skb = skb_dequeue(<xq->skbq);
9000 if (skb != NULL)
9001 ltxq->frms_dequeued++;
9002 LKPI_80211_LTXQ_UNLOCK(ltxq);
9003
9004 stopped:
9005 return (skb);
9006 }
9007
9008 void
9009 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
9010 unsigned long *frame_cnt, unsigned long *byte_cnt)
9011 {
9012 struct lkpi_txq *ltxq;
9013 struct sk_buff *skb;
9014 unsigned long fc, bc;
9015
9016 ltxq = TXQ_TO_LTXQ(txq);
9017
9018 fc = bc = 0;
9019 LKPI_80211_LTXQ_LOCK(ltxq);
9020 skb_queue_walk(<xq->skbq, skb) {
9021 fc++;
9022 bc += skb->len;
9023 }
9024 LKPI_80211_LTXQ_UNLOCK(ltxq);
9025 if (frame_cnt)
9026 *frame_cnt = fc;
9027 if (byte_cnt)
9028 *byte_cnt = bc;
9029
9030 /* Validate that this is doing the correct thing. */
9031 /* Should we keep track on en/dequeue? */
9032 IMPROVE();
9033 }
9034
9035 /*
9036 * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
9037 * The latter tries to derive the success status from the info flags
9038 * passed back from the driver. rawx_mit() saves the ni on the m and the
9039 * m on the skb for us to be able to give feedback to net80211.
9040 */
9041 static void
9042 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
9043 int status)
9044 {
9045 struct ieee80211_node *ni;
9046 struct mbuf *m;
9047
9048 if (skb == NULL)
9049 return;
9050
9051 m = skb->m;
9052 skb->m = NULL;
9053
9054 if (m != NULL) {
9055 ni = m->m_pkthdr.PH_loc.ptr;
9056 /* Status: 0 is ok, != 0 is error. */
9057 ieee80211_tx_complete(ni, m, status);
9058 /* ni & mbuf were consumed. */
9059 }
9060 }
9061
9062 void
9063 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
9064 int status)
9065 {
9066
9067 _lkpi_ieee80211_free_txskb(hw, skb, status);
9068 kfree_skb(skb);
9069 }
9070
9071 void
9072 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
9073 struct ieee80211_tx_status *txstat)
9074 {
9075 struct sk_buff *skb;
9076 struct ieee80211_tx_info *info, _info = { };
9077 struct ieee80211_ratectl_tx_status txs;
9078 struct ieee80211_node *ni;
9079 int status;
9080
9081 skb = txstat->skb;
9082 if (skb != NULL && skb->m != NULL) {
9083 struct mbuf *m;
9084
9085 m = skb->m;
9086 ni = m->m_pkthdr.PH_loc.ptr;
9087 memset(&txs, 0, sizeof(txs));
9088 } else {
9089 ni = NULL;
9090 }
9091
9092 /*
9093 * If we have no info information on tx, set info to an all-zero struct
9094 * to make the code (and debug output) simpler.
9095 */
9096 info = txstat->info;
9097 if (info == NULL)
9098 info = &_info;
9099 if (info->flags & IEEE80211_TX_STAT_ACK) {
9100 status = 0; /* No error. */
9101 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
9102 } else {
9103 status = 1;
9104 txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
9105 }
9106
9107 if (ni != NULL) {
9108 txs.pktlen = skb->len;
9109 txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
9110 if (info->status.rates[0].count > 1) {
9111 txs.long_retries = info->status.rates[0].count - 1; /* 1 + retries in drivers. */
9112 txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
9113 }
9114 #if 0 /* Unused in net80211 currently. */
9115 /* XXX-BZ convert check .flags for MCS/VHT/.. */
9116 txs.final_rate = info->status.rates[0].idx;
9117 txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
9118 #endif
9119 if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
9120 txs.rssi = info->status.ack_signal; /* XXX-BZ CONVERT? */
9121 txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
9122 }
9123
9124 IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
9125 ieee80211_ratectl_tx_complete(ni, &txs);
9126 ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
9127
9128 #ifdef LINUXKPI_DEBUG_80211
9129 if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
9130 printf("TX-RATE: %s: long_retries %d\n", __func__,
9131 txs.long_retries);
9132 }
9133 #endif
9134 }
9135
9136 #ifdef LINUXKPI_DEBUG_80211
9137 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
9138 printf("TX-STATUS: %s: hw %p skb %p status %d : flags %b "
9139 "band %u hw_queue %u tx_time_est %d : "
9140 "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
9141 "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
9142 "tx_time %u flags %b "
9143 "status_driver_data [ %p %p ]\n",
9144 __func__, hw, skb, status, info->flags, IEEE80211_TX_INFO_FLAGS,
9145 info->band, info->hw_queue, info->tx_time_est,
9146 info->status.rates[0].idx, info->status.rates[0].count,
9147 info->status.rates[0].flags,
9148 info->status.rates[1].idx, info->status.rates[1].count,
9149 info->status.rates[1].flags,
9150 info->status.rates[2].idx, info->status.rates[2].count,
9151 info->status.rates[2].flags,
9152 info->status.rates[3].idx, info->status.rates[3].count,
9153 info->status.rates[3].flags,
9154 info->status.ack_signal, info->status.ampdu_ack_len,
9155 info->status.ampdu_len, info->status.antenna,
9156 info->status.tx_time, info->status.flags, IEEE80211_TX_STATUS_FLAGS,
9157 info->status.status_driver_data[0],
9158 info->status.status_driver_data[1]);
9159 #endif
9160
9161 if (txstat->free_list) {
9162 _lkpi_ieee80211_free_txskb(hw, skb, status);
9163 if (skb != NULL)
9164 list_add_tail(&skb->list, txstat->free_list);
9165 } else {
9166 linuxkpi_ieee80211_free_txskb(hw, skb, status);
9167 }
9168 }
9169
9170 void
9171 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
9172 {
9173 struct ieee80211_tx_status status;
9174
9175 memset(&status, 0, sizeof(status));
9176 status.info = IEEE80211_SKB_CB(skb);
9177 status.skb = skb;
9178 /* sta, n_rates, rates, free_list? */
9179
9180 ieee80211_tx_status_ext(hw, &status);
9181 }
9182
9183 /*
9184 * This is an internal bandaid for the moment for the way we glue
9185 * skbs and mbufs together for TX. Once we have skbs backed by
9186 * mbufs this should go away.
9187 * This is a public function but kept on the private KPI (lkpi_)
9188 * and is not exposed by a header file.
9189 */
9190 static void
9191 lkpi_ieee80211_free_skb_mbuf(void *p)
9192 {
9193 struct ieee80211_node *ni;
9194 struct mbuf *m;
9195
9196 if (p == NULL)
9197 return;
9198
9199 m = (struct mbuf *)p;
9200 M_ASSERTPKTHDR(m);
9201
9202 ni = m->m_pkthdr.PH_loc.ptr;
9203 m->m_pkthdr.PH_loc.ptr = NULL;
9204 if (ni != NULL)
9205 ieee80211_free_node(ni);
9206 m_freem(m);
9207 }
9208
9209 void
9210 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
9211 struct delayed_work *w, int delay)
9212 {
9213 struct lkpi_hw *lhw;
9214
9215 /* Need to make sure hw is in a stable (non-suspended) state. */
9216 IMPROVE();
9217
9218 lhw = HW_TO_LHW(hw);
9219 queue_delayed_work(lhw->workq, w, delay);
9220 }
9221
9222 void
9223 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
9224 struct work_struct *w)
9225 {
9226 struct lkpi_hw *lhw;
9227
9228 /* Need to make sure hw is in a stable (non-suspended) state. */
9229 IMPROVE();
9230
9231 lhw = HW_TO_LHW(hw);
9232 queue_work(lhw->workq, w);
9233 }
9234
9235 struct sk_buff *
9236 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, const uint8_t *addr,
9237 const uint8_t *ssid, size_t ssid_len, size_t tailroom)
9238 {
9239 struct sk_buff *skb;
9240 struct ieee80211_frame *wh;
9241 uint8_t *p;
9242 size_t len;
9243
9244 len = sizeof(*wh);
9245 len += 2 + ssid_len;
9246
9247 skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
9248 if (skb == NULL)
9249 return (NULL);
9250
9251 skb_reserve(skb, hw->extra_tx_headroom);
9252
9253 wh = skb_put_zero(skb, sizeof(*wh));
9254 wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
9255 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
9256 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
9257 IEEE80211_ADDR_COPY(wh->i_addr2, addr);
9258 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
9259
9260 p = skb_put(skb, 2 + ssid_len);
9261 *p++ = IEEE80211_ELEMID_SSID;
9262 *p++ = ssid_len;
9263 if (ssid_len > 0)
9264 memcpy(p, ssid, ssid_len);
9265
9266 return (skb);
9267 }
9268
9269 struct sk_buff *
9270 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
9271 struct ieee80211_vif *vif)
9272 {
9273 struct lkpi_vif *lvif;
9274 struct ieee80211vap *vap;
9275 struct sk_buff *skb;
9276 struct ieee80211_frame_pspoll *psp;
9277 uint16_t v;
9278
9279 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
9280 if (skb == NULL)
9281 return (NULL);
9282
9283 skb_reserve(skb, hw->extra_tx_headroom);
9284
9285 lvif = VIF_TO_LVIF(vif);
9286 vap = LVIF_TO_VAP(lvif);
9287
9288 psp = skb_put_zero(skb, sizeof(*psp));
9289 psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
9290 psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
9291 v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
9292 memcpy(&psp->i_aid, &v, sizeof(v));
9293 IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
9294 IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
9295
9296 return (skb);
9297 }
9298
9299 struct sk_buff *
9300 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
9301 struct ieee80211_vif *vif, int linkid, bool qos)
9302 {
9303 struct sk_buff *skb;
9304 struct ieee80211_frame *nullf;
9305
9306 IMPROVE("linkid");
9307
9308 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
9309 if (skb == NULL)
9310 return (NULL);
9311
9312 skb_reserve(skb, hw->extra_tx_headroom);
9313
9314 nullf = skb_put_zero(skb, sizeof(*nullf));
9315 nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
9316 nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
9317 nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
9318
9319 /* XXX-BZ if link is given, this is different. */
9320 IEEE80211_ADDR_COPY(nullf->i_addr1, vif->cfg.ap_addr);
9321 IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
9322 IEEE80211_ADDR_COPY(nullf->i_addr3, vif->cfg.ap_addr);
9323
9324 return (skb);
9325 }
9326
9327 struct wireless_dev *
9328 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
9329 {
9330 struct lkpi_vif *lvif;
9331
9332 lvif = VIF_TO_LVIF(vif);
9333 return (&lvif->wdev);
9334 }
9335
9336 void
9337 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
9338 {
9339 struct lkpi_vif *lvif;
9340 struct ieee80211vap *vap;
9341 enum ieee80211_state nstate;
9342 int arg;
9343
9344 lvif = VIF_TO_LVIF(vif);
9345 vap = LVIF_TO_VAP(lvif);
9346
9347 /*
9348 * Go to init; otherwise we need to elaborately check state and
9349 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
9350 * Let the statemachine handle all neccessary changes.
9351 */
9352 nstate = IEEE80211_S_INIT;
9353 arg = 0; /* Not a valid reason. */
9354
9355 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
9356 "beacons %d dtim_period %d)\n", __func__, vif, vap,
9357 ieee80211_state_name[vap->iv_state],
9358 lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
9359 vif->bss_conf.dtim_period);
9360 ieee80211_new_state(vap, nstate, arg);
9361 }
9362
9363 void
9364 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
9365 {
9366 struct lkpi_vif *lvif;
9367 struct ieee80211vap *vap;
9368
9369 lvif = VIF_TO_LVIF(vif);
9370 vap = LVIF_TO_VAP(lvif);
9371
9372 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
9373 "beacons %d dtim_period %d)\n", __func__, vif, vap,
9374 ieee80211_state_name[vap->iv_state],
9375 lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
9376 vif->bss_conf.dtim_period);
9377 ieee80211_beacon_miss(vap->iv_ic);
9378 }
9379
9380 /* -------------------------------------------------------------------------- */
9381
9382 void
9383 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
9384 {
9385 struct lkpi_hw *lhw;
9386 struct lkpi_vif *lvif;
9387 struct ieee80211_vif *vif;
9388 int ac_count, ac;
9389
9390 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
9391 __func__, qnum, hw->queues, hw));
9392
9393 lhw = wiphy_priv(hw->wiphy);
9394
9395 /* See lkpi_ic_vap_create(). */
9396 if (hw->queues >= IEEE80211_NUM_ACS)
9397 ac_count = IEEE80211_NUM_ACS;
9398 else
9399 ac_count = 1;
9400
9401 LKPI_80211_LHW_LVIF_LOCK(lhw);
9402 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
9403
9404 vif = LVIF_TO_VIF(lvif);
9405 for (ac = 0; ac < ac_count; ac++) {
9406 IMPROVE_TXQ("LOCKING");
9407 if (qnum == vif->hw_queue[ac]) {
9408 #ifdef LINUXKPI_DEBUG_80211
9409 /*
9410 * For now log this to better understand
9411 * how this is supposed to work.
9412 */
9413 if (lvif->hw_queue_stopped[ac] &&
9414 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
9415 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
9416 "lvif %p vif %p ac %d qnum %d already "
9417 "stopped\n", __func__, __LINE__,
9418 lhw, hw, lvif, vif, ac, qnum);
9419 #endif
9420 lvif->hw_queue_stopped[ac] = true;
9421 }
9422 }
9423 }
9424 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
9425 }
9426
9427 void
9428 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
9429 {
9430 int i;
9431
9432 IMPROVE_TXQ("Locking; do we need further info?");
9433 for (i = 0; i < hw->queues; i++)
9434 linuxkpi_ieee80211_stop_queue(hw, i);
9435 }
9436
9437
9438 static void
9439 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
9440 {
9441 struct lkpi_hw *lhw;
9442 struct lkpi_vif *lvif;
9443 struct lkpi_sta *lsta;
9444 int ac_count, ac, tid;
9445
9446 /* See lkpi_ic_vap_create(). */
9447 if (hw->queues >= IEEE80211_NUM_ACS)
9448 ac_count = IEEE80211_NUM_ACS;
9449 else
9450 ac_count = 1;
9451
9452 lhw = wiphy_priv(hw->wiphy);
9453
9454 IMPROVE_TXQ("Locking");
9455 LKPI_80211_LHW_LVIF_LOCK(lhw);
9456 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
9457 struct ieee80211_vif *vif;
9458
9459 vif = LVIF_TO_VIF(lvif);
9460 for (ac = 0; ac < ac_count; ac++) {
9461
9462 if (hwq == vif->hw_queue[ac]) {
9463
9464 /* XXX-BZ what about software scan? */
9465
9466 #ifdef LINUXKPI_DEBUG_80211
9467 /*
9468 * For now log this to better understand
9469 * how this is supposed to work.
9470 */
9471 if (!lvif->hw_queue_stopped[ac] &&
9472 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
9473 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
9474 "lvif %p vif %p ac %d hw_q not stopped\n",
9475 __func__, __LINE__,
9476 lhw, hw, lvif, vif, ac);
9477 #endif
9478 lvif->hw_queue_stopped[ac] = false;
9479
9480 rcu_read_lock();
9481 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
9482 struct ieee80211_sta *sta;
9483
9484 sta = LSTA_TO_STA(lsta);
9485 for (tid = 0; tid < nitems(sta->txq); tid++) {
9486 struct lkpi_txq *ltxq;
9487
9488 if (sta->txq[tid] == NULL)
9489 continue;
9490
9491 if (sta->txq[tid]->ac != ac)
9492 continue;
9493
9494 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
9495 if ((ltxq->flags & LKPI_TXQ_STOPPED) == 0)
9496 continue;
9497
9498 ltxq->flags &= ~LKPI_TXQ_STOPPED;
9499
9500 if (!skb_queue_empty(<xq->skbq))
9501 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid], false);
9502 }
9503 }
9504 rcu_read_unlock();
9505 }
9506 }
9507 }
9508 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
9509 }
9510
9511 static void
9512 lkpi_ieee80211_wake_queues_locked(struct ieee80211_hw *hw)
9513 {
9514 int i;
9515
9516 IMPROVE_TXQ("Is this all/enough here?");
9517 for (i = 0; i < hw->queues; i++)
9518 lkpi_ieee80211_wake_queues(hw, i);
9519 }
9520
9521 void
9522 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
9523 {
9524 struct lkpi_hw *lhw;
9525 unsigned long flags;
9526
9527 lhw = HW_TO_LHW(hw);
9528
9529 spin_lock_irqsave(&lhw->txq_lock, flags);
9530 lkpi_ieee80211_wake_queues_locked(hw);
9531 spin_unlock_irqrestore(&lhw->txq_lock, flags);
9532 }
9533
9534 void
9535 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
9536 {
9537 struct lkpi_hw *lhw;
9538 unsigned long flags;
9539
9540 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
9541 __func__, qnum, hw->queues, hw));
9542
9543 lhw = HW_TO_LHW(hw);
9544
9545 spin_lock_irqsave(&lhw->txq_lock, flags);
9546 lkpi_ieee80211_wake_queues(hw, qnum);
9547 spin_unlock_irqrestore(&lhw->txq_lock, flags);
9548 }
9549
9550 void
9551 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
9552 struct ieee80211_txq *txq)
9553 {
9554 struct lkpi_hw *lhw;
9555
9556 lhw = HW_TO_LHW(hw);
9557
9558 LKPI_80211_LHW_TXQ_LOCK(lhw);
9559 ieee80211_txq_schedule_start(hw, txq->ac);
9560 do {
9561 struct lkpi_txq *ltxq;
9562 struct ieee80211_txq *ntxq;
9563 struct ieee80211_tx_control control;
9564 struct sk_buff *skb;
9565
9566 ntxq = ieee80211_next_txq(hw, txq->ac);
9567 if (ntxq == NULL)
9568 break;
9569 ltxq = TXQ_TO_LTXQ(ntxq);
9570
9571 memset(&control, 0, sizeof(control));
9572 control.sta = ntxq->sta;
9573 do {
9574 skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
9575 if (skb == NULL)
9576 break;
9577 ltxq->frms_tx++;
9578 lkpi_80211_mo_tx(hw, &control, skb);
9579 } while(1);
9580
9581 ieee80211_return_txq(hw, ntxq, false);
9582 } while (1);
9583 ieee80211_txq_schedule_end(hw, txq->ac);
9584 LKPI_80211_LHW_TXQ_UNLOCK(lhw);
9585 }
9586
9587 /* -------------------------------------------------------------------------- */
9588
9589 /* This is just hardware queues. */
9590 /*
9591 * Being called from the driver thus use _bh() locking.
9592 */
9593 void
9594 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
9595 {
9596 struct lkpi_hw *lhw;
9597
9598 lhw = HW_TO_LHW(hw);
9599
9600 if (ac >= IEEE80211_NUM_ACS) {
9601 ic_printf(lhw->ic, "%s: ac %u out of bounds.\n", __func__, ac);
9602 return;
9603 }
9604
9605 spin_lock_bh(&lhw->txq_scheduled_lock[ac]);
9606 IMPROVE("check AIRTIME_FAIRNESS");
9607 if (++lhw->txq_generation[ac] == 0)
9608 lhw->txq_generation[ac]++;
9609 spin_unlock_bh(&lhw->txq_scheduled_lock[ac]);
9610 }
9611
9612 struct ieee80211_txq *
9613 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
9614 {
9615 struct lkpi_hw *lhw;
9616 struct ieee80211_txq *txq;
9617 struct lkpi_txq *ltxq;
9618
9619 lhw = HW_TO_LHW(hw);
9620 txq = NULL;
9621
9622 if (ac >= IEEE80211_NUM_ACS) {
9623 ic_printf(lhw->ic, "%s: ac %u out of bounds.\n", __func__, ac);
9624 return (NULL);
9625 }
9626
9627 spin_lock_bh(&lhw->txq_scheduled_lock[ac]);
9628
9629 /* Check that we are scheduled. */
9630 if (lhw->txq_generation[ac] == 0)
9631 goto out;
9632
9633 ltxq = TAILQ_FIRST(&lhw->txq_scheduled[ac]);
9634 if (ltxq == NULL)
9635 goto out;
9636 if (ltxq->txq_generation == lhw->txq_generation[ac])
9637 goto out;
9638 if ((ltxq->flags & (LKPI_TXQ_STOPPED|LKPI_TXQ_STOPPED_BA)) != 0)
9639 goto out;
9640
9641 IMPROVE("check AIRTIME_FAIRNESS");
9642
9643 TAILQ_REMOVE(&lhw->txq_scheduled[ac], ltxq, txq_entry);
9644 ltxq->txq_generation = lhw->txq_generation[ac];
9645 txq = <xq->txq;
9646 TAILQ_ELEM_INIT(ltxq, txq_entry);
9647
9648 out:
9649 spin_unlock_bh(&lhw->txq_scheduled_lock[ac]);
9650
9651 return (txq);
9652 }
9653
9654 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
9655 struct ieee80211_txq *txq, bool withoutpkts)
9656 {
9657 struct lkpi_hw *lhw;
9658 struct lkpi_txq *ltxq;
9659 bool ltxq_empty;
9660
9661 ltxq = TXQ_TO_LTXQ(txq);
9662
9663 /* Only schedule if work to do or asked to anyway. */
9664 LKPI_80211_LTXQ_LOCK(ltxq);
9665 ltxq_empty = skb_queue_empty(<xq->skbq);
9666 LKPI_80211_LTXQ_UNLOCK(ltxq);
9667 if (!withoutpkts && ltxq_empty)
9668 goto out;
9669
9670 lhw = HW_TO_LHW(hw);
9671 spin_lock_bh(&lhw->txq_scheduled_lock[txq->ac]);
9672 /*
9673 * Make sure we do not double-schedule. We do this by checking tqe_prev,
9674 * the previous entry in our tailq. tqe_prev is always valid if this entry
9675 * is queued, tqe_next may be NULL if this is the only element in the list.
9676 */
9677 if (ltxq->txq_entry.tqe_prev != NULL)
9678 goto unlock;
9679
9680 TAILQ_INSERT_TAIL(&lhw->txq_scheduled[txq->ac], ltxq, txq_entry);
9681 unlock:
9682 spin_unlock_bh(&lhw->txq_scheduled_lock[txq->ac]);
9683
9684 out:
9685 return;
9686 }
9687
9688 /* -------------------------------------------------------------------------- */
9689
9690 struct lkpi_cfg80211_bss {
9691 u_int refcnt;
9692 struct cfg80211_bss bss;
9693 };
9694
9695 struct lkpi_cfg80211_get_bss_iter_lookup {
9696 struct wiphy *wiphy;
9697 struct linuxkpi_ieee80211_channel *chan;
9698 const uint8_t *bssid;
9699 const uint8_t *ssid;
9700 size_t ssid_len;
9701 enum ieee80211_bss_type bss_type;
9702 enum ieee80211_privacy privacy;
9703
9704 /*
9705 * Something to store a copy of the result as the net80211 scan cache
9706 * is not refoucnted so a scan entry might go away any time.
9707 */
9708 bool match;
9709 struct cfg80211_bss *bss;
9710 };
9711
9712 static void
9713 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
9714 {
9715 struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
9716 size_t ielen;
9717
9718 lookup = arg;
9719
9720 /* Do not try to find another match. */
9721 if (lookup->match)
9722 return;
9723
9724 /* Nothing to store result. */
9725 if (lookup->bss == NULL)
9726 return;
9727
9728 if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
9729 /* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
9730 /* We have no idea what to compare to as the drivers only request ANY */
9731 return;
9732 }
9733
9734 if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
9735 /* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
9736 /* We have no idea what to compare to as the drivers only request ANY */
9737 return;
9738 }
9739
9740 if (lookup->chan != NULL) {
9741 struct linuxkpi_ieee80211_channel *chan;
9742
9743 chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
9744 se->se_chan->ic_freq);
9745 if (chan == NULL || chan != lookup->chan)
9746 return;
9747 }
9748
9749 if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
9750 return;
9751
9752 if (lookup->ssid) {
9753 if (lookup->ssid_len != se->se_ssid[1] ||
9754 se->se_ssid[1] == 0)
9755 return;
9756 if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
9757 return;
9758 }
9759
9760 ielen = se->se_ies.len;
9761
9762 lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
9763 M_LKPI80211, M_NOWAIT | M_ZERO);
9764 if (lookup->bss->ies == NULL)
9765 return;
9766
9767 lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
9768 lookup->bss->ies->len = ielen;
9769 if (ielen)
9770 memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
9771
9772 lookup->match = true;
9773 }
9774
9775 struct cfg80211_bss *
9776 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
9777 const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
9778 enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
9779 {
9780 struct lkpi_cfg80211_bss *lbss;
9781 struct lkpi_cfg80211_get_bss_iter_lookup lookup;
9782 struct lkpi_hw *lhw;
9783 struct ieee80211vap *vap;
9784
9785 lhw = wiphy_priv(wiphy);
9786
9787 /* Let's hope we can alloc. */
9788 lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
9789 if (lbss == NULL) {
9790 ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
9791 return (NULL);
9792 }
9793
9794 lookup.wiphy = wiphy;
9795 lookup.chan = chan;
9796 lookup.bssid = bssid;
9797 lookup.ssid = ssid;
9798 lookup.ssid_len = ssid_len;
9799 lookup.bss_type = bss_type;
9800 lookup.privacy = privacy;
9801 lookup.match = false;
9802 lookup.bss = &lbss->bss;
9803
9804 IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
9805 vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
9806 ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
9807 if (!lookup.match) {
9808 free(lbss, M_LKPI80211);
9809 return (NULL);
9810 }
9811
9812 refcount_init(&lbss->refcnt, 1);
9813 return (&lbss->bss);
9814 }
9815
9816 void
9817 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
9818 {
9819 struct lkpi_cfg80211_bss *lbss;
9820
9821 lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
9822
9823 /* Free everything again on refcount ... */
9824 if (refcount_release(&lbss->refcnt)) {
9825 free(lbss->bss.ies, M_LKPI80211);
9826 free(lbss, M_LKPI80211);
9827 }
9828 }
9829
9830 void
9831 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
9832 {
9833 struct lkpi_hw *lhw;
9834 struct ieee80211com *ic;
9835 struct ieee80211vap *vap;
9836
9837 lhw = wiphy_priv(wiphy);
9838 ic = lhw->ic;
9839
9840 /*
9841 * If we haven't called ieee80211_ifattach() yet
9842 * or there is no VAP, there are no scans to flush.
9843 */
9844 if (ic == NULL ||
9845 (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
9846 return;
9847
9848 /* Should only happen on the current one? Not seen it late enough. */
9849 IEEE80211_LOCK(ic);
9850 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
9851 ieee80211_scan_flush(vap);
9852 IEEE80211_UNLOCK(ic);
9853 }
9854
9855 /* -------------------------------------------------------------------------- */
9856
9857 static bool
9858 cfg80211_chan_def_are_same(struct cfg80211_chan_def *cd1,
9859 struct cfg80211_chan_def *cd2)
9860 {
9861
9862 if (cd1 == cd2)
9863 return (true);
9864
9865 if (cd1 == NULL || cd2 == NULL)
9866 return (false);
9867
9868 if (cd1->chan != cd2->chan)
9869 return (false);
9870
9871 if (cd1->width != cd2->width)
9872 return (false);
9873
9874 if (cd1->center_freq1 != cd2->center_freq1)
9875 return (false);
9876
9877 if (cd1->center_freq2 != cd2->center_freq2)
9878 return (false);
9879
9880 if (cd1->punctured != cd2->punctured)
9881 return (false);
9882
9883 return (true);
9884 }
9885
9886 /*
9887 * hw->conf get initialized/set in various places for us:
9888 * - linuxkpi_ieee80211_alloc_hw(): flags
9889 * - linuxkpi_ieee80211_ifattach(): chandef
9890 * - lkpi_ic_vap_create(): listen_interval
9891 * - lkpi_ic_set_channel(): chandef, flags
9892 */
9893
9894 static int
9895 lkpi_80211_update_chandef(struct ieee80211_hw *hw,
9896 struct ieee80211_chanctx_conf *new)
9897 {
9898 struct lkpi_hw *lhw;
9899 struct cfg80211_chan_def *cd;
9900 uint32_t changed;
9901 int error;
9902 bool same;
9903
9904 lockdep_assert_wiphy(hw->wiphy);
9905
9906 lhw = HW_TO_LHW(hw);
9907 if (!lhw->emulate_chanctx)
9908 return (0);
9909
9910 if (new == NULL || new->def.chan == NULL) {
9911 /*
9912 * In case of remove "new" is NULL, we need to get us to some
9913 * basic channel width but we'd also need to set the channel
9914 * accordingly somewhere.
9915 * The same is true if we are scanning in which case the
9916 * scan_chandef should have a channel set.
9917 */
9918 if (lhw->scan_chandef.chan != NULL) {
9919 #ifdef LINUXKPI_DEBUG_80211
9920 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
9921 ic_printf(lhw->ic, "%s:%d: using scan_chandef %p\n",
9922 __func__, __LINE__, &lhw->scan_chandef);
9923 #endif
9924 cd = &lhw->scan_chandef;
9925 } else {
9926 #ifdef LINUXKPI_DEBUG_80211
9927 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
9928 ic_printf(lhw->ic, "%s:%d: using dflt_chandef %p\n",
9929 __func__, __LINE__, &lhw->dflt_chandef);
9930 #endif
9931 cd = &lhw->dflt_chandef;
9932 }
9933 } else {
9934 #ifdef LINUXKPI_DEBUG_80211
9935 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
9936 ic_printf(lhw->ic, "%s:%d: using chanctx %p chandef %p\n",
9937 __func__, __LINE__, new, &new->def);
9938 #endif
9939 cd = &new->def;
9940 }
9941
9942 changed = 0;
9943 same = cfg80211_chan_def_are_same(cd, &hw->conf.chandef);
9944 if (!same) {
9945 /* Copy; the chan pointer is fine and will stay valid. */
9946 hw->conf.chandef = *cd;
9947 changed |= IEEE80211_CONF_CHANGE_CHANNEL;
9948 }
9949 IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
9950
9951 #ifdef LINUXKPI_DEBUG_80211
9952 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
9953 ic_printf(lhw->ic, "%s:%d: chanctx %p { %u } cd %p { %u } "
9954 "hw->conf.chandef %p { %u %d %u %u %u }, "
9955 "changed %#04x same %d\n",
9956 __func__, __LINE__,
9957 new, (new != NULL && new->def.chan != NULL) ?
9958 new->def.chan->center_freq : 0,
9959 cd, cd->chan->center_freq,
9960 &hw->conf.chandef, hw->conf.chandef.chan->center_freq,
9961 hw->conf.chandef.width,
9962 hw->conf.chandef.center_freq1,
9963 hw->conf.chandef.center_freq2,
9964 hw->conf.chandef.punctured,
9965 changed, same);
9966 #endif
9967
9968 if (changed == 0)
9969 return (0);
9970
9971 error = lkpi_80211_mo_config(hw, changed);
9972 return (error);
9973 }
9974
9975 int
9976 ieee80211_emulate_add_chanctx(struct ieee80211_hw *hw,
9977 struct ieee80211_chanctx_conf *chanctx_conf)
9978 {
9979 int error;
9980
9981 lockdep_assert_wiphy(hw->wiphy);
9982
9983 #ifdef LINUXKPI_DEBUG_80211
9984 if ((linuxkpi_debug_80211 & D80211_TRACE) != 0) {
9985 struct lkpi_hw *lhw;
9986
9987 lhw = HW_TO_LHW(hw);
9988 ic_printf(lhw->ic, "%s:%d: chanctx_conf %p\n",
9989 __func__, __LINE__, chanctx_conf);
9990 }
9991 #endif
9992
9993 hw->conf.radar_enabled = chanctx_conf->radar_enabled;
9994 error = lkpi_80211_update_chandef(hw, chanctx_conf);
9995 return (error);
9996 }
9997
9998 void
9999 ieee80211_emulate_remove_chanctx(struct ieee80211_hw *hw,
10000 struct ieee80211_chanctx_conf *chanctx_conf __unused)
10001 {
10002
10003 lockdep_assert_wiphy(hw->wiphy);
10004
10005 #ifdef LINUXKPI_DEBUG_80211
10006 if ((linuxkpi_debug_80211 & D80211_TRACE) != 0) {
10007 struct lkpi_hw *lhw;
10008
10009 lhw = HW_TO_LHW(hw);
10010 ic_printf(lhw->ic, "%s:%d: chanctx_conf %p\n",
10011 __func__, __LINE__, chanctx_conf);
10012 }
10013 #endif
10014
10015 hw->conf.radar_enabled = false;
10016 lkpi_80211_update_chandef(hw, NULL);
10017 }
10018
10019 void
10020 ieee80211_emulate_change_chanctx(struct ieee80211_hw *hw,
10021 struct ieee80211_chanctx_conf *chanctx_conf, uint32_t changed __unused)
10022 {
10023
10024 lockdep_assert_wiphy(hw->wiphy);
10025
10026 #ifdef LINUXKPI_DEBUG_80211
10027 if ((linuxkpi_debug_80211 & D80211_TRACE) != 0) {
10028 struct lkpi_hw *lhw;
10029
10030 lhw = HW_TO_LHW(hw);
10031 ic_printf(lhw->ic, "%s:%d: chanctx_conf %p\n",
10032 __func__, __LINE__, chanctx_conf);
10033 }
10034 #endif
10035
10036 hw->conf.radar_enabled = chanctx_conf->radar_enabled;
10037 lkpi_80211_update_chandef(hw, chanctx_conf);
10038 }
10039
10040 int
10041 ieee80211_emulate_switch_vif_chanctx(struct ieee80211_hw *hw,
10042 struct ieee80211_vif_chanctx_switch *vifs, int n_vifs,
10043 enum ieee80211_chanctx_switch_mode mode __unused)
10044 {
10045 struct ieee80211_chanctx_conf *chanctx_conf;
10046 int error;
10047
10048 lockdep_assert_wiphy(hw->wiphy);
10049
10050 /* Sanity check. */
10051 if (n_vifs <= 0)
10052 return (-EINVAL);
10053 if (vifs == NULL || vifs[0].new_ctx == NULL)
10054 return (-EINVAL);
10055
10056 /*
10057 * What to do if n_vifs > 1?
10058 * Does that make sense for drivers not supporting chanctx?
10059 */
10060 hw->conf.radar_enabled = vifs[0].new_ctx->radar_enabled;
10061 chanctx_conf = vifs[0].new_ctx;
10062 error = lkpi_80211_update_chandef(hw, chanctx_conf);
10063 return (error);
10064 }
10065
10066 /* -------------------------------------------------------------------------- */
10067 /* LinuxKPI 802.11 PM. */
10068 int
10069 lkpi_80211_suspend(struct ieee80211com *ic, pm_message_t state)
10070 {
10071 struct lkpi_hw *lhw;
10072 struct ieee80211_hw *hw;
10073 int error;
10074
10075 lhw = ic->ic_softc;
10076 hw = LHW_TO_HW(lhw);
10077 error = 0;
10078
10079 /* Check:
10080 * - device_set_wakeup_capable() / device_can_wakeup()
10081 * - hw->wiphy->wowlan to be non-NULL, if so contents.
10082 * - hw->wiphy->max_sched_scan_ssids (rtw88)
10083 */
10084 if ((lkpi_suspend_type & 0x2) != 0) {
10085 struct cfg80211_wowlan wowlan;
10086
10087 IMPROVE("various options for WoWLAN");
10088 memset(&wowlan, 0, sizeof(wowlan));
10089 wiphy_lock(hw->wiphy);
10090 error = lkpi_80211_mo_suspend(hw, &wowlan);
10091 wiphy_unlock(hw->wiphy);
10092 if (error == EOPNOTSUPP)
10093 error = 0;
10094 }
10095 if ((lkpi_suspend_type & 0x1) != 0) {
10096 struct lkpi_vif *lvif;
10097
10098 ieee80211_suspend_all(ic);
10099
10100 wiphy_lock(hw->wiphy);
10101 /*
10102 * At the end of this net80211 will run a task to call
10103 * (*ic_parent)() which is entirely unhelpful as we do not
10104 * know when it will happen. So deal with it here.
10105 */
10106 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
10107 lkpi_80211_mo_remove_interface(hw, LVIF_TO_VIF(lvif));
10108 }
10109
10110 if ((lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) != 0)
10111 lkpi_80211_mo_stop(hw, true);
10112 wiphy_unlock(hw->wiphy);
10113 }
10114
10115 if (error < 0)
10116 error = -error;
10117
10118 if (error != 0)
10119 ic_printf(ic, "%s: SUSPEND FAILED: %d\n", __func__, error);
10120
10121 return (error);
10122 }
10123
10124 int
10125 lkpi_80211_resume(struct ieee80211com *ic)
10126 {
10127 struct lkpi_hw *lhw;
10128 struct ieee80211_hw *hw;
10129 int error;
10130 bool hw_scan_running;
10131
10132 lhw = ic->ic_softc;
10133 hw = LHW_TO_HW(lhw);
10134 error = 0;
10135
10136 /*
10137 * Ongoing HW scans during suspend are a problem on resume.
10138 * Be verbose about that.
10139 */
10140 LKPI_80211_LHW_SCAN_LOCK(lhw);
10141 hw_scan_running = (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) != 0;
10142 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
10143 if (hw_scan_running)
10144 ic_printf(ic, "%s: WARNING: ongoing hw scan on resume!\n", __func__);
10145
10146 if ((lkpi_suspend_type & 0x1) != 0) {
10147 struct lkpi_vif *lvif;
10148
10149 wiphy_lock(hw->wiphy);
10150 error = lkpi_80211_mo_start(hw);
10151 if (error != 0 && error != EEXIST) {
10152 ic_printf(ic, "%s: mo_start failed: %d\n",
10153 __func__, error);
10154 wiphy_unlock(hw->wiphy);
10155 goto err;
10156 }
10157
10158 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
10159 error = lkpi_80211_mo_add_interface(hw, LVIF_TO_VIF(lvif));
10160 if (error != 0) {
10161 struct ieee80211vap *vap;
10162
10163 vap = LVIF_TO_VAP(lvif);
10164 ic_printf(ic, "%s: mo_add_interface %s failed: %d\n",
10165 __func__, if_name(vap->iv_ifp), error);
10166 wiphy_unlock(hw->wiphy);
10167 goto err;
10168 }
10169 }
10170 wiphy_unlock(hw->wiphy);
10171
10172 ieee80211_resume_all(ic);
10173 }
10174
10175 if ((lkpi_suspend_type & 0x2) != 0) {
10176 wiphy_lock(hw->wiphy);
10177 error = lkpi_80211_mo_resume(hw);
10178 wiphy_unlock(hw->wiphy);
10179 if (error == EOPNOTSUPP)
10180 error = 0;
10181 }
10182
10183 err:
10184 if (error < 0)
10185 error = -error;
10186
10187 return (error);
10188 }
10189
10190 /* -------------------------------------------------------------------------- */
10191 MODULE_VERSION(linuxkpi_wlan, 1);
10192 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
10193 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
10194