blob: 8088c7137f7c9462417ffdfc07a30dcc37be6ec2 [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 Berg8ca151b2013-01-24 14:25:36 +010086 enum iwl_tsf_id preferred_tsf;
87 bool found_vif;
88};
89
Arik Nemtsova74346d2014-09-21 19:07:30 +030090struct iwl_mvm_hw_queues_iface_iterator_data {
91 struct ieee80211_vif *exclude_vif;
92 unsigned long used_hw_queues;
93};
94
Ilan Peer6e97b0d2013-12-23 22:18:02 +020095static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
96 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +010097{
98 struct iwl_mvm_mac_iface_iterator_data *data = _data;
99 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200100 u16 min_bi;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100101
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200102 /* Skip the interface for which we are trying to assign a tsf_id */
103 if (vif == data->vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100104 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100105
106 /*
107 * The TSF is a hardware/firmware resource, there are 4 and
108 * the driver should assign and free them as needed. However,
109 * there are cases where 2 MACs should share the same TSF ID
110 * for the purpose of clock sync, an optimization to avoid
111 * clock drift causing overlapping TBTTs/DTIMs for a GO and
112 * client in the system.
113 *
114 * The firmware will decide according to the MAC type which
115 * will be the master and slave. Clients that need to sync
116 * with a remote station will be the master, and an AP or GO
117 * will be the slave.
118 *
119 * Depending on the new interface type it can be slaved to
120 * or become the master of an existing interface.
121 */
122 switch (data->vif->type) {
123 case NL80211_IFTYPE_STATION:
124 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200125 * The new interface is a client, so if the one we're iterating
126 * is an AP, and the beacon interval of the AP is a multiple or
127 * divisor of the beacon interval of the client, the same TSF
128 * should be used to avoid drift between the new client and
129 * existing AP. The existing AP will get drift updates from the
130 * new client context in this case.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100131 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200132 if (vif->type != NL80211_IFTYPE_AP ||
133 data->preferred_tsf != NUM_TSF_IDS ||
134 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
135 break;
136
137 min_bi = min(data->vif->bss_conf.beacon_int,
138 vif->bss_conf.beacon_int);
139
140 if (!min_bi)
141 break;
142
143 if ((data->vif->bss_conf.beacon_int -
144 vif->bss_conf.beacon_int) % min_bi == 0) {
145 data->preferred_tsf = mvmvif->tsf_id;
146 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100147 }
148 break;
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200149
Johannes Berg8ca151b2013-01-24 14:25:36 +0100150 case NL80211_IFTYPE_AP:
151 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200152 * The new interface is AP/GO, so if its beacon interval is a
153 * multiple or a divisor of the beacon interval of an existing
154 * interface, it should get drift updates from an existing
155 * client or use the same TSF as an existing GO. There's no
156 * drift between TSFs internally but if they used different
157 * TSFs then a new client MAC could update one of them and
158 * cause drift that way.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100159 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200160 if ((vif->type != NL80211_IFTYPE_AP &&
161 vif->type != NL80211_IFTYPE_STATION) ||
162 data->preferred_tsf != NUM_TSF_IDS ||
163 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
164 break;
165
166 min_bi = min(data->vif->bss_conf.beacon_int,
167 vif->bss_conf.beacon_int);
168
169 if (!min_bi)
170 break;
171
172 if ((data->vif->bss_conf.beacon_int -
173 vif->bss_conf.beacon_int) % min_bi == 0) {
174 data->preferred_tsf = mvmvif->tsf_id;
175 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100176 }
177 break;
178 default:
179 /*
180 * For all other interface types there's no need to
181 * take drift into account. Either they're exclusive
182 * like IBSS and monitor, or we don't care much about
183 * their TSF (like P2P Device), but we won't be able
184 * to share the TSF resource.
185 */
186 break;
187 }
188
189 /*
190 * Unless we exited above, we can't share the TSF resource
191 * that the virtual interface we're iterating over is using
192 * with the new one, so clear the available bit and if this
193 * was the preferred one, reset that as well.
194 */
195 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
196
197 if (data->preferred_tsf == mvmvif->tsf_id)
198 data->preferred_tsf = NUM_TSF_IDS;
199}
200
Johannes Berg110cf812014-08-01 23:14:24 +0200201/*
202 * Get the mask of the queues used by the vif
203 */
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300204u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
Johannes Berg110cf812014-08-01 23:14:24 +0200205{
206 u32 qmask = 0, ac;
207
208 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
209 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
210
Ilan Peer1008e442015-01-17 22:35:00 -0500211 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
212 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
213 qmask |= BIT(vif->hw_queue[ac]);
214 }
Johannes Berg110cf812014-08-01 23:14:24 +0200215
216 if (vif->type == NL80211_IFTYPE_AP)
217 qmask |= BIT(vif->cab_queue);
218
219 return qmask;
220}
221
Arik Nemtsova74346d2014-09-21 19:07:30 +0300222static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
223 struct ieee80211_vif *vif)
224{
225 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
226
227 /* exclude the given vif */
228 if (vif == data->exclude_vif)
229 return;
230
231 data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
232}
233
234static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
235 struct ieee80211_sta *sta)
236{
237 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
238 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
239
240 /* Mark the queues used by the sta */
241 data->used_hw_queues |= mvmsta->tfd_queue_msk;
242}
243
244unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
245 struct ieee80211_vif *exclude_vif)
246{
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200247 u8 sta_id;
Arik Nemtsova74346d2014-09-21 19:07:30 +0300248 struct iwl_mvm_hw_queues_iface_iterator_data data = {
249 .exclude_vif = exclude_vif,
250 .used_hw_queues =
251 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
252 BIT(mvm->aux_queue) |
253 BIT(IWL_MVM_CMD_QUEUE),
254 };
255
256 lockdep_assert_held(&mvm->mutex);
257
258 /* mark all VIF used hw queues */
259 ieee80211_iterate_active_interfaces_atomic(
260 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
261 iwl_mvm_iface_hw_queues_iter, &data);
262
263 /* don't assign the same hw queues as TDLS stations */
264 ieee80211_iterate_stations_atomic(mvm->hw,
265 iwl_mvm_mac_sta_hw_queues_iter,
266 &data);
267
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200268 /*
269 * Some TDLS stations may be removed but are in the process of being
270 * drained. Don't touch their queues.
271 */
272 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
273 data.used_hw_queues |= mvm->tfd_drained[sta_id];
274
Arik Nemtsova74346d2014-09-21 19:07:30 +0300275 return data.used_hw_queues;
276}
277
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200278static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
279 struct ieee80211_vif *vif)
280{
281 struct iwl_mvm_mac_iface_iterator_data *data = _data;
282 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200283
284 /* Iterator may already find the interface being added -- skip it */
285 if (vif == data->vif) {
286 data->found_vif = true;
287 return;
288 }
289
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200290 /* Mark MAC IDs as used by clearing the available bit, and
291 * (below) mark TSFs as used if their existing use is not
292 * compatible with the new interface type.
293 * No locking or atomic bit operations are needed since the
294 * data is on the stack of the caller function.
295 */
296 __clear_bit(mvmvif->id, data->available_mac_ids);
297
298 /* find a suitable tsf_id */
299 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
300}
301
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200302void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
303 struct ieee80211_vif *vif)
304{
305 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
306 struct iwl_mvm_mac_iface_iterator_data data = {
307 .mvm = mvm,
308 .vif = vif,
309 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
310 /* no preference yet */
311 .preferred_tsf = NUM_TSF_IDS,
312 };
313
314 ieee80211_iterate_active_interfaces_atomic(
315 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
316 iwl_mvm_mac_tsf_id_iter, &data);
317
318 if (data.preferred_tsf != NUM_TSF_IDS)
319 mvmvif->tsf_id = data.preferred_tsf;
320 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
321 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
322 NUM_TSF_IDS);
323}
324
Johannes Berg8ca151b2013-01-24 14:25:36 +0100325static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
326 struct ieee80211_vif *vif)
327{
328 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
329 struct iwl_mvm_mac_iface_iterator_data data = {
330 .mvm = mvm,
331 .vif = vif,
332 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
333 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
334 /* no preference yet */
335 .preferred_tsf = NUM_TSF_IDS,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100336 .found_vif = false,
337 };
338 u32 ac;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300339 int ret, i;
Johannes Berg110cf812014-08-01 23:14:24 +0200340 unsigned long used_hw_queues;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100341
342 /*
343 * Allocate a MAC ID and a TSF for this MAC, along with the queues
344 * and other resources.
345 */
346
347 /*
348 * Before the iterator, we start with all MAC IDs and TSFs available.
349 *
350 * During iteration, all MAC IDs are cleared that are in use by other
351 * virtual interfaces, and all TSF IDs are cleared that can't be used
352 * by this new virtual interface because they're used by an interface
353 * that can't share it with the new one.
354 * At the same time, we check if there's a preferred TSF in the case
355 * that we should share it with another interface.
356 */
357
Johannes Berg5023d962013-07-31 14:07:43 +0200358 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
359 switch (vif->type) {
360 case NL80211_IFTYPE_ADHOC:
361 break;
362 case NL80211_IFTYPE_STATION:
363 if (!vif->p2p)
364 break;
365 /* fall through */
366 default:
Ilan Peerec8b6882013-02-13 13:27:18 +0200367 __clear_bit(0, data.available_mac_ids);
Johannes Berg5023d962013-07-31 14:07:43 +0200368 }
Ilan Peerec8b6882013-02-13 13:27:18 +0200369
Johannes Berg8ca151b2013-01-24 14:25:36 +0100370 ieee80211_iterate_active_interfaces_atomic(
371 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
372 iwl_mvm_mac_iface_iterator, &data);
373
Arik Nemtsova74346d2014-09-21 19:07:30 +0300374 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
375
Johannes Berg8ca151b2013-01-24 14:25:36 +0100376 /*
377 * In the case we're getting here during resume, it's similar to
378 * firmware restart, and with RESUME_ALL the iterator will find
379 * the vif being added already.
380 * We don't want to reassign any IDs in either case since doing
381 * so would probably assign different IDs (as interfaces aren't
382 * necessarily added in the same order), but the old IDs were
383 * preserved anyway, so skip ID assignment for both resume and
384 * recovery.
385 */
386 if (data.found_vif)
387 return 0;
388
389 /* Therefore, in recovery, we can't get here */
Ilan Peerfd11bd02013-06-16 12:18:11 +0300390 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
391 return -EBUSY;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100392
393 mvmvif->id = find_first_bit(data.available_mac_ids,
394 NUM_MAC_INDEX_DRIVER);
395 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
396 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
397 ret = -EIO;
398 goto exit_fail;
399 }
400
401 if (data.preferred_tsf != NUM_TSF_IDS)
402 mvmvif->tsf_id = data.preferred_tsf;
403 else
404 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
405 NUM_TSF_IDS);
406 if (mvmvif->tsf_id == NUM_TSF_IDS) {
407 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
408 ret = -EIO;
409 goto exit_fail;
410 }
411
412 mvmvif->color = 0;
413
Ilan Peer1e849c92013-02-13 12:26:39 +0200414 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
415 mvmvif->time_event_data.id = TE_MAX;
416
Johannes Berg8ca151b2013-01-24 14:25:36 +0100417 /* No need to allocate data queues to P2P Device MAC.*/
418 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
419 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
420 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
421
422 return 0;
423 }
424
425 /* Find available queues, and allocate them to the ACs */
426 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg110cf812014-08-01 23:14:24 +0200427 u8 queue = find_first_zero_bit(&used_hw_queues,
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200428 mvm->first_agg_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100429
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200430 if (queue >= mvm->first_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100431 IWL_ERR(mvm, "Failed to allocate queue\n");
432 ret = -EIO;
433 goto exit_fail;
434 }
435
Johannes Berg110cf812014-08-01 23:14:24 +0200436 __set_bit(queue, &used_hw_queues);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100437 vif->hw_queue[ac] = queue;
438 }
439
440 /* Allocate the CAB queue for softAP and GO interfaces */
441 if (vif->type == NL80211_IFTYPE_AP) {
Johannes Berg110cf812014-08-01 23:14:24 +0200442 u8 queue = find_first_zero_bit(&used_hw_queues,
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200443 mvm->first_agg_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100444
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200445 if (queue >= mvm->first_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100446 IWL_ERR(mvm, "Failed to allocate cab queue\n");
447 ret = -EIO;
448 goto exit_fail;
449 }
450
451 vif->cab_queue = queue;
452 } else {
453 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
454 }
455
456 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
457 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
458
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300459 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
460 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
461
Johannes Berg8ca151b2013-01-24 14:25:36 +0100462 return 0;
463
464exit_fail:
465 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
466 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
467 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
468 return ret;
469}
470
471int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
472{
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200473 unsigned int wdg_timeout =
474 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100475 u32 ac;
476 int ret;
477
478 lockdep_assert_held(&mvm->mutex);
479
480 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
481 if (ret)
482 return ret;
483
484 switch (vif->type) {
485 case NL80211_IFTYPE_P2P_DEVICE:
Avri Altman3edf8ff2014-07-30 11:41:01 +0300486 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200487 IWL_MVM_TX_FIFO_VO, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100488 break;
489 case NL80211_IFTYPE_AP:
Avri Altman3edf8ff2014-07-30 11:41:01 +0300490 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200491 IWL_MVM_TX_FIFO_MCAST, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100492 /* fall through */
493 default:
494 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Avri Altman3edf8ff2014-07-30 11:41:01 +0300495 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200496 iwl_mvm_ac_to_tx_fifo[ac],
497 wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100498 break;
499 }
500
501 return 0;
502}
503
504void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
505{
506 int ac;
507
508 lockdep_assert_held(&mvm->mutex);
509
510 switch (vif->type) {
511 case NL80211_IFTYPE_P2P_DEVICE:
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200512 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100513 break;
514 case NL80211_IFTYPE_AP:
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200515 iwl_mvm_disable_txq(mvm, vif->cab_queue, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100516 /* fall through */
517 default:
518 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200519 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac], 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100520 }
521}
522
523static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
524 struct ieee80211_vif *vif,
525 enum ieee80211_band band,
526 u8 *cck_rates, u8 *ofdm_rates)
527{
528 struct ieee80211_supported_band *sband;
529 unsigned long basic = vif->bss_conf.basic_rates;
530 int lowest_present_ofdm = 100;
531 int lowest_present_cck = 100;
532 u8 cck = 0;
533 u8 ofdm = 0;
534 int i;
535
536 sband = mvm->hw->wiphy->bands[band];
537
538 for_each_set_bit(i, &basic, BITS_PER_LONG) {
539 int hw = sband->bitrates[i].hw_value;
540 if (hw >= IWL_FIRST_OFDM_RATE) {
541 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
542 if (lowest_present_ofdm > hw)
543 lowest_present_ofdm = hw;
544 } else {
545 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
546
547 cck |= BIT(hw);
548 if (lowest_present_cck > hw)
549 lowest_present_cck = hw;
550 }
551 }
552
553 /*
554 * Now we've got the basic rates as bitmaps in the ofdm and cck
555 * variables. This isn't sufficient though, as there might not
556 * be all the right rates in the bitmap. E.g. if the only basic
557 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
558 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
559 *
560 * [...] a STA responding to a received frame shall transmit
561 * its Control Response frame [...] at the highest rate in the
562 * BSSBasicRateSet parameter that is less than or equal to the
563 * rate of the immediately previous frame in the frame exchange
564 * sequence ([...]) and that is of the same modulation class
565 * ([...]) as the received frame. If no rate contained in the
566 * BSSBasicRateSet parameter meets these conditions, then the
567 * control frame sent in response to a received frame shall be
568 * transmitted at the highest mandatory rate of the PHY that is
569 * less than or equal to the rate of the received frame, and
570 * that is of the same modulation class as the received frame.
571 *
572 * As a consequence, we need to add all mandatory rates that are
573 * lower than all of the basic rates to these bitmaps.
574 */
575
576 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
577 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
578 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
579 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
580 /* 6M already there or needed so always add */
581 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
582
583 /*
584 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
585 * Note, however:
586 * - if no CCK rates are basic, it must be ERP since there must
587 * be some basic rates at all, so they're OFDM => ERP PHY
588 * (or we're in 5 GHz, and the cck bitmap will never be used)
589 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
590 * - if 5.5M is basic, 1M and 2M are mandatory
591 * - if 2M is basic, 1M is mandatory
592 * - if 1M is basic, that's the only valid ACK rate.
593 * As a consequence, it's not as complicated as it sounds, just add
594 * any lower rates to the ACK rate bitmap.
595 */
596 if (IWL_RATE_11M_INDEX < lowest_present_cck)
597 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
598 if (IWL_RATE_5M_INDEX < lowest_present_cck)
599 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
600 if (IWL_RATE_2M_INDEX < lowest_present_cck)
601 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
602 /* 1M already there or needed so always add */
603 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
604
605 *cck_rates = cck;
606 *ofdm_rates = ofdm;
607}
608
Avri Altman8a5e3662013-11-12 19:16:03 +0200609static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
610 struct ieee80211_vif *vif,
611 struct iwl_mac_ctx_cmd *cmd)
612{
613 /* for both sta and ap, ht_operation_mode hold the protection_mode */
614 u8 protection_mode = vif->bss_conf.ht_operation_mode &
615 IEEE80211_HT_OP_MODE_PROTECTION;
616 /* The fw does not distinguish between ht and fat */
617 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
618
619 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
620 /*
621 * See section 9.23.3.1 of IEEE 80211-2012.
622 * Nongreenfield HT STAs Present is not supported.
623 */
624 switch (protection_mode) {
625 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
626 break;
627 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
628 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
629 cmd->protection_flags |= cpu_to_le32(ht_flag);
630 break;
631 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
632 /* Protect when channel wider than 20MHz */
633 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
634 cmd->protection_flags |= cpu_to_le32(ht_flag);
635 break;
636 default:
637 IWL_ERR(mvm, "Illegal protection mode %d\n",
638 protection_mode);
639 break;
640 }
641}
642
Johannes Berg8ca151b2013-01-24 14:25:36 +0100643static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
644 struct ieee80211_vif *vif,
645 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200646 const u8 *bssid_override,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100647 u32 action)
648{
649 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
650 struct ieee80211_chanctx_conf *chanctx;
Avri Altman8a5e3662013-11-12 19:16:03 +0200651 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
652 IEEE80211_HT_OP_MODE_PROTECTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100653 u8 cck_ack_rates, ofdm_ack_rates;
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200654 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100655 int i;
656
657 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
658 mvmvif->color));
659 cmd->action = cpu_to_le32(action);
660
661 switch (vif->type) {
662 case NL80211_IFTYPE_STATION:
663 if (vif->p2p)
664 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
665 else
666 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
667 break;
668 case NL80211_IFTYPE_AP:
669 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
670 break;
671 case NL80211_IFTYPE_MONITOR:
672 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
673 break;
674 case NL80211_IFTYPE_P2P_DEVICE:
675 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
676 break;
677 case NL80211_IFTYPE_ADHOC:
678 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
679 break;
680 default:
681 WARN_ON_ONCE(1);
682 }
683
684 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
685
686 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200687
688 if (bssid)
689 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100690 else
691 eth_broadcast_addr(cmd->bssid_addr);
692
693 rcu_read_lock();
694 chanctx = rcu_dereference(vif->chanctx_conf);
695 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
696 : IEEE80211_BAND_2GHZ,
697 &cck_ack_rates, &ofdm_ack_rates);
698 rcu_read_unlock();
699
700 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
701 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
702
703 cmd->cck_short_preamble =
704 cpu_to_le32(vif->bss_conf.use_short_preamble ?
705 MAC_FLG_SHORT_PREAMBLE : 0);
706 cmd->short_slot =
707 cpu_to_le32(vif->bss_conf.use_short_slot ?
708 MAC_FLG_SHORT_SLOT : 0);
709
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200710 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
711 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
712
713 cmd->ac[txf].cw_min =
714 cpu_to_le16(mvmvif->queue_params[i].cw_min);
715 cmd->ac[txf].cw_max =
716 cpu_to_le16(mvmvif->queue_params[i].cw_max);
717 cmd->ac[txf].edca_txop =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100718 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200719 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
720 cmd->ac[txf].fifos_mask = BIT(txf);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100721 }
722
Emmanuel Grumbach86a91ec2013-05-26 20:47:53 +0300723 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
724 if (vif->type == NL80211_IFTYPE_AP)
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200725 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
726 BIT(IWL_MVM_TX_FIFO_MCAST);
Emmanuel Grumbach86a91ec2013-05-26 20:47:53 +0300727
Johannes Berg8ca151b2013-01-24 14:25:36 +0100728 if (vif->bss_conf.qos)
729 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
730
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300731 if (vif->bss_conf.use_cts_prot)
Dor Shaishcc7ee2b2013-02-18 13:51:36 +0200732 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300733
Avri Altman8a5e3662013-11-12 19:16:03 +0200734 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
735 vif->bss_conf.use_cts_prot,
736 vif->bss_conf.ht_operation_mode);
737 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100738 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
Avri Altman8a5e3662013-11-12 19:16:03 +0200739 if (ht_enabled)
740 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100741
742 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
743}
744
745static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
746 struct iwl_mac_ctx_cmd *cmd)
747{
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300748 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100749 sizeof(*cmd), cmd);
750 if (ret)
751 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
752 le32_to_cpu(cmd->action), ret);
753 return ret;
754}
755
Luciano Coelhocf520232014-05-13 16:52:54 +0300756static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
757 struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200758 u32 action, bool force_assoc_off,
759 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100760{
Luciano Coelhocf520232014-05-13 16:52:54 +0300761 struct iwl_mac_ctx_cmd cmd = {};
762 struct iwl_mac_data_sta *ctxt_sta;
763
764 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
765
766 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200767 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
Luciano Coelhocf520232014-05-13 16:52:54 +0300768
769 if (vif->p2p) {
770 struct ieee80211_p2p_noa_attr *noa =
771 &vif->bss_conf.p2p_noa_attr;
772
773 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
774 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
775 ctxt_sta = &cmd.p2p_sta.sta;
776 } else {
Luciano Coelhocf520232014-05-13 16:52:54 +0300777 ctxt_sta = &cmd.sta;
778 }
779
Johannes Berg210a5442013-01-24 23:48:23 +0100780 /* We need the dtim_period to set the MAC as associated */
Alexander Bondarba283922013-05-02 16:34:48 +0300781 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
782 !force_assoc_off) {
Johannes Bergd2931bb2013-02-05 18:10:04 +0100783 u32 dtim_offs;
784
785 /*
786 * The DTIM count counts down, so when it is N that means N
787 * more beacon intervals happen until the DTIM TBTT. Therefore
788 * add this to the current time. If that ends up being in the
789 * future, the firmware will handle it.
790 *
791 * Also note that the system_timestamp (which we get here as
792 * "sync_device_ts") and TSF timestamp aren't at exactly the
793 * same offset in the frame -- the TSF is at the first symbol
794 * of the TSF, the system timestamp is at signal acquisition
795 * time. This means there's an offset between them of at most
796 * a few hundred microseconds (24 * 8 bits + PLCP time gives
797 * 384us in the longest case), this is currently not relevant
798 * as the firmware wakes up around 2ms before the TBTT.
799 */
800 dtim_offs = vif->bss_conf.sync_dtim_count *
801 vif->bss_conf.beacon_int;
802 /* convert TU to usecs */
803 dtim_offs *= 1024;
804
805 ctxt_sta->dtim_tsf =
806 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
807 ctxt_sta->dtim_time =
808 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
809
810 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
811 le64_to_cpu(ctxt_sta->dtim_tsf),
812 le32_to_cpu(ctxt_sta->dtim_time),
813 dtim_offs);
814
Johannes Berg210a5442013-01-24 23:48:23 +0100815 ctxt_sta->is_assoc = cpu_to_le32(1);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100816 } else {
Johannes Berg210a5442013-01-24 23:48:23 +0100817 ctxt_sta->is_assoc = cpu_to_le32(0);
Luciano Coelho7c8b3bc2014-07-30 15:04:08 +0300818
819 /* Allow beacons to pass through as long as we are not
820 * associated, or we do not have dtim period information.
821 */
822 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100823 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100824
825 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
826 ctxt_sta->bi_reciprocal =
827 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
828 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
829 vif->bss_conf.dtim_period);
830 ctxt_sta->dtim_reciprocal =
831 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
832 vif->bss_conf.dtim_period));
833
834 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
835 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100836
837 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
838}
839
840static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
841 struct ieee80211_vif *vif,
842 u32 action)
843{
844 struct iwl_mac_ctx_cmd cmd = {};
845
846 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
847
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200848 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg53585492013-03-18 13:04:50 +0100849
850 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
851 MAC_FILTER_IN_CONTROL_AND_MGMT |
852 MAC_FILTER_IN_BEACON |
Johannes Bergfb8b8ee2013-10-21 12:37:53 +0200853 MAC_FILTER_IN_PROBE_REQUEST |
854 MAC_FILTER_IN_CRC32);
855 mvm->hw->flags |= IEEE80211_HW_RX_INCLUDES_FCS;
Johannes Berg53585492013-03-18 13:04:50 +0100856
Johannes Berg8ca151b2013-01-24 14:25:36 +0100857 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
858}
859
Johannes Berg5023d962013-07-31 14:07:43 +0200860static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
861 struct ieee80211_vif *vif,
862 u32 action)
863{
864 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
865 struct iwl_mac_ctx_cmd cmd = {};
866
867 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
868
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200869 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg5023d962013-07-31 14:07:43 +0200870
871 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
872 MAC_FILTER_IN_PROBE_REQUEST);
873
874 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
875 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
876 cmd.ibss.bi_reciprocal =
877 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
878
879 /* TODO: Assumes that the beacon id == mac context id */
880 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
881
882 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
883}
884
Johannes Berg8ca151b2013-01-24 14:25:36 +0100885struct iwl_mvm_go_iterator_data {
886 bool go_active;
887};
888
889static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
890{
891 struct iwl_mvm_go_iterator_data *data = _data;
892 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
893
Johannes Berg5023d962013-07-31 14:07:43 +0200894 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
895 mvmvif->ap_ibss_active)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100896 data->go_active = true;
897}
898
899static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
900 struct ieee80211_vif *vif,
901 u32 action)
902{
903 struct iwl_mac_ctx_cmd cmd = {};
904 struct iwl_mvm_go_iterator_data data = {};
905
906 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
907
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200908 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100909
910 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Ilan Peer1dcd15e2013-02-14 14:25:24 +0200911
912 /* Override the filter flags to accept only probe requests */
913 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100914
915 /*
916 * This flag should be set to true when the P2P Device is
917 * discoverable and there is at least another active P2P GO. Settings
918 * this flag will allow the P2P Device to be discoverable on other
919 * channels in addition to its listen channel.
920 * Note that this flag should not be set in other cases as it opens the
921 * Rx filters on all MAC and increases the number of interrupts.
922 */
923 ieee80211_iterate_active_interfaces_atomic(
924 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
925 iwl_mvm_go_iterator, &data);
926
927 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
928 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
929}
930
931static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
932 struct iwl_mac_beacon_cmd *beacon_cmd,
933 u8 *beacon, u32 frame_size)
934{
935 u32 tim_idx;
936 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
937
938 /* The index is relative to frame start but we start looking at the
939 * variable-length part of the beacon. */
940 tim_idx = mgmt->u.beacon.variable - beacon;
941
942 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
943 while ((tim_idx < (frame_size - 2)) &&
944 (beacon[tim_idx] != WLAN_EID_TIM))
945 tim_idx += beacon[tim_idx+1] + 2;
946
947 /* If TIM field was found, set variables */
948 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
949 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
950 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
951 } else {
952 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
953 }
954}
955
956static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
957 struct ieee80211_vif *vif,
958 struct sk_buff *beacon)
959{
960 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
961 struct iwl_host_cmd cmd = {
962 .id = BEACON_TEMPLATE_CMD,
963 .flags = CMD_ASYNC,
964 };
965 struct iwl_mac_beacon_cmd beacon_cmd = {};
966 struct ieee80211_tx_info *info;
967 u32 beacon_skb_len;
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300968 u32 rate, tx_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100969
970 if (WARN_ON(!beacon))
971 return -EINVAL;
972
973 beacon_skb_len = beacon->len;
974
975 /* TODO: for now the beacon template id is set to be the mac context id.
976 * Might be better to handle it as another resource ... */
977 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300978 info = IEEE80211_SKB_CB(beacon);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100979
980 /* Set up TX command fields */
981 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
982 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
983 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300984 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
985 tx_flags |=
986 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
987 TX_CMD_FLG_BT_PRIO_POS;
988 beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100989
990 mvm->mgmt_last_antenna_idx =
Moshe Harela0544272014-12-08 21:13:14 +0200991 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
Johannes Berg8ca151b2013-01-24 14:25:36 +0100992 mvm->mgmt_last_antenna_idx);
993
994 beacon_cmd.tx.rate_n_flags =
995 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
996 RATE_MCS_ANT_POS);
997
Johannes Berg8ca151b2013-01-24 14:25:36 +0100998 if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
999 rate = IWL_FIRST_OFDM_RATE;
1000 } else {
1001 rate = IWL_FIRST_CCK_RATE;
1002 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
1003 }
1004 beacon_cmd.tx.rate_n_flags |=
1005 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1006
1007 /* Set up TX beacon command fields */
Johannes Berg5023d962013-07-31 14:07:43 +02001008 if (vif->type == NL80211_IFTYPE_AP)
1009 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
1010 beacon->data,
1011 beacon_skb_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001012
1013 /* Submit command */
1014 cmd.len[0] = sizeof(beacon_cmd);
1015 cmd.data[0] = &beacon_cmd;
1016 cmd.dataflags[0] = 0;
1017 cmd.len[1] = beacon_skb_len;
1018 cmd.data[1] = beacon->data;
1019 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1020
1021 return iwl_mvm_send_cmd(mvm, &cmd);
1022}
1023
Johannes Berg5023d962013-07-31 14:07:43 +02001024/* The beacon template for the AP/GO/IBSS has changed and needs update */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001025int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1026 struct ieee80211_vif *vif)
1027{
1028 struct sk_buff *beacon;
1029 int ret;
1030
Johannes Berg5023d962013-07-31 14:07:43 +02001031 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1032 vif->type != NL80211_IFTYPE_ADHOC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001033
Andrei Otcheretianskife887662014-04-10 21:46:16 +03001034 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001035 if (!beacon)
1036 return -ENOMEM;
1037
1038 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1039 dev_kfree_skb(beacon);
1040 return ret;
1041}
1042
Ilan Peer3a3cb922013-06-13 08:30:12 +03001043struct iwl_mvm_mac_ap_iterator_data {
1044 struct iwl_mvm *mvm;
1045 struct ieee80211_vif *vif;
1046 u32 beacon_device_ts;
1047 u16 beacon_int;
1048};
1049
1050/* Find the beacon_device_ts and beacon_int for a managed interface */
1051static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1052 struct ieee80211_vif *vif)
1053{
1054 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1055
1056 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1057 return;
1058
1059 /* Station client has higher priority over P2P client*/
1060 if (vif->p2p && data->beacon_device_ts)
1061 return;
1062
1063 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1064 data->beacon_int = vif->bss_conf.beacon_int;
1065}
1066
Johannes Berg8ca151b2013-01-24 14:25:36 +01001067/*
1068 * Fill the specific data for mac context of type AP of P2P GO
1069 */
1070static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1071 struct ieee80211_vif *vif,
Johannes Berg506a81e2013-02-28 14:05:14 +01001072 struct iwl_mac_data_ap *ctxt_ap,
1073 bool add)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001074{
1075 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer3a3cb922013-06-13 08:30:12 +03001076 struct iwl_mvm_mac_ap_iterator_data data = {
1077 .mvm = mvm,
1078 .vif = vif,
1079 .beacon_device_ts = 0
1080 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001081
1082 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1083 ctxt_ap->bi_reciprocal =
1084 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1085 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1086 vif->bss_conf.dtim_period);
1087 ctxt_ap->dtim_reciprocal =
1088 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1089 vif->bss_conf.dtim_period));
1090
1091 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001092
Johannes Berg506a81e2013-02-28 14:05:14 +01001093 /*
Ilan Peer3a3cb922013-06-13 08:30:12 +03001094 * Only set the beacon time when the MAC is being added, when we
Johannes Berg506a81e2013-02-28 14:05:14 +01001095 * just modify the MAC then we should keep the time -- the firmware
1096 * can otherwise have a "jumping" TBTT.
1097 */
Ilan Peer3a3cb922013-06-13 08:30:12 +03001098 if (add) {
1099 /*
1100 * If there is a station/P2P client interface which is
1101 * associated, set the AP's TBTT far enough from the station's
1102 * TBTT. Otherwise, set it to the current system time
1103 */
1104 ieee80211_iterate_active_interfaces_atomic(
1105 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1106 iwl_mvm_mac_ap_iterator, &data);
1107
1108 if (data.beacon_device_ts) {
Ilan Peer9f8f8ca2013-12-24 17:17:05 +02001109 u32 rand = (prandom_u32() % (64 - 36)) + 36;
Ilan Peer3a3cb922013-06-13 08:30:12 +03001110 mvmvif->ap_beacon_time = data.beacon_device_ts +
1111 ieee80211_tu_to_usec(data.beacon_int * rand /
1112 100);
1113 } else {
1114 mvmvif->ap_beacon_time =
1115 iwl_read_prph(mvm->trans,
1116 DEVICE_SYSTEM_TIME_REG);
1117 }
1118 }
Johannes Berg506a81e2013-02-28 14:05:14 +01001119
1120 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
Johannes Berg506a81e2013-02-28 14:05:14 +01001121 ctxt_ap->beacon_tsf = 0; /* unused */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001122
1123 /* TODO: Assume that the beacon id == mac context id */
1124 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1125}
1126
1127static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1128 struct ieee80211_vif *vif,
1129 u32 action)
1130{
1131 struct iwl_mac_ctx_cmd cmd = {};
1132
1133 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1134
1135 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001136 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001137
Eliad Peller46018792014-07-16 13:24:36 +03001138 /*
1139 * pass probe requests and beacons from other APs (needed
1140 * for ht protection)
1141 */
1142 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1143 MAC_FILTER_IN_BEACON);
Ilan Peer1dcd15e2013-02-14 14:25:24 +02001144
Johannes Berg8ca151b2013-01-24 14:25:36 +01001145 /* Fill the data specific for ap mode */
Johannes Berg506a81e2013-02-28 14:05:14 +01001146 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1147 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001148
1149 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1150}
1151
1152static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1153 struct ieee80211_vif *vif,
1154 u32 action)
1155{
1156 struct iwl_mac_ctx_cmd cmd = {};
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001157 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001158
1159 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1160
1161 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001162 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001163
Eliad Peller46018792014-07-16 13:24:36 +03001164 /*
1165 * pass probe requests and beacons from other APs (needed
1166 * for ht protection)
1167 */
1168 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1169 MAC_FILTER_IN_BEACON);
1170
Johannes Berg8ca151b2013-01-24 14:25:36 +01001171 /* Fill the data specific for GO mode */
Johannes Berg506a81e2013-02-28 14:05:14 +01001172 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1173 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001174
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001175 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1176 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1177 cmd.go.opp_ps_enabled =
1178 cpu_to_le32(!!(noa->oppps_ctwindow &
1179 IEEE80211_P2P_OPPPS_ENABLE_BIT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001180
1181 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1182}
1183
1184static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001185 u32 action, bool force_assoc_off,
1186 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001187{
1188 switch (vif->type) {
1189 case NL80211_IFTYPE_STATION:
Luciano Coelhocf520232014-05-13 16:52:54 +03001190 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001191 force_assoc_off,
1192 bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001193 break;
1194 case NL80211_IFTYPE_AP:
1195 if (!vif->p2p)
1196 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1197 else
1198 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1199 break;
1200 case NL80211_IFTYPE_MONITOR:
1201 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1202 case NL80211_IFTYPE_P2P_DEVICE:
1203 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
Johannes Berg5023d962013-07-31 14:07:43 +02001204 case NL80211_IFTYPE_ADHOC:
1205 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001206 default:
1207 break;
1208 }
1209
1210 return -EOPNOTSUPP;
1211}
1212
1213int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1214{
1215 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1216 int ret;
1217
1218 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1219 vif->addr, ieee80211_vif_type_p2p(vif)))
1220 return -EIO;
1221
Luciano Coelhobca49d92014-05-13 17:33:38 +03001222 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001223 true, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001224 if (ret)
1225 return ret;
1226
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001227 /* will only do anything at resume from D3 time */
1228 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1229
Johannes Berg8ca151b2013-01-24 14:25:36 +01001230 mvmvif->uploaded = true;
1231 return 0;
1232}
1233
Luciano Coelhobca49d92014-05-13 17:33:38 +03001234int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001235 bool force_assoc_off, const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001236{
1237 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1238
1239 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1240 vif->addr, ieee80211_vif_type_p2p(vif)))
1241 return -EIO;
1242
Luciano Coelhobca49d92014-05-13 17:33:38 +03001243 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001244 force_assoc_off, bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001245}
1246
1247int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1248{
1249 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1250 struct iwl_mac_ctx_cmd cmd;
1251 int ret;
1252
1253 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1254 vif->addr, ieee80211_vif_type_p2p(vif)))
1255 return -EIO;
1256
1257 memset(&cmd, 0, sizeof(cmd));
1258
1259 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1260 mvmvif->color));
1261 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1262
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001263 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001264 sizeof(cmd), &cmd);
1265 if (ret) {
1266 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1267 return ret;
1268 }
1269
1270 mvmvif->uploaded = false;
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001271
1272 if (vif->type == NL80211_IFTYPE_MONITOR)
1273 mvm->hw->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
1274
Johannes Berg8ca151b2013-01-24 14:25:36 +01001275 return 0;
1276}
Ilan Peer571765c2013-03-05 15:26:03 +02001277
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001278static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001279 struct ieee80211_vif *csa_vif, u32 gp2,
1280 bool tx_success)
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001281{
1282 struct iwl_mvm_vif *mvmvif =
1283 iwl_mvm_vif_from_mac80211(csa_vif);
1284
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001285 /* Don't start to countdown from a failed beacon */
1286 if (!tx_success && !mvmvif->csa_countdown)
1287 return;
1288
1289 mvmvif->csa_countdown = true;
1290
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001291 if (!ieee80211_csa_is_complete(csa_vif)) {
1292 int c = ieee80211_csa_update_counter(csa_vif);
1293
1294 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1295 if (csa_vif->p2p &&
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001296 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1297 tx_success) {
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001298 u32 rel_time = (c + 1) *
1299 csa_vif->bss_conf.beacon_int -
Luciano Coelhof991e172014-08-26 16:14:10 +03001300 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001301 u32 apply_time = gp2 + rel_time * 1024;
1302
Luciano Coelhof991e172014-08-26 16:14:10 +03001303 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1304 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1305 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1306 apply_time);
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001307 }
1308 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1309 /* we don't have CSA NoA scheduled yet, switch now */
1310 ieee80211_csa_finish(csa_vif);
1311 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1312 }
1313}
1314
Ilan Peer571765c2013-03-05 15:26:03 +02001315int iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1316 struct iwl_rx_cmd_buffer *rxb,
1317 struct iwl_device_cmd *cmd)
1318{
1319 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001320 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
David Spinadel1c87bba2014-02-27 16:41:52 +02001321 struct iwl_mvm_tx_resp *beacon_notify_hdr;
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001322 struct ieee80211_vif *csa_vif;
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001323 struct ieee80211_vif *tx_blocked_vif;
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001324 u16 status;
Ilan Peer571765c2013-03-05 15:26:03 +02001325
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001326 lockdep_assert_held(&mvm->mutex);
1327
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001328 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1329 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
David Spinadel1c87bba2014-02-27 16:41:52 +02001330
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001331 status = le16_to_cpu(beacon_notify_hdr->status.status) & TX_STATUS_MSK;
David Spinadel1c87bba2014-02-27 16:41:52 +02001332 IWL_DEBUG_RX(mvm,
1333 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001334 status, beacon_notify_hdr->failure_frame,
1335 le64_to_cpu(beacon->tsf),
David Spinadel1c87bba2014-02-27 16:41:52 +02001336 mvm->ap_last_beacon_gp2,
1337 le32_to_cpu(beacon_notify_hdr->initial_rate));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001338
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001339 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1340 lockdep_is_held(&mvm->mutex));
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001341 if (unlikely(csa_vif && csa_vif->csa_active))
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001342 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1343 (status == TX_STATUS_SUCCESS));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001344
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001345 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1346 lockdep_is_held(&mvm->mutex));
1347 if (unlikely(tx_blocked_vif)) {
1348 struct iwl_mvm_vif *mvmvif =
1349 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1350
1351 /*
1352 * The channel switch is started and we have blocked the
1353 * stations. If this is the first beacon (the timeout wasn't
1354 * set), set the unblock timeout, otherwise countdown
1355 */
1356 if (!mvm->csa_tx_block_bcn_timeout)
1357 mvm->csa_tx_block_bcn_timeout =
1358 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1359 else
1360 mvm->csa_tx_block_bcn_timeout--;
1361
1362 /* Check if the timeout is expired, and unblock tx */
1363 if (mvm->csa_tx_block_bcn_timeout == 0) {
1364 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1365 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1366 }
1367 }
1368
Ilan Peer571765c2013-03-05 15:26:03 +02001369 return 0;
1370}
Hila Gonend64048e2013-03-13 18:00:03 +02001371
1372static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1373 struct ieee80211_vif *vif)
1374{
Ilan Peer12d423e2013-12-24 22:08:14 +02001375 struct iwl_missed_beacons_notif *missed_beacons = _data;
Hila Gonend64048e2013-03-13 18:00:03 +02001376 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001377 struct iwl_mvm *mvm = mvmvif->mvm;
1378 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1379 struct iwl_fw_dbg_trigger_tlv *trigger;
1380 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1381 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
Hila Gonend64048e2013-03-13 18:00:03 +02001382
Ilan Peer12d423e2013-12-24 22:08:14 +02001383 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1384 return;
1385
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001386 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1387 rx_missed_bcon_since_rx =
1388 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
Ilan Peer12d423e2013-12-24 22:08:14 +02001389 /*
1390 * TODO: the threshold should be adjusted based on latency conditions,
1391 * and/or in case of a CS flow on one of the other AP vifs.
1392 */
1393 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1394 IWL_MVM_MISSED_BEACONS_THRESHOLD)
Hila Gonend64048e2013-03-13 18:00:03 +02001395 ieee80211_beacon_loss(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001396
1397 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1398 FW_DBG_TRIGGER_MISSED_BEACONS))
1399 return;
1400
1401 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1402 FW_DBG_TRIGGER_MISSED_BEACONS);
1403 bcon_trig = (void *)trigger->data;
1404 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1405 stop_trig_missed_bcon_since_rx =
1406 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1407
1408 /* TODO: implement start trigger */
1409
1410 if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
1411 return;
1412
1413 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1414 rx_missed_bcon >= stop_trig_missed_bcon)
Johannes Berg5d4f9292015-03-31 09:12:54 +02001415 iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
Hila Gonend64048e2013-03-13 18:00:03 +02001416}
1417
1418int iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1419 struct iwl_rx_cmd_buffer *rxb,
1420 struct iwl_device_cmd *cmd)
1421{
1422 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Ilan Peer12d423e2013-12-24 22:08:14 +02001423 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1424
1425 IWL_DEBUG_INFO(mvm,
1426 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1427 le32_to_cpu(mb->mac_id),
1428 le32_to_cpu(mb->consec_missed_beacons),
1429 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1430 le32_to_cpu(mb->num_recvd_beacons),
1431 le32_to_cpu(mb->num_expected_beacons));
Hila Gonend64048e2013-03-13 18:00:03 +02001432
1433 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1434 IEEE80211_IFACE_ITER_NORMAL,
1435 iwl_mvm_beacon_loss_iterator,
Ilan Peer12d423e2013-12-24 22:08:14 +02001436 mb);
Hila Gonend64048e2013-03-13 18:00:03 +02001437 return 0;
1438}