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