2 * Copyright (c) 2021-2022 The FreeBSD Foundation
4 * This software was developed by Björn Zeeb under sponsorship from
5 * the FreeBSD Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/param.h>
30 #include <sys/types.h>
31 #include <sys/kernel.h>
32 #include <sys/errno.h>
34 #define LINUXKPI_NET80211
35 #include <net/mac80211.h>
37 #include "linux_80211.h"
39 /* Could be a different tracing framework later. */
40 #ifdef LINUXKPI_DEBUG_80211
41 #define LKPI_80211_TRACE_MO(fmt, ...) \
42 if (linuxkpi_debug_80211 & D80211_TRACE_MO) \
43 printf("LKPI_80211_TRACE_MO %s:%d: %d %d %u_" fmt "\n", \
44 __func__, __LINE__, curcpu, curthread->td_tid, \
45 (unsigned int)ticks, __VA_ARGS__)
47 #define LKPI_80211_TRACE_MO(...) do { } while(0)
51 lkpi_80211_mo_start(struct ieee80211_hw *hw)
57 if (lhw->ops->start == NULL) {
62 if ((lhw->sc_flags & LKPI_MAC80211_DRV_STARTED)) {
63 /* Trying to start twice is an error. */
67 LKPI_80211_TRACE_MO("hw %p", hw);
68 error = lhw->ops->start(hw);
70 lhw->sc_flags |= LKPI_MAC80211_DRV_STARTED;
77 lkpi_80211_mo_stop(struct ieee80211_hw *hw)
82 if (lhw->ops->stop == NULL)
85 LKPI_80211_TRACE_MO("hw %p", hw);
87 lhw->sc_flags &= ~LKPI_MAC80211_DRV_STARTED;
91 lkpi_80211_mo_get_antenna(struct ieee80211_hw *hw, u32 *txs, u32 *rxs)
97 if (lhw->ops->get_antenna == NULL) {
102 LKPI_80211_TRACE_MO("hw %p", hw);
103 error = lhw->ops->get_antenna(hw, txs, rxs);
110 lkpi_80211_mo_set_frag_threshold(struct ieee80211_hw *hw, uint32_t frag_th)
116 if (lhw->ops->set_frag_threshold == NULL) {
121 LKPI_80211_TRACE_MO("hw %p frag_th %u", hw, frag_th);
122 error = lhw->ops->set_frag_threshold(hw, frag_th);
129 lkpi_80211_mo_set_rts_threshold(struct ieee80211_hw *hw, uint32_t rts_th)
135 if (lhw->ops->set_rts_threshold == NULL) {
140 LKPI_80211_TRACE_MO("hw %p rts_th %u", hw, rts_th);
141 error = lhw->ops->set_rts_threshold(hw, rts_th);
149 lkpi_80211_mo_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
152 struct lkpi_vif *lvif;
156 if (lhw->ops->add_interface == NULL) {
161 lvif = VIF_TO_LVIF(vif);
162 LKPI_80211_LVIF_LOCK(lvif);
163 if (lvif->added_to_drv) {
164 LKPI_80211_LVIF_UNLOCK(lvif);
165 /* Trying to add twice is an error. */
169 LKPI_80211_LVIF_UNLOCK(lvif);
171 LKPI_80211_TRACE_MO("hw %p vif %p", hw, vif);
172 error = lhw->ops->add_interface(hw, vif);
174 LKPI_80211_LVIF_LOCK(lvif);
175 lvif->added_to_drv = true;
176 LKPI_80211_LVIF_UNLOCK(lvif);
184 lkpi_80211_mo_remove_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
187 struct lkpi_vif *lvif;
190 if (lhw->ops->remove_interface == NULL)
193 lvif = VIF_TO_LVIF(vif);
194 LKPI_80211_LVIF_LOCK(lvif);
195 if (!lvif->added_to_drv) {
196 LKPI_80211_LVIF_UNLOCK(lvif);
199 LKPI_80211_LVIF_UNLOCK(lvif);
201 LKPI_80211_TRACE_MO("hw %p vif %p", hw, vif);
202 lhw->ops->remove_interface(hw, vif);
203 LKPI_80211_LVIF_LOCK(lvif);
204 lvif->added_to_drv = false;
205 LKPI_80211_LVIF_UNLOCK(lvif);
210 lkpi_80211_mo_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
211 struct ieee80211_scan_request *sr)
217 * MUST NOT return EPERM as that is a "magic number 1" based on rtw88
218 * driver indicating hw_scan is not supported despite the ops call
223 if (lhw->ops->hw_scan == NULL) {
224 /* Return magic number to use sw scan. */
229 LKPI_80211_TRACE_MO("CALLING hw %p vif %p sr %p", hw, vif, sr);
230 error = lhw->ops->hw_scan(hw, vif, sr);
231 LKPI_80211_TRACE_MO("RETURNING hw %p vif %p sr %p error %d", hw, vif, sr, error);
238 lkpi_80211_mo_cancel_hw_scan(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
243 if (lhw->ops->cancel_hw_scan == NULL)
246 LKPI_80211_TRACE_MO("hw %p vif %p", hw, vif);
247 lhw->ops->cancel_hw_scan(hw, vif);
251 lkpi_80211_mo_sw_scan_complete(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
256 if (lhw->ops->sw_scan_complete == NULL)
259 LKPI_80211_TRACE_MO("hw %p vif %p", hw, vif);
260 lhw->ops->sw_scan_complete(hw, vif);
261 lhw->scan_flags &= ~LKPI_LHW_SCAN_RUNNING;
265 lkpi_80211_mo_sw_scan_start(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
271 if (lhw->ops->sw_scan_start == NULL)
274 LKPI_80211_TRACE_MO("hw %p vif %p", hw, vif);
275 lhw->ops->sw_scan_start(hw, vif, addr);
280 * We keep the Linux type here; it really is an uintptr_t.
283 lkpi_80211_mo_prepare_multicast(struct ieee80211_hw *hw,
284 struct netdev_hw_addr_list *mc_list)
290 if (lhw->ops->prepare_multicast == NULL)
293 LKPI_80211_TRACE_MO("hw %p mc_list %p", hw, mc_list);
294 ptr = lhw->ops->prepare_multicast(hw, mc_list);
299 lkpi_80211_mo_configure_filter(struct ieee80211_hw *hw, unsigned int changed_flags,
300 unsigned int *total_flags, u64 mc_ptr)
305 if (lhw->ops->configure_filter == NULL)
311 LKPI_80211_TRACE_MO("hw %p changed_flags %#x total_flags %p mc_ptr %ju", hw, changed_flags, total_flags, (uintmax_t)mc_ptr);
312 lhw->ops->configure_filter(hw, changed_flags, total_flags, mc_ptr);
317 * So far we only called sta_{add,remove} as an alternative to sta_state.
318 * Let's keep the implementation simpler and hide sta_{add,remove} under the
319 * hood here calling them if state_state is not available from mo_sta_state.
322 lkpi_80211_mo_sta_add(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
323 struct ieee80211_sta *sta)
326 struct lkpi_sta *lsta;
330 if (lhw->ops->sta_add == NULL) {
335 lsta = STA_TO_LSTA(sta);
336 if (lsta->added_to_drv) {
341 LKPI_80211_TRACE_MO("hw %p vif %p sta %p", hw, vif, sta);
342 error = lhw->ops->sta_add(hw, vif, sta);
344 lsta->added_to_drv = true;
351 lkpi_80211_mo_sta_remove(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
352 struct ieee80211_sta *sta)
355 struct lkpi_sta *lsta;
359 if (lhw->ops->sta_remove == NULL) {
364 lsta = STA_TO_LSTA(sta);
365 if (!lsta->added_to_drv) {
366 /* If we never added the sta, do not complain on cleanup. */
371 LKPI_80211_TRACE_MO("hw %p vif %p sta %p", hw, vif, sta);
372 error = lhw->ops->sta_remove(hw, vif, sta);
374 lsta->added_to_drv = false;
381 lkpi_80211_mo_sta_state(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
382 struct lkpi_sta *lsta, enum ieee80211_sta_state nstate)
385 struct ieee80211_sta *sta;
389 sta = LSTA_TO_STA(lsta);
390 if (lhw->ops->sta_state != NULL) {
391 LKPI_80211_TRACE_MO("hw %p vif %p sta %p nstate %d", hw, vif, sta, nstate);
392 error = lhw->ops->sta_state(hw, vif, sta, lsta->state, nstate);
394 if (nstate == IEEE80211_STA_NOTEXIST)
395 lsta->added_to_drv = false;
397 lsta->added_to_drv = true;
398 lsta->state = nstate;
403 /* XXX-BZ is the change state AUTH or ASSOC here? */
404 if (lsta->state < IEEE80211_STA_ASSOC && nstate == IEEE80211_STA_ASSOC) {
405 error = lkpi_80211_mo_sta_add(hw, vif, sta);
407 lsta->added_to_drv = true;
408 } else if (lsta->state >= IEEE80211_STA_ASSOC &&
409 nstate < IEEE80211_STA_ASSOC) {
410 error = lkpi_80211_mo_sta_remove(hw, vif, sta);
412 lsta->added_to_drv = false;
417 lsta->state = nstate;
420 /* XXX-BZ should we manage state in here? */
425 lkpi_80211_mo_config(struct ieee80211_hw *hw, uint32_t changed)
431 if (lhw->ops->config == NULL) {
436 LKPI_80211_TRACE_MO("hw %p changed %u", hw, changed);
437 error = lhw->ops->config(hw, changed);
445 lkpi_80211_mo_assign_vif_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
446 struct ieee80211_bss_conf *conf, struct ieee80211_chanctx_conf *chanctx_conf)
452 if (lhw->ops->assign_vif_chanctx == NULL) {
457 LKPI_80211_TRACE_MO("hw %p vif %p bss_conf %p chanctx_conf %p",
458 hw, vif, conf, chanctx_conf);
459 error = lhw->ops->assign_vif_chanctx(hw, vif, conf, chanctx_conf);
461 vif->chanctx_conf = chanctx_conf;
468 lkpi_80211_mo_unassign_vif_chanctx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
469 struct ieee80211_bss_conf *conf, struct ieee80211_chanctx_conf **chanctx_conf)
474 if (lhw->ops->unassign_vif_chanctx == NULL)
477 if (*chanctx_conf == NULL)
480 LKPI_80211_TRACE_MO("hw %p vif %p bss_conf %p chanctx_conf %p",
481 hw, vif, conf, *chanctx_conf);
482 lhw->ops->unassign_vif_chanctx(hw, vif, conf, *chanctx_conf);
483 *chanctx_conf = NULL;
488 lkpi_80211_mo_add_chanctx(struct ieee80211_hw *hw,
489 struct ieee80211_chanctx_conf *chanctx_conf)
492 struct lkpi_chanctx *lchanctx;
496 if (lhw->ops->add_chanctx == NULL) {
501 LKPI_80211_TRACE_MO("hw %p chanctx_conf %p", hw, chanctx_conf);
502 error = lhw->ops->add_chanctx(hw, chanctx_conf);
504 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
505 lchanctx->added_to_drv = true;
513 lkpi_80211_mo_change_chanctx(struct ieee80211_hw *hw,
514 struct ieee80211_chanctx_conf *chanctx_conf, uint32_t changed)
519 if (lhw->ops->change_chanctx == NULL)
522 LKPI_80211_TRACE_MO("hw %p chanctx_conf %p changed %u", hw, chanctx_conf, changed);
523 lhw->ops->change_chanctx(hw, chanctx_conf, changed);
527 lkpi_80211_mo_remove_chanctx(struct ieee80211_hw *hw,
528 struct ieee80211_chanctx_conf *chanctx_conf)
531 struct lkpi_chanctx *lchanctx;
534 if (lhw->ops->remove_chanctx == NULL)
537 LKPI_80211_TRACE_MO("hw %p chanctx_conf %p", hw, chanctx_conf);
538 lhw->ops->remove_chanctx(hw, chanctx_conf);
539 lchanctx = CHANCTX_CONF_TO_LCHANCTX(chanctx_conf);
540 lchanctx->added_to_drv = false;
544 lkpi_80211_mo_bss_info_changed(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
545 struct ieee80211_bss_conf *conf, uint64_t changed)
550 if (lhw->ops->link_info_changed == NULL &&
551 lhw->ops->bss_info_changed == NULL)
554 LKPI_80211_TRACE_MO("hw %p vif %p conf %p changed %#jx", hw, vif, conf, (uintmax_t)changed);
555 if (lhw->ops->link_info_changed != NULL)
556 lhw->ops->link_info_changed(hw, vif, conf, changed);
558 lhw->ops->bss_info_changed(hw, vif, conf, changed);
562 lkpi_80211_mo_conf_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
563 uint32_t link_id, uint16_t ac, const struct ieee80211_tx_queue_params *txqp)
569 if (lhw->ops->conf_tx == NULL) {
574 LKPI_80211_TRACE_MO("hw %p vif %p link_id %u ac %u txpq %p",
575 hw, vif, link_id, ac, txqp);
576 error = lhw->ops->conf_tx(hw, vif, link_id, ac, txqp);
583 lkpi_80211_mo_flush(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
584 uint32_t nqueues, bool drop)
589 if (lhw->ops->flush == NULL)
592 LKPI_80211_TRACE_MO("hw %p vif %p nqueues %u drop %d", hw, vif, nqueues, drop);
593 lhw->ops->flush(hw, vif, nqueues, drop);
597 lkpi_80211_mo_mgd_prepare_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
598 struct ieee80211_prep_tx_info *txinfo)
603 if (lhw->ops->mgd_prepare_tx == NULL)
606 LKPI_80211_TRACE_MO("hw %p vif %p txinfo %p", hw, vif, txinfo);
607 lhw->ops->mgd_prepare_tx(hw, vif, txinfo);
611 lkpi_80211_mo_mgd_complete_tx(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
612 struct ieee80211_prep_tx_info *txinfo)
617 if (lhw->ops->mgd_complete_tx == NULL)
620 LKPI_80211_TRACE_MO("hw %p vif %p txinfo %p", hw, vif, txinfo);
621 lhw->ops->mgd_complete_tx(hw, vif, txinfo);
625 lkpi_80211_mo_tx(struct ieee80211_hw *hw, struct ieee80211_tx_control *txctrl,
631 if (lhw->ops->tx == NULL)
634 LKPI_80211_TRACE_MO("hw %p txctrl %p skb %p", hw, txctrl, skb);
635 lhw->ops->tx(hw, txctrl, skb);
639 lkpi_80211_mo_wake_tx_queue(struct ieee80211_hw *hw, struct ieee80211_txq *txq)
644 if (lhw->ops->wake_tx_queue == NULL)
647 LKPI_80211_TRACE_MO("hw %p txq %p", hw, txq);
648 lhw->ops->wake_tx_queue(hw, txq);
652 lkpi_80211_mo_sync_rx_queues(struct ieee80211_hw *hw)
657 if (lhw->ops->sync_rx_queues == NULL)
660 LKPI_80211_TRACE_MO("hw %p", hw);
661 lhw->ops->sync_rx_queues(hw);
665 lkpi_80211_mo_sta_pre_rcu_remove(struct ieee80211_hw *hw,
666 struct ieee80211_vif *vif, struct ieee80211_sta *sta)
671 if (lhw->ops->sta_pre_rcu_remove == NULL)
674 LKPI_80211_TRACE_MO("hw %p vif %p sta %p", hw, vif, sta);
675 lhw->ops->sta_pre_rcu_remove(hw, vif, sta);
679 lkpi_80211_mo_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
680 struct ieee80211_vif *vif, struct ieee80211_sta *sta,
681 struct ieee80211_key_conf *kc)
687 if (lhw->ops->set_key == NULL) {
692 LKPI_80211_TRACE_MO("hw %p cmd %d vif %p sta %p kc %p", hw, cmd, vif, sta, kc);
693 error = lhw->ops->set_key(hw, cmd, vif, sta, kc);
700 lkpi_80211_mo_ampdu_action(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
701 struct ieee80211_ampdu_params *params)
707 if (lhw->ops->ampdu_action == NULL) {
712 LKPI_80211_TRACE_MO("hw %p vif %p params %p { %p, %d, %u, %u, %u, %u, %d }",
713 hw, vif, params, params->sta, params->action, params->buf_size,
714 params->timeout, params->ssn, params->tid, params->amsdu);
715 error = lhw->ops->ampdu_action(hw, vif, params);