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