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