1 /******************************************************************************
3 * This file is provided under a dual BSD/GPLv2 license. When using or
4 * redistributing this file, you may do so under either license.
8 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * The full GNU General Public License is included in this distribution
25 * in the file called COPYING.
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
33 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
34 * All rights reserved.
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62 *****************************************************************************/
64 #include <linux/etherdevice.h>
65 #include <net/mac80211.h>
71 const u8 iwl_mvm_ac_to_tx_fifo[] = {
78 struct iwl_mvm_mac_iface_iterator_data {
80 struct ieee80211_vif *vif;
81 unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
82 unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
83 unsigned long used_hw_queues[BITS_TO_LONGS(IWL_MVM_FIRST_AGG_QUEUE)];
84 enum iwl_tsf_id preferred_tsf;
88 static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
89 struct ieee80211_vif *vif)
91 struct iwl_mvm_mac_iface_iterator_data *data = _data;
92 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
95 /* Iterator may already find the interface being added -- skip it */
96 if (vif == data->vif) {
97 data->found_vif = true;
101 /* Mark the queues used by the vif */
102 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
103 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
104 __set_bit(vif->hw_queue[ac], data->used_hw_queues);
106 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
107 __set_bit(vif->cab_queue, data->used_hw_queues);
110 * Mark MAC IDs as used by clearing the available bit, and
111 * (below) mark TSFs as used if their existing use is not
112 * compatible with the new interface type.
113 * No locking or atomic bit operations are needed since the
114 * data is on the stack of the caller function.
116 __clear_bit(mvmvif->id, data->available_mac_ids);
119 * The TSF is a hardware/firmware resource, there are 4 and
120 * the driver should assign and free them as needed. However,
121 * there are cases where 2 MACs should share the same TSF ID
122 * for the purpose of clock sync, an optimization to avoid
123 * clock drift causing overlapping TBTTs/DTIMs for a GO and
124 * client in the system.
126 * The firmware will decide according to the MAC type which
127 * will be the master and slave. Clients that need to sync
128 * with a remote station will be the master, and an AP or GO
131 * Depending on the new interface type it can be slaved to
132 * or become the master of an existing interface.
134 switch (data->vif->type) {
135 case NL80211_IFTYPE_STATION:
137 * The new interface is client, so if the existing one
138 * we're iterating is an AP, the TSF should be used to
139 * avoid drift between the new client and existing AP,
140 * the existing AP will get drift updates from the new
141 * client context in this case
143 if (vif->type == NL80211_IFTYPE_AP) {
144 if (data->preferred_tsf == NUM_TSF_IDS &&
145 test_bit(mvmvif->tsf_id, data->available_tsf_ids))
146 data->preferred_tsf = mvmvif->tsf_id;
150 case NL80211_IFTYPE_AP:
152 * The new interface is AP/GO, so should get drift
153 * updates from an existing client or use the same
154 * TSF as an existing GO. There's no drift between
155 * TSFs internally but if they used different TSFs
156 * then a new client MAC could update one of them
157 * and cause drift that way.
159 if (vif->type == NL80211_IFTYPE_STATION ||
160 vif->type == NL80211_IFTYPE_AP) {
161 if (data->preferred_tsf == NUM_TSF_IDS &&
162 test_bit(mvmvif->tsf_id, data->available_tsf_ids))
163 data->preferred_tsf = mvmvif->tsf_id;
169 * For all other interface types there's no need to
170 * take drift into account. Either they're exclusive
171 * like IBSS and monitor, or we don't care much about
172 * their TSF (like P2P Device), but we won't be able
173 * to share the TSF resource.
179 * Unless we exited above, we can't share the TSF resource
180 * that the virtual interface we're iterating over is using
181 * with the new one, so clear the available bit and if this
182 * was the preferred one, reset that as well.
184 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
186 if (data->preferred_tsf == mvmvif->tsf_id)
187 data->preferred_tsf = NUM_TSF_IDS;
191 * Get the mask of the queus used by the vif
193 u32 iwl_mvm_mac_get_queues_mask(struct iwl_mvm *mvm,
194 struct ieee80211_vif *vif)
198 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
199 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
201 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
202 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
203 qmask |= BIT(vif->hw_queue[ac]);
208 static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
209 struct ieee80211_vif *vif)
211 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
212 struct iwl_mvm_mac_iface_iterator_data data = {
215 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
216 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
217 /* no preference yet */
218 .preferred_tsf = NUM_TSF_IDS,
220 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
221 BIT(IWL_MVM_AUX_QUEUE) |
222 BIT(IWL_MVM_CMD_QUEUE)
230 * Allocate a MAC ID and a TSF for this MAC, along with the queues
231 * and other resources.
235 * Before the iterator, we start with all MAC IDs and TSFs available.
237 * During iteration, all MAC IDs are cleared that are in use by other
238 * virtual interfaces, and all TSF IDs are cleared that can't be used
239 * by this new virtual interface because they're used by an interface
240 * that can't share it with the new one.
241 * At the same time, we check if there's a preferred TSF in the case
242 * that we should share it with another interface.
245 /* Currently, MAC ID 0 should be used only for the managed vif */
246 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
247 __clear_bit(0, data.available_mac_ids);
249 ieee80211_iterate_active_interfaces_atomic(
250 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
251 iwl_mvm_mac_iface_iterator, &data);
254 * In the case we're getting here during resume, it's similar to
255 * firmware restart, and with RESUME_ALL the iterator will find
256 * the vif being added already.
257 * We don't want to reassign any IDs in either case since doing
258 * so would probably assign different IDs (as interfaces aren't
259 * necessarily added in the same order), but the old IDs were
260 * preserved anyway, so skip ID assignment for both resume and
266 /* Therefore, in recovery, we can't get here */
267 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
269 mvmvif->id = find_first_bit(data.available_mac_ids,
270 NUM_MAC_INDEX_DRIVER);
271 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
272 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
277 if (data.preferred_tsf != NUM_TSF_IDS)
278 mvmvif->tsf_id = data.preferred_tsf;
280 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
282 if (mvmvif->tsf_id == NUM_TSF_IDS) {
283 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
290 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
291 mvmvif->time_event_data.id = TE_MAX;
293 /* No need to allocate data queues to P2P Device MAC.*/
294 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
295 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
296 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
301 /* Find available queues, and allocate them to the ACs */
302 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
303 u8 queue = find_first_zero_bit(data.used_hw_queues,
304 IWL_MVM_FIRST_AGG_QUEUE);
306 if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
307 IWL_ERR(mvm, "Failed to allocate queue\n");
312 __set_bit(queue, data.used_hw_queues);
313 vif->hw_queue[ac] = queue;
316 /* Allocate the CAB queue for softAP and GO interfaces */
317 if (vif->type == NL80211_IFTYPE_AP) {
318 u8 queue = find_first_zero_bit(data.used_hw_queues,
319 IWL_MVM_FIRST_AGG_QUEUE);
321 if (queue >= IWL_MVM_FIRST_AGG_QUEUE) {
322 IWL_ERR(mvm, "Failed to allocate cab queue\n");
327 vif->cab_queue = queue;
329 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
332 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
333 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
338 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
339 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
340 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
344 int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
349 lockdep_assert_held(&mvm->mutex);
351 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
356 case NL80211_IFTYPE_P2P_DEVICE:
357 iwl_trans_ac_txq_enable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE,
360 case NL80211_IFTYPE_AP:
361 iwl_trans_ac_txq_enable(mvm->trans, vif->cab_queue,
362 IWL_MVM_TX_FIFO_MCAST);
365 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
366 iwl_trans_ac_txq_enable(mvm->trans, vif->hw_queue[ac],
367 iwl_mvm_ac_to_tx_fifo[ac]);
374 void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
378 lockdep_assert_held(&mvm->mutex);
381 case NL80211_IFTYPE_P2P_DEVICE:
382 iwl_trans_txq_disable(mvm->trans, IWL_MVM_OFFCHANNEL_QUEUE);
384 case NL80211_IFTYPE_AP:
385 iwl_trans_txq_disable(mvm->trans, vif->cab_queue);
388 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
389 iwl_trans_txq_disable(mvm->trans, vif->hw_queue[ac]);
393 static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
394 struct ieee80211_vif *vif,
395 enum ieee80211_band band,
396 u8 *cck_rates, u8 *ofdm_rates)
398 struct ieee80211_supported_band *sband;
399 unsigned long basic = vif->bss_conf.basic_rates;
400 int lowest_present_ofdm = 100;
401 int lowest_present_cck = 100;
406 sband = mvm->hw->wiphy->bands[band];
408 for_each_set_bit(i, &basic, BITS_PER_LONG) {
409 int hw = sband->bitrates[i].hw_value;
410 if (hw >= IWL_FIRST_OFDM_RATE) {
411 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
412 if (lowest_present_ofdm > hw)
413 lowest_present_ofdm = hw;
415 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
418 if (lowest_present_cck > hw)
419 lowest_present_cck = hw;
424 * Now we've got the basic rates as bitmaps in the ofdm and cck
425 * variables. This isn't sufficient though, as there might not
426 * be all the right rates in the bitmap. E.g. if the only basic
427 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
428 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
430 * [...] a STA responding to a received frame shall transmit
431 * its Control Response frame [...] at the highest rate in the
432 * BSSBasicRateSet parameter that is less than or equal to the
433 * rate of the immediately previous frame in the frame exchange
434 * sequence ([...]) and that is of the same modulation class
435 * ([...]) as the received frame. If no rate contained in the
436 * BSSBasicRateSet parameter meets these conditions, then the
437 * control frame sent in response to a received frame shall be
438 * transmitted at the highest mandatory rate of the PHY that is
439 * less than or equal to the rate of the received frame, and
440 * that is of the same modulation class as the received frame.
442 * As a consequence, we need to add all mandatory rates that are
443 * lower than all of the basic rates to these bitmaps.
446 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
447 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
448 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
449 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
450 /* 6M already there or needed so always add */
451 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
454 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
456 * - if no CCK rates are basic, it must be ERP since there must
457 * be some basic rates at all, so they're OFDM => ERP PHY
458 * (or we're in 5 GHz, and the cck bitmap will never be used)
459 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
460 * - if 5.5M is basic, 1M and 2M are mandatory
461 * - if 2M is basic, 1M is mandatory
462 * - if 1M is basic, that's the only valid ACK rate.
463 * As a consequence, it's not as complicated as it sounds, just add
464 * any lower rates to the ACK rate bitmap.
466 if (IWL_RATE_11M_INDEX < lowest_present_cck)
467 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
468 if (IWL_RATE_5M_INDEX < lowest_present_cck)
469 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
470 if (IWL_RATE_2M_INDEX < lowest_present_cck)
471 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
472 /* 1M already there or needed so always add */
473 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
479 static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
480 struct ieee80211_vif *vif,
481 struct iwl_mac_ctx_cmd *cmd,
484 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
485 struct ieee80211_chanctx_conf *chanctx;
486 u8 cck_ack_rates, ofdm_ack_rates;
489 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
491 cmd->action = cpu_to_le32(action);
494 case NL80211_IFTYPE_STATION:
496 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
498 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
500 case NL80211_IFTYPE_AP:
501 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
503 case NL80211_IFTYPE_MONITOR:
504 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
506 case NL80211_IFTYPE_P2P_DEVICE:
507 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
509 case NL80211_IFTYPE_ADHOC:
510 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
516 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
518 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
519 if (vif->bss_conf.bssid)
520 memcpy(cmd->bssid_addr, vif->bss_conf.bssid, ETH_ALEN);
522 eth_broadcast_addr(cmd->bssid_addr);
525 chanctx = rcu_dereference(vif->chanctx_conf);
526 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
527 : IEEE80211_BAND_2GHZ,
528 &cck_ack_rates, &ofdm_ack_rates);
531 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
532 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
534 cmd->cck_short_preamble =
535 cpu_to_le32(vif->bss_conf.use_short_preamble ?
536 MAC_FLG_SHORT_PREAMBLE : 0);
538 cpu_to_le32(vif->bss_conf.use_short_slot ?
539 MAC_FLG_SHORT_SLOT : 0);
541 for (i = 0; i < AC_NUM; i++) {
542 cmd->ac[i].cw_min = cpu_to_le16(mvmvif->queue_params[i].cw_min);
543 cmd->ac[i].cw_max = cpu_to_le16(mvmvif->queue_params[i].cw_max);
544 cmd->ac[i].aifsn = mvmvif->queue_params[i].aifs;
545 cmd->ac[i].edca_txop =
546 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
547 cmd->ac[i].fifos_mask = BIT(iwl_mvm_ac_to_tx_fifo[i]);
550 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
551 if (vif->type == NL80211_IFTYPE_AP)
552 cmd->ac[AC_VO].fifos_mask |= BIT(IWL_MVM_TX_FIFO_MCAST);
554 if (vif->bss_conf.qos)
555 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
557 /* Don't use cts to self as the fw doesn't support it currently. */
558 if (vif->bss_conf.use_cts_prot)
559 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
562 * I think that we should enable these 2 flags regardless the HT PROT
563 * fields in the HT IE, but I am not sure. Someone knows whom to ask?...
565 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT) {
566 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
567 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_HT_PROT |
568 MAC_PROT_FLG_FAT_PROT);
571 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
574 static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
575 struct iwl_mac_ctx_cmd *cmd)
577 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
580 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
581 le32_to_cpu(cmd->action), ret);
586 * Fill the specific data for mac context of type station or p2p client
588 static void iwl_mvm_mac_ctxt_cmd_fill_sta(struct iwl_mvm *mvm,
589 struct ieee80211_vif *vif,
590 struct iwl_mac_data_sta *ctxt_sta,
591 bool force_assoc_off)
593 /* We need the dtim_period to set the MAC as associated */
594 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
599 * The DTIM count counts down, so when it is N that means N
600 * more beacon intervals happen until the DTIM TBTT. Therefore
601 * add this to the current time. If that ends up being in the
602 * future, the firmware will handle it.
604 * Also note that the system_timestamp (which we get here as
605 * "sync_device_ts") and TSF timestamp aren't at exactly the
606 * same offset in the frame -- the TSF is at the first symbol
607 * of the TSF, the system timestamp is at signal acquisition
608 * time. This means there's an offset between them of at most
609 * a few hundred microseconds (24 * 8 bits + PLCP time gives
610 * 384us in the longest case), this is currently not relevant
611 * as the firmware wakes up around 2ms before the TBTT.
613 dtim_offs = vif->bss_conf.sync_dtim_count *
614 vif->bss_conf.beacon_int;
615 /* convert TU to usecs */
619 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
620 ctxt_sta->dtim_time =
621 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
623 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
624 le64_to_cpu(ctxt_sta->dtim_tsf),
625 le32_to_cpu(ctxt_sta->dtim_time),
628 ctxt_sta->is_assoc = cpu_to_le32(1);
630 ctxt_sta->is_assoc = cpu_to_le32(0);
633 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
634 ctxt_sta->bi_reciprocal =
635 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
636 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
637 vif->bss_conf.dtim_period);
638 ctxt_sta->dtim_reciprocal =
639 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
640 vif->bss_conf.dtim_period));
642 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
643 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
646 static int iwl_mvm_mac_ctxt_cmd_station(struct iwl_mvm *mvm,
647 struct ieee80211_vif *vif,
650 struct iwl_mac_ctx_cmd cmd = {};
652 WARN_ON(vif->type != NL80211_IFTYPE_STATION || vif->p2p);
654 /* Fill the common data for all mac context types */
655 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
657 /* Allow beacons to pass through as long as we are not associated,or we
658 * do not have dtim period information */
659 if (!vif->bss_conf.assoc || !vif->bss_conf.dtim_period)
660 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
662 cmd.filter_flags &= ~cpu_to_le32(MAC_FILTER_IN_BEACON);
664 /* Fill the data specific for station mode */
665 iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.sta,
666 action == FW_CTXT_ACTION_ADD);
668 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
671 static int iwl_mvm_mac_ctxt_cmd_p2p_client(struct iwl_mvm *mvm,
672 struct ieee80211_vif *vif,
675 struct iwl_mac_ctx_cmd cmd = {};
676 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
678 WARN_ON(vif->type != NL80211_IFTYPE_STATION || !vif->p2p);
680 /* Fill the common data for all mac context types */
681 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
683 /* Fill the data specific for station mode */
684 iwl_mvm_mac_ctxt_cmd_fill_sta(mvm, vif, &cmd.p2p_sta.sta,
685 action == FW_CTXT_ACTION_ADD);
687 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
688 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
690 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
693 static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
694 struct ieee80211_vif *vif,
697 struct iwl_mac_ctx_cmd cmd = {};
699 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
701 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
703 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
704 MAC_FILTER_IN_CONTROL_AND_MGMT |
705 MAC_FILTER_IN_BEACON |
706 MAC_FILTER_IN_PROBE_REQUEST);
708 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
711 struct iwl_mvm_go_iterator_data {
715 static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
717 struct iwl_mvm_go_iterator_data *data = _data;
718 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
720 if (vif->type == NL80211_IFTYPE_AP && vif->p2p && mvmvif->ap_active)
721 data->go_active = true;
724 static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
725 struct ieee80211_vif *vif,
728 struct iwl_mac_ctx_cmd cmd = {};
729 struct iwl_mvm_go_iterator_data data = {};
731 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
733 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
735 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
737 /* Override the filter flags to accept only probe requests */
738 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
741 * This flag should be set to true when the P2P Device is
742 * discoverable and there is at least another active P2P GO. Settings
743 * this flag will allow the P2P Device to be discoverable on other
744 * channels in addition to its listen channel.
745 * Note that this flag should not be set in other cases as it opens the
746 * Rx filters on all MAC and increases the number of interrupts.
748 ieee80211_iterate_active_interfaces_atomic(
749 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
750 iwl_mvm_go_iterator, &data);
752 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
753 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
756 static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
757 struct iwl_mac_beacon_cmd *beacon_cmd,
758 u8 *beacon, u32 frame_size)
761 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
763 /* The index is relative to frame start but we start looking at the
764 * variable-length part of the beacon. */
765 tim_idx = mgmt->u.beacon.variable - beacon;
767 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
768 while ((tim_idx < (frame_size - 2)) &&
769 (beacon[tim_idx] != WLAN_EID_TIM))
770 tim_idx += beacon[tim_idx+1] + 2;
772 /* If TIM field was found, set variables */
773 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
774 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
775 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
777 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
781 static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
782 struct ieee80211_vif *vif,
783 struct sk_buff *beacon)
785 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
786 struct iwl_host_cmd cmd = {
787 .id = BEACON_TEMPLATE_CMD,
790 struct iwl_mac_beacon_cmd beacon_cmd = {};
791 struct ieee80211_tx_info *info;
795 if (WARN_ON(!beacon))
798 beacon_skb_len = beacon->len;
800 /* TODO: for now the beacon template id is set to be the mac context id.
801 * Might be better to handle it as another resource ... */
802 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
804 /* Set up TX command fields */
805 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
806 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
807 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
808 beacon_cmd.tx.tx_flags = cpu_to_le32(TX_CMD_FLG_SEQ_CTL |
812 mvm->mgmt_last_antenna_idx =
813 iwl_mvm_next_antenna(mvm, iwl_fw_valid_tx_ant(mvm->fw),
814 mvm->mgmt_last_antenna_idx);
816 beacon_cmd.tx.rate_n_flags =
817 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
820 info = IEEE80211_SKB_CB(beacon);
822 if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
823 rate = IWL_FIRST_OFDM_RATE;
825 rate = IWL_FIRST_CCK_RATE;
826 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
828 beacon_cmd.tx.rate_n_flags |=
829 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
831 /* Set up TX beacon command fields */
832 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
837 cmd.len[0] = sizeof(beacon_cmd);
838 cmd.data[0] = &beacon_cmd;
839 cmd.dataflags[0] = 0;
840 cmd.len[1] = beacon_skb_len;
841 cmd.data[1] = beacon->data;
842 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
844 return iwl_mvm_send_cmd(mvm, &cmd);
847 /* The beacon template for the AP/GO context has changed and needs update */
848 int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
849 struct ieee80211_vif *vif)
851 struct sk_buff *beacon;
854 WARN_ON(vif->type != NL80211_IFTYPE_AP);
856 beacon = ieee80211_beacon_get(mvm->hw, vif);
860 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
861 dev_kfree_skb(beacon);
866 * Fill the specific data for mac context of type AP of P2P GO
868 static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
869 struct ieee80211_vif *vif,
870 struct iwl_mac_data_ap *ctxt_ap,
873 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
875 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
876 ctxt_ap->bi_reciprocal =
877 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
878 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
879 vif->bss_conf.dtim_period);
880 ctxt_ap->dtim_reciprocal =
881 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
882 vif->bss_conf.dtim_period));
884 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
887 * Only read the system time when the MAC is being added, when we
888 * just modify the MAC then we should keep the time -- the firmware
889 * can otherwise have a "jumping" TBTT.
892 mvmvif->ap_beacon_time =
893 iwl_read_prph(mvm->trans, DEVICE_SYSTEM_TIME_REG);
895 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
897 ctxt_ap->beacon_tsf = 0; /* unused */
899 /* TODO: Assume that the beacon id == mac context id */
900 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
903 static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
904 struct ieee80211_vif *vif,
907 struct iwl_mac_ctx_cmd cmd = {};
909 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
911 /* Fill the common data for all mac context types */
912 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
914 /* Also enable probe requests to pass */
915 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
917 /* Fill the data specific for ap mode */
918 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
919 action == FW_CTXT_ACTION_ADD);
921 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
924 static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
925 struct ieee80211_vif *vif,
928 struct iwl_mac_ctx_cmd cmd = {};
929 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
931 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
933 /* Fill the common data for all mac context types */
934 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, action);
936 /* Fill the data specific for GO mode */
937 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
938 action == FW_CTXT_ACTION_ADD);
940 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
941 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
942 cmd.go.opp_ps_enabled =
943 cpu_to_le32(!!(noa->oppps_ctwindow &
944 IEEE80211_P2P_OPPPS_ENABLE_BIT));
946 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
949 static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
953 case NL80211_IFTYPE_STATION:
955 return iwl_mvm_mac_ctxt_cmd_station(mvm, vif,
958 return iwl_mvm_mac_ctxt_cmd_p2p_client(mvm, vif,
961 case NL80211_IFTYPE_AP:
963 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
965 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
967 case NL80211_IFTYPE_MONITOR:
968 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
969 case NL80211_IFTYPE_P2P_DEVICE:
970 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
978 int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
980 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
983 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
984 vif->addr, ieee80211_vif_type_p2p(vif)))
987 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD);
991 mvmvif->uploaded = true;
995 int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
997 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
999 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1000 vif->addr, ieee80211_vif_type_p2p(vif)))
1003 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY);
1006 int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1008 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1009 struct iwl_mac_ctx_cmd cmd;
1012 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1013 vif->addr, ieee80211_vif_type_p2p(vif)))
1016 memset(&cmd, 0, sizeof(cmd));
1018 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1020 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1022 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, CMD_SYNC,
1025 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1029 mvmvif->uploaded = false;
1033 int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1034 struct iwl_rx_cmd_buffer *rxb,
1035 struct iwl_device_cmd *cmd)
1037 struct iwl_rx_packet *pkt = rxb_addr(rxb);
1038 struct iwl_beacon_notif *beacon = (void *)pkt->data;
1039 u16 status __maybe_unused =
1040 le16_to_cpu(beacon->beacon_notify_hdr.status.status);
1041 u32 rate __maybe_unused =
1042 le32_to_cpu(beacon->beacon_notify_hdr.initial_rate);
1044 IWL_DEBUG_RX(mvm, "beacon status %#x retries:%d tsf:0x%16llX rate:%d\n",
1045 status & TX_STATUS_MSK,
1046 beacon->beacon_notify_hdr.failure_frame,
1047 le64_to_cpu(beacon->tsf),