iwlwifi: only add broadcast station once
[pandora-kernel.git] / drivers / net / wireless / iwlwifi / iwl-sta.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2010 Intel Corporation. All rights reserved.
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 #include <linux/etherdevice.h>
32
33 #include "iwl-dev.h"
34 #include "iwl-core.h"
35 #include "iwl-sta.h"
36
37 u8 iwl_find_station(struct iwl_priv *priv, const u8 *addr)
38 {
39         int i;
40         int start = 0;
41         int ret = IWL_INVALID_STATION;
42         unsigned long flags;
43
44         if ((priv->iw_mode == NL80211_IFTYPE_ADHOC) ||
45             (priv->iw_mode == NL80211_IFTYPE_AP))
46                 start = IWL_STA_ID;
47
48         if (is_broadcast_ether_addr(addr))
49                 return priv->hw_params.bcast_sta_id;
50
51         spin_lock_irqsave(&priv->sta_lock, flags);
52         for (i = start; i < priv->hw_params.max_stations; i++)
53                 if (priv->stations[i].used &&
54                     (!compare_ether_addr(priv->stations[i].sta.sta.addr,
55                                          addr))) {
56                         ret = i;
57                         goto out;
58                 }
59
60         IWL_DEBUG_ASSOC_LIMIT(priv, "can not find STA %pM total %d\n",
61                               addr, priv->num_stations);
62
63  out:
64         spin_unlock_irqrestore(&priv->sta_lock, flags);
65         return ret;
66 }
67 EXPORT_SYMBOL(iwl_find_station);
68
69 int iwl_get_ra_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
70 {
71         if (priv->iw_mode == NL80211_IFTYPE_STATION) {
72                 return IWL_AP_ID;
73         } else {
74                 u8 *da = ieee80211_get_DA(hdr);
75                 return iwl_find_station(priv, da);
76         }
77 }
78 EXPORT_SYMBOL(iwl_get_ra_sta_id);
79
80 /* priv->sta_lock must be held */
81 static void iwl_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
82 {
83
84         if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
85                 IWL_ERR(priv, "ACTIVATE a non DRIVER active station id %u addr %pM\n",
86                         sta_id, priv->stations[sta_id].sta.sta.addr);
87
88         if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
89                 IWL_DEBUG_ASSOC(priv,
90                                 "STA id %u addr %pM already present in uCode (according to driver)\n",
91                                 sta_id, priv->stations[sta_id].sta.sta.addr);
92         } else {
93                 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
94                 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
95                                 sta_id, priv->stations[sta_id].sta.sta.addr);
96         }
97 }
98
99 static void iwl_process_add_sta_resp(struct iwl_priv *priv,
100                                      struct iwl_addsta_cmd *addsta,
101                                      struct iwl_rx_packet *pkt,
102                                      bool sync)
103 {
104         u8 sta_id = addsta->sta.sta_id;
105         unsigned long flags;
106
107         if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
108                 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
109                         pkt->hdr.flags);
110                 return;
111         }
112
113         IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
114                        sta_id);
115
116         spin_lock_irqsave(&priv->sta_lock, flags);
117
118         switch (pkt->u.add_sta.status) {
119         case ADD_STA_SUCCESS_MSK:
120                 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
121                 iwl_sta_ucode_activate(priv, sta_id);
122                 break;
123         case ADD_STA_NO_ROOM_IN_TABLE:
124                 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
125                         sta_id);
126                 break;
127         case ADD_STA_NO_BLOCK_ACK_RESOURCE:
128                 IWL_ERR(priv, "Adding station %d failed, no block ack resource.\n",
129                         sta_id);
130                 break;
131         case ADD_STA_MODIFY_NON_EXIST_STA:
132                 IWL_ERR(priv, "Attempting to modify non-existing station %d \n",
133                         sta_id);
134                 break;
135         default:
136                 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
137                                 pkt->u.add_sta.status);
138                 break;
139         }
140
141         IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
142                        priv->stations[sta_id].sta.mode ==
143                        STA_CONTROL_MODIFY_MSK ?  "Modified" : "Added",
144                        sta_id, priv->stations[sta_id].sta.sta.addr);
145
146         /*
147          * XXX: The MAC address in the command buffer is often changed from
148          * the original sent to the device. That is, the MAC address
149          * written to the command buffer often is not the same MAC adress
150          * read from the command buffer when the command returns. This
151          * issue has not yet been resolved and this debugging is left to
152          * observe the problem.
153          */
154         IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
155                        priv->stations[sta_id].sta.mode ==
156                        STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
157                        addsta->sta.addr);
158
159         /*
160          * Determine if we wanted to modify or add a station,
161          * if adding a station succeeded we have some more initialization
162          * to do when using station notification. TODO
163          */
164
165         spin_unlock_irqrestore(&priv->sta_lock, flags);
166 }
167
168 static void iwl_add_sta_callback(struct iwl_priv *priv,
169                                  struct iwl_device_cmd *cmd,
170                                  struct iwl_rx_packet *pkt)
171 {
172         struct iwl_addsta_cmd *addsta =
173                 (struct iwl_addsta_cmd *)cmd->cmd.payload;
174
175         iwl_process_add_sta_resp(priv, addsta, pkt, false);
176
177 }
178
179 int iwl_send_add_sta(struct iwl_priv *priv,
180                      struct iwl_addsta_cmd *sta, u8 flags)
181 {
182         struct iwl_rx_packet *pkt = NULL;
183         int ret = 0;
184         u8 data[sizeof(*sta)];
185         struct iwl_host_cmd cmd = {
186                 .id = REPLY_ADD_STA,
187                 .flags = flags,
188                 .data = data,
189         };
190
191         if (flags & CMD_ASYNC)
192                 cmd.callback = iwl_add_sta_callback;
193         else
194                 cmd.flags |= CMD_WANT_SKB;
195
196         cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
197         ret = iwl_send_cmd(priv, &cmd);
198
199         if (ret || (flags & CMD_ASYNC))
200                 return ret;
201
202         if (ret == 0) {
203                 pkt = (struct iwl_rx_packet *)cmd.reply_page;
204                 iwl_process_add_sta_resp(priv, sta, pkt, true);
205         }
206         iwl_free_pages(priv, cmd.reply_page);
207
208         return ret;
209 }
210 EXPORT_SYMBOL(iwl_send_add_sta);
211
212 static void iwl_set_ht_add_station(struct iwl_priv *priv, u8 index,
213                                    struct ieee80211_sta_ht_cap *sta_ht_inf)
214 {
215         __le32 sta_flags;
216         u8 mimo_ps_mode;
217
218         if (!sta_ht_inf || !sta_ht_inf->ht_supported)
219                 goto done;
220
221         mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
222         IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
223                         (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
224                         "static" :
225                         (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
226                         "dynamic" : "disabled");
227
228         sta_flags = priv->stations[index].sta.station_flags;
229
230         sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
231
232         switch (mimo_ps_mode) {
233         case WLAN_HT_CAP_SM_PS_STATIC:
234                 sta_flags |= STA_FLG_MIMO_DIS_MSK;
235                 break;
236         case WLAN_HT_CAP_SM_PS_DYNAMIC:
237                 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
238                 break;
239         case WLAN_HT_CAP_SM_PS_DISABLED:
240                 break;
241         default:
242                 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
243                 break;
244         }
245
246         sta_flags |= cpu_to_le32(
247               (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
248
249         sta_flags |= cpu_to_le32(
250               (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
251
252         if (iwl_is_ht40_tx_allowed(priv, sta_ht_inf))
253                 sta_flags |= STA_FLG_HT40_EN_MSK;
254         else
255                 sta_flags &= ~STA_FLG_HT40_EN_MSK;
256
257         priv->stations[index].sta.station_flags = sta_flags;
258  done:
259         return;
260 }
261
262 /**
263  * iwl_add_station - Add station to tables in driver and device
264  */
265 u8 iwl_add_station(struct iwl_priv *priv, const u8 *addr, bool is_ap, u8 flags,
266                 struct ieee80211_sta_ht_cap *ht_info)
267 {
268         struct iwl_station_entry *station;
269         unsigned long flags_spin;
270         int i;
271         int sta_id = IWL_INVALID_STATION;
272         u16 rate;
273
274         spin_lock_irqsave(&priv->sta_lock, flags_spin);
275         if (is_ap)
276                 sta_id = IWL_AP_ID;
277         else if (is_broadcast_ether_addr(addr))
278                 sta_id = priv->hw_params.bcast_sta_id;
279         else
280                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
281                         if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
282                                                 addr)) {
283                                 sta_id = i;
284                                 break;
285                         }
286
287                         if (!priv->stations[i].used &&
288                             sta_id == IWL_INVALID_STATION)
289                                 sta_id = i;
290                 }
291
292         /* These two conditions have the same outcome, but keep them separate
293            since they have different meanings */
294         if (unlikely(sta_id == IWL_INVALID_STATION)) {
295                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
296                 return sta_id;
297         }
298
299         if (priv->stations[sta_id].used &&
300             !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
301                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
302                 return sta_id;
303         }
304
305
306         station = &priv->stations[sta_id];
307         station->used = IWL_STA_DRIVER_ACTIVE;
308         IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
309                         sta_id, addr);
310         priv->num_stations++;
311
312         /* Set up the REPLY_ADD_STA command to send to device */
313         memset(&station->sta, 0, sizeof(struct iwl_addsta_cmd));
314         memcpy(station->sta.sta.addr, addr, ETH_ALEN);
315         station->sta.mode = 0;
316         station->sta.sta.sta_id = sta_id;
317         station->sta.station_flags = 0;
318
319         /* BCAST station and IBSS stations do not work in HT mode */
320         if (sta_id != priv->hw_params.bcast_sta_id &&
321             priv->iw_mode != NL80211_IFTYPE_ADHOC)
322                 iwl_set_ht_add_station(priv, sta_id, ht_info);
323
324         /* 3945 only */
325         rate = (priv->band == IEEE80211_BAND_5GHZ) ?
326                 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
327         /* Turn on both antennas for the station... */
328         station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
329
330         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
331
332         /* Add station to device's station table */
333         iwl_send_add_sta(priv, &station->sta, flags);
334         return sta_id;
335
336 }
337 EXPORT_SYMBOL(iwl_add_station);
338
339 static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, const u8 *addr)
340 {
341         unsigned long flags;
342         u8 sta_id = iwl_find_station(priv, addr);
343
344         BUG_ON(sta_id == IWL_INVALID_STATION);
345
346         IWL_DEBUG_ASSOC(priv, "Removed STA from Ucode: %pM\n", addr);
347
348         spin_lock_irqsave(&priv->sta_lock, flags);
349
350         /* Ucode must be active and driver must be non active */
351         if (priv->stations[sta_id].used != IWL_STA_UCODE_ACTIVE)
352                 IWL_ERR(priv, "removed non active STA %d\n", sta_id);
353
354         priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
355
356         memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
357         spin_unlock_irqrestore(&priv->sta_lock, flags);
358 }
359
360 static void iwl_remove_sta_callback(struct iwl_priv *priv,
361                                     struct iwl_device_cmd *cmd,
362                                     struct iwl_rx_packet *pkt)
363 {
364         struct iwl_rem_sta_cmd *rm_sta =
365                         (struct iwl_rem_sta_cmd *)cmd->cmd.payload;
366         const u8 *addr = rm_sta->addr;
367
368         if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
369                 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
370                 pkt->hdr.flags);
371                 return;
372         }
373
374         switch (pkt->u.rem_sta.status) {
375         case REM_STA_SUCCESS_MSK:
376                 iwl_sta_ucode_deactivate(priv, addr);
377                 break;
378         default:
379                 IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
380                 break;
381         }
382 }
383
384 static int iwl_send_remove_station(struct iwl_priv *priv, const u8 *addr,
385                                    u8 flags)
386 {
387         struct iwl_rx_packet *pkt;
388         int ret;
389
390         struct iwl_rem_sta_cmd rm_sta_cmd;
391
392         struct iwl_host_cmd cmd = {
393                 .id = REPLY_REMOVE_STA,
394                 .len = sizeof(struct iwl_rem_sta_cmd),
395                 .flags = flags,
396                 .data = &rm_sta_cmd,
397         };
398
399         memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
400         rm_sta_cmd.num_sta = 1;
401         memcpy(&rm_sta_cmd.addr, addr , ETH_ALEN);
402
403         if (flags & CMD_ASYNC)
404                 cmd.callback = iwl_remove_sta_callback;
405         else
406                 cmd.flags |= CMD_WANT_SKB;
407         ret = iwl_send_cmd(priv, &cmd);
408
409         if (ret || (flags & CMD_ASYNC))
410                 return ret;
411
412         pkt = (struct iwl_rx_packet *)cmd.reply_page;
413         if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
414                 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
415                           pkt->hdr.flags);
416                 ret = -EIO;
417         }
418
419         if (!ret) {
420                 switch (pkt->u.rem_sta.status) {
421                 case REM_STA_SUCCESS_MSK:
422                         iwl_sta_ucode_deactivate(priv, addr);
423                         IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
424                         break;
425                 default:
426                         ret = -EIO;
427                         IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
428                         break;
429                 }
430         }
431         iwl_free_pages(priv, cmd.reply_page);
432
433         return ret;
434 }
435
436 /**
437  * iwl_remove_station - Remove driver's knowledge of station.
438  */
439 int iwl_remove_station(struct iwl_priv *priv, const u8 *addr, bool is_ap)
440 {
441         int sta_id = IWL_INVALID_STATION;
442         int i, ret = -EINVAL;
443         unsigned long flags;
444
445         spin_lock_irqsave(&priv->sta_lock, flags);
446
447         if (is_ap)
448                 sta_id = IWL_AP_ID;
449         else if (is_broadcast_ether_addr(addr))
450                 sta_id = priv->hw_params.bcast_sta_id;
451         else
452                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
453                         if (priv->stations[i].used &&
454                             !compare_ether_addr(priv->stations[i].sta.sta.addr,
455                                                 addr)) {
456                                 sta_id = i;
457                                 break;
458                         }
459
460         if (unlikely(sta_id == IWL_INVALID_STATION))
461                 goto out;
462
463         IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d  %pM\n",
464                 sta_id, addr);
465
466         if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
467                 IWL_ERR(priv, "Removing %pM but non DRIVER active\n",
468                                 addr);
469                 goto out;
470         }
471
472         if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
473                 IWL_ERR(priv, "Removing %pM but non UCODE active\n",
474                                 addr);
475                 goto out;
476         }
477
478
479         priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
480
481         priv->num_stations--;
482
483         BUG_ON(priv->num_stations < 0);
484
485         spin_unlock_irqrestore(&priv->sta_lock, flags);
486
487         ret = iwl_send_remove_station(priv, addr, CMD_ASYNC);
488         return ret;
489 out:
490         spin_unlock_irqrestore(&priv->sta_lock, flags);
491         return ret;
492 }
493
494 /**
495  * iwl_clear_ucode_stations() - clear entire station table driver and/or ucode
496  * @priv:
497  * @force: If set then the uCode station table needs to be cleared here. If
498  *         not set then the uCode station table has already been cleared,
499  *         for example after sending it a RXON command without ASSOC bit
500  *         set, and we just need to change driver state here.
501  */
502 void iwl_clear_ucode_stations(struct iwl_priv *priv, bool force)
503 {
504         int i;
505         unsigned long flags_spin;
506         bool cleared = false;
507
508         IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver%s\n",
509                         force ? " and ucode" : "");
510
511         if (force) {
512                 if (!iwl_is_ready(priv)) {
513                         /*
514                          * If device is not ready at this point the station
515                          * table is likely already empty (uCode not ready
516                          * to receive station requests) or will soon be
517                          * due to interface going down.
518                          */
519                         IWL_DEBUG_INFO(priv, "Unable to remove stations from device - device not ready\n");
520                 } else {
521                         iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL);
522                 }
523         }
524
525         spin_lock_irqsave(&priv->sta_lock, flags_spin);
526         if (force) {
527                 IWL_DEBUG_INFO(priv, "Clearing all station information in driver\n");
528                 priv->num_stations = 0;
529                 memset(priv->stations, 0, sizeof(priv->stations));
530         } else {
531                 for (i = 0; i < priv->hw_params.max_stations; i++) {
532                         if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
533                                 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d \n", i);
534                                 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
535                                 cleared = true;
536                         }
537                 }
538         }
539         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
540
541         if (!cleared)
542                 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
543 }
544 EXPORT_SYMBOL(iwl_clear_ucode_stations);
545
546 /**
547  * iwl_restore_stations() - Restore driver known stations to device
548  *
549  * All stations considered active by driver, but not present in ucode, is
550  * restored.
551  */
552 void iwl_restore_stations(struct iwl_priv *priv)
553 {
554         unsigned long flags_spin;
555         int i;
556         bool found = false;
557
558         if (!iwl_is_ready(priv)) {
559                 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
560                 return;
561         }
562
563         IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
564         spin_lock_irqsave(&priv->sta_lock, flags_spin);
565         for (i = 0; i < priv->hw_params.max_stations; i++) {
566                 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
567                             !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
568                         IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
569                                         priv->stations[i].sta.sta.addr);
570                         priv->stations[i].sta.mode = 0;
571                         priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
572                         found = true;
573                 }
574         }
575
576         for (i = 0; i < priv->hw_params.max_stations; i++) {
577                 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
578                         iwl_send_add_sta(priv, &priv->stations[i].sta,
579                                          CMD_ASYNC);
580                         priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
581                 }
582         }
583
584         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
585         if (!found)
586                 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
587         else
588                 IWL_DEBUG_INFO(priv, "Restoring all known stations .... in progress.\n");
589 }
590 EXPORT_SYMBOL(iwl_restore_stations);
591
592 int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
593 {
594         int i;
595
596         for (i = 0; i < STA_KEY_MAX_NUM; i++)
597                 if (!test_and_set_bit(i, &priv->ucode_key_table))
598                         return i;
599
600         return WEP_INVALID_OFFSET;
601 }
602 EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
603
604 int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
605 {
606         int i, not_empty = 0;
607         u8 buff[sizeof(struct iwl_wep_cmd) +
608                 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
609         struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
610         size_t cmd_size  = sizeof(struct iwl_wep_cmd);
611         struct iwl_host_cmd cmd = {
612                 .id = REPLY_WEPKEY,
613                 .data = wep_cmd,
614                 .flags = CMD_SYNC,
615         };
616
617         might_sleep();
618
619         memset(wep_cmd, 0, cmd_size +
620                         (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
621
622         for (i = 0; i < WEP_KEYS_MAX ; i++) {
623                 wep_cmd->key[i].key_index = i;
624                 if (priv->wep_keys[i].key_size) {
625                         wep_cmd->key[i].key_offset = i;
626                         not_empty = 1;
627                 } else {
628                         wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
629                 }
630
631                 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
632                 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
633                                 priv->wep_keys[i].key_size);
634         }
635
636         wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
637         wep_cmd->num_keys = WEP_KEYS_MAX;
638
639         cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
640
641         cmd.len = cmd_size;
642
643         if (not_empty || send_if_empty)
644                 return iwl_send_cmd(priv, &cmd);
645         else
646                 return 0;
647 }
648 EXPORT_SYMBOL(iwl_send_static_wepkey_cmd);
649
650 int iwl_remove_default_wep_key(struct iwl_priv *priv,
651                                struct ieee80211_key_conf *keyconf)
652 {
653         int ret;
654
655         WARN_ON(!mutex_is_locked(&priv->mutex));
656
657         IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
658                       keyconf->keyidx);
659
660         if (!test_and_clear_bit(keyconf->keyidx, &priv->ucode_key_table))
661                 IWL_ERR(priv, "index %d not used in uCode key table.\n",
662                           keyconf->keyidx);
663
664         priv->default_wep_key--;
665         memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
666         if (iwl_is_rfkill(priv)) {
667                 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
668                 /* but keys in device are clear anyway so return success */
669                 return 0;
670         }
671         ret = iwl_send_static_wepkey_cmd(priv, 1);
672         IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
673                       keyconf->keyidx, ret);
674
675         return ret;
676 }
677 EXPORT_SYMBOL(iwl_remove_default_wep_key);
678
679 int iwl_set_default_wep_key(struct iwl_priv *priv,
680                             struct ieee80211_key_conf *keyconf)
681 {
682         int ret;
683
684         WARN_ON(!mutex_is_locked(&priv->mutex));
685
686         if (keyconf->keylen != WEP_KEY_LEN_128 &&
687             keyconf->keylen != WEP_KEY_LEN_64) {
688                 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
689                 return -EINVAL;
690         }
691
692         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
693         keyconf->hw_key_idx = HW_KEY_DEFAULT;
694         priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
695
696         priv->default_wep_key++;
697
698         if (test_and_set_bit(keyconf->keyidx, &priv->ucode_key_table))
699                 IWL_ERR(priv, "index %d already used in uCode key table.\n",
700                           keyconf->keyidx);
701
702         priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
703         memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
704                                                         keyconf->keylen);
705
706         ret = iwl_send_static_wepkey_cmd(priv, 0);
707         IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
708                 keyconf->keylen, keyconf->keyidx, ret);
709
710         return ret;
711 }
712 EXPORT_SYMBOL(iwl_set_default_wep_key);
713
714 static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
715                                 struct ieee80211_key_conf *keyconf,
716                                 u8 sta_id)
717 {
718         unsigned long flags;
719         __le16 key_flags = 0;
720         int ret;
721
722         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
723
724         key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
725         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
726         key_flags &= ~STA_KEY_FLG_INVALID;
727
728         if (keyconf->keylen == WEP_KEY_LEN_128)
729                 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
730
731         if (sta_id == priv->hw_params.bcast_sta_id)
732                 key_flags |= STA_KEY_MULTICAST_MSK;
733
734         spin_lock_irqsave(&priv->sta_lock, flags);
735
736         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
737         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
738         priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
739
740         memcpy(priv->stations[sta_id].keyinfo.key,
741                                 keyconf->key, keyconf->keylen);
742
743         memcpy(&priv->stations[sta_id].sta.key.key[3],
744                                 keyconf->key, keyconf->keylen);
745
746         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
747                         == STA_KEY_FLG_NO_ENC)
748                 priv->stations[sta_id].sta.key.key_offset =
749                                  iwl_get_free_ucode_key_index(priv);
750         /* else, we are overriding an existing key => no need to allocated room
751          * in uCode. */
752
753         WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
754                 "no space for a new key");
755
756         priv->stations[sta_id].sta.key.key_flags = key_flags;
757         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
758         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
759
760         ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
761
762         spin_unlock_irqrestore(&priv->sta_lock, flags);
763
764         return ret;
765 }
766
767 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
768                                    struct ieee80211_key_conf *keyconf,
769                                    u8 sta_id)
770 {
771         unsigned long flags;
772         __le16 key_flags = 0;
773         int ret;
774
775         key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
776         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
777         key_flags &= ~STA_KEY_FLG_INVALID;
778
779         if (sta_id == priv->hw_params.bcast_sta_id)
780                 key_flags |= STA_KEY_MULTICAST_MSK;
781
782         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
783
784         spin_lock_irqsave(&priv->sta_lock, flags);
785         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
786         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
787
788         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
789                keyconf->keylen);
790
791         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
792                keyconf->keylen);
793
794         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
795                         == STA_KEY_FLG_NO_ENC)
796                 priv->stations[sta_id].sta.key.key_offset =
797                                  iwl_get_free_ucode_key_index(priv);
798         /* else, we are overriding an existing key => no need to allocated room
799          * in uCode. */
800
801         WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
802                 "no space for a new key");
803
804         priv->stations[sta_id].sta.key.key_flags = key_flags;
805         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
806         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
807
808         ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
809
810         spin_unlock_irqrestore(&priv->sta_lock, flags);
811
812         return ret;
813 }
814
815 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
816                                    struct ieee80211_key_conf *keyconf,
817                                    u8 sta_id)
818 {
819         unsigned long flags;
820         int ret = 0;
821         __le16 key_flags = 0;
822
823         key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
824         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
825         key_flags &= ~STA_KEY_FLG_INVALID;
826
827         if (sta_id == priv->hw_params.bcast_sta_id)
828                 key_flags |= STA_KEY_MULTICAST_MSK;
829
830         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
831         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
832
833         spin_lock_irqsave(&priv->sta_lock, flags);
834
835         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
836         priv->stations[sta_id].keyinfo.keylen = 16;
837
838         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
839                         == STA_KEY_FLG_NO_ENC)
840                 priv->stations[sta_id].sta.key.key_offset =
841                                  iwl_get_free_ucode_key_index(priv);
842         /* else, we are overriding an existing key => no need to allocated room
843          * in uCode. */
844
845         WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
846                 "no space for a new key");
847
848         priv->stations[sta_id].sta.key.key_flags = key_flags;
849
850
851         /* This copy is acutally not needed: we get the key with each TX */
852         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
853
854         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
855
856         spin_unlock_irqrestore(&priv->sta_lock, flags);
857
858         return ret;
859 }
860
861 void iwl_update_tkip_key(struct iwl_priv *priv,
862                         struct ieee80211_key_conf *keyconf,
863                         const u8 *addr, u32 iv32, u16 *phase1key)
864 {
865         u8 sta_id = IWL_INVALID_STATION;
866         unsigned long flags;
867         int i;
868
869         sta_id = iwl_find_station(priv, addr);
870         if (sta_id == IWL_INVALID_STATION) {
871                 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
872                                    addr);
873                 return;
874         }
875
876         if (iwl_scan_cancel(priv)) {
877                 /* cancel scan failed, just live w/ bad key and rely
878                    briefly on SW decryption */
879                 return;
880         }
881
882         spin_lock_irqsave(&priv->sta_lock, flags);
883
884         priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
885
886         for (i = 0; i < 5; i++)
887                 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
888                         cpu_to_le16(phase1key[i]);
889
890         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
891         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
892
893         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
894
895         spin_unlock_irqrestore(&priv->sta_lock, flags);
896
897 }
898 EXPORT_SYMBOL(iwl_update_tkip_key);
899
900 int iwl_remove_dynamic_key(struct iwl_priv *priv,
901                                 struct ieee80211_key_conf *keyconf,
902                                 u8 sta_id)
903 {
904         unsigned long flags;
905         int ret = 0;
906         u16 key_flags;
907         u8 keyidx;
908
909         priv->key_mapping_key--;
910
911         spin_lock_irqsave(&priv->sta_lock, flags);
912         key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
913         keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
914
915         IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
916                       keyconf->keyidx, sta_id);
917
918         if (keyconf->keyidx != keyidx) {
919                 /* We need to remove a key with index different that the one
920                  * in the uCode. This means that the key we need to remove has
921                  * been replaced by another one with different index.
922                  * Don't do anything and return ok
923                  */
924                 spin_unlock_irqrestore(&priv->sta_lock, flags);
925                 return 0;
926         }
927
928         if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
929                 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
930                             keyconf->keyidx, key_flags);
931                 spin_unlock_irqrestore(&priv->sta_lock, flags);
932                 return 0;
933         }
934
935         if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
936                 &priv->ucode_key_table))
937                 IWL_ERR(priv, "index %d not used in uCode key table.\n",
938                         priv->stations[sta_id].sta.key.key_offset);
939         memset(&priv->stations[sta_id].keyinfo, 0,
940                                         sizeof(struct iwl_hw_key));
941         memset(&priv->stations[sta_id].sta.key, 0,
942                                         sizeof(struct iwl4965_keyinfo));
943         priv->stations[sta_id].sta.key.key_flags =
944                         STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
945         priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
946         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
947         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
948
949         if (iwl_is_rfkill(priv)) {
950                 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled. \n");
951                 spin_unlock_irqrestore(&priv->sta_lock, flags);
952                 return 0;
953         }
954         ret =  iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
955         spin_unlock_irqrestore(&priv->sta_lock, flags);
956         return ret;
957 }
958 EXPORT_SYMBOL(iwl_remove_dynamic_key);
959
960 int iwl_set_dynamic_key(struct iwl_priv *priv,
961                                 struct ieee80211_key_conf *keyconf, u8 sta_id)
962 {
963         int ret;
964
965         priv->key_mapping_key++;
966         keyconf->hw_key_idx = HW_KEY_DYNAMIC;
967
968         switch (keyconf->alg) {
969         case ALG_CCMP:
970                 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
971                 break;
972         case ALG_TKIP:
973                 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
974                 break;
975         case ALG_WEP:
976                 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
977                 break;
978         default:
979                 IWL_ERR(priv,
980                         "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
981                 ret = -EINVAL;
982         }
983
984         IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
985                       keyconf->alg, keyconf->keylen, keyconf->keyidx,
986                       sta_id, ret);
987
988         return ret;
989 }
990 EXPORT_SYMBOL(iwl_set_dynamic_key);
991
992 #ifdef CONFIG_IWLWIFI_DEBUG
993 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
994                            struct iwl_link_quality_cmd *lq)
995 {
996         int i;
997         IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
998         IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
999                        lq->general_params.single_stream_ant_msk,
1000                        lq->general_params.dual_stream_ant_msk);
1001
1002         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
1003                 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
1004                                i, lq->rs_table[i].rate_n_flags);
1005 }
1006 #else
1007 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1008                                    struct iwl_link_quality_cmd *lq)
1009 {
1010 }
1011 #endif
1012
1013 int iwl_send_lq_cmd(struct iwl_priv *priv,
1014                     struct iwl_link_quality_cmd *lq, u8 flags)
1015 {
1016         struct iwl_host_cmd cmd = {
1017                 .id = REPLY_TX_LINK_QUALITY_CMD,
1018                 .len = sizeof(struct iwl_link_quality_cmd),
1019                 .flags = flags,
1020                 .data = lq,
1021         };
1022
1023         if ((lq->sta_id == 0xFF) &&
1024             (priv->iw_mode == NL80211_IFTYPE_ADHOC))
1025                 return -EINVAL;
1026
1027         if (lq->sta_id == 0xFF)
1028                 lq->sta_id = IWL_AP_ID;
1029
1030         iwl_dump_lq_cmd(priv, lq);
1031
1032         if (iwl_is_associated(priv) && priv->assoc_station_added)
1033                 return  iwl_send_cmd(priv, &cmd);
1034
1035         return 0;
1036 }
1037 EXPORT_SYMBOL(iwl_send_lq_cmd);
1038
1039 /**
1040  * iwl_sta_init_lq - Initialize a station's hardware rate table
1041  *
1042  * The uCode's station table contains a table of fallback rates
1043  * for automatic fallback during transmission.
1044  *
1045  * NOTE: This sets up a default set of values.  These will be replaced later
1046  *       if the driver's iwl-agn-rs rate scaling algorithm is used, instead of
1047  *       rc80211_simple.
1048  *
1049  * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
1050  *       calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
1051  *       which requires station table entry to exist).
1052  */
1053 static void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, bool is_ap)
1054 {
1055         int i, r;
1056         struct iwl_link_quality_cmd link_cmd = {
1057                 .reserved1 = 0,
1058         };
1059         u32 rate_flags;
1060
1061         /* Set up the rate scaling to start at selected rate, fall back
1062          * all the way down to 1M in IEEE order, and then spin on 1M */
1063         if (is_ap)
1064                 r = IWL_RATE_54M_INDEX;
1065         else if (priv->band == IEEE80211_BAND_5GHZ)
1066                 r = IWL_RATE_6M_INDEX;
1067         else
1068                 r = IWL_RATE_1M_INDEX;
1069
1070         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1071                 rate_flags = 0;
1072                 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
1073                         rate_flags |= RATE_MCS_CCK_MSK;
1074
1075                 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
1076                                 RATE_MCS_ANT_POS;
1077
1078                 link_cmd.rs_table[i].rate_n_flags =
1079                         iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
1080                 r = iwl_get_prev_ieee_rate(r);
1081         }
1082
1083         link_cmd.general_params.single_stream_ant_msk =
1084                                 first_antenna(priv->hw_params.valid_tx_ant);
1085         link_cmd.general_params.dual_stream_ant_msk = 3;
1086         link_cmd.agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
1087         link_cmd.agg_params.agg_time_limit =
1088                 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
1089
1090         /* Update the rate scaling for control frame Tx to AP */
1091         link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
1092
1093         iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
1094                                sizeof(link_cmd), &link_cmd, NULL);
1095 }
1096
1097 /**
1098  * iwl_rxon_add_station - add station into station table.
1099  *
1100  * there is only one AP station with id= IWL_AP_ID
1101  * NOTE: mutex must be held before calling this function
1102  */
1103 int iwl_rxon_add_station(struct iwl_priv *priv, const u8 *addr, bool is_ap)
1104 {
1105         struct ieee80211_sta *sta;
1106         struct ieee80211_sta_ht_cap ht_config;
1107         struct ieee80211_sta_ht_cap *cur_ht_config = NULL;
1108         u8 sta_id;
1109
1110         /*
1111          * Set HT capabilities. It is ok to set this struct even if not using
1112          * HT config: the priv->current_ht_config.is_ht flag will just be false
1113          */
1114         rcu_read_lock();
1115         sta = ieee80211_find_sta(priv->vif, addr);
1116         if (sta) {
1117                 memcpy(&ht_config, &sta->ht_cap, sizeof(ht_config));
1118                 cur_ht_config = &ht_config;
1119         }
1120         rcu_read_unlock();
1121
1122         /* Add station to device's station table */
1123         sta_id = iwl_add_station(priv, addr, is_ap, CMD_SYNC, cur_ht_config);
1124
1125         /* Set up default rate scaling table in device's station table */
1126         iwl_sta_init_lq(priv, addr, is_ap);
1127
1128         return sta_id;
1129 }
1130 EXPORT_SYMBOL(iwl_rxon_add_station);
1131
1132 /**
1133  * iwl_sta_init_bcast_lq - Initialize a bcast station's hardware rate table
1134  *
1135  * NOTE: Run REPLY_ADD_STA command to set up station table entry, before
1136  *       calling this function (which runs REPLY_TX_LINK_QUALITY_CMD,
1137  *       which requires station table entry to exist).
1138  */
1139 static void iwl_sta_init_bcast_lq(struct iwl_priv *priv)
1140 {
1141         int i, r;
1142         struct iwl_link_quality_cmd link_cmd = {
1143                 .reserved1 = 0,
1144         };
1145         u32 rate_flags;
1146
1147         /* Set up the rate scaling to start at selected rate, fall back
1148          * all the way down to 1M in IEEE order, and then spin on 1M */
1149         if (priv->band == IEEE80211_BAND_5GHZ)
1150                 r = IWL_RATE_6M_INDEX;
1151         else
1152                 r = IWL_RATE_1M_INDEX;
1153
1154         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
1155                 rate_flags = 0;
1156                 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
1157                         rate_flags |= RATE_MCS_CCK_MSK;
1158
1159                 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
1160                                 RATE_MCS_ANT_POS;
1161
1162                 link_cmd.rs_table[i].rate_n_flags =
1163                         iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
1164                 r = iwl_get_prev_ieee_rate(r);
1165         }
1166
1167         link_cmd.general_params.single_stream_ant_msk =
1168                                 first_antenna(priv->hw_params.valid_tx_ant);
1169         link_cmd.general_params.dual_stream_ant_msk = 3;
1170         link_cmd.agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
1171         link_cmd.agg_params.agg_time_limit =
1172                 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
1173
1174         /* Update the rate scaling for control frame Tx to AP */
1175         link_cmd.sta_id = priv->hw_params.bcast_sta_id;
1176
1177         iwl_send_cmd_pdu_async(priv, REPLY_TX_LINK_QUALITY_CMD,
1178                                sizeof(link_cmd), &link_cmd, NULL);
1179 }
1180
1181
1182 /**
1183  * iwl_add_bcast_station - add broadcast station into station table.
1184  */
1185 void iwl_add_bcast_station(struct iwl_priv *priv)
1186 {
1187         IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
1188         iwl_add_station(priv, iwl_bcast_addr, false, CMD_SYNC, NULL);
1189
1190         /* Set up default rate scaling table in device's station table */
1191         iwl_sta_init_bcast_lq(priv);
1192 }
1193 EXPORT_SYMBOL(iwl_add_bcast_station);
1194
1195 /**
1196  * iwl3945_add_bcast_station - add broadcast station into station table.
1197  */
1198 void iwl3945_add_bcast_station(struct iwl_priv *priv)
1199 {
1200         IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
1201         iwl_add_station(priv, iwl_bcast_addr, false, CMD_SYNC, NULL);
1202 }
1203 EXPORT_SYMBOL(iwl3945_add_bcast_station);
1204
1205 /**
1206  * iwl_get_sta_id - Find station's index within station table
1207  *
1208  * If new IBSS station, create new entry in station table
1209  */
1210 int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
1211 {
1212         int sta_id;
1213         __le16 fc = hdr->frame_control;
1214
1215         /* If this frame is broadcast or management, use broadcast station id */
1216         if (!ieee80211_is_data(fc) ||  is_multicast_ether_addr(hdr->addr1))
1217                 return priv->hw_params.bcast_sta_id;
1218
1219         switch (priv->iw_mode) {
1220
1221         /* If we are a client station in a BSS network, use the special
1222          * AP station entry (that's the only station we communicate with) */
1223         case NL80211_IFTYPE_STATION:
1224                 return IWL_AP_ID;
1225
1226         /* If we are an AP, then find the station, or use BCAST */
1227         case NL80211_IFTYPE_AP:
1228                 sta_id = iwl_find_station(priv, hdr->addr1);
1229                 if (sta_id != IWL_INVALID_STATION)
1230                         return sta_id;
1231                 return priv->hw_params.bcast_sta_id;
1232
1233         /* If this frame is going out to an IBSS network, find the station,
1234          * or create a new station table entry */
1235         case NL80211_IFTYPE_ADHOC:
1236                 sta_id = iwl_find_station(priv, hdr->addr1);
1237                 if (sta_id != IWL_INVALID_STATION)
1238                         return sta_id;
1239
1240                 /* Create new station table entry */
1241                 sta_id = iwl_add_station(priv, hdr->addr1, false,
1242                                         CMD_ASYNC, NULL);
1243
1244                 if (sta_id != IWL_INVALID_STATION)
1245                         return sta_id;
1246
1247                 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
1248                                "Defaulting to broadcast...\n",
1249                                hdr->addr1);
1250                 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
1251                 return priv->hw_params.bcast_sta_id;
1252
1253         default:
1254                 IWL_WARN(priv, "Unknown mode of operation: %d\n",
1255                         priv->iw_mode);
1256                 return priv->hw_params.bcast_sta_id;
1257         }
1258 }
1259 EXPORT_SYMBOL(iwl_get_sta_id);
1260
1261 /**
1262  * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1263  */
1264 void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1265 {
1266         unsigned long flags;
1267
1268         /* Remove "disable" flag, to enable Tx for this TID */
1269         spin_lock_irqsave(&priv->sta_lock, flags);
1270         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1271         priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1272         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1273         spin_unlock_irqrestore(&priv->sta_lock, flags);
1274
1275         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1276 }
1277 EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1278
1279 int iwl_sta_rx_agg_start(struct iwl_priv *priv,
1280                          const u8 *addr, int tid, u16 ssn)
1281 {
1282         unsigned long flags;
1283         int sta_id;
1284
1285         sta_id = iwl_find_station(priv, addr);
1286         if (sta_id == IWL_INVALID_STATION)
1287                 return -ENXIO;
1288
1289         spin_lock_irqsave(&priv->sta_lock, flags);
1290         priv->stations[sta_id].sta.station_flags_msk = 0;
1291         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1292         priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1293         priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1294         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1295         spin_unlock_irqrestore(&priv->sta_lock, flags);
1296
1297         return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1298                                         CMD_ASYNC);
1299 }
1300 EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1301
1302 int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
1303 {
1304         unsigned long flags;
1305         int sta_id;
1306
1307         sta_id = iwl_find_station(priv, addr);
1308         if (sta_id == IWL_INVALID_STATION) {
1309                 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1310                 return -ENXIO;
1311         }
1312
1313         spin_lock_irqsave(&priv->sta_lock, flags);
1314         priv->stations[sta_id].sta.station_flags_msk = 0;
1315         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1316         priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1317         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1318         spin_unlock_irqrestore(&priv->sta_lock, flags);
1319
1320         return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1321                                         CMD_ASYNC);
1322 }
1323 EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1324
1325 void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
1326 {
1327         unsigned long flags;
1328
1329         spin_lock_irqsave(&priv->sta_lock, flags);
1330         priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1331         priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1332         priv->stations[sta_id].sta.sta.modify_mask = 0;
1333         priv->stations[sta_id].sta.sleep_tx_count = 0;
1334         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1335         spin_unlock_irqrestore(&priv->sta_lock, flags);
1336
1337         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1338 }
1339 EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
1340
1341 void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1342 {
1343         unsigned long flags;
1344
1345         spin_lock_irqsave(&priv->sta_lock, flags);
1346         priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1347         priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1348         priv->stations[sta_id].sta.sta.modify_mask =
1349                                         STA_MODIFY_SLEEP_TX_COUNT_MSK;
1350         priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1351         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1352         spin_unlock_irqrestore(&priv->sta_lock, flags);
1353
1354         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1355 }