Merge branch 'drm-fixes' of git://people.freedesktop.org/~airlied/linux
[pandora-kernel.git] / drivers / net / wireless / iwlegacy / iwl-sta.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2003 - 2011 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 #include <linux/export.h>
35
36 #include "iwl-dev.h"
37 #include "iwl-core.h"
38 #include "iwl-sta.h"
39
40 /* priv->sta_lock must be held */
41 static void iwl_legacy_sta_ucode_activate(struct iwl_priv *priv, u8 sta_id)
42 {
43
44         if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE))
45                 IWL_ERR(priv,
46                         "ACTIVATE a non DRIVER active station id %u addr %pM\n",
47                         sta_id, priv->stations[sta_id].sta.sta.addr);
48
49         if (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) {
50                 IWL_DEBUG_ASSOC(priv,
51                         "STA id %u addr %pM already present"
52                         " in uCode (according to driver)\n",
53                         sta_id, priv->stations[sta_id].sta.sta.addr);
54         } else {
55                 priv->stations[sta_id].used |= IWL_STA_UCODE_ACTIVE;
56                 IWL_DEBUG_ASSOC(priv, "Added STA id %u addr %pM to uCode\n",
57                                 sta_id, priv->stations[sta_id].sta.sta.addr);
58         }
59 }
60
61 static int iwl_legacy_process_add_sta_resp(struct iwl_priv *priv,
62                                     struct iwl_legacy_addsta_cmd *addsta,
63                                     struct iwl_rx_packet *pkt,
64                                     bool sync)
65 {
66         u8 sta_id = addsta->sta.sta_id;
67         unsigned long flags;
68         int ret = -EIO;
69
70         if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
71                 IWL_ERR(priv, "Bad return from REPLY_ADD_STA (0x%08X)\n",
72                         pkt->hdr.flags);
73                 return ret;
74         }
75
76         IWL_DEBUG_INFO(priv, "Processing response for adding station %u\n",
77                        sta_id);
78
79         spin_lock_irqsave(&priv->sta_lock, flags);
80
81         switch (pkt->u.add_sta.status) {
82         case ADD_STA_SUCCESS_MSK:
83                 IWL_DEBUG_INFO(priv, "REPLY_ADD_STA PASSED\n");
84                 iwl_legacy_sta_ucode_activate(priv, sta_id);
85                 ret = 0;
86                 break;
87         case ADD_STA_NO_ROOM_IN_TABLE:
88                 IWL_ERR(priv, "Adding station %d failed, no room in table.\n",
89                         sta_id);
90                 break;
91         case ADD_STA_NO_BLOCK_ACK_RESOURCE:
92                 IWL_ERR(priv,
93                         "Adding station %d failed, no block ack resource.\n",
94                         sta_id);
95                 break;
96         case ADD_STA_MODIFY_NON_EXIST_STA:
97                 IWL_ERR(priv, "Attempting to modify non-existing station %d\n",
98                         sta_id);
99                 break;
100         default:
101                 IWL_DEBUG_ASSOC(priv, "Received REPLY_ADD_STA:(0x%08X)\n",
102                                 pkt->u.add_sta.status);
103                 break;
104         }
105
106         IWL_DEBUG_INFO(priv, "%s station id %u addr %pM\n",
107                        priv->stations[sta_id].sta.mode ==
108                        STA_CONTROL_MODIFY_MSK ?  "Modified" : "Added",
109                        sta_id, priv->stations[sta_id].sta.sta.addr);
110
111         /*
112          * XXX: The MAC address in the command buffer is often changed from
113          * the original sent to the device. That is, the MAC address
114          * written to the command buffer often is not the same MAC address
115          * read from the command buffer when the command returns. This
116          * issue has not yet been resolved and this debugging is left to
117          * observe the problem.
118          */
119         IWL_DEBUG_INFO(priv, "%s station according to cmd buffer %pM\n",
120                        priv->stations[sta_id].sta.mode ==
121                        STA_CONTROL_MODIFY_MSK ? "Modified" : "Added",
122                        addsta->sta.addr);
123         spin_unlock_irqrestore(&priv->sta_lock, flags);
124
125         return ret;
126 }
127
128 static void iwl_legacy_add_sta_callback(struct iwl_priv *priv,
129                                  struct iwl_device_cmd *cmd,
130                                  struct iwl_rx_packet *pkt)
131 {
132         struct iwl_legacy_addsta_cmd *addsta =
133                 (struct iwl_legacy_addsta_cmd *)cmd->cmd.payload;
134
135         iwl_legacy_process_add_sta_resp(priv, addsta, pkt, false);
136
137 }
138
139 int iwl_legacy_send_add_sta(struct iwl_priv *priv,
140                      struct iwl_legacy_addsta_cmd *sta, u8 flags)
141 {
142         struct iwl_rx_packet *pkt = NULL;
143         int ret = 0;
144         u8 data[sizeof(*sta)];
145         struct iwl_host_cmd cmd = {
146                 .id = REPLY_ADD_STA,
147                 .flags = flags,
148                 .data = data,
149         };
150         u8 sta_id __maybe_unused = sta->sta.sta_id;
151
152         IWL_DEBUG_INFO(priv, "Adding sta %u (%pM) %ssynchronously\n",
153                        sta_id, sta->sta.addr, flags & CMD_ASYNC ?  "a" : "");
154
155         if (flags & CMD_ASYNC)
156                 cmd.callback = iwl_legacy_add_sta_callback;
157         else {
158                 cmd.flags |= CMD_WANT_SKB;
159                 might_sleep();
160         }
161
162         cmd.len = priv->cfg->ops->utils->build_addsta_hcmd(sta, data);
163         ret = iwl_legacy_send_cmd(priv, &cmd);
164
165         if (ret || (flags & CMD_ASYNC))
166                 return ret;
167
168         if (ret == 0) {
169                 pkt = (struct iwl_rx_packet *)cmd.reply_page;
170                 ret = iwl_legacy_process_add_sta_resp(priv, sta, pkt, true);
171         }
172         iwl_legacy_free_pages(priv, cmd.reply_page);
173
174         return ret;
175 }
176 EXPORT_SYMBOL(iwl_legacy_send_add_sta);
177
178 static void iwl_legacy_set_ht_add_station(struct iwl_priv *priv, u8 index,
179                                    struct ieee80211_sta *sta,
180                                    struct iwl_rxon_context *ctx)
181 {
182         struct ieee80211_sta_ht_cap *sta_ht_inf = &sta->ht_cap;
183         __le32 sta_flags;
184         u8 mimo_ps_mode;
185
186         if (!sta || !sta_ht_inf->ht_supported)
187                 goto done;
188
189         mimo_ps_mode = (sta_ht_inf->cap & IEEE80211_HT_CAP_SM_PS) >> 2;
190         IWL_DEBUG_ASSOC(priv, "spatial multiplexing power save mode: %s\n",
191                         (mimo_ps_mode == WLAN_HT_CAP_SM_PS_STATIC) ?
192                         "static" :
193                         (mimo_ps_mode == WLAN_HT_CAP_SM_PS_DYNAMIC) ?
194                         "dynamic" : "disabled");
195
196         sta_flags = priv->stations[index].sta.station_flags;
197
198         sta_flags &= ~(STA_FLG_RTS_MIMO_PROT_MSK | STA_FLG_MIMO_DIS_MSK);
199
200         switch (mimo_ps_mode) {
201         case WLAN_HT_CAP_SM_PS_STATIC:
202                 sta_flags |= STA_FLG_MIMO_DIS_MSK;
203                 break;
204         case WLAN_HT_CAP_SM_PS_DYNAMIC:
205                 sta_flags |= STA_FLG_RTS_MIMO_PROT_MSK;
206                 break;
207         case WLAN_HT_CAP_SM_PS_DISABLED:
208                 break;
209         default:
210                 IWL_WARN(priv, "Invalid MIMO PS mode %d\n", mimo_ps_mode);
211                 break;
212         }
213
214         sta_flags |= cpu_to_le32(
215               (u32)sta_ht_inf->ampdu_factor << STA_FLG_MAX_AGG_SIZE_POS);
216
217         sta_flags |= cpu_to_le32(
218               (u32)sta_ht_inf->ampdu_density << STA_FLG_AGG_MPDU_DENSITY_POS);
219
220         if (iwl_legacy_is_ht40_tx_allowed(priv, ctx, &sta->ht_cap))
221                 sta_flags |= STA_FLG_HT40_EN_MSK;
222         else
223                 sta_flags &= ~STA_FLG_HT40_EN_MSK;
224
225         priv->stations[index].sta.station_flags = sta_flags;
226  done:
227         return;
228 }
229
230 /**
231  * iwl_legacy_prep_station - Prepare station information for addition
232  *
233  * should be called with sta_lock held
234  */
235 u8 iwl_legacy_prep_station(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
236                     const u8 *addr, bool is_ap, struct ieee80211_sta *sta)
237 {
238         struct iwl_station_entry *station;
239         int i;
240         u8 sta_id = IWL_INVALID_STATION;
241         u16 rate;
242
243         if (is_ap)
244                 sta_id = ctx->ap_sta_id;
245         else if (is_broadcast_ether_addr(addr))
246                 sta_id = ctx->bcast_sta_id;
247         else
248                 for (i = IWL_STA_ID; i < priv->hw_params.max_stations; i++) {
249                         if (!compare_ether_addr(priv->stations[i].sta.sta.addr,
250                                                 addr)) {
251                                 sta_id = i;
252                                 break;
253                         }
254
255                         if (!priv->stations[i].used &&
256                             sta_id == IWL_INVALID_STATION)
257                                 sta_id = i;
258                 }
259
260         /*
261          * These two conditions have the same outcome, but keep them
262          * separate
263          */
264         if (unlikely(sta_id == IWL_INVALID_STATION))
265                 return sta_id;
266
267         /*
268          * uCode is not able to deal with multiple requests to add a
269          * station. Keep track if one is in progress so that we do not send
270          * another.
271          */
272         if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
273                 IWL_DEBUG_INFO(priv,
274                                 "STA %d already in process of being added.\n",
275                                 sta_id);
276                 return sta_id;
277         }
278
279         if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
280             (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE) &&
281             !compare_ether_addr(priv->stations[sta_id].sta.sta.addr, addr)) {
282                 IWL_DEBUG_ASSOC(priv,
283                                 "STA %d (%pM) already added, not adding again.\n",
284                                 sta_id, addr);
285                 return sta_id;
286         }
287
288         station = &priv->stations[sta_id];
289         station->used = IWL_STA_DRIVER_ACTIVE;
290         IWL_DEBUG_ASSOC(priv, "Add STA to driver ID %d: %pM\n",
291                         sta_id, addr);
292         priv->num_stations++;
293
294         /* Set up the REPLY_ADD_STA command to send to device */
295         memset(&station->sta, 0, sizeof(struct iwl_legacy_addsta_cmd));
296         memcpy(station->sta.sta.addr, addr, ETH_ALEN);
297         station->sta.mode = 0;
298         station->sta.sta.sta_id = sta_id;
299         station->sta.station_flags = ctx->station_flags;
300         station->ctxid = ctx->ctxid;
301
302         if (sta) {
303                 struct iwl_station_priv_common *sta_priv;
304
305                 sta_priv = (void *)sta->drv_priv;
306                 sta_priv->ctx = ctx;
307         }
308
309         /*
310          * OK to call unconditionally, since local stations (IBSS BSSID
311          * STA and broadcast STA) pass in a NULL sta, and mac80211
312          * doesn't allow HT IBSS.
313          */
314         iwl_legacy_set_ht_add_station(priv, sta_id, sta, ctx);
315
316         /* 3945 only */
317         rate = (priv->band == IEEE80211_BAND_5GHZ) ?
318                 IWL_RATE_6M_PLCP : IWL_RATE_1M_PLCP;
319         /* Turn on both antennas for the station... */
320         station->sta.rate_n_flags = cpu_to_le16(rate | RATE_MCS_ANT_AB_MSK);
321
322         return sta_id;
323
324 }
325 EXPORT_SYMBOL_GPL(iwl_legacy_prep_station);
326
327 #define STA_WAIT_TIMEOUT (HZ/2)
328
329 /**
330  * iwl_legacy_add_station_common -
331  */
332 int
333 iwl_legacy_add_station_common(struct iwl_priv *priv,
334                         struct iwl_rxon_context *ctx,
335                            const u8 *addr, bool is_ap,
336                            struct ieee80211_sta *sta, u8 *sta_id_r)
337 {
338         unsigned long flags_spin;
339         int ret = 0;
340         u8 sta_id;
341         struct iwl_legacy_addsta_cmd sta_cmd;
342
343         *sta_id_r = 0;
344         spin_lock_irqsave(&priv->sta_lock, flags_spin);
345         sta_id = iwl_legacy_prep_station(priv, ctx, addr, is_ap, sta);
346         if (sta_id == IWL_INVALID_STATION) {
347                 IWL_ERR(priv, "Unable to prepare station %pM for addition\n",
348                         addr);
349                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
350                 return -EINVAL;
351         }
352
353         /*
354          * uCode is not able to deal with multiple requests to add a
355          * station. Keep track if one is in progress so that we do not send
356          * another.
357          */
358         if (priv->stations[sta_id].used & IWL_STA_UCODE_INPROGRESS) {
359                 IWL_DEBUG_INFO(priv,
360                         "STA %d already in process of being added.\n",
361                        sta_id);
362                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
363                 return -EEXIST;
364         }
365
366         if ((priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE) &&
367             (priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
368                 IWL_DEBUG_ASSOC(priv,
369                         "STA %d (%pM) already added, not adding again.\n",
370                         sta_id, addr);
371                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
372                 return -EEXIST;
373         }
374
375         priv->stations[sta_id].used |= IWL_STA_UCODE_INPROGRESS;
376         memcpy(&sta_cmd, &priv->stations[sta_id].sta,
377                                 sizeof(struct iwl_legacy_addsta_cmd));
378         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
379
380         /* Add station to device's station table */
381         ret = iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
382         if (ret) {
383                 spin_lock_irqsave(&priv->sta_lock, flags_spin);
384                 IWL_ERR(priv, "Adding station %pM failed.\n",
385                         priv->stations[sta_id].sta.sta.addr);
386                 priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
387                 priv->stations[sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
388                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
389         }
390         *sta_id_r = sta_id;
391         return ret;
392 }
393 EXPORT_SYMBOL(iwl_legacy_add_station_common);
394
395 /**
396  * iwl_legacy_sta_ucode_deactivate - deactivate ucode status for a station
397  *
398  * priv->sta_lock must be held
399  */
400 static void iwl_legacy_sta_ucode_deactivate(struct iwl_priv *priv, u8 sta_id)
401 {
402         /* Ucode must be active and driver must be non active */
403         if ((priv->stations[sta_id].used &
404              (IWL_STA_UCODE_ACTIVE | IWL_STA_DRIVER_ACTIVE)) !=
405                                                 IWL_STA_UCODE_ACTIVE)
406                 IWL_ERR(priv, "removed non active STA %u\n", sta_id);
407
408         priv->stations[sta_id].used &= ~IWL_STA_UCODE_ACTIVE;
409
410         memset(&priv->stations[sta_id], 0, sizeof(struct iwl_station_entry));
411         IWL_DEBUG_ASSOC(priv, "Removed STA %u\n", sta_id);
412 }
413
414 static int iwl_legacy_send_remove_station(struct iwl_priv *priv,
415                                    const u8 *addr, int sta_id,
416                                    bool temporary)
417 {
418         struct iwl_rx_packet *pkt;
419         int ret;
420
421         unsigned long flags_spin;
422         struct iwl_rem_sta_cmd rm_sta_cmd;
423
424         struct iwl_host_cmd cmd = {
425                 .id = REPLY_REMOVE_STA,
426                 .len = sizeof(struct iwl_rem_sta_cmd),
427                 .flags = CMD_SYNC,
428                 .data = &rm_sta_cmd,
429         };
430
431         memset(&rm_sta_cmd, 0, sizeof(rm_sta_cmd));
432         rm_sta_cmd.num_sta = 1;
433         memcpy(&rm_sta_cmd.addr, addr, ETH_ALEN);
434
435         cmd.flags |= CMD_WANT_SKB;
436
437         ret = iwl_legacy_send_cmd(priv, &cmd);
438
439         if (ret)
440                 return ret;
441
442         pkt = (struct iwl_rx_packet *)cmd.reply_page;
443         if (pkt->hdr.flags & IWL_CMD_FAILED_MSK) {
444                 IWL_ERR(priv, "Bad return from REPLY_REMOVE_STA (0x%08X)\n",
445                           pkt->hdr.flags);
446                 ret = -EIO;
447         }
448
449         if (!ret) {
450                 switch (pkt->u.rem_sta.status) {
451                 case REM_STA_SUCCESS_MSK:
452                         if (!temporary) {
453                                 spin_lock_irqsave(&priv->sta_lock, flags_spin);
454                                 iwl_legacy_sta_ucode_deactivate(priv, sta_id);
455                                 spin_unlock_irqrestore(&priv->sta_lock,
456                                                                 flags_spin);
457                         }
458                         IWL_DEBUG_ASSOC(priv, "REPLY_REMOVE_STA PASSED\n");
459                         break;
460                 default:
461                         ret = -EIO;
462                         IWL_ERR(priv, "REPLY_REMOVE_STA failed\n");
463                         break;
464                 }
465         }
466         iwl_legacy_free_pages(priv, cmd.reply_page);
467
468         return ret;
469 }
470
471 /**
472  * iwl_legacy_remove_station - Remove driver's knowledge of station.
473  */
474 int iwl_legacy_remove_station(struct iwl_priv *priv, const u8 sta_id,
475                        const u8 *addr)
476 {
477         unsigned long flags;
478
479         if (!iwl_legacy_is_ready(priv)) {
480                 IWL_DEBUG_INFO(priv,
481                         "Unable to remove station %pM, device not ready.\n",
482                         addr);
483                 /*
484                  * It is typical for stations to be removed when we are
485                  * going down. Return success since device will be down
486                  * soon anyway
487                  */
488                 return 0;
489         }
490
491         IWL_DEBUG_ASSOC(priv, "Removing STA from driver:%d  %pM\n",
492                         sta_id, addr);
493
494         if (WARN_ON(sta_id == IWL_INVALID_STATION))
495                 return -EINVAL;
496
497         spin_lock_irqsave(&priv->sta_lock, flags);
498
499         if (!(priv->stations[sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
500                 IWL_DEBUG_INFO(priv, "Removing %pM but non DRIVER active\n",
501                                 addr);
502                 goto out_err;
503         }
504
505         if (!(priv->stations[sta_id].used & IWL_STA_UCODE_ACTIVE)) {
506                 IWL_DEBUG_INFO(priv, "Removing %pM but non UCODE active\n",
507                                 addr);
508                 goto out_err;
509         }
510
511         if (priv->stations[sta_id].used & IWL_STA_LOCAL) {
512                 kfree(priv->stations[sta_id].lq);
513                 priv->stations[sta_id].lq = NULL;
514         }
515
516         priv->stations[sta_id].used &= ~IWL_STA_DRIVER_ACTIVE;
517
518         priv->num_stations--;
519
520         BUG_ON(priv->num_stations < 0);
521
522         spin_unlock_irqrestore(&priv->sta_lock, flags);
523
524         return iwl_legacy_send_remove_station(priv, addr, sta_id, false);
525 out_err:
526         spin_unlock_irqrestore(&priv->sta_lock, flags);
527         return -EINVAL;
528 }
529 EXPORT_SYMBOL_GPL(iwl_legacy_remove_station);
530
531 /**
532  * iwl_legacy_clear_ucode_stations - clear ucode station table bits
533  *
534  * This function clears all the bits in the driver indicating
535  * which stations are active in the ucode. Call when something
536  * other than explicit station management would cause this in
537  * the ucode, e.g. unassociated RXON.
538  */
539 void iwl_legacy_clear_ucode_stations(struct iwl_priv *priv,
540                               struct iwl_rxon_context *ctx)
541 {
542         int i;
543         unsigned long flags_spin;
544         bool cleared = false;
545
546         IWL_DEBUG_INFO(priv, "Clearing ucode stations in driver\n");
547
548         spin_lock_irqsave(&priv->sta_lock, flags_spin);
549         for (i = 0; i < priv->hw_params.max_stations; i++) {
550                 if (ctx && ctx->ctxid != priv->stations[i].ctxid)
551                         continue;
552
553                 if (priv->stations[i].used & IWL_STA_UCODE_ACTIVE) {
554                         IWL_DEBUG_INFO(priv,
555                                 "Clearing ucode active for station %d\n", i);
556                         priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
557                         cleared = true;
558                 }
559         }
560         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
561
562         if (!cleared)
563                 IWL_DEBUG_INFO(priv,
564                         "No active stations found to be cleared\n");
565 }
566 EXPORT_SYMBOL(iwl_legacy_clear_ucode_stations);
567
568 /**
569  * iwl_legacy_restore_stations() - Restore driver known stations to device
570  *
571  * All stations considered active by driver, but not present in ucode, is
572  * restored.
573  *
574  * Function sleeps.
575  */
576 void
577 iwl_legacy_restore_stations(struct iwl_priv *priv, struct iwl_rxon_context *ctx)
578 {
579         struct iwl_legacy_addsta_cmd sta_cmd;
580         struct iwl_link_quality_cmd lq;
581         unsigned long flags_spin;
582         int i;
583         bool found = false;
584         int ret;
585         bool send_lq;
586
587         if (!iwl_legacy_is_ready(priv)) {
588                 IWL_DEBUG_INFO(priv,
589                         "Not ready yet, not restoring any stations.\n");
590                 return;
591         }
592
593         IWL_DEBUG_ASSOC(priv, "Restoring all known stations ... start.\n");
594         spin_lock_irqsave(&priv->sta_lock, flags_spin);
595         for (i = 0; i < priv->hw_params.max_stations; i++) {
596                 if (ctx->ctxid != priv->stations[i].ctxid)
597                         continue;
598                 if ((priv->stations[i].used & IWL_STA_DRIVER_ACTIVE) &&
599                             !(priv->stations[i].used & IWL_STA_UCODE_ACTIVE)) {
600                         IWL_DEBUG_ASSOC(priv, "Restoring sta %pM\n",
601                                         priv->stations[i].sta.sta.addr);
602                         priv->stations[i].sta.mode = 0;
603                         priv->stations[i].used |= IWL_STA_UCODE_INPROGRESS;
604                         found = true;
605                 }
606         }
607
608         for (i = 0; i < priv->hw_params.max_stations; i++) {
609                 if ((priv->stations[i].used & IWL_STA_UCODE_INPROGRESS)) {
610                         memcpy(&sta_cmd, &priv->stations[i].sta,
611                                sizeof(struct iwl_legacy_addsta_cmd));
612                         send_lq = false;
613                         if (priv->stations[i].lq) {
614                                 memcpy(&lq, priv->stations[i].lq,
615                                        sizeof(struct iwl_link_quality_cmd));
616                                 send_lq = true;
617                         }
618                         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
619                         ret = iwl_legacy_send_add_sta(priv, &sta_cmd, CMD_SYNC);
620                         if (ret) {
621                                 spin_lock_irqsave(&priv->sta_lock, flags_spin);
622                                 IWL_ERR(priv, "Adding station %pM failed.\n",
623                                         priv->stations[i].sta.sta.addr);
624                                 priv->stations[i].used &=
625                                                 ~IWL_STA_DRIVER_ACTIVE;
626                                 priv->stations[i].used &=
627                                                 ~IWL_STA_UCODE_INPROGRESS;
628                                 spin_unlock_irqrestore(&priv->sta_lock,
629                                                                 flags_spin);
630                         }
631                         /*
632                          * Rate scaling has already been initialized, send
633                          * current LQ command
634                          */
635                         if (send_lq)
636                                 iwl_legacy_send_lq_cmd(priv, ctx, &lq,
637                                                                 CMD_SYNC, true);
638                         spin_lock_irqsave(&priv->sta_lock, flags_spin);
639                         priv->stations[i].used &= ~IWL_STA_UCODE_INPROGRESS;
640                 }
641         }
642
643         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
644         if (!found)
645                 IWL_DEBUG_INFO(priv, "Restoring all known stations"
646                                 " .... no stations to be restored.\n");
647         else
648                 IWL_DEBUG_INFO(priv, "Restoring all known stations"
649                                 " .... complete.\n");
650 }
651 EXPORT_SYMBOL(iwl_legacy_restore_stations);
652
653 int iwl_legacy_get_free_ucode_key_index(struct iwl_priv *priv)
654 {
655         int i;
656
657         for (i = 0; i < priv->sta_key_max_num; i++)
658                 if (!test_and_set_bit(i, &priv->ucode_key_table))
659                         return i;
660
661         return WEP_INVALID_OFFSET;
662 }
663 EXPORT_SYMBOL(iwl_legacy_get_free_ucode_key_index);
664
665 void iwl_legacy_dealloc_bcast_stations(struct iwl_priv *priv)
666 {
667         unsigned long flags;
668         int i;
669
670         spin_lock_irqsave(&priv->sta_lock, flags);
671         for (i = 0; i < priv->hw_params.max_stations; i++) {
672                 if (!(priv->stations[i].used & IWL_STA_BCAST))
673                         continue;
674
675                 priv->stations[i].used &= ~IWL_STA_UCODE_ACTIVE;
676                 priv->num_stations--;
677                 BUG_ON(priv->num_stations < 0);
678                 kfree(priv->stations[i].lq);
679                 priv->stations[i].lq = NULL;
680         }
681         spin_unlock_irqrestore(&priv->sta_lock, flags);
682 }
683 EXPORT_SYMBOL_GPL(iwl_legacy_dealloc_bcast_stations);
684
685 #ifdef CONFIG_IWLWIFI_LEGACY_DEBUG
686 static void iwl_legacy_dump_lq_cmd(struct iwl_priv *priv,
687                            struct iwl_link_quality_cmd *lq)
688 {
689         int i;
690         IWL_DEBUG_RATE(priv, "lq station id 0x%x\n", lq->sta_id);
691         IWL_DEBUG_RATE(priv, "lq ant 0x%X 0x%X\n",
692                        lq->general_params.single_stream_ant_msk,
693                        lq->general_params.dual_stream_ant_msk);
694
695         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++)
696                 IWL_DEBUG_RATE(priv, "lq index %d 0x%X\n",
697                                i, lq->rs_table[i].rate_n_flags);
698 }
699 #else
700 static inline void iwl_legacy_dump_lq_cmd(struct iwl_priv *priv,
701                                    struct iwl_link_quality_cmd *lq)
702 {
703 }
704 #endif
705
706 /**
707  * iwl_legacy_is_lq_table_valid() - Test one aspect of LQ cmd for validity
708  *
709  * It sometimes happens when a HT rate has been in use and we
710  * loose connectivity with AP then mac80211 will first tell us that the
711  * current channel is not HT anymore before removing the station. In such a
712  * scenario the RXON flags will be updated to indicate we are not
713  * communicating HT anymore, but the LQ command may still contain HT rates.
714  * Test for this to prevent driver from sending LQ command between the time
715  * RXON flags are updated and when LQ command is updated.
716  */
717 static bool iwl_legacy_is_lq_table_valid(struct iwl_priv *priv,
718                               struct iwl_rxon_context *ctx,
719                               struct iwl_link_quality_cmd *lq)
720 {
721         int i;
722
723         if (ctx->ht.enabled)
724                 return true;
725
726         IWL_DEBUG_INFO(priv, "Channel %u is not an HT channel\n",
727                        ctx->active.channel);
728         for (i = 0; i < LINK_QUAL_MAX_RETRY_NUM; i++) {
729                 if (le32_to_cpu(lq->rs_table[i].rate_n_flags) &
730                                                 RATE_MCS_HT_MSK) {
731                         IWL_DEBUG_INFO(priv,
732                                        "index %d of LQ expects HT channel\n",
733                                        i);
734                         return false;
735                 }
736         }
737         return true;
738 }
739
740 /**
741  * iwl_legacy_send_lq_cmd() - Send link quality command
742  * @init: This command is sent as part of station initialization right
743  *        after station has been added.
744  *
745  * The link quality command is sent as the last step of station creation.
746  * This is the special case in which init is set and we call a callback in
747  * this case to clear the state indicating that station creation is in
748  * progress.
749  */
750 int iwl_legacy_send_lq_cmd(struct iwl_priv *priv, struct iwl_rxon_context *ctx,
751                     struct iwl_link_quality_cmd *lq, u8 flags, bool init)
752 {
753         int ret = 0;
754         unsigned long flags_spin;
755
756         struct iwl_host_cmd cmd = {
757                 .id = REPLY_TX_LINK_QUALITY_CMD,
758                 .len = sizeof(struct iwl_link_quality_cmd),
759                 .flags = flags,
760                 .data = lq,
761         };
762
763         if (WARN_ON(lq->sta_id == IWL_INVALID_STATION))
764                 return -EINVAL;
765
766
767         spin_lock_irqsave(&priv->sta_lock, flags_spin);
768         if (!(priv->stations[lq->sta_id].used & IWL_STA_DRIVER_ACTIVE)) {
769                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
770                 return -EINVAL;
771         }
772         spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
773
774         iwl_legacy_dump_lq_cmd(priv, lq);
775         BUG_ON(init && (cmd.flags & CMD_ASYNC));
776
777         if (iwl_legacy_is_lq_table_valid(priv, ctx, lq))
778                 ret = iwl_legacy_send_cmd(priv, &cmd);
779         else
780                 ret = -EINVAL;
781
782         if (cmd.flags & CMD_ASYNC)
783                 return ret;
784
785         if (init) {
786                 IWL_DEBUG_INFO(priv, "init LQ command complete,"
787                                 " clearing sta addition status for sta %d\n",
788                                lq->sta_id);
789                 spin_lock_irqsave(&priv->sta_lock, flags_spin);
790                 priv->stations[lq->sta_id].used &= ~IWL_STA_UCODE_INPROGRESS;
791                 spin_unlock_irqrestore(&priv->sta_lock, flags_spin);
792         }
793         return ret;
794 }
795 EXPORT_SYMBOL(iwl_legacy_send_lq_cmd);
796
797 int iwl_legacy_mac_sta_remove(struct ieee80211_hw *hw,
798                        struct ieee80211_vif *vif,
799                        struct ieee80211_sta *sta)
800 {
801         struct iwl_priv *priv = hw->priv;
802         struct iwl_station_priv_common *sta_common = (void *)sta->drv_priv;
803         int ret;
804
805         IWL_DEBUG_INFO(priv, "received request to remove station %pM\n",
806                         sta->addr);
807         mutex_lock(&priv->mutex);
808         IWL_DEBUG_INFO(priv, "proceeding to remove station %pM\n",
809                         sta->addr);
810         ret = iwl_legacy_remove_station(priv, sta_common->sta_id, sta->addr);
811         if (ret)
812                 IWL_ERR(priv, "Error removing station %pM\n",
813                         sta->addr);
814         mutex_unlock(&priv->mutex);
815         return ret;
816 }
817 EXPORT_SYMBOL(iwl_legacy_mac_sta_remove);