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