blob: b8ab4a108720240786d320c477e919081d6bf123 [file] [log] [blame]
Johannes Berg8ca151b2013-01-24 14:25:36 +01001/******************************************************************************
2 *
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.
5 *
6 * GPL LICENSE SUMMARY
7 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +02008 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +02009 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
23 * USA
24 *
25 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020026 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010027 *
28 * Contact Information:
29 * Intel Linux Wireless <ilw@linux.intel.com>
30 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
31 *
32 * BSD LICENSE
33 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020034 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020035 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010036 * All rights reserved.
37 *
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions
40 * are met:
41 *
42 * * Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * * Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in
46 * the documentation and/or other materials provided with the
47 * distribution.
48 * * Neither the name Intel Corporation nor the names of its
49 * contributors may be used to endorse or promote products derived
50 * from this software without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
53 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
54 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
55 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
56 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
57 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
58 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
59 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
60 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
61 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
62 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
63 *
64 *****************************************************************************/
65
66#include <linux/etherdevice.h>
67#include <net/mac80211.h>
68#include "iwl-io.h"
69#include "iwl-prph.h"
70#include "fw-api.h"
71#include "mvm.h"
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +030072#include "time-event.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010073
74const u8 iwl_mvm_ac_to_tx_fifo[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +010075 IWL_MVM_TX_FIFO_VO,
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +020076 IWL_MVM_TX_FIFO_VI,
77 IWL_MVM_TX_FIFO_BE,
78 IWL_MVM_TX_FIFO_BK,
Johannes Berg8ca151b2013-01-24 14:25:36 +010079};
80
81struct iwl_mvm_mac_iface_iterator_data {
82 struct iwl_mvm *mvm;
83 struct ieee80211_vif *vif;
84 unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
85 unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
Johannes Berg110cf812014-08-01 23:14:24 +020086 u32 used_hw_queues;
Johannes Berg8ca151b2013-01-24 14:25:36 +010087 enum iwl_tsf_id preferred_tsf;
88 bool found_vif;
89};
90
Ilan Peer6e97b0d2013-12-23 22:18:02 +020091static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
92 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +010093{
94 struct iwl_mvm_mac_iface_iterator_data *data = _data;
95 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer2c3e62a2014-02-02 21:54:35 +020096 u16 min_bi;
Johannes Berg8ca151b2013-01-24 14:25:36 +010097
Ilan Peer6e97b0d2013-12-23 22:18:02 +020098 /* Skip the interface for which we are trying to assign a tsf_id */
99 if (vif == data->vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100100 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100101
102 /*
103 * The TSF is a hardware/firmware resource, there are 4 and
104 * the driver should assign and free them as needed. However,
105 * there are cases where 2 MACs should share the same TSF ID
106 * for the purpose of clock sync, an optimization to avoid
107 * clock drift causing overlapping TBTTs/DTIMs for a GO and
108 * client in the system.
109 *
110 * The firmware will decide according to the MAC type which
111 * will be the master and slave. Clients that need to sync
112 * with a remote station will be the master, and an AP or GO
113 * will be the slave.
114 *
115 * Depending on the new interface type it can be slaved to
116 * or become the master of an existing interface.
117 */
118 switch (data->vif->type) {
119 case NL80211_IFTYPE_STATION:
120 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200121 * The new interface is a client, so if the one we're iterating
122 * is an AP, and the beacon interval of the AP is a multiple or
123 * divisor of the beacon interval of the client, the same TSF
124 * should be used to avoid drift between the new client and
125 * existing AP. The existing AP will get drift updates from the
126 * new client context in this case.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100127 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200128 if (vif->type != NL80211_IFTYPE_AP ||
129 data->preferred_tsf != NUM_TSF_IDS ||
130 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
131 break;
132
133 min_bi = min(data->vif->bss_conf.beacon_int,
134 vif->bss_conf.beacon_int);
135
136 if (!min_bi)
137 break;
138
139 if ((data->vif->bss_conf.beacon_int -
140 vif->bss_conf.beacon_int) % min_bi == 0) {
141 data->preferred_tsf = mvmvif->tsf_id;
142 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100143 }
144 break;
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200145
Johannes Berg8ca151b2013-01-24 14:25:36 +0100146 case NL80211_IFTYPE_AP:
147 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200148 * The new interface is AP/GO, so if its beacon interval is a
149 * multiple or a divisor of the beacon interval of an existing
150 * interface, it should get drift updates from an existing
151 * client or use the same TSF as an existing GO. There's no
152 * drift between TSFs internally but if they used different
153 * TSFs then a new client MAC could update one of them and
154 * cause drift that way.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100155 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200156 if ((vif->type != NL80211_IFTYPE_AP &&
157 vif->type != NL80211_IFTYPE_STATION) ||
158 data->preferred_tsf != NUM_TSF_IDS ||
159 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
160 break;
161
162 min_bi = min(data->vif->bss_conf.beacon_int,
163 vif->bss_conf.beacon_int);
164
165 if (!min_bi)
166 break;
167
168 if ((data->vif->bss_conf.beacon_int -
169 vif->bss_conf.beacon_int) % min_bi == 0) {
170 data->preferred_tsf = mvmvif->tsf_id;
171 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100172 }
173 break;
174 default:
175 /*
176 * For all other interface types there's no need to
177 * take drift into account. Either they're exclusive
178 * like IBSS and monitor, or we don't care much about
179 * their TSF (like P2P Device), but we won't be able
180 * to share the TSF resource.
181 */
182 break;
183 }
184
185 /*
186 * Unless we exited above, we can't share the TSF resource
187 * that the virtual interface we're iterating over is using
188 * with the new one, so clear the available bit and if this
189 * was the preferred one, reset that as well.
190 */
191 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
192
193 if (data->preferred_tsf == mvmvif->tsf_id)
194 data->preferred_tsf = NUM_TSF_IDS;
195}
196
Johannes Berg110cf812014-08-01 23:14:24 +0200197/*
198 * Get the mask of the queues used by the vif
199 */
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300200u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
Johannes Berg110cf812014-08-01 23:14:24 +0200201{
202 u32 qmask = 0, ac;
203
204 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
205 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
206
207 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
208 qmask |= BIT(vif->hw_queue[ac]);
209
210 if (vif->type == NL80211_IFTYPE_AP)
211 qmask |= BIT(vif->cab_queue);
212
213 return qmask;
214}
215
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200216static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
217 struct ieee80211_vif *vif)
218{
219 struct iwl_mvm_mac_iface_iterator_data *data = _data;
220 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200221
222 /* Iterator may already find the interface being added -- skip it */
223 if (vif == data->vif) {
224 data->found_vif = true;
225 return;
226 }
227
228 /* Mark the queues used by the vif */
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300229 data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200230
231 /* Mark MAC IDs as used by clearing the available bit, and
232 * (below) mark TSFs as used if their existing use is not
233 * compatible with the new interface type.
234 * No locking or atomic bit operations are needed since the
235 * data is on the stack of the caller function.
236 */
237 __clear_bit(mvmvif->id, data->available_mac_ids);
238
239 /* find a suitable tsf_id */
240 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
241}
242
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200243void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
244 struct ieee80211_vif *vif)
245{
246 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
247 struct iwl_mvm_mac_iface_iterator_data data = {
248 .mvm = mvm,
249 .vif = vif,
250 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
251 /* no preference yet */
252 .preferred_tsf = NUM_TSF_IDS,
253 };
254
255 ieee80211_iterate_active_interfaces_atomic(
256 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
257 iwl_mvm_mac_tsf_id_iter, &data);
258
259 if (data.preferred_tsf != NUM_TSF_IDS)
260 mvmvif->tsf_id = data.preferred_tsf;
261 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
262 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
263 NUM_TSF_IDS);
264}
265
Johannes Berg8ca151b2013-01-24 14:25:36 +0100266static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
267 struct ieee80211_vif *vif)
268{
269 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
270 struct iwl_mvm_mac_iface_iterator_data data = {
271 .mvm = mvm,
272 .vif = vif,
273 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
274 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
275 /* no preference yet */
276 .preferred_tsf = NUM_TSF_IDS,
Johannes Berg110cf812014-08-01 23:14:24 +0200277 .used_hw_queues =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100278 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200279 BIT(mvm->aux_queue) |
Johannes Berg110cf812014-08-01 23:14:24 +0200280 BIT(IWL_MVM_CMD_QUEUE),
Johannes Berg8ca151b2013-01-24 14:25:36 +0100281 .found_vif = false,
282 };
283 u32 ac;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300284 int ret, i;
Johannes Berg110cf812014-08-01 23:14:24 +0200285 unsigned long used_hw_queues;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100286
287 /*
288 * Allocate a MAC ID and a TSF for this MAC, along with the queues
289 * and other resources.
290 */
291
292 /*
293 * Before the iterator, we start with all MAC IDs and TSFs available.
294 *
295 * During iteration, all MAC IDs are cleared that are in use by other
296 * virtual interfaces, and all TSF IDs are cleared that can't be used
297 * by this new virtual interface because they're used by an interface
298 * that can't share it with the new one.
299 * At the same time, we check if there's a preferred TSF in the case
300 * that we should share it with another interface.
301 */
302
Johannes Berg5023d962013-07-31 14:07:43 +0200303 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
304 switch (vif->type) {
305 case NL80211_IFTYPE_ADHOC:
306 break;
307 case NL80211_IFTYPE_STATION:
308 if (!vif->p2p)
309 break;
310 /* fall through */
311 default:
Ilan Peerec8b6882013-02-13 13:27:18 +0200312 __clear_bit(0, data.available_mac_ids);
Johannes Berg5023d962013-07-31 14:07:43 +0200313 }
Ilan Peerec8b6882013-02-13 13:27:18 +0200314
Johannes Berg8ca151b2013-01-24 14:25:36 +0100315 ieee80211_iterate_active_interfaces_atomic(
316 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
317 iwl_mvm_mac_iface_iterator, &data);
318
319 /*
320 * In the case we're getting here during resume, it's similar to
321 * firmware restart, and with RESUME_ALL the iterator will find
322 * the vif being added already.
323 * We don't want to reassign any IDs in either case since doing
324 * so would probably assign different IDs (as interfaces aren't
325 * necessarily added in the same order), but the old IDs were
326 * preserved anyway, so skip ID assignment for both resume and
327 * recovery.
328 */
329 if (data.found_vif)
330 return 0;
331
332 /* Therefore, in recovery, we can't get here */
Ilan Peerfd11bd02013-06-16 12:18:11 +0300333 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
334 return -EBUSY;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100335
336 mvmvif->id = find_first_bit(data.available_mac_ids,
337 NUM_MAC_INDEX_DRIVER);
338 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
339 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
340 ret = -EIO;
341 goto exit_fail;
342 }
343
344 if (data.preferred_tsf != NUM_TSF_IDS)
345 mvmvif->tsf_id = data.preferred_tsf;
346 else
347 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
348 NUM_TSF_IDS);
349 if (mvmvif->tsf_id == NUM_TSF_IDS) {
350 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
351 ret = -EIO;
352 goto exit_fail;
353 }
354
355 mvmvif->color = 0;
356
Ilan Peer1e849c92013-02-13 12:26:39 +0200357 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
358 mvmvif->time_event_data.id = TE_MAX;
359
Johannes Berg8ca151b2013-01-24 14:25:36 +0100360 /* No need to allocate data queues to P2P Device MAC.*/
361 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
362 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
363 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
364
365 return 0;
366 }
367
Johannes Berg110cf812014-08-01 23:14:24 +0200368 used_hw_queues = data.used_hw_queues;
369
Johannes Berg8ca151b2013-01-24 14:25:36 +0100370 /* Find available queues, and allocate them to the ACs */
371 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg110cf812014-08-01 23:14:24 +0200372 u8 queue = find_first_zero_bit(&used_hw_queues,
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200373 mvm->first_agg_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100374
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200375 if (queue >= mvm->first_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100376 IWL_ERR(mvm, "Failed to allocate queue\n");
377 ret = -EIO;
378 goto exit_fail;
379 }
380
Johannes Berg110cf812014-08-01 23:14:24 +0200381 __set_bit(queue, &used_hw_queues);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100382 vif->hw_queue[ac] = queue;
383 }
384
385 /* Allocate the CAB queue for softAP and GO interfaces */
386 if (vif->type == NL80211_IFTYPE_AP) {
Johannes Berg110cf812014-08-01 23:14:24 +0200387 u8 queue = find_first_zero_bit(&used_hw_queues,
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200388 mvm->first_agg_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100389
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200390 if (queue >= mvm->first_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100391 IWL_ERR(mvm, "Failed to allocate cab queue\n");
392 ret = -EIO;
393 goto exit_fail;
394 }
395
396 vif->cab_queue = queue;
397 } else {
398 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
399 }
400
401 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
402 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
403
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300404 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
405 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
406
Johannes Berg8ca151b2013-01-24 14:25:36 +0100407 return 0;
408
409exit_fail:
410 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
411 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
412 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
413 return ret;
414}
415
416int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
417{
418 u32 ac;
419 int ret;
420
421 lockdep_assert_held(&mvm->mutex);
422
423 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
424 if (ret)
425 return ret;
426
427 switch (vif->type) {
428 case NL80211_IFTYPE_P2P_DEVICE:
Avri Altman3edf8ff2014-07-30 11:41:01 +0300429 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
430 IWL_MVM_TX_FIFO_VO);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100431 break;
432 case NL80211_IFTYPE_AP:
Avri Altman3edf8ff2014-07-30 11:41:01 +0300433 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue,
434 IWL_MVM_TX_FIFO_MCAST);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100435 /* fall through */
436 default:
437 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Avri Altman3edf8ff2014-07-30 11:41:01 +0300438 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
439 iwl_mvm_ac_to_tx_fifo[ac]);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100440 break;
441 }
442
443 return 0;
444}
445
446void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
447{
448 int ac;
449
450 lockdep_assert_held(&mvm->mutex);
451
452 switch (vif->type) {
453 case NL80211_IFTYPE_P2P_DEVICE:
Avri Altman3edf8ff2014-07-30 11:41:01 +0300454 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100455 break;
456 case NL80211_IFTYPE_AP:
Avri Altman3edf8ff2014-07-30 11:41:01 +0300457 iwl_mvm_disable_txq(mvm, vif->cab_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100458 /* fall through */
459 default:
460 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Avri Altman3edf8ff2014-07-30 11:41:01 +0300461 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac]);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100462 }
463}
464
465static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
466 struct ieee80211_vif *vif,
467 enum ieee80211_band band,
468 u8 *cck_rates, u8 *ofdm_rates)
469{
470 struct ieee80211_supported_band *sband;
471 unsigned long basic = vif->bss_conf.basic_rates;
472 int lowest_present_ofdm = 100;
473 int lowest_present_cck = 100;
474 u8 cck = 0;
475 u8 ofdm = 0;
476 int i;
477
478 sband = mvm->hw->wiphy->bands[band];
479
480 for_each_set_bit(i, &basic, BITS_PER_LONG) {
481 int hw = sband->bitrates[i].hw_value;
482 if (hw >= IWL_FIRST_OFDM_RATE) {
483 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
484 if (lowest_present_ofdm > hw)
485 lowest_present_ofdm = hw;
486 } else {
487 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
488
489 cck |= BIT(hw);
490 if (lowest_present_cck > hw)
491 lowest_present_cck = hw;
492 }
493 }
494
495 /*
496 * Now we've got the basic rates as bitmaps in the ofdm and cck
497 * variables. This isn't sufficient though, as there might not
498 * be all the right rates in the bitmap. E.g. if the only basic
499 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
500 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
501 *
502 * [...] a STA responding to a received frame shall transmit
503 * its Control Response frame [...] at the highest rate in the
504 * BSSBasicRateSet parameter that is less than or equal to the
505 * rate of the immediately previous frame in the frame exchange
506 * sequence ([...]) and that is of the same modulation class
507 * ([...]) as the received frame. If no rate contained in the
508 * BSSBasicRateSet parameter meets these conditions, then the
509 * control frame sent in response to a received frame shall be
510 * transmitted at the highest mandatory rate of the PHY that is
511 * less than or equal to the rate of the received frame, and
512 * that is of the same modulation class as the received frame.
513 *
514 * As a consequence, we need to add all mandatory rates that are
515 * lower than all of the basic rates to these bitmaps.
516 */
517
518 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
519 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
520 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
521 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
522 /* 6M already there or needed so always add */
523 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
524
525 /*
526 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
527 * Note, however:
528 * - if no CCK rates are basic, it must be ERP since there must
529 * be some basic rates at all, so they're OFDM => ERP PHY
530 * (or we're in 5 GHz, and the cck bitmap will never be used)
531 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
532 * - if 5.5M is basic, 1M and 2M are mandatory
533 * - if 2M is basic, 1M is mandatory
534 * - if 1M is basic, that's the only valid ACK rate.
535 * As a consequence, it's not as complicated as it sounds, just add
536 * any lower rates to the ACK rate bitmap.
537 */
538 if (IWL_RATE_11M_INDEX < lowest_present_cck)
539 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
540 if (IWL_RATE_5M_INDEX < lowest_present_cck)
541 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
542 if (IWL_RATE_2M_INDEX < lowest_present_cck)
543 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
544 /* 1M already there or needed so always add */
545 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
546
547 *cck_rates = cck;
548 *ofdm_rates = ofdm;
549}
550
Avri Altman8a5e3662013-11-12 19:16:03 +0200551static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
552 struct ieee80211_vif *vif,
553 struct iwl_mac_ctx_cmd *cmd)
554{
555 /* for both sta and ap, ht_operation_mode hold the protection_mode */
556 u8 protection_mode = vif->bss_conf.ht_operation_mode &
557 IEEE80211_HT_OP_MODE_PROTECTION;
558 /* The fw does not distinguish between ht and fat */
559 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
560
561 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
562 /*
563 * See section 9.23.3.1 of IEEE 80211-2012.
564 * Nongreenfield HT STAs Present is not supported.
565 */
566 switch (protection_mode) {
567 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
568 break;
569 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
570 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
571 cmd->protection_flags |= cpu_to_le32(ht_flag);
572 break;
573 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
574 /* Protect when channel wider than 20MHz */
575 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
576 cmd->protection_flags |= cpu_to_le32(ht_flag);
577 break;
578 default:
579 IWL_ERR(mvm, "Illegal protection mode %d\n",
580 protection_mode);
581 break;
582 }
583}
584
Johannes Berg8ca151b2013-01-24 14:25:36 +0100585static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
586 struct ieee80211_vif *vif,
587 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200588 const u8 *bssid_override,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100589 u32 action)
590{
591 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
592 struct ieee80211_chanctx_conf *chanctx;
Avri Altman8a5e3662013-11-12 19:16:03 +0200593 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
594 IEEE80211_HT_OP_MODE_PROTECTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100595 u8 cck_ack_rates, ofdm_ack_rates;
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200596 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100597 int i;
598
599 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
600 mvmvif->color));
601 cmd->action = cpu_to_le32(action);
602
603 switch (vif->type) {
604 case NL80211_IFTYPE_STATION:
605 if (vif->p2p)
606 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
607 else
608 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
609 break;
610 case NL80211_IFTYPE_AP:
611 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
612 break;
613 case NL80211_IFTYPE_MONITOR:
614 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
615 break;
616 case NL80211_IFTYPE_P2P_DEVICE:
617 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
618 break;
619 case NL80211_IFTYPE_ADHOC:
620 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
621 break;
622 default:
623 WARN_ON_ONCE(1);
624 }
625
626 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
627
628 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200629
630 if (bssid)
631 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100632 else
633 eth_broadcast_addr(cmd->bssid_addr);
634
635 rcu_read_lock();
636 chanctx = rcu_dereference(vif->chanctx_conf);
637 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
638 : IEEE80211_BAND_2GHZ,
639 &cck_ack_rates, &ofdm_ack_rates);
640 rcu_read_unlock();
641
642 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
643 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
644
645 cmd->cck_short_preamble =
646 cpu_to_le32(vif->bss_conf.use_short_preamble ?
647 MAC_FLG_SHORT_PREAMBLE : 0);
648 cmd->short_slot =
649 cpu_to_le32(vif->bss_conf.use_short_slot ?
650 MAC_FLG_SHORT_SLOT : 0);
651
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200652 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
653 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
654
655 cmd->ac[txf].cw_min =
656 cpu_to_le16(mvmvif->queue_params[i].cw_min);
657 cmd->ac[txf].cw_max =
658 cpu_to_le16(mvmvif->queue_params[i].cw_max);
659 cmd->ac[txf].edca_txop =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100660 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200661 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
662 cmd->ac[txf].fifos_mask = BIT(txf);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100663 }
664
Emmanuel Grumbach86a91ec2013-05-26 20:47:53 +0300665 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
666 if (vif->type == NL80211_IFTYPE_AP)
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200667 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
668 BIT(IWL_MVM_TX_FIFO_MCAST);
Emmanuel Grumbach86a91ec2013-05-26 20:47:53 +0300669
Johannes Berg8ca151b2013-01-24 14:25:36 +0100670 if (vif->bss_conf.qos)
671 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
672
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300673 if (vif->bss_conf.use_cts_prot)
Dor Shaishcc7ee2b2013-02-18 13:51:36 +0200674 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300675
Avri Altman8a5e3662013-11-12 19:16:03 +0200676 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
677 vif->bss_conf.use_cts_prot,
678 vif->bss_conf.ht_operation_mode);
679 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100680 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
Avri Altman8a5e3662013-11-12 19:16:03 +0200681 if (ht_enabled)
682 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100683
684 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
685}
686
687static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
688 struct iwl_mac_ctx_cmd *cmd)
689{
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300690 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100691 sizeof(*cmd), cmd);
692 if (ret)
693 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
694 le32_to_cpu(cmd->action), ret);
695 return ret;
696}
697
Luciano Coelhocf520232014-05-13 16:52:54 +0300698static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
699 struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200700 u32 action, bool force_assoc_off,
701 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100702{
Luciano Coelhocf520232014-05-13 16:52:54 +0300703 struct iwl_mac_ctx_cmd cmd = {};
704 struct iwl_mac_data_sta *ctxt_sta;
705
706 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
707
708 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200709 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
Luciano Coelhocf520232014-05-13 16:52:54 +0300710
711 if (vif->p2p) {
712 struct ieee80211_p2p_noa_attr *noa =
713 &vif->bss_conf.p2p_noa_attr;
714
715 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
716 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
717 ctxt_sta = &cmd.p2p_sta.sta;
718 } else {
Luciano Coelhocf520232014-05-13 16:52:54 +0300719 ctxt_sta = &cmd.sta;
720 }
721
Johannes Berg210a5442013-01-24 23:48:23 +0100722 /* We need the dtim_period to set the MAC as associated */
Alexander Bondarba283922013-05-02 16:34:48 +0300723 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
724 !force_assoc_off) {
Johannes Bergd2931bb2013-02-05 18:10:04 +0100725 u32 dtim_offs;
726
727 /*
728 * The DTIM count counts down, so when it is N that means N
729 * more beacon intervals happen until the DTIM TBTT. Therefore
730 * add this to the current time. If that ends up being in the
731 * future, the firmware will handle it.
732 *
733 * Also note that the system_timestamp (which we get here as
734 * "sync_device_ts") and TSF timestamp aren't at exactly the
735 * same offset in the frame -- the TSF is at the first symbol
736 * of the TSF, the system timestamp is at signal acquisition
737 * time. This means there's an offset between them of at most
738 * a few hundred microseconds (24 * 8 bits + PLCP time gives
739 * 384us in the longest case), this is currently not relevant
740 * as the firmware wakes up around 2ms before the TBTT.
741 */
742 dtim_offs = vif->bss_conf.sync_dtim_count *
743 vif->bss_conf.beacon_int;
744 /* convert TU to usecs */
745 dtim_offs *= 1024;
746
747 ctxt_sta->dtim_tsf =
748 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
749 ctxt_sta->dtim_time =
750 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
751
752 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
753 le64_to_cpu(ctxt_sta->dtim_tsf),
754 le32_to_cpu(ctxt_sta->dtim_time),
755 dtim_offs);
756
Johannes Berg210a5442013-01-24 23:48:23 +0100757 ctxt_sta->is_assoc = cpu_to_le32(1);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100758 } else {
Johannes Berg210a5442013-01-24 23:48:23 +0100759 ctxt_sta->is_assoc = cpu_to_le32(0);
Luciano Coelho7c8b3bc2014-07-30 15:04:08 +0300760
761 /* Allow beacons to pass through as long as we are not
762 * associated, or we do not have dtim period information.
763 */
764 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100765 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100766
767 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
768 ctxt_sta->bi_reciprocal =
769 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
770 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
771 vif->bss_conf.dtim_period);
772 ctxt_sta->dtim_reciprocal =
773 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
774 vif->bss_conf.dtim_period));
775
776 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
777 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100778
779 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
780}
781
782static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
783 struct ieee80211_vif *vif,
784 u32 action)
785{
786 struct iwl_mac_ctx_cmd cmd = {};
787
788 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
789
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200790 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg53585492013-03-18 13:04:50 +0100791
792 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
793 MAC_FILTER_IN_CONTROL_AND_MGMT |
794 MAC_FILTER_IN_BEACON |
Johannes Bergfb8b8ee2013-10-21 12:37:53 +0200795 MAC_FILTER_IN_PROBE_REQUEST |
796 MAC_FILTER_IN_CRC32);
797 mvm->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
Johannes Berg53585492013-03-18 13:04:50 +0100798
Johannes Berg8ca151b2013-01-24 14:25:36 +0100799 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
800}
801
Johannes Berg5023d962013-07-31 14:07:43 +0200802static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
803 struct ieee80211_vif *vif,
804 u32 action)
805{
806 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
807 struct iwl_mac_ctx_cmd cmd = {};
808
809 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
810
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200811 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg5023d962013-07-31 14:07:43 +0200812
813 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
814 MAC_FILTER_IN_PROBE_REQUEST);
815
816 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
817 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
818 cmd.ibss.bi_reciprocal =
819 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
820
821 /* TODO: Assumes that the beacon id == mac context id */
822 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
823
824 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
825}
826
Johannes Berg8ca151b2013-01-24 14:25:36 +0100827struct iwl_mvm_go_iterator_data {
828 bool go_active;
829};
830
831static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
832{
833 struct iwl_mvm_go_iterator_data *data = _data;
834 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
835
Johannes Berg5023d962013-07-31 14:07:43 +0200836 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
837 mvmvif->ap_ibss_active)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100838 data->go_active = true;
839}
840
841static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
842 struct ieee80211_vif *vif,
843 u32 action)
844{
845 struct iwl_mac_ctx_cmd cmd = {};
846 struct iwl_mvm_go_iterator_data data = {};
847
848 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
849
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200850 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100851
852 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Ilan Peer1dcd15e2013-02-14 14:25:24 +0200853
854 /* Override the filter flags to accept only probe requests */
855 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100856
857 /*
858 * This flag should be set to true when the P2P Device is
859 * discoverable and there is at least another active P2P GO. Settings
860 * this flag will allow the P2P Device to be discoverable on other
861 * channels in addition to its listen channel.
862 * Note that this flag should not be set in other cases as it opens the
863 * Rx filters on all MAC and increases the number of interrupts.
864 */
865 ieee80211_iterate_active_interfaces_atomic(
866 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
867 iwl_mvm_go_iterator, &data);
868
869 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
870 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
871}
872
873static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
874 struct iwl_mac_beacon_cmd *beacon_cmd,
875 u8 *beacon, u32 frame_size)
876{
877 u32 tim_idx;
878 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
879
880 /* The index is relative to frame start but we start looking at the
881 * variable-length part of the beacon. */
882 tim_idx = mgmt->u.beacon.variable - beacon;
883
884 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
885 while ((tim_idx < (frame_size - 2)) &&
886 (beacon[tim_idx] != WLAN_EID_TIM))
887 tim_idx += beacon[tim_idx+1] + 2;
888
889 /* If TIM field was found, set variables */
890 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
891 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
892 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
893 } else {
894 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
895 }
896}
897
898static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
899 struct ieee80211_vif *vif,
900 struct sk_buff *beacon)
901{
902 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
903 struct iwl_host_cmd cmd = {
904 .id = BEACON_TEMPLATE_CMD,
905 .flags = CMD_ASYNC,
906 };
907 struct iwl_mac_beacon_cmd beacon_cmd = {};
908 struct ieee80211_tx_info *info;
909 u32 beacon_skb_len;
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300910 u32 rate, tx_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100911
912 if (WARN_ON(!beacon))
913 return -EINVAL;
914
915 beacon_skb_len = beacon->len;
916
917 /* TODO: for now the beacon template id is set to be the mac context id.
918 * Might be better to handle it as another resource ... */
919 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300920 info = IEEE80211_SKB_CB(beacon);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100921
922 /* Set up TX command fields */
923 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
924 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
925 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300926 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
927 tx_flags |=
928 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
929 TX_CMD_FLG_BT_PRIO_POS;
930 beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100931
932 mvm->mgmt_last_antenna_idx =
Johannes Berg4ed735e2014-02-12 21:47:44 +0100933 iwl_mvm_next_antenna(mvm, mvm->fw->valid_tx_ant,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100934 mvm->mgmt_last_antenna_idx);
935
936 beacon_cmd.tx.rate_n_flags =
937 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
938 RATE_MCS_ANT_POS);
939
Johannes Berg8ca151b2013-01-24 14:25:36 +0100940 if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
941 rate = IWL_FIRST_OFDM_RATE;
942 } else {
943 rate = IWL_FIRST_CCK_RATE;
944 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
945 }
946 beacon_cmd.tx.rate_n_flags |=
947 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
948
949 /* Set up TX beacon command fields */
Johannes Berg5023d962013-07-31 14:07:43 +0200950 if (vif->type == NL80211_IFTYPE_AP)
951 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
952 beacon->data,
953 beacon_skb_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100954
955 /* Submit command */
956 cmd.len[0] = sizeof(beacon_cmd);
957 cmd.data[0] = &beacon_cmd;
958 cmd.dataflags[0] = 0;
959 cmd.len[1] = beacon_skb_len;
960 cmd.data[1] = beacon->data;
961 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
962
963 return iwl_mvm_send_cmd(mvm, &cmd);
964}
965
Johannes Berg5023d962013-07-31 14:07:43 +0200966/* The beacon template for the AP/GO/IBSS has changed and needs update */
Johannes Berg8ca151b2013-01-24 14:25:36 +0100967int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
968 struct ieee80211_vif *vif)
969{
970 struct sk_buff *beacon;
971 int ret;
972
Johannes Berg5023d962013-07-31 14:07:43 +0200973 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
974 vif->type != NL80211_IFTYPE_ADHOC);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100975
Andrei Otcheretianskife887662014-04-10 21:46:16 +0300976 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100977 if (!beacon)
978 return -ENOMEM;
979
980 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
981 dev_kfree_skb(beacon);
982 return ret;
983}
984
Ilan Peer3a3cb922013-06-13 08:30:12 +0300985struct iwl_mvm_mac_ap_iterator_data {
986 struct iwl_mvm *mvm;
987 struct ieee80211_vif *vif;
988 u32 beacon_device_ts;
989 u16 beacon_int;
990};
991
992/* Find the beacon_device_ts and beacon_int for a managed interface */
993static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
994 struct ieee80211_vif *vif)
995{
996 struct iwl_mvm_mac_ap_iterator_data *data = _data;
997
998 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
999 return;
1000
1001 /* Station client has higher priority over P2P client*/
1002 if (vif->p2p && data->beacon_device_ts)
1003 return;
1004
1005 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1006 data->beacon_int = vif->bss_conf.beacon_int;
1007}
1008
Johannes Berg8ca151b2013-01-24 14:25:36 +01001009/*
1010 * Fill the specific data for mac context of type AP of P2P GO
1011 */
1012static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1013 struct ieee80211_vif *vif,
Johannes Berg506a81e2013-02-28 14:05:14 +01001014 struct iwl_mac_data_ap *ctxt_ap,
1015 bool add)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001016{
1017 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer3a3cb922013-06-13 08:30:12 +03001018 struct iwl_mvm_mac_ap_iterator_data data = {
1019 .mvm = mvm,
1020 .vif = vif,
1021 .beacon_device_ts = 0
1022 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001023
1024 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1025 ctxt_ap->bi_reciprocal =
1026 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1027 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1028 vif->bss_conf.dtim_period);
1029 ctxt_ap->dtim_reciprocal =
1030 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1031 vif->bss_conf.dtim_period));
1032
1033 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001034
Johannes Berg506a81e2013-02-28 14:05:14 +01001035 /*
Ilan Peer3a3cb922013-06-13 08:30:12 +03001036 * Only set the beacon time when the MAC is being added, when we
Johannes Berg506a81e2013-02-28 14:05:14 +01001037 * just modify the MAC then we should keep the time -- the firmware
1038 * can otherwise have a "jumping" TBTT.
1039 */
Ilan Peer3a3cb922013-06-13 08:30:12 +03001040 if (add) {
1041 /*
1042 * If there is a station/P2P client interface which is
1043 * associated, set the AP's TBTT far enough from the station's
1044 * TBTT. Otherwise, set it to the current system time
1045 */
1046 ieee80211_iterate_active_interfaces_atomic(
1047 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1048 iwl_mvm_mac_ap_iterator, &data);
1049
1050 if (data.beacon_device_ts) {
Ilan Peer9f8f8ca2013-12-24 17:17:05 +02001051 u32 rand = (prandom_u32() % (64 - 36)) + 36;
Ilan Peer3a3cb922013-06-13 08:30:12 +03001052 mvmvif->ap_beacon_time = data.beacon_device_ts +
1053 ieee80211_tu_to_usec(data.beacon_int * rand /
1054 100);
1055 } else {
1056 mvmvif->ap_beacon_time =
1057 iwl_read_prph(mvm->trans,
1058 DEVICE_SYSTEM_TIME_REG);
1059 }
1060 }
Johannes Berg506a81e2013-02-28 14:05:14 +01001061
1062 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
Johannes Berg506a81e2013-02-28 14:05:14 +01001063 ctxt_ap->beacon_tsf = 0; /* unused */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001064
1065 /* TODO: Assume that the beacon id == mac context id */
1066 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1067}
1068
1069static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1070 struct ieee80211_vif *vif,
1071 u32 action)
1072{
1073 struct iwl_mac_ctx_cmd cmd = {};
1074
1075 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1076
1077 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001078 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001079
Eliad Peller46018792014-07-16 13:24:36 +03001080 /*
1081 * pass probe requests and beacons from other APs (needed
1082 * for ht protection)
1083 */
1084 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1085 MAC_FILTER_IN_BEACON);
Ilan Peer1dcd15e2013-02-14 14:25:24 +02001086
Johannes Berg8ca151b2013-01-24 14:25:36 +01001087 /* Fill the data specific for ap mode */
Johannes Berg506a81e2013-02-28 14:05:14 +01001088 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1089 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001090
1091 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1092}
1093
1094static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1095 struct ieee80211_vif *vif,
1096 u32 action)
1097{
1098 struct iwl_mac_ctx_cmd cmd = {};
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001099 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001100
1101 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1102
1103 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001104 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001105
Eliad Peller46018792014-07-16 13:24:36 +03001106 /*
1107 * pass probe requests and beacons from other APs (needed
1108 * for ht protection)
1109 */
1110 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1111 MAC_FILTER_IN_BEACON);
1112
Johannes Berg8ca151b2013-01-24 14:25:36 +01001113 /* Fill the data specific for GO mode */
Johannes Berg506a81e2013-02-28 14:05:14 +01001114 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1115 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001116
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001117 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1118 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1119 cmd.go.opp_ps_enabled =
1120 cpu_to_le32(!!(noa->oppps_ctwindow &
1121 IEEE80211_P2P_OPPPS_ENABLE_BIT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001122
1123 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1124}
1125
1126static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001127 u32 action, bool force_assoc_off,
1128 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001129{
1130 switch (vif->type) {
1131 case NL80211_IFTYPE_STATION:
Luciano Coelhocf520232014-05-13 16:52:54 +03001132 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001133 force_assoc_off,
1134 bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001135 break;
1136 case NL80211_IFTYPE_AP:
1137 if (!vif->p2p)
1138 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1139 else
1140 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1141 break;
1142 case NL80211_IFTYPE_MONITOR:
1143 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1144 case NL80211_IFTYPE_P2P_DEVICE:
1145 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
Johannes Berg5023d962013-07-31 14:07:43 +02001146 case NL80211_IFTYPE_ADHOC:
1147 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001148 default:
1149 break;
1150 }
1151
1152 return -EOPNOTSUPP;
1153}
1154
1155int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1156{
1157 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1158 int ret;
1159
1160 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1161 vif->addr, ieee80211_vif_type_p2p(vif)))
1162 return -EIO;
1163
Luciano Coelhobca49d92014-05-13 17:33:38 +03001164 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001165 true, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001166 if (ret)
1167 return ret;
1168
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001169 /* will only do anything at resume from D3 time */
1170 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1171
Johannes Berg8ca151b2013-01-24 14:25:36 +01001172 mvmvif->uploaded = true;
1173 return 0;
1174}
1175
Luciano Coelhobca49d92014-05-13 17:33:38 +03001176int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001177 bool force_assoc_off, const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001178{
1179 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1180
1181 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1182 vif->addr, ieee80211_vif_type_p2p(vif)))
1183 return -EIO;
1184
Luciano Coelhobca49d92014-05-13 17:33:38 +03001185 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001186 force_assoc_off, bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001187}
1188
1189int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1190{
1191 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1192 struct iwl_mac_ctx_cmd cmd;
1193 int ret;
1194
1195 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1196 vif->addr, ieee80211_vif_type_p2p(vif)))
1197 return -EIO;
1198
1199 memset(&cmd, 0, sizeof(cmd));
1200
1201 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1202 mvmvif->color));
1203 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1204
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001205 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001206 sizeof(cmd), &cmd);
1207 if (ret) {
1208 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1209 return ret;
1210 }
1211
1212 mvmvif->uploaded = false;
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001213
1214 if (vif->type == NL80211_IFTYPE_MONITOR)
1215 mvm->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
1216
Johannes Berg8ca151b2013-01-24 14:25:36 +01001217 return 0;
1218}
Ilan Peer571765c2013-03-05 15:26:03 +02001219
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001220static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
1221 struct ieee80211_vif *csa_vif, u32 gp2)
1222{
1223 struct iwl_mvm_vif *mvmvif =
1224 iwl_mvm_vif_from_mac80211(csa_vif);
1225
1226 if (!ieee80211_csa_is_complete(csa_vif)) {
1227 int c = ieee80211_csa_update_counter(csa_vif);
1228
1229 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1230 if (csa_vif->p2p &&
1231 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2) {
1232 u32 rel_time = (c + 1) *
1233 csa_vif->bss_conf.beacon_int -
Luciano Coelhof991e172014-08-26 16:14:10 +03001234 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001235 u32 apply_time = gp2 + rel_time * 1024;
1236
Luciano Coelhof991e172014-08-26 16:14:10 +03001237 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1238 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1239 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1240 apply_time);
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001241 }
1242 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1243 /* we don't have CSA NoA scheduled yet, switch now */
1244 ieee80211_csa_finish(csa_vif);
1245 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1246 }
1247}
1248
Ilan Peer571765c2013-03-05 15:26:03 +02001249int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1250 struct iwl_rx_cmd_buffer *rxb,
1251 struct iwl_device_cmd *cmd)
1252{
1253 struct iwl_rx_packet *pkt = rxb_addr(rxb);
David Spinadel1c87bba2014-02-27 16:41:52 +02001254 struct iwl_mvm_tx_resp *beacon_notify_hdr;
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001255 struct ieee80211_vif *csa_vif;
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001256 struct ieee80211_vif *tx_blocked_vif;
David Spinadel1c87bba2014-02-27 16:41:52 +02001257 u64 tsf;
Ilan Peer571765c2013-03-05 15:26:03 +02001258
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001259 lockdep_assert_held(&mvm->mutex);
1260
David Spinadel1c87bba2014-02-27 16:41:52 +02001261 if (mvm->fw->ucode_capa.api[0] & IWL_UCODE_TLV_CAPA_EXTENDED_BEACON) {
1262 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
1263
1264 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1265 tsf = le64_to_cpu(beacon->tsf);
1266 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
1267 } else {
1268 struct iwl_beacon_notif *beacon = (void *)pkt->data;
1269
1270 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1271 tsf = le64_to_cpu(beacon->tsf);
1272 }
1273
1274 IWL_DEBUG_RX(mvm,
1275 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
1276 le16_to_cpu(beacon_notify_hdr->status.status) &
1277 TX_STATUS_MSK,
1278 beacon_notify_hdr->failure_frame, tsf,
1279 mvm->ap_last_beacon_gp2,
1280 le32_to_cpu(beacon_notify_hdr->initial_rate));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001281
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001282 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1283 lockdep_is_held(&mvm->mutex));
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001284 if (unlikely(csa_vif && csa_vif->csa_active))
1285 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2);
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001286
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001287 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1288 lockdep_is_held(&mvm->mutex));
1289 if (unlikely(tx_blocked_vif)) {
1290 struct iwl_mvm_vif *mvmvif =
1291 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1292
1293 /*
1294 * The channel switch is started and we have blocked the
1295 * stations. If this is the first beacon (the timeout wasn't
1296 * set), set the unblock timeout, otherwise countdown
1297 */
1298 if (!mvm->csa_tx_block_bcn_timeout)
1299 mvm->csa_tx_block_bcn_timeout =
1300 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1301 else
1302 mvm->csa_tx_block_bcn_timeout--;
1303
1304 /* Check if the timeout is expired, and unblock tx */
1305 if (mvm->csa_tx_block_bcn_timeout == 0) {
1306 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1307 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1308 }
1309 }
1310
Ilan Peer571765c2013-03-05 15:26:03 +02001311 return 0;
1312}
Hila Gonend64048e2013-03-13 18:00:03 +02001313
1314static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1315 struct ieee80211_vif *vif)
1316{
Ilan Peer12d423e2013-12-24 22:08:14 +02001317 struct iwl_missed_beacons_notif *missed_beacons = _data;
Hila Gonend64048e2013-03-13 18:00:03 +02001318 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1319
Ilan Peer12d423e2013-12-24 22:08:14 +02001320 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1321 return;
1322
1323 /*
1324 * TODO: the threshold should be adjusted based on latency conditions,
1325 * and/or in case of a CS flow on one of the other AP vifs.
1326 */
1327 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1328 IWL_MVM_MISSED_BEACONS_THRESHOLD)
Hila Gonend64048e2013-03-13 18:00:03 +02001329 ieee80211_beacon_loss(vif);
1330}
1331
1332int iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1333 struct iwl_rx_cmd_buffer *rxb,
1334 struct iwl_device_cmd *cmd)
1335{
1336 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Ilan Peer12d423e2013-12-24 22:08:14 +02001337 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1338
1339 IWL_DEBUG_INFO(mvm,
1340 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1341 le32_to_cpu(mb->mac_id),
1342 le32_to_cpu(mb->consec_missed_beacons),
1343 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1344 le32_to_cpu(mb->num_recvd_beacons),
1345 le32_to_cpu(mb->num_expected_beacons));
Hila Gonend64048e2013-03-13 18:00:03 +02001346
1347 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1348 IEEE80211_IFACE_ITER_NORMAL,
1349 iwl_mvm_beacon_loss_iterator,
Ilan Peer12d423e2013-12-24 22:08:14 +02001350 mb);
Hila Gonend64048e2013-03-13 18:00:03 +02001351 return 0;
1352}