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