blob: 361565076eaf88200b3bc364f4974b5ae1fdbb47 [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
Gregory Greenman94939082015-08-24 14:38:35 +030010 * Copyright(c) 2015 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010011 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of version 2 of the GNU General Public License as
14 * published by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
24 * USA
25 *
26 * The full GNU General Public License is included in this distribution
Emmanuel Grumbach410dc5a2013-02-18 09:22:28 +020027 * in the file called COPYING.
Johannes Berg8ca151b2013-01-24 14:25:36 +010028 *
29 * Contact Information:
30 * Intel Linux Wireless <ilw@linux.intel.com>
31 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
32 *
33 * BSD LICENSE
34 *
Emmanuel Grumbach51368bf2013-12-30 13:15:54 +020035 * Copyright(c) 2012 - 2014 Intel Corporation. All rights reserved.
Johannes Berg8b4139d2014-07-24 14:05:26 +020036 * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
Gregory Greenman94939082015-08-24 14:38:35 +030037 * Copyright(c) 2015 Intel Deutschland GmbH
Johannes Berg8ca151b2013-01-24 14:25:36 +010038 * All rights reserved.
39 *
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that the following conditions
42 * are met:
43 *
44 * * Redistributions of source code must retain the above copyright
45 * notice, this list of conditions and the following disclaimer.
46 * * Redistributions in binary form must reproduce the above copyright
47 * notice, this list of conditions and the following disclaimer in
48 * the documentation and/or other materials provided with the
49 * distribution.
50 * * Neither the name Intel Corporation nor the names of its
51 * contributors may be used to endorse or promote products derived
52 * from this software without specific prior written permission.
53 *
54 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
55 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
56 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
57 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
58 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
59 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
61 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
62 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
63 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
64 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 *****************************************************************************/
67
68#include <linux/etherdevice.h>
69#include <net/mac80211.h>
70#include "iwl-io.h"
71#include "iwl-prph.h"
72#include "fw-api.h"
73#include "mvm.h"
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +030074#include "time-event.h"
Johannes Berg8ca151b2013-01-24 14:25:36 +010075
76const u8 iwl_mvm_ac_to_tx_fifo[] = {
Johannes Berg8ca151b2013-01-24 14:25:36 +010077 IWL_MVM_TX_FIFO_VO,
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +020078 IWL_MVM_TX_FIFO_VI,
79 IWL_MVM_TX_FIFO_BE,
80 IWL_MVM_TX_FIFO_BK,
Johannes Berg8ca151b2013-01-24 14:25:36 +010081};
82
83struct iwl_mvm_mac_iface_iterator_data {
84 struct iwl_mvm *mvm;
85 struct ieee80211_vif *vif;
86 unsigned long available_mac_ids[BITS_TO_LONGS(NUM_MAC_INDEX_DRIVER)];
87 unsigned long available_tsf_ids[BITS_TO_LONGS(NUM_TSF_IDS)];
Johannes Berg8ca151b2013-01-24 14:25:36 +010088 enum iwl_tsf_id preferred_tsf;
89 bool found_vif;
90};
91
Arik Nemtsova74346d2014-09-21 19:07:30 +030092struct iwl_mvm_hw_queues_iface_iterator_data {
93 struct ieee80211_vif *exclude_vif;
94 unsigned long used_hw_queues;
95};
96
Ilan Peer6e97b0d2013-12-23 22:18:02 +020097static void iwl_mvm_mac_tsf_id_iter(void *_data, u8 *mac,
98 struct ieee80211_vif *vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +010099{
100 struct iwl_mvm_mac_iface_iterator_data *data = _data;
101 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200102 u16 min_bi;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100103
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200104 /* Skip the interface for which we are trying to assign a tsf_id */
105 if (vif == data->vif)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100106 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100107
108 /*
109 * The TSF is a hardware/firmware resource, there are 4 and
110 * the driver should assign and free them as needed. However,
111 * there are cases where 2 MACs should share the same TSF ID
112 * for the purpose of clock sync, an optimization to avoid
113 * clock drift causing overlapping TBTTs/DTIMs for a GO and
114 * client in the system.
115 *
116 * The firmware will decide according to the MAC type which
117 * will be the master and slave. Clients that need to sync
118 * with a remote station will be the master, and an AP or GO
119 * will be the slave.
120 *
121 * Depending on the new interface type it can be slaved to
122 * or become the master of an existing interface.
123 */
124 switch (data->vif->type) {
125 case NL80211_IFTYPE_STATION:
126 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200127 * The new interface is a client, so if the one we're iterating
128 * is an AP, and the beacon interval of the AP is a multiple or
129 * divisor of the beacon interval of the client, the same TSF
130 * should be used to avoid drift between the new client and
131 * existing AP. The existing AP will get drift updates from the
132 * new client context in this case.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100133 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200134 if (vif->type != NL80211_IFTYPE_AP ||
135 data->preferred_tsf != NUM_TSF_IDS ||
136 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
137 break;
138
139 min_bi = min(data->vif->bss_conf.beacon_int,
140 vif->bss_conf.beacon_int);
141
142 if (!min_bi)
143 break;
144
145 if ((data->vif->bss_conf.beacon_int -
146 vif->bss_conf.beacon_int) % min_bi == 0) {
147 data->preferred_tsf = mvmvif->tsf_id;
148 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100149 }
150 break;
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200151
Johannes Berg8ca151b2013-01-24 14:25:36 +0100152 case NL80211_IFTYPE_AP:
153 /*
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200154 * The new interface is AP/GO, so if its beacon interval is a
155 * multiple or a divisor of the beacon interval of an existing
156 * interface, it should get drift updates from an existing
157 * client or use the same TSF as an existing GO. There's no
158 * drift between TSFs internally but if they used different
159 * TSFs then a new client MAC could update one of them and
160 * cause drift that way.
Johannes Berg8ca151b2013-01-24 14:25:36 +0100161 */
Ilan Peer2c3e62a2014-02-02 21:54:35 +0200162 if ((vif->type != NL80211_IFTYPE_AP &&
163 vif->type != NL80211_IFTYPE_STATION) ||
164 data->preferred_tsf != NUM_TSF_IDS ||
165 !test_bit(mvmvif->tsf_id, data->available_tsf_ids))
166 break;
167
168 min_bi = min(data->vif->bss_conf.beacon_int,
169 vif->bss_conf.beacon_int);
170
171 if (!min_bi)
172 break;
173
174 if ((data->vif->bss_conf.beacon_int -
175 vif->bss_conf.beacon_int) % min_bi == 0) {
176 data->preferred_tsf = mvmvif->tsf_id;
177 return;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100178 }
179 break;
180 default:
181 /*
182 * For all other interface types there's no need to
183 * take drift into account. Either they're exclusive
184 * like IBSS and monitor, or we don't care much about
185 * their TSF (like P2P Device), but we won't be able
186 * to share the TSF resource.
187 */
188 break;
189 }
190
191 /*
192 * Unless we exited above, we can't share the TSF resource
193 * that the virtual interface we're iterating over is using
194 * with the new one, so clear the available bit and if this
195 * was the preferred one, reset that as well.
196 */
197 __clear_bit(mvmvif->tsf_id, data->available_tsf_ids);
198
199 if (data->preferred_tsf == mvmvif->tsf_id)
200 data->preferred_tsf = NUM_TSF_IDS;
201}
202
Johannes Berg110cf812014-08-01 23:14:24 +0200203/*
204 * Get the mask of the queues used by the vif
205 */
Arik Nemtsovd92b732e2014-09-21 19:00:42 +0300206u32 iwl_mvm_mac_get_queues_mask(struct ieee80211_vif *vif)
Johannes Berg110cf812014-08-01 23:14:24 +0200207{
208 u32 qmask = 0, ac;
209
210 if (vif->type == NL80211_IFTYPE_P2P_DEVICE)
211 return BIT(IWL_MVM_OFFCHANNEL_QUEUE);
212
Ilan Peer1008e442015-01-17 22:35:00 -0500213 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
214 if (vif->hw_queue[ac] != IEEE80211_INVAL_HW_QUEUE)
215 qmask |= BIT(vif->hw_queue[ac]);
216 }
Johannes Berg110cf812014-08-01 23:14:24 +0200217
218 if (vif->type == NL80211_IFTYPE_AP)
219 qmask |= BIT(vif->cab_queue);
220
221 return qmask;
222}
223
Arik Nemtsova74346d2014-09-21 19:07:30 +0300224static void iwl_mvm_iface_hw_queues_iter(void *_data, u8 *mac,
225 struct ieee80211_vif *vif)
226{
227 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
228
229 /* exclude the given vif */
230 if (vif == data->exclude_vif)
231 return;
232
233 data->used_hw_queues |= iwl_mvm_mac_get_queues_mask(vif);
234}
235
236static void iwl_mvm_mac_sta_hw_queues_iter(void *_data,
237 struct ieee80211_sta *sta)
238{
239 struct iwl_mvm_hw_queues_iface_iterator_data *data = _data;
240 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta);
241
242 /* Mark the queues used by the sta */
243 data->used_hw_queues |= mvmsta->tfd_queue_msk;
244}
245
246unsigned long iwl_mvm_get_used_hw_queues(struct iwl_mvm *mvm,
247 struct ieee80211_vif *exclude_vif)
248{
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200249 u8 sta_id;
Arik Nemtsova74346d2014-09-21 19:07:30 +0300250 struct iwl_mvm_hw_queues_iface_iterator_data data = {
251 .exclude_vif = exclude_vif,
252 .used_hw_queues =
253 BIT(IWL_MVM_OFFCHANNEL_QUEUE) |
254 BIT(mvm->aux_queue) |
255 BIT(IWL_MVM_CMD_QUEUE),
256 };
257
258 lockdep_assert_held(&mvm->mutex);
259
260 /* mark all VIF used hw queues */
261 ieee80211_iterate_active_interfaces_atomic(
262 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
263 iwl_mvm_iface_hw_queues_iter, &data);
264
265 /* don't assign the same hw queues as TDLS stations */
266 ieee80211_iterate_stations_atomic(mvm->hw,
267 iwl_mvm_mac_sta_hw_queues_iter,
268 &data);
269
Arik Nemtsov51ec09a2015-01-21 11:50:31 +0200270 /*
271 * Some TDLS stations may be removed but are in the process of being
272 * drained. Don't touch their queues.
273 */
274 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT)
275 data.used_hw_queues |= mvm->tfd_drained[sta_id];
276
Arik Nemtsova74346d2014-09-21 19:07:30 +0300277 return data.used_hw_queues;
278}
279
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200280static void iwl_mvm_mac_iface_iterator(void *_data, u8 *mac,
281 struct ieee80211_vif *vif)
282{
283 struct iwl_mvm_mac_iface_iterator_data *data = _data;
284 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200285
286 /* Iterator may already find the interface being added -- skip it */
287 if (vif == data->vif) {
288 data->found_vif = true;
289 return;
290 }
291
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200292 /* Mark MAC IDs as used by clearing the available bit, and
293 * (below) mark TSFs as used if their existing use is not
294 * compatible with the new interface type.
295 * No locking or atomic bit operations are needed since the
296 * data is on the stack of the caller function.
297 */
298 __clear_bit(mvmvif->id, data->available_mac_ids);
299
300 /* find a suitable tsf_id */
301 iwl_mvm_mac_tsf_id_iter(_data, mac, vif);
302}
303
Ilan Peer6e97b0d2013-12-23 22:18:02 +0200304void iwl_mvm_mac_ctxt_recalc_tsf_id(struct iwl_mvm *mvm,
305 struct ieee80211_vif *vif)
306{
307 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
308 struct iwl_mvm_mac_iface_iterator_data data = {
309 .mvm = mvm,
310 .vif = vif,
311 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
312 /* no preference yet */
313 .preferred_tsf = NUM_TSF_IDS,
314 };
315
316 ieee80211_iterate_active_interfaces_atomic(
317 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
318 iwl_mvm_mac_tsf_id_iter, &data);
319
320 if (data.preferred_tsf != NUM_TSF_IDS)
321 mvmvif->tsf_id = data.preferred_tsf;
322 else if (!test_bit(mvmvif->tsf_id, data.available_tsf_ids))
323 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
324 NUM_TSF_IDS);
325}
326
Johannes Berg8ca151b2013-01-24 14:25:36 +0100327static int iwl_mvm_mac_ctxt_allocate_resources(struct iwl_mvm *mvm,
328 struct ieee80211_vif *vif)
329{
330 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
331 struct iwl_mvm_mac_iface_iterator_data data = {
332 .mvm = mvm,
333 .vif = vif,
334 .available_mac_ids = { (1 << NUM_MAC_INDEX_DRIVER) - 1 },
335 .available_tsf_ids = { (1 << NUM_TSF_IDS) - 1 },
336 /* no preference yet */
337 .preferred_tsf = NUM_TSF_IDS,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100338 .found_vif = false,
339 };
340 u32 ac;
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300341 int ret, i;
Johannes Berg110cf812014-08-01 23:14:24 +0200342 unsigned long used_hw_queues;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100343
344 /*
345 * Allocate a MAC ID and a TSF for this MAC, along with the queues
346 * and other resources.
347 */
348
349 /*
350 * Before the iterator, we start with all MAC IDs and TSFs available.
351 *
352 * During iteration, all MAC IDs are cleared that are in use by other
353 * virtual interfaces, and all TSF IDs are cleared that can't be used
354 * by this new virtual interface because they're used by an interface
355 * that can't share it with the new one.
356 * At the same time, we check if there's a preferred TSF in the case
357 * that we should share it with another interface.
358 */
359
Johannes Berg5023d962013-07-31 14:07:43 +0200360 /* Currently, MAC ID 0 should be used only for the managed/IBSS vif */
361 switch (vif->type) {
362 case NL80211_IFTYPE_ADHOC:
363 break;
364 case NL80211_IFTYPE_STATION:
365 if (!vif->p2p)
366 break;
367 /* fall through */
368 default:
Ilan Peerec8b6882013-02-13 13:27:18 +0200369 __clear_bit(0, data.available_mac_ids);
Johannes Berg5023d962013-07-31 14:07:43 +0200370 }
Ilan Peerec8b6882013-02-13 13:27:18 +0200371
Johannes Berg8ca151b2013-01-24 14:25:36 +0100372 ieee80211_iterate_active_interfaces_atomic(
373 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
374 iwl_mvm_mac_iface_iterator, &data);
375
Arik Nemtsova74346d2014-09-21 19:07:30 +0300376 used_hw_queues = iwl_mvm_get_used_hw_queues(mvm, vif);
377
Johannes Berg8ca151b2013-01-24 14:25:36 +0100378 /*
379 * In the case we're getting here during resume, it's similar to
380 * firmware restart, and with RESUME_ALL the iterator will find
381 * the vif being added already.
382 * We don't want to reassign any IDs in either case since doing
383 * so would probably assign different IDs (as interfaces aren't
384 * necessarily added in the same order), but the old IDs were
385 * preserved anyway, so skip ID assignment for both resume and
386 * recovery.
387 */
388 if (data.found_vif)
389 return 0;
390
391 /* Therefore, in recovery, we can't get here */
Ilan Peerfd11bd02013-06-16 12:18:11 +0300392 if (WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)))
393 return -EBUSY;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100394
395 mvmvif->id = find_first_bit(data.available_mac_ids,
396 NUM_MAC_INDEX_DRIVER);
397 if (mvmvif->id == NUM_MAC_INDEX_DRIVER) {
398 IWL_ERR(mvm, "Failed to init MAC context - no free ID!\n");
399 ret = -EIO;
400 goto exit_fail;
401 }
402
403 if (data.preferred_tsf != NUM_TSF_IDS)
404 mvmvif->tsf_id = data.preferred_tsf;
405 else
406 mvmvif->tsf_id = find_first_bit(data.available_tsf_ids,
407 NUM_TSF_IDS);
408 if (mvmvif->tsf_id == NUM_TSF_IDS) {
409 IWL_ERR(mvm, "Failed to init MAC context - no free TSF!\n");
410 ret = -EIO;
411 goto exit_fail;
412 }
413
414 mvmvif->color = 0;
415
Ilan Peer1e849c92013-02-13 12:26:39 +0200416 INIT_LIST_HEAD(&mvmvif->time_event_data.list);
417 mvmvif->time_event_data.id = TE_MAX;
418
Johannes Berg8ca151b2013-01-24 14:25:36 +0100419 /* No need to allocate data queues to P2P Device MAC.*/
420 if (vif->type == NL80211_IFTYPE_P2P_DEVICE) {
421 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
422 vif->hw_queue[ac] = IEEE80211_INVAL_HW_QUEUE;
423
424 return 0;
425 }
426
427 /* Find available queues, and allocate them to the ACs */
428 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++) {
Johannes Berg110cf812014-08-01 23:14:24 +0200429 u8 queue = find_first_zero_bit(&used_hw_queues,
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200430 mvm->first_agg_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100431
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200432 if (queue >= mvm->first_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100433 IWL_ERR(mvm, "Failed to allocate queue\n");
434 ret = -EIO;
435 goto exit_fail;
436 }
437
Johannes Berg110cf812014-08-01 23:14:24 +0200438 __set_bit(queue, &used_hw_queues);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100439 vif->hw_queue[ac] = queue;
440 }
441
442 /* Allocate the CAB queue for softAP and GO interfaces */
443 if (vif->type == NL80211_IFTYPE_AP) {
Johannes Berg110cf812014-08-01 23:14:24 +0200444 u8 queue = find_first_zero_bit(&used_hw_queues,
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200445 mvm->first_agg_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100446
Eytan Lifshitz19e737c2013-09-09 13:30:15 +0200447 if (queue >= mvm->first_agg_queue) {
Johannes Berg8ca151b2013-01-24 14:25:36 +0100448 IWL_ERR(mvm, "Failed to allocate cab queue\n");
449 ret = -EIO;
450 goto exit_fail;
451 }
452
453 vif->cab_queue = queue;
454 } else {
455 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
456 }
457
458 mvmvif->bcast_sta.sta_id = IWL_MVM_STATION_COUNT;
459 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
460
Eytan Lifshitz9ee718a2013-05-19 19:14:41 +0300461 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++)
462 mvmvif->smps_requests[i] = IEEE80211_SMPS_AUTOMATIC;
463
Johannes Berg8ca151b2013-01-24 14:25:36 +0100464 return 0;
465
466exit_fail:
467 memset(mvmvif, 0, sizeof(struct iwl_mvm_vif));
468 memset(vif->hw_queue, IEEE80211_INVAL_HW_QUEUE, sizeof(vif->hw_queue));
469 vif->cab_queue = IEEE80211_INVAL_HW_QUEUE;
470 return ret;
471}
472
473int iwl_mvm_mac_ctxt_init(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
474{
Emmanuel Grumbach5d42e7b2015-03-19 20:04:51 +0200475 unsigned int wdg_timeout =
476 iwl_mvm_get_wd_timeout(mvm, vif, false, false);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100477 u32 ac;
478 int ret;
479
480 lockdep_assert_held(&mvm->mutex);
481
482 ret = iwl_mvm_mac_ctxt_allocate_resources(mvm, vif);
483 if (ret)
484 return ret;
485
486 switch (vif->type) {
487 case NL80211_IFTYPE_P2P_DEVICE:
Avri Altman3edf8ff2014-07-30 11:41:01 +0300488 iwl_mvm_enable_ac_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200489 IWL_MVM_TX_FIFO_VO, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100490 break;
491 case NL80211_IFTYPE_AP:
Avri Altman3edf8ff2014-07-30 11:41:01 +0300492 iwl_mvm_enable_ac_txq(mvm, vif->cab_queue,
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200493 IWL_MVM_TX_FIFO_MCAST, wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100494 /* fall through */
495 default:
496 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Avri Altman3edf8ff2014-07-30 11:41:01 +0300497 iwl_mvm_enable_ac_txq(mvm, vif->hw_queue[ac],
Emmanuel Grumbach4cf677f2015-01-12 14:38:29 +0200498 iwl_mvm_ac_to_tx_fifo[ac],
499 wdg_timeout);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100500 break;
501 }
502
503 return 0;
504}
505
506void iwl_mvm_mac_ctxt_release(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
507{
508 int ac;
509
510 lockdep_assert_held(&mvm->mutex);
511
512 switch (vif->type) {
513 case NL80211_IFTYPE_P2P_DEVICE:
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200514 iwl_mvm_disable_txq(mvm, IWL_MVM_OFFCHANNEL_QUEUE, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100515 break;
516 case NL80211_IFTYPE_AP:
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200517 iwl_mvm_disable_txq(mvm, vif->cab_queue, 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100518 /* fall through */
519 default:
520 for (ac = 0; ac < IEEE80211_NUM_ACS; ac++)
Emmanuel Grumbach0294d9e2015-01-05 16:52:55 +0200521 iwl_mvm_disable_txq(mvm, vif->hw_queue[ac], 0);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100522 }
523}
524
525static void iwl_mvm_ack_rates(struct iwl_mvm *mvm,
526 struct ieee80211_vif *vif,
527 enum ieee80211_band band,
528 u8 *cck_rates, u8 *ofdm_rates)
529{
530 struct ieee80211_supported_band *sband;
531 unsigned long basic = vif->bss_conf.basic_rates;
532 int lowest_present_ofdm = 100;
533 int lowest_present_cck = 100;
534 u8 cck = 0;
535 u8 ofdm = 0;
536 int i;
537
538 sband = mvm->hw->wiphy->bands[band];
539
540 for_each_set_bit(i, &basic, BITS_PER_LONG) {
541 int hw = sband->bitrates[i].hw_value;
542 if (hw >= IWL_FIRST_OFDM_RATE) {
543 ofdm |= BIT(hw - IWL_FIRST_OFDM_RATE);
544 if (lowest_present_ofdm > hw)
545 lowest_present_ofdm = hw;
546 } else {
547 BUILD_BUG_ON(IWL_FIRST_CCK_RATE != 0);
548
549 cck |= BIT(hw);
550 if (lowest_present_cck > hw)
551 lowest_present_cck = hw;
552 }
553 }
554
555 /*
556 * Now we've got the basic rates as bitmaps in the ofdm and cck
557 * variables. This isn't sufficient though, as there might not
558 * be all the right rates in the bitmap. E.g. if the only basic
559 * rates are 5.5 Mbps and 11 Mbps, we still need to add 1 Mbps
560 * and 6 Mbps because the 802.11-2007 standard says in 9.6:
561 *
562 * [...] a STA responding to a received frame shall transmit
563 * its Control Response frame [...] at the highest rate in the
564 * BSSBasicRateSet parameter that is less than or equal to the
565 * rate of the immediately previous frame in the frame exchange
566 * sequence ([...]) and that is of the same modulation class
567 * ([...]) as the received frame. If no rate contained in the
568 * BSSBasicRateSet parameter meets these conditions, then the
569 * control frame sent in response to a received frame shall be
570 * transmitted at the highest mandatory rate of the PHY that is
571 * less than or equal to the rate of the received frame, and
572 * that is of the same modulation class as the received frame.
573 *
574 * As a consequence, we need to add all mandatory rates that are
575 * lower than all of the basic rates to these bitmaps.
576 */
577
578 if (IWL_RATE_24M_INDEX < lowest_present_ofdm)
579 ofdm |= IWL_RATE_BIT_MSK(24) >> IWL_FIRST_OFDM_RATE;
580 if (IWL_RATE_12M_INDEX < lowest_present_ofdm)
581 ofdm |= IWL_RATE_BIT_MSK(12) >> IWL_FIRST_OFDM_RATE;
582 /* 6M already there or needed so always add */
583 ofdm |= IWL_RATE_BIT_MSK(6) >> IWL_FIRST_OFDM_RATE;
584
585 /*
586 * CCK is a bit more complex with DSSS vs. HR/DSSS vs. ERP.
587 * Note, however:
588 * - if no CCK rates are basic, it must be ERP since there must
589 * be some basic rates at all, so they're OFDM => ERP PHY
590 * (or we're in 5 GHz, and the cck bitmap will never be used)
591 * - if 11M is a basic rate, it must be ERP as well, so add 5.5M
592 * - if 5.5M is basic, 1M and 2M are mandatory
593 * - if 2M is basic, 1M is mandatory
594 * - if 1M is basic, that's the only valid ACK rate.
595 * As a consequence, it's not as complicated as it sounds, just add
596 * any lower rates to the ACK rate bitmap.
597 */
598 if (IWL_RATE_11M_INDEX < lowest_present_cck)
599 cck |= IWL_RATE_BIT_MSK(11) >> IWL_FIRST_CCK_RATE;
600 if (IWL_RATE_5M_INDEX < lowest_present_cck)
601 cck |= IWL_RATE_BIT_MSK(5) >> IWL_FIRST_CCK_RATE;
602 if (IWL_RATE_2M_INDEX < lowest_present_cck)
603 cck |= IWL_RATE_BIT_MSK(2) >> IWL_FIRST_CCK_RATE;
604 /* 1M already there or needed so always add */
605 cck |= IWL_RATE_BIT_MSK(1) >> IWL_FIRST_CCK_RATE;
606
607 *cck_rates = cck;
608 *ofdm_rates = ofdm;
609}
610
Avri Altman8a5e3662013-11-12 19:16:03 +0200611static void iwl_mvm_mac_ctxt_set_ht_flags(struct iwl_mvm *mvm,
612 struct ieee80211_vif *vif,
613 struct iwl_mac_ctx_cmd *cmd)
614{
615 /* for both sta and ap, ht_operation_mode hold the protection_mode */
616 u8 protection_mode = vif->bss_conf.ht_operation_mode &
617 IEEE80211_HT_OP_MODE_PROTECTION;
618 /* The fw does not distinguish between ht and fat */
619 u32 ht_flag = MAC_PROT_FLG_HT_PROT | MAC_PROT_FLG_FAT_PROT;
620
621 IWL_DEBUG_RATE(mvm, "protection mode set to %d\n", protection_mode);
622 /*
623 * See section 9.23.3.1 of IEEE 80211-2012.
624 * Nongreenfield HT STAs Present is not supported.
625 */
626 switch (protection_mode) {
627 case IEEE80211_HT_OP_MODE_PROTECTION_NONE:
628 break;
629 case IEEE80211_HT_OP_MODE_PROTECTION_NONMEMBER:
630 case IEEE80211_HT_OP_MODE_PROTECTION_NONHT_MIXED:
631 cmd->protection_flags |= cpu_to_le32(ht_flag);
632 break;
633 case IEEE80211_HT_OP_MODE_PROTECTION_20MHZ:
634 /* Protect when channel wider than 20MHz */
635 if (vif->bss_conf.chandef.width > NL80211_CHAN_WIDTH_20)
636 cmd->protection_flags |= cpu_to_le32(ht_flag);
637 break;
638 default:
639 IWL_ERR(mvm, "Illegal protection mode %d\n",
640 protection_mode);
641 break;
642 }
643}
644
Johannes Berg8ca151b2013-01-24 14:25:36 +0100645static void iwl_mvm_mac_ctxt_cmd_common(struct iwl_mvm *mvm,
646 struct ieee80211_vif *vif,
647 struct iwl_mac_ctx_cmd *cmd,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200648 const u8 *bssid_override,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100649 u32 action)
650{
651 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
652 struct ieee80211_chanctx_conf *chanctx;
Avri Altman8a5e3662013-11-12 19:16:03 +0200653 bool ht_enabled = !!(vif->bss_conf.ht_operation_mode &
654 IEEE80211_HT_OP_MODE_PROTECTION);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100655 u8 cck_ack_rates, ofdm_ack_rates;
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200656 const u8 *bssid = bssid_override ?: vif->bss_conf.bssid;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100657 int i;
658
659 cmd->id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
660 mvmvif->color));
661 cmd->action = cpu_to_le32(action);
662
663 switch (vif->type) {
664 case NL80211_IFTYPE_STATION:
665 if (vif->p2p)
666 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_STA);
667 else
668 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_BSS_STA);
669 break;
670 case NL80211_IFTYPE_AP:
671 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_GO);
672 break;
673 case NL80211_IFTYPE_MONITOR:
674 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_LISTENER);
675 break;
676 case NL80211_IFTYPE_P2P_DEVICE:
677 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_P2P_DEVICE);
678 break;
679 case NL80211_IFTYPE_ADHOC:
680 cmd->mac_type = cpu_to_le32(FW_MAC_TYPE_IBSS);
681 break;
682 default:
683 WARN_ON_ONCE(1);
684 }
685
686 cmd->tsf_id = cpu_to_le32(mvmvif->tsf_id);
687
688 memcpy(cmd->node_addr, vif->addr, ETH_ALEN);
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200689
690 if (bssid)
691 memcpy(cmd->bssid_addr, bssid, ETH_ALEN);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100692 else
693 eth_broadcast_addr(cmd->bssid_addr);
694
695 rcu_read_lock();
696 chanctx = rcu_dereference(vif->chanctx_conf);
697 iwl_mvm_ack_rates(mvm, vif, chanctx ? chanctx->def.chan->band
698 : IEEE80211_BAND_2GHZ,
699 &cck_ack_rates, &ofdm_ack_rates);
700 rcu_read_unlock();
701
702 cmd->cck_rates = cpu_to_le32((u32)cck_ack_rates);
703 cmd->ofdm_rates = cpu_to_le32((u32)ofdm_ack_rates);
704
705 cmd->cck_short_preamble =
706 cpu_to_le32(vif->bss_conf.use_short_preamble ?
707 MAC_FLG_SHORT_PREAMBLE : 0);
708 cmd->short_slot =
709 cpu_to_le32(vif->bss_conf.use_short_slot ?
710 MAC_FLG_SHORT_SLOT : 0);
711
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200712 for (i = 0; i < IEEE80211_NUM_ACS; i++) {
713 u8 txf = iwl_mvm_ac_to_tx_fifo[i];
714
715 cmd->ac[txf].cw_min =
716 cpu_to_le16(mvmvif->queue_params[i].cw_min);
717 cmd->ac[txf].cw_max =
718 cpu_to_le16(mvmvif->queue_params[i].cw_max);
719 cmd->ac[txf].edca_txop =
Johannes Berg8ca151b2013-01-24 14:25:36 +0100720 cpu_to_le16(mvmvif->queue_params[i].txop * 32);
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200721 cmd->ac[txf].aifsn = mvmvif->queue_params[i].aifs;
722 cmd->ac[txf].fifos_mask = BIT(txf);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100723 }
724
Emmanuel Grumbach86a91ec2013-05-26 20:47:53 +0300725 /* in AP mode, the MCAST FIFO takes the EDCA params from VO */
726 if (vif->type == NL80211_IFTYPE_AP)
Emmanuel Grumbach3dd94792013-12-19 16:50:49 +0200727 cmd->ac[IWL_MVM_TX_FIFO_VO].fifos_mask |=
728 BIT(IWL_MVM_TX_FIFO_MCAST);
Emmanuel Grumbach86a91ec2013-05-26 20:47:53 +0300729
Johannes Berg8ca151b2013-01-24 14:25:36 +0100730 if (vif->bss_conf.qos)
731 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_UPDATE_EDCA);
732
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300733 if (vif->bss_conf.use_cts_prot)
Dor Shaishcc7ee2b2013-02-18 13:51:36 +0200734 cmd->protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Emmanuel Grumbachdc271ee2014-07-03 20:46:35 +0300735
Avri Altman8a5e3662013-11-12 19:16:03 +0200736 IWL_DEBUG_RATE(mvm, "use_cts_prot %d, ht_operation_mode %d\n",
737 vif->bss_conf.use_cts_prot,
738 vif->bss_conf.ht_operation_mode);
739 if (vif->bss_conf.chandef.width != NL80211_CHAN_WIDTH_20_NOHT)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100740 cmd->qos_flags |= cpu_to_le32(MAC_QOS_FLG_TGN);
Avri Altman8a5e3662013-11-12 19:16:03 +0200741 if (ht_enabled)
742 iwl_mvm_mac_ctxt_set_ht_flags(mvm, vif, cmd);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100743
744 cmd->filter_flags = cpu_to_le32(MAC_FILTER_ACCEPT_GRP);
745}
746
747static int iwl_mvm_mac_ctxt_send_cmd(struct iwl_mvm *mvm,
748 struct iwl_mac_ctx_cmd *cmd)
749{
Emmanuel Grumbacha1022922014-05-12 11:36:41 +0300750 int ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +0100751 sizeof(*cmd), cmd);
752 if (ret)
753 IWL_ERR(mvm, "Failed to send MAC context (action:%d): %d\n",
754 le32_to_cpu(cmd->action), ret);
755 return ret;
756}
757
Luciano Coelhocf520232014-05-13 16:52:54 +0300758static int iwl_mvm_mac_ctxt_cmd_sta(struct iwl_mvm *mvm,
759 struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200760 u32 action, bool force_assoc_off,
761 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100762{
Luciano Coelhocf520232014-05-13 16:52:54 +0300763 struct iwl_mac_ctx_cmd cmd = {};
764 struct iwl_mac_data_sta *ctxt_sta;
765
766 WARN_ON(vif->type != NL80211_IFTYPE_STATION);
767
768 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200769 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, bssid_override, action);
Luciano Coelhocf520232014-05-13 16:52:54 +0300770
771 if (vif->p2p) {
772 struct ieee80211_p2p_noa_attr *noa =
773 &vif->bss_conf.p2p_noa_attr;
774
775 cmd.p2p_sta.ctwin = cpu_to_le32(noa->oppps_ctwindow &
776 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
777 ctxt_sta = &cmd.p2p_sta.sta;
778 } else {
Luciano Coelhocf520232014-05-13 16:52:54 +0300779 ctxt_sta = &cmd.sta;
780 }
781
Johannes Berg210a5442013-01-24 23:48:23 +0100782 /* We need the dtim_period to set the MAC as associated */
Alexander Bondarba283922013-05-02 16:34:48 +0300783 if (vif->bss_conf.assoc && vif->bss_conf.dtim_period &&
784 !force_assoc_off) {
Johannes Bergd2931bb2013-02-05 18:10:04 +0100785 u32 dtim_offs;
786
787 /*
788 * The DTIM count counts down, so when it is N that means N
789 * more beacon intervals happen until the DTIM TBTT. Therefore
790 * add this to the current time. If that ends up being in the
791 * future, the firmware will handle it.
792 *
793 * Also note that the system_timestamp (which we get here as
794 * "sync_device_ts") and TSF timestamp aren't at exactly the
795 * same offset in the frame -- the TSF is at the first symbol
796 * of the TSF, the system timestamp is at signal acquisition
797 * time. This means there's an offset between them of at most
798 * a few hundred microseconds (24 * 8 bits + PLCP time gives
799 * 384us in the longest case), this is currently not relevant
800 * as the firmware wakes up around 2ms before the TBTT.
801 */
802 dtim_offs = vif->bss_conf.sync_dtim_count *
803 vif->bss_conf.beacon_int;
804 /* convert TU to usecs */
805 dtim_offs *= 1024;
806
807 ctxt_sta->dtim_tsf =
808 cpu_to_le64(vif->bss_conf.sync_tsf + dtim_offs);
809 ctxt_sta->dtim_time =
810 cpu_to_le32(vif->bss_conf.sync_device_ts + dtim_offs);
811
812 IWL_DEBUG_INFO(mvm, "DTIM TBTT is 0x%llx/0x%x, offset %d\n",
813 le64_to_cpu(ctxt_sta->dtim_tsf),
814 le32_to_cpu(ctxt_sta->dtim_time),
815 dtim_offs);
816
Johannes Berg210a5442013-01-24 23:48:23 +0100817 ctxt_sta->is_assoc = cpu_to_le32(1);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100818 } else {
Johannes Berg210a5442013-01-24 23:48:23 +0100819 ctxt_sta->is_assoc = cpu_to_le32(0);
Luciano Coelho7c8b3bc2014-07-30 15:04:08 +0300820
821 /* Allow beacons to pass through as long as we are not
822 * associated, or we do not have dtim period information.
823 */
824 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
Johannes Bergd2931bb2013-02-05 18:10:04 +0100825 }
Johannes Berg8ca151b2013-01-24 14:25:36 +0100826
827 ctxt_sta->bi = cpu_to_le32(vif->bss_conf.beacon_int);
828 ctxt_sta->bi_reciprocal =
829 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
830 ctxt_sta->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
831 vif->bss_conf.dtim_period);
832 ctxt_sta->dtim_reciprocal =
833 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
834 vif->bss_conf.dtim_period));
835
836 ctxt_sta->listen_interval = cpu_to_le32(mvm->hw->conf.listen_interval);
837 ctxt_sta->assoc_id = cpu_to_le32(vif->bss_conf.aid);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100838
839 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
840}
841
842static int iwl_mvm_mac_ctxt_cmd_listener(struct iwl_mvm *mvm,
843 struct ieee80211_vif *vif,
844 u32 action)
845{
846 struct iwl_mac_ctx_cmd cmd = {};
847
848 WARN_ON(vif->type != NL80211_IFTYPE_MONITOR);
849
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200850 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg53585492013-03-18 13:04:50 +0100851
852 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROMISC |
853 MAC_FILTER_IN_CONTROL_AND_MGMT |
854 MAC_FILTER_IN_BEACON |
Johannes Bergfb8b8ee2013-10-21 12:37:53 +0200855 MAC_FILTER_IN_PROBE_REQUEST |
856 MAC_FILTER_IN_CRC32);
Johannes Berg30686bf2015-06-02 21:39:54 +0200857 ieee80211_hw_set(mvm->hw, RX_INCLUDES_FCS);
Johannes Berg53585492013-03-18 13:04:50 +0100858
Johannes Berg8ca151b2013-01-24 14:25:36 +0100859 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
860}
861
Johannes Berg5023d962013-07-31 14:07:43 +0200862static int iwl_mvm_mac_ctxt_cmd_ibss(struct iwl_mvm *mvm,
863 struct ieee80211_vif *vif,
864 u32 action)
865{
866 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
867 struct iwl_mac_ctx_cmd cmd = {};
868
869 WARN_ON(vif->type != NL80211_IFTYPE_ADHOC);
870
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200871 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg5023d962013-07-31 14:07:43 +0200872
873 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_BEACON |
874 MAC_FILTER_IN_PROBE_REQUEST);
875
876 /* cmd.ibss.beacon_time/cmd.ibss.beacon_tsf are curently ignored */
877 cmd.ibss.bi = cpu_to_le32(vif->bss_conf.beacon_int);
878 cmd.ibss.bi_reciprocal =
879 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
880
881 /* TODO: Assumes that the beacon id == mac context id */
882 cmd.ibss.beacon_template = cpu_to_le32(mvmvif->id);
883
884 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
885}
886
Johannes Berg8ca151b2013-01-24 14:25:36 +0100887struct iwl_mvm_go_iterator_data {
888 bool go_active;
889};
890
891static void iwl_mvm_go_iterator(void *_data, u8 *mac, struct ieee80211_vif *vif)
892{
893 struct iwl_mvm_go_iterator_data *data = _data;
894 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
895
Johannes Berg5023d962013-07-31 14:07:43 +0200896 if (vif->type == NL80211_IFTYPE_AP && vif->p2p &&
897 mvmvif->ap_ibss_active)
Johannes Berg8ca151b2013-01-24 14:25:36 +0100898 data->go_active = true;
899}
900
901static int iwl_mvm_mac_ctxt_cmd_p2p_device(struct iwl_mvm *mvm,
902 struct ieee80211_vif *vif,
903 u32 action)
904{
905 struct iwl_mac_ctx_cmd cmd = {};
906 struct iwl_mvm_go_iterator_data data = {};
907
908 WARN_ON(vif->type != NL80211_IFTYPE_P2P_DEVICE);
909
Johannes Berg3dfd3a92014-08-11 21:37:30 +0200910 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100911
912 cmd.protection_flags |= cpu_to_le32(MAC_PROT_FLG_TGG_PROTECT);
Ilan Peer1dcd15e2013-02-14 14:25:24 +0200913
914 /* Override the filter flags to accept only probe requests */
915 cmd.filter_flags = cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100916
917 /*
918 * This flag should be set to true when the P2P Device is
919 * discoverable and there is at least another active P2P GO. Settings
920 * this flag will allow the P2P Device to be discoverable on other
921 * channels in addition to its listen channel.
922 * Note that this flag should not be set in other cases as it opens the
923 * Rx filters on all MAC and increases the number of interrupts.
924 */
925 ieee80211_iterate_active_interfaces_atomic(
926 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
927 iwl_mvm_go_iterator, &data);
928
929 cmd.p2p_dev.is_disc_extended = cpu_to_le32(data.go_active ? 1 : 0);
930 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
931}
932
933static void iwl_mvm_mac_ctxt_set_tim(struct iwl_mvm *mvm,
934 struct iwl_mac_beacon_cmd *beacon_cmd,
935 u8 *beacon, u32 frame_size)
936{
937 u32 tim_idx;
938 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)beacon;
939
940 /* The index is relative to frame start but we start looking at the
941 * variable-length part of the beacon. */
942 tim_idx = mgmt->u.beacon.variable - beacon;
943
944 /* Parse variable-length elements of beacon to find WLAN_EID_TIM */
945 while ((tim_idx < (frame_size - 2)) &&
946 (beacon[tim_idx] != WLAN_EID_TIM))
947 tim_idx += beacon[tim_idx+1] + 2;
948
949 /* If TIM field was found, set variables */
950 if ((tim_idx < (frame_size - 1)) && (beacon[tim_idx] == WLAN_EID_TIM)) {
951 beacon_cmd->tim_idx = cpu_to_le32(tim_idx);
952 beacon_cmd->tim_size = cpu_to_le32((u32)beacon[tim_idx+1]);
953 } else {
954 IWL_WARN(mvm, "Unable to find TIM Element in beacon\n");
955 }
956}
957
958static int iwl_mvm_mac_ctxt_send_beacon(struct iwl_mvm *mvm,
959 struct ieee80211_vif *vif,
960 struct sk_buff *beacon)
961{
962 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
963 struct iwl_host_cmd cmd = {
964 .id = BEACON_TEMPLATE_CMD,
965 .flags = CMD_ASYNC,
966 };
967 struct iwl_mac_beacon_cmd beacon_cmd = {};
968 struct ieee80211_tx_info *info;
969 u32 beacon_skb_len;
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300970 u32 rate, tx_flags;
Johannes Berg8ca151b2013-01-24 14:25:36 +0100971
972 if (WARN_ON(!beacon))
973 return -EINVAL;
974
975 beacon_skb_len = beacon->len;
976
977 /* TODO: for now the beacon template id is set to be the mac context id.
978 * Might be better to handle it as another resource ... */
979 beacon_cmd.template_id = cpu_to_le32((u32)mvmvif->id);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300980 info = IEEE80211_SKB_CB(beacon);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100981
982 /* Set up TX command fields */
983 beacon_cmd.tx.len = cpu_to_le16((u16)beacon_skb_len);
984 beacon_cmd.tx.sta_id = mvmvif->bcast_sta.sta_id;
985 beacon_cmd.tx.life_time = cpu_to_le32(TX_CMD_LIFE_TIME_INFINITE);
Emmanuel Grumbach75f6b9b2014-06-02 10:03:33 +0300986 tx_flags = TX_CMD_FLG_SEQ_CTL | TX_CMD_FLG_TSF;
987 tx_flags |=
988 iwl_mvm_bt_coex_tx_prio(mvm, (void *)beacon->data, info, 0) <<
989 TX_CMD_FLG_BT_PRIO_POS;
990 beacon_cmd.tx.tx_flags = cpu_to_le32(tx_flags);
Johannes Berg8ca151b2013-01-24 14:25:36 +0100991
992 mvm->mgmt_last_antenna_idx =
Moshe Harela0544272014-12-08 21:13:14 +0200993 iwl_mvm_next_antenna(mvm, iwl_mvm_get_valid_tx_ant(mvm),
Johannes Berg8ca151b2013-01-24 14:25:36 +0100994 mvm->mgmt_last_antenna_idx);
995
996 beacon_cmd.tx.rate_n_flags =
997 cpu_to_le32(BIT(mvm->mgmt_last_antenna_idx) <<
998 RATE_MCS_ANT_POS);
999
Johannes Berg8ca151b2013-01-24 14:25:36 +01001000 if (info->band == IEEE80211_BAND_5GHZ || vif->p2p) {
1001 rate = IWL_FIRST_OFDM_RATE;
1002 } else {
1003 rate = IWL_FIRST_CCK_RATE;
1004 beacon_cmd.tx.rate_n_flags |= cpu_to_le32(RATE_MCS_CCK_MSK);
1005 }
1006 beacon_cmd.tx.rate_n_flags |=
1007 cpu_to_le32(iwl_mvm_mac80211_idx_to_hwrate(rate));
1008
1009 /* Set up TX beacon command fields */
Johannes Berg5023d962013-07-31 14:07:43 +02001010 if (vif->type == NL80211_IFTYPE_AP)
1011 iwl_mvm_mac_ctxt_set_tim(mvm, &beacon_cmd,
1012 beacon->data,
1013 beacon_skb_len);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001014
1015 /* Submit command */
1016 cmd.len[0] = sizeof(beacon_cmd);
1017 cmd.data[0] = &beacon_cmd;
1018 cmd.dataflags[0] = 0;
1019 cmd.len[1] = beacon_skb_len;
1020 cmd.data[1] = beacon->data;
1021 cmd.dataflags[1] = IWL_HCMD_DFL_DUP;
1022
1023 return iwl_mvm_send_cmd(mvm, &cmd);
1024}
1025
Johannes Berg5023d962013-07-31 14:07:43 +02001026/* The beacon template for the AP/GO/IBSS has changed and needs update */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001027int iwl_mvm_mac_ctxt_beacon_changed(struct iwl_mvm *mvm,
1028 struct ieee80211_vif *vif)
1029{
1030 struct sk_buff *beacon;
1031 int ret;
1032
Johannes Berg5023d962013-07-31 14:07:43 +02001033 WARN_ON(vif->type != NL80211_IFTYPE_AP &&
1034 vif->type != NL80211_IFTYPE_ADHOC);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001035
Andrei Otcheretianskife887662014-04-10 21:46:16 +03001036 beacon = ieee80211_beacon_get_template(mvm->hw, vif, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001037 if (!beacon)
1038 return -ENOMEM;
1039
1040 ret = iwl_mvm_mac_ctxt_send_beacon(mvm, vif, beacon);
1041 dev_kfree_skb(beacon);
1042 return ret;
1043}
1044
Ilan Peer3a3cb922013-06-13 08:30:12 +03001045struct iwl_mvm_mac_ap_iterator_data {
1046 struct iwl_mvm *mvm;
1047 struct ieee80211_vif *vif;
1048 u32 beacon_device_ts;
1049 u16 beacon_int;
1050};
1051
1052/* Find the beacon_device_ts and beacon_int for a managed interface */
1053static void iwl_mvm_mac_ap_iterator(void *_data, u8 *mac,
1054 struct ieee80211_vif *vif)
1055{
1056 struct iwl_mvm_mac_ap_iterator_data *data = _data;
1057
1058 if (vif->type != NL80211_IFTYPE_STATION || !vif->bss_conf.assoc)
1059 return;
1060
1061 /* Station client has higher priority over P2P client*/
1062 if (vif->p2p && data->beacon_device_ts)
1063 return;
1064
1065 data->beacon_device_ts = vif->bss_conf.sync_device_ts;
1066 data->beacon_int = vif->bss_conf.beacon_int;
1067}
1068
Johannes Berg8ca151b2013-01-24 14:25:36 +01001069/*
1070 * Fill the specific data for mac context of type AP of P2P GO
1071 */
1072static void iwl_mvm_mac_ctxt_cmd_fill_ap(struct iwl_mvm *mvm,
1073 struct ieee80211_vif *vif,
Johannes Berg506a81e2013-02-28 14:05:14 +01001074 struct iwl_mac_data_ap *ctxt_ap,
1075 bool add)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001076{
1077 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Ilan Peer3a3cb922013-06-13 08:30:12 +03001078 struct iwl_mvm_mac_ap_iterator_data data = {
1079 .mvm = mvm,
1080 .vif = vif,
1081 .beacon_device_ts = 0
1082 };
Johannes Berg8ca151b2013-01-24 14:25:36 +01001083
1084 ctxt_ap->bi = cpu_to_le32(vif->bss_conf.beacon_int);
1085 ctxt_ap->bi_reciprocal =
1086 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int));
1087 ctxt_ap->dtim_interval = cpu_to_le32(vif->bss_conf.beacon_int *
1088 vif->bss_conf.dtim_period);
1089 ctxt_ap->dtim_reciprocal =
1090 cpu_to_le32(iwl_mvm_reciprocal(vif->bss_conf.beacon_int *
1091 vif->bss_conf.dtim_period));
1092
1093 ctxt_ap->mcast_qid = cpu_to_le32(vif->cab_queue);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001094
Johannes Berg506a81e2013-02-28 14:05:14 +01001095 /*
Ilan Peer3a3cb922013-06-13 08:30:12 +03001096 * Only set the beacon time when the MAC is being added, when we
Johannes Berg506a81e2013-02-28 14:05:14 +01001097 * just modify the MAC then we should keep the time -- the firmware
1098 * can otherwise have a "jumping" TBTT.
1099 */
Ilan Peer3a3cb922013-06-13 08:30:12 +03001100 if (add) {
1101 /*
1102 * If there is a station/P2P client interface which is
1103 * associated, set the AP's TBTT far enough from the station's
1104 * TBTT. Otherwise, set it to the current system time
1105 */
1106 ieee80211_iterate_active_interfaces_atomic(
1107 mvm->hw, IEEE80211_IFACE_ITER_RESUME_ALL,
1108 iwl_mvm_mac_ap_iterator, &data);
1109
1110 if (data.beacon_device_ts) {
Ilan Peer9f8f8ca2013-12-24 17:17:05 +02001111 u32 rand = (prandom_u32() % (64 - 36)) + 36;
Ilan Peer3a3cb922013-06-13 08:30:12 +03001112 mvmvif->ap_beacon_time = data.beacon_device_ts +
1113 ieee80211_tu_to_usec(data.beacon_int * rand /
1114 100);
1115 } else {
1116 mvmvif->ap_beacon_time =
1117 iwl_read_prph(mvm->trans,
1118 DEVICE_SYSTEM_TIME_REG);
1119 }
1120 }
Johannes Berg506a81e2013-02-28 14:05:14 +01001121
1122 ctxt_ap->beacon_time = cpu_to_le32(mvmvif->ap_beacon_time);
Johannes Berg506a81e2013-02-28 14:05:14 +01001123 ctxt_ap->beacon_tsf = 0; /* unused */
Johannes Berg8ca151b2013-01-24 14:25:36 +01001124
1125 /* TODO: Assume that the beacon id == mac context id */
1126 ctxt_ap->beacon_template = cpu_to_le32(mvmvif->id);
1127}
1128
Ilan Peerf82c8332015-09-10 12:54:38 +03001129static int iwl_mvm_mac_ctxt_cmd_ap(struct iwl_mvm *mvm,
1130 struct ieee80211_vif *vif,
1131 u32 action)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001132{
Gregory Greenman94939082015-08-24 14:38:35 +03001133 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001134 struct iwl_mac_ctx_cmd cmd = {};
1135
1136 WARN_ON(vif->type != NL80211_IFTYPE_AP || vif->p2p);
1137
1138 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001139 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001140
Eliad Peller46018792014-07-16 13:24:36 +03001141 /*
1142 * pass probe requests and beacons from other APs (needed
Gregory Greenman94939082015-08-24 14:38:35 +03001143 * for ht protection); when there're no any associated station
1144 * don't ask FW to pass beacons to prevent unnecessary wake-ups.
Eliad Peller46018792014-07-16 13:24:36 +03001145 */
Gregory Greenman94939082015-08-24 14:38:35 +03001146 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST);
1147 if (mvmvif->ap_assoc_sta_count) {
1148 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_BEACON);
1149 IWL_DEBUG_HC(mvm, "Asking FW to pass beacons\n");
1150 } else {
1151 IWL_DEBUG_HC(mvm, "No need to receive beacons\n");
1152 }
Ilan Peer1dcd15e2013-02-14 14:25:24 +02001153
Johannes Berg8ca151b2013-01-24 14:25:36 +01001154 /* Fill the data specific for ap mode */
Johannes Berg506a81e2013-02-28 14:05:14 +01001155 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.ap,
1156 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001157
1158 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1159}
1160
1161static int iwl_mvm_mac_ctxt_cmd_go(struct iwl_mvm *mvm,
1162 struct ieee80211_vif *vif,
1163 u32 action)
1164{
1165 struct iwl_mac_ctx_cmd cmd = {};
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001166 struct ieee80211_p2p_noa_attr *noa = &vif->bss_conf.p2p_noa_attr;
Johannes Berg8ca151b2013-01-24 14:25:36 +01001167
1168 WARN_ON(vif->type != NL80211_IFTYPE_AP || !vif->p2p);
1169
1170 /* Fill the common data for all mac context types */
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001171 iwl_mvm_mac_ctxt_cmd_common(mvm, vif, &cmd, NULL, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001172
Eliad Peller46018792014-07-16 13:24:36 +03001173 /*
1174 * pass probe requests and beacons from other APs (needed
1175 * for ht protection)
1176 */
1177 cmd.filter_flags |= cpu_to_le32(MAC_FILTER_IN_PROBE_REQUEST |
1178 MAC_FILTER_IN_BEACON);
1179
Johannes Berg8ca151b2013-01-24 14:25:36 +01001180 /* Fill the data specific for GO mode */
Johannes Berg506a81e2013-02-28 14:05:14 +01001181 iwl_mvm_mac_ctxt_cmd_fill_ap(mvm, vif, &cmd.go.ap,
1182 action == FW_CTXT_ACTION_ADD);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001183
Janusz Dziedzic67baf662013-03-21 15:47:56 +01001184 cmd.go.ctwin = cpu_to_le32(noa->oppps_ctwindow &
1185 IEEE80211_P2P_OPPPS_CTWINDOW_MASK);
1186 cmd.go.opp_ps_enabled =
1187 cpu_to_le32(!!(noa->oppps_ctwindow &
1188 IEEE80211_P2P_OPPPS_ENABLE_BIT));
Johannes Berg8ca151b2013-01-24 14:25:36 +01001189
1190 return iwl_mvm_mac_ctxt_send_cmd(mvm, &cmd);
1191}
1192
1193static int iwl_mvm_mac_ctx_send(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001194 u32 action, bool force_assoc_off,
1195 const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001196{
1197 switch (vif->type) {
1198 case NL80211_IFTYPE_STATION:
Luciano Coelhocf520232014-05-13 16:52:54 +03001199 return iwl_mvm_mac_ctxt_cmd_sta(mvm, vif, action,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001200 force_assoc_off,
1201 bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001202 break;
1203 case NL80211_IFTYPE_AP:
1204 if (!vif->p2p)
1205 return iwl_mvm_mac_ctxt_cmd_ap(mvm, vif, action);
1206 else
1207 return iwl_mvm_mac_ctxt_cmd_go(mvm, vif, action);
1208 break;
1209 case NL80211_IFTYPE_MONITOR:
1210 return iwl_mvm_mac_ctxt_cmd_listener(mvm, vif, action);
1211 case NL80211_IFTYPE_P2P_DEVICE:
1212 return iwl_mvm_mac_ctxt_cmd_p2p_device(mvm, vif, action);
Johannes Berg5023d962013-07-31 14:07:43 +02001213 case NL80211_IFTYPE_ADHOC:
1214 return iwl_mvm_mac_ctxt_cmd_ibss(mvm, vif, action);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001215 default:
1216 break;
1217 }
1218
1219 return -EOPNOTSUPP;
1220}
1221
1222int iwl_mvm_mac_ctxt_add(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1223{
1224 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1225 int ret;
1226
1227 if (WARN_ONCE(mvmvif->uploaded, "Adding active MAC %pM/%d\n",
1228 vif->addr, ieee80211_vif_type_p2p(vif)))
1229 return -EIO;
1230
Luciano Coelhobca49d92014-05-13 17:33:38 +03001231 ret = iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_ADD,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001232 true, NULL);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001233 if (ret)
1234 return ret;
1235
Johannes Berg6d9d32b2013-08-06 18:58:56 +02001236 /* will only do anything at resume from D3 time */
1237 iwl_mvm_set_last_nonqos_seq(mvm, vif);
1238
Johannes Berg8ca151b2013-01-24 14:25:36 +01001239 mvmvif->uploaded = true;
1240 return 0;
1241}
1242
Luciano Coelhobca49d92014-05-13 17:33:38 +03001243int iwl_mvm_mac_ctxt_changed(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001244 bool force_assoc_off, const u8 *bssid_override)
Johannes Berg8ca151b2013-01-24 14:25:36 +01001245{
1246 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1247
1248 if (WARN_ONCE(!mvmvif->uploaded, "Changing inactive MAC %pM/%d\n",
1249 vif->addr, ieee80211_vif_type_p2p(vif)))
1250 return -EIO;
1251
Luciano Coelhobca49d92014-05-13 17:33:38 +03001252 return iwl_mvm_mac_ctx_send(mvm, vif, FW_CTXT_ACTION_MODIFY,
Johannes Berg3dfd3a92014-08-11 21:37:30 +02001253 force_assoc_off, bssid_override);
Johannes Berg8ca151b2013-01-24 14:25:36 +01001254}
1255
1256int iwl_mvm_mac_ctxt_remove(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1257{
1258 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1259 struct iwl_mac_ctx_cmd cmd;
1260 int ret;
1261
1262 if (WARN_ONCE(!mvmvif->uploaded, "Removing inactive MAC %pM/%d\n",
1263 vif->addr, ieee80211_vif_type_p2p(vif)))
1264 return -EIO;
1265
1266 memset(&cmd, 0, sizeof(cmd));
1267
1268 cmd.id_and_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mvmvif->id,
1269 mvmvif->color));
1270 cmd.action = cpu_to_le32(FW_CTXT_ACTION_REMOVE);
1271
Emmanuel Grumbacha1022922014-05-12 11:36:41 +03001272 ret = iwl_mvm_send_cmd_pdu(mvm, MAC_CONTEXT_CMD, 0,
Johannes Berg8ca151b2013-01-24 14:25:36 +01001273 sizeof(cmd), &cmd);
1274 if (ret) {
1275 IWL_ERR(mvm, "Failed to remove MAC context: %d\n", ret);
1276 return ret;
1277 }
1278
1279 mvmvif->uploaded = false;
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001280
1281 if (vif->type == NL80211_IFTYPE_MONITOR)
Johannes Berg30686bf2015-06-02 21:39:54 +02001282 __clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, mvm->hw->flags);
Johannes Bergfb8b8ee2013-10-21 12:37:53 +02001283
Johannes Berg8ca151b2013-01-24 14:25:36 +01001284 return 0;
1285}
Ilan Peer571765c2013-03-05 15:26:03 +02001286
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001287static void iwl_mvm_csa_count_down(struct iwl_mvm *mvm,
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001288 struct ieee80211_vif *csa_vif, u32 gp2,
1289 bool tx_success)
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001290{
1291 struct iwl_mvm_vif *mvmvif =
1292 iwl_mvm_vif_from_mac80211(csa_vif);
1293
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001294 /* Don't start to countdown from a failed beacon */
1295 if (!tx_success && !mvmvif->csa_countdown)
1296 return;
1297
1298 mvmvif->csa_countdown = true;
1299
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001300 if (!ieee80211_csa_is_complete(csa_vif)) {
1301 int c = ieee80211_csa_update_counter(csa_vif);
1302
1303 iwl_mvm_mac_ctxt_beacon_changed(mvm, csa_vif);
1304 if (csa_vif->p2p &&
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001305 !iwl_mvm_te_scheduled(&mvmvif->time_event_data) && gp2 &&
1306 tx_success) {
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001307 u32 rel_time = (c + 1) *
1308 csa_vif->bss_conf.beacon_int -
Luciano Coelhof991e172014-08-26 16:14:10 +03001309 IWL_MVM_CHANNEL_SWITCH_TIME_GO;
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001310 u32 apply_time = gp2 + rel_time * 1024;
1311
Luciano Coelhof991e172014-08-26 16:14:10 +03001312 iwl_mvm_schedule_csa_period(mvm, csa_vif,
1313 IWL_MVM_CHANNEL_SWITCH_TIME_GO -
1314 IWL_MVM_CHANNEL_SWITCH_MARGIN,
1315 apply_time);
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001316 }
1317 } else if (!iwl_mvm_te_scheduled(&mvmvif->time_event_data)) {
1318 /* we don't have CSA NoA scheduled yet, switch now */
1319 ieee80211_csa_finish(csa_vif);
1320 RCU_INIT_POINTER(mvm->csa_vif, NULL);
1321 }
1322}
1323
Johannes Berg04168412015-06-23 21:22:09 +02001324void iwl_mvm_rx_beacon_notif(struct iwl_mvm *mvm,
1325 struct iwl_rx_cmd_buffer *rxb)
Ilan Peer571765c2013-03-05 15:26:03 +02001326{
1327 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001328 struct iwl_extended_beacon_notif *beacon = (void *)pkt->data;
David Spinadel1c87bba2014-02-27 16:41:52 +02001329 struct iwl_mvm_tx_resp *beacon_notify_hdr;
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001330 struct ieee80211_vif *csa_vif;
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001331 struct ieee80211_vif *tx_blocked_vif;
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001332 u16 status;
Ilan Peer571765c2013-03-05 15:26:03 +02001333
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001334 lockdep_assert_held(&mvm->mutex);
1335
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001336 beacon_notify_hdr = &beacon->beacon_notify_hdr;
1337 mvm->ap_last_beacon_gp2 = le32_to_cpu(beacon->gp2);
David Spinadel1c87bba2014-02-27 16:41:52 +02001338
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001339 status = le16_to_cpu(beacon_notify_hdr->status.status) & TX_STATUS_MSK;
David Spinadel1c87bba2014-02-27 16:41:52 +02001340 IWL_DEBUG_RX(mvm,
1341 "beacon status %#x retries:%d tsf:0x%16llX gp2:0x%X rate:%d\n",
Emmanuel Grumbach2d2c0e92014-11-24 08:51:46 +02001342 status, beacon_notify_hdr->failure_frame,
1343 le64_to_cpu(beacon->tsf),
David Spinadel1c87bba2014-02-27 16:41:52 +02001344 mvm->ap_last_beacon_gp2,
1345 le32_to_cpu(beacon_notify_hdr->initial_rate));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001346
Andrei Otcheretianski664322f2014-06-05 16:40:36 +03001347 csa_vif = rcu_dereference_protected(mvm->csa_vif,
1348 lockdep_is_held(&mvm->mutex));
Andrei Otcheretianski7f0a7c62014-05-04 11:48:12 +03001349 if (unlikely(csa_vif && csa_vif->csa_active))
Andrei Otcheretianski7ef0aab2014-11-10 11:10:11 +02001350 iwl_mvm_csa_count_down(mvm, csa_vif, mvm->ap_last_beacon_gp2,
1351 (status == TX_STATUS_SUCCESS));
Andrei Otcheretianskibd3398e2013-10-22 05:01:12 +02001352
Andrei Otcheretianski003e52362014-05-25 17:24:22 +03001353 tx_blocked_vif = rcu_dereference_protected(mvm->csa_tx_blocked_vif,
1354 lockdep_is_held(&mvm->mutex));
1355 if (unlikely(tx_blocked_vif)) {
1356 struct iwl_mvm_vif *mvmvif =
1357 iwl_mvm_vif_from_mac80211(tx_blocked_vif);
1358
1359 /*
1360 * The channel switch is started and we have blocked the
1361 * stations. If this is the first beacon (the timeout wasn't
1362 * set), set the unblock timeout, otherwise countdown
1363 */
1364 if (!mvm->csa_tx_block_bcn_timeout)
1365 mvm->csa_tx_block_bcn_timeout =
1366 IWL_MVM_CS_UNBLOCK_TX_TIMEOUT;
1367 else
1368 mvm->csa_tx_block_bcn_timeout--;
1369
1370 /* Check if the timeout is expired, and unblock tx */
1371 if (mvm->csa_tx_block_bcn_timeout == 0) {
1372 iwl_mvm_modify_all_sta_disable_tx(mvm, mvmvif, false);
1373 RCU_INIT_POINTER(mvm->csa_tx_blocked_vif, NULL);
1374 }
1375 }
Ilan Peer571765c2013-03-05 15:26:03 +02001376}
Hila Gonend64048e2013-03-13 18:00:03 +02001377
1378static void iwl_mvm_beacon_loss_iterator(void *_data, u8 *mac,
1379 struct ieee80211_vif *vif)
1380{
Ilan Peer12d423e2013-12-24 22:08:14 +02001381 struct iwl_missed_beacons_notif *missed_beacons = _data;
Hila Gonend64048e2013-03-13 18:00:03 +02001382 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001383 struct iwl_mvm *mvm = mvmvif->mvm;
1384 struct iwl_fw_dbg_trigger_missed_bcon *bcon_trig;
1385 struct iwl_fw_dbg_trigger_tlv *trigger;
1386 u32 stop_trig_missed_bcon, stop_trig_missed_bcon_since_rx;
1387 u32 rx_missed_bcon, rx_missed_bcon_since_rx;
Hila Gonend64048e2013-03-13 18:00:03 +02001388
Ilan Peer12d423e2013-12-24 22:08:14 +02001389 if (mvmvif->id != (u16)le32_to_cpu(missed_beacons->mac_id))
1390 return;
1391
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001392 rx_missed_bcon = le32_to_cpu(missed_beacons->consec_missed_beacons);
1393 rx_missed_bcon_since_rx =
1394 le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx);
Ilan Peer12d423e2013-12-24 22:08:14 +02001395 /*
1396 * TODO: the threshold should be adjusted based on latency conditions,
1397 * and/or in case of a CS flow on one of the other AP vifs.
1398 */
1399 if (le32_to_cpu(missed_beacons->consec_missed_beacons_since_last_rx) >
1400 IWL_MVM_MISSED_BEACONS_THRESHOLD)
Hila Gonend64048e2013-03-13 18:00:03 +02001401 ieee80211_beacon_loss(vif);
Emmanuel Grumbach9d761fd2015-02-02 12:44:23 +02001402
1403 if (!iwl_fw_dbg_trigger_enabled(mvm->fw,
1404 FW_DBG_TRIGGER_MISSED_BEACONS))
1405 return;
1406
1407 trigger = iwl_fw_dbg_get_trigger(mvm->fw,
1408 FW_DBG_TRIGGER_MISSED_BEACONS);
1409 bcon_trig = (void *)trigger->data;
1410 stop_trig_missed_bcon = le32_to_cpu(bcon_trig->stop_consec_missed_bcon);
1411 stop_trig_missed_bcon_since_rx =
1412 le32_to_cpu(bcon_trig->stop_consec_missed_bcon_since_rx);
1413
1414 /* TODO: implement start trigger */
1415
1416 if (!iwl_fw_dbg_trigger_check_stop(mvm, vif, trigger))
1417 return;
1418
1419 if (rx_missed_bcon_since_rx >= stop_trig_missed_bcon_since_rx ||
1420 rx_missed_bcon >= stop_trig_missed_bcon)
Johannes Berg5d4f9292015-03-31 09:12:54 +02001421 iwl_mvm_fw_dbg_collect_trig(mvm, trigger, NULL);
Hila Gonend64048e2013-03-13 18:00:03 +02001422}
1423
Johannes Berg04168412015-06-23 21:22:09 +02001424void iwl_mvm_rx_missed_beacons_notif(struct iwl_mvm *mvm,
1425 struct iwl_rx_cmd_buffer *rxb)
Hila Gonend64048e2013-03-13 18:00:03 +02001426{
1427 struct iwl_rx_packet *pkt = rxb_addr(rxb);
Ilan Peer12d423e2013-12-24 22:08:14 +02001428 struct iwl_missed_beacons_notif *mb = (void *)pkt->data;
1429
1430 IWL_DEBUG_INFO(mvm,
1431 "missed bcn mac_id=%u, consecutive=%u (%u, %u, %u)\n",
1432 le32_to_cpu(mb->mac_id),
1433 le32_to_cpu(mb->consec_missed_beacons),
1434 le32_to_cpu(mb->consec_missed_beacons_since_last_rx),
1435 le32_to_cpu(mb->num_recvd_beacons),
1436 le32_to_cpu(mb->num_expected_beacons));
Hila Gonend64048e2013-03-13 18:00:03 +02001437
1438 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
1439 IEEE80211_IFACE_ITER_NORMAL,
1440 iwl_mvm_beacon_loss_iterator,
Ilan Peer12d423e2013-12-24 22:08:14 +02001441 mb);
Hila Gonend64048e2013-03-13 18:00:03 +02001442}