blob: 1b112dfbce779716eb35ff24cf42983aaf0cb5bb [file] [log] [blame]
Johannes Berga30e3112010-09-22 18:02:01 +02001/******************************************************************************
2 *
Wey-Yi Guy901069c2011-04-05 09:42:00 -07003 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
Johannes Berga30e3112010-09-22 18:02:01 +02004 *
5 * Portions of this file are derived from the ipw3945 project, as well
6 * as portions of the ieee80211 subsystem header files.
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
20 *
21 * The full GNU General Public License is included in this distribution in the
22 * file called LICENSE.
23 *
24 * Contact Information:
25 * Intel Linux Wireless <ilw@linux.intel.com>
26 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
27 *
28 *****************************************************************************/
29
30#include <net/mac80211.h>
31
32#include "iwl-dev.h"
33#include "iwl-core.h"
Johannes Berga30e3112010-09-22 18:02:01 +020034#include "iwl-agn.h"
Emmanuel Grumbachbdfbf092011-07-08 08:46:16 -070035#include "iwl-trans.h"
Johannes Berga30e3112010-09-22 18:02:01 +020036
Wey-Yi Guyc745f552011-10-10 07:27:12 -070037/* priv->shrd->sta_lock must be held */
38static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
39{
40
41 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
42 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u "
43 "addr %pM\n",
44 sta_id, priv->stations[sta_id].sta.sta.addr);
45
46 if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
47 IWL_DEBUG_ASSOC(priv,
48 "STA id %u addr %pM already present in uCode "
49 "(according to driver)\n",
50 sta_id, priv->stations[sta_id].sta.sta.addr);
51 } else {
52 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
53 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
54 sta_id, priv->stations[sta_id].sta.sta.addr);
55 }
56}
57
58static int iwl_process_add_sta_resp(struct iwl_priv *priv,
59 struct iwl_addsta_cmd *addsta,
60 struct iwl_rx_packet *pkt)
61{
62 u8 sta_id = addsta->sta.sta_id;
63 unsigned long flags;
64 int ret = -EIO;
65
66 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
67 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
68 pkt->hdr.flags);
69 return ret;
70 }
71
72 IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
73 sta_id);
74
75 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
76
77 switch (pkt->u.add_sta.status) {
78 case ADD_STA_SUCCESS_MSK:
79 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
80 iwl_sta_ucode_activate(priv, sta_id);
81 ret = 0;
82 break;
83 case ADD_STA_NO_ROOM_IN_TABLE:
84 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
85 sta_id);
86 break;
87 case ADD_STA_NO_BLOCK_ACK_RESOURCE:
88 IWL_ERR(priv, "Adding station %d failed, no block ack "
89 "resource.\n", sta_id);
90 break;
91 case ADD_STA_MODIFY_NON_EXIST_STA:
92 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
93 sta_id);
94 break;
95 default:
96 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
97 pkt->u.add_sta.status);
98 break;
99 }
100
101 IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
102 priv->stations[sta_id].sta.mode ==
103 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
104 sta_id, priv->stations[sta_id].sta.sta.addr);
105
106 /*
107 * XXX: The MAC address in the command buffer is often changed from
108 * the original sent to the device. That is, the MAC address
109 * written to the command buffer often is not the same MAC address
110 * read from the command buffer when the command returns. This
111 * issue has not yet been resolved and this debugging is left to
112 * observe the problem.
113 */
114 IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
115 priv->stations[sta_id].sta.mode ==
116 STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
117 addsta->sta.addr);
118 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
119
120 return ret;
121}
122
123int iwl_add_sta_callback(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb,
124 struct iwl_device_cmd *cmd)
125{
126 struct iwl_rx_packet *pkt = rxb_addr(rxb);
127 struct iwl_addsta_cmd *addsta =
128 (struct iwl_addsta_cmd *) cmd->payload;
129
130 return iwl_process_add_sta_resp(priv, addsta, pkt);
131}
132
133static u16 iwlagn_build_addsta_hcmd(const struct iwl_addsta_cmd *cmd, u8 *data)
134{
135 u16 size = (u16)sizeof(struct iwl_addsta_cmd);
136 struct iwl_addsta_cmd *addsta = (struct iwl_addsta_cmd *)data;
137 memcpy(addsta, cmd, size);
138 /* resrved in 5000 */
139 addsta->rate_n_flags = cpu_to_le16(0);
140 return size;
141}
142
143int iwl_send_add_sta(struct iwl_priv *priv,
144 struct iwl_addsta_cmd *sta, u8 flags)
145{
146 int ret = 0;
147 u8 data[sizeof(*sta)];
148 struct iwl_host_cmd cmd = {
149 .id = REPLY_ADD_STA,
150 .flags = flags,
151 .data = { data, },
152 };
153 u8 sta_id __maybe_unused = sta->sta.sta_id;
154
155 IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
156 sta_id, sta->sta.addr, flags & CMD_ASYNC ? "a" : "");
157
158 if (!(flags & CMD_ASYNC)) {
159 cmd.flags |= CMD_WANT_SKB;
160 might_sleep();
161 }
162
163 cmd.len[0] = iwlagn_build_addsta_hcmd(sta, data);
164 ret = iwl_trans_send_cmd(trans(priv), &cmd);
165
166 if (ret || (flags & CMD_ASYNC))
167 return ret;
168 /*else the command was successfully sent in SYNC mode, need to free
169 * the reply page */
170
171 iwl_free_pages(priv->shrd, cmd.reply_page);
172
173 if (cmd.handler_status)
174 IWL_ERR(priv, "%s - error in the CMD response %d", __func__,
175 cmd.handler_status);
176
177 return cmd.handler_status;
178}
179
180static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
181 struct ieee80211_sta *sta,
182 struct iwl_rxon_context *ctx)
183{
184 struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
185 __le32 sta_flags;
186 u8 mimo_ps_mode;
187
188 if (!sta || !sta_ht_inf->ht_supported)
189 goto done;
190
191 mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
192 IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
193 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
194 "static" :
195 (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
196 "dynamic" : "disabled");
197
198 sta_flags = priv->stations[index].sta.station_flags;
199
200 sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
201
202 switch (mimo_ps_mode) {
203 case WLAN_HT_CAP_SM_PS_STATIC:
204 sta_flags |= STA_FLG_MIMO_DIS_MSK;
205 break;
206 case WLAN_HT_CAP_SM_PS_DYNAMIC:
207 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
208 break;
209 case WLAN_HT_CAP_SM_PS_DISABLED:
210 break;
211 default:
212 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
213 break;
214 }
215
216 sta_flags |= cpu_to_le32(
217 (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
218
219 sta_flags |= cpu_to_le32(
220 (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
221
222 if (iwl_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
223 sta_flags |= STA_FLG_HT40_EN_MSK;
224 else
225 sta_flags &= ~STA_FLG_HT40_EN_MSK;
226
227 priv->stations[index].sta.station_flags = sta_flags;
228 done:
229 return;
230}
231
232/**
233 * iwl_prep_station - Prepare station information for addition
234 *
235 * should be called with sta_lock held
236 */
237u8 iwl_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
238 const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
239{
240 struct iwl_station_entry *station;
241 int i;
242 u8 sta_id = IWL_INVALID_STATION;
243
244 if (is_ap)
245 sta_id = ctx->ap_sta_id;
246 else if (is_broadcast_ether_addr(addr))
247 sta_id = ctx->bcast_sta_id;
248 else
249 for (i = IWL_STA_ID; i < IWLAGN_STATION_COUNT; i++) {
250 if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
251 addr)) {
252 sta_id = i;
253 break;
254 }
255
256 if (!priv->stations[i].used &&
257 sta_id == IWL_INVALID_STATION)
258 sta_id = i;
259 }
260
261 /*
262 * These two conditions have the same outcome, but keep them
263 * separate
264 */
265 if (unlikely(sta_id == IWL_INVALID_STATION))
266 return sta_id;
267
268 /*
269 * uCode is not able to deal with multiple requests to add a
270 * station. Keep track if one is in progress so that we do not send
271 * another.
272 */
273 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
274 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
275 "added.\n", sta_id);
276 return sta_id;
277 }
278
279 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
280 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
281 !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
282 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
283 "adding again.\n", sta_id, addr);
284 return sta_id;
285 }
286
287 station = &priv->stations[sta_id];
288 station->used = IWL_STA_DRIVER_ACTIVE;
289 IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
290 sta_id, addr);
291 priv->num_stations++;
292
293 /* Set up the REPLY_ADD_STA command to send to device */
294 memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
295 memcpy(station->sta.sta.addr, addr, ETH_ALEN);
296 station->sta.mode = 0;
297 station->sta.sta.sta_id = sta_id;
298 station->sta.station_flags = ctx->station_flags;
299 station->ctxid = ctx->ctxid;
300
301 if (sta) {
302 struct iwl_station_priv *sta_priv;
303
304 sta_priv = (void *)sta->drv_priv;
305 sta_priv->ctx = ctx;
306 }
307
308 /*
309 * OK to call unconditionally, since local stations (IBSS BSSID
310 * STA and broadcast STA) pass in a NULL sta, and mac80211
311 * doesn't allow HT IBSS.
312 */
313 iwl_set_ht_add_station(priv, sta_id, sta, ctx);
314
315 return sta_id;
316
317}
318
319#define STA_WAIT_TIMEOUT (HZ/2)
320
321/**
322 * iwl_add_station_common -
323 */
324int iwl_add_station_common(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
325 const u8 *addr, bool is_ap,
326 struct ieee80211_sta *sta, u8 *sta_id_r)
327{
328 unsigned long flags_spin;
329 int ret = 0;
330 u8 sta_id;
331 struct iwl_addsta_cmd sta_cmd;
332
333 *sta_id_r = 0;
334 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
335 sta_id = iwl_prep_station(priv, ctx, addr, is_ap, sta);
336 if (sta_id == IWL_INVALID_STATION) {
337 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
338 addr);
339 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
340 return -EINVAL;
341 }
342
343 /*
344 * uCode is not able to deal with multiple requests to add a
345 * station. Keep track if one is in progress so that we do not send
346 * another.
347 */
348 if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
349 IWL_DEBUG_INFO(priv, "STA %d already in process of being "
350 "added.\n", sta_id);
351 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
352 return -EEXIST;
353 }
354
355 if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
356 (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
357 IWL_DEBUG_ASSOC(priv, "STA %d (%pM) already added, not "
358 "adding again.\n", sta_id, addr);
359 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
360 return -EEXIST;
361 }
362
363 priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
364 memcpy(&sta_cmd, &priv->stations[sta_id].sta,
365 sizeof(struct iwl_addsta_cmd));
366 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
367
368 /* Add station to device's station table */
369 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
370 if (ret) {
371 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
372 IWL_ERR(priv, "Adding station %pM failed.\n",
373 priv->stations[sta_id].sta.sta.addr);
374 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
375 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
376 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
377 }
378 *sta_id_r = sta_id;
379 return ret;
380}
381
382/**
383 * iwl_sta_ucode_deactivate - deactivate ucode status for a station
384 *
385 * priv->shrd->sta_lock must be held
386 */
387static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
388{
389 /* Ucode must be active and driver must be non active */
390 if ((priv->stations[sta_id].used &
391 (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
392 IWL_STA_UCODE_ACTIVE)
393 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
394
395 priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
396
397 memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
398 IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
399}
400
401static int iwl_send_remove_station(struct iwl_priv *priv,
402 const u8 *addr, int sta_id,
403 bool temporary)
404{
405 struct iwl_rx_packet *pkt;
406 int ret;
407
408 unsigned long flags_spin;
409 struct iwl_rem_sta_cmd rm_sta_cmd;
410
411 struct iwl_host_cmd cmd = {
412 .id = REPLY_REMOVE_STA,
413 .len = { sizeof(struct iwl_rem_sta_cmd), },
414 .flags = CMD_SYNC,
415 .data = { &rm_sta_cmd, },
416 };
417
418 memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
419 rm_sta_cmd.num_sta = 1;
420 memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
421
422 cmd.flags |= CMD_WANT_SKB;
423
424 ret = iwl_trans_send_cmd(trans(priv), &cmd);
425
426 if (ret)
427 return ret;
428
429 pkt = (struct iwl_rx_packet *)cmd.reply_page;
430 if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
431 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
432 pkt->hdr.flags);
433 ret = -EIO;
434 }
435
436 if (!ret) {
437 switch (pkt->u.rem_sta.status) {
438 case REM_STA_SUCCESS_MSK:
439 if (!temporary) {
440 spin_lock_irqsave(&priv->shrd->sta_lock,
441 flags_spin);
442 iwl_sta_ucode_deactivate(priv, sta_id);
443 spin_unlock_irqrestore(&priv->shrd->sta_lock,
444 flags_spin);
445 }
446 IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
447 break;
448 default:
449 ret = -EIO;
450 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
451 break;
452 }
453 }
454 iwl_free_pages(priv->shrd, cmd.reply_page);
455
456 return ret;
457}
458
459/**
460 * iwl_remove_station - Remove driver's knowledge of station.
461 */
462int iwl_remove_station(struct iwl_priv *priv, const u8 sta_id,
463 const u8 *addr)
464{
465 unsigned long flags;
466
467 if (!iwl_is_ready(priv->shrd)) {
468 IWL_DEBUG_INFO(priv,
469 "Unable to remove station %pM, device not ready.\n",
470 addr);
471 /*
472 * It is typical for stations to be removed when we are
473 * going down. Return success since device will be down
474 * soon anyway
475 */
476 return 0;
477 }
478
479 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
480 sta_id, addr);
481
482 if (WARN_ON(sta_id == IWL_INVALID_STATION))
483 return -EINVAL;
484
485 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
486
487 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
488 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
489 addr);
490 goto out_err;
491 }
492
493 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
494 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
495 addr);
496 goto out_err;
497 }
498
499 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
500 kfree(priv->stations[sta_id].lq);
501 priv->stations[sta_id].lq = NULL;
502 }
503
504 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
505
506 priv->num_stations--;
507
508 if (WARN_ON(priv->num_stations < 0))
509 priv->num_stations = 0;
510
511 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
512
513 return iwl_send_remove_station(priv, addr, sta_id, false);
514out_err:
515 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
516 return -EINVAL;
517}
518
519/**
520 * iwl_clear_ucode_stations - clear ucode station table bits
521 *
522 * This function clears all the bits in the driver indicating
523 * which stations are active in the ucode. Call when something
524 * other than explicit station management would cause this in
525 * the ucode, e.g. unassociated RXON.
526 */
527void iwl_clear_ucode_stations(struct iwl_priv *priv,
528 struct iwl_rxon_context *ctx)
529{
530 int i;
531 unsigned long flags_spin;
532 bool cleared = false;
533
534 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
535
536 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
537 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
538 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
539 continue;
540
541 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
542 IWL_DEBUG_INFO(priv,
543 "Clearing ucode active for station %d\n", i);
544 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
545 cleared = true;
546 }
547 }
548 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
549
550 if (!cleared)
551 IWL_DEBUG_INFO(priv,
552 "No active stations found to be cleared\n");
553}
554
555/**
556 * iwl_restore_stations() - Restore driver known stations to device
557 *
558 * All stations considered active by driver, but not present in ucode, is
559 * restored.
560 *
561 * Function sleeps.
562 */
563void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
564{
565 struct iwl_addsta_cmd sta_cmd;
566 struct iwl_link_quality_cmd lq;
567 unsigned long flags_spin;
568 int i;
569 bool found = false;
570 int ret;
571 bool send_lq;
572
573 if (!iwl_is_ready(priv->shrd)) {
574 IWL_DEBUG_INFO(priv,
575 "Not ready yet, not restoring any stations.\n");
576 return;
577 }
578
579 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
580 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
581 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
582 if (ctx->ctxid != priv->stations[i].ctxid)
583 continue;
584 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
585 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
586 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
587 priv->stations[i].sta.sta.addr);
588 priv->stations[i].sta.mode = 0;
589 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
590 found = true;
591 }
592 }
593
594 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
595 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
596 memcpy(&sta_cmd, &priv->stations[i].sta,
597 sizeof(struct iwl_addsta_cmd));
598 send_lq = false;
599 if (priv->stations[i].lq) {
600 if (priv->shrd->wowlan)
601 iwl_sta_fill_lq(priv, ctx, i, &lq);
602 else
603 memcpy(&lq, priv->stations[i].lq,
604 sizeof(struct iwl_link_quality_cmd));
605 send_lq = true;
606 }
607 spin_unlock_irqrestore(&priv->shrd->sta_lock,
608 flags_spin);
609 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
610 if (ret) {
611 spin_lock_irqsave(&priv->shrd->sta_lock,
612 flags_spin);
613 IWL_ERR(priv, "Adding station %pM failed.\n",
614 priv->stations[i].sta.sta.addr);
615 priv->stations[i].used &=
616 ~IWL_STA_DRIVER_ACTIVE;
617 priv->stations[i].used &=
618 ~IWL_STA_UCODE_INPROGRESS;
619 spin_unlock_irqrestore(&priv->shrd->sta_lock,
620 flags_spin);
621 }
622 /*
623 * Rate scaling has already been initialized, send
624 * current LQ command
625 */
626 if (send_lq)
627 iwl_send_lq_cmd(priv, ctx, &lq,
628 CMD_SYNC, true);
629 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
630 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
631 }
632 }
633
634 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
635 if (!found)
636 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
637 "no stations to be restored.\n");
638 else
639 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
640 "complete.\n");
641}
642
643void iwl_reprogram_ap_sta(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
644{
645 unsigned long flags;
646 int sta_id = ctx->ap_sta_id;
647 int ret;
648 struct iwl_addsta_cmd sta_cmd;
649 struct iwl_link_quality_cmd lq;
Johannes Bergaed0fd42011-11-10 06:55:05 -0800650 bool active, have_lq = false;
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700651
652 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
653 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
654 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
655 return;
656 }
657
658 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
659 sta_cmd.mode = 0;
Johannes Bergaed0fd42011-11-10 06:55:05 -0800660 if (priv->stations[sta_id].lq) {
661 memcpy(&lq, priv->stations[sta_id].lq, sizeof(lq));
662 have_lq = true;
663 }
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700664
665 active = priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE;
666 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
667 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
668
669 if (active) {
670 ret = iwl_send_remove_station(
671 priv, priv->stations[sta_id].sta.sta.addr,
672 sta_id, true);
673 if (ret)
674 IWL_ERR(priv, "failed to remove STA %pM (%d)\n",
675 priv->stations[sta_id].sta.sta.addr, ret);
676 }
677 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
678 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
679 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
680
681 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
682 if (ret)
683 IWL_ERR(priv, "failed to re-add STA %pM (%d)\n",
684 priv->stations[sta_id].sta.sta.addr, ret);
Johannes Bergaed0fd42011-11-10 06:55:05 -0800685 if (have_lq)
686 iwl_send_lq_cmd(priv, ctx, &lq, CMD_SYNC, true);
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700687}
688
689int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
690{
691 int i;
692
693 for (i = 0; i < priv->sta_key_max_num; i++)
694 if (!test_and_set_bit(i, &priv->ucode_key_table))
695 return i;
696
697 return WEP_INVALID_OFFSET;
698}
699
700void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
701{
702 unsigned long flags;
703 int i;
704
705 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
706 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
707 if (!(priv->stations[i].used & IWL_STA_BCAST))
708 continue;
709
710 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
711 priv->num_stations--;
712 if (WARN_ON(priv->num_stations < 0))
713 priv->num_stations = 0;
714 kfree(priv->stations[i].lq);
715 priv->stations[i].lq = NULL;
716 }
717 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
718}
719
720#ifdef CONFIG_IWLWIFI_DEBUG
721static void iwl_dump_lq_cmd(struct iwl_priv *priv,
722 struct iwl_link_quality_cmd *lq)
723{
724 int i;
725 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
726 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
727 lq->general_params.single_stream_ant_msk,
728 lq->general_params.dual_stream_ant_msk);
729
730 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
731 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
732 i, lq->rs_table[i].rate_n_flags);
733}
734#else
735static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
736 struct iwl_link_quality_cmd *lq)
737{
738}
739#endif
740
741/**
742 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
743 *
744 * It sometimes happens when a HT rate has been in use and we
745 * loose connectivity with AP then mac80211 will first tell us that the
746 * current channel is not HT anymore before removing the station. In such a
747 * scenario the RXON flags will be updated to indicate we are not
748 * communicating HT anymore, but the LQ command may still contain HT rates.
749 * Test for this to prevent driver from sending LQ command between the time
750 * RXON flags are updated and when LQ command is updated.
751 */
752static bool is_lq_table_valid(struct iwl_priv *priv,
753 struct iwl_rxon_context *ctx,
754 struct iwl_link_quality_cmd *lq)
755{
756 int i;
757
758 if (ctx->ht.enabled)
759 return true;
760
761 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
762 ctx->active.channel);
763 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
764 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
765 RATE_MCS_HT_MSK) {
766 IWL_DEBUG_INFO(priv,
767 "index %d of LQ expects HT channel\n",
768 i);
769 return false;
770 }
771 }
772 return true;
773}
774
775/**
776 * iwl_send_lq_cmd() - Send link quality command
777 * @init: This command is sent as part of station initialization right
778 * after station has been added.
779 *
780 * The link quality command is sent as the last step of station creation.
781 * This is the special case in which init is set and we call a callback in
782 * this case to clear the state indicating that station creation is in
783 * progress.
784 */
785int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
786 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
787{
788 int ret = 0;
789 unsigned long flags_spin;
790
791 struct iwl_host_cmd cmd = {
792 .id = REPLY_TX_LINK_QUALITY_CMD,
793 .len = { sizeof(struct iwl_link_quality_cmd), },
794 .flags = flags,
795 .data = { lq, },
796 };
797
798 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
799 return -EINVAL;
800
801
802 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
803 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
804 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
805 return -EINVAL;
806 }
807 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
808
809 iwl_dump_lq_cmd(priv, lq);
810 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
811 return -EINVAL;
812
813 if (is_lq_table_valid(priv, ctx, lq))
814 ret = iwl_trans_send_cmd(trans(priv), &cmd);
815 else
816 ret = -EINVAL;
817
818 if (cmd.flags & CMD_ASYNC)
819 return ret;
820
821 if (init) {
822 IWL_DEBUG_INFO(priv, "init LQ command complete, "
823 "clearing sta addition status for sta %d\n",
824 lq->sta_id);
825 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
826 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
827 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
828 }
829 return ret;
830}
831
832int iwlagn_mac_sta_remove(struct ieee80211_hw *hw,
833 struct ieee80211_vif *vif,
834 struct ieee80211_sta *sta)
835{
836 struct iwl_priv *priv = hw->priv;
837 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
838 int ret;
839
840 IWL_DEBUG_MAC80211(priv, "enter: received request to remove "
841 "station %pM\n", sta->addr);
842 mutex_lock(&priv->shrd->mutex);
843 IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
844 sta->addr);
845 ret = iwl_remove_station(priv, sta_priv->sta_id, sta->addr);
846 if (ret)
Todd Previteb36b1102011-11-10 06:55:02 -0800847 IWL_DEBUG_QUIET_RFKILL(priv, "Error removing station %pM\n",
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700848 sta->addr);
849 mutex_unlock(&priv->shrd->mutex);
850 IWL_DEBUG_MAC80211(priv, "leave\n");
851
852 return ret;
853}
854
Johannes Bergc079166e2011-10-10 07:26:54 -0700855void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
856 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
Johannes Berga30e3112010-09-22 18:02:01 +0200857{
858 int i, r;
Johannes Berga30e3112010-09-22 18:02:01 +0200859 u32 rate_flags = 0;
860 __le32 rate_n_flags;
861
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -0700862 lockdep_assert_held(&priv->shrd->mutex);
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700863
Johannes Bergc079166e2011-10-10 07:26:54 -0700864 memset(link_cmd, 0, sizeof(*link_cmd));
865
Johannes Berga30e3112010-09-22 18:02:01 +0200866 /* Set up the rate scaling to start at selected rate, fall back
867 * all the way down to 1M in IEEE order, and then spin on 1M */
868 if (priv->band == IEEE80211_BAND_5GHZ)
869 r = IWL_RATE_6M_INDEX;
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700870 else if (ctx && ctx->vif && ctx->vif->p2p)
871 r = IWL_RATE_6M_INDEX;
Johannes Berga30e3112010-09-22 18:02:01 +0200872 else
873 r = IWL_RATE_1M_INDEX;
874
875 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
876 rate_flags |= RATE_MCS_CCK_MSK;
877
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700878 rate_flags |= first_antenna(hw_params(priv).valid_tx_ant) <<
Johannes Berga30e3112010-09-22 18:02:01 +0200879 RATE_MCS_ANT_POS;
880 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
881 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
882 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
883
884 link_cmd->general_params.single_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700885 first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +0200886
887 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700888 hw_params(priv).valid_tx_ant &
889 ~first_antenna(hw_params(priv).valid_tx_ant);
Johannes Berga30e3112010-09-22 18:02:01 +0200890 if (!link_cmd->general_params.dual_stream_ant_msk) {
891 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700892 } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
Johannes Berga30e3112010-09-22 18:02:01 +0200893 link_cmd->general_params.dual_stream_ant_msk =
Emmanuel Grumbachd6189122011-08-25 23:10:39 -0700894 hw_params(priv).valid_tx_ant;
Johannes Berga30e3112010-09-22 18:02:01 +0200895 }
896
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700897 link_cmd->agg_params.agg_dis_start_th =
898 LINK_QUAL_AGG_DISABLE_START_DEF;
Johannes Berga30e3112010-09-22 18:02:01 +0200899 link_cmd->agg_params.agg_time_limit =
900 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
901
902 link_cmd->sta_id = sta_id;
Johannes Bergc079166e2011-10-10 07:26:54 -0700903}
904
905static struct iwl_link_quality_cmd *
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700906iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
907 u8 sta_id)
Johannes Bergc079166e2011-10-10 07:26:54 -0700908{
909 struct iwl_link_quality_cmd *link_cmd;
910
911 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
912 if (!link_cmd) {
913 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
914 return NULL;
915 }
916
917 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
Johannes Berga30e3112010-09-22 18:02:01 +0200918
919 return link_cmd;
920}
921
922/*
923 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
924 *
925 * Function sleeps.
926 */
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700927int iwlagn_add_bssid_station(struct iwl_priv *priv,
928 struct iwl_rxon_context *ctx,
Johannes Berga30e3112010-09-22 18:02:01 +0200929 const u8 *addr, u8 *sta_id_r)
930{
931 int ret;
932 u8 sta_id;
933 struct iwl_link_quality_cmd *link_cmd;
934 unsigned long flags;
935
936 if (sta_id_r)
937 *sta_id_r = IWL_INVALID_STATION;
938
939 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
940 if (ret) {
941 IWL_ERR(priv, "Unable to add station %pM\n", addr);
942 return ret;
943 }
944
945 if (sta_id_r)
946 *sta_id_r = sta_id;
947
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700948 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200949 priv->stations[sta_id].used |= IWL_STA_LOCAL;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700950 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200951
952 /* Set up default rate scaling table in device's station table */
Johannes Bergf775aa06d2011-06-22 06:34:09 -0700953 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +0200954 if (!link_cmd) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -0700955 IWL_ERR(priv,
956 "Unable to initialize rate scaling for station %pM.\n",
Johannes Berga30e3112010-09-22 18:02:01 +0200957 addr);
958 return -ENOMEM;
959 }
960
961 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
962 if (ret)
963 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
964
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700965 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200966 priv->stations[sta_id].lq = link_cmd;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -0700967 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +0200968
969 return 0;
970}
971
Johannes Berg5a3d9882011-07-15 13:03:12 -0700972/*
973 * static WEP keys
974 *
975 * For each context, the device has a table of 4 static WEP keys
976 * (one for each key index) that is updated with the following
977 * commands.
978 */
979
Johannes Berga30e3112010-09-22 18:02:01 +0200980static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
981 struct iwl_rxon_context *ctx,
982 bool send_if_empty)
983{
984 int i, not_empty = 0;
985 u8 buff[sizeof(struct iwl_wep_cmd) +
986 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
987 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
988 size_t cmd_size = sizeof(struct iwl_wep_cmd);
989 struct iwl_host_cmd cmd = {
990 .id = ctx->wep_key_cmd,
Johannes Berg3fa50732011-05-04 07:50:38 -0700991 .data = { wep_cmd, },
Johannes Berga30e3112010-09-22 18:02:01 +0200992 .flags = CMD_SYNC,
993 };
994
995 might_sleep();
996
997 memset(wep_cmd, 0, cmd_size +
998 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
999
1000 for (i = 0; i < WEP_KEYS_MAX ; i++) {
1001 wep_cmd->key[i].key_index = i;
1002 if (ctx->wep_keys[i].key_size) {
1003 wep_cmd->key[i].key_offset = i;
1004 not_empty = 1;
1005 } else {
1006 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
1007 }
1008
1009 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
1010 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
1011 ctx->wep_keys[i].key_size);
1012 }
1013
1014 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
1015 wep_cmd->num_keys = WEP_KEYS_MAX;
1016
1017 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1018
Johannes Berg3fa50732011-05-04 07:50:38 -07001019 cmd.len[0] = cmd_size;
Johannes Berga30e3112010-09-22 18:02:01 +02001020
1021 if (not_empty || send_if_empty)
Emmanuel Grumbache6bb4c92011-08-25 23:10:48 -07001022 return iwl_trans_send_cmd(trans(priv), &cmd);
Johannes Berga30e3112010-09-22 18:02:01 +02001023 else
1024 return 0;
1025}
1026
1027int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1028 struct iwl_rxon_context *ctx)
1029{
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001030 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001031
1032 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1033}
1034
1035int iwl_remove_default_wep_key(struct iwl_priv *priv,
1036 struct iwl_rxon_context *ctx,
1037 struct ieee80211_key_conf *keyconf)
1038{
1039 int ret;
1040
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001041 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001042
1043 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1044 keyconf->keyidx);
1045
1046 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
Emmanuel Grumbach845a9c02011-08-25 23:11:04 -07001047 if (iwl_is_rfkill(priv->shrd)) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001048 IWL_DEBUG_WEP(priv,
1049 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
Johannes Berga30e3112010-09-22 18:02:01 +02001050 /* but keys in device are clear anyway so return success */
1051 return 0;
1052 }
1053 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1054 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1055 keyconf->keyidx, ret);
1056
1057 return ret;
1058}
1059
1060int iwl_set_default_wep_key(struct iwl_priv *priv,
1061 struct iwl_rxon_context *ctx,
1062 struct ieee80211_key_conf *keyconf)
1063{
1064 int ret;
1065
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001066 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001067
1068 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1069 keyconf->keylen != WEP_KEY_LEN_64) {
Wey-Yi Guyc745f552011-10-10 07:27:12 -07001070 IWL_DEBUG_WEP(priv,
1071 "Bad WEP key length %d\n", keyconf->keylen);
Johannes Berga30e3112010-09-22 18:02:01 +02001072 return -EINVAL;
1073 }
1074
Johannes Berg5a3d9882011-07-15 13:03:12 -07001075 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
Johannes Berga30e3112010-09-22 18:02:01 +02001076
1077 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1078 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1079 keyconf->keylen);
1080
1081 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1082 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1083 keyconf->keylen, keyconf->keyidx, ret);
1084
1085 return ret;
1086}
1087
Johannes Berg5a3d9882011-07-15 13:03:12 -07001088/*
1089 * dynamic (per-station) keys
1090 *
1091 * The dynamic keys are a little more complicated. The device has
1092 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1093 * These are linked to stations by a table that contains an index
1094 * into the key table for each station/key index/{mcast,unicast},
1095 * i.e. it's basically an array of pointers like this:
1096 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1097 * (it really works differently, but you can think of it as such)
1098 *
1099 * The key uploading and linking happens in the same command, the
1100 * add station command with STA_MODIFY_KEY_MASK.
1101 */
1102
1103static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1104 struct ieee80211_vif *vif,
1105 struct ieee80211_sta *sta)
1106{
1107 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1108 u8 sta_id = IWL_INVALID_STATION;
1109
1110 if (sta)
1111 sta_id = iwl_sta_id(sta);
1112
1113 /*
1114 * The device expects GTKs for station interfaces to be
1115 * installed as GTKs for the AP station. If we have no
1116 * station ID, then use the ap_sta_id in that case.
1117 */
1118 if (!sta && vif && vif_priv->ctx) {
1119 switch (vif->type) {
1120 case NL80211_IFTYPE_STATION:
1121 sta_id = vif_priv->ctx->ap_sta_id;
1122 break;
1123 default:
1124 /*
1125 * In all other cases, the key will be
1126 * used either for TX only or is bound
1127 * to a station already.
1128 */
1129 break;
1130 }
1131 }
1132
1133 return sta_id;
1134}
1135
Johannes Berge1b1c082011-07-17 01:44:11 -07001136static int iwlagn_send_sta_key(struct iwl_priv *priv,
1137 struct ieee80211_key_conf *keyconf,
1138 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1139 u32 cmd_flags)
Johannes Berga30e3112010-09-22 18:02:01 +02001140{
1141 unsigned long flags;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001142 __le16 key_flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001143 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001144 int i;
Johannes Berge1b1c082011-07-17 01:44:11 -07001145
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001146 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001147 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001148 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001149
Johannes Berg5a3d9882011-07-15 13:03:12 -07001150 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1151 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001152
Johannes Berg5a3d9882011-07-15 13:03:12 -07001153 switch (keyconf->cipher) {
1154 case WLAN_CIPHER_SUITE_CCMP:
1155 key_flags |= STA_KEY_FLG_CCMP;
1156 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1157 break;
1158 case WLAN_CIPHER_SUITE_TKIP:
1159 key_flags |= STA_KEY_FLG_TKIP;
1160 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1161 for (i = 0; i < 5; i++)
1162 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1163 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1164 break;
1165 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berga30e3112010-09-22 18:02:01 +02001166 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001167 /* fall through */
1168 case WLAN_CIPHER_SUITE_WEP40:
1169 key_flags |= STA_KEY_FLG_WEP;
1170 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1171 break;
1172 default:
1173 WARN_ON(1);
1174 return -EINVAL;
1175 }
Johannes Berga30e3112010-09-22 18:02:01 +02001176
Johannes Berg5a3d9882011-07-15 13:03:12 -07001177 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
Johannes Berga30e3112010-09-22 18:02:01 +02001178 key_flags |= STA_KEY_MULTICAST_MSK;
1179
Johannes Berg5a3d9882011-07-15 13:03:12 -07001180 /* key pointer (offset) */
1181 sta_cmd.key.key_offset = keyconf->hw_key_idx;
Johannes Berga30e3112010-09-22 18:02:01 +02001182
Johannes Berg5a3d9882011-07-15 13:03:12 -07001183 sta_cmd.key.key_flags = key_flags;
1184 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1185 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
Johannes Berga30e3112010-09-22 18:02:01 +02001186
Johannes Berg5a3d9882011-07-15 13:03:12 -07001187 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001188}
1189
1190void iwl_update_tkip_key(struct iwl_priv *priv,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001191 struct ieee80211_vif *vif,
Johannes Berga30e3112010-09-22 18:02:01 +02001192 struct ieee80211_key_conf *keyconf,
1193 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1194{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001195 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1196
1197 if (sta_id == IWL_INVALID_STATION)
1198 return;
Johannes Berga30e3112010-09-22 18:02:01 +02001199
1200 if (iwl_scan_cancel(priv)) {
1201 /* cancel scan failed, just live w/ bad key and rely
1202 briefly on SW decryption */
1203 return;
1204 }
1205
Johannes Berge1b1c082011-07-17 01:44:11 -07001206 iwlagn_send_sta_key(priv, keyconf, sta_id,
1207 iv32, phase1key, CMD_ASYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001208}
1209
1210int iwl_remove_dynamic_key(struct iwl_priv *priv,
1211 struct iwl_rxon_context *ctx,
1212 struct ieee80211_key_conf *keyconf,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001213 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001214{
1215 unsigned long flags;
Johannes Berga30e3112010-09-22 18:02:01 +02001216 struct iwl_addsta_cmd sta_cmd;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001217 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1218
1219 /* if station isn't there, neither is the key */
1220 if (sta_id == IWL_INVALID_STATION)
1221 return -ENOENT;
1222
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001223 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001224 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1225 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1226 sta_id = IWL_INVALID_STATION;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001227 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berg5a3d9882011-07-15 13:03:12 -07001228
1229 if (sta_id == IWL_INVALID_STATION)
1230 return 0;
Johannes Berga30e3112010-09-22 18:02:01 +02001231
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001232 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001233
1234 ctx->key_mapping_keys--;
1235
Johannes Berga30e3112010-09-22 18:02:01 +02001236 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1237 keyconf->keyidx, sta_id);
1238
Johannes Berg5a3d9882011-07-15 13:03:12 -07001239 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1240 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1241 keyconf->hw_key_idx);
Johannes Berga30e3112010-09-22 18:02:01 +02001242
Johannes Berg5a3d9882011-07-15 13:03:12 -07001243 sta_cmd.key.key_flags = STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1244 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1245 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1246 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
Johannes Berga30e3112010-09-22 18:02:01 +02001247
1248 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1249}
1250
Johannes Berg5a3d9882011-07-15 13:03:12 -07001251int iwl_set_dynamic_key(struct iwl_priv *priv,
1252 struct iwl_rxon_context *ctx,
1253 struct ieee80211_key_conf *keyconf,
1254 struct ieee80211_sta *sta)
Johannes Berga30e3112010-09-22 18:02:01 +02001255{
Johannes Berg5a3d9882011-07-15 13:03:12 -07001256 struct ieee80211_key_seq seq;
1257 u16 p1k[5];
Johannes Berga30e3112010-09-22 18:02:01 +02001258 int ret;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001259 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1260 const u8 *addr;
1261
1262 if (sta_id == IWL_INVALID_STATION)
1263 return -EINVAL;
Johannes Berga30e3112010-09-22 18:02:01 +02001264
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001265 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001266
Johannes Berg5a3d9882011-07-15 13:03:12 -07001267 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1268 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1269 return -ENOSPC;
1270
Johannes Berga30e3112010-09-22 18:02:01 +02001271 ctx->key_mapping_keys++;
Johannes Berga30e3112010-09-22 18:02:01 +02001272
1273 switch (keyconf->cipher) {
Johannes Berga30e3112010-09-22 18:02:01 +02001274 case WLAN_CIPHER_SUITE_TKIP:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001275 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
1276 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1277
1278 if (sta)
1279 addr = sta->addr;
1280 else /* station mode case only */
1281 addr = ctx->active.bssid_addr;
1282
1283 /* pre-fill phase 1 key into device cache */
1284 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1285 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
Johannes Berge1b1c082011-07-17 01:44:11 -07001286 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1287 seq.tkip.iv32, p1k, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001288 break;
Johannes Berg5a3d9882011-07-15 13:03:12 -07001289 case WLAN_CIPHER_SUITE_CCMP:
1290 keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
1291 /* fall through */
Johannes Berga30e3112010-09-22 18:02:01 +02001292 case WLAN_CIPHER_SUITE_WEP40:
1293 case WLAN_CIPHER_SUITE_WEP104:
Johannes Berge1b1c082011-07-17 01:44:11 -07001294 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1295 0, NULL, CMD_SYNC);
Johannes Berga30e3112010-09-22 18:02:01 +02001296 break;
1297 default:
Johannes Berg5a3d9882011-07-15 13:03:12 -07001298 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
Johannes Berga30e3112010-09-22 18:02:01 +02001299 ret = -EINVAL;
1300 }
1301
Johannes Berg5a3d9882011-07-15 13:03:12 -07001302 if (ret) {
1303 ctx->key_mapping_keys--;
1304 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1305 }
1306
1307 IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
Johannes Berga30e3112010-09-22 18:02:01 +02001308 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
Johannes Berg5a3d9882011-07-15 13:03:12 -07001309 sta ? sta->addr : NULL, ret);
Johannes Berga30e3112010-09-22 18:02:01 +02001310
1311 return ret;
1312}
1313
1314/**
1315 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1316 *
1317 * This adds the broadcast station into the driver's station table
1318 * and marks it driver active, so that it will be restored to the
1319 * device at the next best time.
1320 */
1321int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1322 struct iwl_rxon_context *ctx)
1323{
1324 struct iwl_link_quality_cmd *link_cmd;
1325 unsigned long flags;
1326 u8 sta_id;
1327
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001328 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001329 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1330 if (sta_id == IWL_INVALID_STATION) {
1331 IWL_ERR(priv, "Unable to prepare broadcast station\n");
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001332 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001333
1334 return -EINVAL;
1335 }
1336
1337 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1338 priv->stations[sta_id].used |= IWL_STA_BCAST;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001339 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001340
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001341 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001342 if (!link_cmd) {
1343 IWL_ERR(priv,
1344 "Unable to initialize rate scaling for bcast station.\n");
1345 return -ENOMEM;
1346 }
1347
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001348 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001349 priv->stations[sta_id].lq = link_cmd;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001350 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001351
1352 return 0;
1353}
1354
1355/**
1356 * iwl_update_bcast_station - update broadcast station's LQ command
1357 *
1358 * Only used by iwlagn. Placed here to have all bcast station management
1359 * code together.
1360 */
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001361int iwl_update_bcast_station(struct iwl_priv *priv,
1362 struct iwl_rxon_context *ctx)
Johannes Berga30e3112010-09-22 18:02:01 +02001363{
1364 unsigned long flags;
1365 struct iwl_link_quality_cmd *link_cmd;
1366 u8 sta_id = ctx->bcast_sta_id;
1367
Johannes Bergf775aa06d2011-06-22 06:34:09 -07001368 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
Johannes Berga30e3112010-09-22 18:02:01 +02001369 if (!link_cmd) {
1370 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1371 return -ENOMEM;
1372 }
1373
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001374 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001375 if (priv->stations[sta_id].lq)
1376 kfree(priv->stations[sta_id].lq);
1377 else
1378 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1379 priv->stations[sta_id].lq = link_cmd;
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001380 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001381
1382 return 0;
1383}
1384
1385int iwl_update_bcast_stations(struct iwl_priv *priv)
1386{
1387 struct iwl_rxon_context *ctx;
1388 int ret = 0;
1389
1390 for_each_context(priv, ctx) {
1391 ret = iwl_update_bcast_station(priv, ctx);
1392 if (ret)
1393 break;
1394 }
1395
1396 return ret;
1397}
1398
1399/**
1400 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1401 */
1402int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1403{
1404 unsigned long flags;
1405 struct iwl_addsta_cmd sta_cmd;
1406
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001407 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001408
1409 /* Remove "disable" flag, to enable Tx for this TID */
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001410 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001411 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1412 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1413 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1414 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001415 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001416
1417 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1418}
1419
1420int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1421 int tid, u16 ssn)
1422{
1423 unsigned long flags;
1424 int sta_id;
1425 struct iwl_addsta_cmd sta_cmd;
1426
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001427 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001428
1429 sta_id = iwl_sta_id(sta);
1430 if (sta_id == IWL_INVALID_STATION)
1431 return -ENXIO;
1432
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001433 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001434 priv->stations[sta_id].sta.station_flags_msk = 0;
1435 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1436 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1437 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1438 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1439 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001440 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001441
1442 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1443}
1444
1445int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1446 int tid)
1447{
1448 unsigned long flags;
1449 int sta_id;
1450 struct iwl_addsta_cmd sta_cmd;
1451
Emmanuel Grumbach6ac2f832011-08-25 23:10:44 -07001452 lockdep_assert_held(&priv->shrd->mutex);
Johannes Berga30e3112010-09-22 18:02:01 +02001453
1454 sta_id = iwl_sta_id(sta);
1455 if (sta_id == IWL_INVALID_STATION) {
1456 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1457 return -ENXIO;
1458 }
1459
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001460 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001461 priv->stations[sta_id].sta.station_flags_msk = 0;
1462 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1463 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1464 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1465 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001466 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001467
1468 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1469}
1470
Johannes Bergae79d232010-11-10 09:56:38 -08001471static void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
Johannes Berga30e3112010-09-22 18:02:01 +02001472{
1473 unsigned long flags;
1474
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001475 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001476 priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1477 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1478 priv->stations[sta_id].sta.sta.modify_mask = 0;
1479 priv->stations[sta_id].sta.sleep_tx_count = 0;
1480 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1481 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001482 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001483
1484}
1485
1486void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1487{
1488 unsigned long flags;
1489
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001490 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001491 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1492 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1493 priv->stations[sta_id].sta.sta.modify_mask =
1494 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1495 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1496 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1497 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
Emmanuel Grumbachf39c95e2011-08-25 23:10:47 -07001498 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
Johannes Berga30e3112010-09-22 18:02:01 +02001499
1500}
Johannes Bergae79d232010-11-10 09:56:38 -08001501
1502void iwlagn_mac_sta_notify(struct ieee80211_hw *hw,
1503 struct ieee80211_vif *vif,
1504 enum sta_notify_cmd cmd,
1505 struct ieee80211_sta *sta)
1506{
1507 struct iwl_priv *priv = hw->priv;
1508 struct iwl_station_priv *sta_priv = (void *)sta->drv_priv;
1509 int sta_id;
1510
Wey-Yi Guy770c72c2011-10-10 07:27:10 -07001511 IWL_DEBUG_MAC80211(priv, "enter\n");
1512
Johannes Bergae79d232010-11-10 09:56:38 -08001513 switch (cmd) {
1514 case STA_NOTIFY_SLEEP:
1515 WARN_ON(!sta_priv->client);
1516 sta_priv->asleep = true;
1517 if (atomic_read(&sta_priv->pending_frames) > 0)
1518 ieee80211_sta_block_awake(hw, sta, true);
1519 break;
1520 case STA_NOTIFY_AWAKE:
1521 WARN_ON(!sta_priv->client);
1522 if (!sta_priv->asleep)
1523 break;
1524 sta_priv->asleep = false;
1525 sta_id = iwl_sta_id(sta);
1526 if (sta_id != IWL_INVALID_STATION)
1527 iwl_sta_modify_ps_wake(priv, sta_id);
1528 break;
1529 default:
1530 break;
1531 }
Wey-Yi Guy770c72c2011-10-10 07:27:10 -07001532 IWL_DEBUG_MAC80211(priv, "leave\n");
Johannes Bergae79d232010-11-10 09:56:38 -08001533}