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 lockdep_assert_wiphy(hw->wiphy);
2723
2724 if (ieee80211_vif_is_mld(vif)) {
2725 TODO("This likely needs a subset only; split up into 3 parts.");
2726 }
2727
2728 /* Nothing to do? */
2729 if (bss_changed == 0)
2730 return;
2731
2732 /*
2733 * If the vif is not known to the driver there is nothing to notifiy for.
2734 * We MUST NOT check for !lvif_bss_synched here (the reasonable it seems)
2735 * as we need to execute the update(s) or we will have follow-up issues.
2736 */
2737 lvif = VIF_TO_LVIF(vif);
2738 if (!lvif->added_to_drv)
2739 return;
2740
2741 /*
2742 * With the advent of MLO bss_conf got split up into vif and link
2743 * change notfications, while historically it was one.
2744 * We now need to support all possible models.
2745 */
2746 vif_cfg_bits = bss_changed & BSS_CHANGED_VIF_CFG_BITS;
2747 if (vif_cfg_bits != 0)
2748 lkpi_80211_mo_vif_cfg_changed(hw, vif, vif_cfg_bits, false);
2749
2750 link_info_bits = bss_changed & ~(BSS_CHANGED_VIF_CFG_BITS);
2751 if (link_info_bits != 0)
2752 lkpi_80211_mo_link_info_changed(hw, vif, &vif->bss_conf,
2753 link_info_bits, 0, false);
2754
2755 lkpi_80211_mo_bss_info_changed(hw, vif, &vif->bss_conf, bss_changed);
2756 }
2757
2758 /* -------------------------------------------------------------------------- */
2759
2760 static int
lkpi_sta_state_do_nada(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2761 lkpi_sta_state_do_nada(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2762 {
2763 return (0);
2764 }
2765
2766 /* UP1 */
2767 static int
lkpi_sta_init_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2768 lkpi_sta_init_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2769 {
2770 return (lkpi_sta_state_do_nada(vap, nstate, arg));
2771 }
2772
2773 /* UP2 */
2774 static int
lkpi_sta_scan_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)2775 lkpi_sta_scan_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
2776 {
2777 struct linuxkpi_ieee80211_channel *chan;
2778 struct cfg80211_chan_def chandef;
2779 struct ieee80211_chanctx_conf *chanctx_conf;
2780 struct lkpi_hw *lhw;
2781 struct ieee80211_hw *hw;
2782 struct lkpi_vif *lvif;
2783 struct ieee80211_vif *vif;
2784 struct ieee80211_node *ni;
2785 struct lkpi_sta *lsta;
2786 enum ieee80211_bss_changed bss_changed;
2787 struct ieee80211_prep_tx_info prep_tx_info;
2788 uint32_t changed;
2789 int error;
2790 bool synched, can_ht;
2791
2792 /*
2793 * In here we use vap->iv_bss until lvif->lvif_bss is set.
2794 * For all later (STATE >= AUTH) functions we need to use the lvif
2795 * cache which will be tracked even through (*iv_update_bss)().
2796 */
2797
2798 if (vap->iv_bss == NULL) {
2799 ic_printf(vap->iv_ic, "%s: no iv_bss for vap %p\n", __func__, vap);
2800 return (EINVAL);
2801 }
2802 /*
2803 * Keep the ni alive locally. In theory (and practice) iv_bss can change
2804 * once we unlock here. This is due to net80211 allowing state changes
2805 * and new join1() despite having an active node as well as due to
2806 * the fact that the iv_bss can be swapped under the hood in (*iv_update_bss).
2807 */
2808 ni = ieee80211_ref_node(vap->iv_bss);
2809 if (ni->ni_chan == NULL || ni->ni_chan == IEEE80211_CHAN_ANYC) {
2810 ic_printf(vap->iv_ic, "%s: no channel set for iv_bss ni %p "
2811 "on vap %p\n", __func__, ni, vap);
2812 ieee80211_free_node(ni); /* Error handling for the local ni. */
2813 return (EINVAL);
2814 }
2815
2816 lhw = vap->iv_ic->ic_softc;
2817 chan = lkpi_find_lkpi80211_chan(lhw, ni->ni_chan);
2818 if (chan == NULL) {
2819 ic_printf(vap->iv_ic, "%s: failed to get LKPI channel from "
2820 "iv_bss ni %p on vap %p\n", __func__, ni, vap);
2821 ieee80211_free_node(ni); /* Error handling for the local ni. */
2822 return (ESRCH);
2823 }
2824
2825 hw = LHW_TO_HW(lhw);
2826 lvif = VAP_TO_LVIF(vap);
2827 vif = LVIF_TO_VIF(lvif);
2828
2829 LKPI_80211_LVIF_LOCK(lvif);
2830 /* XXX-BZ KASSERT later? */
2831 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL) {
2832 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2833 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
2834 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2835 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2836 lvif->lvif_bss_synched);
2837 LKPI_80211_LVIF_UNLOCK(lvif);
2838 ieee80211_free_node(ni); /* Error handling for the local ni. */
2839 return (EBUSY);
2840 }
2841 LKPI_80211_LVIF_UNLOCK(lvif);
2842
2843 IEEE80211_UNLOCK(vap->iv_ic);
2844 wiphy_lock(hw->wiphy);
2845
2846 /* Add chanctx (or if exists, change it). */
2847 chanctx_conf = lkpi_get_chanctx_conf(hw, vif);
2848
2849 KASSERT(ni->ni_chan != NULL && ni->ni_chan != IEEE80211_CHAN_ANYC,
2850 ("%s:%d: ni %p ni_chan %p\n", __func__, __LINE__, ni, ni->ni_chan));
2851
2852 #ifdef LKPI_80211_HT
2853 can_ht = (vap->iv_ic->ic_flags_ht & IEEE80211_FHT_HT) != 0;
2854 #else
2855 can_ht = false;
2856 #endif
2857 lkpi_init_chandef(vap->iv_ic, &chandef, chan, ni->ni_chan, can_ht);
2858 hw->conf.radar_enabled =
2859 ((chan->flags & IEEE80211_CHAN_RADAR) != 0) ? true : false;
2860 hw->conf.chandef = chandef;
2861 vif->bss_conf.chanreq.oper = hw->conf.chandef;
2862 #ifdef LINUXKPI_DEBUG_80211
2863 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
2864 ic_printf(vap->iv_ic, "%s:%d: hw->conf.chandef %p = chandef %p = "
2865 "vif->bss_conf.chanreq.oper %p\n", __func__, __LINE__,
2866 &hw->conf.chandef, &chandef, &vif->bss_conf.chanreq.oper);
2867 #endif
2868
2869 changed = lkpi_init_chanctx_conf(hw, &chandef, chanctx_conf);
2870
2871 /* Responder ... */
2872
2873 /* Set bss info (bss_info_changed). */
2874 bss_changed = 0;
2875 IEEE80211_ADDR_COPY(vif->cfg.ap_addr, ni->ni_bssid);
2876 vif->bss_conf.bssid = ni->ni_bssid;
2877 bss_changed |= BSS_CHANGED_BSSID;
2878 vif->bss_conf.txpower = ni->ni_txpower;
2879 bss_changed |= BSS_CHANGED_TXPOWER;
2880 vif->cfg.idle = false;
2881 bss_changed |= BSS_CHANGED_IDLE;
2882
2883 lvif->beacons = 0;
2884 /* Should almost assert it is this. */
2885 vif->cfg.assoc = false;
2886 vif->cfg.aid = 0;
2887
2888 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
2889
2890 error = lkpi_set_chanctx_conf(hw, vif, chanctx_conf, changed, true);
2891 if (error != 0)
2892 goto out;
2893
2894 IMPROVE("update radiotap chan fields too");
2895
2896 /* RATES */
2897 IMPROVE("bss info: not all needs to come now and rates are missing");
2898 bss_changed |= lkpi_sta_supp_rates(hw, vif, ni, NULL);
2899 if (ieee80211_hw_check(hw, HAS_RATE_CONTROL))
2900 lkpi_80211_mo_set_bitrate_mask(hw, vif, &lvif->br_mask);
2901 TODO("cfg80211_tid_config WHERE?");
2902
2903 lkpi_bss_info_change(hw, vif, bss_changed);
2904
2905 /*
2906 * Given ni and lsta are 1:1 from alloc to free we can assert that
2907 * ni always has lsta data attach despite net80211 node swapping
2908 * under the hoods.
2909 */
2910 KASSERT(ni->ni_drv_data != NULL, ("%s: ni %p ni_drv_data %p\n",
2911 __func__, ni, ni->ni_drv_data));
2912 lsta = ni->ni_drv_data;
2913
2914 /* Insert the [l]sta into the list of known stations. */
2915 list_add_tail(&lsta->lsta_list, &lvif->lsta_list);
2916
2917 /* Add (or adjust) sta and change state (from NOTEXIST) to NONE. */
2918 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
2919 KASSERT(lsta->state == IEEE80211_STA_NOTEXIST, ("%s: lsta %p state not "
2920 "NOTEXIST: %#x\n", __func__, lsta, lsta->state));
2921 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
2922 if (error != 0) {
2923 IMPROVE("do we need to undo the chan ctx?");
2924 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
2925 "failed: %d\n", __func__, __LINE__, error);
2926 goto out;
2927 }
2928 #if 0
2929 lsta->added_to_drv = true; /* mo manages. */
2930 #endif
2931
2932 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
2933
2934 #if 0
2935 /*
2936 * Wakeup all queues now that sta is there so we have as much time to
2937 * possibly prepare the queue in the driver to be ready for the 1st
2938 * packet; lkpi_80211_txq_tx_one() still has a workaround as there
2939 * is no guarantee or way to check.
2940 * XXX-BZ and by now we know that this does not work on all drivers
2941 * for all queues.
2942 */
2943 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, false);
2944 #endif
2945
2946 /* Start mgd_prepare_tx. */
2947 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
2948 prep_tx_info.duration = PREP_TX_INFO_DURATION; /* SAE */
2949 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
2950 prep_tx_info.link_id = 0;
2951 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
2952 lsta->in_mgd = true;
2953
2954 /*
2955 * What is going to happen next:
2956 * - <twiddle> .. we should end up in "auth_to_assoc"
2957 * - event_callback
2958 * - update sta_state (NONE to AUTH)
2959 * - mgd_complete_tx
2960 * (ideally we'd do that on a callback for something else ...)
2961 */
2962
2963 wiphy_unlock(hw->wiphy);
2964 IEEE80211_LOCK(vap->iv_ic);
2965
2966 LKPI_80211_LVIF_LOCK(lvif);
2967 /* Re-check given (*iv_update_bss) could have happened while we were unlocked. */
2968 if (lvif->lvif_bss_synched || lvif->lvif_bss != NULL ||
2969 lsta->ni != vap->iv_bss)
2970 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
2971 "lvif_bss->ni %p synched %d, ni %p lsta %p\n", __func__, __LINE__,
2972 lvif, vap, vap->iv_bss, lvif->lvif_bss,
2973 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
2974 lvif->lvif_bss_synched, ni, lsta);
2975
2976 /*
2977 * Reference the "ni" for caching the lsta/ni in lvif->lvif_bss.
2978 * Given we cache lsta we use lsta->ni instead of ni here (even though
2979 * lsta->ni == ni) to be distinct from the rest of the code where we do
2980 * assume that ni == vap->iv_bss which it may or may not be.
2981 * So do NOT use iv_bss here anymore as that may have diverged from our
2982 * function local ni already while ic was unlocked and would lead to
2983 * inconsistencies. Go and see if we lost a race and do not update
2984 * lvif_bss_synched in that case.
2985 */
2986 ieee80211_ref_node(lsta->ni);
2987 lvif->lvif_bss = lsta;
2988 if (lsta->ni == vap->iv_bss) {
2989 lvif->lvif_bss_synched = synched = true;
2990 } else {
2991 /* Set to un-synched no matter what. */
2992 lvif->lvif_bss_synched = synched = false;
2993 /*
2994 * We do not error as someone has to take us down.
2995 * If we are followed by a 2nd, new net80211::join1() going to
2996 * AUTH lkpi_sta_a_to_a() will error, lkpi_sta_auth_to_{scan,init}()
2997 * will take the lvif->lvif_bss node down eventually.
2998 * What happens with the vap->iv_bss node will entirely be up
2999 * to net80211 as we never used the node beyond alloc()/free()
3000 * and we do not hold an extra reference for that anymore given
3001 * ni : lsta == 1:1.
3002 * Problem is if we do not error a MGMT/AUTH frame will be
3003 * sent from net80211::sta_newstate(); disable lsta queue below.
3004 */
3005 }
3006 LKPI_80211_LVIF_UNLOCK(lvif);
3007 /*
3008 * Make sure in case the sta did not change and we re-added it,
3009 * that we can tx again but only if the vif/iv_bss are in sync.
3010 * Otherwise this should prevent the MGMT/AUTH frame from being
3011 * sent triggering a warning in iwlwifi.
3012 */
3013 LKPI_80211_LSTA_TXQ_LOCK(lsta);
3014 lsta->txq_ready = synched;
3015 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
3016 goto out_relocked;
3017
3018 out:
3019 wiphy_unlock(hw->wiphy);
3020 IEEE80211_LOCK(vap->iv_ic);
3021 out_relocked:
3022 /*
3023 * Release the reference that kept the ni stable locally
3024 * during the work of this function.
3025 */
3026 if (ni != NULL)
3027 ieee80211_free_node(ni);
3028 return (error);
3029 }
3030
3031 /* UP3.1 */
3032 static int
lkpi_sta_auth_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3033 lkpi_sta_auth_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3034 {
3035 struct lkpi_hw *lhw;
3036 struct ieee80211_hw *hw;
3037 struct lkpi_vif *lvif;
3038 struct ieee80211_vif *vif;
3039 struct lkpi_sta *lsta;
3040 struct ieee80211_prep_tx_info prep_tx_info;
3041 int error;
3042
3043 lhw = vap->iv_ic->ic_softc;
3044 hw = LHW_TO_HW(lhw);
3045 lvif = VAP_TO_LVIF(vap);
3046 vif = LVIF_TO_VIF(lvif);
3047
3048 IEEE80211_UNLOCK(vap->iv_ic);
3049 wiphy_lock(hw->wiphy);
3050
3051 LKPI_80211_LVIF_LOCK(lvif);
3052 /* XXX-BZ KASSERT later? */
3053 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
3054 #ifdef LINUXKPI_DEBUG_80211
3055 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3056 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3057 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3058 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3059 lvif->lvif_bss_synched);
3060 #endif
3061 error = ENOTRECOVERABLE;
3062 LKPI_80211_LVIF_UNLOCK(lvif);
3063 goto out;
3064 }
3065 lsta = lvif->lvif_bss;
3066 LKPI_80211_LVIF_UNLOCK(lvif);
3067
3068 KASSERT(lsta != NULL, ("%s: lsta %p\n", __func__, lsta));
3069
3070 /* Finish auth. */
3071 IMPROVE("event callback");
3072
3073 /* Update sta_state (NONE to AUTH). */
3074 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3075 "NONE: %#x\n", __func__, lsta, lsta->state));
3076 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3077 if (error != 0) {
3078 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3079 "failed: %d\n", __func__, __LINE__, error);
3080 goto out;
3081 }
3082
3083 /* End mgd_complete_tx. */
3084 if (lsta->in_mgd) {
3085 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3086 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
3087 prep_tx_info.success = true;
3088 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3089 lsta->in_mgd = false;
3090 }
3091
3092 /* Now start assoc. unless nstate=RUN (auth_to_run). */
3093
3094 /* Start mgd_prepare_tx. */
3095 if (nstate == IEEE80211_S_ASSOC && !lsta->in_mgd) {
3096 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3097 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3098 prep_tx_info.link_id = 0;
3099 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3100 lsta->in_mgd = true;
3101 }
3102
3103 #if 0
3104 /* We do not yet have a packet to go out. */
3105 /* Wake tx queue to get packet out. */
3106 lkpi_wake_tx_queues(hw, LSTA_TO_STA(lsta), false, true);
3107 #endif
3108
3109 /*
3110 * <twiddle> .. we end up in "assoc_to_run"
3111 * - update sta_state (AUTH to ASSOC)
3112 * - conf_tx [all]
3113 * - bss_info_changed (assoc, aid, ssid, ..)
3114 * - change_chanctx (if needed)
3115 * - event_callback
3116 * - mgd_complete_tx
3117 */
3118
3119 out:
3120 wiphy_unlock(hw->wiphy);
3121 IEEE80211_LOCK(vap->iv_ic);
3122 return (error);
3123 }
3124
3125 static int lkpi_sta_assoc_to_run(struct ieee80211vap *, enum ieee80211_state, int);
3126
3127 /* UP3.2 */
3128 static int
lkpi_sta_auth_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3129 lkpi_sta_auth_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3130 {
3131 int error;
3132
3133 error = lkpi_sta_auth_to_assoc(vap, nstate, arg);
3134 if (error == 0)
3135 error = lkpi_sta_assoc_to_run(vap, nstate, arg);
3136 return (error);
3137 }
3138
3139 /* UP4 */
3140 static int
lkpi_sta_assoc_to_run(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3141 lkpi_sta_assoc_to_run(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3142 {
3143 struct lkpi_hw *lhw;
3144 struct ieee80211_hw *hw;
3145 struct lkpi_vif *lvif;
3146 struct ieee80211_vif *vif;
3147 struct ieee80211_node *ni;
3148 struct lkpi_sta *lsta;
3149 struct ieee80211_sta *sta;
3150 struct ieee80211_prep_tx_info prep_tx_info;
3151 enum ieee80211_bss_changed bss_changed;
3152 int error;
3153
3154 lhw = vap->iv_ic->ic_softc;
3155 hw = LHW_TO_HW(lhw);
3156 lvif = VAP_TO_LVIF(vap);
3157 vif = LVIF_TO_VIF(lvif);
3158
3159 IEEE80211_UNLOCK(vap->iv_ic);
3160 wiphy_lock(hw->wiphy);
3161
3162 LKPI_80211_LVIF_LOCK(lvif);
3163 /* XXX-BZ KASSERT later? */
3164 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
3165 #ifdef LINUXKPI_DEBUG_80211
3166 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3167 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3168 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3169 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3170 lvif->lvif_bss_synched);
3171 #endif
3172 LKPI_80211_LVIF_UNLOCK(lvif);
3173 error = ENOTRECOVERABLE;
3174 goto out;
3175 }
3176 lsta = lvif->lvif_bss;
3177 LKPI_80211_LVIF_UNLOCK(lvif);
3178 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3179 "lvif %p vap %p\n", __func__,
3180 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3181
3182 ni = lsta->ni; /* Reference held for lvif_bss. */
3183
3184 IMPROVE("ponder some of this moved to ic_newassoc, scan_assoc_success, "
3185 "and to lesser extend ieee80211_notify_node_join");
3186
3187 /* Finish assoc. */
3188 /* Update sta_state (AUTH to ASSOC) and set aid. */
3189 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3190 "AUTH: %#x\n", __func__, lsta, lsta->state));
3191 sta = LSTA_TO_STA(lsta);
3192 sta->aid = IEEE80211_NODE_AID(ni);
3193 #ifdef LKPI_80211_WME
3194 if (vap->iv_flags & IEEE80211_F_WME)
3195 sta->wme = true;
3196 #endif
3197 bss_changed = 0;
3198 /*
3199 * This sync needs to happen before the sta_state change to ASSOC.
3200 * At least mt7921 (likely all drivers) rely on, e.g., ht_cap, vht_cap,
3201 * .. to be set at the point we go to assoc.
3202 */
3203 bss_changed |= lkpi_sta_sync_from_ni(hw, vif, sta, ni, true);
3204 if (ieee80211_hw_check(hw, HAS_RATE_CONTROL))
3205 lkpi_80211_mo_set_bitrate_mask(hw, vif, &lvif->br_mask);
3206
3207 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3208 if (error != 0) {
3209 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3210 "failed: %d\n", __func__, __LINE__, error);
3211 goto out;
3212 }
3213
3214 IMPROVE("wme / conf_tx [all]");
3215
3216 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
3217 #ifdef LKPI_80211_WME
3218 bss_changed |= lkpi_wme_update(lhw, vap, true);
3219 #endif
3220 if (!vif->cfg.assoc || vif->cfg.aid != IEEE80211_NODE_AID(ni)) {
3221 lvif->beacons = 0;
3222 vif->cfg.assoc = true;
3223 vif->cfg.aid = IEEE80211_NODE_AID(ni);
3224 bss_changed |= BSS_CHANGED_ASSOC;
3225 }
3226 /* We set SSID but this is not BSSID! */
3227 vif->cfg.ssid_len = ni->ni_esslen;
3228 memcpy(vif->cfg.ssid, ni->ni_essid, ni->ni_esslen);
3229 if ((vap->iv_flags & IEEE80211_F_SHPREAMBLE) !=
3230 vif->bss_conf.use_short_preamble) {
3231 vif->bss_conf.use_short_preamble ^= 1;
3232 /* bss_changed |= BSS_CHANGED_??? */
3233 }
3234 if ((vap->iv_flags & IEEE80211_F_SHSLOT) !=
3235 vif->bss_conf.use_short_slot) {
3236 vif->bss_conf.use_short_slot ^= 1;
3237 /* bss_changed |= BSS_CHANGED_??? */
3238 }
3239 if ((ni->ni_flags & IEEE80211_NODE_QOS) !=
3240 vif->bss_conf.qos) {
3241 vif->bss_conf.qos ^= 1;
3242 bss_changed |= BSS_CHANGED_QOS;
3243 }
3244
3245 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
3246 lkpi_bss_info_change(hw, vif, bss_changed);
3247
3248 /* - change_chanctx (if needed)
3249 * - event_callback
3250 */
3251
3252 /* End mgd_complete_tx. (we do not have to check ostate == IEEE80211_S_ASSOC). */
3253 if (lsta->in_mgd) {
3254 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3255 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3256 prep_tx_info.success = true; /* Needs vif->cfg.assoc set! */
3257 prep_tx_info.link_id = 0;
3258 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3259 lsta->in_mgd = false;
3260 }
3261
3262 /*
3263 * And then:
3264 * - (more packets)?
3265 * - set_key
3266 * - set_default_unicast_key
3267 * - set_key (?)
3268 * - ipv6_addr_change (?)
3269 */
3270
3271 if (!ieee80211_node_is_authorized(ni)) {
3272 IMPROVE("net80211 does not consider node authorized");
3273 }
3274
3275 bss_changed = 0;
3276 IMPROVE("Is this the right spot, has net80211 done all updates already?");
3277
3278 /* Update thresholds. */
3279 hw->wiphy->frag_threshold = vap->iv_fragthreshold;
3280 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
3281 hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
3282 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
3283
3284 /* Update sta_state (ASSOC to AUTHORIZED). */
3285 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3286 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3287 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3288 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTHORIZED);
3289 if (error != 0) {
3290 IMPROVE("undo some changes?");
3291 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTHORIZED) "
3292 "failed: %d\n", __func__, __LINE__, error);
3293 goto out;
3294 }
3295
3296 /* - drv_config (?)
3297 * - bss_info_changed
3298 * - set_rekey_data (?)
3299 *
3300 * And now we should be passing packets.
3301 */
3302 IMPROVE("Need that bssid setting, and the keys");
3303
3304 bss_changed |= lkpi_update_dtim_tsf(vif, ni, vap, __func__, __LINE__);
3305 lkpi_bss_info_change(hw, vif, bss_changed);
3306
3307 /* Prepare_multicast && configure_filter. */
3308 lkpi_update_mcast_filter_locked(vap->iv_ic);
3309
3310 out:
3311 wiphy_unlock(hw->wiphy);
3312 IEEE80211_LOCK(vap->iv_ic);
3313 return (error);
3314 }
3315
3316 /*
3317 * DOWN1
3318 * "to assoc" means we are going back to State 2 from State 4[/3].
3319 * This means ni still is authenticated, so we keep sta, chanctx, ..
3320 * We will send a (Re)Assoc Request in case net80211 handles roadming.
3321 * Note: this can be called as part of a DEAUTH going to State 1 as well,
3322 * so for RoC prep_tx_info we need to check nstate (see run_to_{auth,scan,init}).
3323 */
3324 static int
lkpi_sta_run_to_assoc(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3325 lkpi_sta_run_to_assoc(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3326 {
3327 struct lkpi_hw *lhw;
3328 struct ieee80211_hw *hw;
3329 struct lkpi_vif *lvif;
3330 struct ieee80211_vif *vif;
3331 struct ieee80211_node *ni;
3332 struct lkpi_sta *lsta;
3333 struct ieee80211_sta *sta;
3334 struct ieee80211_prep_tx_info prep_tx_info;
3335 #if 0
3336 enum ieee80211_bss_changed bss_changed;
3337 #endif
3338 struct ieee80211_rx_ampdu *rap;
3339 int error;
3340
3341 lhw = vap->iv_ic->ic_softc;
3342 hw = LHW_TO_HW(lhw);
3343 lvif = VAP_TO_LVIF(vap);
3344 vif = LVIF_TO_VIF(lvif);
3345
3346 IEEE80211_UNLOCK(vap->iv_ic);
3347 wiphy_lock(hw->wiphy);
3348
3349 LKPI_80211_LVIF_LOCK(lvif);
3350 #ifdef LINUXKPI_DEBUG_80211
3351 /* XXX-BZ KASSERT later; state going down so no action. */
3352 if (lvif->lvif_bss == NULL)
3353 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3354 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3355 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3356 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3357 lvif->lvif_bss_synched);
3358 #endif
3359 lsta = lvif->lvif_bss;
3360 LKPI_80211_LVIF_UNLOCK(lvif);
3361 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3362 "lvif %p vap %p\n", __func__,
3363 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3364
3365 ni = lsta->ni; /* Reference held for lvif_bss. */
3366 sta = LSTA_TO_STA(lsta);
3367
3368 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3369
3370 /* flush, drop. */
3371 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
3372
3373 /* We should make this a KASSERT. */
3374 if (lsta->in_mgd) {
3375 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p lsta %p in_mgd\n",
3376 __func__, __LINE__, lvif, vap, lsta);
3377 }
3378 /*
3379 * Problem is that we should hook into the tx/rx flow and not
3380 * try to re-model the state machine parts. We may miss a SME
3381 * triggered frame this way.
3382 */
3383 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3384 if (nstate == IEEE80211_S_ASSOC) {
3385 if (vap->iv_roaming == IEEE80211_ROAMING_AUTO) {
3386 if (arg)
3387 prep_tx_info.subtype = IEEE80211_STYPE_REASSOC_REQ;
3388 else
3389 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3390 } else {
3391 /* wpa_supplicant upon RTM_IEEE80211_LEAVE. */
3392 prep_tx_info.subtype = IEEE80211_STYPE_DISASSOC;
3393 }
3394 } else
3395 prep_tx_info.subtype = IEEE80211_STYPE_DEAUTH;
3396 prep_tx_info.was_assoc = true;
3397 prep_tx_info.link_id = 0;
3398 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3399 lsta->in_mgd = true;
3400
3401 wiphy_unlock(hw->wiphy);
3402 IEEE80211_LOCK(vap->iv_ic);
3403
3404 /* Call iv_newstate first so we get potential (RE-)ASSOC/DEAUTH? packet out. */
3405 error = lvif->iv_newstate(vap, nstate, arg);
3406 if (error != 0) {
3407 ic_printf(vap->iv_ic, "%s:%d: iv_newstate(%p, %d, %d) "
3408 "failed: %d\n", __func__, __LINE__, vap, nstate, arg, error);
3409 goto outni;
3410 }
3411
3412 /* Stop any BA sessions if still active. */
3413 for (int rapn = 0; rapn < WME_NUM_TID; rapn++) {
3414 rap = &ni->ni_rx_ampdu[rapn];
3415
3416 if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
3417 continue;
3418
3419 vap->iv_ic->ic_ampdu_rx_stop(ni, rap);
3420 }
3421
3422 IEEE80211_UNLOCK(vap->iv_ic);
3423
3424 /* Ensure the packets get out. */
3425 lkpi_80211_flush_tx(lhw, lsta);
3426
3427 wiphy_lock(hw->wiphy);
3428
3429 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3430
3431 /* Wake tx queues to get packet(s) out. */
3432 lkpi_wake_tx_queues(hw, sta, false, true);
3433
3434 /* flush, no drop */
3435 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
3436
3437 /* End mgd_complete_tx. */
3438 /* We should make this a KASSERT. */
3439 if (!lsta->in_mgd) {
3440 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p lsta %p !in_mgd\n",
3441 __func__, __LINE__, lvif, vap, lsta);
3442 }
3443 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3444 lsta->in_mgd = false;
3445
3446 #if 0
3447 /* sync_rx_queues */
3448 lkpi_80211_mo_sync_rx_queues(hw);
3449
3450 /* sta_pre_rcu_remove */
3451 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3452 #endif
3453
3454 /* Take the station down. */
3455
3456 /* Adjust sta and change state (from AUTHORIZED) to ASSOC. */
3457 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3458 KASSERT(lsta->state == IEEE80211_STA_AUTHORIZED, ("%s: lsta %p state not "
3459 "AUTHORIZED: %#x\n", __func__, lsta, lsta->state));
3460 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_ASSOC);
3461 if (error != 0) {
3462 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(ASSOC) "
3463 "failed: %d\n", __func__, __LINE__, error);
3464 goto out;
3465 }
3466
3467 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3468
3469 #ifdef LKPI_80211_HW_CRYPTO
3470 if (lkpi_hwcrypto) {
3471 error = lkpi_sta_del_keys(hw, vif, lsta);
3472 if (error != 0) {
3473 ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3474 "failed: %d\n", __func__, __LINE__, error);
3475 /*
3476 * Either drv/fw will crash or cleanup itself,
3477 * otherwise net80211 will delete the keys (at a
3478 * less appropriate time).
3479 */
3480 /* goto out; */
3481 }
3482 }
3483 #endif
3484
3485 /* Update sta_state (ASSOC to AUTH). */
3486 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3487 KASSERT(lsta->state == IEEE80211_STA_ASSOC, ("%s: lsta %p state not "
3488 "ASSOC: %#x\n", __func__, lsta, lsta->state));
3489 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_AUTH);
3490 if (error != 0) {
3491 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(AUTH) "
3492 "failed: %d\n", __func__, __LINE__, error);
3493 goto out;
3494 }
3495
3496 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3497
3498 #if 0
3499 /* Update bss info (bss_info_changed) (assoc, aid, ..). */
3500 /* See comment in DOWN4. */
3501 lkpi_disassoc(sta, vif, lhw);
3502 #endif
3503
3504 error = EALREADY;
3505 out:
3506 wiphy_unlock(hw->wiphy);
3507 IEEE80211_LOCK(vap->iv_ic);
3508 outni:
3509 return (error);
3510 }
3511
3512 /*
3513 * DOWN2
3514 * We are in state 2 and go back to state 1 and will try to auth again
3515 * (to IEEE80211_S_AUTH in FreeBSD means "try to auth"). This should be
3516 * like scan_to_auth but that we keep the "ni" and with that chanctx/bssid,
3517 * which essentially makes this "a_to_a" in LinuxKPI.
3518 */
3519 static int
lkpi_sta_assoc_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3520 lkpi_sta_assoc_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3521 {
3522 struct lkpi_hw *lhw;
3523 struct ieee80211_hw *hw;
3524 struct lkpi_vif *lvif;
3525 struct ieee80211_vif *vif;
3526 struct ieee80211_node *ni;
3527 struct lkpi_sta *lsta;
3528 struct ieee80211_prep_tx_info prep_tx_info;
3529 int error;
3530
3531 lhw = vap->iv_ic->ic_softc;
3532 hw = LHW_TO_HW(lhw);
3533 lvif = VAP_TO_LVIF(vap);
3534 vif = LVIF_TO_VIF(lvif);
3535
3536 IEEE80211_UNLOCK(vap->iv_ic);
3537 wiphy_lock(hw->wiphy);
3538
3539 LKPI_80211_LVIF_LOCK(lvif);
3540 #ifdef LINUXKPI_DEBUG_80211
3541 /* XXX-BZ KASSERT later; state going down so no action. */
3542 if (lvif->lvif_bss == NULL)
3543 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3544 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3545 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3546 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3547 lvif->lvif_bss_synched);
3548 #endif
3549 lsta = lvif->lvif_bss;
3550 LKPI_80211_LVIF_UNLOCK(lvif);
3551 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3552 "lvif %p vap %p\n", __func__,
3553 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3554
3555 ni = lsta->ni; /* Reference held for lvif_bss. */
3556
3557 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3558
3559 /* End mgd_complete_tx. */
3560 if (lsta->in_mgd && vap->iv_state == IEEE80211_S_ASSOC) {
3561 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3562 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3563 prep_tx_info.link_id = 0;
3564 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3565 lsta->in_mgd = false;
3566 } else if (lsta->in_mgd) {
3567 ic_printf(vap->iv_ic, "%s:%d: in_mgd %d (%s) -> %d (%s) %d\n",
3568 __func__, __LINE__,
3569 vap->iv_state, ieee80211_state_name[vap->iv_state],
3570 nstate, ieee80211_state_name[nstate], arg);
3571 }
3572
3573 /* Take the station down. */
3574 /* Update sta_state (AUTH to NONE). */
3575 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3576 KASSERT(lsta->state == IEEE80211_STA_AUTH, ("%s: lsta %p state not "
3577 "AUTH: %#x\n", __func__, lsta, lsta->state));
3578 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NONE);
3579 if (error != 0) {
3580 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NONE) "
3581 "failed: %d\n", __func__, __LINE__, error);
3582 goto out;
3583 }
3584
3585 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3586
3587 out:
3588 wiphy_unlock(hw->wiphy);
3589 IEEE80211_LOCK(vap->iv_ic);
3590 return (error);
3591 }
3592
3593 /*
3594 * DOWN3
3595 * We are in state 1. Either auth timed out (arg != 0) or we have an internal
3596 * state change forcing us to give up trying to authenticate.
3597 * Cleanup and remove chanctx, sta, ...
3598 */
3599 static int
lkpi_sta_auth_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3600 lkpi_sta_auth_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3601 {
3602 struct lkpi_hw *lhw;
3603 struct ieee80211_hw *hw;
3604 struct lkpi_vif *lvif;
3605 struct ieee80211_vif *vif;
3606 struct ieee80211_node *ni;
3607 struct lkpi_sta *lsta;
3608 struct ieee80211_sta *sta;
3609 struct ieee80211_prep_tx_info prep_tx_info;
3610 enum ieee80211_bss_changed bss_changed;
3611 int error;
3612
3613 lhw = vap->iv_ic->ic_softc;
3614 hw = LHW_TO_HW(lhw);
3615 lvif = VAP_TO_LVIF(vap);
3616 vif = LVIF_TO_VIF(lvif);
3617
3618 IEEE80211_UNLOCK(vap->iv_ic);
3619 wiphy_lock(hw->wiphy);
3620
3621 LKPI_80211_LVIF_LOCK(lvif);
3622 /*
3623 * XXX-BZ KASSERT later; state going down so no action in theory
3624 * but try to avoid a NULL-pointer derref for now and gracefully
3625 * fail for non-debug kernels.
3626 */
3627 if (lvif->lvif_bss == NULL) {
3628 ic_printf(vap->iv_ic, "%s:%d: ERROR: lvif %p vap %p iv_bss %p "
3629 "lvif_bss %p lvif_bss->ni %p synched %d; "
3630 "expect follow-up problems\n", __func__, __LINE__,
3631 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3632 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3633 lvif->lvif_bss_synched);
3634 LKPI_80211_LVIF_UNLOCK(lvif);
3635 /*
3636 * This will likely lead to a firmware crash (if there
3637 * was not one before already) and need a
3638 * ieee80211_restart_hw() but still better than a panic
3639 * for users as they can at least recover.
3640 */
3641 error = ENOTRECOVERABLE;
3642 goto out;
3643 }
3644 lsta = lvif->lvif_bss;
3645 LKPI_80211_LVIF_UNLOCK(lvif);
3646 KASSERT(lsta != NULL && lsta->ni != NULL, ("%s: lsta %p ni %p "
3647 "lvif %p vap %p\n", __func__,
3648 lsta, (lsta != NULL) ? lsta->ni : NULL, lvif, vap));
3649 ni = lsta->ni; /* Reference held for lvif_bss. */
3650 sta = LSTA_TO_STA(lsta);
3651
3652 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3653
3654 /* flush, drop. */
3655 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), true);
3656
3657 /* Wake tx queues to get packet(s) out. */
3658 lkpi_wake_tx_queues(hw, sta, false, true);
3659
3660 /* flush, no drop */
3661 lkpi_80211_mo_flush(hw, vif, nitems(sta->txq), false);
3662
3663 /* End mgd_complete_tx. */
3664 if (lsta->in_mgd) {
3665 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3666 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
3667 prep_tx_info.link_id = 0;
3668 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3669 lsta->in_mgd = false;
3670 }
3671
3672 /* sync_rx_queues */
3673 lkpi_80211_mo_sync_rx_queues(hw);
3674
3675 #ifdef LKPI_80211_HW_CRYPTO
3676 if (lkpi_hwcrypto) {
3677 error = lkpi_sta_del_keys(hw, vif, lsta);
3678 if (error != 0) {
3679 ic_printf(vap->iv_ic, "%s:%d: lkpi_sta_del_keys "
3680 "failed: %d\n", __func__, __LINE__, error);
3681 /*
3682 * Either drv/fw will crash or cleanup itself,
3683 * otherwise net80211 will delete the keys (at a
3684 * less appropriate time).
3685 */
3686 /* goto out; */
3687 }
3688 }
3689 #endif
3690
3691 /* sta_pre_rcu_remove */
3692 lkpi_80211_mo_sta_pre_rcu_remove(hw, vif, sta);
3693
3694 synchronize_net();
3695
3696 /* Take the station down. */
3697
3698 bss_changed = 0;
3699 /*
3700 * Start updating bss info (*bss_info_changed) (assoc, aid, ..).
3701 *
3702 * One would expect this to happen when going off AUTHORIZED but
3703 * not so.
3704 *
3705 * Immediately issuing the (*bss_info_changed) used to also remove the
3706 * sta from firmware for iwlwifi; or we have problems with the sta
3707 * silently not being removed and then crash upon the next sta add.
3708 * Neither seems to be the case or a problem still.
3709 *
3710 * Contrary for BE200 (iwlwifi/mld) if we do not issue the
3711 * (*vif_cfg_change) to tell FW that we are no longer assoc
3712 * it will crash now upon sta rm. So the order now is as we once
3713 * expected it:
3714 *
3715 * 1) lkpi_disassoc(): set vif->cfg.assoc = false and .aid=0
3716 * 2) add the remaining BSS_CHANGED changes and call (*bss_info_changed)
3717 * (which may be split up into (*vif_cfg_change) and
3718 * (*link_info_changed) for more modern drivers).
3719 * 3) call the last sta_state update -> IEEE80211_STA_NOTEXIST
3720 * (removes the sta given assoc is false) and tidy up our lists.
3721 * 4) call unassign_vif_chanctx
3722 * 5) call lkpi_hw_conf_idle
3723 * 6) call remove_chanctx
3724 *
3725 * Note: vif->driver_flags & IEEE80211_VIF_REMOVE_AP_AFTER_DISASSOC
3726 * might change this.
3727 */
3728 bss_changed |= lkpi_disassoc(sta, vif, lhw);
3729
3730 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3731
3732 IMPROVE("Any bss_info changes to announce?");
3733 vif->bss_conf.qos = false;
3734 bss_changed |= BSS_CHANGED_QOS;
3735 vif->cfg.ssid_len = 0;
3736 memset(vif->cfg.ssid, '\0', sizeof(vif->cfg.ssid));
3737 IEEE80211_ADDR_COPY(vif->cfg.ap_addr, ieee80211broadcastaddr);
3738 bss_changed |= BSS_CHANGED_BSSID;
3739 vif->bss_conf.use_short_preamble = false;
3740 /* XXX BSS_CHANGED_???? */
3741 vif->bss_conf.dtim_period = 0; /* go back to 0. */
3742 bss_changed |= BSS_CHANGED_BEACON_INFO;
3743 lkpi_bss_info_change(hw, vif, bss_changed);
3744
3745 /* Adjust sta and change state (from NONE) to NOTEXIST. */
3746 KASSERT(lsta != NULL, ("%s: ni %p lsta is NULL\n", __func__, ni));
3747 KASSERT(lsta->state == IEEE80211_STA_NONE, ("%s: lsta %p state not "
3748 "NONE: %#x, nstate %d arg %d\n", __func__, lsta, lsta->state, nstate, arg));
3749 error = lkpi_80211_mo_sta_state(hw, vif, lsta, IEEE80211_STA_NOTEXIST);
3750 if (error != 0) {
3751 IMPROVE("do we need to undo the chan ctx?");
3752 ic_printf(vap->iv_ic, "%s:%d: mo_sta_state(NOTEXIST) "
3753 "failed: %d\n", __func__, __LINE__, error);
3754 goto out;
3755 }
3756
3757 lkpi_lsta_remove(lsta, lvif);
3758
3759 lkpi_lsta_dump(lsta, ni, __func__, __LINE__);
3760
3761 LKPI_80211_LVIF_LOCK(lvif);
3762 /* Remove ni reference for this cache of lsta. */
3763 lvif->lvif_bss = NULL;
3764 lvif->lvif_bss_synched = false;
3765 LKPI_80211_LVIF_UNLOCK(lvif);
3766
3767 /* conf_tx */
3768
3769 lkpi_remove_chanctx(hw, vif);
3770
3771 out:
3772 wiphy_unlock(hw->wiphy);
3773 IEEE80211_LOCK(vap->iv_ic);
3774 if (error == 0) {
3775 /*
3776 * We do this outside the wiphy lock as net80211::node_free() may call
3777 * into crypto code to delete keys and we have a recursed on
3778 * non-recursive sx panic. Also only do this if we get here w/o error.
3779 *
3780 * The very last release the reference on the ni for the ni/lsta on
3781 * lvif->lvif_bss. Upon return from this both ni and lsta are invalid
3782 * and potentially freed.
3783 */
3784 ieee80211_free_node(ni);
3785 }
3786 return (error);
3787 }
3788
3789 /* DOWN4 */
3790 static int
lkpi_sta_scan_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3791 lkpi_sta_scan_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3792 {
3793 /* lkpi_iv_newstate() handles the stop scan case in common code. */
3794 return (lkpi_sta_state_do_nada(vap, nstate, arg));
3795 }
3796
3797 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
3798
3799 static int
lkpi_sta_auth_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3800 lkpi_sta_auth_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3801 {
3802 int error;
3803
3804 error = lkpi_sta_auth_to_scan(vap, nstate, arg);
3805 if (error == 0)
3806 error = lkpi_sta_scan_to_init(vap, nstate, arg);
3807 return (error);
3808 }
3809
3810 /* auth_to_auth, assoc_to_assoc. */
3811 static int
lkpi_sta_a_to_a(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3812 lkpi_sta_a_to_a(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3813 {
3814 struct lkpi_hw *lhw;
3815 struct ieee80211_hw *hw;
3816 struct lkpi_vif *lvif;
3817 struct ieee80211_vif *vif;
3818 struct lkpi_sta *lsta;
3819 struct ieee80211_prep_tx_info prep_tx_info;
3820 int error;
3821
3822 lhw = vap->iv_ic->ic_softc;
3823 hw = LHW_TO_HW(lhw);
3824 lvif = VAP_TO_LVIF(vap);
3825 vif = LVIF_TO_VIF(lvif);
3826
3827 IEEE80211_UNLOCK(vap->iv_ic);
3828 wiphy_lock(hw->wiphy);
3829
3830 LKPI_80211_LVIF_LOCK(lvif);
3831 /* XXX-BZ KASSERT later? */
3832 if (!lvif->lvif_bss_synched || lvif->lvif_bss == NULL) {
3833 #ifdef LINUXKPI_DEBUG_80211
3834 ic_printf(vap->iv_ic, "%s:%d: lvif %p vap %p iv_bss %p lvif_bss %p "
3835 "lvif_bss->ni %p synched %d\n", __func__, __LINE__,
3836 lvif, vap, vap->iv_bss, lvif->lvif_bss,
3837 (lvif->lvif_bss != NULL) ? lvif->lvif_bss->ni : NULL,
3838 lvif->lvif_bss_synched);
3839 #endif
3840 LKPI_80211_LVIF_UNLOCK(lvif);
3841 error = ENOTRECOVERABLE;
3842 goto out;
3843 }
3844 lsta = lvif->lvif_bss;
3845 LKPI_80211_LVIF_UNLOCK(lvif);
3846
3847 KASSERT(lsta != NULL, ("%s: lsta %p! lvif %p vap %p\n", __func__,
3848 lsta, lvif, vap));
3849
3850 IMPROVE("event callback?");
3851
3852 /* End mgd_complete_tx. */
3853 if (lsta->in_mgd) {
3854 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3855 if (vap->iv_state == IEEE80211_S_AUTH)
3856 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
3857 else
3858 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3859 prep_tx_info.link_id = 0;
3860 lkpi_80211_mo_mgd_complete_tx(hw, vif, &prep_tx_info);
3861 lsta->in_mgd = false;
3862 }
3863
3864 /* Now start auth/assoc. */
3865
3866 /* Start mgd_prepare_tx. */
3867 if (!lsta->in_mgd) {
3868 memset(&prep_tx_info, 0, sizeof(prep_tx_info));
3869 if (nstate == IEEE80211_S_AUTH)
3870 prep_tx_info.subtype = IEEE80211_STYPE_AUTH;
3871 else
3872 prep_tx_info.subtype = IEEE80211_STYPE_ASSOC_REQ;
3873 prep_tx_info.link_id = 0;
3874 lkpi_80211_mo_mgd_prepare_tx(hw, vif, &prep_tx_info);
3875 lsta->in_mgd = true;
3876 }
3877
3878 error = 0;
3879 out:
3880 wiphy_unlock(hw->wiphy);
3881 IEEE80211_LOCK(vap->iv_ic);
3882
3883 return (error);
3884 }
3885
3886 static int
lkpi_sta_assoc_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3887 lkpi_sta_assoc_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3888 {
3889 int error;
3890
3891 error = lkpi_sta_assoc_to_auth(vap, nstate, arg);
3892 if (error != 0 && error != EALREADY)
3893 return (error);
3894
3895 error = lkpi_sta_auth_to_scan(vap, nstate, arg);
3896 return (error);
3897 }
3898
3899 static int
lkpi_sta_assoc_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3900 lkpi_sta_assoc_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3901 {
3902 int error;
3903
3904 error = lkpi_sta_assoc_to_scan(vap, nstate, arg);
3905 if (error != 0 && error != EALREADY)
3906 return (error);
3907
3908 error = lkpi_sta_scan_to_init(vap, nstate, arg); /* do_nada */
3909 return (error);
3910 }
3911
3912 static int
lkpi_sta_run_to_init(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3913 lkpi_sta_run_to_init(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3914 {
3915 int error;
3916
3917 error = lkpi_sta_run_to_assoc(vap, nstate, arg);
3918 if (error != 0 && error != EALREADY)
3919 return (error);
3920
3921 error = lkpi_sta_assoc_to_init(vap, nstate, arg);
3922 return (error);
3923 }
3924
3925 static int
lkpi_sta_run_to_scan(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3926 lkpi_sta_run_to_scan(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3927 {
3928 int error;
3929
3930 error = lkpi_sta_run_to_assoc(vap, nstate, arg);
3931 if (error != 0 && error != EALREADY)
3932 return (error);
3933
3934 error = lkpi_sta_assoc_to_scan(vap, nstate, arg);
3935 return (error);
3936 }
3937
3938 static int
lkpi_sta_run_to_auth(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3939 lkpi_sta_run_to_auth(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3940 {
3941 int error;
3942
3943 error = lkpi_sta_run_to_assoc(vap, nstate, arg);
3944 if (error != 0 && error != EALREADY)
3945 return (error);
3946
3947 error = lkpi_sta_assoc_to_auth(vap, nstate, arg);
3948 return (error);
3949 }
3950
3951 /* -------------------------------------------------------------------------- */
3952
3953 /*
3954 * The matches the documented state changes in net80211::sta_newstate().
3955 * XXX (1) without CSA and SLEEP yet, * XXX (2) not all unhandled cases
3956 * there are "invalid" (so there is a room for failure here).
3957 */
3958 struct fsm_state {
3959 /* INIT, SCAN, AUTH, ASSOC, CAC, RUN, CSA, SLEEP */
3960 enum ieee80211_state ostate;
3961 enum ieee80211_state nstate;
3962 int (*handler)(struct ieee80211vap *, enum ieee80211_state, int);
3963 } sta_state_fsm[] = {
3964 { IEEE80211_S_INIT, IEEE80211_S_INIT, lkpi_sta_state_do_nada },
3965 { IEEE80211_S_SCAN, IEEE80211_S_INIT, lkpi_sta_state_do_nada }, /* DOWN4 scan_to_init */
3966 { IEEE80211_S_AUTH, IEEE80211_S_INIT, lkpi_sta_auth_to_init }, /* not explicitly in sta_newstate() */
3967 { IEEE80211_S_ASSOC, IEEE80211_S_INIT, lkpi_sta_assoc_to_init }, /* Send DEAUTH. */
3968 { IEEE80211_S_RUN, IEEE80211_S_INIT, lkpi_sta_run_to_init }, /* Send DISASSOC. */
3969
3970 { IEEE80211_S_INIT, IEEE80211_S_SCAN, lkpi_sta_init_to_scan }, /* UP1 */
3971 { IEEE80211_S_SCAN, IEEE80211_S_SCAN, lkpi_sta_state_do_nada },
3972 { IEEE80211_S_AUTH, IEEE80211_S_SCAN, lkpi_sta_auth_to_scan }, /* DOWN3 */
3973 { IEEE80211_S_ASSOC, IEEE80211_S_SCAN, lkpi_sta_assoc_to_scan },
3974 { IEEE80211_S_RUN, IEEE80211_S_SCAN, lkpi_sta_run_to_scan }, /* Beacon miss. */
3975
3976 { IEEE80211_S_INIT, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* Send AUTH. */
3977 { IEEE80211_S_SCAN, IEEE80211_S_AUTH, lkpi_sta_scan_to_auth }, /* UP2 Send AUTH. */
3978 { IEEE80211_S_AUTH, IEEE80211_S_AUTH, lkpi_sta_a_to_a }, /* Send ?AUTH. */
3979 { IEEE80211_S_ASSOC, IEEE80211_S_AUTH, lkpi_sta_assoc_to_auth }, /* DOWN2 Send ?AUTH. */
3980 { IEEE80211_S_RUN, IEEE80211_S_AUTH, lkpi_sta_run_to_auth }, /* Send ?AUTH. */
3981
3982 { IEEE80211_S_AUTH, IEEE80211_S_ASSOC, lkpi_sta_auth_to_assoc }, /* UP3.1 Send ASSOCREQ. */
3983 { IEEE80211_S_ASSOC, IEEE80211_S_ASSOC, lkpi_sta_a_to_a }, /* Send ASSOCREQ. */
3984 { IEEE80211_S_RUN, IEEE80211_S_ASSOC, lkpi_sta_run_to_assoc }, /* DOWN1 Send ASSOCREQ/REASSOCREQ. */
3985
3986 { IEEE80211_S_AUTH, IEEE80211_S_RUN, lkpi_sta_auth_to_run }, /* UP3.2 */
3987 { IEEE80211_S_ASSOC, IEEE80211_S_RUN, lkpi_sta_assoc_to_run }, /* UP4 */
3988 { IEEE80211_S_RUN, IEEE80211_S_RUN, lkpi_sta_state_do_nada },
3989
3990 /* Dummy at the end without handler. */
3991 { IEEE80211_S_INIT, IEEE80211_S_INIT, NULL },
3992 };
3993
3994 static int
lkpi_iv_newstate(struct ieee80211vap * vap,enum ieee80211_state nstate,int arg)3995 lkpi_iv_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
3996 {
3997 struct ieee80211com *ic;
3998 struct lkpi_hw *lhw;
3999 struct lkpi_vif *lvif;
4000 struct ieee80211_vif *vif;
4001 struct fsm_state *s;
4002 enum ieee80211_state ostate;
4003 int error;
4004
4005 ic = vap->iv_ic;
4006 IEEE80211_LOCK_ASSERT(ic);
4007 ostate = vap->iv_state;
4008
4009 #ifdef LINUXKPI_DEBUG_80211
4010 if (linuxkpi_debug_80211 & D80211_TRACE)
4011 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x\n",
4012 __func__, __LINE__, vap, nstate, arg);
4013 #endif
4014
4015 if (vap->iv_opmode == IEEE80211_M_STA) {
4016
4017 lhw = ic->ic_softc;
4018 lvif = VAP_TO_LVIF(vap);
4019 vif = LVIF_TO_VIF(lvif);
4020
4021 /* No need to replicate this in most state handlers. */
4022 if (nstate > IEEE80211_S_SCAN)
4023 lkpi_stop_hw_scan(lhw, vif);
4024
4025 s = sta_state_fsm;
4026
4027 } else {
4028 ic_printf(vap->iv_ic, "%s: only station mode currently supported: "
4029 "vap %p iv_opmode %d\n", __func__, vap, vap->iv_opmode);
4030 return (ENOSYS);
4031 }
4032
4033 error = 0;
4034 for (; s->handler != NULL; s++) {
4035 if (ostate == s->ostate && nstate == s->nstate) {
4036 #ifdef LINUXKPI_DEBUG_80211
4037 if (linuxkpi_debug_80211 & D80211_TRACE)
4038 ic_printf(vap->iv_ic, "%s: new state %d (%s) ->"
4039 " %d (%s): arg %d.\n", __func__,
4040 ostate, ieee80211_state_name[ostate],
4041 nstate, ieee80211_state_name[nstate], arg);
4042 #endif
4043 error = s->handler(vap, nstate, arg);
4044 break;
4045 }
4046 }
4047 IEEE80211_LOCK_ASSERT(vap->iv_ic);
4048
4049 if (s->handler == NULL) {
4050 IMPROVE("turn this into a KASSERT\n");
4051 ic_printf(vap->iv_ic, "%s: unsupported state transition "
4052 "%d (%s) -> %d (%s)\n", __func__,
4053 ostate, ieee80211_state_name[ostate],
4054 nstate, ieee80211_state_name[nstate]);
4055 return (ENOSYS);
4056 }
4057
4058 if (error == EALREADY) {
4059 #ifdef LINUXKPI_DEBUG_80211
4060 if (linuxkpi_debug_80211 & D80211_TRACE)
4061 ic_printf(vap->iv_ic, "%s: state transition %d (%s) -> "
4062 "%d (%s): iv_newstate already handled: %d.\n",
4063 __func__, ostate, ieee80211_state_name[ostate],
4064 nstate, ieee80211_state_name[nstate], error);
4065 #endif
4066 return (0);
4067 }
4068
4069 if (error != 0) {
4070 ic_printf(vap->iv_ic, "%s: error %d during state transition "
4071 "%d (%s) -> %d (%s)\n", __func__, error,
4072 ostate, ieee80211_state_name[ostate],
4073 nstate, ieee80211_state_name[nstate]);
4074 return (error);
4075 }
4076
4077 #ifdef LINUXKPI_DEBUG_80211
4078 if (linuxkpi_debug_80211 & D80211_TRACE)
4079 ic_printf(vap->iv_ic, "%s:%d: vap %p nstate %#x arg %#x "
4080 "calling net80211 parent\n",
4081 __func__, __LINE__, vap, nstate, arg);
4082 #endif
4083
4084 return (lvif->iv_newstate(vap, nstate, arg));
4085 }
4086
4087 /* -------------------------------------------------------------------------- */
4088
4089 /*
4090 * We overload (*iv_update_bss) as otherwise we have cases in, e.g.,
4091 * net80211::ieee80211_sta_join1() where vap->iv_bss gets replaced by a
4092 * new node without us knowing and thus our ni/lsta are out of sync.
4093 */
4094 static struct ieee80211_node *
lkpi_iv_update_bss(struct ieee80211vap * vap,struct ieee80211_node * ni)4095 lkpi_iv_update_bss(struct ieee80211vap *vap, struct ieee80211_node *ni)
4096 {
4097 struct lkpi_vif *lvif;
4098 struct ieee80211_node *rni;
4099
4100 IEEE80211_LOCK_ASSERT(vap->iv_ic);
4101
4102 lvif = VAP_TO_LVIF(vap);
4103
4104 LKPI_80211_LVIF_LOCK(lvif);
4105 lvif->lvif_bss_synched = false;
4106 LKPI_80211_LVIF_UNLOCK(lvif);
4107
4108 rni = lvif->iv_update_bss(vap, ni);
4109 return (rni);
4110 }
4111
4112 #ifdef LKPI_80211_WME
4113 static int
lkpi_wme_update(struct lkpi_hw * lhw,struct ieee80211vap * vap,bool planned)4114 lkpi_wme_update(struct lkpi_hw *lhw, struct ieee80211vap *vap, bool planned)
4115 {
4116 struct ieee80211com *ic;
4117 struct ieee80211_hw *hw;
4118 struct lkpi_vif *lvif;
4119 struct ieee80211_vif *vif;
4120 struct chanAccParams chp;
4121 struct wmeParams wmeparr[WME_NUM_AC];
4122 struct ieee80211_tx_queue_params txqp;
4123 enum ieee80211_bss_changed bss_changed;
4124 int error;
4125 uint16_t ac;
4126
4127 hw = LHW_TO_HW(lhw);
4128 lockdep_assert_wiphy(hw->wiphy);
4129
4130 IMPROVE();
4131 KASSERT(WME_NUM_AC == IEEE80211_NUM_ACS, ("%s: WME_NUM_AC %d != "
4132 "IEEE80211_NUM_ACS %d\n", __func__, WME_NUM_AC, IEEE80211_NUM_ACS));
4133
4134 if (vap == NULL)
4135 return (0);
4136
4137 if ((vap->iv_flags & IEEE80211_F_WME) == 0)
4138 return (0);
4139
4140 if (lhw->ops->conf_tx == NULL)
4141 return (0);
4142
4143 if (!planned && (vap->iv_state != IEEE80211_S_RUN)) {
4144 lhw->update_wme = true;
4145 return (0);
4146 }
4147 lhw->update_wme = false;
4148
4149 ic = lhw->ic;
4150 ieee80211_wme_ic_getparams(ic, &chp);
4151 IEEE80211_LOCK(ic);
4152 for (ac = 0; ac < WME_NUM_AC; ac++)
4153 wmeparr[ac] = chp.cap_wmeParams[ac];
4154 IEEE80211_UNLOCK(ic);
4155
4156 lvif = VAP_TO_LVIF(vap);
4157 vif = LVIF_TO_VIF(lvif);
4158
4159 /* Configure tx queues (conf_tx) & send BSS_CHANGED_QOS. */
4160 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4161 struct wmeParams *wmep;
4162
4163 wmep = &wmeparr[ac];
4164 bzero(&txqp, sizeof(txqp));
4165 txqp.cw_min = wmep->wmep_logcwmin;
4166 txqp.cw_max = wmep->wmep_logcwmax;
4167 txqp.txop = wmep->wmep_txopLimit;
4168 txqp.aifs = wmep->wmep_aifsn;
4169 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
4170 if (error != 0)
4171 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
4172 __func__, ac, error);
4173 }
4174 bss_changed = BSS_CHANGED_QOS;
4175 if (!planned)
4176 lkpi_bss_info_change(hw, vif, bss_changed);
4177
4178 return (bss_changed);
4179 }
4180 #endif
4181
4182 static int
lkpi_ic_wme_update(struct ieee80211com * ic)4183 lkpi_ic_wme_update(struct ieee80211com *ic)
4184 {
4185 #ifdef LKPI_80211_WME
4186 struct ieee80211vap *vap;
4187 struct lkpi_hw *lhw;
4188 struct ieee80211_hw *hw;
4189
4190 IMPROVE("Use the per-VAP callback in net80211.");
4191 vap = TAILQ_FIRST(&ic->ic_vaps);
4192 if (vap == NULL)
4193 return (0);
4194
4195 lhw = ic->ic_softc;
4196 hw = LHW_TO_HW(lhw);
4197
4198 wiphy_lock(hw->wiphy);
4199 lkpi_wme_update(lhw, vap, false);
4200 wiphy_unlock(hw->wiphy);
4201 #endif
4202 return (0); /* unused */
4203 }
4204
4205 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)4206 lkpi_iv_sta_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
4207 int subtype, const struct ieee80211_rx_stats *rxs, int rssi, int nf)
4208 {
4209 struct lkpi_hw *lhw;
4210 struct ieee80211_hw *hw;
4211 struct lkpi_vif *lvif;
4212 struct ieee80211_vif *vif;
4213 enum ieee80211_bss_changed bss_changed;
4214
4215 lvif = VAP_TO_LVIF(ni->ni_vap);
4216 vif = LVIF_TO_VIF(lvif);
4217
4218 lvif->iv_recv_mgmt(ni, m0, subtype, rxs, rssi, nf);
4219
4220 switch (subtype) {
4221 case IEEE80211_FC0_SUBTYPE_PROBE_RESP:
4222 break;
4223 case IEEE80211_FC0_SUBTYPE_BEACON:
4224 /*
4225 * Only count beacons when assoc. SCAN has its own logging.
4226 * This is for connection/beacon loss/session protection almost
4227 * over debugging when trying to get into a stable RUN state.
4228 */
4229 if (vif->cfg.assoc)
4230 lvif->beacons++;
4231 break;
4232 default:
4233 return;
4234 }
4235
4236 lhw = ni->ni_ic->ic_softc;
4237 hw = LHW_TO_HW(lhw);
4238
4239 /*
4240 * If this direct call to mo_bss_info_changed will not work due to
4241 * locking, see if queue_work() is fast enough.
4242 */
4243 wiphy_lock(hw->wiphy);
4244 bss_changed = lkpi_update_dtim_tsf(vif, ni, ni->ni_vap, __func__, __LINE__);
4245 lkpi_bss_info_change(hw, vif, bss_changed);
4246 wiphy_unlock(hw->wiphy);
4247 }
4248
4249 /*
4250 * Change link-layer address on the vif (if the vap is not started/"UP").
4251 * This can happen if a user changes 'ether' using ifconfig.
4252 * The code is based on net80211/ieee80211_freebsd.c::wlan_iflladdr() but
4253 * we do use a per-[l]vif event handler to be sure we exist as we
4254 * cannot assume that from every vap derives a vif and we have a hard
4255 * time checking based on net80211 information.
4256 * Should this ever become a real problem we could add a callback function
4257 * to wlan_iflladdr() to be set optionally but that would be for a
4258 * single-consumer (or needs a list) -- was just too complicated for an
4259 * otherwise perfect mechanism FreeBSD already provides.
4260 */
4261 static void
lkpi_vif_iflladdr(void * arg,struct ifnet * ifp)4262 lkpi_vif_iflladdr(void *arg, struct ifnet *ifp)
4263 {
4264 struct epoch_tracker et;
4265 struct ieee80211_vif *vif;
4266
4267 NET_EPOCH_ENTER(et);
4268 /* NB: identify vap's by if_transmit; left as an extra check. */
4269 if (if_gettransmitfn(ifp) != ieee80211_vap_transmit ||
4270 (if_getflags(ifp) & IFF_UP) != 0) {
4271 NET_EPOCH_EXIT(et);
4272 return;
4273 }
4274
4275 vif = arg;
4276 IEEE80211_ADDR_COPY(vif->bss_conf.addr, if_getlladdr(ifp));
4277 NET_EPOCH_EXIT(et);
4278 }
4279
4280 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])4281 lkpi_ic_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ],
4282 int unit, enum ieee80211_opmode opmode, int flags,
4283 const uint8_t bssid[IEEE80211_ADDR_LEN],
4284 const uint8_t mac[IEEE80211_ADDR_LEN])
4285 {
4286 struct lkpi_hw *lhw;
4287 struct ieee80211_hw *hw;
4288 struct lkpi_vif *lvif;
4289 struct ieee80211vap *vap;
4290 struct ieee80211_vif *vif;
4291 struct ieee80211_tx_queue_params txqp;
4292 enum ieee80211_bss_changed bss_changed;
4293 enum nl80211_band band;
4294 struct sysctl_oid *node;
4295 size_t len;
4296 int error, i;
4297 uint16_t ac;
4298
4299 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* 1 so far. Add <n> once this works. */
4300 return (NULL);
4301
4302 lhw = ic->ic_softc;
4303 hw = LHW_TO_HW(lhw);
4304
4305 len = sizeof(*lvif);
4306 len += hw->vif_data_size; /* vif->drv_priv */
4307
4308 lvif = malloc(len, M_80211_VAP, M_WAITOK | M_ZERO);
4309 mtx_init(&lvif->mtx, "lvif", NULL, MTX_DEF);
4310 TASK_INIT(&lvif->sw_scan_task, 0, lkpi_sw_scan_task, lvif);
4311 INIT_LIST_HEAD(&lvif->lsta_list);
4312 lvif->lvif_bss = NULL;
4313 refcount_init(&lvif->nt_unlocked, 0);
4314 lvif->lvif_bss_synched = false;
4315 vap = LVIF_TO_VAP(lvif);
4316 vif = LVIF_TO_VIF(lvif);
4317
4318 /*
4319 * Setup legacy br_mask here. We will call (*set_bitrate_mask)
4320 * elsewhere to announce it to the driver but it is a static
4321 * setup.
4322 * Also setup basic_rates with just the mandatory rates for the
4323 * current band (if avail).
4324 */
4325 for (band = 0; band < NUM_NL80211_BANDS; band++) {
4326 struct ieee80211_supported_band *supband;
4327 uint32_t rate_mandatory;;
4328
4329 supband = hw->wiphy->bands[band];
4330 if (supband == NULL || supband->n_bitrates == 0)
4331 continue;
4332
4333 /* Per-band legacy br_mask. */
4334 lvif->br_mask.control[band].legacy = (1 << supband->n_bitrates) - 1;
4335
4336 /* basic_rates for the current band. */
4337 if (hw->conf.chandef.chan == NULL ||
4338 hw->conf.chandef.chan->band != band)
4339 continue;
4340
4341 switch (band) {
4342 case NL80211_BAND_2GHZ:
4343 /* We have to assume 11g support here. */
4344 rate_mandatory = IEEE80211_RATE_MANDATORY_G |
4345 IEEE80211_RATE_MANDATORY_B;
4346 break;
4347 case NL80211_BAND_5GHZ:
4348 rate_mandatory = IEEE80211_RATE_MANDATORY_A;
4349 break;
4350 default:
4351 continue;
4352 }
4353
4354 for (i = 0; i < supband->n_bitrates; i++) {
4355 if ((supband->bitrates[i].flags & rate_mandatory) != 0)
4356 vif->bss_conf.basic_rates |= BIT(i);
4357 }
4358 }
4359
4360 memcpy(vif->addr, mac, IEEE80211_ADDR_LEN);
4361 vif->p2p = false;
4362 vif->probe_req_reg = false;
4363 vif->type = lkpi_opmode_to_vif_type(opmode);
4364 lvif->wdev.wiphy = hw->wiphy;
4365 lvif->wdev.iftype = vif->type;
4366 wiphy_lock(hw->wiphy);
4367 list_add_rcu(&lvif->wdev.list, &hw->wiphy->wdev_list);
4368 wiphy_unlock(hw->wiphy);
4369 memcpy(lvif->wdev.address, mac, IEEE80211_ADDR_LEN);
4370 #if 0
4371 /* Need to fill in other fields as well. */
4372 struct net_device *netdev; /* When do we create this? */
4373 uint32_t radio_mask;
4374 #endif
4375 IMPROVE("wdev");
4376
4377 /* Create a chanctx to be used later. */
4378 IMPROVE("lkpi_alloc_lchanctx reserved as many as can be");
4379 (void) lkpi_find_lchanctx_reserved(hw, lvif);
4380
4381 /* XXX-BZ hardcoded for now! */
4382 #if 1
4383 RCU_INIT_POINTER(vif->bss_conf.chanctx_conf, NULL);
4384 vif->bss_conf.vif = vif;
4385 /* vap->iv_myaddr is not set until net80211::vap_setup or vap_attach. */
4386 IEEE80211_ADDR_COPY(vif->bss_conf.addr, mac);
4387 lvif->lvif_ifllevent = EVENTHANDLER_REGISTER(iflladdr_event,
4388 lkpi_vif_iflladdr, vif, EVENTHANDLER_PRI_ANY);
4389 vif->bss_conf.link_id = 0; /* Non-MLO operation. */
4390 vif->bss_conf.chanreq.oper.chan = lhw->dflt_chandef.chan;
4391 vif->bss_conf.chanreq.oper.width = NL80211_CHAN_WIDTH_20_NOHT;
4392 vif->bss_conf.use_short_preamble = false; /* vap->iv_flags IEEE80211_F_SHPREAMBLE */
4393 vif->bss_conf.use_short_slot = false; /* vap->iv_flags IEEE80211_F_SHSLOT */
4394 vif->bss_conf.qos = false;
4395 vif->bss_conf.use_cts_prot = false; /* vap->iv_protmode */
4396 vif->bss_conf.ht_operation_mode = IEEE80211_HT_OP_MODE_PROTECTION_NONE;
4397 IEEE80211_ADDR_COPY(vif->cfg.ap_addr, ieee80211broadcastaddr);
4398 vif->cfg.aid = 0;
4399 vif->cfg.assoc = false;
4400 vif->cfg.idle = true;
4401 vif->cfg.ps = false;
4402 IMPROVE("Check other fields and then figure out whats is left elsewhere of them");
4403 /*
4404 * We need to initialize it to something as the bss_info_changed call
4405 * will try to copy from it in iwlwifi and NULL is a panic.
4406 * We will set the proper one in scan_to_auth() before being assoc.
4407 */
4408 vif->bss_conf.bssid = ieee80211broadcastaddr;
4409 #endif
4410 #if 0
4411 vif->bss_conf.dtim_period = 0; /* IEEE80211_DTIM_DEFAULT ; must stay 0. */
4412 IEEE80211_ADDR_COPY(vif->bss_conf.bssid, bssid);
4413 vif->bss_conf.beacon_int = ic->ic_bintval;
4414 /* iwlwifi bug. */
4415 if (vif->bss_conf.beacon_int < 16)
4416 vif->bss_conf.beacon_int = 16;
4417 #endif
4418
4419 /* Link Config */
4420 vif->link_conf[0] = &vif->bss_conf;
4421 for (i = 0; i < nitems(vif->link_conf); i++) {
4422 IMPROVE("more than 1 link one day");
4423 }
4424
4425 /* Setup queue defaults; driver may override in (*add_interface). */
4426 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
4427 if (ieee80211_hw_check(hw, QUEUE_CONTROL))
4428 vif->hw_queue[i] = IEEE80211_INVAL_HW_QUEUE;
4429 else if (hw->queues >= IEEE80211_NUM_ACS)
4430 vif->hw_queue[i] = i;
4431 else
4432 vif->hw_queue[i] = 0;
4433
4434 /* Initialize the queue to running. Stopped? */
4435 lvif->hw_queue_stopped[i] = false;
4436 }
4437 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
4438
4439 IMPROVE();
4440
4441 wiphy_lock(hw->wiphy);
4442 error = lkpi_80211_mo_start(hw);
4443 if (error != 0) {
4444 wiphy_unlock(hw->wiphy);
4445 ic_printf(ic, "%s: failed to start hw: %d\n", __func__, error);
4446 mtx_destroy(&lvif->mtx);
4447 free(lvif, M_80211_VAP);
4448 return (NULL);
4449 }
4450
4451 error = lkpi_80211_mo_add_interface(hw, vif);
4452 if (error != 0) {
4453 IMPROVE(); /* XXX-BZ mo_stop()? */
4454 wiphy_unlock(hw->wiphy);
4455 ic_printf(ic, "%s: failed to add interface: %d\n", __func__, error);
4456 mtx_destroy(&lvif->mtx);
4457 free(lvif, M_80211_VAP);
4458 return (NULL);
4459 }
4460 wiphy_unlock(hw->wiphy);
4461
4462 LKPI_80211_LHW_LVIF_LOCK(lhw);
4463 TAILQ_INSERT_TAIL(&lhw->lvif_head, lvif, lvif_entry);
4464 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4465
4466 wiphy_lock(hw->wiphy);
4467
4468 /* Set bss_info. */
4469 bss_changed = 0;
4470 lkpi_bss_info_change(hw, vif, bss_changed);
4471
4472 /* Configure tx queues (conf_tx), default WME & send BSS_CHANGED_QOS. */
4473 IMPROVE("Hardcoded values; to fix see 802.11-2016, 9.4.2.29 EDCA Parameter Set element");
4474 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
4475
4476 bzero(&txqp, sizeof(txqp));
4477 txqp.cw_min = 15;
4478 txqp.cw_max = 1023;
4479 txqp.txop = 0;
4480 txqp.aifs = 2;
4481 error = lkpi_80211_mo_conf_tx(hw, vif, /* link_id */0, ac, &txqp);
4482 if (error != 0)
4483 ic_printf(ic, "%s: conf_tx ac %u failed %d\n",
4484 __func__, ac, error);
4485 }
4486 bss_changed = BSS_CHANGED_QOS;
4487 lkpi_bss_info_change(hw, vif, bss_changed);
4488
4489 /* Force MC init. */
4490 lkpi_update_mcast_filter_locked(ic);
4491
4492 wiphy_unlock(hw->wiphy);
4493
4494 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid);
4495
4496 /* Now we have a valid vap->iv_ifp. Any checksum offloading goes below. */
4497
4498 IMPROVE();
4499
4500 /* Override with LinuxKPI method so we can drive mac80211/cfg80211. */
4501 lvif->iv_newstate = vap->iv_newstate;
4502 vap->iv_newstate = lkpi_iv_newstate;
4503 lvif->iv_update_bss = vap->iv_update_bss;
4504 vap->iv_update_bss = lkpi_iv_update_bss;
4505 lvif->iv_recv_mgmt = vap->iv_recv_mgmt;
4506 vap->iv_recv_mgmt = lkpi_iv_sta_recv_mgmt;
4507
4508 #ifdef LKPI_80211_HW_CRYPTO
4509 /* Key management. */
4510 if (lkpi_hwcrypto && lhw->ops->set_key != NULL) {
4511 vap->iv_key_set = lkpi_iv_key_set;
4512 vap->iv_key_delete = lkpi_iv_key_delete;
4513 vap->iv_key_update_begin = lkpi_iv_key_update_begin;
4514 vap->iv_key_update_end = lkpi_iv_key_update_end;
4515 }
4516 #endif
4517
4518 #ifdef LKPI_80211_HT
4519 /* Stay with the iv_ampdu_rxmax,limit / iv_ampdu_density defaults until later. */
4520 #endif
4521
4522 ieee80211_ratectl_init(vap);
4523
4524 /* Complete setup. */
4525 ieee80211_vap_attach(vap, ieee80211_media_change,
4526 ieee80211_media_status, mac);
4527
4528 #ifdef LKPI_80211_HT
4529 /*
4530 * Modern chipset/fw/drv will do A-MPDU in drv/fw and fail
4531 * to do so if they cannot do the crypto too.
4532 */
4533 if (!lkpi_hwcrypto && IEEE80211_CONF_AMPDU_OFFLOAD(ic))
4534 vap->iv_flags_ht &= ~IEEE80211_FHT_AMPDU_RX;
4535 #endif
4536
4537 if (hw->max_listen_interval == 0)
4538 hw->max_listen_interval = 7 * (ic->ic_lintval / ic->ic_bintval);
4539 hw->conf.listen_interval = hw->max_listen_interval;
4540
4541 wiphy_lock(hw->wiphy);
4542 /* XXX-BZ do we need to be able to update these? */
4543 hw->wiphy->frag_threshold = vap->iv_fragthreshold;
4544 lkpi_80211_mo_set_frag_threshold(hw, vap->iv_fragthreshold);
4545 hw->wiphy->rts_threshold = vap->iv_rtsthreshold;
4546 lkpi_80211_mo_set_rts_threshold(hw, vap->iv_rtsthreshold);
4547 wiphy_unlock(hw->wiphy);
4548 /* any others? */
4549
4550 /* Add per-VIF/VAP sysctls. */
4551 sysctl_ctx_init(&lvif->sysctl_ctx);
4552
4553 node = SYSCTL_ADD_NODE(&lvif->sysctl_ctx,
4554 SYSCTL_CHILDREN(&sysctl___compat_linuxkpi_80211),
4555 OID_AUTO, if_name(vap->iv_ifp),
4556 CTLFLAG_RD | CTLFLAG_SKIP | CTLFLAG_MPSAFE, NULL, "VIF Information");
4557
4558 SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
4559 SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas",
4560 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE, lvif, 0,
4561 lkpi_80211_dump_stas, "A", "Dump sta statistics of this vif");
4562 SYSCTL_ADD_PROC(&lvif->sysctl_ctx,
4563 SYSCTL_CHILDREN(node), OID_AUTO, "dump_stas_queues",
4564 CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE | CTLFLAG_SKIP, lvif, 0,
4565 lkpi_80211_dump_sta_queues, "A",
4566 "Dump queue statistics for any sta of this vif");
4567
4568 IMPROVE();
4569
4570 return (vap);
4571 }
4572
4573 void
linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw * hw)4574 linuxkpi_ieee80211_unregister_hw(struct ieee80211_hw *hw)
4575 {
4576
4577 wiphy_unregister(hw->wiphy);
4578 linuxkpi_ieee80211_ifdetach(hw);
4579
4580 IMPROVE();
4581 }
4582
4583 void
linuxkpi_ieee80211_restart_hw(struct ieee80211_hw * hw)4584 linuxkpi_ieee80211_restart_hw(struct ieee80211_hw *hw)
4585 {
4586
4587 TODO();
4588 }
4589
4590 static void
lkpi_ic_vap_delete(struct ieee80211vap * vap)4591 lkpi_ic_vap_delete(struct ieee80211vap *vap)
4592 {
4593 struct ieee80211com *ic;
4594 struct lkpi_hw *lhw;
4595 struct ieee80211_hw *hw;
4596 struct lkpi_vif *lvif;
4597 struct ieee80211_vif *vif;
4598
4599 lvif = VAP_TO_LVIF(vap);
4600 vif = LVIF_TO_VIF(lvif);
4601 ic = vap->iv_ic;
4602 lhw = ic->ic_softc;
4603 hw = LHW_TO_HW(lhw);
4604
4605 EVENTHANDLER_DEREGISTER(iflladdr_event, lvif->lvif_ifllevent);
4606
4607 /* Clear up per-VIF/VAP sysctls. */
4608 sysctl_ctx_free(&lvif->sysctl_ctx);
4609
4610 ieee80211_draintask(ic, &lvif->sw_scan_task);
4611
4612 LKPI_80211_LHW_LVIF_LOCK(lhw);
4613 TAILQ_REMOVE(&lhw->lvif_head, lvif, lvif_entry);
4614 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
4615
4616 wiphy_lock(hw->wiphy);
4617 list_del_rcu(&lvif->wdev.list);
4618 wiphy_unlock(hw->wiphy);
4619
4620 ieee80211_ratectl_deinit(vap);
4621 ieee80211_vap_detach(vap);
4622
4623 IMPROVE("clear up other bits in this state");
4624
4625 wiphy_lock(hw->wiphy);
4626 lkpi_80211_mo_remove_interface(hw, vif);
4627
4628 /* Single VAP, so we can do this here. */
4629 lkpi_80211_mo_stop(hw, false); /* XXX SUSPEND */
4630 wiphy_unlock(hw->wiphy);
4631
4632 mtx_destroy(&lvif->mtx);
4633 free(lvif, M_80211_VAP);
4634 }
4635
4636 static void
lkpi_ic_update_mcast(struct ieee80211com * ic)4637 lkpi_ic_update_mcast(struct ieee80211com *ic)
4638 {
4639 struct ieee80211vap *vap;
4640 struct lkpi_hw *lhw;
4641
4642 lhw = ic->ic_softc;
4643
4644 LKPI_80211_LHW_MC_LOCK(lhw);
4645 /* Cleanup anything on the current list. */
4646 lkpi_cleanup_mcast_list_locked(lhw);
4647
4648 /* Build up the new list (or allmulti). */
4649 if (ic->ic_allmulti == 0) {
4650 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
4651 if_foreach_llmaddr(vap->iv_ifp,
4652 lkpi_ic_update_mcast_copy, &lhw->mc_list);
4653 lhw->mc_all_multi = false;
4654 } else {
4655 lhw->mc_all_multi = true;
4656 }
4657 LKPI_80211_LHW_MC_UNLOCK(lhw);
4658
4659 lkpi_update_mcast_filter(ic);
4660 TRACEOK();
4661 }
4662
4663 static void
lkpi_ic_update_promisc(struct ieee80211com * ic)4664 lkpi_ic_update_promisc(struct ieee80211com *ic)
4665 {
4666
4667 UNIMPLEMENTED;
4668 }
4669
4670 static void
lkpi_ic_update_chw(struct ieee80211com * ic)4671 lkpi_ic_update_chw(struct ieee80211com *ic)
4672 {
4673
4674 UNIMPLEMENTED;
4675 }
4676
4677 /* Start / stop device. */
4678 static void
lkpi_ic_parent(struct ieee80211com * ic)4679 lkpi_ic_parent(struct ieee80211com *ic)
4680 {
4681 struct lkpi_hw *lhw;
4682 struct ieee80211_hw *hw;
4683 #ifdef HW_START_STOP
4684 int error;
4685 #endif
4686 bool start_all;
4687
4688 IMPROVE();
4689
4690 lhw = ic->ic_softc;
4691 hw = LHW_TO_HW(lhw);
4692 start_all = false;
4693
4694 /* IEEE80211_UNLOCK(ic); */
4695 wiphy_lock(hw->wiphy);
4696 if (ic->ic_nrunning > 0) {
4697 #ifdef HW_START_STOP
4698 error = lkpi_80211_mo_start(hw);
4699 if (error == 0)
4700 #endif
4701 start_all = true;
4702 } else {
4703 #ifdef HW_START_STOP
4704 lkpi_80211_mo_stop(hw, false); /* XXX SUSPEND */
4705 #endif
4706 }
4707 wiphy_unlock(hw->wiphy);
4708 /* IEEE80211_LOCK(ic); */
4709
4710 if (start_all)
4711 ieee80211_start_all(ic);
4712 }
4713
4714 bool
linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie,const u8 * ie_ids,size_t ie_ids_len)4715 linuxkpi_ieee80211_is_ie_id_in_ie_buf(const u8 ie, const u8 *ie_ids,
4716 size_t ie_ids_len)
4717 {
4718 int i;
4719
4720 for (i = 0; i < ie_ids_len; i++) {
4721 if (ie == *ie_ids)
4722 return (true);
4723 }
4724
4725 return (false);
4726 }
4727
4728 /* Return true if skipped; false if error. */
4729 bool
linuxkpi_ieee80211_ie_advance(size_t * xp,const u8 * ies,size_t ies_len)4730 linuxkpi_ieee80211_ie_advance(size_t *xp, const u8 *ies, size_t ies_len)
4731 {
4732 size_t x;
4733 uint8_t l;
4734
4735 x = *xp;
4736
4737 KASSERT(x < ies_len, ("%s: x %zu ies_len %zu ies %p\n",
4738 __func__, x, ies_len, ies));
4739 l = ies[x + 1];
4740 x += 2 + l;
4741
4742 if (x > ies_len)
4743 return (false);
4744
4745 *xp = x;
4746 return (true);
4747 }
4748
4749 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)4750 lkpi_scan_ies_add(uint8_t *p, struct ieee80211_scan_ies *scan_ies,
4751 uint32_t band_mask, struct ieee80211vap *vap, struct ieee80211_hw *hw)
4752 {
4753 struct ieee80211_supported_band *supband;
4754 struct linuxkpi_ieee80211_channel *channels;
4755 struct ieee80211com *ic;
4756 const struct ieee80211_channel *chan;
4757 const struct ieee80211_rateset *rs;
4758 uint8_t *pb;
4759 int band, i;
4760
4761 ic = vap->iv_ic;
4762 for (band = 0; band < NUM_NL80211_BANDS; band++) {
4763 if ((band_mask & (1 << band)) == 0)
4764 continue;
4765
4766 supband = hw->wiphy->bands[band];
4767 /*
4768 * This should not happen;
4769 * band_mask is a bitmask of valid bands to scan on.
4770 */
4771 if (supband == NULL || supband->n_channels == 0)
4772 continue;
4773
4774 /* Find a first channel to get the mode and rates from. */
4775 channels = supband->channels;
4776 chan = NULL;
4777 for (i = 0; i < supband->n_channels; i++) {
4778 uint32_t flags;
4779
4780 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
4781 continue;
4782
4783 flags = 0;
4784 switch (band) {
4785 case NL80211_BAND_2GHZ:
4786 flags |= IEEE80211_CHAN_G;
4787 break;
4788 case NL80211_BAND_5GHZ:
4789 flags |= IEEE80211_CHAN_A;
4790 break;
4791 default:
4792 panic("%s:%d: unupported band %d\n",
4793 __func__, __LINE__, band);
4794 }
4795
4796 chan = ieee80211_find_channel(ic,
4797 channels[i].center_freq, flags);
4798 if (chan != NULL)
4799 break;
4800 }
4801
4802 /* This really should not happen. */
4803 if (chan == NULL)
4804 continue;
4805
4806 pb = p;
4807 rs = ieee80211_get_suprates(ic, chan); /* calls chan2mode */
4808 p = ieee80211_add_rates(p, rs);
4809 p = ieee80211_add_xrates(p, rs);
4810
4811 #if defined(LKPI_80211_HT)
4812 if ((vap->iv_flags_ht & IEEE80211_FHT_HT) != 0) {
4813 struct ieee80211_channel *c;
4814
4815 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4816 vap->iv_flags_ht);
4817 p = ieee80211_add_htcap_ch(p, vap, c);
4818 }
4819 #endif
4820 #if defined(LKPI_80211_VHT)
4821 if (band == NL80211_BAND_5GHZ &&
4822 (vap->iv_vht_flags & IEEE80211_FVHT_VHT) != 0) {
4823 struct ieee80211_channel *c;
4824
4825 c = ieee80211_ht_adjust_channel(ic, ic->ic_curchan,
4826 vap->iv_flags_ht);
4827 c = ieee80211_vht_adjust_channel(ic, c,
4828 vap->iv_vht_flags);
4829 p = ieee80211_add_vhtcap_ch(p, vap, c);
4830 }
4831 #endif
4832
4833 scan_ies->ies[band] = pb;
4834 scan_ies->len[band] = p - pb;
4835 }
4836
4837 /* Add common_ies */
4838 pb = p;
4839 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
4840 vap->iv_wpa_ie != NULL) {
4841 memcpy(p, vap->iv_wpa_ie, 2 + vap->iv_wpa_ie[1]);
4842 p += 2 + vap->iv_wpa_ie[1];
4843 }
4844 if (vap->iv_appie_probereq != NULL) {
4845 memcpy(p, vap->iv_appie_probereq->ie_data,
4846 vap->iv_appie_probereq->ie_len);
4847 p += vap->iv_appie_probereq->ie_len;
4848 }
4849 scan_ies->common_ies = pb;
4850 scan_ies->common_ie_len = p - pb;
4851
4852 return (p);
4853 }
4854
4855 static void
lkpi_enable_hw_scan(struct lkpi_hw * lhw)4856 lkpi_enable_hw_scan(struct lkpi_hw *lhw)
4857 {
4858
4859 if (lhw->ops->hw_scan) {
4860 /*
4861 * Advertise full-offload scanning.
4862 *
4863 * Not limiting to SINGLE_SCAN_ON_ALL_BANDS here as otherwise
4864 * we essentially disable hw_scan for all drivers not setting
4865 * the flag.
4866 */
4867 lhw->ic->ic_flags_ext |= IEEE80211_FEXT_SCAN_OFFLOAD;
4868 lhw->scan_flags |= LKPI_LHW_SCAN_HW;
4869 }
4870 }
4871
4872 #ifndef LKPI_80211_USE_SCANLIST
4873 static const uint32_t chan_pri[] = {
4874 5180, 5500, 5745,
4875 5260, 5580, 5660, 5825,
4876 5220, 5300, 5540, 5620, 5700, 5785, 5865,
4877 2437, 2412, 2422, 2462, 2472, 2432, 2452
4878 };
4879
4880 static int
lkpi_scan_chan_list_idx(const struct linuxkpi_ieee80211_channel * lc)4881 lkpi_scan_chan_list_idx(const struct linuxkpi_ieee80211_channel *lc)
4882 {
4883 int i;
4884
4885 for (i = 0; i < nitems(chan_pri); i++) {
4886 if (lc->center_freq == chan_pri[i])
4887 return (i);
4888 }
4889
4890 return (-1);
4891 }
4892
4893 static int
lkpi_scan_chan_list_comp(const struct linuxkpi_ieee80211_channel * lc1,const struct linuxkpi_ieee80211_channel * lc2)4894 lkpi_scan_chan_list_comp(const struct linuxkpi_ieee80211_channel *lc1,
4895 const struct linuxkpi_ieee80211_channel *lc2)
4896 {
4897 int idx1, idx2;
4898
4899 /* Find index in list. */
4900 idx1 = lkpi_scan_chan_list_idx(lc1);
4901 idx2 = lkpi_scan_chan_list_idx(lc2);
4902
4903 if (idx1 == -1 && idx2 != -1)
4904 return (1);
4905 if (idx1 != -1 && idx2 == -1)
4906 return (-1);
4907
4908 /* Neither on the list, use center_freq. */
4909 if (idx1 == -1 && idx2 == -1)
4910 return (lc1->center_freq - lc2->center_freq);
4911
4912 /* Whichever is first in the list. */
4913 return (idx1 - idx2);
4914 }
4915
4916 static void
lkpi_scan_chan_list_resort(struct linuxkpi_ieee80211_channel ** cpp,size_t nchan)4917 lkpi_scan_chan_list_resort(struct linuxkpi_ieee80211_channel **cpp, size_t nchan)
4918 {
4919 struct linuxkpi_ieee80211_channel *lc, *nc;
4920 size_t i, j;
4921 int rc;
4922
4923 for (i = (nchan - 1); i > 0; i--) {
4924 for (j = i; j > 0 ; j--) {
4925 lc = *(cpp + j);
4926 nc = *(cpp + j - 1);
4927 rc = lkpi_scan_chan_list_comp(lc, nc);
4928 if (rc < 0) {
4929 *(cpp + j) = nc;
4930 *(cpp + j - 1) = lc;
4931 }
4932 }
4933 }
4934 }
4935
4936 static bool
lkpi_scan_chan(struct linuxkpi_ieee80211_channel * c,struct ieee80211com * ic,bool log)4937 lkpi_scan_chan(struct linuxkpi_ieee80211_channel *c,
4938 struct ieee80211com *ic, bool log)
4939 {
4940
4941 if ((c->flags & IEEE80211_CHAN_DISABLED) != 0) {
4942 if (log)
4943 TRACE_SCAN(ic, "Skipping disabled chan "
4944 "on band %s [%#x/%u/%#x]",
4945 lkpi_nl80211_band_name(c->band), c->hw_value,
4946 c->center_freq, c->flags);
4947 return (false);
4948 }
4949 if (isclr(ic->ic_chan_active, ieee80211_mhz2ieee(c->center_freq,
4950 lkpi_nl80211_band_to_net80211_band(c->band)))) {
4951 if (log)
4952 TRACE_SCAN(ic, "Skipping !active chan "
4953 "on band %s [%#x/%u/%#x]",
4954 lkpi_nl80211_band_name(c->band), c->hw_value,
4955 c->center_freq, c->flags);
4956 return (false);
4957 }
4958 return (true);
4959 }
4960 #endif
4961
4962 static void
lkpi_ic_scan_start(struct ieee80211com * ic)4963 lkpi_ic_scan_start(struct ieee80211com *ic)
4964 {
4965 struct lkpi_hw *lhw;
4966 struct ieee80211_hw *hw;
4967 struct lkpi_vif *lvif;
4968 struct ieee80211_vif *vif;
4969 struct ieee80211_scan_state *ss;
4970 struct ieee80211vap *vap;
4971 int error;
4972 bool is_hw_scan;
4973
4974 lhw = ic->ic_softc;
4975 ss = ic->ic_scan;
4976 vap = ss->ss_vap;
4977 TRACE_SCAN(ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4978
4979 LKPI_80211_LHW_SCAN_LOCK(lhw);
4980 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
4981 /* A scan is still running. */
4982 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4983 TRACE_SCAN(ic, "Trying to start new scan while still running; "
4984 "cancelling new net80211 scan; scan_flags %b",
4985 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
4986 ieee80211_cancel_scan(vap);
4987 return;
4988 }
4989 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
4990 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
4991
4992 #if 0
4993 if (vap->iv_state != IEEE80211_S_SCAN) {
4994 TODO("We need to be able to scan if not in S_SCAN");
4995 TRACE_SCAN(ic, "scan_flags %b iv_state %d",
4996 lhw->scan_flags, LKPI_LHW_SCAN_BITS, vap->iv_state);
4997 ieee80211_cancel_scan(vap);
4998 return;
4999 }
5000 #endif
5001
5002 hw = LHW_TO_HW(lhw);
5003 if (!is_hw_scan) {
5004 /* If hw_scan is cleared clear FEXT_SCAN_OFFLOAD too. */
5005 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCAN_OFFLOAD;
5006
5007 lvif = VAP_TO_LVIF(vap);
5008 vif = LVIF_TO_VIF(lvif);
5009
5010 if (vap->iv_state == IEEE80211_S_SCAN) {
5011 wiphy_lock(hw->wiphy);
5012 lkpi_hw_conf_idle(hw, false);
5013 wiphy_unlock(hw->wiphy);
5014 }
5015
5016 LKPI_80211_LHW_SCAN_LOCK(lhw);
5017 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
5018 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5019
5020 lkpi_update_mcast_filter(ic);
5021
5022 TRACE_SCAN(vap->iv_ic, "Starting SW_SCAN: scan_flags %b",
5023 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
5024 lkpi_80211_mo_sw_scan_start(hw, vif, vif->addr);
5025 /* net80211::scan_start() handled PS for us. */
5026 IMPROVE();
5027 /* XXX Also means it is too late to flush queues?
5028 * need to check iv_sta_ps or overload? */
5029 /* XXX want to adjust ss end time/ maxdwell? */
5030
5031 } else {
5032 struct ieee80211_scan_request *hw_req;
5033 struct linuxkpi_ieee80211_channel *lc, **cpp;
5034 struct cfg80211_ssid *ssids;
5035 struct cfg80211_scan_6ghz_params *s6gp;
5036 size_t chan_len, nchan, ssids_len, s6ghzlen;
5037 int band, i, ssid_count, common_ie_len;
5038 #ifndef LKPI_80211_USE_SCANLIST
5039 int n;
5040 #endif
5041 uint32_t band_mask;
5042 uint8_t *ie, *ieend;
5043 bool running;
5044
5045 ssid_count = min(ss->ss_nssid, hw->wiphy->max_scan_ssids);
5046 ssids_len = ssid_count * sizeof(*ssids);
5047 s6ghzlen = 0 * (sizeof(*s6gp)); /* XXX-BZ */
5048
5049 band_mask = 0;
5050 nchan = 0;
5051 if (ieee80211_hw_check(hw, SINGLE_SCAN_ON_ALL_BANDS)) {
5052 #ifdef LKPI_80211_USE_SCANLIST
5053 /* Avoid net80211 scan lists until it has proper scan offload support. */
5054 for (i = ss->ss_next; i < ss->ss_last; i++) {
5055 nchan++;
5056 band = lkpi_net80211_chan_to_nl80211_band(
5057 ss->ss_chans[ss->ss_next + i]);
5058 band_mask |= (1 << band);
5059 }
5060 #else
5061 /* Instead we scan for all channels all the time. */
5062 for (band = 0; band < NUM_NL80211_BANDS; band++) {
5063 switch (band) {
5064 case NL80211_BAND_2GHZ:
5065 case NL80211_BAND_5GHZ:
5066 break;
5067 default:
5068 continue;
5069 }
5070 if (hw->wiphy->bands[band] != NULL) {
5071 struct linuxkpi_ieee80211_channel *channels;
5072 int n;
5073
5074 band_mask |= (1 << band);
5075
5076 channels = hw->wiphy->bands[band]->channels;
5077 n = hw->wiphy->bands[band]->n_channels;
5078 for (i = 0; i < n; i++) {
5079 if (lkpi_scan_chan(&channels[i], ic, true))
5080 nchan++;
5081 }
5082 }
5083 }
5084 #endif
5085 } else {
5086 IMPROVE("individual band scans not yet supported, only scanning first band");
5087 /* In theory net80211 should drive this. */
5088 /* Probably we need to add local logic for now;
5089 * need to deal with scan_complete
5090 * and cancel_scan and keep local state.
5091 * Also cut the nchan down above.
5092 */
5093 /* XXX-BZ ath10k does not set this but still does it? &$%^ */
5094 }
5095
5096 chan_len = nchan * (sizeof(lc) + sizeof(*lc));
5097
5098 common_ie_len = 0;
5099 if ((vap->iv_flags & IEEE80211_F_WPA1) != 0 &&
5100 vap->iv_wpa_ie != NULL)
5101 common_ie_len += vap->iv_wpa_ie[1];
5102 if (vap->iv_appie_probereq != NULL)
5103 common_ie_len += vap->iv_appie_probereq->ie_len;
5104
5105 /* We would love to check this at an earlier stage... */
5106 if (common_ie_len > hw->wiphy->max_scan_ie_len) {
5107 ic_printf(ic, "WARNING: %s: common_ie_len %d > "
5108 "wiphy->max_scan_ie_len %d\n", __func__,
5109 common_ie_len, hw->wiphy->max_scan_ie_len);
5110 }
5111
5112 lvif = VAP_TO_LVIF(vap);
5113
5114 hw_req = malloc(sizeof(*hw_req) + ssids_len +
5115 s6ghzlen + chan_len + lhw->supbands * lhw->scan_ie_len +
5116 common_ie_len, M_LKPI80211, M_WAITOK | M_ZERO);
5117
5118 hw_req->req.flags = 0; /* XXX ??? */
5119 hw_req->req.wdev = &lvif->wdev;
5120 hw_req->req.wiphy = hw->wiphy;
5121 hw_req->req.no_cck = false; /* XXX */
5122
5123 /*
5124 * In general setting duration[_mandatory] seems to pessimise
5125 * default scanning behaviour. We only use it for BGSCANnig
5126 * to keep the dwell times small.
5127 * Setting duration_mandatory makes this the maximum dwell
5128 * time (otherwise may be shorter). Duration is in TU.
5129 */
5130 if ((ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN) != 0) {
5131 unsigned long dwell;
5132
5133 if ((ic->ic_caps & IEEE80211_C_BGSCAN) == 0 ||
5134 (vap->iv_flags & IEEE80211_F_BGSCAN) == 0)
5135 ic_printf(ic, "BGSCAN despite off: %b, %b, %b\n",
5136 ic->ic_flags_ext, IEEE80211_FEXT_BITS,
5137 vap->iv_flags, IEEE80211_F_BITS,
5138 ic->ic_caps, IEEE80211_C_BITS);
5139
5140 dwell = ss->ss_mindwell;
5141 if (dwell == 0)
5142 dwell = msecs_to_ticks(20);
5143
5144 hw_req->req.duration_mandatory = true;
5145 hw_req->req.duration = TICKS_2_USEC(dwell) / 1024;
5146 }
5147
5148 #ifdef __notyet__
5149 hw_req->req.flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
5150 memcpy(hw_req->req.mac_addr, xxx, IEEE80211_ADDR_LEN);
5151 memset(hw_req->req.mac_addr_mask, 0xxx, IEEE80211_ADDR_LEN);
5152 #endif
5153 eth_broadcast_addr(hw_req->req.bssid);
5154
5155 hw_req->req.n_channels = nchan;
5156 cpp = (struct linuxkpi_ieee80211_channel **)(hw_req + 1);
5157 lc = (struct linuxkpi_ieee80211_channel *)(cpp + nchan);
5158 #ifdef LKPI_80211_USE_SCANLIST
5159 for (i = 0; i < nchan; i++) {
5160 *(cpp + i) =
5161 (struct linuxkpi_ieee80211_channel *)(lc + i);
5162 }
5163 /* Avoid net80211 scan lists until it has proper scan offload support. */
5164 for (i = 0; i < nchan; i++) {
5165 struct ieee80211_channel *c;
5166
5167 c = ss->ss_chans[ss->ss_next + i];
5168 lc->center_freq = c->ic_freq; /* XXX */
5169 /* lc->flags */
5170 lc->band = lkpi_net80211_chan_to_nl80211_band(c);
5171 lc->max_power = c->ic_maxpower;
5172 /* lc-> ... */
5173 lc++;
5174 }
5175 #else
5176 /* Add bands in reverse order for scanning. */
5177 n = 0;
5178 for (band = NUM_NL80211_BANDS - 1; band >= 0; band--) {
5179 struct ieee80211_supported_band *supband;
5180 struct linuxkpi_ieee80211_channel *channels;
5181
5182 /* Band disabled for scanning? */
5183 if ((band_mask & (1 << band)) == 0)
5184 continue;
5185
5186 /* Nothing to scan in band? */
5187 supband = hw->wiphy->bands[band];
5188 if (supband == NULL || supband->n_channels == 0)
5189 continue;
5190
5191 channels = supband->channels;
5192 for (i = 0; i < supband->n_channels; i++) {
5193 if (lkpi_scan_chan(&channels[i], ic, false))
5194 *(cpp + n++) = &channels[i];
5195 }
5196 }
5197 if (lkpi_order_scanlist)
5198 lkpi_scan_chan_list_resort(cpp, nchan);
5199
5200 if ((linuxkpi_debug_80211 & D80211_SCAN) != 0) {
5201 printf("%s:%d: %s SCAN Channel List (nchan=%zu): ",
5202 __func__, __LINE__, ic->ic_name, nchan);
5203 for (i = 0; i < nchan; i++) {
5204 struct linuxkpi_ieee80211_channel *xc;
5205
5206 xc = *(cpp + i);
5207 printf(" %d(%d)",
5208 ieee80211_mhz2ieee(xc->center_freq,
5209 lkpi_nl80211_band_to_net80211_band(
5210 xc->band)),
5211 xc->center_freq);
5212 }
5213 printf("\n");
5214 }
5215 #endif
5216
5217 hw_req->req.n_ssids = ssid_count;
5218 if (hw_req->req.n_ssids > 0) {
5219 ssids = (struct cfg80211_ssid *)lc;
5220 hw_req->req.ssids = ssids;
5221 for (i = 0; i < ssid_count; i++) {
5222 ssids->ssid_len = ss->ss_ssid[i].len;
5223 memcpy(ssids->ssid, ss->ss_ssid[i].ssid,
5224 ss->ss_ssid[i].len);
5225 ssids++;
5226 }
5227 s6gp = (struct cfg80211_scan_6ghz_params *)ssids;
5228 } else {
5229 s6gp = (struct cfg80211_scan_6ghz_params *)lc;
5230 }
5231
5232 /* 6GHz one day. */
5233 hw_req->req.n_6ghz_params = 0;
5234 hw_req->req.scan_6ghz_params = NULL;
5235 hw_req->req.scan_6ghz = false; /* Weird boolean; not what you think. */
5236 /* s6gp->... */
5237
5238 ie = ieend = (uint8_t *)s6gp;
5239 /* Copy per-band IEs, copy common IEs */
5240 ieend = lkpi_scan_ies_add(ie, &hw_req->ies, band_mask, vap, hw);
5241 hw_req->req.ie = ie;
5242 hw_req->req.ie_len = ieend - ie;
5243 hw_req->req.scan_start = jiffies;
5244
5245 vif = LVIF_TO_VIF(lvif);
5246
5247 LKPI_80211_LHW_SCAN_LOCK(lhw);
5248 /* Re-check under lock. */
5249 running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
5250 if (!running) {
5251 KASSERT(lhw->hw_req == NULL, ("%s: ic %p lhw %p hw_req %p "
5252 "!= NULL\n", __func__, ic, lhw, lhw->hw_req));
5253
5254 lhw->scan_flags |= LKPI_LHW_SCAN_RUNNING;
5255 lhw->hw_req = hw_req;
5256 }
5257 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5258 if (running) {
5259 free(hw_req, M_LKPI80211);
5260 TRACE_SCAN(ic, "Trying to start new scan while still "
5261 "running (2); cancelling new net80211 scan; "
5262 "scan_flags %b",
5263 lhw->scan_flags, LKPI_LHW_SCAN_BITS);
5264 ieee80211_cancel_scan(vap);
5265 return;
5266 }
5267
5268 wiphy_lock(hw->wiphy);
5269 lkpi_update_mcast_filter_locked(ic);
5270 TRACE_SCAN(ic, "Starting HW_SCAN: scan_flags %b, "
5271 "ie_len %d, n_ssids %d, n_chan %d, common_ie_len %d [%d, %d]",
5272 lhw->scan_flags, LKPI_LHW_SCAN_BITS, hw_req->req.ie_len,
5273 hw_req->req.n_ssids, hw_req->req.n_channels,
5274 hw_req->ies.common_ie_len,
5275 hw_req->ies.len[NL80211_BAND_2GHZ],
5276 hw_req->ies.len[NL80211_BAND_5GHZ]);
5277
5278 error = lkpi_80211_mo_hw_scan(hw, vif, hw_req);
5279 wiphy_unlock(hw->wiphy);
5280 if (error != 0) {
5281 bool scan_done;
5282 int e;
5283
5284 TRACE_SCAN(ic, "hw_scan failed; scan_flags %b, error %d",
5285 lhw->scan_flags, LKPI_LHW_SCAN_BITS, error);
5286 ieee80211_cancel_scan(vap);
5287
5288 /*
5289 * ieee80211_scan_completed must be called in either
5290 * case of error or none. So let the free happen there
5291 * and only there.
5292 * That would be fine in theory but in practice drivers
5293 * behave differently:
5294 * ath10k does not return hw_scan until after scan_complete
5295 * and can then still return an error.
5296 * rtw88 can return 1 or -EBUSY without scan_complete
5297 * iwlwifi can return various errors before scan starts
5298 * ...
5299 * So we cannot rely on that behaviour and have to check
5300 * and balance between both code paths.
5301 */
5302 e = 0;
5303 scan_done = true;
5304 LKPI_80211_LHW_SCAN_LOCK(lhw);
5305 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0) {
5306
5307 free(lhw->hw_req, M_LKPI80211);
5308 lhw->hw_req = NULL;
5309 /*
5310 * The ieee80211_cancel_scan() above runs in a
5311 * taskq and it may take ages for the previous
5312 * scan to clear; starting a new one right away
5313 * we run into the problem that the old one is
5314 * still active.
5315 */
5316 e = msleep(lhw, &lhw->scan_mtx, 0, "lhwscanstop", hz);
5317 scan_done = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
5318
5319 /*
5320 * Now we can clear running if no one else did.
5321 */
5322 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
5323 }
5324 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5325 lkpi_update_mcast_filter(ic);
5326 if (!scan_done) {
5327 ic_printf(ic, "ERROR: %s: timeout/error to wait "
5328 "for ieee80211_cancel_scan: %d\n", __func__, e);
5329 return;
5330 }
5331
5332 /*
5333 * XXX-SIGH magic number.
5334 * rtw88 has a magic "return 1" if offloading scan is
5335 * not possible. Fall back to sw scan in that case.
5336 */
5337 if (error == 1) {
5338 /*
5339 * We need to put this into some defered context
5340 * the net80211 scan may not be done yet
5341 * (ic_flags & IEEE80211_F_SCAN) and we cannot
5342 * wait here; if we do scan_curchan_task always
5343 * runs after our timeout to finalize the scan.
5344 */
5345 ieee80211_runtask(ic, &lvif->sw_scan_task);
5346 return;
5347 }
5348
5349 ic_printf(ic, "ERROR: %s: hw_scan returned %d\n",
5350 __func__, error);
5351 }
5352 }
5353 }
5354
5355 static void
lkpi_sw_scan_task(void * arg,int pending __unused)5356 lkpi_sw_scan_task(void *arg, int pending __unused)
5357 {
5358 struct lkpi_hw *lhw;
5359 struct lkpi_vif *lvif;
5360 struct ieee80211vap *vap;
5361 struct ieee80211_scan_state *ss;
5362
5363 lvif = arg;
5364 vap = LVIF_TO_VAP(lvif);
5365 lhw = vap->iv_ic->ic_softc;
5366 ss = vap->iv_ic->ic_scan;
5367
5368 LKPI_80211_LHW_SCAN_LOCK(lhw);
5369 /*
5370 * We will re-enable this at scan_end calling lkpi_enable_hw_scan().
5371 * IEEE80211_FEXT_SCAN_OFFLOAD will be cleared by lkpi_ic_scan_start.
5372 */
5373 lhw->scan_flags &= ~LKPI_LHW_SCAN_HW;
5374 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5375
5376 TRACE_SCAN(vap->iv_ic, "Triggering SW_SCAN: pending %d, scan_flags %b",
5377 pending, lhw->scan_flags, LKPI_LHW_SCAN_BITS);
5378
5379 /*
5380 * This will call ic_scan_start() and we will get into the right path
5381 * unless other scans started in between.
5382 */
5383 ieee80211_start_scan(vap,
5384 IEEE80211_SCAN_ONCE,
5385 msecs_to_ticks(10000), /* 10000 ms (=~ 50 chan * 200 ms) */
5386 ss->ss_mindwell ? ss->ss_mindwell : msecs_to_ticks(20),
5387 ss->ss_maxdwell ? ss->ss_maxdwell : msecs_to_ticks(200),
5388 vap->iv_des_nssid, vap->iv_des_ssid);
5389 }
5390
5391 static void
lkpi_ic_scan_end(struct ieee80211com * ic)5392 lkpi_ic_scan_end(struct ieee80211com *ic)
5393 {
5394 struct lkpi_hw *lhw;
5395 bool is_hw_scan;
5396
5397 lhw = ic->ic_softc;
5398 TRACE_SCAN(ic, "scan_flags %b", lhw->scan_flags, LKPI_LHW_SCAN_BITS);
5399
5400 LKPI_80211_LHW_SCAN_LOCK(lhw);
5401 if ((lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) == 0) {
5402 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5403 return;
5404 }
5405 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
5406 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5407
5408 if (!is_hw_scan) {
5409 struct ieee80211_scan_state *ss;
5410 struct ieee80211vap *vap;
5411 struct ieee80211_hw *hw;
5412 struct lkpi_vif *lvif;
5413 struct ieee80211_vif *vif;
5414
5415 ss = ic->ic_scan;
5416 vap = ss->ss_vap;
5417 hw = LHW_TO_HW(lhw);
5418 lvif = VAP_TO_LVIF(vap);
5419 vif = LVIF_TO_VIF(lvif);
5420
5421 lkpi_80211_mo_sw_scan_complete(hw, vif);
5422
5423 /* Send PS to stop buffering if n80211 does not for us? */
5424
5425 if (vap->iv_state == IEEE80211_S_SCAN) {
5426 wiphy_lock(hw->wiphy);
5427 lkpi_hw_conf_idle(hw, true);
5428 wiphy_unlock(hw->wiphy);
5429 }
5430 }
5431
5432 /*
5433 * In case we disabled the hw_scan in lkpi_ic_scan_start() and
5434 * switched to swscan, re-enable hw_scan if available.
5435 */
5436 lkpi_enable_hw_scan(lhw);
5437
5438 /* Clear the scanning chandef. */
5439 memset(&lhw->scan_chandef, 0, sizeof(lhw->scan_chandef));
5440
5441 LKPI_80211_LHW_SCAN_LOCK(lhw);
5442 wakeup(lhw);
5443 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5444 }
5445
5446 static void
lkpi_ic_scan_curchan(struct ieee80211_scan_state * ss,unsigned long maxdwell)5447 lkpi_ic_scan_curchan(struct ieee80211_scan_state *ss,
5448 unsigned long maxdwell)
5449 {
5450 struct lkpi_hw *lhw;
5451 bool is_hw_scan;
5452
5453 lhw = ss->ss_ic->ic_softc;
5454 TRACE_SCAN(ss->ss_ic, "scan_flags %b chan %d maxdwell %lu",
5455 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
5456 ss->ss_ic->ic_curchan->ic_ieee, maxdwell);
5457
5458 LKPI_80211_LHW_SCAN_LOCK(lhw);
5459 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
5460 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5461 if (!is_hw_scan)
5462 lhw->ic_scan_curchan(ss, maxdwell);
5463 }
5464
5465 static void
lkpi_ic_scan_mindwell(struct ieee80211_scan_state * ss)5466 lkpi_ic_scan_mindwell(struct ieee80211_scan_state *ss)
5467 {
5468 struct lkpi_hw *lhw;
5469 bool is_hw_scan;
5470
5471 lhw = ss->ss_ic->ic_softc;
5472 TRACE_SCAN(ss->ss_ic, "scan_flags %b chan %d mindwell %lu",
5473 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
5474 ss->ss_ic->ic_curchan->ic_ieee, ss->ss_mindwell);
5475
5476 LKPI_80211_LHW_SCAN_LOCK(lhw);
5477 is_hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
5478 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5479 if (!is_hw_scan)
5480 lhw->ic_scan_mindwell(ss);
5481 }
5482
5483 struct lkpi_ic_set_channel_iter_arg {
5484 struct linuxkpi_ieee80211_channel *chan;
5485 struct ieee80211_chanctx_conf *chanctx_conf;
5486 };
5487
5488 static void
lkpi_ic_set_channel_chanctx_iterf(struct ieee80211_hw * hw,struct ieee80211_chanctx_conf * chanctx_conf,void * arg)5489 lkpi_ic_set_channel_chanctx_iterf(struct ieee80211_hw *hw,
5490 struct ieee80211_chanctx_conf *chanctx_conf, void *arg)
5491 {
5492 struct lkpi_ic_set_channel_iter_arg *chanctx_iter_arg;
5493
5494 chanctx_iter_arg = arg;
5495 if (chanctx_iter_arg->chanctx_conf != NULL)
5496 return;
5497
5498 if (chanctx_iter_arg->chan == chanctx_conf->def.chan)
5499 chanctx_iter_arg->chanctx_conf = chanctx_conf;
5500 }
5501
5502 static void
lkpi_ic_set_channel(struct ieee80211com * ic)5503 lkpi_ic_set_channel(struct ieee80211com *ic)
5504 {
5505 struct lkpi_hw *lhw;
5506 struct ieee80211_hw *hw;
5507 struct ieee80211_channel *c;
5508 struct linuxkpi_ieee80211_channel *chan;
5509 struct ieee80211_chanctx_conf *chanctx_conf;
5510 uint32_t changed;
5511 int error;
5512 bool hw_scan, scan_running;
5513
5514 IEEE80211_UNLOCK_ASSERT(ic);
5515
5516 lhw = ic->ic_softc;
5517
5518 c = ic->ic_curchan;
5519 if (c == NULL || c == IEEE80211_CHAN_ANYC) {
5520 ic_printf(ic, "%s: Unset channel: c %p, ignoring update\n",
5521 __func__, c);
5522 return;
5523 }
5524
5525 chan = lkpi_find_lkpi80211_chan(lhw, c);
5526 if (chan == NULL) {
5527 ic_printf(ic, "%s: No channel found for c %p(%d) chan %p\n",
5528 __func__, c, c->ic_ieee, chan);
5529 return;
5530 }
5531
5532 /*
5533 * All net80211 callers call ieee80211_radiotap_chan_change().
5534 * That means we have nothing to do ourselves.
5535 */
5536
5537 /* If we have a hw_scan running do not switch channels. */
5538 LKPI_80211_LHW_SCAN_LOCK(lhw);
5539 scan_running = (lhw->scan_flags & LKPI_LHW_SCAN_RUNNING) != 0;
5540 hw_scan = (lhw->scan_flags & LKPI_LHW_SCAN_HW) != 0;
5541 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
5542 if (scan_running && hw_scan) {
5543 TRACE_SCAN(ic, "scan_flags %b chan %d nothing to do.",
5544 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
5545 c->ic_ieee);
5546 /* Let us hope we set tx power levels elsewhere. */
5547 return;
5548 }
5549
5550 hw = LHW_TO_HW(lhw);
5551 wiphy_lock(hw->wiphy);
5552 if (scan_running) {
5553 struct ieee80211vap *vap;
5554 struct lkpi_vif *lvif;
5555 struct ieee80211_vif *vif;
5556
5557 /*
5558 * For now and for scanning just pick the first VIF.
5559 * net80211 will need to grow DBDC/link_id support
5560 * for us to find the vif/chanctx otherwise.
5561 */
5562 vap = TAILQ_FIRST(&ic->ic_vaps);
5563 lvif = VAP_TO_LVIF(vap);
5564 vif = LVIF_TO_VIF(lvif);
5565
5566 /* We always set the chandef to no-HT for scanning. */
5567 cfg80211_chandef_create(&lhw->scan_chandef, chan,
5568 NL80211_CHAN_NO_HT);
5569 #ifdef LINUXKPI_DEBUG_80211
5570 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
5571 ic_printf(ic, "%s:%d: initialized lhw->scan_chandef\n",
5572 __func__, __LINE__);
5573 #endif
5574
5575 /*
5576 * This works for as long as we do not do BGSCANs; otherwise
5577 * it'll have to be offchan work.
5578 */
5579 chanctx_conf = lkpi_get_chanctx_conf(hw, vif);
5580 changed = lkpi_init_chanctx_conf(hw, &lhw->scan_chandef, chanctx_conf);
5581 error = lkpi_set_chanctx_conf(hw, vif, chanctx_conf, changed, true);
5582
5583 TRACE_SCAN(ic, "scan_flags %b chan %d ???, error %d",
5584 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
5585 c->ic_ieee, error);
5586
5587 IMPROVE("max power for scanning; TODO in lkpi_80211_update_chandef");
5588
5589 } else if (lhw->emulate_chanctx) {
5590 /*
5591 * We do not set the channel here for normal chanctx operation.
5592 * That's just a setup to fail. scan_to_auth will setup all the
5593 * other neccessary options for this to work.
5594 */
5595 struct lkpi_ic_set_channel_iter_arg chanctx_iter_arg = {
5596 .chan = chan,
5597 .chanctx_conf = NULL,
5598 };
5599 struct cfg80211_chan_def chandef;
5600
5601 lkpi_init_chandef(ic, &chandef, chan, c, false);
5602
5603 ieee80211_iter_chan_contexts_mtx(hw,
5604 lkpi_ic_set_channel_chanctx_iterf, &chanctx_iter_arg);
5605
5606 if (chanctx_iter_arg.chanctx_conf == NULL) {
5607 /* No chanctx found for this channel. */
5608 struct ieee80211vap *vap;
5609 struct lkpi_vif *lvif;
5610 struct ieee80211_vif *vif;
5611
5612 /*
5613 * For now just pick the first VIF.
5614 * net80211 will need to grow DBDC/link_id support
5615 * for us to find the vif/chanctx otherwise.
5616 */
5617 vap = TAILQ_FIRST(&ic->ic_vaps);
5618 lvif = VAP_TO_LVIF(vap);
5619 vif = LVIF_TO_VIF(lvif);
5620
5621 #ifdef LINUXKPI_DEBUG_80211
5622 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
5623 ic_printf(ic, "%s:%d: using on stack chandef\n",
5624 __func__, __LINE__);
5625 #endif
5626 chanctx_conf = lkpi_get_chanctx_conf(hw, vif);
5627 changed = lkpi_init_chanctx_conf(hw, &chandef, chanctx_conf);
5628 IMPROVE("update HT, VHT, bw, ...");
5629 error = lkpi_set_chanctx_conf(hw, vif, chanctx_conf, changed, true);
5630
5631 } else {
5632 /*
5633 * We know we are on the same channel.
5634 * Do we really have to reset everything?
5635 */
5636 IMPROVE("update HT, VHT, bw, ...");
5637
5638 #ifdef LINUXKPI_DEBUG_80211
5639 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
5640 ic_printf(ic, "%s:%d: using on stack chandef\n",
5641 __func__, __LINE__);
5642 #endif
5643
5644 chanctx_conf = chanctx_iter_arg.chanctx_conf;
5645 changed = lkpi_init_chanctx_conf(hw, &chandef, chanctx_conf);
5646 lkpi_80211_mo_change_chanctx(hw, chanctx_conf, changed);
5647 }
5648 }
5649
5650 /* Currently PS is hard coded off! Not sure it belongs here. */
5651 IMPROVE("PS");
5652 if (ieee80211_hw_check(hw, SUPPORTS_PS) &&
5653 (hw->conf.flags & IEEE80211_CONF_PS) != 0) {
5654 hw->conf.flags &= ~IEEE80211_CONF_PS;
5655 error = lkpi_80211_mo_config(hw, IEEE80211_CONF_CHANGE_PS);
5656 if (error != 0 && error != EOPNOTSUPP)
5657 ic_printf(ic, "ERROR: %s: config %#0x returned "
5658 "%d\n", __func__, IEEE80211_CONF_CHANGE_PS,
5659 error);
5660 }
5661
5662 wiphy_unlock(hw->wiphy);
5663 }
5664
5665 static struct ieee80211_node *
lkpi_ic_node_alloc(struct ieee80211vap * vap,const uint8_t mac[IEEE80211_ADDR_LEN])5666 lkpi_ic_node_alloc(struct ieee80211vap *vap,
5667 const uint8_t mac[IEEE80211_ADDR_LEN])
5668 {
5669 struct ieee80211com *ic;
5670 struct lkpi_hw *lhw;
5671 struct ieee80211_node *ni;
5672 struct ieee80211_hw *hw;
5673 struct lkpi_sta *lsta;
5674
5675 ic = vap->iv_ic;
5676 lhw = ic->ic_softc;
5677
5678 /* We keep allocations de-coupled so we can deal with the two worlds. */
5679 if (lhw->ic_node_alloc == NULL)
5680 return (NULL);
5681
5682 ni = lhw->ic_node_alloc(vap, mac);
5683 if (ni == NULL)
5684 return (NULL);
5685
5686 hw = LHW_TO_HW(lhw);
5687 lsta = lkpi_lsta_alloc(vap, mac, hw, ni);
5688 if (lsta == NULL) {
5689 if (lhw->ic_node_free != NULL)
5690 lhw->ic_node_free(ni);
5691 return (NULL);
5692 }
5693
5694 return (ni);
5695 }
5696
5697 static int
lkpi_ic_node_init(struct ieee80211_node * ni)5698 lkpi_ic_node_init(struct ieee80211_node *ni)
5699 {
5700 struct ieee80211com *ic;
5701 struct lkpi_hw *lhw;
5702 int error;
5703
5704 ic = ni->ni_ic;
5705 lhw = ic->ic_softc;
5706
5707 if (lhw->ic_node_init != NULL) {
5708 error = lhw->ic_node_init(ni);
5709 if (error != 0)
5710 return (error);
5711 }
5712
5713 /* XXX-BZ Sync other state over. */
5714 IMPROVE();
5715
5716 return (0);
5717 }
5718
5719 static void
lkpi_ic_node_cleanup(struct ieee80211_node * ni)5720 lkpi_ic_node_cleanup(struct ieee80211_node *ni)
5721 {
5722 struct ieee80211com *ic;
5723 struct lkpi_hw *lhw;
5724
5725 ic = ni->ni_ic;
5726 lhw = ic->ic_softc;
5727
5728 /* XXX-BZ remove from driver, ... */
5729 IMPROVE();
5730
5731 if (lhw->ic_node_cleanup != NULL)
5732 lhw->ic_node_cleanup(ni);
5733 }
5734
5735 static void
lkpi_ic_node_free(struct ieee80211_node * ni)5736 lkpi_ic_node_free(struct ieee80211_node *ni)
5737 {
5738 struct ieee80211com *ic;
5739 struct lkpi_hw *lhw;
5740 struct lkpi_sta *lsta;
5741
5742 ic = ni->ni_ic;
5743 lhw = ic->ic_softc;
5744 lsta = ni->ni_drv_data;
5745
5746 /* KASSERT lsta is not NULL here. Print ni/ni__refcnt. */
5747
5748 /*
5749 * Pass in the original ni just in case of error we could check that
5750 * it is the same as lsta->ni.
5751 */
5752 lkpi_lsta_free(lsta, ni);
5753
5754 if (lhw->ic_node_free != NULL)
5755 lhw->ic_node_free(ni);
5756 }
5757
5758 /*
5759 * lkpi_xmit() called from both the (*ic_raw_xmit) as well as the (*ic_transmit)
5760 * call path.
5761 * Unfortunately they have slightly different invariants. See
5762 * ieee80211_raw_output() and ieee80211_parent_xmitpkt().
5763 * Both take care of the ni reference in case of error, and otherwise during
5764 * the callback after transmit.
5765 * The difference is that in case of error (*ic_raw_xmit) needs us to release
5766 * the mbuf, while (*ic_transmit) will free the mbuf itself.
5767 */
5768 static int
lkpi_xmit(struct ieee80211_node * ni,struct mbuf * m,const struct ieee80211_bpf_params * params __unused,bool freem)5769 lkpi_xmit(struct ieee80211_node *ni, struct mbuf *m,
5770 const struct ieee80211_bpf_params *params __unused,
5771 bool freem)
5772 {
5773 struct lkpi_sta *lsta;
5774 int error;
5775
5776 lsta = ni->ni_drv_data;
5777 LKPI_80211_LSTA_TXQ_LOCK(lsta);
5778 #if 0
5779 if (!lsta->added_to_drv || !lsta->txq_ready) {
5780 #else
5781 /*
5782 * Backout this part of 886653492945f which breaks rtw88 or
5783 * in general drivers without (*sta_state)() but only the
5784 * legacy fallback to (*sta_add)().
5785 */
5786 if (!lsta->txq_ready) {
5787 #endif
5788 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5789 if (freem)
5790 m_freem(m);
5791 return (ENETDOWN);
5792 }
5793
5794 /* Queue the packet and enqueue the task to handle it. */
5795 error = mbufq_enqueue(&lsta->txq, m);
5796 if (error != 0) {
5797 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5798 if (freem)
5799 m_freem(m);
5800 #ifdef LINUXKPI_DEBUG_80211
5801 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5802 ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
5803 __func__, error);
5804 #endif
5805 return (ENETDOWN);
5806 }
5807 taskqueue_enqueue(taskqueue_thread, &lsta->txq_task);
5808 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
5809
5810 #ifdef LINUXKPI_DEBUG_80211
5811 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
5812 printf("%s:%d lsta %p ni %p %6D mbuf_qlen %d\n",
5813 __func__, __LINE__, lsta, ni, ni->ni_macaddr, ":",
5814 mbufq_len(&lsta->txq));
5815 #endif
5816
5817 return (0);
5818 }
5819
5820 static int
5821 lkpi_ic_raw_xmit(struct ieee80211_node *ni, struct mbuf *m,
5822 const struct ieee80211_bpf_params *params __unused)
5823 {
5824 return (lkpi_xmit(ni, m, NULL, true));
5825 }
5826
5827 #ifdef LKPI_80211_HW_CRYPTO
5828 /*
5829 * This is a bit of a hack given we know we are operating on a
5830 * single frame and we know that hardware will deal with it.
5831 * But otherwise the enmic bit and the encrypt bit need to be
5832 * decoupled.
5833 */
5834 static int
5835 lkpi_hw_crypto_prepare_tkip(struct ieee80211_key *k,
5836 struct ieee80211_key_conf *kc, struct sk_buff *skb)
5837 {
5838 struct ieee80211_hdr *hdr;
5839 uint32_t hlen, hdrlen;
5840 uint8_t *p;
5841
5842 /*
5843 * TKIP only happens on data.
5844 */
5845 hdr = (void *)skb->data;
5846 if (!ieee80211_is_data_present(hdr->frame_control))
5847 return (0);
5848
5849 /*
5850 * "enmic" (though we do not do that).
5851 */
5852 /* any conditions to not apply this? */
5853 if (skb_tailroom(skb) < ieee80211_crypto_get_key_txmic_len(k))
5854 return (ENOBUFS);
5855
5856 p = skb_put(skb, ieee80211_crypto_get_key_txmic_len(k));
5857 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_MIC_SPACE) != 0)
5858 goto encrypt;
5859
5860 /*
5861 * (*enmic) which we hopefully do not have to do with hw accel.
5862 * That means if we make it here we have a problem.
5863 */
5864 TODO("(*enmic)");
5865 return (ENXIO);
5866
5867 encrypt:
5868 /*
5869 * "encrypt" (though we do not do that).
5870 */
5871 /*
5872 * Check if we have anything to do as requested by driver
5873 * or if we are done?
5874 */
5875 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
5876 (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0)
5877 return (0);
5878
5879 hlen = k->wk_cipher->ic_header;
5880 if (skb_headroom(skb) < hlen)
5881 return (ENOBUFS);
5882
5883 hdr = (void *)skb->data;
5884 hdrlen = ieee80211_hdrlen(hdr->frame_control);
5885 p = skb_push(skb, hlen);
5886 memmove(p, p + hlen, hdrlen);
5887
5888 /* If driver request space only we are done. */
5889 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
5890 return (0);
5891
5892 p += hdrlen;
5893 k->wk_cipher->ic_setiv(k, p);
5894
5895 /* If we make it hear we do sw encryption. */
5896 TODO("sw encrypt");
5897 return (ENXIO);
5898 }
5899
5900 static int
5901 lkpi_hw_crypto_prepare_ccmp(struct ieee80211_key *k,
5902 struct ieee80211_key_conf *kc, struct sk_buff *skb)
5903 {
5904 struct ieee80211_hdr *hdr;
5905 uint32_t hlen, hdrlen;
5906 uint8_t *p;
5907
5908 hdr = (void *)skb->data;
5909
5910 /*
5911 * Check if we have anythig to do as requested by driver
5912 * or if we are done?
5913 */
5914 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) == 0 &&
5915 (kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV) == 0 &&
5916 /* MFP */
5917 !((kc->flags & IEEE80211_KEY_FLAG_GENERATE_IV_MGMT) != 0 &&
5918 ieee80211_is_mgmt(hdr->frame_control)))
5919 return (0);
5920
5921 hlen = k->wk_cipher->ic_header;
5922 if (skb_headroom(skb) < hlen)
5923 return (ENOBUFS);
5924
5925 hdrlen = ieee80211_hdrlen(hdr->frame_control);
5926 p = skb_push(skb, hlen);
5927 memmove(p, p + hlen, hdrlen);
5928
5929 /* If driver request space only we are done. */
5930 if ((kc->flags & IEEE80211_KEY_FLAG_PUT_IV_SPACE) != 0)
5931 return (0);
5932
5933 p += hdrlen;
5934 k->wk_cipher->ic_setiv(k, p);
5935
5936 return (0);
5937 }
5938
5939 static int
5940 lkpi_hw_crypto_prepare(struct lkpi_sta *lsta, struct ieee80211_key *k,
5941 struct sk_buff *skb)
5942 {
5943 struct ieee80211_tx_info *info;
5944 struct ieee80211_key_conf *kc;
5945
5946 KASSERT(lsta != NULL, ("%s: lsta is NULL", __func__));
5947 KASSERT(k != NULL, ("%s: key is NULL", __func__));
5948 KASSERT(skb != NULL, ("%s: skb is NULL", __func__));
5949
5950 kc = lsta->kc[k->wk_keyix];
5951
5952 info = IEEE80211_SKB_CB(skb);
5953 info->control.hw_key = kc;
5954
5955 /* MUST NOT happen. KASSERT? */
5956 if (kc == NULL) {
5957 ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p skb %p, "
5958 "kc is NULL on hw crypto offload\n", __func__, lsta, k, skb);
5959 return (ENXIO);
5960 }
5961
5962 switch (kc->cipher) {
5963 case WLAN_CIPHER_SUITE_TKIP:
5964 return (lkpi_hw_crypto_prepare_tkip(k, kc, skb));
5965 case WLAN_CIPHER_SUITE_CCMP:
5966 return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5967 case WLAN_CIPHER_SUITE_GCMP:
5968 return (lkpi_hw_crypto_prepare_ccmp(k, kc, skb));
5969 case WLAN_CIPHER_SUITE_WEP40:
5970 case WLAN_CIPHER_SUITE_WEP104:
5971 case WLAN_CIPHER_SUITE_CCMP_256:
5972 case WLAN_CIPHER_SUITE_GCMP_256:
5973 case WLAN_CIPHER_SUITE_AES_CMAC:
5974 case WLAN_CIPHER_SUITE_BIP_CMAC_256:
5975 case WLAN_CIPHER_SUITE_BIP_GMAC_128:
5976 case WLAN_CIPHER_SUITE_BIP_GMAC_256:
5977 default:
5978 ic_printf(lsta->ni->ni_ic, "%s: lsta %p k %p kc %p skb %p, "
5979 "unsupported cipher suite %u (%s)\n", __func__, lsta, k, kc,
5980 skb, kc->cipher, lkpi_cipher_suite_to_name(kc->cipher));
5981 return (EOPNOTSUPP);
5982 }
5983 }
5984
5985 static uint8_t
5986 lkpi_hw_crypto_tailroom(struct lkpi_sta *lsta, struct ieee80211_key *k)
5987 {
5988 struct ieee80211_key_conf *kc;
5989
5990 kc = lsta->kc[k->wk_keyix];
5991 if (kc == NULL)
5992 return (0);
5993
5994 IMPROVE("which other flags need tailroom?");
5995 if (kc->flags & (IEEE80211_KEY_FLAG_PUT_MIC_SPACE))
5996 return (32); /* Large enough to hold everything and pow2. */
5997
5998 return (0);
5999 }
6000 #endif
6001
6002 static void
6003 lkpi_80211_txq_tx_one(struct lkpi_sta *lsta, struct mbuf *m)
6004 {
6005 struct ieee80211_node *ni;
6006 struct ieee80211_frame *wh;
6007 struct ieee80211_key *k;
6008 struct sk_buff *skb;
6009 struct ieee80211com *ic;
6010 struct lkpi_hw *lhw;
6011 struct ieee80211_hw *hw;
6012 struct lkpi_vif *lvif;
6013 struct ieee80211_vif *vif;
6014 struct ieee80211_channel *c;
6015 struct ieee80211_tx_control control;
6016 struct ieee80211_tx_info *info;
6017 struct ieee80211_sta *sta;
6018 struct ieee80211_hdr *hdr;
6019 struct lkpi_txq *ltxq;
6020 void *buf;
6021 ieee80211_keyix keyix;
6022 uint8_t ac, tid, tailroom;
6023
6024 M_ASSERTPKTHDR(m);
6025 #ifdef LINUXKPI_DEBUG_80211
6026 if (linuxkpi_debug_80211 & D80211_TRACE_TX_DUMP)
6027 hexdump(mtod(m, const void *), m->m_len, "RAW TX (plain) ", 0);
6028 #endif
6029
6030 ni = lsta->ni;
6031 ieee80211_output_seqno_assign(ni, -1, m);
6032
6033 k = NULL;
6034 keyix = IEEE80211_KEYIX_NONE;
6035 wh = mtod(m, struct ieee80211_frame *);
6036 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) {
6037
6038 #ifdef LKPI_80211_HW_CRYPTO
6039 if (lkpi_hwcrypto) {
6040 k = ieee80211_crypto_get_txkey(ni, m);
6041 if (k != NULL && lsta->kc[k->wk_keyix] != NULL)
6042 keyix = k->wk_keyix;
6043 }
6044 #endif
6045
6046 /* Encrypt the frame if need be. */
6047 if (keyix == IEEE80211_KEYIX_NONE) {
6048 /* Retrieve key for TX && do software encryption. */
6049 k = ieee80211_crypto_encap(ni, m);
6050 if (k == NULL) {
6051 ieee80211_free_node(ni);
6052 m_freem(m);
6053 return;
6054 }
6055 }
6056 }
6057
6058 ic = ni->ni_ic;
6059 lhw = ic->ic_softc;
6060 hw = LHW_TO_HW(lhw);
6061 c = ni->ni_chan;
6062
6063 if (ieee80211_radiotap_active_vap(ni->ni_vap)) {
6064 struct lkpi_radiotap_tx_hdr *rtap;
6065
6066 rtap = &lhw->rtap_tx;
6067 rtap->wt_flags = 0;
6068 if (k != NULL)
6069 rtap->wt_flags |= IEEE80211_RADIOTAP_F_WEP;
6070 if (m->m_flags & M_FRAG)
6071 rtap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG;
6072 IMPROVE();
6073 rtap->wt_rate = 0;
6074 if (c != NULL && c != IEEE80211_CHAN_ANYC) {
6075 rtap->wt_chan_freq = htole16(c->ic_freq);
6076 rtap->wt_chan_flags = htole16(c->ic_flags);
6077 }
6078
6079 ieee80211_radiotap_tx(ni->ni_vap, m);
6080 }
6081
6082 #ifdef LKPI_80211_HW_CRYPTO
6083 if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE)
6084 tailroom = lkpi_hw_crypto_tailroom(lsta, k);
6085 else
6086 #endif
6087 tailroom = 0;
6088
6089 /*
6090 * net80211 should handle hw->extra_tx_headroom.
6091 * Though for as long as we are copying we don't mind.
6092 * XXX-BZ rtw88 asks for too much headroom for ipv6+tcp:
6093 * https://lists.freebsd.org/archives/freebsd-transport/2022-February/000012.html
6094 */
6095 skb = dev_alloc_skb(hw->extra_tx_headroom + tailroom + m->m_pkthdr.len);
6096 if (skb == NULL) {
6097 static uint8_t skb_alloc_failures = 0;
6098
6099 if (skb_alloc_failures++ == 0) {
6100 int tid;
6101
6102 sta = LSTA_TO_STA(lsta);
6103 ic_printf(ic, "ERROR %s: skb alloc failed %d + %d, lsta %p sta %p ni %p\n",
6104 __func__, hw->extra_tx_headroom, m->m_pkthdr.len, lsta, sta, ni);
6105 for (tid = 0; tid < nitems(sta->txq); tid++) {
6106 if (sta->txq[tid] == NULL)
6107 continue;
6108 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
6109 ic_printf(ic, " tid %d ltxq %p flags %b skb_queue_len %u\n",
6110 tid, ltxq, ltxq->flags, LKPI_TXQ_FLAGS_BITS, skb_queue_len(<xq->skbq));
6111 }
6112 }
6113 ieee80211_free_node(ni);
6114 m_freem(m);
6115 return;
6116 }
6117 skb_reserve(skb, hw->extra_tx_headroom);
6118
6119 /* XXX-BZ we need a SKB version understanding mbuf. */
6120 /* Save the mbuf for ieee80211_tx_complete(). */
6121 skb->m_free_func = lkpi_ieee80211_free_skb_mbuf;
6122 skb->m = m;
6123 #if 0
6124 skb_put_data(skb, m->m_data, m->m_pkthdr.len);
6125 #else
6126 buf = skb_put(skb, m->m_pkthdr.len);
6127 m_copydata(m, 0, m->m_pkthdr.len, buf);
6128 #endif
6129 /* Save the ni. */
6130 m->m_pkthdr.PH_loc.ptr = ni;
6131
6132 lvif = VAP_TO_LVIF(ni->ni_vap);
6133 vif = LVIF_TO_VIF(lvif);
6134
6135 hdr = (void *)skb->data;
6136 tid = linuxkpi_ieee80211_get_tid(hdr, true);
6137 if (tid == IEEE80211_NONQOS_TID) { /* == IEEE80211_NUM_TIDS */
6138 if (!ieee80211_is_data(hdr->frame_control)) {
6139 /* MGMT and CTRL frames go on TID 7/VO. */
6140 skb->priority = 7;
6141 ac = IEEE80211_AC_VO;
6142 } else {
6143 /* Other non-QOS traffic goes to BE. */
6144 /* Contrary to net80211 we MUST NOT promote M_EAPOL. */
6145 skb->priority = 0;
6146 ac = IEEE80211_AC_BE;
6147 }
6148 } else {
6149 skb->priority = tid & IEEE80211_QOS_CTL_TID_MASK;
6150 ac = ieee80211e_up_to_ac[tid & 7];
6151 }
6152 skb_set_queue_mapping(skb, ac);
6153
6154 info = IEEE80211_SKB_CB(skb);
6155 info->flags |= IEEE80211_TX_CTL_REQ_TX_STATUS;
6156 /* Slight delay; probably only happens on scanning so fine? */
6157 if (c == NULL || c == IEEE80211_CHAN_ANYC)
6158 c = ic->ic_curchan;
6159 info->band = lkpi_net80211_chan_to_nl80211_band(c);
6160 info->hw_queue = vif->hw_queue[ac];
6161 if ((m->m_flags & M_EAPOL) != 0) {
6162 info->control.flags |= IEEE80211_TX_CTRL_PORT_CTRL_PROTO;
6163 info->flags |= IEEE80211_TX_CTL_USE_MINRATE; /* mt76 */
6164 TRACE_RATES("M_EAPOL -> TX_CTL_USE_MINRATE");
6165 }
6166 info->control.vif = vif;
6167
6168 /* IMPROVE("MLO"); */
6169 info->control.flags |=
6170 u32_encode_bits(IEEE80211_LINK_UNSPECIFIED, IEEE80211_TX_CTRL_MLO_LINK);
6171
6172 if (tid != IEEE80211_NONQOS_TID) {
6173 struct ieee80211_tx_ampdu *tap;
6174
6175 tap = &ni->ni_tx_ampdu[tid];
6176 if (ieee80211_is_data_qos(hdr->frame_control) &&
6177 !ieee80211_is_qos_nullfunc(hdr->frame_control) &&
6178 !is_multicast_ether_addr(hdr->addr1) &&
6179 IEEE80211_AMPDU_RUNNING(tap))
6180 info->flags |= IEEE80211_TX_CTL_AMPDU;
6181 }
6182
6183 /* XXX-BZ info->control.rates for non-HW rate control and injected packets. */
6184 #ifdef __notyet__
6185 #ifdef LKPI_80211_HT
6186 info->control.rts_cts_rate_idx=
6187 info->control.use_rts= /* RTS */
6188 info->control.use_cts_prot= /* RTS/CTS*/
6189 #endif
6190 #endif
6191
6192 sta = LSTA_TO_STA(lsta);
6193 #ifdef LKPI_80211_HW_CRYPTO
6194 if (lkpi_hwcrypto && keyix != IEEE80211_KEYIX_NONE) {
6195 int error;
6196
6197 error = lkpi_hw_crypto_prepare(lsta, k, skb);
6198 if (error != 0) {
6199 /*
6200 * We only have to free the skb which will free the
6201 * mbuf and release the reference on the ni.
6202 */
6203 dev_kfree_skb(skb);
6204 return;
6205 }
6206 /* Reset header as data might have moved. */
6207 hdr = (void *)skb->data;
6208 }
6209 #endif
6210
6211 IMPROVE();
6212
6213 ltxq = NULL;
6214 if (!ieee80211_is_data_present(hdr->frame_control)) {
6215 if (vif->type == NL80211_IFTYPE_STATION &&
6216 lsta->added_to_drv &&
6217 sta->txq[IEEE80211_NUM_TIDS] != NULL)
6218 ltxq = TXQ_TO_LTXQ(sta->txq[IEEE80211_NUM_TIDS]);
6219 } else if (lsta->added_to_drv &&
6220 sta->txq[skb->priority] != NULL) {
6221 ltxq = TXQ_TO_LTXQ(sta->txq[skb->priority]);
6222 }
6223 if (ltxq == NULL)
6224 goto ops_tx;
6225
6226 KASSERT(ltxq != NULL, ("%s: lsta %p sta %p m %p skb %p "
6227 "ltxq %p != NULL\n", __func__, lsta, sta, m, skb, ltxq));
6228
6229 LKPI_80211_LTXQ_LOCK(ltxq);
6230 skb_queue_tail(<xq->skbq, skb);
6231 ltxq->frms_enqueued++;
6232 #ifdef LINUXKPI_DEBUG_80211
6233 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6234 printf("%s:%d mo_wake_tx_queue :: %d %lu lsta %p sta %p "
6235 "ni %p %6D skb %p lxtq %p { qlen %u, ac %d tid %u } "
6236 "WAKE_TX_Q ac %d prio %u qmap %u\n",
6237 __func__, __LINE__,
6238 curthread->td_tid, jiffies,
6239 lsta, sta, ni, ni->ni_macaddr, ":", skb, ltxq,
6240 skb_queue_len(<xq->skbq), ltxq->txq.ac,
6241 ltxq->txq.tid, ac, skb->priority, skb->qmap);
6242 #endif
6243 LKPI_80211_LTXQ_UNLOCK(ltxq);
6244 wiphy_lock(hw->wiphy);
6245 lkpi_80211_mo_wake_tx_queue(hw, <xq->txq, true);
6246 wiphy_unlock(hw->wiphy);
6247 return;
6248
6249 ops_tx:
6250 #ifdef LINUXKPI_DEBUG_80211
6251 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6252 printf("%s:%d mo_tx :: lsta %p { added_to_drv %d } sta %p "
6253 "ni %p %6D skb %p TX ac %d prio %u qmap %u\n",
6254 __func__, __LINE__, lsta, lsta->added_to_drv, sta,
6255 ni, ni->ni_macaddr, ":", skb, ac, skb->priority, skb->qmap);
6256 #endif
6257 memset(&control, 0, sizeof(control));
6258 if (lsta->added_to_drv)
6259 control.sta = sta;
6260 wiphy_lock(hw->wiphy);
6261 lkpi_80211_mo_tx(hw, &control, skb);
6262 lsta->frms_tx++;
6263 wiphy_unlock(hw->wiphy);
6264 }
6265
6266 static void
6267 lkpi_80211_txq_task(void *ctx, int pending)
6268 {
6269 struct lkpi_sta *lsta;
6270 struct mbufq mq;
6271 struct mbuf *m;
6272 bool shall_tx;
6273
6274 lsta = ctx;
6275
6276 #ifdef LINUXKPI_DEBUG_80211
6277 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
6278 printf("%s:%d lsta %p ni %p %6D pending %d mbuf_qlen %d\n",
6279 __func__, __LINE__, lsta, lsta->ni, lsta->ni->ni_macaddr, ":",
6280 pending, mbufq_len(&lsta->txq));
6281 #endif
6282
6283 mbufq_init(&mq, IFQ_MAXLEN);
6284
6285 LKPI_80211_LSTA_TXQ_LOCK(lsta);
6286 /*
6287 * Do not re-check lsta->txq_ready here; we may have a pending
6288 * disassoc/deauth frame still. On the contrary if txq_ready is
6289 * false we do not have a valid sta anymore in the firmware so no
6290 * point to try to TX.
6291 * We also use txq_ready as a semaphore and will drain the txq manually
6292 * if needed on our way towards SCAN/INIT in the state machine.
6293 */
6294 #if 0
6295 shall_tx = lsta->added_to_drv && lsta->txq_ready;
6296 #else
6297 /*
6298 * Backout this part of 886653492945f which breaks rtw88 or
6299 * in general drivers without (*sta_state)() but only the
6300 * legacy fallback to (*sta_add)().
6301 */
6302 shall_tx = lsta->txq_ready;
6303 #endif
6304 if (__predict_true(shall_tx))
6305 mbufq_concat(&mq, &lsta->txq);
6306 /*
6307 * else a state change will push the packets out manually or
6308 * lkpi_lsta_free() will drain the lsta->txq and free the mbufs.
6309 */
6310 LKPI_80211_LSTA_TXQ_UNLOCK(lsta);
6311
6312 m = mbufq_dequeue(&mq);
6313 while (m != NULL) {
6314 lkpi_80211_txq_tx_one(lsta, m);
6315 m = mbufq_dequeue(&mq);
6316 }
6317 }
6318
6319 static int
6320 lkpi_ic_transmit(struct ieee80211com *ic, struct mbuf *m)
6321 {
6322
6323 /* XXX TODO */
6324 IMPROVE();
6325
6326 /* Quick and dirty cheating hack. */
6327 struct ieee80211_node *ni;
6328
6329 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif;
6330 return (lkpi_xmit(ni, m, NULL, false));
6331 }
6332
6333 #ifdef LKPI_80211_HT
6334 static int
6335 lkpi_ic_recv_action(struct ieee80211_node *ni, const struct ieee80211_frame *wh,
6336 const uint8_t *frm, const uint8_t *efrm)
6337 {
6338 struct ieee80211com *ic;
6339 struct lkpi_hw *lhw;
6340
6341 ic = ni->ni_ic;
6342 lhw = ic->ic_softc;
6343
6344 TRACEOK("recv_action called");
6345
6346 return (lhw->ic_recv_action(ni, wh, frm, efrm));
6347 }
6348
6349 static int
6350 lkpi_ic_send_action(struct ieee80211_node *ni, int category, int action, void *sa)
6351 {
6352 struct ieee80211com *ic;
6353 struct lkpi_hw *lhw;
6354
6355 ic = ni->ni_ic;
6356 lhw = ic->ic_softc;
6357
6358 TRACEOK("send_action with action %d called", action);
6359
6360 return (lhw->ic_send_action(ni, category, action, sa));
6361 }
6362
6363
6364 static int
6365 lkpi_ic_ampdu_enable(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
6366 {
6367 struct ieee80211com *ic;
6368 struct lkpi_hw *lhw;
6369
6370 ic = ni->ni_ic;
6371 lhw = ic->ic_softc;
6372
6373 TRACEOK("ieee80211_ampdu_enable called");
6374
6375 return (lhw->ic_ampdu_enable(ni, tap));
6376 }
6377
6378 /*
6379 * (*ic_addba_request)() is called by ieee80211_ampdu_request() before
6380 * calling send_action(CAT_BA, BA_ADDBA_REQUEST).
6381 *
6382 * NB: returns 0 on ERROR!
6383 */
6384 static int
6385 lkpi_ic_addba_request(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
6386 int dialogtoken, int baparamset, int batimeout)
6387 {
6388 struct ieee80211com *ic;
6389 struct lkpi_hw *lhw;
6390 struct ieee80211_hw *hw;
6391 struct ieee80211vap *vap;
6392 struct lkpi_vif *lvif;
6393 struct ieee80211_vif *vif;
6394 struct lkpi_sta *lsta;
6395 struct ieee80211_sta *sta;
6396 struct ieee80211_ampdu_params params = { };
6397 int error;
6398
6399 ic = ni->ni_ic;
6400 lhw = ic->ic_softc;
6401 hw = LHW_TO_HW(lhw);
6402 vap = ni->ni_vap;
6403 lvif = VAP_TO_LVIF(vap);
6404 vif = LVIF_TO_VIF(lvif);
6405 lsta = ni->ni_drv_data;
6406 sta = LSTA_TO_STA(lsta);
6407
6408 TRACEOK("ADDBA REQ tid %u", tap->txa_tid);
6409
6410 if (!lsta->added_to_drv) {
6411 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
6412 __func__, lsta, ni, sta);
6413 return (0);
6414 }
6415
6416 params.sta = sta;
6417 params.action = IEEE80211_AMPDU_TX_START;
6418 /* Keep 0 here! */
6419 params.buf_size = 0;
6420 params.timeout = 0;
6421 params.ssn = tap->txa_start & (IEEE80211_SEQ_RANGE-1);
6422 params.tid = tap->txa_tid;
6423 params.amsdu = false;
6424
6425 /* We get called from if_transmit all the way up unlocked in net80211. */
6426 IEEE80211_UNLOCK_ASSERT(ic);
6427 wiphy_lock(hw->wiphy);
6428 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6429 wiphy_unlock(hw->wiphy);
6430 if (error == IEEE80211_AMPDU_TX_START_IMMEDIATE) {
6431 ic_printf(ic, "%s: mo_ampdu_action returned AMPDU_TX_START_IMMEDIATE. "
6432 "ni %p tap %p\n", __func__, ni, tap);
6433 } else if (error != 0) {
6434 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
6435 __func__, error, ni, tap);
6436 return (0);
6437 }
6438
6439 if (sta->txq[tap->txa_tid] != NULL) {
6440 struct lkpi_txq *ltxq;
6441
6442 ltxq = TXQ_TO_LTXQ(sta->txq[tap->txa_tid]);
6443 TRACEOK("ADDBA REQ ltxq tid %u flags %b qlen %d", tap->txa_tid,
6444 ltxq->flags, LKPI_TXQ_FLAGS_BITS, skb_queue_len(<xq->skbq));
6445 ltxq->flags |= LKPI_TXQ_STOPPED_BA;
6446 }
6447
6448 return (lhw->ic_addba_request(ni, tap, dialogtoken, baparamset, batimeout));
6449 }
6450
6451 /*
6452 * (*ic_addba_response)() is called from ht_recv_action_ba_addba_response()
6453 * and calls the default ieee80211_addba_response() which always returns 1.
6454 *
6455 * NB: No error checking in net80211!
6456 * Staying with 0 is an error.
6457 */
6458 static int
6459 lkpi_ic_addba_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
6460 int status, int baparamset, int batimeout)
6461 {
6462 struct ieee80211com *ic;
6463 struct lkpi_hw *lhw;
6464 struct ieee80211_hw *hw;
6465 struct ieee80211vap *vap;
6466 struct lkpi_vif *lvif;
6467 struct ieee80211_vif *vif;
6468 struct lkpi_sta *lsta;
6469 struct ieee80211_sta *sta;
6470 struct ieee80211_ampdu_params params = { };
6471 int error;
6472
6473 ic = ni->ni_ic;
6474 lhw = ic->ic_softc;
6475 hw = LHW_TO_HW(lhw);
6476 vap = ni->ni_vap;
6477 lvif = VAP_TO_LVIF(vap);
6478 vif = LVIF_TO_VIF(lvif);
6479 lsta = ni->ni_drv_data;
6480 sta = LSTA_TO_STA(lsta);
6481
6482 TRACEOK("ADDBA RESP status %d (0 == SUCCESS) tid %u", status, tap->txa_tid);
6483
6484 if (!lsta->added_to_drv) {
6485 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
6486 __func__, lsta, ni, sta);
6487 return (0);
6488 }
6489
6490 if (status == IEEE80211_STATUS_SUCCESS) {
6491 params.sta = sta;
6492 params.action = IEEE80211_AMPDU_TX_OPERATIONAL;
6493 params.buf_size = tap->txa_wnd;
6494 params.timeout = 0;
6495 params.ssn = 0;
6496 params.tid = tap->txa_tid;
6497 if ((tap->txa_flags & IEEE80211_AGGR_AMSDU) != 0)
6498 params.amsdu = true;
6499 else
6500 params.amsdu = false;
6501 } else {
6502 /* We need to free the allocated resources. */
6503 params.sta = sta;
6504 switch (status) {
6505 /* params.action = FLUSH, FLUSH_CONT */
6506 default:
6507 params.action = IEEE80211_AMPDU_TX_STOP_CONT;
6508 break;
6509 }
6510 params.buf_size = 0;
6511 params.timeout = 0;
6512 params.ssn = 0;
6513 params.tid = tap->txa_tid;
6514 params.amsdu = false;
6515 }
6516
6517 /* We are called all they way up from ieee80211_input* without lock. */
6518 wiphy_lock(hw->wiphy);
6519 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6520 wiphy_unlock(hw->wiphy);
6521 if (error != 0) {
6522 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
6523 __func__, error, ni, tap);
6524 return (0);
6525 }
6526
6527 if (sta->txq[tap->txa_tid] != NULL) {
6528 struct lkpi_txq *ltxq;
6529
6530 ltxq = TXQ_TO_LTXQ(sta->txq[tap->txa_tid]);
6531 TRACEOK("ADDBA RESP ltxq tid %u flags %b qlen %d", tap->txa_tid,
6532 ltxq->flags, LKPI_TXQ_FLAGS_BITS, skb_queue_len(<xq->skbq));
6533 ltxq->flags &= ~LKPI_TXQ_STOPPED_BA;
6534
6535 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tap->txa_tid], true);
6536 }
6537
6538 IMPROVE_HT("who unleashes the TXQ? and when?, do we need to ni->ni_txseqs[tid] = tap->txa_start & 0xfff;");
6539
6540 return (lhw->ic_addba_response(ni, tap, status, baparamset, batimeout));
6541 }
6542
6543 /*
6544 * (*ic_addba_stop)() is called from ampdu_tx_stop(), ht_recv_action_ba_delba(),
6545 * and ieee80211_ampdu_stop() and calls the default ieee80211_addba_stop().
6546 */
6547 static void
6548 lkpi_ic_addba_stop(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
6549 {
6550 struct ieee80211com *ic;
6551 struct lkpi_hw *lhw;
6552 struct ieee80211_hw *hw;
6553 struct ieee80211vap *vap;
6554 struct lkpi_vif *lvif;
6555 struct ieee80211_vif *vif;
6556 struct lkpi_sta *lsta;
6557 struct ieee80211_sta *sta;
6558 struct ieee80211_ampdu_params params = { };
6559 int error;
6560
6561 ic = ni->ni_ic;
6562 lhw = ic->ic_softc;
6563 hw = LHW_TO_HW(lhw);
6564 vap = ni->ni_vap;
6565 lvif = VAP_TO_LVIF(vap);
6566 vif = LVIF_TO_VIF(lvif);
6567 lsta = ni->ni_drv_data;
6568 sta = LSTA_TO_STA(lsta);
6569
6570 if (!lsta->added_to_drv) {
6571 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
6572 __func__, lsta, ni, sta);
6573 goto n80211;
6574 }
6575
6576 /* We need to free the allocated resources. */
6577 params.sta = sta;
6578 IMPROVE("net80211 does not provide a reason to us");
6579 params.action = IEEE80211_AMPDU_TX_STOP_CONT; /* params.action = FLUSH, FLUSH_CONT */
6580 params.buf_size = 0;
6581 params.timeout = 0;
6582 params.ssn = 0;
6583 params.tid = tap->txa_tid;
6584 params.amsdu = false;
6585
6586 IEEE80211_UNLOCK(ic);
6587 wiphy_lock(hw->wiphy);
6588 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6589 wiphy_unlock(hw->wiphy);
6590 IEEE80211_LOCK(ic);
6591 if (error != 0) {
6592 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p tap %p\n",
6593 __func__, error, ni, tap);
6594 goto n80211;
6595 }
6596
6597 IMPROVE_HT("anyting else?");
6598
6599 n80211:
6600 lhw->ic_addba_stop(ni, tap);
6601 }
6602
6603 static void
6604 lkpi_ic_addba_response_timeout(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap)
6605 {
6606 struct ieee80211com *ic;
6607 struct lkpi_hw *lhw;
6608 struct lkpi_sta *lsta;
6609 struct ieee80211_sta *sta;
6610
6611 ic = ni->ni_ic;
6612 lhw = ic->ic_softc;
6613 lsta = ni->ni_drv_data;
6614 sta = LSTA_TO_STA(lsta);
6615
6616 TRACEOK("ADDBA RESP TIMEO tid %u", tap->txa_tid);
6617
6618 IMPROVE_HT();
6619
6620 if (!lsta->added_to_drv) {
6621 ic_printf(ic, "%s: lsta %p ni %p, sta %p not added to firmware\n",
6622 __func__, lsta, ni, sta);
6623 goto n80211;
6624 }
6625
6626 /* We need to re-enable the txq and get packets out. */
6627 if (sta->txq[tap->txa_tid] != NULL) {
6628 struct lkpi_txq *ltxq;
6629 struct ieee80211_hw *hw;
6630
6631 ltxq = TXQ_TO_LTXQ(sta->txq[tap->txa_tid]);
6632 TRACEOK("ADDBA RESP TIMEO ltxq tid %u flags %b qlen %d",
6633 tap->txa_tid, ltxq->flags, LKPI_TXQ_FLAGS_BITS,
6634 skb_queue_len(<xq->skbq));
6635 ltxq->flags &= ~LKPI_TXQ_STOPPED_BA;
6636
6637 hw = LHW_TO_HW(lhw);
6638 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tap->txa_tid], true);
6639 }
6640
6641 n80211:
6642 lhw->ic_addba_response_timeout(ni, tap);
6643 }
6644
6645 static void
6646 lkpi_ic_bar_response(struct ieee80211_node *ni, struct ieee80211_tx_ampdu *tap,
6647 int status)
6648 {
6649 struct ieee80211com *ic;
6650 struct lkpi_hw *lhw;
6651
6652 ic = ni->ni_ic;
6653 lhw = ic->ic_softc;
6654
6655 IMPROVE_HT();
6656
6657 lhw->ic_bar_response(ni, tap, status);
6658 }
6659
6660 static int
6661 lkpi_ic_ampdu_rx_start(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap,
6662 int baparamset, int batimeout, int baseqctl)
6663 {
6664 struct ieee80211com *ic;
6665 struct lkpi_hw *lhw;
6666 struct ieee80211_hw *hw;
6667 struct ieee80211vap *vap;
6668 struct lkpi_vif *lvif;
6669 struct ieee80211_vif *vif;
6670 struct lkpi_sta *lsta;
6671 struct ieee80211_sta *sta;
6672 struct ieee80211_ampdu_params params = { };
6673 int error;
6674
6675 ic = ni->ni_ic;
6676 lhw = ic->ic_softc;
6677 hw = LHW_TO_HW(lhw);
6678 vap = ni->ni_vap;
6679 lvif = VAP_TO_LVIF(vap);
6680 vif = LVIF_TO_VIF(lvif);
6681 lsta = ni->ni_drv_data;
6682 sta = LSTA_TO_STA(lsta);
6683
6684 IEEE80211_UNLOCK_ASSERT(ic);
6685
6686 if (!lsta->added_to_drv) {
6687 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
6688 __func__, lsta, ni, vap, sta);
6689 return (-ENXIO);
6690 }
6691
6692 if (lsta->state != IEEE80211_STA_AUTHORIZED) {
6693 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
6694 __func__, lsta, ni, vap, sta, lsta->state);
6695 return (-ENXIO);
6696 }
6697
6698 params.sta = sta;
6699 params.action = IEEE80211_AMPDU_RX_START;
6700 params.buf_size = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_BUFSIZ);
6701 if (params.buf_size == 0)
6702 params.buf_size = IEEE80211_MAX_AMPDU_BUF_HT;
6703 else
6704 params.buf_size = min(params.buf_size, IEEE80211_MAX_AMPDU_BUF_HT);
6705 if (hw->max_rx_aggregation_subframes > 0 &&
6706 params.buf_size > hw->max_rx_aggregation_subframes)
6707 params.buf_size = hw->max_rx_aggregation_subframes;
6708 params.timeout = le16toh(batimeout);
6709 params.ssn = _IEEE80211_MASKSHIFT(le16toh(baseqctl), IEEE80211_BASEQ_START);
6710 params.tid = _IEEE80211_MASKSHIFT(le16toh(baparamset), IEEE80211_BAPS_TID);
6711
6712 /* Based on net80211::ampdu_rx_start(). */
6713 if ((vap->iv_htcaps & IEEE80211_HTC_RX_AMSDU_AMPDU) &&
6714 (_IEEE80211_MASKSHIFT(baparamset, IEEE80211_BAPS_AMSDU)))
6715 params.amsdu = true;
6716 else
6717 params.amsdu = false;
6718
6719 wiphy_lock(hw->wiphy);
6720 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6721 wiphy_unlock(hw->wiphy);
6722 if (error != 0) {
6723 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
6724 __func__, error, ni, rap);
6725 return (error);
6726 }
6727
6728 if (!ieee80211_hw_check(hw, SUPPORTS_REORDERING_BUFFER)) {
6729 IMPROVE("%s: TODO: SUPPORTS_REORDERING_BUFFER not set; check net80211\n", __func__);
6730 }
6731
6732 IMPROVE_HT("net80211 is missing the error check on return and assumes success");
6733
6734 error = lhw->ic_ampdu_rx_start(ni, rap, baparamset, batimeout, baseqctl);
6735 return (error);
6736 }
6737
6738 static void
6739 lkpi_ic_ampdu_rx_stop(struct ieee80211_node *ni, struct ieee80211_rx_ampdu *rap)
6740 {
6741 struct ieee80211com *ic;
6742 struct lkpi_hw *lhw;
6743 struct ieee80211_hw *hw;
6744 struct ieee80211vap *vap;
6745 struct lkpi_vif *lvif;
6746 struct ieee80211_vif *vif;
6747 struct lkpi_sta *lsta;
6748 struct ieee80211_sta *sta;
6749 struct ieee80211_ampdu_params params = { };
6750 int error;
6751 uint8_t tid;
6752 bool ic_locked;
6753
6754 ic = ni->ni_ic;
6755 lhw = ic->ic_softc;
6756
6757 /*
6758 * We should not (cannot) call into mac80211 ops with AMPDU_RX_STOP if
6759 * we did not START. Some drivers pass it down to firmware which will
6760 * simply barf and net80211 calls ieee80211_ht_node_cleanup() from
6761 * ieee80211_ht_node_init() amongst others which will iterate over all
6762 * tid and call ic_ampdu_rx_stop() unconditionally.
6763 * XXX net80211 should probably be more "gentle" in these cases and
6764 * track some state itself.
6765 */
6766 if ((rap->rxa_flags & IEEE80211_AGGR_RUNNING) == 0)
6767 goto net80211_only;
6768
6769 hw = LHW_TO_HW(lhw);
6770 vap = ni->ni_vap;
6771 lvif = VAP_TO_LVIF(vap);
6772 vif = LVIF_TO_VIF(lvif);
6773 lsta = ni->ni_drv_data;
6774 if (lsta == NULL) {
6775 ic_printf(ic, "%s: lsta %p ni %p vap %p, lsta is NULL\n",
6776 __func__, lsta, ni, vap);
6777 goto net80211_only;
6778 }
6779 sta = LSTA_TO_STA(lsta);
6780
6781 if (!lsta->added_to_drv) {
6782 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p not added to firmware\n",
6783 __func__, lsta, ni, vap, sta);
6784 goto net80211_only;
6785 }
6786
6787 if (lsta->state != IEEE80211_STA_AUTHORIZED) {
6788 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p state %d not AUTHORIZED\n",
6789 __func__, lsta, ni, vap, sta, lsta->state);
6790 goto net80211_only;
6791 }
6792
6793 IMPROVE_HT("This really should be passed from ht_recv_action_ba_delba.");
6794 for (tid = 0; tid < WME_NUM_TID; tid++) {
6795 if (&ni->ni_rx_ampdu[tid] == rap)
6796 break;
6797 }
6798 if (tid == WME_NUM_TID) {
6799 ic_printf(ic, "%s: lsta %p ni %p vap %p, sta %p TID not found\n",
6800 __func__, lsta, ni, vap, sta);
6801 goto net80211_only;
6802 }
6803
6804 params.sta = sta;
6805 params.action = IEEE80211_AMPDU_RX_STOP;
6806 params.buf_size = 0;
6807 params.timeout = 0;
6808 params.ssn = 0;
6809 params.tid = tid;
6810 params.amsdu = false;
6811
6812 ic_locked = IEEE80211_IS_LOCKED(ic);
6813 if (ic_locked)
6814 IEEE80211_UNLOCK(ic);
6815 wiphy_lock(hw->wiphy);
6816 error = lkpi_80211_mo_ampdu_action(hw, vif, ¶ms);
6817 wiphy_unlock(hw->wiphy);
6818 if (ic_locked)
6819 IEEE80211_LOCK(ic);
6820 if (error != 0)
6821 ic_printf(ic, "%s: mo_ampdu_action returned %d. ni %p rap %p\n",
6822 __func__, error, ni, rap);
6823
6824 net80211_only:
6825 lhw->ic_ampdu_rx_stop(ni, rap);
6826 }
6827 #endif
6828
6829 int
6830 linuxkpi_ieee80211_start_tx_ba_session(struct ieee80211_sta *sta, uint8_t tid,
6831 int timeout)
6832 {
6833 struct lkpi_sta *lsta;
6834 struct ieee80211_hw *hw;
6835 struct lkpi_hw *lhw;
6836 struct ieee80211_tx_ampdu *tap;
6837 int worked;
6838
6839 lsta = STA_TO_LSTA(sta);
6840
6841 /* If tid is out of range, fail gracefully. */
6842 /* XXX-BZ are we limited to 8? */
6843 if (tid >= IEEE80211_NUM_TIDS) {
6844 net80211_vap_printf(lsta->ni->ni_vap, "%s: tid %u out of range "
6845 ">= %u\n", __func__, tid, IEEE80211_NUM_TIDS);
6846 return (-EINVAL);
6847 }
6848
6849 hw = lsta->hw;
6850 lhw = HW_TO_LHW(hw);
6851
6852 /* No ampdu_action support, just error. */
6853 if (lhw->ops->ampdu_action == NULL) {
6854 net80211_vap_printf(lsta->ni->ni_vap, "%s: (*ampdu_action) "
6855 "not supported\n", __func__);
6856 return (-ENOTSUPP);
6857 }
6858
6859 /* Does HW allow us to set this up? */
6860 if (!ieee80211_hw_check(hw, AMPDU_AGGREGATION)) {
6861 net80211_vap_printf(lsta->ni->ni_vap, "%s: !AMPDU_AGGREGATION\n",
6862 __func__);
6863 return (-ENOTSUPP);
6864 }
6865 if (ieee80211_hw_check(hw, TX_AMPDU_SETUP_IN_HW)) {
6866 net80211_vap_printf(lsta->ni->ni_vap, "%s: TX_AMPDU_SETUP_IN_HW\n",
6867 __func__);
6868 return (-EPERM);
6869 }
6870
6871 /* We need at least HT or higher support enabled. */
6872 if (!sta->deflink.ht_cap.ht_supported &&
6873 !sta->deflink.vht_cap.vht_supported &&
6874 !sta->deflink.he_cap.has_he &&
6875 !sta->deflink.eht_cap.has_eht) {
6876 net80211_vap_printf(lsta->ni->ni_vap, "%s: HT or later not "
6877 "supported\n", __func__);
6878 return (-EINVAL);
6879 }
6880
6881 #ifdef __notyet__
6882 /*
6883 * We need some rate limiting/disabling in case we try too hard and
6884 * get NACKed over and over.
6885 * XXX-BZ This check should likely go to addba_req along with a counter.
6886 */
6887 if (lsta->block_ba)
6888 return (-EACCESS);
6889 #endif
6890
6891 /* XXX-BZ locking? */
6892
6893 /* Do we have a running session already? */
6894 tap = &lsta->ni->ni_tx_ampdu[tid];
6895 if (IEEE80211_AMPDU_REQUESTED(tap)) {
6896 net80211_vap_printf(lsta->ni->ni_vap, "%s: "
6897 "AMPDU requested/running\n", __func__);
6898 return (-EINPROGRESS);
6899 }
6900
6901 /* Tell net80211 to setup an aggr sessions. */
6902 /* XXX-BZ we have no way to carry the timeout forward easily. */
6903 worked = ieee80211_ampdu_tx_request_ext(lsta->ni, tid);
6904 TRACEOK("ieee80211_ampdu_tx_request_ext %d", worked);
6905
6906 if (worked != 1) {
6907 net80211_vap_printf(lsta->ni->ni_vap, "%s: "
6908 "ieee80211_ampdu_tx_request_ext returned %d != 1\n",
6909 __func__, worked);
6910 return (-EINVAL);
6911 }
6912
6913 /*
6914 * How do we make sure the EAPOL handshake has completed?
6915 * Let ieee80211_output do it.
6916 */
6917 if (1) {
6918 /* Immediately trigger the setup and output of the action frame. */
6919 worked = ieee80211_ampdu_request(lsta->ni, tap);
6920 if (worked != 1) {
6921 net80211_vap_printf(lsta->ni->ni_vap, "%s: "
6922 "ieee80211_ampdu_request returned %d != 1\n",
6923 __func__, worked);
6924 return (-EAGAIN);
6925 }
6926 }
6927
6928 return (0);
6929 }
6930
6931 static void
6932 lkpi_ic_getradiocaps_ht(struct ieee80211com *ic, struct ieee80211_hw *hw,
6933 uint8_t *bands, int *chan_flags, enum nl80211_band band)
6934 {
6935 #ifdef LKPI_80211_HT
6936 struct ieee80211_sta_ht_cap *ht_cap;
6937
6938 ht_cap = &hw->wiphy->bands[band]->ht_cap;
6939 if (!ht_cap->ht_supported)
6940 return;
6941
6942 switch (band) {
6943 case NL80211_BAND_2GHZ:
6944 setbit(bands, IEEE80211_MODE_11NG);
6945 break;
6946 case NL80211_BAND_5GHZ:
6947 setbit(bands, IEEE80211_MODE_11NA);
6948 break;
6949 default:
6950 IMPROVE("Unsupported band %d", band);
6951 return;
6952 }
6953
6954 ic->ic_htcaps = IEEE80211_HTC_HT; /* HT operation */
6955
6956 /*
6957 * Rather than manually checking each flag and
6958 * translating IEEE80211_HT_CAP_ to IEEE80211_HTCAP_,
6959 * simply copy the 16bits.
6960 */
6961 ic->ic_htcaps |= ht_cap->cap;
6962
6963 /* Then deal with the other flags. */
6964 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
6965 ic->ic_htcaps |= IEEE80211_HTC_AMPDU;
6966 #ifdef __notyet__
6967 if (ieee80211_hw_check(hw, TX_AMSDU))
6968 ic->ic_htcaps |= IEEE80211_HTC_AMSDU;
6969 if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
6970 ic->ic_htcaps |= (IEEE80211_HTC_RX_AMSDU_AMPDU |
6971 IEEE80211_HTC_TX_AMSDU_AMPDU);
6972 #endif
6973
6974 IMPROVE("PS, ampdu_*, ht_cap.mcs.tx_params, ...");
6975
6976 /* Only add HT40 channels if supported. */
6977 if ((ic->ic_htcaps & IEEE80211_HTCAP_CHWIDTH40) != 0 &&
6978 chan_flags != NULL)
6979 *chan_flags |= NET80211_CBW_FLAG_HT40;
6980 #endif
6981 }
6982
6983 static void
6984 lkpi_ic_getradiocaps(struct ieee80211com *ic, int maxchan,
6985 int *n, struct ieee80211_channel *c)
6986 {
6987 struct lkpi_hw *lhw;
6988 struct ieee80211_hw *hw;
6989 struct linuxkpi_ieee80211_channel *channels;
6990 uint8_t bands[IEEE80211_MODE_BYTES];
6991 int chan_flags, error, i, nchans;
6992
6993 /* Channels */
6994 lhw = ic->ic_softc;
6995 hw = LHW_TO_HW(lhw);
6996
6997 /* NL80211_BAND_2GHZ */
6998 nchans = 0;
6999 if (hw->wiphy->bands[NL80211_BAND_2GHZ] != NULL)
7000 nchans = hw->wiphy->bands[NL80211_BAND_2GHZ]->n_channels;
7001 if (nchans > 0) {
7002 struct ieee80211_supported_band *supband;
7003
7004 memset(bands, 0, sizeof(bands));
7005 chan_flags = 0;
7006 setbit(bands, IEEE80211_MODE_11B);
7007
7008 /* Check for 11g (simplified). */
7009 supband = hw->wiphy->bands[NL80211_BAND_2GHZ];
7010 for (i = 0; i < supband->n_bitrates; i++) {
7011 if ((supband->bitrates[i].flags &
7012 IEEE80211_RATE_MANDATORY_G) != 0) {
7013 setbit(bands, IEEE80211_MODE_11G);
7014 break;
7015 }
7016 }
7017
7018 IMPROVE("the bitrates may have flags?");
7019
7020 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
7021 NL80211_BAND_2GHZ);
7022
7023 channels = supband->channels;
7024 for (i = 0; i < nchans && *n < maxchan; i++) {
7025 uint32_t nflags = 0;
7026 int cflags = chan_flags;
7027
7028 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
7029 ic_printf(ic, "%s: Skipping disabled chan "
7030 "[%u/%u/%#x]\n", __func__,
7031 channels[i].hw_value,
7032 channels[i].center_freq, channels[i].flags);
7033 continue;
7034 }
7035 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
7036 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
7037 if (channels[i].flags & IEEE80211_CHAN_RADAR)
7038 nflags |= IEEE80211_CHAN_DFS;
7039 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
7040 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
7041 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
7042 cflags &= ~NET80211_CBW_FLAG_VHT80;
7043 /* XXX how to map the remaining enum ieee80211_channel_flags? */
7044 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
7045 cflags &= ~NET80211_CBW_FLAG_HT40;
7046
7047 error = ieee80211_add_channel_cbw(c, maxchan, n,
7048 ieee80211_mhz2ieee(channels[i].center_freq,
7049 lkpi_nl80211_band_to_net80211_band(channels[i].band)),
7050 channels[i].center_freq, channels[i].max_power,
7051 nflags, bands, cflags);
7052 /* net80211::ENOBUFS: *n >= maxchans */
7053 if (error != 0 && error != ENOBUFS)
7054 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
7055 "returned error %d\n",
7056 __func__, channels[i].hw_value,
7057 channels[i].center_freq, channels[i].flags,
7058 nflags, chan_flags, cflags, error);
7059 if (error != 0)
7060 break;
7061 }
7062 }
7063
7064 /* NL80211_BAND_5GHZ */
7065 nchans = 0;
7066 if (hw->wiphy->bands[NL80211_BAND_5GHZ] != NULL)
7067 nchans = hw->wiphy->bands[NL80211_BAND_5GHZ]->n_channels;
7068 if (nchans > 0) {
7069 memset(bands, 0, sizeof(bands));
7070 chan_flags = 0;
7071 setbit(bands, IEEE80211_MODE_11A);
7072
7073 lkpi_ic_getradiocaps_ht(ic, hw, bands, &chan_flags,
7074 NL80211_BAND_5GHZ);
7075
7076 #ifdef LKPI_80211_VHT
7077 if (hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_supported) {
7078
7079 ic->ic_flags_ext |= IEEE80211_FEXT_VHT;
7080 ic->ic_vht_cap.vht_cap_info =
7081 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.cap;
7082 ic->ic_vht_cap.supp_mcs =
7083 hw->wiphy->bands[NL80211_BAND_5GHZ]->vht_cap.vht_mcs;
7084
7085 setbit(bands, IEEE80211_MODE_VHT_5GHZ);
7086 chan_flags |= NET80211_CBW_FLAG_VHT80;
7087 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160MHZ(
7088 ic->ic_vht_cap.vht_cap_info))
7089 chan_flags |= NET80211_CBW_FLAG_VHT160;
7090 if (IEEE80211_VHTCAP_SUPP_CHAN_WIDTH_IS_160_80P80MHZ(
7091 ic->ic_vht_cap.vht_cap_info))
7092 chan_flags |= NET80211_CBW_FLAG_VHT80P80;
7093 }
7094 #endif
7095
7096 channels = hw->wiphy->bands[NL80211_BAND_5GHZ]->channels;
7097 for (i = 0; i < nchans && *n < maxchan; i++) {
7098 uint32_t nflags = 0;
7099 int cflags = chan_flags;
7100
7101 if (channels[i].flags & IEEE80211_CHAN_DISABLED) {
7102 ic_printf(ic, "%s: Skipping disabled chan "
7103 "[%u/%u/%#x]\n", __func__,
7104 channels[i].hw_value,
7105 channels[i].center_freq, channels[i].flags);
7106 continue;
7107 }
7108 if (channels[i].flags & IEEE80211_CHAN_NO_IR)
7109 nflags |= (IEEE80211_CHAN_NOADHOC|IEEE80211_CHAN_PASSIVE);
7110 if (channels[i].flags & IEEE80211_CHAN_RADAR)
7111 nflags |= IEEE80211_CHAN_DFS;
7112 if (channels[i].flags & IEEE80211_CHAN_NO_160MHZ)
7113 cflags &= ~(NET80211_CBW_FLAG_VHT160|NET80211_CBW_FLAG_VHT80P80);
7114 if (channels[i].flags & IEEE80211_CHAN_NO_80MHZ)
7115 cflags &= ~NET80211_CBW_FLAG_VHT80;
7116 /* XXX hwo to map the remaining enum ieee80211_channel_flags? */
7117 if (channels[i].flags & IEEE80211_CHAN_NO_HT40)
7118 cflags &= ~NET80211_CBW_FLAG_HT40;
7119
7120 error = ieee80211_add_channel_cbw(c, maxchan, n,
7121 ieee80211_mhz2ieee(channels[i].center_freq,
7122 lkpi_nl80211_band_to_net80211_band(channels[i].band)),
7123 channels[i].center_freq, channels[i].max_power,
7124 nflags, bands, cflags);
7125 /* net80211::ENOBUFS: *n >= maxchans */
7126 if (error != 0 && error != ENOBUFS)
7127 ic_printf(ic, "%s: Adding chan %u/%u/%#x/%#x/%#x/%#x "
7128 "returned error %d\n",
7129 __func__, channels[i].hw_value,
7130 channels[i].center_freq, channels[i].flags,
7131 nflags, chan_flags, cflags, error);
7132 if (error != 0)
7133 break;
7134 }
7135 }
7136 }
7137
7138 static void *
7139 lkpi_ieee80211_ifalloc(void)
7140 {
7141 struct ieee80211com *ic;
7142
7143 ic = malloc(sizeof(*ic), M_LKPI80211, M_WAITOK | M_ZERO);
7144
7145 /* Setting these happens later when we have device information. */
7146 ic->ic_softc = NULL;
7147 ic->ic_name = "linuxkpi";
7148
7149 return (ic);
7150 }
7151
7152 struct ieee80211_hw *
7153 linuxkpi_ieee80211_alloc_hw(size_t priv_len, const struct ieee80211_ops *ops)
7154 {
7155 struct ieee80211_hw *hw;
7156 struct lkpi_hw *lhw;
7157 struct wiphy *wiphy;
7158 int ac;
7159 bool emuchanctx;
7160
7161 /*
7162 * Do certain checks before starting to allocate resources.
7163 * Store results in temporary variables.
7164 */
7165
7166 /* ac1d519c01ca introduced emulating chanctx changes. */
7167 emuchanctx = false;
7168 if (ops->add_chanctx == ieee80211_emulate_add_chanctx &&
7169 ops->change_chanctx == ieee80211_emulate_change_chanctx &&
7170 ops->remove_chanctx == ieee80211_emulate_remove_chanctx) {
7171 /*
7172 * If we emulate the chanctx ops, we must not have
7173 * assign_vif_chanctx and unassign_vif_chanctx.
7174 */
7175 if (ops->assign_vif_chanctx != NULL ||
7176 ops->unassign_vif_chanctx != NULL) {
7177 /* Fail gracefully. */
7178 printf("%s: emulate_chanctx but "
7179 "assign_vif_chanctx %p != NULL || "
7180 "unassign_vif_chanctx %p != NULL\n", __func__,
7181 ops->assign_vif_chanctx, ops->unassign_vif_chanctx);
7182 return (NULL);
7183 }
7184 emuchanctx = true;
7185 }
7186 if (!emuchanctx && (ops->add_chanctx == ieee80211_emulate_add_chanctx ||
7187 ops->change_chanctx == ieee80211_emulate_change_chanctx ||
7188 ops->remove_chanctx == ieee80211_emulate_remove_chanctx)) {
7189 printf("%s: not emulating chanctx changes but emulating "
7190 "function set: %d/%d/%d\n", __func__,
7191 ops->add_chanctx == ieee80211_emulate_add_chanctx,
7192 ops->change_chanctx == ieee80211_emulate_change_chanctx,
7193 ops->remove_chanctx == ieee80211_emulate_remove_chanctx);
7194 return (NULL);
7195 }
7196 if (!emuchanctx && (ops->add_chanctx == NULL || ops->change_chanctx == NULL ||
7197 ops->remove_chanctx == NULL || ops->assign_vif_chanctx == NULL ||
7198 ops->unassign_vif_chanctx == NULL)) {
7199 printf("%s: not all functions set for chanctx operations "
7200 "(emulating chanctx %d): %p/%p/%p %p/%p\n",
7201 __func__, emuchanctx,
7202 ops->add_chanctx, ops->change_chanctx, ops->remove_chanctx,
7203 ops->assign_vif_chanctx, ops->unassign_vif_chanctx);
7204 return (NULL);
7205 }
7206
7207 /* Get us and the driver data also allocated. */
7208 wiphy = wiphy_new(&linuxkpi_mac80211cfgops, sizeof(*lhw) + priv_len);
7209 if (wiphy == NULL)
7210 return (NULL);
7211
7212 lhw = wiphy_priv(wiphy);
7213 lhw->ops = ops;
7214
7215 LKPI_80211_LHW_SCAN_LOCK_INIT(lhw);
7216 LKPI_80211_LHW_TXQ_LOCK_INIT(lhw);
7217 spin_lock_init(&lhw->txq_lock);
7218 sx_init_flags(&lhw->lvif_sx, "lhw-lvif", SX_RECURSE | SX_DUPOK);
7219 LKPI_80211_LHW_MC_LOCK_INIT(lhw);
7220 TAILQ_INIT(&lhw->lvif_head);
7221 __hw_addr_init(&lhw->mc_list);
7222 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
7223 spin_lock_init(&lhw->txq_scheduled_lock[ac]);
7224 lhw->txq_generation[ac] = 1;
7225 TAILQ_INIT(&lhw->txq_scheduled[ac]);
7226 }
7227
7228 /* Chanctx_conf */
7229 INIT_LIST_HEAD(&lhw->lchanctx_list);
7230 INIT_LIST_HEAD(&lhw->lchanctx_list_reserved);
7231 lhw->emulate_chanctx = emuchanctx;
7232
7233 /* Deferred RX path. */
7234 LKPI_80211_LHW_RXQ_LOCK_INIT(lhw);
7235 TASK_INIT(&lhw->rxq_task, 0, lkpi_80211_lhw_rxq_task, lhw);
7236 mbufq_init(&lhw->rxq, 32 * NAPI_POLL_WEIGHT);
7237 lhw->rxq_stopped = false;
7238
7239 /*
7240 * XXX-BZ TODO make sure there is a "_null" function to all ops
7241 * not initialized.
7242 */
7243 hw = LHW_TO_HW(lhw);
7244 hw->wiphy = wiphy;
7245 hw->conf.flags |= IEEE80211_CONF_IDLE;
7246 hw->priv = (void *)(lhw + 1);
7247
7248 /* BSD Specific. */
7249 lhw->ic = lkpi_ieee80211_ifalloc();
7250
7251 if (lhw->emulate_chanctx)
7252 ic_printf(lhw->ic, "Using chanctx emulation.\n");
7253 IMPROVE();
7254
7255 return (hw);
7256 }
7257
7258 void
7259 linuxkpi_ieee80211_iffree(struct ieee80211_hw *hw)
7260 {
7261 struct lkpi_hw *lhw;
7262 struct mbuf *m;
7263 int ac;
7264
7265 lhw = HW_TO_LHW(hw);
7266 free(lhw->ic, M_LKPI80211);
7267 lhw->ic = NULL;
7268
7269 /*
7270 * Drain the deferred RX path.
7271 */
7272 LKPI_80211_LHW_RXQ_LOCK(lhw);
7273 lhw->rxq_stopped = true;
7274 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
7275
7276 /* Drain taskq, won't be restarted due to rxq_stopped being set. */
7277 while (taskqueue_cancel(taskqueue_thread, &lhw->rxq_task, NULL) != 0)
7278 taskqueue_drain(taskqueue_thread, &lhw->rxq_task);
7279
7280 /* Flush mbufq (make sure to release ni refs!). */
7281 m = mbufq_dequeue(&lhw->rxq);
7282 while (m != NULL) {
7283 #ifdef LKPI_80211_USE_MTAG
7284 struct m_tag *mtag;
7285
7286 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
7287 if (mtag != NULL) {
7288 struct lkpi_80211_tag_rxni *rxni;
7289
7290 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
7291 ieee80211_free_node(rxni->ni);
7292 }
7293 #else
7294 if (m->m_pkthdr.PH_loc.ptr != NULL) {
7295 struct ieee80211_node *ni;
7296
7297 ni = m->m_pkthdr.PH_loc.ptr;
7298 ieee80211_free_node(ni);
7299 }
7300 #endif
7301 m_freem(m);
7302 m = mbufq_dequeue(&lhw->rxq);
7303 }
7304 KASSERT(mbufq_empty(&lhw->rxq), ("%s: lhw %p has rxq len %d != 0\n",
7305 __func__, lhw, mbufq_len(&lhw->rxq)));
7306 LKPI_80211_LHW_RXQ_LOCK_DESTROY(lhw);
7307
7308 wiphy_lock(hw->wiphy);
7309 /* Chanctx_conf. */
7310 if (!list_empty_careful(&lhw->lchanctx_list)) {
7311 struct lkpi_chanctx *lchanctx, *next;
7312 struct ieee80211_chanctx_conf *chanctx_conf;
7313
7314 list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list, entry) {
7315 if (lchanctx->added_to_drv) {
7316 /* In reality we should panic? */
7317 chanctx_conf = &lchanctx->chanctx_conf;
7318 lkpi_80211_mo_remove_chanctx(hw, chanctx_conf);
7319 }
7320 list_del(&lchanctx->entry);
7321 /* No need to reset the lchanctx here as we will free it below. */
7322 list_add_rcu(&lchanctx->entry, &lhw->lchanctx_list_reserved);
7323 }
7324 }
7325 if (!list_empty_careful(&lhw->lchanctx_list_reserved)) {
7326 struct lkpi_chanctx *lchanctx, *next;
7327
7328 list_for_each_entry_safe(lchanctx, next, &lhw->lchanctx_list_reserved, entry) {
7329 list_del(&lchanctx->entry);
7330 if (lchanctx->added_to_drv)
7331 panic("%s: lchanctx %p on reserved list still added_to_drv\n",
7332 __func__, lchanctx);
7333 free(lchanctx, M_LKPI80211);
7334 }
7335 }
7336 wiphy_unlock(hw->wiphy);
7337
7338 LKPI_80211_LHW_MC_LOCK(lhw);
7339 lkpi_cleanup_mcast_list_locked(lhw);
7340 LKPI_80211_LHW_MC_UNLOCK(lhw);
7341
7342 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
7343 spin_lock_destroy(&lhw->txq_scheduled_lock[ac]);
7344
7345 /* Cleanup more of lhw here or in wiphy_free()? */
7346 spin_lock_destroy(&lhw->txq_lock);
7347 LKPI_80211_LHW_TXQ_LOCK_DESTROY(lhw);
7348 LKPI_80211_LHW_SCAN_LOCK_DESTROY(lhw);
7349 sx_destroy(&lhw->lvif_sx);
7350 LKPI_80211_LHW_MC_LOCK_DESTROY(lhw)
7351 IMPROVE();
7352 }
7353
7354 void
7355 linuxkpi_set_ieee80211_dev(struct ieee80211_hw *hw)
7356 {
7357 struct lkpi_hw *lhw;
7358 struct ieee80211com *ic;
7359 struct device *dev;
7360
7361 lhw = HW_TO_LHW(hw);
7362 ic = lhw->ic;
7363
7364 /* Save the backpointer from net80211 to LinuxKPI. */
7365 ic->ic_softc = lhw;
7366
7367 /*
7368 * Set a proper name before ieee80211_ifattach() if dev is set.
7369 * ath1xk also unset the dev so we need to check.
7370 * Also we will (ab)use this opportunity to register the
7371 * power management sub-children if thay exist (for suspend/resume).
7372 */
7373 dev = wiphy_dev(hw->wiphy);
7374 if (dev != NULL) {
7375 ic->ic_name = dev_name(dev);
7376 if (dev->bsddev != NULL) {
7377 bus_identify_children(dev->bsddev);
7378 bus_enumerate_hinted_children(dev->bsddev);
7379 bus_topo_lock();
7380 bus_attach_children(dev->bsddev);
7381 bus_topo_unlock();
7382 }
7383 } else {
7384 TODO("adjust arguments to still have the old dev or go through "
7385 "the hoops of getting the bsddev from hw and detach; "
7386 "or do in XXX; check ath1kx drivers");
7387 }
7388
7389 /* XXX-BZ do we also need to set wiphy name? */
7390 }
7391
7392 struct ieee80211_hw *
7393 linuxkpi_wiphy_to_ieee80211_hw(struct wiphy *wiphy)
7394 {
7395 struct lkpi_hw *lhw;
7396
7397 lhw = wiphy_priv(wiphy);
7398 return (LHW_TO_HW(lhw));
7399 }
7400
7401 static void
7402 lkpi_radiotap_attach(struct lkpi_hw *lhw)
7403 {
7404 struct ieee80211com *ic;
7405
7406 ic = lhw->ic;
7407 ieee80211_radiotap_attach(ic,
7408 &lhw->rtap_tx.wt_ihdr, sizeof(lhw->rtap_tx),
7409 LKPI_RTAP_TX_FLAGS_PRESENT,
7410 &lhw->rtap_rx.wr_ihdr, sizeof(lhw->rtap_rx),
7411 LKPI_RTAP_RX_FLAGS_PRESENT);
7412 }
7413
7414 int
7415 linuxkpi_ieee80211_ifattach(struct ieee80211_hw *hw)
7416 {
7417 struct ieee80211com *ic;
7418 struct lkpi_hw *lhw;
7419 int band, i;
7420
7421 lhw = HW_TO_LHW(hw);
7422 ic = lhw->ic;
7423
7424 /* We do it this late as wiphy->dev should be set for the name. */
7425 lhw->workq = alloc_ordered_workqueue(wiphy_name(hw->wiphy), 0);
7426 if (lhw->workq == NULL)
7427 return (-EAGAIN);
7428
7429 /* XXX-BZ figure this out how they count his... */
7430 if (!is_zero_ether_addr(hw->wiphy->perm_addr)) {
7431 IEEE80211_ADDR_COPY(ic->ic_macaddr,
7432 hw->wiphy->perm_addr);
7433 } else if (hw->wiphy->n_addresses > 0) {
7434 /* We take the first one. */
7435 IEEE80211_ADDR_COPY(ic->ic_macaddr,
7436 hw->wiphy->addresses[0].addr);
7437 } else {
7438 ic_printf(ic, "%s: warning, no hardware address!\n", __func__);
7439 }
7440
7441 #ifdef __not_yet__
7442 /* See comment in lkpi_80211_txq_tx_one(). */
7443 ic->ic_headroom = hw->extra_tx_headroom;
7444 #endif
7445
7446 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
7447 ic->ic_opmode = IEEE80211_M_STA;
7448
7449 /* Set device capabilities. */
7450 /* XXX-BZ we need to get these from linux80211/drivers and convert. */
7451 ic->ic_caps =
7452 IEEE80211_C_STA |
7453 IEEE80211_C_MONITOR |
7454 IEEE80211_C_WPA | /* WPA/RSN */
7455 #ifdef LKPI_80211_WME
7456 IEEE80211_C_WME |
7457 #endif
7458 #if 0
7459 IEEE80211_C_PMGT |
7460 #endif
7461 IEEE80211_C_SHSLOT | /* short slot time supported */
7462 IEEE80211_C_SHPREAMBLE /* short preamble supported */
7463 ;
7464
7465 #ifdef LKPI_80211_BGSCAN
7466 if (lhw->ops->hw_scan)
7467 ic->ic_caps |= IEEE80211_C_BGSCAN;
7468 #endif
7469
7470 lkpi_enable_hw_scan(lhw);
7471
7472 /* Does the driver/firmware handle rate countrol? */
7473 /* Currently only older iwlwifi mvm devices are in this category. */
7474 if (bootverbose && !ieee80211_hw_check(hw, HAS_RATE_CONTROL))
7475 ic_printf(ic, "NOTE: rate control not supported by LinuxKPI; "
7476 "expect low rates only\n");
7477
7478 /* Does HW support Fragmentation offload? */
7479 if (ieee80211_hw_check(hw, SUPPORTS_TX_FRAG))
7480 ic->ic_flags_ext |= IEEE80211_FEXT_FRAG_OFFLOAD;
7481
7482 /* Does HW support full AMPDU[-TX] offload? */
7483 if (ieee80211_hw_check(hw, AMPDU_AGGREGATION))
7484 ic->ic_flags_ext |= IEEE80211_FEXT_AMPDU_OFFLOAD;
7485 #ifdef __notyet__
7486 if (ieee80211_hw_check(hw, TX_AMSDU))
7487 if (ieee80211_hw_check(hw, SUPPORTS_AMSDU_IN_AMPDU))
7488 #endif
7489
7490 /*
7491 * The wiphy variables report bitmasks of avail antennas.
7492 * (*get_antenna) get the current bitmask sets which can be
7493 * altered by (*set_antenna) for some drivers.
7494 * XXX-BZ will the count alone do us much good long-term in net80211?
7495 */
7496 if (hw->wiphy->available_antennas_rx ||
7497 hw->wiphy->available_antennas_tx) {
7498 uint32_t rxs, txs;
7499
7500 if (lkpi_80211_mo_get_antenna(hw, &txs, &rxs) == 0) {
7501 ic->ic_rxstream = bitcount32(rxs);
7502 ic->ic_txstream = bitcount32(txs);
7503 }
7504 }
7505
7506 ic->ic_cryptocaps = 0;
7507 #ifdef LKPI_80211_HW_CRYPTO
7508 if (lkpi_hwcrypto && hw->wiphy->n_cipher_suites > 0) {
7509 uint32_t hwciphers;
7510
7511 hwciphers = 0;
7512 for (i = 0; i < hw->wiphy->n_cipher_suites; i++) {
7513 uint32_t cs;
7514
7515 cs = lkpi_l80211_to_net80211_cyphers(
7516 ic, hw->wiphy->cipher_suites[i]);
7517 if (cs == IEEE80211_CRYPTO_TKIP) {
7518 /*
7519 * We do set this here. We will only find out
7520 * when doing a SET_KEY operation depending on
7521 * what the driver returns.
7522 * net80211::ieee80211_crypto_newkey()
7523 * checks this so we will have to do flags
7524 * surgery later.
7525 */
7526 cs |= IEEE80211_CRYPTO_TKIPMIC;
7527 }
7528 hwciphers |= cs;
7529 }
7530 /*
7531 * (20250415) nothing anywhere in the path checks we actually
7532 * support all these in net80211.
7533 * net80211 supports _256 variants but the ioctl does not.
7534 */
7535 IMPROVE("as net80211 grows more support, enable them");
7536 hwciphers &= (IEEE80211_CRYPTO_WEP |
7537 IEEE80211_CRYPTO_TKIP | IEEE80211_CRYPTO_TKIPMIC |
7538 IEEE80211_CRYPTO_AES_CCM | IEEE80211_CRYPTO_AES_GCM_128);
7539 /*
7540 * We only support CCMP here, so further filter.
7541 * Also permit TKIP if turned on.
7542 */
7543 hwciphers &= (IEEE80211_CRYPTO_AES_CCM |
7544 IEEE80211_CRYPTO_AES_GCM_128 |
7545 (lkpi_hwcrypto_tkip ? (IEEE80211_CRYPTO_TKIP |
7546 IEEE80211_CRYPTO_TKIPMIC) : 0));
7547 ieee80211_set_hardware_ciphers(ic, hwciphers);
7548 }
7549 #endif
7550
7551 lkpi_ic_getradiocaps(ic, IEEE80211_CHAN_MAX, &ic->ic_nchans,
7552 ic->ic_channels);
7553
7554 ieee80211_ifattach(ic);
7555
7556 ic->ic_update_mcast = lkpi_ic_update_mcast;
7557 ic->ic_update_promisc = lkpi_ic_update_promisc;
7558 ic->ic_update_chw = lkpi_ic_update_chw;
7559 ic->ic_parent = lkpi_ic_parent;
7560 ic->ic_scan_start = lkpi_ic_scan_start;
7561 ic->ic_scan_end = lkpi_ic_scan_end;
7562 ic->ic_set_channel = lkpi_ic_set_channel;
7563 ic->ic_transmit = lkpi_ic_transmit;
7564 ic->ic_raw_xmit = lkpi_ic_raw_xmit;
7565 ic->ic_vap_create = lkpi_ic_vap_create;
7566 ic->ic_vap_delete = lkpi_ic_vap_delete;
7567 ic->ic_getradiocaps = lkpi_ic_getradiocaps;
7568 ic->ic_wme.wme_update = lkpi_ic_wme_update;
7569
7570 lhw->ic_scan_curchan = ic->ic_scan_curchan;
7571 ic->ic_scan_curchan = lkpi_ic_scan_curchan;
7572 lhw->ic_scan_mindwell = ic->ic_scan_mindwell;
7573 ic->ic_scan_mindwell = lkpi_ic_scan_mindwell;
7574
7575 lhw->ic_node_alloc = ic->ic_node_alloc;
7576 ic->ic_node_alloc = lkpi_ic_node_alloc;
7577 lhw->ic_node_init = ic->ic_node_init;
7578 ic->ic_node_init = lkpi_ic_node_init;
7579 lhw->ic_node_cleanup = ic->ic_node_cleanup;
7580 ic->ic_node_cleanup = lkpi_ic_node_cleanup;
7581 lhw->ic_node_free = ic->ic_node_free;
7582 ic->ic_node_free = lkpi_ic_node_free;
7583
7584 #ifdef LKPI_80211_HT
7585 /*
7586 * Only attach if the driver/firmware supports (*ampdu_action)().
7587 * Otherwise it is in the hands of net80211.
7588 */
7589 if (lhw->ops->ampdu_action != NULL) {
7590 lhw->ic_recv_action = ic->ic_recv_action;
7591 ic->ic_recv_action = lkpi_ic_recv_action;
7592 lhw->ic_send_action = ic->ic_send_action;
7593 ic->ic_send_action = lkpi_ic_send_action;
7594
7595 lhw->ic_ampdu_enable = ic->ic_ampdu_enable;
7596 ic->ic_ampdu_enable = lkpi_ic_ampdu_enable;
7597
7598 lhw->ic_addba_request = ic->ic_addba_request;
7599 ic->ic_addba_request = lkpi_ic_addba_request;
7600 lhw->ic_addba_response = ic->ic_addba_response;
7601 ic->ic_addba_response = lkpi_ic_addba_response;
7602 lhw->ic_addba_stop = ic->ic_addba_stop;
7603 ic->ic_addba_stop = lkpi_ic_addba_stop;
7604 lhw->ic_addba_response_timeout = ic->ic_addba_response_timeout;
7605 ic->ic_addba_response_timeout = lkpi_ic_addba_response_timeout;
7606
7607 lhw->ic_bar_response = ic->ic_bar_response;
7608 ic->ic_bar_response = lkpi_ic_bar_response;
7609
7610 lhw->ic_ampdu_rx_start = ic->ic_ampdu_rx_start;
7611 ic->ic_ampdu_rx_start = lkpi_ic_ampdu_rx_start;
7612 lhw->ic_ampdu_rx_stop = ic->ic_ampdu_rx_stop;
7613 ic->ic_ampdu_rx_stop = lkpi_ic_ampdu_rx_stop;
7614 }
7615 #endif
7616
7617 lkpi_radiotap_attach(lhw);
7618
7619 /*
7620 * Assign the first possible channel for now; seems Realtek drivers
7621 * expect one.
7622 * Also remember the amount of bands we support and the most rates
7623 * in any band so we can scale [(ext) sup rates] IE(s) accordingly.
7624 */
7625 lhw->supbands = lhw->max_rates = 0;
7626 for (band = 0; band < NUM_NL80211_BANDS; band++) {
7627 struct ieee80211_supported_band *supband;
7628 struct linuxkpi_ieee80211_channel *channels;
7629
7630 supband = hw->wiphy->bands[band];
7631 if (supband == NULL || supband->n_channels == 0)
7632 continue;
7633
7634 lhw->supbands++;
7635 lhw->max_rates = max(lhw->max_rates, supband->n_bitrates);
7636
7637 /* If we have a channel, we need to keep counting supbands. */
7638 if (hw->conf.chandef.chan != NULL)
7639 continue;
7640
7641 channels = supband->channels;
7642 for (i = 0; i < supband->n_channels; i++) {
7643
7644 if (channels[i].flags & IEEE80211_CHAN_DISABLED)
7645 continue;
7646
7647 cfg80211_chandef_create(&hw->conf.chandef, &channels[i],
7648 #ifdef LKPI_80211_HT
7649 (ic->ic_flags_ht & IEEE80211_FHT_HT) ? NL80211_CHAN_HT20 :
7650 #endif
7651 NL80211_CHAN_NO_HT);
7652 lhw->dflt_chandef = hw->conf.chandef;
7653 #ifdef LINUXKPI_DEBUG_80211
7654 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
7655 ic_printf(ic, "%s:%d: initialized "
7656 "hw->conf.chandef and dflt_chandef to %p\n",
7657 __func__, __LINE__, &lhw->dflt_chandef);
7658 #endif
7659 break;
7660 }
7661 }
7662
7663 IMPROVE("see net80211::ieee80211_chan_init vs. wiphy->bands[].bitrates possibly in lkpi_ic_getradiocaps?");
7664
7665 /* Make sure we do not support more than net80211 is willing to take. */
7666 if (lhw->max_rates > IEEE80211_RATE_MAXSIZE) {
7667 ic_printf(ic, "%s: limiting max_rates %d to %d!\n", __func__,
7668 lhw->max_rates, IEEE80211_RATE_MAXSIZE);
7669 lhw->max_rates = IEEE80211_RATE_MAXSIZE;
7670 }
7671
7672 /*
7673 * The maximum supported bitrates on any band + size for
7674 * DSSS Parameter Set give our per-band IE size.
7675 * SSID is the responsibility of the driver and goes on the side.
7676 * The user specified bits coming from the vap go into the
7677 * "common ies" fields.
7678 */
7679 lhw->scan_ie_len = 2 + IEEE80211_RATE_SIZE;
7680 if (lhw->max_rates > IEEE80211_RATE_SIZE)
7681 lhw->scan_ie_len += 2 + (lhw->max_rates - IEEE80211_RATE_SIZE);
7682
7683 if (hw->wiphy->features & NL80211_FEATURE_DS_PARAM_SET_IE_IN_PROBES) {
7684 /*
7685 * net80211 does not seem to support the DSSS Parameter Set but
7686 * some of the drivers insert it so calculate the extra fixed
7687 * space in.
7688 */
7689 lhw->scan_ie_len += 2 + 1;
7690 }
7691
7692 #if defined(LKPI_80211_HT)
7693 if ((ic->ic_htcaps & IEEE80211_HTC_HT) != 0)
7694 lhw->scan_ie_len += sizeof(struct ieee80211_ie_htcap);
7695 #endif
7696 #if defined(LKPI_80211_VHT)
7697 if (IEEE80211_CONF_VHT(ic))
7698 lhw->scan_ie_len += 2 + sizeof(struct ieee80211_vht_cap);
7699 #endif
7700
7701 /* Reduce the max_scan_ie_len "left" by the amount we consume already. */
7702 if (hw->wiphy->max_scan_ie_len > 0) {
7703 if (lhw->scan_ie_len > hw->wiphy->max_scan_ie_len)
7704 goto err;
7705 hw->wiphy->max_scan_ie_len -= lhw->scan_ie_len;
7706 }
7707
7708 if (bootverbose) {
7709 if (hw->netdev_features != 0)
7710 ic_printf(ic, "netdev_features %b\n",
7711 hw->netdev_features, NETIF_F_BITS);
7712 ieee80211_announce(ic);
7713 }
7714
7715 return (0);
7716 err:
7717 IMPROVE("TODO FIXME CLEANUP");
7718 return (-EAGAIN);
7719 }
7720
7721 void
7722 linuxkpi_ieee80211_ifdetach(struct ieee80211_hw *hw)
7723 {
7724 struct lkpi_hw *lhw;
7725 struct ieee80211com *ic;
7726
7727 lhw = HW_TO_LHW(hw);
7728 ic = lhw->ic;
7729 ieee80211_ifdetach(ic);
7730 }
7731
7732 void
7733 linuxkpi_ieee80211_iterate_interfaces(struct ieee80211_hw *hw,
7734 enum ieee80211_iface_iter flags,
7735 void(*iterfunc)(void *, uint8_t *, struct ieee80211_vif *),
7736 void *arg)
7737 {
7738 struct lkpi_hw *lhw;
7739 struct lkpi_vif *lvif;
7740 struct ieee80211_vif *vif;
7741 bool active, atomic, nin_drv;
7742
7743 lhw = HW_TO_LHW(hw);
7744
7745 if (flags & ~(IEEE80211_IFACE_ITER_NORMAL|
7746 IEEE80211_IFACE_ITER_RESUME_ALL|
7747 IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER|
7748 IEEE80211_IFACE_ITER_ACTIVE|IEEE80211_IFACE_ITER__ATOMIC|
7749 IEEE80211_IFACE_ITER__MTX)) {
7750 ic_printf(lhw->ic, "XXX TODO %s flags(%#x) not yet supported.\n",
7751 __func__, flags);
7752 }
7753
7754 if ((flags & IEEE80211_IFACE_ITER__MTX) != 0)
7755 lockdep_assert_wiphy(hw->wiphy);
7756
7757 active = (flags & IEEE80211_IFACE_ITER_ACTIVE) != 0;
7758 atomic = (flags & IEEE80211_IFACE_ITER__ATOMIC) != 0;
7759 nin_drv = (flags & IEEE80211_IFACE_SKIP_SDATA_NOT_IN_DRIVER) != 0;
7760
7761 if (atomic) {
7762 IMPROVE("LKPI_80211_LHW_LVIF_LOCK atomic assume to be rcu?");
7763 LKPI_80211_LHW_LVIF_LOCK(lhw);
7764 }
7765 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
7766 struct ieee80211vap *vap;
7767
7768 vif = LVIF_TO_VIF(lvif);
7769
7770 /*
7771 * If we want "active" interfaces, we need to distinguish on
7772 * whether the driver knows about them or not to be able to
7773 * handle the "resume" case correctly. Skip the ones the
7774 * driver does not know about.
7775 */
7776 if (active && !lvif->added_to_drv &&
7777 (flags & IEEE80211_IFACE_ITER_RESUME_ALL) != 0)
7778 continue;
7779
7780 /*
7781 * If we shall skip interfaces not added to the driver do so
7782 * if we haven't yet.
7783 */
7784 if (nin_drv && !lvif->added_to_drv)
7785 continue;
7786
7787 /*
7788 * Run the iterator function if we are either not asking
7789 * asking for active only or if the VAP is "running".
7790 */
7791 /* XXX-BZ probably should have state in the lvif as well. */
7792 vap = LVIF_TO_VAP(lvif);
7793 if (!active || (vap->iv_state != IEEE80211_S_INIT))
7794 iterfunc(arg, vif->addr, vif);
7795 }
7796 if (atomic)
7797 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
7798 }
7799
7800 static void
7801 lkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
7802 ieee80211_keyix keyix, struct lkpi_sta *lsta,
7803 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
7804 struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
7805 void *arg)
7806 {
7807 #ifdef LINUXKPI_DEBUG_80211
7808 if (linuxkpi_debug_80211 & D80211_TRACE_HW_CRYPTO)
7809 net80211_vap_printf(LVIF_TO_VAP(VIF_TO_LVIF(vif)),
7810 "%s:%d: lsta %6D added_to_drv %d kc[keyix %u] %p\n",
7811 __func__, __LINE__, LSTA_TO_STA(lsta)->addr, ":",
7812 lsta->added_to_drv, keyix, lsta->kc[keyix]);
7813 #endif
7814
7815 if (!lsta->added_to_drv)
7816 return;
7817
7818 if (lsta->kc[keyix] == NULL)
7819 return;
7820
7821 iterfunc(hw, vif, LSTA_TO_STA(lsta), lsta->kc[keyix], arg);
7822 }
7823
7824 void
7825 linuxkpi_ieee80211_iterate_keys(struct ieee80211_hw *hw,
7826 struct ieee80211_vif *vif,
7827 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_vif *,
7828 struct ieee80211_sta *, struct ieee80211_key_conf *, void *),
7829 void *arg, bool rcu)
7830 {
7831 struct lkpi_sta *lsta;
7832 struct lkpi_vif *lvif;
7833
7834 lvif = VIF_TO_LVIF(vif);
7835
7836 if (rcu) {
7837 rcu_read_lock_held(); /* XXX-BZ is this correct? */
7838
7839 if (vif == NULL) {
7840 TODO();
7841 } else {
7842 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7843 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
7844 keyix++)
7845 lkpi_ieee80211_iterate_keys(hw, vif,
7846 keyix, lsta, iterfunc, arg);
7847 }
7848 }
7849 } else {
7850 TODO("Used by suspend/resume; order of keys as installed to "
7851 "firmware is important; we'll need to rewrite some code for that");
7852 lockdep_assert_wiphy(hw->wiphy);
7853
7854 if (vif == NULL) {
7855 TODO();
7856 } else {
7857 list_for_each_entry(lsta, &lvif->lsta_list, lsta_list) {
7858 for (ieee80211_keyix keyix = 0; keyix < nitems(lsta->kc);
7859 keyix++)
7860 lkpi_ieee80211_iterate_keys(hw, vif,
7861 keyix, lsta, iterfunc, arg);
7862 }
7863 }
7864 }
7865 }
7866
7867 void
7868 linuxkpi_ieee80211_iterate_chan_contexts(struct ieee80211_hw *hw,
7869 void(*iterfunc)(struct ieee80211_hw *, struct ieee80211_chanctx_conf *,
7870 void *),
7871 void *arg)
7872 {
7873 struct lkpi_hw *lhw;
7874 struct lkpi_chanctx *lchanctx;
7875
7876 KASSERT(hw != NULL && iterfunc != NULL,
7877 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
7878
7879 lhw = HW_TO_LHW(hw);
7880
7881 rcu_read_lock();
7882 list_for_each_entry_rcu(lchanctx, &lhw->lchanctx_list, entry) {
7883 if (!lchanctx->added_to_drv)
7884 continue;
7885 iterfunc(hw, &lchanctx->chanctx_conf, arg);
7886 }
7887 rcu_read_unlock();
7888 }
7889
7890 void
7891 linuxkpi_ieee80211_iterate_stations_atomic(struct ieee80211_hw *hw,
7892 void (*iterfunc)(void *, struct ieee80211_sta *), void *arg)
7893 {
7894 struct lkpi_hw *lhw;
7895 struct lkpi_vif *lvif;
7896 struct lkpi_sta *lsta;
7897 struct ieee80211_sta *sta;
7898
7899 KASSERT(hw != NULL && iterfunc != NULL,
7900 ("%s: hw %p iterfunc %p arg %p\n", __func__, hw, iterfunc, arg));
7901
7902 lhw = HW_TO_LHW(hw);
7903
7904 LKPI_80211_LHW_LVIF_LOCK(lhw);
7905 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
7906
7907 rcu_read_lock();
7908 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
7909 if (!lsta->added_to_drv)
7910 continue;
7911 sta = LSTA_TO_STA(lsta);
7912 iterfunc(arg, sta);
7913 }
7914 rcu_read_unlock();
7915 }
7916 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
7917 }
7918
7919 struct linuxkpi_ieee80211_regdomain *
7920 lkpi_get_linuxkpi_ieee80211_regdomain(size_t n)
7921 {
7922 struct linuxkpi_ieee80211_regdomain *regd;
7923
7924 regd = kzalloc(sizeof(*regd) + n * sizeof(struct ieee80211_reg_rule),
7925 GFP_KERNEL);
7926 return (regd);
7927 }
7928
7929 int
7930 linuxkpi_regulatory_set_wiphy_regd_sync(struct wiphy *wiphy,
7931 struct linuxkpi_ieee80211_regdomain *regd)
7932 {
7933 struct lkpi_hw *lhw;
7934 struct ieee80211com *ic;
7935 struct ieee80211_regdomain *rd;
7936
7937 lhw = wiphy_priv(wiphy);
7938 ic = lhw->ic;
7939
7940 rd = &ic->ic_regdomain;
7941 if (rd->isocc[0] == '\0') {
7942 rd->isocc[0] = regd->alpha2[0];
7943 rd->isocc[1] = regd->alpha2[1];
7944 }
7945
7946 TODO();
7947 /* XXX-BZ finish the rest. */
7948
7949 return (0);
7950 }
7951
7952 void
7953 linuxkpi_ieee80211_scan_completed(struct ieee80211_hw *hw,
7954 struct cfg80211_scan_info *info)
7955 {
7956 struct lkpi_hw *lhw;
7957 struct ieee80211com *ic;
7958 struct ieee80211_scan_state *ss;
7959
7960 lhw = wiphy_priv(hw->wiphy);
7961 ic = lhw->ic;
7962 ss = ic->ic_scan;
7963
7964 TRACE_SCAN(ic, "scan_flags %b info { %ju, %6D, aborted %d }",
7965 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
7966 (uintmax_t)info->scan_start_tsf, info->tsf_bssid, ":",
7967 info->aborted);
7968
7969 ieee80211_scan_done(ss->ss_vap);
7970
7971 LKPI_80211_LHW_SCAN_LOCK(lhw);
7972 free(lhw->hw_req, M_LKPI80211);
7973 lhw->hw_req = NULL;
7974 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
7975 /* The wakeup(lhw) will be called from lkpi_ic_scan_end(). */
7976 /* wakeup(lhw); */
7977 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
7978
7979 return;
7980 }
7981
7982 static void
7983 lkpi_80211_lhw_rxq_rx_one(struct lkpi_hw *lhw, struct mbuf *m)
7984 {
7985 struct ieee80211_node *ni;
7986 #ifdef LKPI_80211_USE_MTAG
7987 struct m_tag *mtag;
7988 #endif
7989 int ok;
7990
7991 ni = NULL;
7992 #ifdef LKPI_80211_USE_MTAG
7993 mtag = m_tag_locate(m, MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI, NULL);
7994 if (mtag != NULL) {
7995 struct lkpi_80211_tag_rxni *rxni;
7996
7997 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
7998 ni = rxni->ni;
7999 }
8000 #else
8001 if (m->m_pkthdr.PH_loc.ptr != NULL) {
8002 ni = m->m_pkthdr.PH_loc.ptr;
8003 m->m_pkthdr.PH_loc.ptr = NULL;
8004 }
8005 #endif
8006
8007 if (ni != NULL) {
8008 ok = ieee80211_input_mimo(ni, m);
8009 ieee80211_free_node(ni); /* Release the reference. */
8010 if (ok < 0)
8011 m_freem(m);
8012 } else {
8013 ok = ieee80211_input_mimo_all(lhw->ic, m);
8014 /* mbuf got consumed. */
8015 }
8016
8017 #ifdef LINUXKPI_DEBUG_80211
8018 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8019 printf("TRACE-RX: %s: handled frame type %#0x\n", __func__, ok);
8020 #endif
8021 }
8022
8023 static void
8024 lkpi_80211_lhw_rxq_task(void *ctx, int pending)
8025 {
8026 struct lkpi_hw *lhw;
8027 struct mbufq mq;
8028 struct mbuf *m;
8029
8030 lhw = ctx;
8031
8032 #ifdef LINUXKPI_DEBUG_80211
8033 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8034 printf("TRACE-RX: %s: lhw %p pending %d mbuf_qlen %d\n",
8035 __func__, lhw, pending, mbufq_len(&lhw->rxq));
8036 #endif
8037
8038 mbufq_init(&mq, IFQ_MAXLEN);
8039
8040 LKPI_80211_LHW_RXQ_LOCK(lhw);
8041 mbufq_concat(&mq, &lhw->rxq);
8042 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
8043
8044 m = mbufq_dequeue(&mq);
8045 while (m != NULL) {
8046 lkpi_80211_lhw_rxq_rx_one(lhw, m);
8047 m = mbufq_dequeue(&mq);
8048 }
8049 }
8050
8051 static void
8052 lkpi_convert_rx_status(struct ieee80211_hw *hw, struct lkpi_sta *lsta,
8053 struct ieee80211_rx_status *rx_status,
8054 struct ieee80211_rx_stats *rx_stats,
8055 uint8_t *rssip)
8056 {
8057 struct ieee80211_supported_band *supband;
8058 struct rate_info rxrate;
8059 int i;
8060 uint8_t rssi;
8061
8062 memset(&rxrate, 0, sizeof(rxrate));
8063 memset(rx_stats, 0, sizeof(*rx_stats));
8064 rx_stats->r_flags = IEEE80211_R_NF | IEEE80211_R_RSSI;
8065 /* XXX-BZ correct hardcoded noise floor, survey data? */
8066 rx_stats->c_nf = -96;
8067 if (ieee80211_hw_check(hw, SIGNAL_DBM) &&
8068 !(rx_status->flag & RX_FLAG_NO_SIGNAL_VAL))
8069 rssi = rx_status->signal;
8070 else
8071 rssi = rx_stats->c_nf;
8072 /*
8073 * net80211 signal strength data are in .5 dBm units relative to
8074 * the current noise floor (see comment in ieee80211_node.h).
8075 */
8076 rssi -= rx_stats->c_nf;
8077 if (rssip != NULL)
8078 *rssip = rssi;
8079 rx_stats->c_rssi = rssi * 2;
8080 rx_stats->r_flags |= IEEE80211_R_BAND;
8081 rx_stats->c_band =
8082 lkpi_nl80211_band_to_net80211_band(rx_status->band);
8083 rx_stats->r_flags |= IEEE80211_R_FREQ | IEEE80211_R_IEEE;
8084 rx_stats->c_freq = rx_status->freq;
8085 rx_stats->c_ieee = ieee80211_mhz2ieee(rx_stats->c_freq, rx_stats->c_band);
8086
8087 rx_stats->c_rx_tsf = rx_status->mactime;
8088
8089 /* XXX RX_FLAG_MACTIME_IS_RTAP_TS64 ? */
8090 if ((rx_status->flag & RX_FLAG_MACTIME) ==
8091 (RX_FLAG_MACTIME_START|RX_FLAG_MACTIME_END)) {
8092 rx_stats->r_flags |= IEEE80211_R_TSF64;
8093 /* XXX RX_FLAG_MACTIME_PLCP_START ? */
8094 if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_START)
8095 rx_stats->r_flags |= IEEE80211_R_TSF_START;
8096 if ((rx_status->flag & RX_FLAG_MACTIME) == RX_FLAG_MACTIME_END)
8097 rx_stats->r_flags |= IEEE80211_R_TSF_END;
8098 /* XXX-BZ if TSF_END will net80211 do the unwind of time? */
8099 }
8100
8101 if (rx_status->chains != 0) {
8102 int cc;
8103 int8_t crssi;
8104
8105 rx_stats->c_chain = rx_status->chains;
8106 rx_stats->r_flags |= IEEE80211_R_C_CHAIN;
8107
8108 cc = 0;
8109 for (i = 0; i < nitems(rx_status->chain_signal); i++) {
8110 if (!(rx_status->chains & BIT(i)))
8111 continue;
8112 crssi = rx_status->chain_signal[i];
8113 crssi -= rx_stats->c_nf;
8114 rx_stats->c_rssi_ctl[i] = crssi * 2;
8115 rx_stats->c_rssi_ext[i] = crssi * 2; /* XXX _ext ??? ATH thing? */
8116 /* We currently only have the global noise floor value. */
8117 rx_stats->c_nf_ctl[i] = rx_stats->c_nf;
8118 rx_stats->c_nf_ext[i] = rx_stats->c_nf;
8119 cc++;
8120 }
8121 if (cc > 0)
8122 rx_stats->r_flags |= (IEEE80211_R_C_NF | IEEE80211_R_C_RSSI);
8123 }
8124
8125 /* rx_status->antenna */
8126
8127 /* XXX-NET80211 We are not going to populate c_phytype! */
8128
8129 switch (rx_status->encoding) {
8130 case RX_ENC_LEGACY:
8131 {
8132 uint32_t legacy = 0;
8133
8134 supband = hw->wiphy->bands[rx_status->band];
8135 if (supband != NULL)
8136 legacy = supband->bitrates[rx_status->rate_idx].bitrate;
8137 rx_stats->c_rate = legacy;
8138 rxrate.legacy = legacy;
8139 /* Is there a LinuxKPI way of reporting IEEE80211_RX_F_CCK / _OFDM? */
8140 break;
8141 }
8142 case RX_ENC_HT:
8143 rx_stats->c_pktflags |= IEEE80211_RX_F_HT;
8144 rx_stats->c_rate = rx_status->rate_idx; /* mcs */
8145 rxrate.flags |= RATE_INFO_FLAGS_MCS;
8146 rxrate.mcs = rx_status->rate_idx;
8147 if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
8148 rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
8149 rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
8150 }
8151 break;
8152 case RX_ENC_VHT:
8153 rx_stats->c_pktflags |= IEEE80211_RX_F_VHT;
8154 rx_stats->c_rate = rx_status->rate_idx; /* mcs */
8155 rx_stats->c_vhtnss = rx_status->nss;
8156 rxrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
8157 rxrate.mcs = rx_status->rate_idx;
8158 rxrate.nss = rx_status->nss;
8159 if ((rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI) != 0) {
8160 rx_stats->c_pktflags |= IEEE80211_RX_F_SHORTGI;
8161 rxrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
8162 }
8163 break;
8164 case RX_ENC_HE:
8165 rxrate.flags |= RATE_INFO_FLAGS_HE_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 case RX_ENC_EHT:
8172 rxrate.flags |= RATE_INFO_FLAGS_EHT_MCS;
8173 rxrate.mcs = rx_status->rate_idx;
8174 rxrate.nss = rx_status->nss;
8175 /* XXX TODO */
8176 TODO("net80211 has not matching encoding for %u", rx_status->encoding);
8177 break;
8178 }
8179
8180 rxrate.bw = rx_status->bw;
8181 switch (rx_status->bw) {
8182 case RATE_INFO_BW_20:
8183 rx_stats->c_width = IEEE80211_RX_FW_20MHZ;
8184 break;
8185 case RATE_INFO_BW_40:
8186 rx_stats->c_width = IEEE80211_RX_FW_40MHZ;
8187 break;
8188 case RATE_INFO_BW_80:
8189 rx_stats->c_width = IEEE80211_RX_FW_80MHZ;
8190 break;
8191 case RATE_INFO_BW_160:
8192 rx_stats->c_width = IEEE80211_RX_FW_160MHZ;
8193 break;
8194 case RATE_INFO_BW_320:
8195 case RATE_INFO_BW_HE_RU:
8196 case RATE_INFO_BW_EHT_RU:
8197 case RATE_INFO_BW_5:
8198 case RATE_INFO_BW_10:
8199 TODO("net80211 has not matching bandwidth for %u", rx_status->bw);
8200 break;
8201 }
8202
8203 if ((rx_status->enc_flags & RX_ENC_FLAG_LDPC) != 0)
8204 rx_stats->c_pktflags |= IEEE80211_RX_F_LDPC;
8205 if ((rx_status->enc_flags & RX_ENC_FLAG_STBC_MASK) != 0)
8206 rx_stats->c_pktflags |= IEEE80211_RX_F_STBC;
8207
8208 /*
8209 * We only need these for LKPI_80211_HW_CRYPTO in theory but in
8210 * case the hardware does something we do not expect always leave
8211 * these enabled. Leaving this commant as documentation for the || 1.
8212 */
8213 #if defined(LKPI_80211_HW_CRYPTO) || 1
8214 if (rx_status->flag & RX_FLAG_DECRYPTED) {
8215 rx_stats->c_pktflags |= IEEE80211_RX_F_DECRYPTED;
8216 /* Only valid if decrypted is set. */
8217 if (rx_status->flag & RX_FLAG_PN_VALIDATED)
8218 rx_stats->c_pktflags |= IEEE80211_RX_F_PN_VALIDATED;
8219 }
8220 if (rx_status->flag & RX_FLAG_IV_STRIPPED)
8221 rx_stats->c_pktflags |= IEEE80211_RX_F_IV_STRIP;
8222 if (rx_status->flag & RX_FLAG_ICV_STRIPPED)
8223 rx_stats->c_pktflags |= IEEE80211_RX_F_ICV_STRIP;
8224 if (rx_status->flag & RX_FLAG_MIC_STRIPPED)
8225 rx_stats->c_pktflags |= IEEE80211_RX_F_MIC_STRIP;
8226 if (rx_status->flag & RX_FLAG_MMIC_STRIPPED)
8227 rx_stats->c_pktflags |= IEEE80211_RX_F_MMIC_STRIP;
8228 if (rx_status->flag & RX_FLAG_MMIC_ERROR)
8229 rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_MMIC;
8230 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
8231 rx_stats->c_pktflags |= IEEE80211_RX_F_FAIL_FCSCRC;
8232 #endif
8233
8234 /* Fill in some sinfo bits to fill gaps not reported byt the driver. */
8235 if (lsta != NULL) {
8236 memcpy(&lsta->sinfo.rxrate, &rxrate, sizeof(rxrate));
8237 lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_RX_BITRATE);
8238
8239 if (rx_status->chains != 0) {
8240 lsta->sinfo.chains = rx_status->chains;
8241 memcpy(lsta->sinfo.chain_signal, rx_status->chain_signal,
8242 sizeof(lsta->sinfo.chain_signal));
8243 lsta->sinfo.filled |= BIT_ULL(NL80211_STA_INFO_CHAIN_SIGNAL);
8244 }
8245 }
8246 }
8247
8248 #ifdef LINUXKPI_DEBUG_80211
8249 static void
8250 lkpi_rx_log_beacon(struct mbuf *m, struct lkpi_hw *lhw,
8251 struct ieee80211_rx_status *rx_status)
8252 {
8253 struct ieee80211_mgmt *f;
8254 uint8_t *e;
8255 char ssid[IEEE80211_NWID_LEN * 4 + 1];
8256
8257 memset(ssid, '\0', sizeof(ssid));
8258
8259 f = mtod(m, struct ieee80211_mgmt *);
8260 e = f->u.beacon.variable;
8261 /*
8262 * Usually SSID is right after the fixed part and for debugging we will
8263 * be fine should we miss it if it is not.
8264 */
8265 while ((e - (uint8_t *)f) < m->m_len) {
8266 if (*e == IEEE80211_ELEMID_SSID)
8267 break;
8268 e += (2 + *(e + 1));
8269 }
8270 if (*e == IEEE80211_ELEMID_SSID) {
8271 int i, len;
8272 char *p;
8273
8274 p = ssid;
8275 len = m->m_len - ((e + 2) - (uint8_t *)f);
8276 if (len > *(e + 1))
8277 len = *(e + 1);
8278 e += 2;
8279 for (i = 0; i < len; i++) {
8280 /* Printable character? */
8281 if (*e >= 0x20 && *e < 0x7f) {
8282 *p++ = *e++;
8283 } else {
8284 snprintf(p, 5, "%#04x", *e++);
8285 p += 4;
8286 }
8287 }
8288 *p = '\0';
8289 }
8290
8291 /* We print skb, skb->data, m as we are seeing 'ghost beacons'. */
8292 TRACE_SCAN_BEACON(lhw->ic, "Beacon: scan_flags %b, band %s freq %u chan %-4d "
8293 "len %d { %#06x %#06x %6D %6D %6D %#06x %ju %u %#06x SSID '%s' }",
8294 lhw->scan_flags, LKPI_LHW_SCAN_BITS,
8295 lkpi_nl80211_band_name(rx_status->band), rx_status->freq,
8296 linuxkpi_ieee80211_frequency_to_channel(rx_status->freq, 0),
8297 m->m_pkthdr.len, f->frame_control, f->duration_id,
8298 f->da, ":", f->sa, ":", f->bssid, ":", f->seq_ctrl,
8299 (uintmax_t)le64_to_cpu(f->u.beacon.timestamp),
8300 le16_to_cpu(f->u.beacon.beacon_int),
8301 le16_to_cpu(f->u.beacon.capab_info), ssid);
8302 }
8303 #endif
8304
8305 /* For %list see comment towards the end of the function. */
8306 void
8307 linuxkpi_ieee80211_rx(struct ieee80211_hw *hw, struct sk_buff *skb,
8308 struct ieee80211_sta *sta, struct napi_struct *napi __unused,
8309 struct list_head *list __unused)
8310 {
8311 struct lkpi_hw *lhw;
8312 struct ieee80211com *ic;
8313 struct mbuf *m;
8314 struct skb_shared_info *shinfo;
8315 struct ieee80211_rx_status *rx_status;
8316 struct ieee80211_rx_stats rx_stats;
8317 struct ieee80211_node *ni;
8318 struct ieee80211vap *vap;
8319 struct ieee80211_hdr *hdr;
8320 struct lkpi_sta *lsta;
8321 int i, offset, ok, error;
8322 uint8_t rssi;
8323 bool is_beacon;
8324
8325 lhw = HW_TO_LHW(hw);
8326 ic = lhw->ic;
8327
8328 if (skb->len < 2) {
8329 /* Need 80211 stats here. */
8330 counter_u64_add(ic->ic_ierrors, 1);
8331 IMPROVE();
8332 goto err;
8333 }
8334
8335 /*
8336 * For now do the data copy; we can later improve things. Might even
8337 * have an mbuf backing the skb data then?
8338 */
8339 m = m_get3(skb->len, M_NOWAIT, MT_DATA, M_PKTHDR);
8340 if (m == NULL) {
8341 counter_u64_add(ic->ic_ierrors, 1);
8342 goto err;
8343 }
8344 m_copyback(m, 0, skb->tail - skb->data, skb->data);
8345
8346 shinfo = skb_shinfo(skb);
8347 offset = m->m_len;
8348 for (i = 0; i < shinfo->nr_frags; i++) {
8349 m_copyback(m, offset, shinfo->frags[i].size,
8350 (uint8_t *)linux_page_address(shinfo->frags[i].page) +
8351 shinfo->frags[i].offset);
8352 offset += shinfo->frags[i].size;
8353 }
8354
8355 rx_status = IEEE80211_SKB_RXCB(skb);
8356
8357 hdr = (void *)skb->data;
8358 is_beacon = ieee80211_is_beacon(hdr->frame_control);
8359
8360 #ifdef LINUXKPI_DEBUG_80211
8361 /*
8362 * We use the mbuf here as otherwise the variable part might
8363 * be in skb frags.
8364 */
8365 if (is_beacon && ((linuxkpi_debug_80211 & D80211_SCAN_BEACON) != 0))
8366 lkpi_rx_log_beacon(m, lhw, rx_status);
8367
8368 if (is_beacon && (linuxkpi_debug_80211 & D80211_TRACE_RX_BEACONS) == 0 &&
8369 (linuxkpi_debug_80211 & D80211_SCAN_BEACON) == 0)
8370 goto no_trace_beacons;
8371
8372 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8373 printf("TRACE-RX: %s: skb %p l/d/t-len (%u/%u/%u) "
8374 "h %p d %p t %p e %p sh %p (%u) m %p plen %u len %u%s\n",
8375 __func__, skb, skb->len, skb->data_len,
8376 skb->truesize, skb->head, skb->data, skb->tail, skb->end,
8377 shinfo, shinfo->nr_frags,
8378 m, m->m_pkthdr.len, m->m_len, is_beacon ? " beacon" : "");
8379
8380 if (linuxkpi_debug_80211 & D80211_TRACE_RX_DUMP)
8381 hexdump(mtod(m, const void *), m->m_len, "RX (raw) ", 0);
8382
8383 /* Implement a dump_rxcb() !!! */
8384 if ((linuxkpi_debug_80211 & D80211_TRACE_RX) != 0 ||
8385 (linuxkpi_debug_80211 & D80211_SCAN_BEACON) != 0)
8386 printf("TRACE-RX: %s: RXCB: %ju %ju %u, %b, %u, %#0x, %#0x, "
8387 "%u band %u, %u { %d %d %d %d }, %d, %#x %#x %#x %#x %u %u %u\n",
8388 __func__,
8389 (uintmax_t)rx_status->boottime_ns,
8390 (uintmax_t)rx_status->mactime,
8391 rx_status->device_timestamp,
8392 rx_status->flag, IEEE80211_RX_STATUS_FLAGS_BITS,
8393 rx_status->freq,
8394 rx_status->bw,
8395 rx_status->encoding,
8396 rx_status->ampdu_reference,
8397 rx_status->band,
8398 rx_status->chains,
8399 rx_status->chain_signal[0],
8400 rx_status->chain_signal[1],
8401 rx_status->chain_signal[2],
8402 rx_status->chain_signal[3],
8403 rx_status->signal,
8404 rx_status->enc_flags,
8405 rx_status->he_dcm,
8406 rx_status->he_gi,
8407 rx_status->he_ru,
8408 rx_status->zero_length_psdu_type,
8409 rx_status->nss,
8410 rx_status->rate_idx);
8411 no_trace_beacons:
8412 #endif
8413
8414 lsta = NULL;
8415 if (sta != NULL) {
8416 lsta = STA_TO_LSTA(sta);
8417 ni = ieee80211_ref_node(lsta->ni);
8418 } else {
8419 struct ieee80211_frame_min *wh;
8420
8421 wh = mtod(m, struct ieee80211_frame_min *);
8422 ni = ieee80211_find_rxnode(ic, wh);
8423 if (ni != NULL)
8424 lsta = ni->ni_drv_data;
8425 }
8426
8427 rssi = 0;
8428 lkpi_convert_rx_status(hw, lsta, rx_status, &rx_stats, &rssi);
8429
8430 ok = ieee80211_add_rx_params(m, &rx_stats);
8431 if (ok == 0) {
8432 m_freem(m);
8433 counter_u64_add(ic->ic_ierrors, 1);
8434 goto err;
8435 }
8436
8437 if (ni != NULL)
8438 vap = ni->ni_vap;
8439 else
8440 /*
8441 * XXX-BZ can we improve this by looking at the frame hdr
8442 * or other meta-data passed up?
8443 */
8444 vap = TAILQ_FIRST(&ic->ic_vaps);
8445
8446 #ifdef LINUXKPI_DEBUG_80211
8447 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8448 printf("TRACE-RX: %s: sta %p lsta %p state %d ni %p vap %p%s\n",
8449 __func__, sta, lsta, (lsta != NULL) ? lsta->state : -1,
8450 ni, vap, is_beacon ? " beacon" : "");
8451 #endif
8452
8453 if (ni != NULL && vap != NULL && is_beacon &&
8454 rx_status->device_timestamp > 0 &&
8455 m->m_pkthdr.len >= sizeof(struct ieee80211_frame)) {
8456 struct lkpi_vif *lvif;
8457 struct ieee80211_vif *vif;
8458 struct ieee80211_frame *wh;
8459
8460 lvif = VAP_TO_LVIF(vap);
8461 vif = LVIF_TO_VIF(lvif);
8462
8463 wh = mtod(m, struct ieee80211_frame *);
8464 if (!IEEE80211_ADDR_EQ(wh->i_addr2, vif->cfg.ap_addr))
8465 goto skip_device_ts;
8466
8467 IMPROVE("TIMING_BEACON_ONLY?");
8468 /* mac80211 specific (not net80211) so keep it here. */
8469 vif->bss_conf.sync_device_ts = rx_status->device_timestamp;
8470 /*
8471 * net80211 should take care of the other information (sync_tsf,
8472 * sync_dtim_count) as otherwise we need to parse the beacon.
8473 */
8474 skip_device_ts:
8475 ;
8476 }
8477
8478 if (vap != NULL && vap->iv_state > IEEE80211_S_INIT &&
8479 ieee80211_radiotap_active_vap(vap)) {
8480 struct lkpi_radiotap_rx_hdr *rtap;
8481
8482 rtap = &lhw->rtap_rx;
8483 rtap->wr_tsft = rx_status->device_timestamp;
8484 rtap->wr_flags = 0;
8485 if (rx_status->enc_flags & RX_ENC_FLAG_SHORTPRE)
8486 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE;
8487 if (rx_status->enc_flags & RX_ENC_FLAG_SHORT_GI)
8488 rtap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTGI;
8489 #if 0 /* .. or it does not given we strip it below. */
8490 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
8491 rtap->wr_flags |= IEEE80211_RADIOTAP_F_FCS;
8492 #endif
8493 if (rx_status->flag & RX_FLAG_FAILED_FCS_CRC)
8494 rtap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS;
8495 rtap->wr_rate = 0;
8496 IMPROVE();
8497 /* XXX TODO status->encoding / rate_index / bw */
8498 rtap->wr_chan_freq = htole16(rx_stats.c_freq);
8499 if (ic->ic_curchan->ic_ieee == rx_stats.c_ieee)
8500 rtap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
8501 rtap->wr_dbm_antsignal = rssi;
8502 rtap->wr_dbm_antnoise = rx_stats.c_nf;
8503 }
8504
8505 if (ieee80211_hw_check(hw, RX_INCLUDES_FCS))
8506 m_adj(m, -IEEE80211_CRC_LEN);
8507
8508 #if 0
8509 if (list != NULL) {
8510 /*
8511 * Normally this would be queued up and delivered by
8512 * netif_receive_skb_list(), napi_gro_receive(), or the like.
8513 * See mt76::mac80211.c as only current possible consumer.
8514 */
8515 IMPROVE("we simply pass the packet to net80211 to deal with.");
8516 }
8517 #endif
8518
8519 /* Attach meta-information to the mbuf for the deferred RX path. */
8520 if (ni != NULL) {
8521 #ifdef LKPI_80211_USE_MTAG
8522 struct m_tag *mtag;
8523 struct lkpi_80211_tag_rxni *rxni;
8524
8525 mtag = m_tag_alloc(MTAG_ABI_LKPI80211, LKPI80211_TAG_RXNI,
8526 sizeof(*rxni), IEEE80211_M_NOWAIT);
8527 if (mtag == NULL) {
8528 m_freem(m);
8529 counter_u64_add(ic->ic_ierrors, 1);
8530 goto err;
8531 }
8532 rxni = (struct lkpi_80211_tag_rxni *)(mtag + 1);
8533 rxni->ni = ni; /* We hold a reference. */
8534 m_tag_prepend(m, mtag);
8535 #else
8536 m->m_pkthdr.PH_loc.ptr = ni; /* We hold a reference. */
8537 #endif
8538 }
8539
8540 LKPI_80211_LHW_RXQ_LOCK(lhw);
8541 if (lhw->rxq_stopped) {
8542 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
8543 m_freem(m);
8544 counter_u64_add(ic->ic_ierrors, 1);
8545 goto err;
8546 }
8547
8548 error = mbufq_enqueue(&lhw->rxq, m);
8549 if (error != 0) {
8550 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
8551 m_freem(m);
8552 counter_u64_add(ic->ic_ierrors, 1);
8553 #ifdef LINUXKPI_DEBUG_80211
8554 if (linuxkpi_debug_80211 & D80211_TRACE_RX)
8555 ic_printf(ni->ni_ic, "%s: mbufq_enqueue failed: %d\n",
8556 __func__, error);
8557 #endif
8558 goto err;
8559 }
8560 taskqueue_enqueue(taskqueue_thread, &lhw->rxq_task);
8561 LKPI_80211_LHW_RXQ_UNLOCK(lhw);
8562
8563 IMPROVE();
8564
8565 err:
8566 /* The skb is ours so we can free it :-) */
8567 kfree_skb(skb);
8568 }
8569
8570 uint8_t
8571 linuxkpi_ieee80211_get_tid(struct ieee80211_hdr *hdr, bool nonqos_ok)
8572 {
8573 const struct ieee80211_frame *wh;
8574 uint8_t tid;
8575
8576 /* Linux seems to assume this is a QOS-Data-Frame */
8577 KASSERT(nonqos_ok || ieee80211_is_data_qos(hdr->frame_control),
8578 ("%s: hdr %p fc %#06x not qos_data\n", __func__, hdr,
8579 hdr->frame_control));
8580
8581 wh = (const struct ieee80211_frame *)hdr;
8582 tid = ieee80211_gettid(wh);
8583 KASSERT(nonqos_ok || tid == (tid & IEEE80211_QOS_TID), ("%s: tid %u "
8584 "not expected (%u?)\n", __func__, tid, IEEE80211_NONQOS_TID));
8585
8586 return (tid);
8587 }
8588
8589 /* -------------------------------------------------------------------------- */
8590
8591 static void
8592 lkpi_wiphy_work(struct work_struct *work)
8593 {
8594 struct lkpi_wiphy *lwiphy;
8595 struct wiphy *wiphy;
8596 struct wiphy_work *wk;
8597
8598 lwiphy = container_of(work, struct lkpi_wiphy, wwk);
8599 wiphy = LWIPHY_TO_WIPHY(lwiphy);
8600
8601 wiphy_lock(wiphy);
8602
8603 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8604 wk = list_first_entry_or_null(&lwiphy->wwk_list, struct wiphy_work, entry);
8605 /* If there is nothing we do nothing. */
8606 if (wk == NULL) {
8607 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8608 wiphy_unlock(wiphy);
8609 return;
8610 }
8611 list_del_init(&wk->entry);
8612
8613 /* More work to do? */
8614 if (!list_empty(&lwiphy->wwk_list))
8615 schedule_work(work);
8616 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8617
8618 /* Finally call the (*wiphy_work_fn)() function. */
8619 wk->fn(wiphy, wk);
8620
8621 wiphy_unlock(wiphy);
8622 }
8623
8624 void
8625 linuxkpi_wiphy_work_queue(struct wiphy *wiphy, struct wiphy_work *wwk)
8626 {
8627 struct lkpi_wiphy *lwiphy;
8628
8629 lwiphy = WIPHY_TO_LWIPHY(wiphy);
8630
8631 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8632 /* Do not double-queue. */
8633 if (list_empty(&wwk->entry))
8634 list_add_tail(&wwk->entry, &lwiphy->wwk_list);
8635 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8636
8637 /*
8638 * See how ieee80211_queue_work() work continues in Linux or if things
8639 * migrate here over time?
8640 * Use a system queue from linux/workqueue.h for now.
8641 */
8642 queue_work(system_wq, &lwiphy->wwk);
8643 }
8644
8645 void
8646 linuxkpi_wiphy_work_cancel(struct wiphy *wiphy, struct wiphy_work *wwk)
8647 {
8648 struct lkpi_wiphy *lwiphy;
8649
8650 lwiphy = WIPHY_TO_LWIPHY(wiphy);
8651
8652 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8653 /* Only cancel if queued. */
8654 if (!list_empty(&wwk->entry))
8655 list_del_init(&wwk->entry);
8656 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8657 }
8658
8659 void
8660 linuxkpi_wiphy_work_flush(struct wiphy *wiphy, struct wiphy_work *wwk)
8661 {
8662 struct lkpi_wiphy *lwiphy;
8663 struct wiphy_work *wk;
8664
8665 lwiphy = WIPHY_TO_LWIPHY(wiphy);
8666 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8667 /* If wwk is unset, flush everything; called when wiphy is shut down. */
8668 if (wwk != NULL && list_empty(&wwk->entry)) {
8669 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8670 return;
8671 }
8672
8673 while (!list_empty(&lwiphy->wwk_list)) {
8674
8675 wk = list_first_entry(&lwiphy->wwk_list, struct wiphy_work,
8676 entry);
8677 list_del_init(&wk->entry);
8678 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8679 wk->fn(wiphy, wk);
8680 LKPI_80211_LWIPHY_WORK_LOCK(lwiphy);
8681 if (wk == wwk)
8682 break;
8683 }
8684 LKPI_80211_LWIPHY_WORK_UNLOCK(lwiphy);
8685 }
8686
8687 void
8688 lkpi_wiphy_delayed_work_timer(struct timer_list *tl)
8689 {
8690 struct wiphy_delayed_work *wdwk;
8691
8692 wdwk = timer_container_of(wdwk, tl, timer);
8693 wiphy_work_queue(wdwk->wiphy, &wdwk->work);
8694 }
8695
8696 void
8697 linuxkpi_wiphy_delayed_work_queue(struct wiphy *wiphy,
8698 struct wiphy_delayed_work *wdwk, unsigned long delay)
8699 {
8700 if (delay == 0) {
8701 /* Run right away. */
8702 del_timer(&wdwk->timer);
8703 wiphy_work_queue(wiphy, &wdwk->work);
8704 } else {
8705 wdwk->wiphy = wiphy;
8706 mod_timer(&wdwk->timer, jiffies + delay);
8707 }
8708 }
8709
8710 void
8711 linuxkpi_wiphy_delayed_work_cancel(struct wiphy *wiphy,
8712 struct wiphy_delayed_work *wdwk)
8713 {
8714 del_timer_sync(&wdwk->timer);
8715 wiphy_work_cancel(wiphy, &wdwk->work);
8716 }
8717
8718 void
8719 linuxkpi_wiphy_delayed_work_flush(struct wiphy *wiphy,
8720 struct wiphy_delayed_work *wdwk)
8721 {
8722 lockdep_assert_held(&wiphy->mtx);
8723
8724 del_timer_sync(&wdwk->timer);
8725 wiphy_work_flush(wiphy, &wdwk->work);
8726 }
8727
8728 /* -------------------------------------------------------------------------- */
8729
8730 struct wiphy *
8731 linuxkpi_wiphy_new(const struct cfg80211_ops *ops, size_t priv_len)
8732 {
8733 struct lkpi_wiphy *lwiphy;
8734 struct wiphy *wiphy;
8735
8736 lwiphy = kzalloc(sizeof(*lwiphy) + priv_len, GFP_KERNEL);
8737 if (lwiphy == NULL)
8738 return (NULL);
8739 lwiphy->ops = ops;
8740
8741 LKPI_80211_LWIPHY_WORK_LOCK_INIT(lwiphy);
8742 INIT_LIST_HEAD(&lwiphy->wwk_list);
8743 INIT_WORK(&lwiphy->wwk, lkpi_wiphy_work);
8744
8745 wiphy = LWIPHY_TO_WIPHY(lwiphy);
8746
8747 INIT_LIST_HEAD(&wiphy->wdev_list);
8748 mutex_init(&wiphy->mtx);
8749 TODO();
8750
8751 return (wiphy);
8752 }
8753
8754 void
8755 linuxkpi_wiphy_free(struct wiphy *wiphy)
8756 {
8757 struct lkpi_wiphy *lwiphy;
8758
8759 if (wiphy == NULL)
8760 return;
8761
8762 linuxkpi_wiphy_work_flush(wiphy, NULL);
8763 mutex_destroy(&wiphy->mtx);
8764
8765 lwiphy = WIPHY_TO_LWIPHY(wiphy);
8766 LKPI_80211_LWIPHY_WORK_LOCK_DESTROY(lwiphy);
8767
8768 kfree(lwiphy);
8769 }
8770
8771 static void
8772 lkpi_wiphy_band_annotate(struct wiphy *wiphy)
8773 {
8774 int band;
8775
8776 for (band = 0; band < NUM_NL80211_BANDS; band++) {
8777 struct ieee80211_supported_band *supband;
8778 int i;
8779
8780 supband = wiphy->bands[band];
8781 if (supband == NULL)
8782 continue;
8783
8784 switch (band) {
8785 case NL80211_BAND_2GHZ:
8786 case NL80211_BAND_5GHZ:
8787 break;
8788 default:
8789 #ifdef LINUXKPI_DEBUG_80211
8790 IMPROVE("band %d(%s) not yet supported",
8791 band, lkpi_nl80211_band_name(band));
8792 /* For bands added here, also check lkpi_lsta_alloc(). */
8793 #endif
8794 continue;
8795 }
8796
8797 /* Band bitrates are times 10; e.g., 55 is 5.5Mbit/s. */
8798 for (i = 0; i < supband->n_bitrates; i++) {
8799 switch (band) {
8800 case NL80211_BAND_2GHZ:
8801 switch (supband->bitrates[i].bitrate) {
8802 case 110:
8803 case 55:
8804 case 20:
8805 case 10:
8806 supband->bitrates[i].flags |=
8807 IEEE80211_RATE_MANDATORY_B;
8808 /* FALLTHROUGH */
8809 /* 11g only */
8810 case 240:
8811 case 120:
8812 case 60:
8813 supband->bitrates[i].flags |=
8814 IEEE80211_RATE_MANDATORY_G;
8815 break;
8816 }
8817 break;
8818 case NL80211_BAND_5GHZ:
8819 switch (supband->bitrates[i].bitrate) {
8820 case 240:
8821 case 120:
8822 case 60:
8823 supband->bitrates[i].flags |=
8824 IEEE80211_RATE_MANDATORY_A;
8825 break;
8826 }
8827 break;
8828 }
8829 TRACE_RATES("band %d bitrate[%d/%u] %u flags %#010x",
8830 band, i, supband->n_bitrates,
8831 supband->bitrates[i].bitrate,
8832 supband->bitrates[i].flags);
8833 }
8834 }
8835 }
8836
8837 int
8838 linuxkpi_80211_wiphy_register(struct wiphy *wiphy)
8839 {
8840 TODO("Lots of checks and initialization");
8841
8842 lkpi_wiphy_band_annotate(wiphy);
8843
8844 return (0);
8845 }
8846
8847 static uint32_t
8848 lkpi_cfg80211_calculate_bitrate_ht(struct rate_info *rate)
8849 {
8850 /*
8851 * IEEE Std 802.11-2024, 19.5 Parameters for HT-MCSs;
8852 * Tables Table 19-27-MCS NSS=1 800ns GI, and following.
8853 * We use 100Kbit/s entries, the expacted return scale.
8854 * We can calulate MCS0..31 entries.
8855 */
8856 uint32_t r;
8857 uint8_t nss, mcsidx;
8858
8859 if (rate->mcs > 31)
8860 goto inval;
8861
8862 switch (rate->bw) {
8863 case RATE_INFO_BW_20:
8864 r = 65;
8865 break;
8866 case RATE_INFO_BW_40:
8867 r = 135;
8868 break;
8869 default:
8870 goto inval;
8871 /* NOTREACHED */
8872 }
8873
8874 nss = (rate->mcs >> 3) + 1;
8875 mcsidx = rate->mcs & 0x07;
8876
8877 switch (mcsidx) {
8878 case 0 ... 3:
8879 r *= (mcsidx + 1);
8880 break;
8881 case 4:
8882 r *= (mcsidx + 2);
8883 break;
8884 case 5 ... 7:
8885 r *= (mcsidx + 3);
8886 break;
8887 default:
8888 goto inval;
8889 /* NOTREACHED */
8890 }
8891 r *= nss;
8892 if ((rate->flags & RATE_INFO_FLAGS_SHORT_GI) != 0)
8893 r = (r * 10) / 9;
8894
8895 return (r);
8896
8897 inval:
8898 /* Do not warn every time or we get too much spam on console! */
8899 WARN_ONCE(1, "%s: rate mcs %u nss %u mcsidx %u invalid!\n", __func__,
8900 rate->mcs, nss, mcsidx);
8901 return (0);
8902 }
8903
8904 static uint32_t
8905 lkpi_cfg80211_calculate_bitrate_vht(struct rate_info *rate)
8906 {
8907 /*
8908 * IEEE Std 802.11-2024, 21.5 Parameters for VHT-MCSs;
8909 * Tables 21-29 NSS=1 800ns GI, and following.
8910 * We use 100Kbit/s entries, the expacted return scale.
8911 */
8912 static const uint16_t datarate[4][10] = {
8913 /* BW20 */
8914 { 65, 130, 195, 260, 390, 520, 585, 650, 780, 0 },
8915 /* BW40 */
8916 { 135, 270, 405, 540, 810, 1080, 1215, 1350, 1620, 1800 },
8917 /* BW80 */
8918 { 293, 585, 878, 1170, 1755, 2340, 2633, 2925, 3510, 3900 },
8919 /* BW160 */
8920 { 585, 1170, 1755, 2340, 3510, 4680, 5265, 5850, 7020, 7800 }
8921 };
8922 uint32_t r;
8923 uint8_t bwidx;
8924
8925 /* Should we warn about out of bounds values? */
8926 if (rate->mcs > 9 || rate->nss > 8)
8927 goto inval;
8928
8929 switch (rate->bw) {
8930 case RATE_INFO_BW_20:
8931 bwidx = 0;
8932 break;
8933 case RATE_INFO_BW_40:
8934 bwidx = 1;
8935 break;
8936 case RATE_INFO_BW_80:
8937 bwidx = 2;
8938 break;
8939 case RATE_INFO_BW_160:
8940 bwidx = 2;
8941 break;
8942 default:
8943 goto inval;
8944 /* NOTREACHED */
8945 }
8946
8947 r = datarate[bwidx][rate->mcs];
8948 /* Calculate the other NSS tables and the SGI column. */
8949 r *= rate->nss;
8950 if ((rate->flags & RATE_INFO_FLAGS_SHORT_GI) != 0)
8951 r = (r * 10) / 9;
8952
8953 return (r);
8954
8955 inval:
8956 /* Do not warn every time or we get too much spam on console! */
8957 WARN_ONCE(1, "%s: rate mcs %u nss %u bw %d (%s) invalid!\n", __func__,
8958 rate->mcs, rate->nss, rate->bw, lkpi_rate_info_bw_to_str(rate->bw));
8959 return (0);
8960 }
8961
8962 uint32_t
8963 linuxkpi_cfg80211_calculate_bitrate(struct rate_info *rate)
8964 {
8965
8966 /* Beware: order! */
8967 if (rate->flags & RATE_INFO_FLAGS_MCS)
8968 return (lkpi_cfg80211_calculate_bitrate_ht(rate));
8969
8970 if (rate->flags & RATE_INFO_FLAGS_VHT_MCS)
8971 return (lkpi_cfg80211_calculate_bitrate_vht(rate));
8972
8973 IMPROVE("HE/EHT/...");
8974
8975 return (rate->legacy);
8976 }
8977
8978 uint32_t
8979 linuxkpi_ieee80211_channel_to_frequency(uint32_t channel,
8980 enum nl80211_band band)
8981 {
8982
8983 switch (band) {
8984 case NL80211_BAND_2GHZ:
8985 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_2GHZ));
8986 break;
8987 case NL80211_BAND_5GHZ:
8988 return (ieee80211_ieee2mhz(channel, IEEE80211_CHAN_5GHZ));
8989 break;
8990 default:
8991 /* XXX abort, retry, error, panic? */
8992 break;
8993 }
8994
8995 return (0);
8996 }
8997
8998 uint32_t
8999 linuxkpi_ieee80211_frequency_to_channel(uint32_t freq, uint32_t flags __unused)
9000 {
9001
9002 return (ieee80211_mhz2ieee(freq, 0));
9003 }
9004
9005 #if 0
9006 static struct lkpi_sta *
9007 lkpi_find_lsta_by_ni(struct lkpi_vif *lvif, struct ieee80211_node *ni)
9008 {
9009 struct lkpi_sta *lsta, *temp;
9010
9011 rcu_read_lock();
9012 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
9013 if (lsta->ni == ni) {
9014 rcu_read_unlock();
9015 return (lsta);
9016 }
9017 }
9018 rcu_read_unlock();
9019
9020 return (NULL);
9021 }
9022 #endif
9023
9024 struct ieee80211_sta *
9025 linuxkpi_ieee80211_find_sta(struct ieee80211_vif *vif, const u8 *peer)
9026 {
9027 struct lkpi_vif *lvif;
9028 struct lkpi_sta *lsta;
9029 struct ieee80211_sta *sta;
9030
9031 lvif = VIF_TO_LVIF(vif);
9032
9033 rcu_read_lock();
9034 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
9035 sta = LSTA_TO_STA(lsta);
9036 if (IEEE80211_ADDR_EQ(sta->addr, peer)) {
9037 rcu_read_unlock();
9038 return (sta);
9039 }
9040 }
9041 rcu_read_unlock();
9042 return (NULL);
9043 }
9044
9045 struct ieee80211_sta *
9046 linuxkpi_ieee80211_find_sta_by_ifaddr(struct ieee80211_hw *hw,
9047 const uint8_t *addr, const uint8_t *ourvifaddr)
9048 {
9049 struct lkpi_hw *lhw;
9050 struct lkpi_vif *lvif;
9051 struct lkpi_sta *lsta;
9052 struct ieee80211_vif *vif;
9053 struct ieee80211_sta *sta;
9054
9055 lhw = wiphy_priv(hw->wiphy);
9056 sta = NULL;
9057
9058 LKPI_80211_LHW_LVIF_LOCK(lhw);
9059 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
9060
9061 /* XXX-BZ check our address from the vif. */
9062
9063 vif = LVIF_TO_VIF(lvif);
9064 if (ourvifaddr != NULL &&
9065 !IEEE80211_ADDR_EQ(vif->addr, ourvifaddr))
9066 continue;
9067 sta = linuxkpi_ieee80211_find_sta(vif, addr);
9068 if (sta != NULL)
9069 break;
9070 }
9071 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
9072
9073 if (sta != NULL) {
9074 lsta = STA_TO_LSTA(sta);
9075 if (!lsta->added_to_drv)
9076 return (NULL);
9077 }
9078
9079 return (sta);
9080 }
9081
9082 struct sk_buff *
9083 linuxkpi_ieee80211_tx_dequeue(struct ieee80211_hw *hw,
9084 struct ieee80211_txq *txq)
9085 {
9086 struct lkpi_txq *ltxq;
9087 struct lkpi_vif *lvif;
9088 struct sk_buff *skb;
9089
9090 IMPROVE("wiphy_lock? or assert?");
9091 skb = NULL;
9092 ltxq = TXQ_TO_LTXQ(txq);
9093 ltxq->flags |= LKPI_TXQ_SEEN_DEQUEUE;
9094
9095 if ((ltxq->flags & (LKPI_TXQ_STOPPED|LKPI_TXQ_STOPPED_BA)) != 0)
9096 goto stopped;
9097
9098 lvif = VIF_TO_LVIF(ltxq->txq.vif);
9099 if (lvif->hw_queue_stopped[ltxq->txq.ac]) {
9100 ltxq->flags |= LKPI_TXQ_STOPPED;
9101 goto stopped;
9102 }
9103
9104 IMPROVE("hw(TX_FRAG_LIST)");
9105
9106 LKPI_80211_LTXQ_LOCK(ltxq);
9107 skb = skb_dequeue(<xq->skbq);
9108 if (skb != NULL)
9109 ltxq->frms_dequeued++;
9110 LKPI_80211_LTXQ_UNLOCK(ltxq);
9111
9112 stopped:
9113 return (skb);
9114 }
9115
9116 void
9117 linuxkpi_ieee80211_txq_get_depth(struct ieee80211_txq *txq,
9118 unsigned long *frame_cnt, unsigned long *byte_cnt)
9119 {
9120 struct lkpi_txq *ltxq;
9121 struct sk_buff *skb;
9122 unsigned long fc, bc;
9123
9124 ltxq = TXQ_TO_LTXQ(txq);
9125
9126 fc = bc = 0;
9127 LKPI_80211_LTXQ_LOCK(ltxq);
9128 skb_queue_walk(<xq->skbq, skb) {
9129 fc++;
9130 bc += skb->len;
9131 }
9132 LKPI_80211_LTXQ_UNLOCK(ltxq);
9133 if (frame_cnt)
9134 *frame_cnt = fc;
9135 if (byte_cnt)
9136 *byte_cnt = bc;
9137
9138 /* Validate that this is doing the correct thing. */
9139 /* Should we keep track on en/dequeue? */
9140 IMPROVE();
9141 }
9142
9143 /*
9144 * We are called from ieee80211_free_txskb() or ieee80211_tx_status().
9145 * The latter tries to derive the success status from the info flags
9146 * passed back from the driver. rawx_mit() saves the ni on the m and the
9147 * m on the skb for us to be able to give feedback to net80211.
9148 */
9149 static void
9150 _lkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
9151 int status)
9152 {
9153 struct ieee80211_node *ni;
9154 struct mbuf *m;
9155
9156 if (skb == NULL)
9157 return;
9158
9159 m = skb->m;
9160 skb->m = NULL;
9161
9162 if (m != NULL) {
9163 ni = m->m_pkthdr.PH_loc.ptr;
9164 /* Status: 0 is ok, != 0 is error. */
9165 ieee80211_tx_complete(ni, m, status);
9166 /* ni & mbuf were consumed. */
9167 }
9168 }
9169
9170 void
9171 linuxkpi_ieee80211_free_txskb(struct ieee80211_hw *hw, struct sk_buff *skb,
9172 int status)
9173 {
9174
9175 _lkpi_ieee80211_free_txskb(hw, skb, status);
9176 kfree_skb(skb);
9177 }
9178
9179 void
9180 linuxkpi_ieee80211_tx_status_ext(struct ieee80211_hw *hw,
9181 struct ieee80211_tx_status *txstat)
9182 {
9183 struct sk_buff *skb;
9184 struct ieee80211_tx_info *info, _info = { };
9185 struct ieee80211_ratectl_tx_status txs;
9186 struct ieee80211_node *ni;
9187 int status;
9188
9189 skb = txstat->skb;
9190 if (skb != NULL && skb->m != NULL) {
9191 struct mbuf *m;
9192
9193 m = skb->m;
9194 ni = m->m_pkthdr.PH_loc.ptr;
9195 memset(&txs, 0, sizeof(txs));
9196 } else {
9197 ni = NULL;
9198 }
9199
9200 /*
9201 * If we have no info information on tx, set info to an all-zero struct
9202 * to make the code (and debug output) simpler.
9203 */
9204 info = txstat->info;
9205 if (info == NULL)
9206 info = &_info;
9207 if (info->flags & IEEE80211_TX_STAT_ACK) {
9208 status = 0; /* No error. */
9209 txs.status = IEEE80211_RATECTL_TX_SUCCESS;
9210 } else {
9211 status = 1;
9212 txs.status = IEEE80211_RATECTL_TX_FAIL_UNSPECIFIED;
9213 }
9214
9215 if (ni != NULL) {
9216 txs.pktlen = skb->len;
9217 txs.flags |= IEEE80211_RATECTL_STATUS_PKTLEN;
9218 if (info->status.rates[0].count > 1) {
9219 txs.long_retries = info->status.rates[0].count - 1; /* 1 + retries in drivers. */
9220 txs.flags |= IEEE80211_RATECTL_STATUS_LONG_RETRY;
9221 }
9222 #if 0 /* Unused in net80211 currently. */
9223 /* XXX-BZ convert check .flags for MCS/VHT/.. */
9224 txs.final_rate = info->status.rates[0].idx;
9225 txs.flags |= IEEE80211_RATECTL_STATUS_FINAL_RATE;
9226 #endif
9227 if (info->status.flags & IEEE80211_TX_STATUS_ACK_SIGNAL_VALID) {
9228 txs.rssi = info->status.ack_signal; /* XXX-BZ CONVERT? */
9229 txs.flags |= IEEE80211_RATECTL_STATUS_RSSI;
9230 }
9231
9232 IMPROVE("only update rate if needed but that requires us to get a proper rate from mo_sta_statistics");
9233 ieee80211_ratectl_tx_complete(ni, &txs);
9234 /*
9235 * A tx completion can land here after the vap has been torn
9236 * down (iv_bss cleared on the way to INIT) while frames were
9237 * still in flight; there is no bss node left to rate-adjust.
9238 * This is another case of !lvif->lvif_bss_synched but checking
9239 * that seems too cumbersome.
9240 */
9241 if (ni->ni_vap->iv_bss != NULL)
9242 ieee80211_ratectl_rate(ni->ni_vap->iv_bss, NULL, 0);
9243
9244 #ifdef LINUXKPI_DEBUG_80211
9245 if (linuxkpi_debug_80211 & D80211_TRACE_TX) {
9246 printf("TX-RATE: %s: long_retries %d\n", __func__,
9247 txs.long_retries);
9248 }
9249 #endif
9250 }
9251
9252 #ifdef LINUXKPI_DEBUG_80211
9253 if (linuxkpi_debug_80211 & D80211_TRACE_TX)
9254 printf("TX-STATUS: %s: hw %p skb %p status %d : flags %b "
9255 "band %u hw_queue %u tx_time_est %d : "
9256 "rates [ %u %u %#x, %u %u %#x, %u %u %#x, %u %u %#x ] "
9257 "ack_signal %u ampdu_ack_len %u ampdu_len %u antenna %u "
9258 "tx_time %u flags %b "
9259 "status_driver_data [ %p %p ]\n",
9260 __func__, hw, skb, status, info->flags, IEEE80211_TX_INFO_FLAGS,
9261 info->band, info->hw_queue, info->tx_time_est,
9262 info->status.rates[0].idx, info->status.rates[0].count,
9263 info->status.rates[0].flags,
9264 info->status.rates[1].idx, info->status.rates[1].count,
9265 info->status.rates[1].flags,
9266 info->status.rates[2].idx, info->status.rates[2].count,
9267 info->status.rates[2].flags,
9268 info->status.rates[3].idx, info->status.rates[3].count,
9269 info->status.rates[3].flags,
9270 info->status.ack_signal, info->status.ampdu_ack_len,
9271 info->status.ampdu_len, info->status.antenna,
9272 info->status.tx_time, info->status.flags, IEEE80211_TX_STATUS_FLAGS,
9273 info->status.status_driver_data[0],
9274 info->status.status_driver_data[1]);
9275 #endif
9276
9277 if (txstat->free_list) {
9278 _lkpi_ieee80211_free_txskb(hw, skb, status);
9279 if (skb != NULL)
9280 list_add_tail(&skb->list, txstat->free_list);
9281 } else {
9282 linuxkpi_ieee80211_free_txskb(hw, skb, status);
9283 }
9284 }
9285
9286 void
9287 linuxkpi_ieee80211_tx_status(struct ieee80211_hw *hw, struct sk_buff *skb)
9288 {
9289 struct ieee80211_tx_status status;
9290
9291 memset(&status, 0, sizeof(status));
9292 status.info = IEEE80211_SKB_CB(skb);
9293 status.skb = skb;
9294 /* sta, n_rates, rates, free_list? */
9295
9296 ieee80211_tx_status_ext(hw, &status);
9297 }
9298
9299 /*
9300 * This is an internal bandaid for the moment for the way we glue
9301 * skbs and mbufs together for TX. Once we have skbs backed by
9302 * mbufs this should go away.
9303 * This is a public function but kept on the private KPI (lkpi_)
9304 * and is not exposed by a header file.
9305 */
9306 static void
9307 lkpi_ieee80211_free_skb_mbuf(void *p)
9308 {
9309 struct ieee80211_node *ni;
9310 struct mbuf *m;
9311
9312 if (p == NULL)
9313 return;
9314
9315 m = (struct mbuf *)p;
9316 M_ASSERTPKTHDR(m);
9317
9318 ni = m->m_pkthdr.PH_loc.ptr;
9319 m->m_pkthdr.PH_loc.ptr = NULL;
9320 if (ni != NULL)
9321 ieee80211_free_node(ni);
9322 m_freem(m);
9323 }
9324
9325 void
9326 linuxkpi_ieee80211_queue_delayed_work(struct ieee80211_hw *hw,
9327 struct delayed_work *w, int delay)
9328 {
9329 struct lkpi_hw *lhw;
9330
9331 /* Need to make sure hw is in a stable (non-suspended) state. */
9332 IMPROVE();
9333
9334 lhw = HW_TO_LHW(hw);
9335 queue_delayed_work(lhw->workq, w, delay);
9336 }
9337
9338 void
9339 linuxkpi_ieee80211_queue_work(struct ieee80211_hw *hw,
9340 struct work_struct *w)
9341 {
9342 struct lkpi_hw *lhw;
9343
9344 /* Need to make sure hw is in a stable (non-suspended) state. */
9345 IMPROVE();
9346
9347 lhw = HW_TO_LHW(hw);
9348 queue_work(lhw->workq, w);
9349 }
9350
9351 struct sk_buff *
9352 linuxkpi_ieee80211_probereq_get(struct ieee80211_hw *hw, const uint8_t *addr,
9353 const uint8_t *ssid, size_t ssid_len, size_t tailroom)
9354 {
9355 struct sk_buff *skb;
9356 struct ieee80211_frame *wh;
9357 uint8_t *p;
9358 size_t len;
9359
9360 len = sizeof(*wh);
9361 len += 2 + ssid_len;
9362
9363 skb = dev_alloc_skb(hw->extra_tx_headroom + len + tailroom);
9364 if (skb == NULL)
9365 return (NULL);
9366
9367 skb_reserve(skb, hw->extra_tx_headroom);
9368
9369 wh = skb_put_zero(skb, sizeof(*wh));
9370 wh->i_fc[0] = IEEE80211_FC0_VERSION_0;
9371 wh->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PROBE_REQ | IEEE80211_FC0_TYPE_MGT;
9372 IEEE80211_ADDR_COPY(wh->i_addr1, ieee80211broadcastaddr);
9373 IEEE80211_ADDR_COPY(wh->i_addr2, addr);
9374 IEEE80211_ADDR_COPY(wh->i_addr3, ieee80211broadcastaddr);
9375
9376 p = skb_put(skb, 2 + ssid_len);
9377 *p++ = IEEE80211_ELEMID_SSID;
9378 *p++ = ssid_len;
9379 if (ssid_len > 0)
9380 memcpy(p, ssid, ssid_len);
9381
9382 return (skb);
9383 }
9384
9385 struct sk_buff *
9386 linuxkpi_ieee80211_pspoll_get(struct ieee80211_hw *hw,
9387 struct ieee80211_vif *vif)
9388 {
9389 struct lkpi_vif *lvif;
9390 struct ieee80211vap *vap;
9391 struct sk_buff *skb;
9392 struct ieee80211_frame_pspoll *psp;
9393 uint16_t v;
9394
9395 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*psp));
9396 if (skb == NULL)
9397 return (NULL);
9398
9399 skb_reserve(skb, hw->extra_tx_headroom);
9400
9401 lvif = VIF_TO_LVIF(vif);
9402 vap = LVIF_TO_VAP(lvif);
9403
9404 psp = skb_put_zero(skb, sizeof(*psp));
9405 psp->i_fc[0] = IEEE80211_FC0_VERSION_0;
9406 psp->i_fc[0] |= IEEE80211_FC0_SUBTYPE_PS_POLL | IEEE80211_FC0_TYPE_CTL;
9407 v = htole16(vif->cfg.aid | 1<<15 | 1<<16);
9408 memcpy(&psp->i_aid, &v, sizeof(v));
9409 IEEE80211_ADDR_COPY(psp->i_bssid, vap->iv_bss->ni_macaddr);
9410 IEEE80211_ADDR_COPY(psp->i_ta, vif->addr);
9411
9412 return (skb);
9413 }
9414
9415 struct sk_buff *
9416 linuxkpi_ieee80211_nullfunc_get(struct ieee80211_hw *hw,
9417 struct ieee80211_vif *vif, int linkid, bool qos)
9418 {
9419 struct sk_buff *skb;
9420 struct ieee80211_frame *nullf;
9421
9422 IMPROVE("linkid");
9423
9424 skb = dev_alloc_skb(hw->extra_tx_headroom + sizeof(*nullf));
9425 if (skb == NULL)
9426 return (NULL);
9427
9428 skb_reserve(skb, hw->extra_tx_headroom);
9429
9430 nullf = skb_put_zero(skb, sizeof(*nullf));
9431 nullf->i_fc[0] = IEEE80211_FC0_VERSION_0;
9432 nullf->i_fc[0] |= IEEE80211_FC0_SUBTYPE_NODATA | IEEE80211_FC0_TYPE_DATA;
9433 nullf->i_fc[1] = IEEE80211_FC1_DIR_TODS;
9434
9435 /* XXX-BZ if link is given, this is different. */
9436 IEEE80211_ADDR_COPY(nullf->i_addr1, vif->cfg.ap_addr);
9437 IEEE80211_ADDR_COPY(nullf->i_addr2, vif->addr);
9438 IEEE80211_ADDR_COPY(nullf->i_addr3, vif->cfg.ap_addr);
9439
9440 return (skb);
9441 }
9442
9443 struct wireless_dev *
9444 linuxkpi_ieee80211_vif_to_wdev(struct ieee80211_vif *vif)
9445 {
9446 struct lkpi_vif *lvif;
9447
9448 lvif = VIF_TO_LVIF(vif);
9449 return (&lvif->wdev);
9450 }
9451
9452 void
9453 linuxkpi_ieee80211_connection_loss(struct ieee80211_vif *vif)
9454 {
9455 struct lkpi_vif *lvif;
9456 struct ieee80211vap *vap;
9457 enum ieee80211_state nstate;
9458 int arg;
9459
9460 lvif = VIF_TO_LVIF(vif);
9461 vap = LVIF_TO_VAP(lvif);
9462
9463 /*
9464 * Go to init; otherwise we need to elaborately check state and
9465 * handle accordingly, e.g., if in RUN we could call iv_bmiss.
9466 * Let the statemachine handle all neccessary changes.
9467 */
9468 nstate = IEEE80211_S_INIT;
9469 arg = 0; /* Not a valid reason. */
9470
9471 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
9472 "beacons %d dtim_period %d)\n", __func__, vif, vap,
9473 ieee80211_state_name[vap->iv_state],
9474 lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
9475 vif->bss_conf.dtim_period);
9476 ieee80211_new_state(vap, nstate, arg);
9477 }
9478
9479 void
9480 linuxkpi_ieee80211_beacon_loss(struct ieee80211_vif *vif)
9481 {
9482 struct lkpi_vif *lvif;
9483 struct ieee80211vap *vap;
9484
9485 lvif = VIF_TO_LVIF(vif);
9486 vap = LVIF_TO_VAP(lvif);
9487
9488 ic_printf(vap->iv_ic, "%s: vif %p vap %p state %s (synched %d, assoc %d "
9489 "beacons %d dtim_period %d)\n", __func__, vif, vap,
9490 ieee80211_state_name[vap->iv_state],
9491 lvif->lvif_bss_synched, vif->cfg.assoc, lvif->beacons,
9492 vif->bss_conf.dtim_period);
9493 ieee80211_beacon_miss(vap->iv_ic);
9494 }
9495
9496 /* -------------------------------------------------------------------------- */
9497
9498 void
9499 linuxkpi_ieee80211_stop_queue(struct ieee80211_hw *hw, int qnum)
9500 {
9501 struct lkpi_hw *lhw;
9502 struct lkpi_vif *lvif;
9503 struct ieee80211_vif *vif;
9504 int ac_count, ac;
9505
9506 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
9507 __func__, qnum, hw->queues, hw));
9508
9509 lhw = wiphy_priv(hw->wiphy);
9510
9511 /* See lkpi_ic_vap_create(). */
9512 if (hw->queues >= IEEE80211_NUM_ACS)
9513 ac_count = IEEE80211_NUM_ACS;
9514 else
9515 ac_count = 1;
9516
9517 LKPI_80211_LHW_LVIF_LOCK(lhw);
9518 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
9519
9520 vif = LVIF_TO_VIF(lvif);
9521 for (ac = 0; ac < ac_count; ac++) {
9522 IMPROVE_TXQ("LOCKING");
9523 if (qnum == vif->hw_queue[ac]) {
9524 #ifdef LINUXKPI_DEBUG_80211
9525 /*
9526 * For now log this to better understand
9527 * how this is supposed to work.
9528 */
9529 if (lvif->hw_queue_stopped[ac] &&
9530 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
9531 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
9532 "lvif %p vif %p ac %d qnum %d already "
9533 "stopped\n", __func__, __LINE__,
9534 lhw, hw, lvif, vif, ac, qnum);
9535 #endif
9536 lvif->hw_queue_stopped[ac] = true;
9537 }
9538 }
9539 }
9540 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
9541 }
9542
9543 void
9544 linuxkpi_ieee80211_stop_queues(struct ieee80211_hw *hw)
9545 {
9546 int i;
9547
9548 IMPROVE_TXQ("Locking; do we need further info?");
9549 for (i = 0; i < hw->queues; i++)
9550 linuxkpi_ieee80211_stop_queue(hw, i);
9551 }
9552
9553
9554 static void
9555 lkpi_ieee80211_wake_queues(struct ieee80211_hw *hw, int hwq)
9556 {
9557 struct lkpi_hw *lhw;
9558 struct lkpi_vif *lvif;
9559 struct lkpi_sta *lsta;
9560 int ac_count, ac, tid;
9561
9562 /* See lkpi_ic_vap_create(). */
9563 if (hw->queues >= IEEE80211_NUM_ACS)
9564 ac_count = IEEE80211_NUM_ACS;
9565 else
9566 ac_count = 1;
9567
9568 lhw = wiphy_priv(hw->wiphy);
9569
9570 IMPROVE_TXQ("Locking");
9571 LKPI_80211_LHW_LVIF_LOCK(lhw);
9572 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
9573 struct ieee80211_vif *vif;
9574
9575 vif = LVIF_TO_VIF(lvif);
9576 for (ac = 0; ac < ac_count; ac++) {
9577
9578 if (hwq == vif->hw_queue[ac]) {
9579
9580 /* XXX-BZ what about software scan? */
9581
9582 #ifdef LINUXKPI_DEBUG_80211
9583 /*
9584 * For now log this to better understand
9585 * how this is supposed to work.
9586 */
9587 if (!lvif->hw_queue_stopped[ac] &&
9588 (linuxkpi_debug_80211 & D80211_IMPROVE_TXQ) != 0)
9589 ic_printf(lhw->ic, "%s:%d: lhw %p hw %p "
9590 "lvif %p vif %p ac %d hw_q not stopped\n",
9591 __func__, __LINE__,
9592 lhw, hw, lvif, vif, ac);
9593 #endif
9594 lvif->hw_queue_stopped[ac] = false;
9595
9596 rcu_read_lock();
9597 list_for_each_entry_rcu(lsta, &lvif->lsta_list, lsta_list) {
9598 struct ieee80211_sta *sta;
9599
9600 sta = LSTA_TO_STA(lsta);
9601 for (tid = 0; tid < nitems(sta->txq); tid++) {
9602 struct lkpi_txq *ltxq;
9603
9604 if (sta->txq[tid] == NULL)
9605 continue;
9606
9607 if (sta->txq[tid]->ac != ac)
9608 continue;
9609
9610 ltxq = TXQ_TO_LTXQ(sta->txq[tid]);
9611 if ((ltxq->flags & LKPI_TXQ_STOPPED) == 0)
9612 continue;
9613
9614 ltxq->flags &= ~LKPI_TXQ_STOPPED;
9615
9616 if (!skb_queue_empty(<xq->skbq))
9617 lkpi_80211_mo_wake_tx_queue(hw, sta->txq[tid], false);
9618 }
9619 }
9620 rcu_read_unlock();
9621 }
9622 }
9623 }
9624 LKPI_80211_LHW_LVIF_UNLOCK(lhw);
9625 }
9626
9627 static void
9628 lkpi_ieee80211_wake_queues_locked(struct ieee80211_hw *hw)
9629 {
9630 int i;
9631
9632 IMPROVE_TXQ("Is this all/enough here?");
9633 for (i = 0; i < hw->queues; i++)
9634 lkpi_ieee80211_wake_queues(hw, i);
9635 }
9636
9637 void
9638 linuxkpi_ieee80211_wake_queues(struct ieee80211_hw *hw)
9639 {
9640 struct lkpi_hw *lhw;
9641 unsigned long flags;
9642
9643 lhw = HW_TO_LHW(hw);
9644
9645 spin_lock_irqsave(&lhw->txq_lock, flags);
9646 lkpi_ieee80211_wake_queues_locked(hw);
9647 spin_unlock_irqrestore(&lhw->txq_lock, flags);
9648 }
9649
9650 void
9651 linuxkpi_ieee80211_wake_queue(struct ieee80211_hw *hw, int qnum)
9652 {
9653 struct lkpi_hw *lhw;
9654 unsigned long flags;
9655
9656 KASSERT(qnum < hw->queues, ("%s: qnum %d >= hw->queues %d, hw %p\n",
9657 __func__, qnum, hw->queues, hw));
9658
9659 lhw = HW_TO_LHW(hw);
9660
9661 spin_lock_irqsave(&lhw->txq_lock, flags);
9662 lkpi_ieee80211_wake_queues(hw, qnum);
9663 spin_unlock_irqrestore(&lhw->txq_lock, flags);
9664 }
9665
9666 void
9667 linuxkpi_ieee80211_handle_wake_tx_queue(struct ieee80211_hw *hw,
9668 struct ieee80211_txq *txq)
9669 {
9670 struct lkpi_hw *lhw;
9671
9672 lhw = HW_TO_LHW(hw);
9673
9674 LKPI_80211_LHW_TXQ_LOCK(lhw);
9675 ieee80211_txq_schedule_start(hw, txq->ac);
9676 do {
9677 struct lkpi_txq *ltxq;
9678 struct ieee80211_txq *ntxq;
9679 struct ieee80211_tx_control control;
9680 struct sk_buff *skb;
9681
9682 ntxq = ieee80211_next_txq(hw, txq->ac);
9683 if (ntxq == NULL)
9684 break;
9685 ltxq = TXQ_TO_LTXQ(ntxq);
9686
9687 memset(&control, 0, sizeof(control));
9688 control.sta = ntxq->sta;
9689 do {
9690 skb = linuxkpi_ieee80211_tx_dequeue(hw, ntxq);
9691 if (skb == NULL)
9692 break;
9693 ltxq->frms_tx++;
9694 lkpi_80211_mo_tx(hw, &control, skb);
9695 } while(1);
9696
9697 ieee80211_return_txq(hw, ntxq, false);
9698 } while (1);
9699 ieee80211_txq_schedule_end(hw, txq->ac);
9700 LKPI_80211_LHW_TXQ_UNLOCK(lhw);
9701 }
9702
9703 /* -------------------------------------------------------------------------- */
9704
9705 /* This is just hardware queues. */
9706 /*
9707 * Being called from the driver thus use _bh() locking.
9708 */
9709 void
9710 linuxkpi_ieee80211_txq_schedule_start(struct ieee80211_hw *hw, uint8_t ac)
9711 {
9712 struct lkpi_hw *lhw;
9713
9714 lhw = HW_TO_LHW(hw);
9715
9716 if (ac >= IEEE80211_NUM_ACS) {
9717 ic_printf(lhw->ic, "%s: ac %u out of bounds.\n", __func__, ac);
9718 return;
9719 }
9720
9721 spin_lock_bh(&lhw->txq_scheduled_lock[ac]);
9722 IMPROVE("check AIRTIME_FAIRNESS");
9723 if (++lhw->txq_generation[ac] == 0)
9724 lhw->txq_generation[ac]++;
9725 spin_unlock_bh(&lhw->txq_scheduled_lock[ac]);
9726 }
9727
9728 struct ieee80211_txq *
9729 linuxkpi_ieee80211_next_txq(struct ieee80211_hw *hw, uint8_t ac)
9730 {
9731 struct lkpi_hw *lhw;
9732 struct ieee80211_txq *txq;
9733 struct lkpi_txq *ltxq;
9734
9735 lhw = HW_TO_LHW(hw);
9736 txq = NULL;
9737
9738 if (ac >= IEEE80211_NUM_ACS) {
9739 ic_printf(lhw->ic, "%s: ac %u out of bounds.\n", __func__, ac);
9740 return (NULL);
9741 }
9742
9743 spin_lock_bh(&lhw->txq_scheduled_lock[ac]);
9744
9745 /* Check that we are scheduled. */
9746 if (lhw->txq_generation[ac] == 0)
9747 goto out;
9748
9749 ltxq = TAILQ_FIRST(&lhw->txq_scheduled[ac]);
9750 if (ltxq == NULL)
9751 goto out;
9752 if (ltxq->txq_generation == lhw->txq_generation[ac])
9753 goto out;
9754 if ((ltxq->flags & (LKPI_TXQ_STOPPED|LKPI_TXQ_STOPPED_BA)) != 0)
9755 goto out;
9756
9757 IMPROVE("check AIRTIME_FAIRNESS");
9758
9759 TAILQ_REMOVE(&lhw->txq_scheduled[ac], ltxq, txq_entry);
9760 ltxq->txq_generation = lhw->txq_generation[ac];
9761 txq = <xq->txq;
9762 TAILQ_ELEM_INIT(ltxq, txq_entry);
9763
9764 out:
9765 spin_unlock_bh(&lhw->txq_scheduled_lock[ac]);
9766
9767 return (txq);
9768 }
9769
9770 void linuxkpi_ieee80211_schedule_txq(struct ieee80211_hw *hw,
9771 struct ieee80211_txq *txq, bool withoutpkts)
9772 {
9773 struct lkpi_hw *lhw;
9774 struct lkpi_txq *ltxq;
9775 bool ltxq_empty;
9776
9777 ltxq = TXQ_TO_LTXQ(txq);
9778
9779 /* Only schedule if work to do or asked to anyway. */
9780 LKPI_80211_LTXQ_LOCK(ltxq);
9781 ltxq_empty = skb_queue_empty(<xq->skbq);
9782 LKPI_80211_LTXQ_UNLOCK(ltxq);
9783 if (!withoutpkts && ltxq_empty)
9784 goto out;
9785
9786 lhw = HW_TO_LHW(hw);
9787 spin_lock_bh(&lhw->txq_scheduled_lock[txq->ac]);
9788 /*
9789 * Make sure we do not double-schedule. We do this by checking tqe_prev,
9790 * the previous entry in our tailq. tqe_prev is always valid if this entry
9791 * is queued, tqe_next may be NULL if this is the only element in the list.
9792 */
9793 if (ltxq->txq_entry.tqe_prev != NULL)
9794 goto unlock;
9795
9796 TAILQ_INSERT_TAIL(&lhw->txq_scheduled[txq->ac], ltxq, txq_entry);
9797 unlock:
9798 spin_unlock_bh(&lhw->txq_scheduled_lock[txq->ac]);
9799
9800 out:
9801 return;
9802 }
9803
9804 /* -------------------------------------------------------------------------- */
9805
9806 struct lkpi_cfg80211_bss {
9807 u_int refcnt;
9808 struct cfg80211_bss bss;
9809 };
9810
9811 struct lkpi_cfg80211_get_bss_iter_lookup {
9812 struct wiphy *wiphy;
9813 struct linuxkpi_ieee80211_channel *chan;
9814 const uint8_t *bssid;
9815 const uint8_t *ssid;
9816 size_t ssid_len;
9817 enum ieee80211_bss_type bss_type;
9818 enum ieee80211_privacy privacy;
9819
9820 /*
9821 * Something to store a copy of the result as the net80211 scan cache
9822 * is not refoucnted so a scan entry might go away any time.
9823 */
9824 bool match;
9825 struct cfg80211_bss *bss;
9826 };
9827
9828 static void
9829 lkpi_cfg80211_get_bss_iterf(void *arg, const struct ieee80211_scan_entry *se)
9830 {
9831 struct lkpi_cfg80211_get_bss_iter_lookup *lookup;
9832 size_t ielen;
9833
9834 lookup = arg;
9835
9836 /* Do not try to find another match. */
9837 if (lookup->match)
9838 return;
9839
9840 /* Nothing to store result. */
9841 if (lookup->bss == NULL)
9842 return;
9843
9844 if (lookup->privacy != IEEE80211_PRIVACY_ANY) {
9845 /* if (se->se_capinfo & IEEE80211_CAPINFO_PRIVACY) */
9846 /* We have no idea what to compare to as the drivers only request ANY */
9847 return;
9848 }
9849
9850 if (lookup->bss_type != IEEE80211_BSS_TYPE_ANY) {
9851 /* if (se->se_capinfo & (IEEE80211_CAPINFO_IBSS|IEEE80211_CAPINFO_ESS)) */
9852 /* We have no idea what to compare to as the drivers only request ANY */
9853 return;
9854 }
9855
9856 if (lookup->chan != NULL) {
9857 struct linuxkpi_ieee80211_channel *chan;
9858
9859 chan = linuxkpi_ieee80211_get_channel(lookup->wiphy,
9860 se->se_chan->ic_freq);
9861 if (chan == NULL || chan != lookup->chan)
9862 return;
9863 }
9864
9865 if (lookup->bssid && !IEEE80211_ADDR_EQ(lookup->bssid, se->se_bssid))
9866 return;
9867
9868 if (lookup->ssid) {
9869 if (lookup->ssid_len != se->se_ssid[1] ||
9870 se->se_ssid[1] == 0)
9871 return;
9872 if (memcmp(lookup->ssid, se->se_ssid+2, lookup->ssid_len) != 0)
9873 return;
9874 }
9875
9876 ielen = se->se_ies.len;
9877
9878 lookup->bss->ies = malloc(sizeof(*lookup->bss->ies) + ielen,
9879 M_LKPI80211, M_NOWAIT | M_ZERO);
9880 if (lookup->bss->ies == NULL)
9881 return;
9882
9883 lookup->bss->ies->data = (uint8_t *)lookup->bss->ies + sizeof(*lookup->bss->ies);
9884 lookup->bss->ies->len = ielen;
9885 if (ielen)
9886 memcpy(lookup->bss->ies->data, se->se_ies.data, ielen);
9887
9888 lookup->match = true;
9889 }
9890
9891 struct cfg80211_bss *
9892 linuxkpi_cfg80211_get_bss(struct wiphy *wiphy, struct linuxkpi_ieee80211_channel *chan,
9893 const uint8_t *bssid, const uint8_t *ssid, size_t ssid_len,
9894 enum ieee80211_bss_type bss_type, enum ieee80211_privacy privacy)
9895 {
9896 struct lkpi_cfg80211_bss *lbss;
9897 struct lkpi_cfg80211_get_bss_iter_lookup lookup;
9898 struct lkpi_hw *lhw;
9899 struct ieee80211vap *vap;
9900
9901 lhw = wiphy_priv(wiphy);
9902
9903 /* Let's hope we can alloc. */
9904 lbss = malloc(sizeof(*lbss), M_LKPI80211, M_NOWAIT | M_ZERO);
9905 if (lbss == NULL) {
9906 ic_printf(lhw->ic, "%s: alloc failed.\n", __func__);
9907 return (NULL);
9908 }
9909
9910 lookup.wiphy = wiphy;
9911 lookup.chan = chan;
9912 lookup.bssid = bssid;
9913 lookup.ssid = ssid;
9914 lookup.ssid_len = ssid_len;
9915 lookup.bss_type = bss_type;
9916 lookup.privacy = privacy;
9917 lookup.match = false;
9918 lookup.bss = &lbss->bss;
9919
9920 IMPROVE("Iterate over all VAPs comparing perm_addr and addresses?");
9921 vap = TAILQ_FIRST(&lhw->ic->ic_vaps);
9922 ieee80211_scan_iterate(vap, lkpi_cfg80211_get_bss_iterf, &lookup);
9923 if (!lookup.match) {
9924 free(lbss, M_LKPI80211);
9925 return (NULL);
9926 }
9927
9928 refcount_init(&lbss->refcnt, 1);
9929 return (&lbss->bss);
9930 }
9931
9932 void
9933 linuxkpi_cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *bss)
9934 {
9935 struct lkpi_cfg80211_bss *lbss;
9936
9937 lbss = container_of(bss, struct lkpi_cfg80211_bss, bss);
9938
9939 /* Free everything again on refcount ... */
9940 if (refcount_release(&lbss->refcnt)) {
9941 free(lbss->bss.ies, M_LKPI80211);
9942 free(lbss, M_LKPI80211);
9943 }
9944 }
9945
9946 void
9947 linuxkpi_cfg80211_bss_flush(struct wiphy *wiphy)
9948 {
9949 struct lkpi_hw *lhw;
9950 struct ieee80211com *ic;
9951 struct ieee80211vap *vap;
9952
9953 lhw = wiphy_priv(wiphy);
9954 ic = lhw->ic;
9955
9956 /*
9957 * If we haven't called ieee80211_ifattach() yet
9958 * or there is no VAP, there are no scans to flush.
9959 */
9960 if (ic == NULL ||
9961 (lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) == 0)
9962 return;
9963
9964 /* Should only happen on the current one? Not seen it late enough. */
9965 IEEE80211_LOCK(ic);
9966 TAILQ_FOREACH(vap, &ic->ic_vaps, iv_next)
9967 ieee80211_scan_flush(vap);
9968 IEEE80211_UNLOCK(ic);
9969 }
9970
9971 /* -------------------------------------------------------------------------- */
9972
9973 static bool
9974 cfg80211_chan_def_are_same(struct cfg80211_chan_def *cd1,
9975 struct cfg80211_chan_def *cd2)
9976 {
9977
9978 if (cd1 == cd2)
9979 return (true);
9980
9981 if (cd1 == NULL || cd2 == NULL)
9982 return (false);
9983
9984 if (cd1->chan != cd2->chan)
9985 return (false);
9986
9987 if (cd1->width != cd2->width)
9988 return (false);
9989
9990 if (cd1->center_freq1 != cd2->center_freq1)
9991 return (false);
9992
9993 if (cd1->center_freq2 != cd2->center_freq2)
9994 return (false);
9995
9996 if (cd1->punctured != cd2->punctured)
9997 return (false);
9998
9999 return (true);
10000 }
10001
10002 /*
10003 * hw->conf get initialized/set in various places for us:
10004 * - linuxkpi_ieee80211_alloc_hw(): flags
10005 * - linuxkpi_ieee80211_ifattach(): chandef
10006 * - lkpi_ic_vap_create(): listen_interval
10007 * - lkpi_ic_set_channel(): chandef, flags
10008 */
10009
10010 static int
10011 lkpi_80211_update_chandef(struct ieee80211_hw *hw,
10012 struct ieee80211_chanctx_conf *new)
10013 {
10014 struct lkpi_hw *lhw;
10015 struct cfg80211_chan_def *cd;
10016 uint32_t changed;
10017 int error;
10018 bool same;
10019
10020 lockdep_assert_wiphy(hw->wiphy);
10021
10022 lhw = HW_TO_LHW(hw);
10023 if (!lhw->emulate_chanctx)
10024 return (0);
10025
10026 if (new == NULL || new->def.chan == NULL) {
10027 /*
10028 * In case of remove "new" is NULL, we need to get us to some
10029 * basic channel width but we'd also need to set the channel
10030 * accordingly somewhere.
10031 * The same is true if we are scanning in which case the
10032 * scan_chandef should have a channel set.
10033 */
10034 if (lhw->scan_chandef.chan != NULL) {
10035 #ifdef LINUXKPI_DEBUG_80211
10036 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
10037 ic_printf(lhw->ic, "%s:%d: using scan_chandef %p\n",
10038 __func__, __LINE__, &lhw->scan_chandef);
10039 #endif
10040 cd = &lhw->scan_chandef;
10041 } else {
10042 #ifdef LINUXKPI_DEBUG_80211
10043 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
10044 ic_printf(lhw->ic, "%s:%d: using dflt_chandef %p\n",
10045 __func__, __LINE__, &lhw->dflt_chandef);
10046 #endif
10047 cd = &lhw->dflt_chandef;
10048 }
10049 } else {
10050 #ifdef LINUXKPI_DEBUG_80211
10051 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
10052 ic_printf(lhw->ic, "%s:%d: using chanctx %p chandef %p\n",
10053 __func__, __LINE__, new, &new->def);
10054 #endif
10055 cd = &new->def;
10056 }
10057
10058 changed = 0;
10059 same = cfg80211_chan_def_are_same(cd, &hw->conf.chandef);
10060 if (!same) {
10061 /* Copy; the chan pointer is fine and will stay valid. */
10062 hw->conf.chandef = *cd;
10063 changed |= IEEE80211_CONF_CHANGE_CHANNEL;
10064 }
10065 IMPROVE("IEEE80211_CONF_CHANGE_PS, IEEE80211_CONF_CHANGE_POWER");
10066
10067 #ifdef LINUXKPI_DEBUG_80211
10068 if ((linuxkpi_debug_80211 & D80211_CHANDEF) != 0)
10069 ic_printf(lhw->ic, "%s:%d: chanctx %p { %u } cd %p { %u } "
10070 "hw->conf.chandef %p { %u %d %u %u %u }, "
10071 "changed %#04x same %d\n",
10072 __func__, __LINE__,
10073 new, (new != NULL && new->def.chan != NULL) ?
10074 new->def.chan->center_freq : 0,
10075 cd, cd->chan->center_freq,
10076 &hw->conf.chandef, hw->conf.chandef.chan->center_freq,
10077 hw->conf.chandef.width,
10078 hw->conf.chandef.center_freq1,
10079 hw->conf.chandef.center_freq2,
10080 hw->conf.chandef.punctured,
10081 changed, same);
10082 #endif
10083
10084 if (changed == 0)
10085 return (0);
10086
10087 error = lkpi_80211_mo_config(hw, changed);
10088 return (error);
10089 }
10090
10091 int
10092 ieee80211_emulate_add_chanctx(struct ieee80211_hw *hw,
10093 struct ieee80211_chanctx_conf *chanctx_conf)
10094 {
10095 int error;
10096
10097 lockdep_assert_wiphy(hw->wiphy);
10098
10099 #ifdef LINUXKPI_DEBUG_80211
10100 if ((linuxkpi_debug_80211 & D80211_TRACE) != 0) {
10101 struct lkpi_hw *lhw;
10102
10103 lhw = HW_TO_LHW(hw);
10104 ic_printf(lhw->ic, "%s:%d: chanctx_conf %p\n",
10105 __func__, __LINE__, chanctx_conf);
10106 }
10107 #endif
10108
10109 hw->conf.radar_enabled = chanctx_conf->radar_enabled;
10110 error = lkpi_80211_update_chandef(hw, chanctx_conf);
10111 return (error);
10112 }
10113
10114 void
10115 ieee80211_emulate_remove_chanctx(struct ieee80211_hw *hw,
10116 struct ieee80211_chanctx_conf *chanctx_conf __unused)
10117 {
10118
10119 lockdep_assert_wiphy(hw->wiphy);
10120
10121 #ifdef LINUXKPI_DEBUG_80211
10122 if ((linuxkpi_debug_80211 & D80211_TRACE) != 0) {
10123 struct lkpi_hw *lhw;
10124
10125 lhw = HW_TO_LHW(hw);
10126 ic_printf(lhw->ic, "%s:%d: chanctx_conf %p\n",
10127 __func__, __LINE__, chanctx_conf);
10128 }
10129 #endif
10130
10131 hw->conf.radar_enabled = false;
10132 lkpi_80211_update_chandef(hw, NULL);
10133 }
10134
10135 void
10136 ieee80211_emulate_change_chanctx(struct ieee80211_hw *hw,
10137 struct ieee80211_chanctx_conf *chanctx_conf, uint32_t changed __unused)
10138 {
10139
10140 lockdep_assert_wiphy(hw->wiphy);
10141
10142 #ifdef LINUXKPI_DEBUG_80211
10143 if ((linuxkpi_debug_80211 & D80211_TRACE) != 0) {
10144 struct lkpi_hw *lhw;
10145
10146 lhw = HW_TO_LHW(hw);
10147 ic_printf(lhw->ic, "%s:%d: chanctx_conf %p\n",
10148 __func__, __LINE__, chanctx_conf);
10149 }
10150 #endif
10151
10152 hw->conf.radar_enabled = chanctx_conf->radar_enabled;
10153 lkpi_80211_update_chandef(hw, chanctx_conf);
10154 }
10155
10156 int
10157 ieee80211_emulate_switch_vif_chanctx(struct ieee80211_hw *hw,
10158 struct ieee80211_vif_chanctx_switch *vifs, int n_vifs,
10159 enum ieee80211_chanctx_switch_mode mode __unused)
10160 {
10161 struct ieee80211_chanctx_conf *chanctx_conf;
10162 int error;
10163
10164 lockdep_assert_wiphy(hw->wiphy);
10165
10166 /* Sanity check. */
10167 if (n_vifs <= 0)
10168 return (-EINVAL);
10169 if (vifs == NULL || vifs[0].new_ctx == NULL)
10170 return (-EINVAL);
10171
10172 /*
10173 * What to do if n_vifs > 1?
10174 * Does that make sense for drivers not supporting chanctx?
10175 */
10176 hw->conf.radar_enabled = vifs[0].new_ctx->radar_enabled;
10177 chanctx_conf = vifs[0].new_ctx;
10178 error = lkpi_80211_update_chandef(hw, chanctx_conf);
10179 return (error);
10180 }
10181
10182 /* -------------------------------------------------------------------------- */
10183 /* LinuxKPI 802.11 PM. */
10184 int
10185 lkpi_80211_suspend(struct ieee80211com *ic, pm_message_t state)
10186 {
10187 struct lkpi_hw *lhw;
10188 struct ieee80211_hw *hw;
10189 int error;
10190
10191 lhw = ic->ic_softc;
10192 hw = LHW_TO_HW(lhw);
10193 error = 0;
10194
10195 /* Check:
10196 * - device_set_wakeup_capable() / device_can_wakeup()
10197 * - hw->wiphy->wowlan to be non-NULL, if so contents.
10198 * - hw->wiphy->max_sched_scan_ssids (rtw88)
10199 */
10200 if ((lkpi_suspend_type & 0x2) != 0) {
10201 struct cfg80211_wowlan wowlan;
10202
10203 IMPROVE("various options for WoWLAN");
10204 memset(&wowlan, 0, sizeof(wowlan));
10205 wiphy_lock(hw->wiphy);
10206 error = lkpi_80211_mo_suspend(hw, &wowlan);
10207 wiphy_unlock(hw->wiphy);
10208 if (error == EOPNOTSUPP)
10209 error = 0;
10210 }
10211 if ((lkpi_suspend_type & 0x1) != 0) {
10212 struct lkpi_vif *lvif;
10213
10214 ieee80211_suspend_all(ic);
10215
10216 wiphy_lock(hw->wiphy);
10217 /*
10218 * At the end of this net80211 will run a task to call
10219 * (*ic_parent)() which is entirely unhelpful as we do not
10220 * know when it will happen. So deal with it here.
10221 */
10222 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
10223 lkpi_80211_mo_remove_interface(hw, LVIF_TO_VIF(lvif));
10224 }
10225
10226 if ((lhw->sc_flags & LKPI_MAC80211_DRV_STARTED) != 0)
10227 lkpi_80211_mo_stop(hw, true);
10228 wiphy_unlock(hw->wiphy);
10229 }
10230
10231 if (error < 0)
10232 error = -error;
10233
10234 if (error != 0)
10235 ic_printf(ic, "%s: SUSPEND FAILED: %d\n", __func__, error);
10236
10237 return (error);
10238 }
10239
10240 int
10241 lkpi_80211_resume(struct ieee80211com *ic)
10242 {
10243 struct lkpi_hw *lhw;
10244 struct ieee80211_hw *hw;
10245 int error;
10246 bool hw_scan_running;
10247
10248 lhw = ic->ic_softc;
10249 hw = LHW_TO_HW(lhw);
10250 error = 0;
10251
10252 /*
10253 * Ongoing HW scans during suspend are a problem on resume.
10254 * Be verbose about that.
10255 */
10256 LKPI_80211_LHW_SCAN_LOCK(lhw);
10257 hw_scan_running = (lhw->scan_flags & (LKPI_LHW_SCAN_RUNNING|LKPI_LHW_SCAN_HW)) != 0;
10258 LKPI_80211_LHW_SCAN_UNLOCK(lhw);
10259 if (hw_scan_running)
10260 ic_printf(ic, "%s: WARNING: ongoing hw scan on resume!\n", __func__);
10261
10262 if ((lkpi_suspend_type & 0x1) != 0) {
10263 struct lkpi_vif *lvif;
10264
10265 wiphy_lock(hw->wiphy);
10266 error = lkpi_80211_mo_start(hw);
10267 if (error != 0 && error != EEXIST) {
10268 ic_printf(ic, "%s: mo_start failed: %d\n",
10269 __func__, error);
10270 wiphy_unlock(hw->wiphy);
10271 goto err;
10272 }
10273
10274 TAILQ_FOREACH(lvif, &lhw->lvif_head, lvif_entry) {
10275 error = lkpi_80211_mo_add_interface(hw, LVIF_TO_VIF(lvif));
10276 if (error != 0) {
10277 struct ieee80211vap *vap;
10278
10279 vap = LVIF_TO_VAP(lvif);
10280 ic_printf(ic, "%s: mo_add_interface %s failed: %d\n",
10281 __func__, if_name(vap->iv_ifp), error);
10282 wiphy_unlock(hw->wiphy);
10283 goto err;
10284 }
10285 }
10286 wiphy_unlock(hw->wiphy);
10287
10288 ieee80211_resume_all(ic);
10289 }
10290
10291 if ((lkpi_suspend_type & 0x2) != 0) {
10292 wiphy_lock(hw->wiphy);
10293 error = lkpi_80211_mo_resume(hw);
10294 wiphy_unlock(hw->wiphy);
10295 if (error == EOPNOTSUPP)
10296 error = 0;
10297 }
10298
10299 err:
10300 if (error < 0)
10301 error = -error;
10302
10303 return (error);
10304 }
10305
10306 /* -------------------------------------------------------------------------- */
10307 MODULE_VERSION(linuxkpi_wlan, 1);
10308 MODULE_DEPEND(linuxkpi_wlan, linuxkpi, 1, 1, 1);
10309 MODULE_DEPEND(linuxkpi_wlan, wlan, 1, 1, 1);
10310