iwlwifi: add IO function for continuous write of target memory
[linux-2.6-block.git] / drivers / net / wireless / iwlwifi / iwl-agn-sta.c
CommitLineData
a30e3112
JB
1/******************************************************************************
2 *
901069c7 3 * Copyright(c) 2003 - 2011 Intel Corporation. All rights reserved.
a30e3112
JB
4 *
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"
a30e3112 34#include "iwl-agn.h"
bdfbf092 35#include "iwl-trans.h"
a30e3112 36
c745f55b
WYG
37/* 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);
7f62cd17
WYG
138 /* resrved in agn */
139 addsta->legacy_reserved = cpu_to_le16(0);
c745f55b
WYG
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;
855c2ee8 466 u8 tid;
c745f55b
WYG
467
468 if (!iwl_is_ready(priv->shrd)) {
469 IWL_DEBUG_INFO(priv,
470 "Unable to remove station %pM, device not ready.\n",
471 addr);
472 /*
473 * It is typical for stations to be removed when we are
474 * going down. Return success since device will be down
475 * soon anyway
476 */
477 return 0;
478 }
479
480 IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d %pM\n",
481 sta_id, addr);
482
483 if (WARN_ON(sta_id == IWL_INVALID_STATION))
484 return -EINVAL;
485
486 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
487
488 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
489 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
490 addr);
491 goto out_err;
492 }
493
494 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
495 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
496 addr);
497 goto out_err;
498 }
499
500 if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
501 kfree(priv->stations[sta_id].lq);
502 priv->stations[sta_id].lq = NULL;
503 }
504
855c2ee8
EG
505 for (tid = 0; tid < IWL_MAX_TID_COUNT; tid++)
506 memset(&priv->tid_data[sta_id][tid], 0,
507 sizeof(priv->tid_data[sta_id][tid]));
508
c745f55b
WYG
509 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
510
511 priv->num_stations--;
512
513 if (WARN_ON(priv->num_stations < 0))
514 priv->num_stations = 0;
515
516 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
517
518 return iwl_send_remove_station(priv, addr, sta_id, false);
519out_err:
520 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
521 return -EINVAL;
522}
523
524/**
525 * iwl_clear_ucode_stations - clear ucode station table bits
526 *
527 * This function clears all the bits in the driver indicating
528 * which stations are active in the ucode. Call when something
529 * other than explicit station management would cause this in
530 * the ucode, e.g. unassociated RXON.
531 */
532void iwl_clear_ucode_stations(struct iwl_priv *priv,
533 struct iwl_rxon_context *ctx)
534{
535 int i;
536 unsigned long flags_spin;
537 bool cleared = false;
538
539 IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
540
541 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
542 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
543 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
544 continue;
545
546 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
547 IWL_DEBUG_INFO(priv,
548 "Clearing ucode active for station %d\n", i);
549 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
550 cleared = true;
551 }
552 }
553 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
554
555 if (!cleared)
556 IWL_DEBUG_INFO(priv,
557 "No active stations found to be cleared\n");
558}
559
560/**
561 * iwl_restore_stations() - Restore driver known stations to device
562 *
563 * All stations considered active by driver, but not present in ucode, is
564 * restored.
565 *
566 * Function sleeps.
567 */
568void iwl_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
569{
570 struct iwl_addsta_cmd sta_cmd;
571 struct iwl_link_quality_cmd lq;
572 unsigned long flags_spin;
573 int i;
574 bool found = false;
575 int ret;
576 bool send_lq;
577
578 if (!iwl_is_ready(priv->shrd)) {
579 IWL_DEBUG_INFO(priv,
580 "Not ready yet, not restoring any stations.\n");
581 return;
582 }
583
584 IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
585 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
586 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
587 if (ctx->ctxid != priv->stations[i].ctxid)
588 continue;
589 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
590 !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
591 IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
592 priv->stations[i].sta.sta.addr);
593 priv->stations[i].sta.mode = 0;
594 priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
595 found = true;
596 }
597 }
598
599 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
600 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
601 memcpy(&sta_cmd, &priv->stations[i].sta,
602 sizeof(struct iwl_addsta_cmd));
603 send_lq = false;
604 if (priv->stations[i].lq) {
605 if (priv->shrd->wowlan)
606 iwl_sta_fill_lq(priv, ctx, i, &lq);
607 else
608 memcpy(&lq, priv->stations[i].lq,
609 sizeof(struct iwl_link_quality_cmd));
610 send_lq = true;
611 }
612 spin_unlock_irqrestore(&priv->shrd->sta_lock,
613 flags_spin);
614 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
615 if (ret) {
616 spin_lock_irqsave(&priv->shrd->sta_lock,
617 flags_spin);
618 IWL_ERR(priv, "Adding station %pM failed.\n",
619 priv->stations[i].sta.sta.addr);
620 priv->stations[i].used &=
621 ~IWL_STA_DRIVER_ACTIVE;
622 priv->stations[i].used &=
623 ~IWL_STA_UCODE_INPROGRESS;
624 spin_unlock_irqrestore(&priv->shrd->sta_lock,
625 flags_spin);
626 }
627 /*
628 * Rate scaling has already been initialized, send
629 * current LQ command
630 */
631 if (send_lq)
632 iwl_send_lq_cmd(priv, ctx, &lq,
633 CMD_SYNC, true);
634 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
635 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
636 }
637 }
638
639 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
640 if (!found)
641 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
642 "no stations to be restored.\n");
643 else
644 IWL_DEBUG_INFO(priv, "Restoring all known stations .... "
645 "complete.\n");
646}
647
648void iwl_reprogram_ap_sta(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
649{
650 unsigned long flags;
651 int sta_id = ctx->ap_sta_id;
652 int ret;
653 struct iwl_addsta_cmd sta_cmd;
654 struct iwl_link_quality_cmd lq;
aed0fd4a 655 bool active, have_lq = false;
c745f55b
WYG
656
657 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
658 if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
659 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
660 return;
661 }
662
663 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
664 sta_cmd.mode = 0;
aed0fd4a
JB
665 if (priv->stations[sta_id].lq) {
666 memcpy(&lq, priv->stations[sta_id].lq, sizeof(lq));
667 have_lq = true;
668 }
c745f55b
WYG
669
670 active = priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE;
671 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
672 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
673
674 if (active) {
675 ret = iwl_send_remove_station(
676 priv, priv->stations[sta_id].sta.sta.addr,
677 sta_id, true);
678 if (ret)
679 IWL_ERR(priv, "failed to remove STA %pM (%d)\n",
680 priv->stations[sta_id].sta.sta.addr, ret);
681 }
682 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
683 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
684 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
685
686 ret = iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
687 if (ret)
688 IWL_ERR(priv, "failed to re-add STA %pM (%d)\n",
689 priv->stations[sta_id].sta.sta.addr, ret);
aed0fd4a
JB
690 if (have_lq)
691 iwl_send_lq_cmd(priv, ctx, &lq, CMD_SYNC, true);
c745f55b
WYG
692}
693
694int iwl_get_free_ucode_key_offset(struct iwl_priv *priv)
695{
696 int i;
697
698 for (i = 0; i < priv->sta_key_max_num; i++)
699 if (!test_and_set_bit(i, &priv->ucode_key_table))
700 return i;
701
702 return WEP_INVALID_OFFSET;
703}
704
705void iwl_dealloc_bcast_stations(struct iwl_priv *priv)
706{
707 unsigned long flags;
708 int i;
709
710 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
711 for (i = 0; i < IWLAGN_STATION_COUNT; i++) {
712 if (!(priv->stations[i].used & IWL_STA_BCAST))
713 continue;
714
715 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
716 priv->num_stations--;
717 if (WARN_ON(priv->num_stations < 0))
718 priv->num_stations = 0;
719 kfree(priv->stations[i].lq);
720 priv->stations[i].lq = NULL;
721 }
722 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
723}
724
725#ifdef CONFIG_IWLWIFI_DEBUG
726static void iwl_dump_lq_cmd(struct iwl_priv *priv,
727 struct iwl_link_quality_cmd *lq)
728{
729 int i;
730 IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
731 IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
732 lq->general_params.single_stream_ant_msk,
733 lq->general_params.dual_stream_ant_msk);
734
735 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
736 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
737 i, lq->rs_table[i].rate_n_flags);
738}
739#else
740static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
741 struct iwl_link_quality_cmd *lq)
742{
743}
744#endif
745
746/**
747 * is_lq_table_valid() - Test one aspect of LQ cmd for validity
748 *
749 * It sometimes happens when a HT rate has been in use and we
750 * loose connectivity with AP then mac80211 will first tell us that the
751 * current channel is not HT anymore before removing the station. In such a
752 * scenario the RXON flags will be updated to indicate we are not
753 * communicating HT anymore, but the LQ command may still contain HT rates.
754 * Test for this to prevent driver from sending LQ command between the time
755 * RXON flags are updated and when LQ command is updated.
756 */
757static bool is_lq_table_valid(struct iwl_priv *priv,
758 struct iwl_rxon_context *ctx,
759 struct iwl_link_quality_cmd *lq)
760{
761 int i;
762
763 if (ctx->ht.enabled)
764 return true;
765
766 IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
767 ctx->active.channel);
768 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
769 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
770 RATE_MCS_HT_MSK) {
771 IWL_DEBUG_INFO(priv,
772 "index %d of LQ expects HT channel\n",
773 i);
774 return false;
775 }
776 }
777 return true;
778}
779
780/**
781 * iwl_send_lq_cmd() - Send link quality command
782 * @init: This command is sent as part of station initialization right
783 * after station has been added.
784 *
785 * The link quality command is sent as the last step of station creation.
786 * This is the special case in which init is set and we call a callback in
787 * this case to clear the state indicating that station creation is in
788 * progress.
789 */
790int iwl_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
791 struct iwl_link_quality_cmd *lq, u8 flags, bool init)
792{
793 int ret = 0;
794 unsigned long flags_spin;
795
796 struct iwl_host_cmd cmd = {
797 .id = REPLY_TX_LINK_QUALITY_CMD,
798 .len = { sizeof(struct iwl_link_quality_cmd), },
799 .flags = flags,
800 .data = { lq, },
801 };
802
803 if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
804 return -EINVAL;
805
806
807 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
808 if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
809 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
810 return -EINVAL;
811 }
812 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
813
814 iwl_dump_lq_cmd(priv, lq);
815 if (WARN_ON(init && (cmd.flags & CMD_ASYNC)))
816 return -EINVAL;
817
818 if (is_lq_table_valid(priv, ctx, lq))
819 ret = iwl_trans_send_cmd(trans(priv), &cmd);
820 else
821 ret = -EINVAL;
822
823 if (cmd.flags & CMD_ASYNC)
824 return ret;
825
826 if (init) {
827 IWL_DEBUG_INFO(priv, "init LQ command complete, "
828 "clearing sta addition status for sta %d\n",
829 lq->sta_id);
830 spin_lock_irqsave(&priv->shrd->sta_lock, flags_spin);
831 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
832 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags_spin);
833 }
834 return ret;
835}
836
c745f55b 837
c079166e
JB
838void iwl_sta_fill_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
839 u8 sta_id, struct iwl_link_quality_cmd *link_cmd)
a30e3112
JB
840{
841 int i, r;
a30e3112
JB
842 u32 rate_flags = 0;
843 __le32 rate_n_flags;
844
6ac2f839 845 lockdep_assert_held(&priv->shrd->mutex);
f775aa06 846
c079166e
JB
847 memset(link_cmd, 0, sizeof(*link_cmd));
848
a30e3112
JB
849 /* Set up the rate scaling to start at selected rate, fall back
850 * all the way down to 1M in IEEE order, and then spin on 1M */
851 if (priv->band == IEEE80211_BAND_5GHZ)
852 r = IWL_RATE_6M_INDEX;
f775aa06
JB
853 else if (ctx && ctx->vif && ctx->vif->p2p)
854 r = IWL_RATE_6M_INDEX;
a30e3112
JB
855 else
856 r = IWL_RATE_1M_INDEX;
857
858 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
859 rate_flags |= RATE_MCS_CCK_MSK;
860
d6189124 861 rate_flags |= first_antenna(hw_params(priv).valid_tx_ant) <<
a30e3112
JB
862 RATE_MCS_ANT_POS;
863 rate_n_flags = iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
864 for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
865 link_cmd->rs_table[i].rate_n_flags = rate_n_flags;
866
867 link_cmd->general_params.single_stream_ant_msk =
d6189124 868 first_antenna(hw_params(priv).valid_tx_ant);
a30e3112
JB
869
870 link_cmd->general_params.dual_stream_ant_msk =
d6189124
EG
871 hw_params(priv).valid_tx_ant &
872 ~first_antenna(hw_params(priv).valid_tx_ant);
a30e3112
JB
873 if (!link_cmd->general_params.dual_stream_ant_msk) {
874 link_cmd->general_params.dual_stream_ant_msk = ANT_AB;
d6189124 875 } else if (num_of_ant(hw_params(priv).valid_tx_ant) == 2) {
a30e3112 876 link_cmd->general_params.dual_stream_ant_msk =
d6189124 877 hw_params(priv).valid_tx_ant;
a30e3112
JB
878 }
879
c745f55b
WYG
880 link_cmd->agg_params.agg_dis_start_th =
881 LINK_QUAL_AGG_DISABLE_START_DEF;
a30e3112
JB
882 link_cmd->agg_params.agg_time_limit =
883 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
884
885 link_cmd->sta_id = sta_id;
c079166e
JB
886}
887
888static struct iwl_link_quality_cmd *
c745f55b
WYG
889iwl_sta_alloc_lq(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
890 u8 sta_id)
c079166e
JB
891{
892 struct iwl_link_quality_cmd *link_cmd;
893
894 link_cmd = kzalloc(sizeof(struct iwl_link_quality_cmd), GFP_KERNEL);
895 if (!link_cmd) {
896 IWL_ERR(priv, "Unable to allocate memory for LQ cmd.\n");
897 return NULL;
898 }
899
900 iwl_sta_fill_lq(priv, ctx, sta_id, link_cmd);
a30e3112
JB
901
902 return link_cmd;
903}
904
905/*
906 * iwlagn_add_bssid_station - Add the special IBSS BSSID station
907 *
908 * Function sleeps.
909 */
c745f55b
WYG
910int iwlagn_add_bssid_station(struct iwl_priv *priv,
911 struct iwl_rxon_context *ctx,
a30e3112
JB
912 const u8 *addr, u8 *sta_id_r)
913{
914 int ret;
915 u8 sta_id;
916 struct iwl_link_quality_cmd *link_cmd;
917 unsigned long flags;
918
919 if (sta_id_r)
920 *sta_id_r = IWL_INVALID_STATION;
921
922 ret = iwl_add_station_common(priv, ctx, addr, 0, NULL, &sta_id);
923 if (ret) {
924 IWL_ERR(priv, "Unable to add station %pM\n", addr);
925 return ret;
926 }
927
928 if (sta_id_r)
929 *sta_id_r = sta_id;
930
f39c95e8 931 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
a30e3112 932 priv->stations[sta_id].used |= IWL_STA_LOCAL;
f39c95e8 933 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112
JB
934
935 /* Set up default rate scaling table in device's station table */
f775aa06 936 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
a30e3112 937 if (!link_cmd) {
c745f55b
WYG
938 IWL_ERR(priv,
939 "Unable to initialize rate scaling for station %pM.\n",
a30e3112
JB
940 addr);
941 return -ENOMEM;
942 }
943
944 ret = iwl_send_lq_cmd(priv, ctx, link_cmd, CMD_SYNC, true);
945 if (ret)
946 IWL_ERR(priv, "Link quality command failed (%d)\n", ret);
947
f39c95e8 948 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
a30e3112 949 priv->stations[sta_id].lq = link_cmd;
f39c95e8 950 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112
JB
951
952 return 0;
953}
954
5a3d9882
JB
955/*
956 * static WEP keys
957 *
958 * For each context, the device has a table of 4 static WEP keys
959 * (one for each key index) that is updated with the following
960 * commands.
961 */
962
a30e3112
JB
963static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv,
964 struct iwl_rxon_context *ctx,
965 bool send_if_empty)
966{
967 int i, not_empty = 0;
968 u8 buff[sizeof(struct iwl_wep_cmd) +
969 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
970 struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
971 size_t cmd_size = sizeof(struct iwl_wep_cmd);
972 struct iwl_host_cmd cmd = {
973 .id = ctx->wep_key_cmd,
3fa50738 974 .data = { wep_cmd, },
a30e3112
JB
975 .flags = CMD_SYNC,
976 };
977
978 might_sleep();
979
980 memset(wep_cmd, 0, cmd_size +
981 (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
982
983 for (i = 0; i < WEP_KEYS_MAX ; i++) {
984 wep_cmd->key[i].key_index = i;
985 if (ctx->wep_keys[i].key_size) {
986 wep_cmd->key[i].key_offset = i;
987 not_empty = 1;
988 } else {
989 wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
990 }
991
992 wep_cmd->key[i].key_size = ctx->wep_keys[i].key_size;
993 memcpy(&wep_cmd->key[i].key[3], ctx->wep_keys[i].key,
994 ctx->wep_keys[i].key_size);
995 }
996
997 wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
998 wep_cmd->num_keys = WEP_KEYS_MAX;
999
1000 cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
1001
3fa50738 1002 cmd.len[0] = cmd_size;
a30e3112
JB
1003
1004 if (not_empty || send_if_empty)
e6bb4c9c 1005 return iwl_trans_send_cmd(trans(priv), &cmd);
a30e3112
JB
1006 else
1007 return 0;
1008}
1009
1010int iwl_restore_default_wep_keys(struct iwl_priv *priv,
1011 struct iwl_rxon_context *ctx)
1012{
6ac2f839 1013 lockdep_assert_held(&priv->shrd->mutex);
a30e3112
JB
1014
1015 return iwl_send_static_wepkey_cmd(priv, ctx, false);
1016}
1017
1018int iwl_remove_default_wep_key(struct iwl_priv *priv,
1019 struct iwl_rxon_context *ctx,
1020 struct ieee80211_key_conf *keyconf)
1021{
1022 int ret;
1023
6ac2f839 1024 lockdep_assert_held(&priv->shrd->mutex);
a30e3112
JB
1025
1026 IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
1027 keyconf->keyidx);
1028
1029 memset(&ctx->wep_keys[keyconf->keyidx], 0, sizeof(ctx->wep_keys[0]));
845a9c0d 1030 if (iwl_is_rfkill(priv->shrd)) {
c745f55b
WYG
1031 IWL_DEBUG_WEP(priv,
1032 "Not sending REPLY_WEPKEY command due to RFKILL.\n");
a30e3112
JB
1033 /* but keys in device are clear anyway so return success */
1034 return 0;
1035 }
1036 ret = iwl_send_static_wepkey_cmd(priv, ctx, 1);
1037 IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
1038 keyconf->keyidx, ret);
1039
1040 return ret;
1041}
1042
1043int iwl_set_default_wep_key(struct iwl_priv *priv,
1044 struct iwl_rxon_context *ctx,
1045 struct ieee80211_key_conf *keyconf)
1046{
1047 int ret;
1048
6ac2f839 1049 lockdep_assert_held(&priv->shrd->mutex);
a30e3112
JB
1050
1051 if (keyconf->keylen != WEP_KEY_LEN_128 &&
1052 keyconf->keylen != WEP_KEY_LEN_64) {
c745f55b
WYG
1053 IWL_DEBUG_WEP(priv,
1054 "Bad WEP key length %d\n", keyconf->keylen);
a30e3112
JB
1055 return -EINVAL;
1056 }
1057
5a3d9882 1058 keyconf->hw_key_idx = IWLAGN_HW_KEY_DEFAULT;
a30e3112
JB
1059
1060 ctx->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
1061 memcpy(&ctx->wep_keys[keyconf->keyidx].key, &keyconf->key,
1062 keyconf->keylen);
1063
1064 ret = iwl_send_static_wepkey_cmd(priv, ctx, false);
1065 IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
1066 keyconf->keylen, keyconf->keyidx, ret);
1067
1068 return ret;
1069}
1070
5a3d9882
JB
1071/*
1072 * dynamic (per-station) keys
1073 *
1074 * The dynamic keys are a little more complicated. The device has
1075 * a key cache of up to STA_KEY_MAX_NUM/STA_KEY_MAX_NUM_PAN keys.
1076 * These are linked to stations by a table that contains an index
1077 * into the key table for each station/key index/{mcast,unicast},
1078 * i.e. it's basically an array of pointers like this:
1079 * key_offset_t key_mapping[NUM_STATIONS][4][2];
1080 * (it really works differently, but you can think of it as such)
1081 *
1082 * The key uploading and linking happens in the same command, the
1083 * add station command with STA_MODIFY_KEY_MASK.
1084 */
a30e3112 1085
5a3d9882
JB
1086static u8 iwlagn_key_sta_id(struct iwl_priv *priv,
1087 struct ieee80211_vif *vif,
1088 struct ieee80211_sta *sta)
1089{
1090 struct iwl_vif_priv *vif_priv = (void *)vif->drv_priv;
1091 u8 sta_id = IWL_INVALID_STATION;
1092
1093 if (sta)
1094 sta_id = iwl_sta_id(sta);
1095
1096 /*
1097 * The device expects GTKs for station interfaces to be
1098 * installed as GTKs for the AP station. If we have no
1099 * station ID, then use the ap_sta_id in that case.
1100 */
1101 if (!sta && vif && vif_priv->ctx) {
1102 switch (vif->type) {
1103 case NL80211_IFTYPE_STATION:
1104 sta_id = vif_priv->ctx->ap_sta_id;
1105 break;
1106 default:
1107 /*
1108 * In all other cases, the key will be
1109 * used either for TX only or is bound
1110 * to a station already.
1111 */
1112 break;
1113 }
1114 }
a30e3112 1115
5a3d9882 1116 return sta_id;
a30e3112
JB
1117}
1118
e1b1c087
JB
1119static int iwlagn_send_sta_key(struct iwl_priv *priv,
1120 struct ieee80211_key_conf *keyconf,
1121 u8 sta_id, u32 tkip_iv32, u16 *tkip_p1k,
1122 u32 cmd_flags)
a30e3112
JB
1123{
1124 unsigned long flags;
5a3d9882 1125 __le16 key_flags;
a30e3112 1126 struct iwl_addsta_cmd sta_cmd;
5a3d9882 1127 int i;
e1b1c087 1128
f39c95e8 1129 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
5a3d9882 1130 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
f39c95e8 1131 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112 1132
5a3d9882
JB
1133 key_flags = cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
1134 key_flags |= STA_KEY_FLG_MAP_KEY_MSK;
a30e3112 1135
5a3d9882
JB
1136 switch (keyconf->cipher) {
1137 case WLAN_CIPHER_SUITE_CCMP:
1138 key_flags |= STA_KEY_FLG_CCMP;
1139 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1140 break;
1141 case WLAN_CIPHER_SUITE_TKIP:
1142 key_flags |= STA_KEY_FLG_TKIP;
1143 sta_cmd.key.tkip_rx_tsc_byte2 = tkip_iv32;
1144 for (i = 0; i < 5; i++)
1145 sta_cmd.key.tkip_rx_ttak[i] = cpu_to_le16(tkip_p1k[i]);
1146 memcpy(sta_cmd.key.key, keyconf->key, keyconf->keylen);
1147 break;
1148 case WLAN_CIPHER_SUITE_WEP104:
1149 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
1150 /* fall through */
1151 case WLAN_CIPHER_SUITE_WEP40:
1152 key_flags |= STA_KEY_FLG_WEP;
1153 memcpy(&sta_cmd.key.key[3], keyconf->key, keyconf->keylen);
1154 break;
1155 default:
1156 WARN_ON(1);
1157 return -EINVAL;
1158 }
a30e3112 1159
5a3d9882 1160 if (!(keyconf->flags & IEEE80211_KEY_FLAG_PAIRWISE))
a30e3112
JB
1161 key_flags |= STA_KEY_MULTICAST_MSK;
1162
5a3d9882
JB
1163 /* key pointer (offset) */
1164 sta_cmd.key.key_offset = keyconf->hw_key_idx;
a30e3112 1165
5a3d9882
JB
1166 sta_cmd.key.key_flags = key_flags;
1167 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
1168 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
a30e3112 1169
5a3d9882 1170 return iwl_send_add_sta(priv, &sta_cmd, cmd_flags);
a30e3112
JB
1171}
1172
1173void iwl_update_tkip_key(struct iwl_priv *priv,
5a3d9882 1174 struct ieee80211_vif *vif,
a30e3112
JB
1175 struct ieee80211_key_conf *keyconf,
1176 struct ieee80211_sta *sta, u32 iv32, u16 *phase1key)
1177{
5a3d9882
JB
1178 u8 sta_id = iwlagn_key_sta_id(priv, vif, sta);
1179
1180 if (sta_id == IWL_INVALID_STATION)
1181 return;
a30e3112
JB
1182
1183 if (iwl_scan_cancel(priv)) {
1184 /* cancel scan failed, just live w/ bad key and rely
1185 briefly on SW decryption */
1186 return;
1187 }
1188
e1b1c087
JB
1189 iwlagn_send_sta_key(priv, keyconf, sta_id,
1190 iv32, phase1key, CMD_ASYNC);
a30e3112
JB
1191}
1192
1193int iwl_remove_dynamic_key(struct iwl_priv *priv,
1194 struct iwl_rxon_context *ctx,
1195 struct ieee80211_key_conf *keyconf,
5a3d9882 1196 struct ieee80211_sta *sta)
a30e3112
JB
1197{
1198 unsigned long flags;
a30e3112 1199 struct iwl_addsta_cmd sta_cmd;
5a3d9882
JB
1200 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1201
1202 /* if station isn't there, neither is the key */
1203 if (sta_id == IWL_INVALID_STATION)
1204 return -ENOENT;
1205
f39c95e8 1206 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
5a3d9882
JB
1207 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(sta_cmd));
1208 if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE))
1209 sta_id = IWL_INVALID_STATION;
f39c95e8 1210 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
5a3d9882
JB
1211
1212 if (sta_id == IWL_INVALID_STATION)
1213 return 0;
a30e3112 1214
6ac2f839 1215 lockdep_assert_held(&priv->shrd->mutex);
a30e3112
JB
1216
1217 ctx->key_mapping_keys--;
1218
a30e3112
JB
1219 IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1220 keyconf->keyidx, sta_id);
1221
5a3d9882
JB
1222 if (!test_and_clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table))
1223 IWL_ERR(priv, "offset %d not used in uCode key table.\n",
1224 keyconf->hw_key_idx);
a30e3112 1225
5a3d9882
JB
1226 sta_cmd.key.key_flags = STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1227 sta_cmd.key.key_offset = WEP_INVALID_OFFSET;
1228 sta_cmd.sta.modify_mask = STA_MODIFY_KEY_MASK;
1229 sta_cmd.mode = STA_CONTROL_MODIFY_MSK;
a30e3112
JB
1230
1231 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1232}
1233
5a3d9882
JB
1234int iwl_set_dynamic_key(struct iwl_priv *priv,
1235 struct iwl_rxon_context *ctx,
1236 struct ieee80211_key_conf *keyconf,
1237 struct ieee80211_sta *sta)
a30e3112 1238{
5a3d9882
JB
1239 struct ieee80211_key_seq seq;
1240 u16 p1k[5];
a30e3112 1241 int ret;
5a3d9882
JB
1242 u8 sta_id = iwlagn_key_sta_id(priv, ctx->vif, sta);
1243 const u8 *addr;
1244
1245 if (sta_id == IWL_INVALID_STATION)
1246 return -EINVAL;
a30e3112 1247
6ac2f839 1248 lockdep_assert_held(&priv->shrd->mutex);
a30e3112 1249
5a3d9882
JB
1250 keyconf->hw_key_idx = iwl_get_free_ucode_key_offset(priv);
1251 if (keyconf->hw_key_idx == WEP_INVALID_OFFSET)
1252 return -ENOSPC;
1253
a30e3112 1254 ctx->key_mapping_keys++;
a30e3112
JB
1255
1256 switch (keyconf->cipher) {
a30e3112 1257 case WLAN_CIPHER_SUITE_TKIP:
5a3d9882
JB
1258 if (sta)
1259 addr = sta->addr;
1260 else /* station mode case only */
1261 addr = ctx->active.bssid_addr;
1262
1263 /* pre-fill phase 1 key into device cache */
1264 ieee80211_get_key_rx_seq(keyconf, 0, &seq);
1265 ieee80211_get_tkip_rx_p1k(keyconf, addr, seq.tkip.iv32, p1k);
e1b1c087
JB
1266 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1267 seq.tkip.iv32, p1k, CMD_SYNC);
a30e3112 1268 break;
5a3d9882 1269 case WLAN_CIPHER_SUITE_CCMP:
a30e3112
JB
1270 case WLAN_CIPHER_SUITE_WEP40:
1271 case WLAN_CIPHER_SUITE_WEP104:
e1b1c087
JB
1272 ret = iwlagn_send_sta_key(priv, keyconf, sta_id,
1273 0, NULL, CMD_SYNC);
a30e3112
JB
1274 break;
1275 default:
5a3d9882 1276 IWL_ERR(priv, "Unknown cipher %x\n", keyconf->cipher);
a30e3112
JB
1277 ret = -EINVAL;
1278 }
1279
5a3d9882
JB
1280 if (ret) {
1281 ctx->key_mapping_keys--;
1282 clear_bit(keyconf->hw_key_idx, &priv->ucode_key_table);
1283 }
1284
1285 IWL_DEBUG_WEP(priv, "Set dynamic key: cipher=%x len=%d idx=%d sta=%pM ret=%d\n",
a30e3112 1286 keyconf->cipher, keyconf->keylen, keyconf->keyidx,
5a3d9882 1287 sta ? sta->addr : NULL, ret);
a30e3112
JB
1288
1289 return ret;
1290}
1291
1292/**
1293 * iwlagn_alloc_bcast_station - add broadcast station into driver's station table.
1294 *
1295 * This adds the broadcast station into the driver's station table
1296 * and marks it driver active, so that it will be restored to the
1297 * device at the next best time.
1298 */
1299int iwlagn_alloc_bcast_station(struct iwl_priv *priv,
1300 struct iwl_rxon_context *ctx)
1301{
1302 struct iwl_link_quality_cmd *link_cmd;
1303 unsigned long flags;
1304 u8 sta_id;
1305
f39c95e8 1306 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
a30e3112
JB
1307 sta_id = iwl_prep_station(priv, ctx, iwl_bcast_addr, false, NULL);
1308 if (sta_id == IWL_INVALID_STATION) {
1309 IWL_ERR(priv, "Unable to prepare broadcast station\n");
f39c95e8 1310 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112
JB
1311
1312 return -EINVAL;
1313 }
1314
1315 priv->stations[sta_id].used |= IWL_STA_DRIVER_ACTIVE;
1316 priv->stations[sta_id].used |= IWL_STA_BCAST;
f39c95e8 1317 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112 1318
f775aa06 1319 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
a30e3112
JB
1320 if (!link_cmd) {
1321 IWL_ERR(priv,
1322 "Unable to initialize rate scaling for bcast station.\n");
1323 return -ENOMEM;
1324 }
1325
f39c95e8 1326 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
a30e3112 1327 priv->stations[sta_id].lq = link_cmd;
f39c95e8 1328 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112
JB
1329
1330 return 0;
1331}
1332
1333/**
1334 * iwl_update_bcast_station - update broadcast station's LQ command
1335 *
1336 * Only used by iwlagn. Placed here to have all bcast station management
1337 * code together.
1338 */
f775aa06
JB
1339int iwl_update_bcast_station(struct iwl_priv *priv,
1340 struct iwl_rxon_context *ctx)
a30e3112
JB
1341{
1342 unsigned long flags;
1343 struct iwl_link_quality_cmd *link_cmd;
1344 u8 sta_id = ctx->bcast_sta_id;
1345
f775aa06 1346 link_cmd = iwl_sta_alloc_lq(priv, ctx, sta_id);
a30e3112
JB
1347 if (!link_cmd) {
1348 IWL_ERR(priv, "Unable to initialize rate scaling for bcast station.\n");
1349 return -ENOMEM;
1350 }
1351
f39c95e8 1352 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
a30e3112
JB
1353 if (priv->stations[sta_id].lq)
1354 kfree(priv->stations[sta_id].lq);
1355 else
1356 IWL_DEBUG_INFO(priv, "Bcast station rate scaling has not been initialized yet.\n");
1357 priv->stations[sta_id].lq = link_cmd;
f39c95e8 1358 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112
JB
1359
1360 return 0;
1361}
1362
1363int iwl_update_bcast_stations(struct iwl_priv *priv)
1364{
1365 struct iwl_rxon_context *ctx;
1366 int ret = 0;
1367
1368 for_each_context(priv, ctx) {
1369 ret = iwl_update_bcast_station(priv, ctx);
1370 if (ret)
1371 break;
1372 }
1373
1374 return ret;
1375}
1376
1377/**
1378 * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1379 */
1380int iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1381{
1382 unsigned long flags;
1383 struct iwl_addsta_cmd sta_cmd;
1384
6ac2f839 1385 lockdep_assert_held(&priv->shrd->mutex);
a30e3112
JB
1386
1387 /* Remove "disable" flag, to enable Tx for this TID */
f39c95e8 1388 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
a30e3112
JB
1389 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1390 priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1391 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1392 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
f39c95e8 1393 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112
JB
1394
1395 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1396}
1397
1398int iwl_sta_rx_agg_start(struct iwl_priv *priv, struct ieee80211_sta *sta,
1399 int tid, u16 ssn)
1400{
1401 unsigned long flags;
1402 int sta_id;
1403 struct iwl_addsta_cmd sta_cmd;
1404
6ac2f839 1405 lockdep_assert_held(&priv->shrd->mutex);
a30e3112
JB
1406
1407 sta_id = iwl_sta_id(sta);
1408 if (sta_id == IWL_INVALID_STATION)
1409 return -ENXIO;
1410
f39c95e8 1411 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
a30e3112
JB
1412 priv->stations[sta_id].sta.station_flags_msk = 0;
1413 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1414 priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1415 priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1416 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1417 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
f39c95e8 1418 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112
JB
1419
1420 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1421}
1422
1423int iwl_sta_rx_agg_stop(struct iwl_priv *priv, struct ieee80211_sta *sta,
1424 int tid)
1425{
1426 unsigned long flags;
1427 int sta_id;
1428 struct iwl_addsta_cmd sta_cmd;
1429
6ac2f839 1430 lockdep_assert_held(&priv->shrd->mutex);
a30e3112
JB
1431
1432 sta_id = iwl_sta_id(sta);
1433 if (sta_id == IWL_INVALID_STATION) {
1434 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1435 return -ENXIO;
1436 }
1437
f39c95e8 1438 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
a30e3112
JB
1439 priv->stations[sta_id].sta.station_flags_msk = 0;
1440 priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1441 priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1442 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1443 memcpy(&sta_cmd, &priv->stations[sta_id].sta, sizeof(struct iwl_addsta_cmd));
f39c95e8 1444 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112
JB
1445
1446 return iwl_send_add_sta(priv, &sta_cmd, CMD_SYNC);
1447}
1448
a30e3112 1449
a30e3112
JB
1450
1451void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1452{
1453 unsigned long flags;
1454
f39c95e8 1455 spin_lock_irqsave(&priv->shrd->sta_lock, flags);
a30e3112
JB
1456 priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1457 priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1458 priv->stations[sta_id].sta.sta.modify_mask =
1459 STA_MODIFY_SLEEP_TX_COUNT_MSK;
1460 priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1461 priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1462 iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
f39c95e8 1463 spin_unlock_irqrestore(&priv->shrd->sta_lock, flags);
a30e3112
JB
1464
1465}