iwlwifi: fix compiler warning
[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 void iwl_sta_init_lq(struct iwl_priv *priv, const u8 *addr, bool is_ap)
422 {
423         int i, r;
424         struct iwl_link_quality_cmd link_cmd = {
425                 .reserved1 = 0,
426         };
427         u32 rate_flags;
428         int ret = 0;
429
430         /* Set up the rate scaling to start at selected rate, fall back
431          * all the way down to 1M in IEEE order, and then spin on 1M */
432         if (is_ap)
433                 r = IWL_RATE_54M_INDEX;
434         else if (priv->band == IEEE80211_BAND_5GHZ)
435                 r = IWL_RATE_6M_INDEX;
436         else
437                 r = IWL_RATE_1M_INDEX;
438
439         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
440                 rate_flags = 0;
441                 if (r >= IWL_FIRST_CCK_RATE && r <= IWL_LAST_CCK_RATE)
442                         rate_flags |= RATE_MCS_CCK_MSK;
443
444                 rate_flags |= first_antenna(priv->hw_params.valid_tx_ant) <<
445                                 RATE_MCS_ANT_POS;
446
447                 link_cmd.rs_table[i].rate_n_flags =
448                         iwl_hw_set_rate_n_flags(iwl_rates[r].plcp, rate_flags);
449                 r = iwl_get_prev_ieee_rate(r);
450         }
451
452         link_cmd.general_params.single_stream_ant_msk =
453                                 first_antenna(priv->hw_params.valid_tx_ant);
454         link_cmd.general_params.dual_stream_ant_msk = 3;
455         link_cmd.agg_params.agg_dis_start_th = LINK_QUAL_AGG_DISABLE_START_DEF;
456         link_cmd.agg_params.agg_time_limit =
457                 cpu_to_le16(LINK_QUAL_AGG_TIME_LIMIT_DEF);
458
459         /* Update the rate scaling for control frame Tx to AP */
460         link_cmd.sta_id = is_ap ? IWL_AP_ID : priv->hw_params.bcast_sta_id;
461
462         ret = iwl_send_cmd_pdu(priv, REPLY_TX_LINK_QUALITY_CMD,
463                                sizeof(link_cmd), &link_cmd);
464         if (ret)
465                 IWL_ERR(priv, "REPLY_TX_LINK_QUALITY_CMD failed (%d)\n", ret);
466 }
467
468 /*
469  * iwl_add_local_stations - Add stations not requested by mac80211
470  *
471  * This will be either the broadcast station or the bssid station needed by
472  * ad-hoc.
473  *
474  * Function sleeps.
475  */
476 int iwl_add_local_station(struct iwl_priv *priv, const u8 *addr, bool init_rs)
477 {
478         int ret;
479         u8 sta_id;
480
481         ret = iwl_add_station_common(priv, addr, 0, NULL, &sta_id);
482         if (ret) {
483                 IWL_ERR(priv, "Unable to add station %pM\n", addr);
484                 return ret;
485         }
486
487         if (init_rs)
488                 /* Set up default rate scaling table in device's station table */
489                 iwl_sta_init_lq(priv, addr, false);
490         return 0;
491 }
492 EXPORT_SYMBOL(iwl_add_local_station);
493
494 /**
495  * iwl_sta_ucode_deactivate - deactivate ucode status for a station
496  *
497  * priv->sta_lock must be held
498  */
499 static void iwl_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
500 {
501         /* Ucode must be active and driver must be non active */
502         if (priv->stations[sta_id].used != IWL_STA_UCODE_ACTIVE)
503                 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
504
505         priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
506
507         memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
508         IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
509 }
510
511 static int iwl_send_remove_station(struct iwl_priv *priv,
512                                    struct iwl_station_entry *station)
513 {
514         struct iwl_rx_packet *pkt;
515         int ret;
516
517         unsigned long flags_spin;
518         struct iwl_rem_sta_cmd rm_sta_cmd;
519
520         struct iwl_host_cmd cmd = {
521                 .id = REPLY_REMOVE_STA,
522                 .len = sizeof(struct iwl_rem_sta_cmd),
523                 .flags = CMD_SYNC,
524                 .data = &rm_sta_cmd,
525         };
526
527         memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
528         rm_sta_cmd.num_sta = 1;
529         memcpy(&rm_sta_cmd.addr, &station->sta.sta.addr , ETH_ALEN);
530
531         cmd.flags |= CMD_WANT_SKB;
532
533         ret = iwl_send_cmd(priv, &cmd);
534
535         if (ret)
536                 return ret;
537
538         pkt = (struct iwl_rx_packet *)cmd.reply_page;
539         if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
540                 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
541                           pkt->hdr.flags);
542                 ret = -EIO;
543         }
544
545         if (!ret) {
546                 switch (pkt->u.rem_sta.status) {
547                 case REM_STA_SUCCESS_MSK:
548                         spin_lock_irqsave(&priv->sta_lock, flags_spin);
549                         iwl_sta_ucode_deactivate(priv, station->sta.sta.sta_id);
550                         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
551                         IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
552                         break;
553                 default:
554                         ret = -EIO;
555                         IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
556                         break;
557                 }
558         }
559         iwl_free_pages(priv, cmd.reply_page);
560
561         return ret;
562 }
563
564 /**
565  * iwl_remove_station - Remove driver's knowledge of station.
566  */
567 static int iwl_remove_station(struct iwl_priv *priv, struct ieee80211_sta *sta)
568 {
569         int sta_id = IWL_INVALID_STATION;
570         int i, ret = -EINVAL;
571         unsigned long flags;
572         bool is_ap = priv->iw_mode == NL80211_IFTYPE_STATION;
573         struct iwl_station_entry *station;
574
575         if (!iwl_is_ready(priv)) {
576                 IWL_DEBUG_INFO(priv,
577                         "Unable to remove station %pM, device not ready.\n",
578                         sta->addr);
579                 /*
580                  * It is typical for stations to be removed when we are
581                  * going down. Return success since device will be down
582                  * soon anyway
583                  */
584                 return 0;
585         }
586
587         spin_lock_irqsave(&priv->sta_lock, flags);
588
589         if (is_ap)
590                 sta_id = IWL_AP_ID;
591         else
592                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++)
593                         if (priv->stations[i].used &&
594                             !compare_ether_addr(priv->stations[i].sta.sta.addr,
595                                                 sta->addr)) {
596                                 sta_id = i;
597                                 break;
598                         }
599
600         if (unlikely(sta_id == IWL_INVALID_STATION))
601                 goto out;
602
603         IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d  %pM\n",
604                 sta_id, sta->addr);
605
606         if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
607                 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
608                                 sta->addr);
609                 goto out;
610         }
611
612         if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
613                 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
614                                 sta->addr);
615                 goto out;
616         }
617
618
619         priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
620
621         priv->num_stations--;
622
623         BUG_ON(priv->num_stations < 0);
624
625         station = &priv->stations[sta_id];
626         spin_unlock_irqrestore(&priv->sta_lock, flags);
627
628         ret = iwl_send_remove_station(priv, station);
629         return ret;
630 out:
631         spin_unlock_irqrestore(&priv->sta_lock, flags);
632         return ret;
633 }
634
635 /**
636  * iwl_clear_ucode_stations() - clear entire station table driver and/or ucode
637  * @priv:
638  * @force: If set then the uCode station table needs to be cleared here. If
639  *         not set then the uCode station table has already been cleared,
640  *         for example after sending it a RXON command without ASSOC bit
641  *         set, and we just need to change driver state here.
642  */
643 void iwl_clear_ucode_stations(struct iwl_priv *priv, bool force)
644 {
645         int i;
646         unsigned long flags_spin;
647         bool cleared = false;
648
649         IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver%s\n",
650                         force ? " and ucode" : "");
651
652         if (force) {
653                 if (!iwl_is_ready(priv)) {
654                         /*
655                          * If device is not ready at this point the station
656                          * table is likely already empty (uCode not ready
657                          * to receive station requests) or will soon be
658                          * due to interface going down.
659                          */
660                         IWL_DEBUG_INFO(priv, "Unable to remove stations from device - device not ready\n");
661                 } else {
662                         iwl_send_cmd_pdu_async(priv, REPLY_REMOVE_ALL_STA, 0, NULL, NULL);
663                 }
664         }
665
666         spin_lock_irqsave(&priv->sta_lock, flags_spin);
667         if (force) {
668                 IWL_DEBUG_INFO(priv, "Clearing all station information in driver\n");
669                 priv->num_stations = 0;
670                 memset(priv->stations, 0, sizeof(priv->stations));
671         } else {
672                 for (i = 0; i < priv->hw_params.max_stations; i++) {
673                         if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
674                                 IWL_DEBUG_INFO(priv, "Clearing ucode active for station %d\n", i);
675                                 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
676                                 cleared = true;
677                         }
678                 }
679         }
680         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
681
682         if (!cleared)
683                 IWL_DEBUG_INFO(priv, "No active stations found to be cleared\n");
684 }
685 EXPORT_SYMBOL(iwl_clear_ucode_stations);
686
687 /**
688  * iwl_restore_stations() - Restore driver known stations to device
689  *
690  * All stations considered active by driver, but not present in ucode, is
691  * restored.
692  *
693  * Function sleeps.
694  */
695 void iwl_restore_stations(struct iwl_priv *priv)
696 {
697         struct iwl_station_entry *station;
698         unsigned long flags_spin;
699         int i;
700         bool found = false;
701         int ret;
702
703         if (!iwl_is_ready(priv)) {
704                 IWL_DEBUG_INFO(priv, "Not ready yet, not restoring any stations.\n");
705                 return;
706         }
707
708         IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
709         spin_lock_irqsave(&priv->sta_lock, flags_spin);
710         for (i = 0; i < priv->hw_params.max_stations; i++) {
711                 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
712                             !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
713                         IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
714                                         priv->stations[i].sta.sta.addr);
715                         priv->stations[i].sta.mode = 0;
716                         priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
717                         found = true;
718                 }
719         }
720
721         for (i = 0; i < priv->hw_params.max_stations; i++) {
722                 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
723                         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
724                         station = &priv->stations[i];
725                         ret = iwl_send_add_sta(priv, &priv->stations[i].sta, CMD_SYNC);
726                         if (ret) {
727                                 IWL_ERR(priv, "Adding station %pM failed.\n",
728                                         station->sta.sta.addr);
729                                 spin_lock_irqsave(&priv->sta_lock, flags_spin);
730                                 priv->stations[i].used &= ~IWL_STA_DRIVER_ACTIVE;
731                                 priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
732                                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
733                         }
734                         /*
735                          * Rate scaling has already been initialized, send
736                          * current LQ command
737                          */
738                         if (station->lq)
739                                 iwl_send_lq_cmd(priv, station->lq, CMD_SYNC, true);
740                         spin_lock_irqsave(&priv->sta_lock, flags_spin);
741                         priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
742                 }
743         }
744
745         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
746         if (!found)
747                 IWL_DEBUG_INFO(priv, "Restoring all known stations .... no stations to be restored.\n");
748         else
749                 IWL_DEBUG_INFO(priv, "Restoring all known stations .... complete.\n");
750 }
751 EXPORT_SYMBOL(iwl_restore_stations);
752
753 int iwl_get_free_ucode_key_index(struct iwl_priv *priv)
754 {
755         int i;
756
757         for (i = 0; i < STA_KEY_MAX_NUM; i++)
758                 if (!test_and_set_bit(i, &priv->ucode_key_table))
759                         return i;
760
761         return WEP_INVALID_OFFSET;
762 }
763 EXPORT_SYMBOL(iwl_get_free_ucode_key_index);
764
765 static int iwl_send_static_wepkey_cmd(struct iwl_priv *priv, u8 send_if_empty)
766 {
767         int i, not_empty = 0;
768         u8 buff[sizeof(struct iwl_wep_cmd) +
769                 sizeof(struct iwl_wep_key) * WEP_KEYS_MAX];
770         struct iwl_wep_cmd *wep_cmd = (struct iwl_wep_cmd *)buff;
771         size_t cmd_size  = sizeof(struct iwl_wep_cmd);
772         struct iwl_host_cmd cmd = {
773                 .id = REPLY_WEPKEY,
774                 .data = wep_cmd,
775                 .flags = CMD_SYNC,
776         };
777
778         might_sleep();
779
780         memset(wep_cmd, 0, cmd_size +
781                         (sizeof(struct iwl_wep_key) * WEP_KEYS_MAX));
782
783         for (i = 0; i < WEP_KEYS_MAX ; i++) {
784                 wep_cmd->key[i].key_index = i;
785                 if (priv->wep_keys[i].key_size) {
786                         wep_cmd->key[i].key_offset = i;
787                         not_empty = 1;
788                 } else {
789                         wep_cmd->key[i].key_offset = WEP_INVALID_OFFSET;
790                 }
791
792                 wep_cmd->key[i].key_size = priv->wep_keys[i].key_size;
793                 memcpy(&wep_cmd->key[i].key[3], priv->wep_keys[i].key,
794                                 priv->wep_keys[i].key_size);
795         }
796
797         wep_cmd->global_key_type = WEP_KEY_WEP_TYPE;
798         wep_cmd->num_keys = WEP_KEYS_MAX;
799
800         cmd_size += sizeof(struct iwl_wep_key) * WEP_KEYS_MAX;
801
802         cmd.len = cmd_size;
803
804         if (not_empty || send_if_empty)
805                 return iwl_send_cmd(priv, &cmd);
806         else
807                 return 0;
808 }
809
810 int iwl_restore_default_wep_keys(struct iwl_priv *priv)
811 {
812         WARN_ON(!mutex_is_locked(&priv->mutex));
813
814         return iwl_send_static_wepkey_cmd(priv, 0);
815 }
816 EXPORT_SYMBOL(iwl_restore_default_wep_keys);
817
818 int iwl_remove_default_wep_key(struct iwl_priv *priv,
819                                struct ieee80211_key_conf *keyconf)
820 {
821         int ret;
822
823         WARN_ON(!mutex_is_locked(&priv->mutex));
824
825         IWL_DEBUG_WEP(priv, "Removing default WEP key: idx=%d\n",
826                       keyconf->keyidx);
827
828         memset(&priv->wep_keys[keyconf->keyidx], 0, sizeof(priv->wep_keys[0]));
829         if (iwl_is_rfkill(priv)) {
830                 IWL_DEBUG_WEP(priv, "Not sending REPLY_WEPKEY command due to RFKILL.\n");
831                 /* but keys in device are clear anyway so return success */
832                 return 0;
833         }
834         ret = iwl_send_static_wepkey_cmd(priv, 1);
835         IWL_DEBUG_WEP(priv, "Remove default WEP key: idx=%d ret=%d\n",
836                       keyconf->keyidx, ret);
837
838         return ret;
839 }
840 EXPORT_SYMBOL(iwl_remove_default_wep_key);
841
842 int iwl_set_default_wep_key(struct iwl_priv *priv,
843                             struct ieee80211_key_conf *keyconf)
844 {
845         int ret;
846
847         WARN_ON(!mutex_is_locked(&priv->mutex));
848
849         if (keyconf->keylen != WEP_KEY_LEN_128 &&
850             keyconf->keylen != WEP_KEY_LEN_64) {
851                 IWL_DEBUG_WEP(priv, "Bad WEP key length %d\n", keyconf->keylen);
852                 return -EINVAL;
853         }
854
855         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
856         keyconf->hw_key_idx = HW_KEY_DEFAULT;
857         priv->stations[IWL_AP_ID].keyinfo.alg = ALG_WEP;
858
859         priv->wep_keys[keyconf->keyidx].key_size = keyconf->keylen;
860         memcpy(&priv->wep_keys[keyconf->keyidx].key, &keyconf->key,
861                                                         keyconf->keylen);
862
863         ret = iwl_send_static_wepkey_cmd(priv, 0);
864         IWL_DEBUG_WEP(priv, "Set default WEP key: len=%d idx=%d ret=%d\n",
865                 keyconf->keylen, keyconf->keyidx, ret);
866
867         return ret;
868 }
869 EXPORT_SYMBOL(iwl_set_default_wep_key);
870
871 static int iwl_set_wep_dynamic_key_info(struct iwl_priv *priv,
872                                 struct ieee80211_key_conf *keyconf,
873                                 u8 sta_id)
874 {
875         unsigned long flags;
876         __le16 key_flags = 0;
877         int ret;
878
879         keyconf->flags &= ~IEEE80211_KEY_FLAG_GENERATE_IV;
880
881         key_flags |= (STA_KEY_FLG_WEP | STA_KEY_FLG_MAP_KEY_MSK);
882         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
883         key_flags &= ~STA_KEY_FLG_INVALID;
884
885         if (keyconf->keylen == WEP_KEY_LEN_128)
886                 key_flags |= STA_KEY_FLG_KEY_SIZE_MSK;
887
888         if (sta_id == priv->hw_params.bcast_sta_id)
889                 key_flags |= STA_KEY_MULTICAST_MSK;
890
891         spin_lock_irqsave(&priv->sta_lock, flags);
892
893         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
894         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
895         priv->stations[sta_id].keyinfo.keyidx = keyconf->keyidx;
896
897         memcpy(priv->stations[sta_id].keyinfo.key,
898                                 keyconf->key, keyconf->keylen);
899
900         memcpy(&priv->stations[sta_id].sta.key.key[3],
901                                 keyconf->key, keyconf->keylen);
902
903         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
904                         == STA_KEY_FLG_NO_ENC)
905                 priv->stations[sta_id].sta.key.key_offset =
906                                  iwl_get_free_ucode_key_index(priv);
907         /* else, we are overriding an existing key => no need to allocated room
908          * in uCode. */
909
910         WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
911                 "no space for a new key");
912
913         priv->stations[sta_id].sta.key.key_flags = key_flags;
914         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
915         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
916
917         ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
918
919         spin_unlock_irqrestore(&priv->sta_lock, flags);
920
921         return ret;
922 }
923
924 static int iwl_set_ccmp_dynamic_key_info(struct iwl_priv *priv,
925                                    struct ieee80211_key_conf *keyconf,
926                                    u8 sta_id)
927 {
928         unsigned long flags;
929         __le16 key_flags = 0;
930         int ret;
931
932         key_flags |= (STA_KEY_FLG_CCMP | STA_KEY_FLG_MAP_KEY_MSK);
933         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
934         key_flags &= ~STA_KEY_FLG_INVALID;
935
936         if (sta_id == priv->hw_params.bcast_sta_id)
937                 key_flags |= STA_KEY_MULTICAST_MSK;
938
939         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
940
941         spin_lock_irqsave(&priv->sta_lock, flags);
942         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
943         priv->stations[sta_id].keyinfo.keylen = keyconf->keylen;
944
945         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key,
946                keyconf->keylen);
947
948         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key,
949                keyconf->keylen);
950
951         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
952                         == STA_KEY_FLG_NO_ENC)
953                 priv->stations[sta_id].sta.key.key_offset =
954                                  iwl_get_free_ucode_key_index(priv);
955         /* else, we are overriding an existing key => no need to allocated room
956          * in uCode. */
957
958         WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
959                 "no space for a new key");
960
961         priv->stations[sta_id].sta.key.key_flags = key_flags;
962         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
963         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
964
965         ret = iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
966
967         spin_unlock_irqrestore(&priv->sta_lock, flags);
968
969         return ret;
970 }
971
972 static int iwl_set_tkip_dynamic_key_info(struct iwl_priv *priv,
973                                    struct ieee80211_key_conf *keyconf,
974                                    u8 sta_id)
975 {
976         unsigned long flags;
977         int ret = 0;
978         __le16 key_flags = 0;
979
980         key_flags |= (STA_KEY_FLG_TKIP | STA_KEY_FLG_MAP_KEY_MSK);
981         key_flags |= cpu_to_le16(keyconf->keyidx << STA_KEY_FLG_KEYID_POS);
982         key_flags &= ~STA_KEY_FLG_INVALID;
983
984         if (sta_id == priv->hw_params.bcast_sta_id)
985                 key_flags |= STA_KEY_MULTICAST_MSK;
986
987         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_IV;
988         keyconf->flags |= IEEE80211_KEY_FLAG_GENERATE_MMIC;
989
990         spin_lock_irqsave(&priv->sta_lock, flags);
991
992         priv->stations[sta_id].keyinfo.alg = keyconf->alg;
993         priv->stations[sta_id].keyinfo.keylen = 16;
994
995         if ((priv->stations[sta_id].sta.key.key_flags & STA_KEY_FLG_ENCRYPT_MSK)
996                         == STA_KEY_FLG_NO_ENC)
997                 priv->stations[sta_id].sta.key.key_offset =
998                                  iwl_get_free_ucode_key_index(priv);
999         /* else, we are overriding an existing key => no need to allocated room
1000          * in uCode. */
1001
1002         WARN(priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET,
1003                 "no space for a new key");
1004
1005         priv->stations[sta_id].sta.key.key_flags = key_flags;
1006
1007
1008         /* This copy is acutally not needed: we get the key with each TX */
1009         memcpy(priv->stations[sta_id].keyinfo.key, keyconf->key, 16);
1010
1011         memcpy(priv->stations[sta_id].sta.key.key, keyconf->key, 16);
1012
1013         spin_unlock_irqrestore(&priv->sta_lock, flags);
1014
1015         return ret;
1016 }
1017
1018 void iwl_update_tkip_key(struct iwl_priv *priv,
1019                         struct ieee80211_key_conf *keyconf,
1020                         const u8 *addr, u32 iv32, u16 *phase1key)
1021 {
1022         u8 sta_id = IWL_INVALID_STATION;
1023         unsigned long flags;
1024         int i;
1025
1026         sta_id = iwl_find_station(priv, addr);
1027         if (sta_id == IWL_INVALID_STATION) {
1028                 IWL_DEBUG_MAC80211(priv, "leave - %pM not in station map.\n",
1029                                    addr);
1030                 return;
1031         }
1032
1033         if (iwl_scan_cancel(priv)) {
1034                 /* cancel scan failed, just live w/ bad key and rely
1035                    briefly on SW decryption */
1036                 return;
1037         }
1038
1039         spin_lock_irqsave(&priv->sta_lock, flags);
1040
1041         priv->stations[sta_id].sta.key.tkip_rx_tsc_byte2 = (u8) iv32;
1042
1043         for (i = 0; i < 5; i++)
1044                 priv->stations[sta_id].sta.key.tkip_rx_ttak[i] =
1045                         cpu_to_le16(phase1key[i]);
1046
1047         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1048         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1049
1050         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1051
1052         spin_unlock_irqrestore(&priv->sta_lock, flags);
1053
1054 }
1055 EXPORT_SYMBOL(iwl_update_tkip_key);
1056
1057 int iwl_remove_dynamic_key(struct iwl_priv *priv,
1058                                 struct ieee80211_key_conf *keyconf,
1059                                 u8 sta_id)
1060 {
1061         unsigned long flags;
1062         int ret = 0;
1063         u16 key_flags;
1064         u8 keyidx;
1065
1066         priv->key_mapping_key--;
1067
1068         spin_lock_irqsave(&priv->sta_lock, flags);
1069         key_flags = le16_to_cpu(priv->stations[sta_id].sta.key.key_flags);
1070         keyidx = (key_flags >> STA_KEY_FLG_KEYID_POS) & 0x3;
1071
1072         IWL_DEBUG_WEP(priv, "Remove dynamic key: idx=%d sta=%d\n",
1073                       keyconf->keyidx, sta_id);
1074
1075         if (keyconf->keyidx != keyidx) {
1076                 /* We need to remove a key with index different that the one
1077                  * in the uCode. This means that the key we need to remove has
1078                  * been replaced by another one with different index.
1079                  * Don't do anything and return ok
1080                  */
1081                 spin_unlock_irqrestore(&priv->sta_lock, flags);
1082                 return 0;
1083         }
1084
1085         if (priv->stations[sta_id].sta.key.key_offset == WEP_INVALID_OFFSET) {
1086                 IWL_WARN(priv, "Removing wrong key %d 0x%x\n",
1087                             keyconf->keyidx, key_flags);
1088                 spin_unlock_irqrestore(&priv->sta_lock, flags);
1089                 return 0;
1090         }
1091
1092         if (!test_and_clear_bit(priv->stations[sta_id].sta.key.key_offset,
1093                 &priv->ucode_key_table))
1094                 IWL_ERR(priv, "index %d not used in uCode key table.\n",
1095                         priv->stations[sta_id].sta.key.key_offset);
1096         memset(&priv->stations[sta_id].keyinfo, 0,
1097                                         sizeof(struct iwl_hw_key));
1098         memset(&priv->stations[sta_id].sta.key, 0,
1099                                         sizeof(struct iwl4965_keyinfo));
1100         priv->stations[sta_id].sta.key.key_flags =
1101                         STA_KEY_FLG_NO_ENC | STA_KEY_FLG_INVALID;
1102         priv->stations[sta_id].sta.key.key_offset = WEP_INVALID_OFFSET;
1103         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_KEY_MASK;
1104         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1105
1106         if (iwl_is_rfkill(priv)) {
1107                 IWL_DEBUG_WEP(priv, "Not sending REPLY_ADD_STA command because RFKILL enabled.\n");
1108                 spin_unlock_irqrestore(&priv->sta_lock, flags);
1109                 return 0;
1110         }
1111         ret =  iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1112         spin_unlock_irqrestore(&priv->sta_lock, flags);
1113         return ret;
1114 }
1115 EXPORT_SYMBOL(iwl_remove_dynamic_key);
1116
1117 int iwl_set_dynamic_key(struct iwl_priv *priv,
1118                                 struct ieee80211_key_conf *keyconf, u8 sta_id)
1119 {
1120         int ret;
1121
1122         priv->key_mapping_key++;
1123         keyconf->hw_key_idx = HW_KEY_DYNAMIC;
1124
1125         switch (keyconf->alg) {
1126         case ALG_CCMP:
1127                 ret = iwl_set_ccmp_dynamic_key_info(priv, keyconf, sta_id);
1128                 break;
1129         case ALG_TKIP:
1130                 ret = iwl_set_tkip_dynamic_key_info(priv, keyconf, sta_id);
1131                 break;
1132         case ALG_WEP:
1133                 ret = iwl_set_wep_dynamic_key_info(priv, keyconf, sta_id);
1134                 break;
1135         default:
1136                 IWL_ERR(priv,
1137                         "Unknown alg: %s alg = %d\n", __func__, keyconf->alg);
1138                 ret = -EINVAL;
1139         }
1140
1141         IWL_DEBUG_WEP(priv, "Set dynamic key: alg= %d len=%d idx=%d sta=%d ret=%d\n",
1142                       keyconf->alg, keyconf->keylen, keyconf->keyidx,
1143                       sta_id, ret);
1144
1145         return ret;
1146 }
1147 EXPORT_SYMBOL(iwl_set_dynamic_key);
1148
1149 #ifdef CONFIG_IWLWIFI_DEBUG
1150 static void iwl_dump_lq_cmd(struct iwl_priv *priv,
1151                            struct iwl_link_quality_cmd *lq)
1152 {
1153         int i;
1154         IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
1155         IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
1156                        lq->general_params.single_stream_ant_msk,
1157                        lq->general_params.dual_stream_ant_msk);
1158
1159         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
1160                 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
1161                                i, lq->rs_table[i].rate_n_flags);
1162 }
1163 #else
1164 static inline void iwl_dump_lq_cmd(struct iwl_priv *priv,
1165                                    struct iwl_link_quality_cmd *lq)
1166 {
1167 }
1168 #endif
1169
1170 /**
1171  * iwl_send_lq_cmd() - Send link quality command
1172  * @init: This command is sent as part of station initialization right
1173  *        after station has been added.
1174  *
1175  * The link quality command is sent as the last step of station creation.
1176  * This is the special case in which init is set and we call a callback in
1177  * this case to clear the state indicating that station creation is in
1178  * progress.
1179  */
1180 int iwl_send_lq_cmd(struct iwl_priv *priv,
1181                     struct iwl_link_quality_cmd *lq, u8 flags, bool init)
1182 {
1183         int ret = 0;
1184         unsigned long flags_spin;
1185
1186         struct iwl_host_cmd cmd = {
1187                 .id = REPLY_TX_LINK_QUALITY_CMD,
1188                 .len = sizeof(struct iwl_link_quality_cmd),
1189                 .flags = flags,
1190                 .data = lq,
1191         };
1192
1193         if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
1194                 return -EINVAL;
1195
1196         iwl_dump_lq_cmd(priv, lq);
1197         BUG_ON(init && (cmd.flags & CMD_ASYNC));
1198
1199         iwl_dump_lq_cmd(priv, lq);
1200         ret = iwl_send_cmd(priv, &cmd);
1201         if (ret || (cmd.flags & CMD_ASYNC))
1202                 return ret;
1203
1204         if (init) {
1205                 IWL_DEBUG_INFO(priv, "init LQ command complete, clearing sta addition status for sta %d\n",
1206                                lq->sta_id);
1207                 spin_lock_irqsave(&priv->sta_lock, flags_spin);
1208                 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1209                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
1210         }
1211         return 0;
1212 }
1213 EXPORT_SYMBOL(iwl_send_lq_cmd);
1214
1215 /**
1216  * iwl_add_bcast_station - add broadcast station into station table.
1217  */
1218 void iwl_add_bcast_station(struct iwl_priv *priv)
1219 {
1220         IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
1221         iwl_add_local_station(priv, iwl_bcast_addr, true);
1222 }
1223 EXPORT_SYMBOL(iwl_add_bcast_station);
1224
1225 /**
1226  * iwl3945_add_bcast_station - add broadcast station into station table.
1227  */
1228 void iwl3945_add_bcast_station(struct iwl_priv *priv)
1229 {
1230         IWL_DEBUG_INFO(priv, "Adding broadcast station to station table\n");
1231         iwl_add_local_station(priv, iwl_bcast_addr, false);
1232         /*
1233          * It is assumed that when station is added more initialization
1234          * needs to be done, but for 3945 it is not the case and we can
1235          * just release station table access right here.
1236          */
1237         priv->stations[priv->hw_params.bcast_sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
1238
1239 }
1240 EXPORT_SYMBOL(iwl3945_add_bcast_station);
1241
1242 /**
1243  * iwl_get_sta_id - Find station's index within station table
1244  *
1245  * If new IBSS station, create new entry in station table
1246  */
1247 int iwl_get_sta_id(struct iwl_priv *priv, struct ieee80211_hdr *hdr)
1248 {
1249         int sta_id;
1250         __le16 fc = hdr->frame_control;
1251
1252         /* If this frame is broadcast or management, use broadcast station id */
1253         if (!ieee80211_is_data(fc) ||  is_multicast_ether_addr(hdr->addr1))
1254                 return priv->hw_params.bcast_sta_id;
1255
1256         switch (priv->iw_mode) {
1257
1258         /* If we are a client station in a BSS network, use the special
1259          * AP station entry (that's the only station we communicate with) */
1260         case NL80211_IFTYPE_STATION:
1261                 /*
1262                  * If addition of station not complete yet, which means
1263                  * that rate scaling has not been initialized, then return
1264                  * the broadcast station.
1265                  */
1266                 if (!(priv->stations[IWL_AP_ID].used & IWL_STA_UCODE_ACTIVE))
1267                         return priv->hw_params.bcast_sta_id;
1268                 return IWL_AP_ID;
1269
1270         /* If we are an AP, then find the station, or use BCAST */
1271         case NL80211_IFTYPE_AP:
1272                 sta_id = iwl_find_station(priv, hdr->addr1);
1273                 if (sta_id != IWL_INVALID_STATION)
1274                         return sta_id;
1275                 return priv->hw_params.bcast_sta_id;
1276
1277         /* If this frame is going out to an IBSS network, find the station,
1278          * or create a new station table entry */
1279         case NL80211_IFTYPE_ADHOC:
1280                 sta_id = iwl_find_station(priv, hdr->addr1);
1281                 if (sta_id != IWL_INVALID_STATION)
1282                         return sta_id;
1283
1284                 IWL_DEBUG_DROP(priv, "Station %pM not in station map. "
1285                                "Defaulting to broadcast...\n",
1286                                hdr->addr1);
1287                 iwl_print_hex_dump(priv, IWL_DL_DROP, (u8 *) hdr, sizeof(*hdr));
1288                 return priv->hw_params.bcast_sta_id;
1289
1290         default:
1291                 IWL_WARN(priv, "Unknown mode of operation: %d\n",
1292                         priv->iw_mode);
1293                 return priv->hw_params.bcast_sta_id;
1294         }
1295 }
1296 EXPORT_SYMBOL(iwl_get_sta_id);
1297
1298 /**
1299  * iwl_sta_tx_modify_enable_tid - Enable Tx for this TID in station table
1300  */
1301 void iwl_sta_tx_modify_enable_tid(struct iwl_priv *priv, int sta_id, int tid)
1302 {
1303         unsigned long flags;
1304
1305         /* Remove "disable" flag, to enable Tx for this TID */
1306         spin_lock_irqsave(&priv->sta_lock, flags);
1307         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_TID_DISABLE_TX;
1308         priv->stations[sta_id].sta.tid_disable_tx &= cpu_to_le16(~(1 << tid));
1309         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1310         spin_unlock_irqrestore(&priv->sta_lock, flags);
1311
1312         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1313 }
1314 EXPORT_SYMBOL(iwl_sta_tx_modify_enable_tid);
1315
1316 int iwl_sta_rx_agg_start(struct iwl_priv *priv,
1317                          const u8 *addr, int tid, u16 ssn)
1318 {
1319         unsigned long flags;
1320         int sta_id;
1321
1322         sta_id = iwl_find_station(priv, addr);
1323         if (sta_id == IWL_INVALID_STATION)
1324                 return -ENXIO;
1325
1326         spin_lock_irqsave(&priv->sta_lock, flags);
1327         priv->stations[sta_id].sta.station_flags_msk = 0;
1328         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_ADDBA_TID_MSK;
1329         priv->stations[sta_id].sta.add_immediate_ba_tid = (u8)tid;
1330         priv->stations[sta_id].sta.add_immediate_ba_ssn = cpu_to_le16(ssn);
1331         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1332         spin_unlock_irqrestore(&priv->sta_lock, flags);
1333
1334         return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1335                                         CMD_ASYNC);
1336 }
1337 EXPORT_SYMBOL(iwl_sta_rx_agg_start);
1338
1339 int iwl_sta_rx_agg_stop(struct iwl_priv *priv, const u8 *addr, int tid)
1340 {
1341         unsigned long flags;
1342         int sta_id;
1343
1344         sta_id = iwl_find_station(priv, addr);
1345         if (sta_id == IWL_INVALID_STATION) {
1346                 IWL_ERR(priv, "Invalid station for AGG tid %d\n", tid);
1347                 return -ENXIO;
1348         }
1349
1350         spin_lock_irqsave(&priv->sta_lock, flags);
1351         priv->stations[sta_id].sta.station_flags_msk = 0;
1352         priv->stations[sta_id].sta.sta.modify_mask = STA_MODIFY_DELBA_TID_MSK;
1353         priv->stations[sta_id].sta.remove_immediate_ba_tid = (u8)tid;
1354         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1355         spin_unlock_irqrestore(&priv->sta_lock, flags);
1356
1357         return iwl_send_add_sta(priv, &priv->stations[sta_id].sta,
1358                                         CMD_ASYNC);
1359 }
1360 EXPORT_SYMBOL(iwl_sta_rx_agg_stop);
1361
1362 void iwl_sta_modify_ps_wake(struct iwl_priv *priv, int sta_id)
1363 {
1364         unsigned long flags;
1365
1366         spin_lock_irqsave(&priv->sta_lock, flags);
1367         priv->stations[sta_id].sta.station_flags &= ~STA_FLG_PWR_SAVE_MSK;
1368         priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1369         priv->stations[sta_id].sta.sta.modify_mask = 0;
1370         priv->stations[sta_id].sta.sleep_tx_count = 0;
1371         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1372         spin_unlock_irqrestore(&priv->sta_lock, flags);
1373
1374         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1375 }
1376 EXPORT_SYMBOL(iwl_sta_modify_ps_wake);
1377
1378 void iwl_sta_modify_sleep_tx_count(struct iwl_priv *priv, int sta_id, int cnt)
1379 {
1380         unsigned long flags;
1381
1382         spin_lock_irqsave(&priv->sta_lock, flags);
1383         priv->stations[sta_id].sta.station_flags |= STA_FLG_PWR_SAVE_MSK;
1384         priv->stations[sta_id].sta.station_flags_msk = STA_FLG_PWR_SAVE_MSK;
1385         priv->stations[sta_id].sta.sta.modify_mask =
1386                                         STA_MODIFY_SLEEP_TX_COUNT_MSK;
1387         priv->stations[sta_id].sta.sleep_tx_count = cpu_to_le16(cnt);
1388         priv->stations[sta_id].sta.mode = STA_CONTROL_MODIFY_MSK;
1389         spin_unlock_irqrestore(&priv->sta_lock, flags);
1390
1391         iwl_send_add_sta(priv, &priv->stations[sta_id].sta, CMD_ASYNC);
1392 }
1393 EXPORT_SYMBOL(iwl_sta_modify_sleep_tx_count);
1394
1395 int iwl_mac_sta_remove(struct ieee80211_hw *hw,
1396                                struct ieee80211_vif *vif,
1397                                struct ieee80211_sta *sta)
1398 {
1399         int ret;
1400         struct iwl_priv *priv = hw->priv;
1401         IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
1402                         sta->addr);
1403         ret = iwl_remove_station(priv, sta);
1404         if (ret)
1405                 IWL_ERR(priv, "Error removing station %pM\n",
1406                         sta->addr);
1407         return ret;
1408 }
1409 EXPORT_SYMBOL(iwl_mac_sta_remove);