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