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