iwlwifi: mvm: fix the keyidx assignment
[linux-2.6-block.git] / drivers / net / wireless / iwlwifi / mvm / sta.c
CommitLineData
8ca151b5
JB
1/******************************************************************************
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 *
8 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110,
22 * USA
23 *
24 * The full GNU General Public License is included in this distribution
25 * in the file called LICENSE.GPL.
26 *
27 * Contact Information:
28 * Intel Linux Wireless <ilw@linux.intel.com>
29 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
30 *
31 * BSD LICENSE
32 *
33 * Copyright(c) 2012 - 2013 Intel Corporation. All rights reserved.
34 * All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 *
40 * * Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * * Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in
44 * the documentation and/or other materials provided with the
45 * distribution.
46 * * Neither the name Intel Corporation nor the names of its
47 * contributors may be used to endorse or promote products derived
48 * from this software without specific prior written permission.
49 *
50 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
51 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
52 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
53 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
54 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
55 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
56 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
57 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
58 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
59 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
60 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61 *
62 *****************************************************************************/
63#include <net/mac80211.h>
64
65#include "mvm.h"
66#include "sta.h"
67
68static int iwl_mvm_find_free_sta_id(struct iwl_mvm *mvm)
69{
70 int sta_id;
71
72 WARN_ON_ONCE(test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status));
73
74 lockdep_assert_held(&mvm->mutex);
75
76 /* Don't take rcu_read_lock() since we are protected by mvm->mutex */
77 for (sta_id = 0; sta_id < IWL_MVM_STATION_COUNT; sta_id++)
78 if (!rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
79 lockdep_is_held(&mvm->mutex)))
80 return sta_id;
81 return IWL_MVM_STATION_COUNT;
82}
83
84/* add a NEW station to fw */
85int iwl_mvm_sta_add_to_fw(struct iwl_mvm *mvm, struct ieee80211_sta *sta)
86{
87 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
88 struct iwl_mvm_add_sta_cmd add_sta_cmd;
89 int ret;
90 u32 status;
91 u32 agg_size = 0, mpdu_dens = 0;
92
93 memset(&add_sta_cmd, 0, sizeof(add_sta_cmd));
94
95 add_sta_cmd.sta_id = mvm_sta->sta_id;
96 add_sta_cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
97 add_sta_cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
98 memcpy(&add_sta_cmd.addr, sta->addr, ETH_ALEN);
99
100 /* STA_FLG_FAT_EN_MSK ? */
101 /* STA_FLG_MIMO_EN_MSK ? */
102
103 if (sta->ht_cap.ht_supported) {
104 add_sta_cmd.station_flags_msk |=
105 cpu_to_le32(STA_FLG_MAX_AGG_SIZE_MSK |
106 STA_FLG_AGG_MPDU_DENS_MSK);
107
108 mpdu_dens = sta->ht_cap.ampdu_density;
109 }
110
111 if (sta->vht_cap.vht_supported) {
112 agg_size = sta->vht_cap.cap &
113 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_MASK;
114 agg_size >>=
115 IEEE80211_VHT_CAP_MAX_A_MPDU_LENGTH_EXPONENT_SHIFT;
116 } else if (sta->ht_cap.ht_supported) {
117 agg_size = sta->ht_cap.ampdu_factor;
118 }
119
120 add_sta_cmd.station_flags |=
121 cpu_to_le32(agg_size << STA_FLG_MAX_AGG_SIZE_SHIFT);
122 add_sta_cmd.station_flags |=
123 cpu_to_le32(mpdu_dens << STA_FLG_AGG_MPDU_DENS_SHIFT);
124
125 status = ADD_STA_SUCCESS;
126 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(add_sta_cmd),
127 &add_sta_cmd, &status);
128 if (ret)
129 return ret;
130
131 switch (status) {
132 case ADD_STA_SUCCESS:
133 IWL_DEBUG_ASSOC(mvm, "ADD_STA PASSED\n");
134 break;
135 default:
136 ret = -EIO;
137 IWL_ERR(mvm, "ADD_STA failed\n");
138 break;
139 }
140
141 return ret;
142}
143
144int iwl_mvm_add_sta(struct iwl_mvm *mvm,
145 struct ieee80211_vif *vif,
146 struct ieee80211_sta *sta)
147{
148 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
149 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
150 int i, ret, sta_id;
151
152 lockdep_assert_held(&mvm->mutex);
153
154 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
155 sta_id = iwl_mvm_find_free_sta_id(mvm);
156 else
157 sta_id = mvm_sta->sta_id;
158
159 if (WARN_ON_ONCE(sta_id == IWL_MVM_STATION_COUNT))
160 return -ENOSPC;
161
162 spin_lock_init(&mvm_sta->lock);
163
164 mvm_sta->sta_id = sta_id;
165 mvm_sta->mac_id_n_color = FW_CMD_ID_AND_COLOR(mvmvif->id,
166 mvmvif->color);
167 mvm_sta->vif = vif;
168 mvm_sta->max_agg_bufsize = LINK_QUAL_AGG_FRAME_LIMIT_DEF;
169
170 /* HW restart, don't assume the memory has been zeroed */
171 atomic_set(&mvm_sta->pending_frames, 0);
172 mvm_sta->tid_disable_agg = 0;
173 mvm_sta->tfd_queue_msk = 0;
174 for (i = 0; i < IEEE80211_NUM_ACS; i++)
175 if (vif->hw_queue[i] != IEEE80211_INVAL_HW_QUEUE)
176 mvm_sta->tfd_queue_msk |= BIT(vif->hw_queue[i]);
177
178 if (vif->cab_queue != IEEE80211_INVAL_HW_QUEUE)
179 mvm_sta->tfd_queue_msk |= BIT(vif->cab_queue);
180
181 /* for HW restart - need to reset the seq_number etc... */
182 memset(mvm_sta->tid_data, 0, sizeof(mvm_sta->tid_data));
183
184 ret = iwl_mvm_sta_add_to_fw(mvm, sta);
185 if (ret)
186 return ret;
187
188 /* The first station added is the AP, the others are TDLS STAs */
189 if (vif->type == NL80211_IFTYPE_STATION &&
190 mvmvif->ap_sta_id == IWL_MVM_STATION_COUNT)
191 mvmvif->ap_sta_id = sta_id;
192
193 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], sta);
194
195 return 0;
196}
197
198int iwl_mvm_drain_sta(struct iwl_mvm *mvm, struct iwl_mvm_sta *mvmsta,
199 bool drain)
200{
201 struct iwl_mvm_add_sta_cmd cmd = {};
202 int ret;
203 u32 status;
204
205 lockdep_assert_held(&mvm->mutex);
206
207 cmd.mac_id_n_color = cpu_to_le32(mvmsta->mac_id_n_color);
208 cmd.sta_id = mvmsta->sta_id;
209 cmd.add_modify = STA_MODE_MODIFY;
210 cmd.station_flags = drain ? cpu_to_le32(STA_FLG_DRAIN_FLOW) : 0;
211 cmd.station_flags_msk = cpu_to_le32(STA_FLG_DRAIN_FLOW);
212
213 status = ADD_STA_SUCCESS;
214 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
215 &cmd, &status);
216 if (ret)
217 return ret;
218
219 switch (status) {
220 case ADD_STA_SUCCESS:
221 IWL_DEBUG_INFO(mvm, "Frames for staid %d will drained in fw\n",
222 mvmsta->sta_id);
223 break;
224 default:
225 ret = -EIO;
226 IWL_ERR(mvm, "Couldn't drain frames for staid %d\n",
227 mvmsta->sta_id);
228 break;
229 }
230
231 return ret;
232}
233
234/*
235 * Remove a station from the FW table. Before sending the command to remove
236 * the station validate that the station is indeed known to the driver (sanity
237 * only).
238 */
239static int iwl_mvm_rm_sta_common(struct iwl_mvm *mvm, u8 sta_id)
240{
241 struct ieee80211_sta *sta;
242 struct iwl_mvm_rm_sta_cmd rm_sta_cmd = {
243 .sta_id = sta_id,
244 };
245 int ret;
246
247 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
248 lockdep_is_held(&mvm->mutex));
249
250 /* Note: internal stations are marked as error values */
251 if (!sta) {
252 IWL_ERR(mvm, "Invalid station id\n");
253 return -EINVAL;
254 }
255
256 ret = iwl_mvm_send_cmd_pdu(mvm, REMOVE_STA, CMD_SYNC,
257 sizeof(rm_sta_cmd), &rm_sta_cmd);
258 if (ret) {
259 IWL_ERR(mvm, "Failed to remove station. Id=%d\n", sta_id);
260 return ret;
261 }
262
263 return 0;
264}
265
266void iwl_mvm_sta_drained_wk(struct work_struct *wk)
267{
268 struct iwl_mvm *mvm = container_of(wk, struct iwl_mvm, sta_drained_wk);
269 u8 sta_id;
270
271 /*
272 * The mutex is needed because of the SYNC cmd, but not only: if the
273 * work would run concurrently with iwl_mvm_rm_sta, it would run before
274 * iwl_mvm_rm_sta sets the station as busy, and exit. Then
275 * iwl_mvm_rm_sta would set the station as busy, and nobody will clean
276 * that later.
277 */
278 mutex_lock(&mvm->mutex);
279
280 for_each_set_bit(sta_id, mvm->sta_drained, IWL_MVM_STATION_COUNT) {
281 int ret;
282 struct ieee80211_sta *sta =
283 rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
284 lockdep_is_held(&mvm->mutex));
285
286 /* This station is in use */
287 if (!IS_ERR(sta))
288 continue;
289
290 if (PTR_ERR(sta) == -EINVAL) {
291 IWL_ERR(mvm, "Drained sta %d, but it is internal?\n",
292 sta_id);
293 continue;
294 }
295
296 if (!sta) {
297 IWL_ERR(mvm, "Drained sta %d, but it was NULL?\n",
298 sta_id);
299 continue;
300 }
301
302 WARN_ON(PTR_ERR(sta) != -EBUSY);
303 /* This station was removed and we waited until it got drained,
304 * we can now proceed and remove it.
305 */
306 ret = iwl_mvm_rm_sta_common(mvm, sta_id);
307 if (ret) {
308 IWL_ERR(mvm,
309 "Couldn't remove sta %d after it was drained\n",
310 sta_id);
311 continue;
312 }
313 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
314 clear_bit(sta_id, mvm->sta_drained);
315 }
316
317 mutex_unlock(&mvm->mutex);
318}
319
320int iwl_mvm_rm_sta(struct iwl_mvm *mvm,
321 struct ieee80211_vif *vif,
322 struct ieee80211_sta *sta)
323{
324 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
325 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
326 int ret;
327
328 lockdep_assert_held(&mvm->mutex);
329
330 if (vif->type == NL80211_IFTYPE_STATION &&
331 mvmvif->ap_sta_id == mvm_sta->sta_id) {
332 /*
333 * Put a non-NULL since the fw station isn't removed.
334 * It will be removed after the MAC will be set as
335 * unassoc.
336 */
337 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
338 ERR_PTR(-EINVAL));
339
340 /* flush its queues here since we are freeing mvm_sta */
341 ret = iwl_mvm_flush_tx_path(mvm, mvm_sta->tfd_queue_msk, true);
342
343 /* if we are associated - we can't remove the AP STA now */
344 if (vif->bss_conf.assoc)
345 return ret;
346
347 /* unassoc - go ahead - remove the AP STA now */
348 mvmvif->ap_sta_id = IWL_MVM_STATION_COUNT;
349 }
350
351 /*
352 * There are frames pending on the AC queues for this station.
353 * We need to wait until all the frames are drained...
354 */
355 if (atomic_read(&mvm_sta->pending_frames)) {
356 ret = iwl_mvm_drain_sta(mvm, mvm_sta, true);
357 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id],
358 ERR_PTR(-EBUSY));
359 } else {
360 ret = iwl_mvm_rm_sta_common(mvm, mvm_sta->sta_id);
361 rcu_assign_pointer(mvm->fw_id_to_mac_id[mvm_sta->sta_id], NULL);
362 }
363
364 return ret;
365}
366
367int iwl_mvm_rm_sta_id(struct iwl_mvm *mvm,
368 struct ieee80211_vif *vif,
369 u8 sta_id)
370{
371 int ret = iwl_mvm_rm_sta_common(mvm, sta_id);
372
373 lockdep_assert_held(&mvm->mutex);
374
375 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta_id], NULL);
376 return ret;
377}
378
379int iwl_mvm_allocate_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta,
380 u32 qmask)
381{
382 if (!test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status)) {
383 sta->sta_id = iwl_mvm_find_free_sta_id(mvm);
384 if (WARN_ON_ONCE(sta->sta_id == IWL_MVM_STATION_COUNT))
385 return -ENOSPC;
386 }
387
388 sta->tfd_queue_msk = qmask;
389
390 /* put a non-NULL value so iterating over the stations won't stop */
391 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], ERR_PTR(-EINVAL));
392 return 0;
393}
394
395void iwl_mvm_dealloc_int_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *sta)
396{
397 rcu_assign_pointer(mvm->fw_id_to_mac_id[sta->sta_id], NULL);
398 memset(sta, 0, sizeof(struct iwl_mvm_int_sta));
399 sta->sta_id = IWL_MVM_STATION_COUNT;
400}
401
402static int iwl_mvm_add_int_sta_common(struct iwl_mvm *mvm,
403 struct iwl_mvm_int_sta *sta,
404 const u8 *addr,
405 u16 mac_id, u16 color)
406{
407 struct iwl_mvm_add_sta_cmd cmd;
408 int ret;
409 u32 status;
410
411 lockdep_assert_held(&mvm->mutex);
412
413 memset(&cmd, 0, sizeof(struct iwl_mvm_add_sta_cmd));
414 cmd.sta_id = sta->sta_id;
415 cmd.mac_id_n_color = cpu_to_le32(FW_CMD_ID_AND_COLOR(mac_id,
416 color));
417
418 cmd.tfd_queue_msk = cpu_to_le32(sta->tfd_queue_msk);
419
420 if (addr)
421 memcpy(cmd.addr, addr, ETH_ALEN);
422
423 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
424 &cmd, &status);
425 if (ret)
426 return ret;
427
428 switch (status) {
429 case ADD_STA_SUCCESS:
430 IWL_DEBUG_INFO(mvm, "Internal station added.\n");
431 return 0;
432 default:
433 ret = -EIO;
434 IWL_ERR(mvm, "Add internal station failed, status=0x%x\n",
435 status);
436 break;
437 }
438 return ret;
439}
440
441int iwl_mvm_add_aux_sta(struct iwl_mvm *mvm)
442{
443 int ret;
444
445 lockdep_assert_held(&mvm->mutex);
446
447 /* Add the aux station, but without any queues */
448 ret = iwl_mvm_allocate_int_sta(mvm, &mvm->aux_sta, 0);
449 if (ret)
450 return ret;
451
452 ret = iwl_mvm_add_int_sta_common(mvm, &mvm->aux_sta, NULL,
453 MAC_INDEX_AUX, 0);
454
455 if (ret)
456 iwl_mvm_dealloc_int_sta(mvm, &mvm->aux_sta);
457 return ret;
458}
459
460/*
461 * Send the add station command for the vif's broadcast station.
462 * Assumes that the station was already allocated.
463 *
464 * @mvm: the mvm component
465 * @vif: the interface to which the broadcast station is added
466 * @bsta: the broadcast station to add.
467 */
468int iwl_mvm_send_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
469 struct iwl_mvm_int_sta *bsta)
470{
471 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
472 static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
473
474 lockdep_assert_held(&mvm->mutex);
475
476 if (WARN_ON_ONCE(bsta->sta_id == IWL_MVM_STATION_COUNT))
477 return -ENOSPC;
478
479 return iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
480 mvmvif->id, mvmvif->color);
481}
482
483/* Send the FW a request to remove the station from it's internal data
484 * structures, but DO NOT remove the entry from the local data structures. */
485int iwl_mvm_send_rm_bcast_sta(struct iwl_mvm *mvm,
486 struct iwl_mvm_int_sta *bsta)
487{
488 int ret;
489
490 lockdep_assert_held(&mvm->mutex);
491
492 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
493 if (ret)
494 IWL_WARN(mvm, "Failed sending remove station\n");
495 return ret;
496}
497
498/* Allocate a new station entry for the broadcast station to the given vif,
499 * and send it to the FW.
500 * Note that each P2P mac should have its own broadcast station.
501 *
502 * @mvm: the mvm component
503 * @vif: the interface to which the broadcast station is added
504 * @bsta: the broadcast station to add. */
505int iwl_mvm_add_bcast_sta(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
506 struct iwl_mvm_int_sta *bsta)
507{
508 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
509 static const u8 baddr[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
510 u32 qmask;
511 int ret;
512
513 lockdep_assert_held(&mvm->mutex);
514
515 qmask = iwl_mvm_mac_get_queues_mask(mvm, vif);
516 ret = iwl_mvm_allocate_int_sta(mvm, bsta, qmask);
517 if (ret)
518 return ret;
519
520 ret = iwl_mvm_add_int_sta_common(mvm, bsta, baddr,
521 mvmvif->id, mvmvif->color);
522
523 if (ret)
524 iwl_mvm_dealloc_int_sta(mvm, bsta);
525 return ret;
526}
527
528/*
529 * Send the FW a request to remove the station from it's internal data
530 * structures, and in addition remove it from the local data structure.
531 */
532int iwl_mvm_rm_bcast_sta(struct iwl_mvm *mvm, struct iwl_mvm_int_sta *bsta)
533{
534 int ret;
535
536 lockdep_assert_held(&mvm->mutex);
537
538 ret = iwl_mvm_rm_sta_common(mvm, bsta->sta_id);
539 if (ret)
540 return ret;
541
542 iwl_mvm_dealloc_int_sta(mvm, bsta);
543 return ret;
544}
545
546int iwl_mvm_sta_rx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
547 int tid, u16 ssn, bool start)
548{
549 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
550 struct iwl_mvm_add_sta_cmd cmd = {};
551 int ret;
552 u32 status;
553
554 lockdep_assert_held(&mvm->mutex);
555
556 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
557 cmd.sta_id = mvm_sta->sta_id;
558 cmd.add_modify = STA_MODE_MODIFY;
559 cmd.add_immediate_ba_tid = (u8) tid;
560 cmd.add_immediate_ba_ssn = cpu_to_le16(ssn);
561 cmd.modify_mask = start ? STA_MODIFY_ADD_BA_TID :
562 STA_MODIFY_REMOVE_BA_TID;
563
564 status = ADD_STA_SUCCESS;
565 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
566 &cmd, &status);
567 if (ret)
568 return ret;
569
570 switch (status) {
571 case ADD_STA_SUCCESS:
572 IWL_DEBUG_INFO(mvm, "RX BA Session %sed in fw\n",
573 start ? "start" : "stopp");
574 break;
575 case ADD_STA_IMMEDIATE_BA_FAILURE:
576 IWL_WARN(mvm, "RX BA Session refused by fw\n");
577 ret = -ENOSPC;
578 break;
579 default:
580 ret = -EIO;
581 IWL_ERR(mvm, "RX BA Session failed %sing, status 0x%x\n",
582 start ? "start" : "stopp", status);
583 break;
584 }
585
586 return ret;
587}
588
589static int iwl_mvm_sta_tx_agg(struct iwl_mvm *mvm, struct ieee80211_sta *sta,
590 int tid, u8 queue, bool start)
591{
592 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
593 struct iwl_mvm_add_sta_cmd cmd = {};
594 int ret;
595 u32 status;
596
597 lockdep_assert_held(&mvm->mutex);
598
599 if (start) {
600 mvm_sta->tfd_queue_msk |= BIT(queue);
601 mvm_sta->tid_disable_agg &= ~BIT(tid);
602 } else {
603 mvm_sta->tfd_queue_msk &= ~BIT(queue);
604 mvm_sta->tid_disable_agg |= BIT(tid);
605 }
606
607 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
608 cmd.sta_id = mvm_sta->sta_id;
609 cmd.add_modify = STA_MODE_MODIFY;
610 cmd.modify_mask = STA_MODIFY_QUEUES | STA_MODIFY_TID_DISABLE_TX;
611 cmd.tfd_queue_msk = cpu_to_le32(mvm_sta->tfd_queue_msk);
612 cmd.tid_disable_tx = cpu_to_le16(mvm_sta->tid_disable_agg);
613
614 status = ADD_STA_SUCCESS;
615 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
616 &cmd, &status);
617 if (ret)
618 return ret;
619
620 switch (status) {
621 case ADD_STA_SUCCESS:
622 break;
623 default:
624 ret = -EIO;
625 IWL_ERR(mvm, "TX BA Session failed %sing, status 0x%x\n",
626 start ? "start" : "stopp", status);
627 break;
628 }
629
630 return ret;
631}
632
633static const u8 tid_to_ac[] = {
634 IEEE80211_AC_BE,
635 IEEE80211_AC_BK,
636 IEEE80211_AC_BK,
637 IEEE80211_AC_BE,
638 IEEE80211_AC_VI,
639 IEEE80211_AC_VI,
640 IEEE80211_AC_VO,
641 IEEE80211_AC_VO,
642};
643
644int iwl_mvm_sta_tx_agg_start(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
645 struct ieee80211_sta *sta, u16 tid, u16 *ssn)
646{
647 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
648 struct iwl_mvm_tid_data *tid_data;
649 int txq_id;
650
651 if (WARN_ON_ONCE(tid >= IWL_MAX_TID_COUNT))
652 return -EINVAL;
653
654 if (mvmsta->tid_data[tid].state != IWL_AGG_OFF) {
655 IWL_ERR(mvm, "Start AGG when state is not IWL_AGG_OFF %d!\n",
656 mvmsta->tid_data[tid].state);
657 return -ENXIO;
658 }
659
660 lockdep_assert_held(&mvm->mutex);
661
662 for (txq_id = IWL_MVM_FIRST_AGG_QUEUE;
663 txq_id <= IWL_MVM_LAST_AGG_QUEUE; txq_id++)
664 if (mvm->queue_to_mac80211[txq_id] ==
665 IWL_INVALID_MAC80211_QUEUE)
666 break;
667
668 if (txq_id > IWL_MVM_LAST_AGG_QUEUE) {
669 IWL_ERR(mvm, "Failed to allocate agg queue\n");
670 return -EIO;
671 }
672
673 /* the new tx queue is still connected to the same mac80211 queue */
674 mvm->queue_to_mac80211[txq_id] = vif->hw_queue[tid_to_ac[tid]];
675
676 spin_lock_bh(&mvmsta->lock);
677 tid_data = &mvmsta->tid_data[tid];
678 tid_data->ssn = SEQ_TO_SN(tid_data->seq_number);
679 tid_data->txq_id = txq_id;
680 *ssn = tid_data->ssn;
681
682 IWL_DEBUG_TX_QUEUES(mvm,
683 "Start AGG: sta %d tid %d queue %d - ssn = %d, next_recl = %d\n",
684 mvmsta->sta_id, tid, txq_id, tid_data->ssn,
685 tid_data->next_reclaimed);
686
687 if (tid_data->ssn == tid_data->next_reclaimed) {
688 tid_data->state = IWL_AGG_STARTING;
689 ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
690 } else {
691 tid_data->state = IWL_EMPTYING_HW_QUEUE_ADDBA;
692 }
693
694 spin_unlock_bh(&mvmsta->lock);
695
696 return 0;
697}
698
699int iwl_mvm_sta_tx_agg_oper(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
700 struct ieee80211_sta *sta, u16 tid, u8 buf_size)
701{
702 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
703 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
704 int queue, fifo, ret;
705 u16 ssn;
706
707 buf_size = min_t(int, buf_size, LINK_QUAL_AGG_FRAME_LIMIT_DEF);
708
709 spin_lock_bh(&mvmsta->lock);
710 ssn = tid_data->ssn;
711 queue = tid_data->txq_id;
712 tid_data->state = IWL_AGG_ON;
713 tid_data->ssn = 0xffff;
714 spin_unlock_bh(&mvmsta->lock);
715
716 fifo = iwl_mvm_ac_to_tx_fifo[tid_to_ac[tid]];
717
718 ret = iwl_mvm_sta_tx_agg(mvm, sta, tid, queue, true);
719 if (ret)
720 return -EIO;
721
722 iwl_trans_txq_enable(mvm->trans, queue, fifo, mvmsta->sta_id, tid,
723 buf_size, ssn);
724
725 /*
726 * Even though in theory the peer could have different
727 * aggregation reorder buffer sizes for different sessions,
728 * our ucode doesn't allow for that and has a global limit
729 * for each station. Therefore, use the minimum of all the
730 * aggregation sessions and our default value.
731 */
732 mvmsta->max_agg_bufsize =
733 min(mvmsta->max_agg_bufsize, buf_size);
734 mvmsta->lq_sta.lq.agg_frame_cnt_limit = mvmsta->max_agg_bufsize;
735
736 if (mvm->cfg->ht_params->use_rts_for_aggregation) {
737 /*
738 * switch to RTS/CTS if it is the prefer protection
739 * method for HT traffic
740 */
741 mvmsta->lq_sta.lq.flags |= LQ_FLAG_SET_STA_TLC_RTS_MSK;
742 /*
743 * TODO: remove the TLC_RTS flag when we tear down the last
744 * AGG session (agg_tids_count in DVM)
745 */
746 }
747
748 IWL_DEBUG_HT(mvm, "Tx aggregation enabled on ra = %pM tid = %d\n",
749 sta->addr, tid);
750
751 return iwl_mvm_send_lq_cmd(mvm, &mvmsta->lq_sta.lq, CMD_ASYNC, false);
752}
753
754int iwl_mvm_sta_tx_agg_stop(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
755 struct ieee80211_sta *sta, u16 tid)
756{
757 struct iwl_mvm_sta *mvmsta = (void *)sta->drv_priv;
758 struct iwl_mvm_tid_data *tid_data = &mvmsta->tid_data[tid];
759 u16 txq_id;
760 int err;
761
762 spin_lock_bh(&mvmsta->lock);
763
764 txq_id = tid_data->txq_id;
765
766 IWL_DEBUG_TX_QUEUES(mvm, "Stop AGG: sta %d tid %d q %d state %d\n",
767 mvmsta->sta_id, tid, txq_id, tid_data->state);
768
769 switch (tid_data->state) {
770 case IWL_AGG_ON:
771 tid_data->ssn = SEQ_TO_SN(tid_data->seq_number);
772
773 IWL_DEBUG_TX_QUEUES(mvm,
774 "ssn = %d, next_recl = %d\n",
775 tid_data->ssn, tid_data->next_reclaimed);
776
777 /* There are still packets for this RA / TID in the HW */
778 if (tid_data->ssn != tid_data->next_reclaimed) {
779 tid_data->state = IWL_EMPTYING_HW_QUEUE_DELBA;
780 err = 0;
781 break;
782 }
783
784 tid_data->ssn = 0xffff;
785 iwl_trans_txq_disable(mvm->trans, txq_id);
786 /* fall through */
787 case IWL_AGG_STARTING:
788 case IWL_EMPTYING_HW_QUEUE_ADDBA:
789 /*
790 * The agg session has been stopped before it was set up. This
791 * can happen when the AddBA timer times out for example.
792 */
793
794 /* No barriers since we are under mutex */
795 lockdep_assert_held(&mvm->mutex);
796 mvm->queue_to_mac80211[txq_id] = IWL_INVALID_MAC80211_QUEUE;
797
798 ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
799 tid_data->state = IWL_AGG_OFF;
800 err = 0;
801 break;
802 default:
803 IWL_ERR(mvm,
804 "Stopping AGG while state not ON or starting for %d on %d (%d)\n",
805 mvmsta->sta_id, tid, tid_data->state);
806 IWL_ERR(mvm,
807 "\ttid_data->txq_id = %d\n", tid_data->txq_id);
808 err = -EINVAL;
809 }
810
811 spin_unlock_bh(&mvmsta->lock);
812
813 return err;
814}
815
816static int iwl_mvm_set_fw_key_idx(struct iwl_mvm *mvm)
817{
818 int i;
819
820 lockdep_assert_held(&mvm->mutex);
821
822 i = find_first_zero_bit(mvm->fw_key_table, STA_KEY_MAX_NUM);
823
824 if (i == STA_KEY_MAX_NUM)
825 return STA_KEY_IDX_INVALID;
826
827 __set_bit(i, mvm->fw_key_table);
828
829 return i;
830}
831
832static u8 iwl_mvm_get_key_sta_id(struct ieee80211_vif *vif,
833 struct ieee80211_sta *sta)
834{
835 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
836
837 if (sta) {
838 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
839
840 return mvm_sta->sta_id;
841 }
842
843 /*
844 * The device expects GTKs for station interfaces to be
845 * installed as GTKs for the AP station. If we have no
846 * station ID, then use AP's station ID.
847 */
848 if (vif->type == NL80211_IFTYPE_STATION &&
849 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT)
850 return mvmvif->ap_sta_id;
851
852 return IWL_INVALID_STATION;
853}
854
855static int iwl_mvm_send_sta_key(struct iwl_mvm *mvm,
856 struct iwl_mvm_sta *mvm_sta,
857 struct ieee80211_key_conf *keyconf,
858 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
859 u32 cmd_flags)
860{
861 __le16 key_flags;
862 struct iwl_mvm_add_sta_cmd cmd = {};
863 int ret, status;
864 u16 keyidx;
865 int i;
866
867 keyidx = (keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
868 STA_KEY_FLG_KEYID_MSK;
869 key_flags = cpu_to_le16(keyidx);
870 key_flags |= cpu_to_le16(STA_KEY_FLG_WEP_KEY_MAP);
871
872 switch (keyconf->cipher) {
873 case WLAN_CIPHER_SUITE_TKIP:
874 key_flags |= cpu_to_le16(STA_KEY_FLG_TKIP);
875 cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
876 for (i = 0; i < 5; i++)
877 cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
878 memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
879 break;
880 case WLAN_CIPHER_SUITE_CCMP:
881 key_flags |= cpu_to_le16(STA_KEY_FLG_CCM);
882 memcpy(cmd.key.key, keyconf->key, keyconf->keylen);
883 break;
884 default:
885 WARN_ON(1);
886 return -EINVAL;
887 }
888
889 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
890 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
891
892 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
893 cmd.key.key_offset = keyconf->hw_key_idx;
894 cmd.key.key_flags = key_flags;
895 cmd.add_modify = STA_MODE_MODIFY;
896 cmd.modify_mask = STA_MODIFY_KEY;
897 cmd.sta_id = sta_id;
898
899 status = ADD_STA_SUCCESS;
900 if (cmd_flags == CMD_SYNC)
901 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
902 &cmd, &status);
903 else
904 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC,
905 sizeof(cmd), &cmd);
906
907 switch (status) {
908 case ADD_STA_SUCCESS:
909 IWL_DEBUG_WEP(mvm, "MODIFY_STA: set dynamic key passed\n");
910 break;
911 default:
912 ret = -EIO;
913 IWL_ERR(mvm, "MODIFY_STA: set dynamic key failed\n");
914 break;
915 }
916
917 return ret;
918}
919
920static int iwl_mvm_send_sta_igtk(struct iwl_mvm *mvm,
921 struct ieee80211_key_conf *keyconf,
922 u8 sta_id, bool remove_key)
923{
924 struct iwl_mvm_mgmt_mcast_key_cmd igtk_cmd = {};
925
926 /* verify the key details match the required command's expectations */
927 if (WARN_ON((keyconf->cipher != WLAN_CIPHER_SUITE_AES_CMAC) ||
928 (keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE) ||
929 (keyconf->keyidx != 4 && keyconf->keyidx != 5)))
930 return -EINVAL;
931
932 igtk_cmd.key_id = cpu_to_le32(keyconf->keyidx);
933 igtk_cmd.sta_id = cpu_to_le32(sta_id);
934
935 if (remove_key) {
936 igtk_cmd.ctrl_flags |= cpu_to_le32(STA_KEY_NOT_VALID);
937 } else {
938 struct ieee80211_key_seq seq;
939 const u8 *pn;
940
941 memcpy(igtk_cmd.IGTK, keyconf->key, keyconf->keylen);
942 ieee80211_aes_cmac_calculate_k1_k2(keyconf,
943 igtk_cmd.K1, igtk_cmd.K2);
944 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
945 pn = seq.aes_cmac.pn;
946 igtk_cmd.receive_seq_cnt = cpu_to_le64(((u64) pn[5] << 0) |
947 ((u64) pn[4] << 8) |
948 ((u64) pn[3] << 16) |
949 ((u64) pn[2] << 24) |
950 ((u64) pn[1] << 32) |
951 ((u64) pn[0] << 40));
952 }
953
954 IWL_DEBUG_INFO(mvm, "%s igtk for sta %u\n",
955 remove_key ? "removing" : "installing",
956 igtk_cmd.sta_id);
957
958 return iwl_mvm_send_cmd_pdu(mvm, MGMT_MCAST_KEY, CMD_SYNC,
959 sizeof(igtk_cmd), &igtk_cmd);
960}
961
962
963static inline u8 *iwl_mvm_get_mac_addr(struct iwl_mvm *mvm,
964 struct ieee80211_vif *vif,
965 struct ieee80211_sta *sta)
966{
967 struct iwl_mvm_vif *mvmvif = (void *)vif->drv_priv;
968
969 if (sta)
970 return sta->addr;
971
972 if (vif->type == NL80211_IFTYPE_STATION &&
973 mvmvif->ap_sta_id != IWL_MVM_STATION_COUNT) {
974 u8 sta_id = mvmvif->ap_sta_id;
975 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
976 lockdep_is_held(&mvm->mutex));
977 return sta->addr;
978 }
979
980
981 return NULL;
982}
983
984int iwl_mvm_set_sta_key(struct iwl_mvm *mvm,
985 struct ieee80211_vif *vif,
986 struct ieee80211_sta *sta,
987 struct ieee80211_key_conf *keyconf,
988 bool have_key_offset)
989{
990 struct iwl_mvm_sta *mvm_sta;
991 int ret;
992 u8 *addr, sta_id;
993 struct ieee80211_key_seq seq;
994 u16 p1k[5];
995
996 lockdep_assert_held(&mvm->mutex);
997
998 /* Get the station id from the mvm local station table */
999 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1000 if (sta_id == IWL_INVALID_STATION) {
1001 IWL_ERR(mvm, "Failed to find station id\n");
1002 return -EINVAL;
1003 }
1004
1005 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC) {
1006 ret = iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, false);
1007 goto end;
1008 }
1009
1010 /*
1011 * It is possible that the 'sta' parameter is NULL, and thus
1012 * there is a need to retrieve the sta from the local station table.
1013 */
1014 if (!sta) {
1015 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1016 lockdep_is_held(&mvm->mutex));
1017 if (IS_ERR_OR_NULL(sta)) {
1018 IWL_ERR(mvm, "Invalid station id\n");
1019 return -EINVAL;
1020 }
1021 }
1022
1023 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1024 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1025 return -EINVAL;
1026
1027 if (!have_key_offset) {
1028 /*
1029 * The D3 firmware hardcodes the PTK offset to 0, so we have to
1030 * configure it there. As a result, this workaround exists to
1031 * let the caller set the key offset (hw_key_idx), see d3.c.
1032 */
1033 keyconf->hw_key_idx = iwl_mvm_set_fw_key_idx(mvm);
1034 if (keyconf->hw_key_idx == STA_KEY_IDX_INVALID)
1035 return -ENOSPC;
1036 }
1037
1038 switch (keyconf->cipher) {
1039 case WLAN_CIPHER_SUITE_TKIP:
1040 addr = iwl_mvm_get_mac_addr(mvm, vif, sta);
1041 /* get phase 1 key from mac80211 */
1042 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1043 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
1044 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1045 seq.tkip.iv32, p1k, CMD_SYNC);
1046 break;
1047 case WLAN_CIPHER_SUITE_CCMP:
1048 ret = iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1049 0, NULL, CMD_SYNC);
1050 break;
1051 default:
1052 IWL_ERR(mvm, "Unknown cipher %x\n", keyconf->cipher);
1053 ret = -EINVAL;
1054 }
1055
1056 if (ret)
1057 __clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1058
1059end:
1060 IWL_DEBUG_WEP(mvm, "key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
1061 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
1062 sta->addr, ret);
1063 return ret;
1064}
1065
1066int iwl_mvm_remove_sta_key(struct iwl_mvm *mvm,
1067 struct ieee80211_vif *vif,
1068 struct ieee80211_sta *sta,
1069 struct ieee80211_key_conf *keyconf)
1070{
1071 struct iwl_mvm_sta *mvm_sta;
1072 struct iwl_mvm_add_sta_cmd cmd = {};
1073 __le16 key_flags;
1074 int ret, status;
1075 u8 sta_id;
1076
1077 lockdep_assert_held(&mvm->mutex);
1078
1079 /* Get the station id from the mvm local station table */
1080 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1081
1082 IWL_DEBUG_WEP(mvm, "mvm remove dynamic key: idx=%d sta=%d\n",
1083 keyconf->keyidx, sta_id);
1084
1085 if (keyconf->cipher == WLAN_CIPHER_SUITE_AES_CMAC)
1086 return iwl_mvm_send_sta_igtk(mvm, keyconf, sta_id, true);
1087
1088 ret = __test_and_clear_bit(keyconf->hw_key_idx, mvm->fw_key_table);
1089 if (!ret) {
1090 IWL_ERR(mvm, "offset %d not used in fw key table.\n",
1091 keyconf->hw_key_idx);
1092 return -ENOENT;
1093 }
1094
1095 if (sta_id == IWL_INVALID_STATION) {
1096 IWL_DEBUG_WEP(mvm, "station non-existent, early return.\n");
1097 return 0;
1098 }
1099
1100 /*
1101 * It is possible that the 'sta' parameter is NULL, and thus
1102 * there is a need to retrieve the sta from the local station table,
1103 * for example when a GTK is removed (where the sta_id will then be
1104 * the AP ID, and no station was passed by mac80211.)
1105 */
1106 if (!sta) {
1107 sta = rcu_dereference_protected(mvm->fw_id_to_mac_id[sta_id],
1108 lockdep_is_held(&mvm->mutex));
1109 if (!sta) {
1110 IWL_ERR(mvm, "Invalid station id\n");
1111 return -EINVAL;
1112 }
1113 }
1114
1115 mvm_sta = (struct iwl_mvm_sta *)sta->drv_priv;
1116 if (WARN_ON_ONCE(mvm_sta->vif != vif))
1117 return -EINVAL;
1118
8115efbd
EG
1119 key_flags = cpu_to_le16((keyconf->keyidx << STA_KEY_FLG_KEYID_POS) &
1120 STA_KEY_FLG_KEYID_MSK);
8ca151b5
JB
1121 key_flags |= cpu_to_le16(STA_KEY_FLG_NO_ENC | STA_KEY_FLG_WEP_KEY_MAP);
1122 key_flags |= cpu_to_le16(STA_KEY_NOT_VALID);
1123
1124 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
1125 key_flags |= cpu_to_le16(STA_KEY_MULTICAST);
1126
1127 cmd.mac_id_n_color = cpu_to_le32(mvm_sta->mac_id_n_color);
1128 cmd.key.key_flags = key_flags;
1129 cmd.key.key_offset = keyconf->hw_key_idx;
1130 cmd.sta_id = sta_id;
1131
1132 cmd.modify_mask = STA_MODIFY_KEY;
1133 cmd.add_modify = STA_MODE_MODIFY;
1134
1135 status = ADD_STA_SUCCESS;
1136 ret = iwl_mvm_send_cmd_pdu_status(mvm, ADD_STA, sizeof(cmd),
1137 &cmd, &status);
1138
1139 switch (status) {
1140 case ADD_STA_SUCCESS:
1141 IWL_DEBUG_WEP(mvm, "MODIFY_STA: remove sta key passed\n");
1142 break;
1143 default:
1144 ret = -EIO;
1145 IWL_ERR(mvm, "MODIFY_STA: remove sta key failed\n");
1146 break;
1147 }
1148
1149 return ret;
1150}
1151
1152void iwl_mvm_update_tkip_key(struct iwl_mvm *mvm,
1153 struct ieee80211_vif *vif,
1154 struct ieee80211_key_conf *keyconf,
1155 struct ieee80211_sta *sta, u32 iv32,
1156 u16 *phase1key)
1157{
1158 struct iwl_mvm_sta *mvm_sta = (void *)sta->drv_priv;
1159 u8 sta_id = iwl_mvm_get_key_sta_id(vif, sta);
1160
1161 if (sta_id == IWL_INVALID_STATION)
1162 return;
1163
1164 iwl_mvm_send_sta_key(mvm, mvm_sta, keyconf, sta_id,
1165 iv32, phase1key, CMD_ASYNC);
1166}
1167
1168void iwl_mvm_sta_modify_ps_wake(struct iwl_mvm *mvm, int sta_id)
1169{
1170 struct iwl_mvm_add_sta_cmd cmd = {
1171 .add_modify = STA_MODE_MODIFY,
1172 .sta_id = sta_id,
1173 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1174 .sleep_state_flags = cpu_to_le16(STA_SLEEP_STATE_AWAKE),
1175 };
1176 int ret;
1177
1178 /*
1179 * Same modify mask for sleep_tx_count and sleep_state_flags but this
1180 * should be fine since if we set the STA as "awake", then
1181 * sleep_tx_count is not relevant.
1182 */
1183 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1184 if (ret)
1185 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1186}
1187
1188void iwl_mvm_sta_modify_sleep_tx_count(struct iwl_mvm *mvm, int sta_id,
1189 enum ieee80211_frame_release_type reason,
1190 u16 cnt)
1191{
1192 u16 sleep_state_flags =
1193 (reason == IEEE80211_FRAME_RELEASE_UAPSD) ?
1194 STA_SLEEP_STATE_UAPSD : STA_SLEEP_STATE_PS_POLL;
1195 struct iwl_mvm_add_sta_cmd cmd = {
1196 .add_modify = STA_MODE_MODIFY,
1197 .sta_id = sta_id,
1198 .modify_mask = STA_MODIFY_SLEEPING_STA_TX_COUNT,
1199 .sleep_tx_count = cpu_to_le16(cnt),
1200 /*
1201 * Same modify mask for sleep_tx_count and sleep_state_flags so
1202 * we must set the sleep_state_flags too.
1203 */
1204 .sleep_state_flags = cpu_to_le16(sleep_state_flags),
1205 };
1206 int ret;
1207
1208 /* TODO: somehow the fw doesn't seem to take PS_POLL into account */
1209 ret = iwl_mvm_send_cmd_pdu(mvm, ADD_STA, CMD_ASYNC, sizeof(cmd), &cmd);
1210 if (ret)
1211 IWL_ERR(mvm, "Failed to send ADD_STA command (%d)\n", ret);
1212}