blob: 44ca10cb0d393aa4b83d9a94c69530d10f24697f [file] [log] [blame]
Solomon Peachya910e4a2013-05-24 20:04:38 -04001/*
2 * Datapath implementation for ST-Ericsson CW1200 mac80211 drivers
3 *
4 * Copyright (c) 2010, ST-Ericsson
5 * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 */
11
12#include <net/mac80211.h>
13#include <linux/etherdevice.h>
14#include <linux/skbuff.h>
15
16#include "cw1200.h"
17#include "wsm.h"
18#include "bh.h"
19#include "sta.h"
20#include "debug.h"
21
22#define CW1200_INVALID_RATE_ID (0xFF)
23
24static int cw1200_handle_action_rx(struct cw1200_common *priv,
25 struct sk_buff *skb);
26static const struct ieee80211_rate *
27cw1200_get_tx_rate(const struct cw1200_common *priv,
28 const struct ieee80211_tx_rate *rate);
29
30/* ******************************************************************** */
31/* TX queue lock / unlock */
32
33static inline void cw1200_tx_queues_lock(struct cw1200_common *priv)
34{
35 int i;
36 for (i = 0; i < 4; ++i)
37 cw1200_queue_lock(&priv->tx_queue[i]);
38}
39
40static inline void cw1200_tx_queues_unlock(struct cw1200_common *priv)
41{
42 int i;
43 for (i = 0; i < 4; ++i)
44 cw1200_queue_unlock(&priv->tx_queue[i]);
45}
46
47/* ******************************************************************** */
48/* TX policy cache implementation */
49
50static void tx_policy_dump(struct tx_policy *policy)
51{
52 pr_debug("[TX policy] %.1X%.1X%.1X%.1X%.1X%.1X%.1X%.1X %.1X%.1X%.1X%.1X%.1X%.1X%.1X%.1X %.1X%.1X%.1X%.1X%.1X%.1X%.1X%.1X: %d\n",
53 policy->raw[0] & 0x0F, policy->raw[0] >> 4,
54 policy->raw[1] & 0x0F, policy->raw[1] >> 4,
55 policy->raw[2] & 0x0F, policy->raw[2] >> 4,
56 policy->raw[3] & 0x0F, policy->raw[3] >> 4,
57 policy->raw[4] & 0x0F, policy->raw[4] >> 4,
58 policy->raw[5] & 0x0F, policy->raw[5] >> 4,
59 policy->raw[6] & 0x0F, policy->raw[6] >> 4,
60 policy->raw[7] & 0x0F, policy->raw[7] >> 4,
61 policy->raw[8] & 0x0F, policy->raw[8] >> 4,
62 policy->raw[9] & 0x0F, policy->raw[9] >> 4,
63 policy->raw[10] & 0x0F, policy->raw[10] >> 4,
64 policy->raw[11] & 0x0F, policy->raw[11] >> 4,
65 policy->defined);
66}
67
68static void tx_policy_build(const struct cw1200_common *priv,
69 /* [out] */ struct tx_policy *policy,
70 struct ieee80211_tx_rate *rates, size_t count)
71{
72 int i, j;
73 unsigned limit = priv->short_frame_max_tx_count;
74 unsigned total = 0;
75 BUG_ON(rates[0].idx < 0);
76 memset(policy, 0, sizeof(*policy));
77
Solomon Peachya910e4a2013-05-24 20:04:38 -040078 /* Sort rates in descending order. */
79 for (i = 1; i < count; ++i) {
80 if (rates[i].idx < 0) {
81 count = i;
82 break;
83 }
84 if (rates[i].idx > rates[i - 1].idx) {
85 struct ieee80211_tx_rate tmp = rates[i - 1];
86 rates[i - 1] = rates[i];
87 rates[i] = tmp;
88 }
89 }
90
91 /* Eliminate duplicates. */
92 total = rates[0].count;
93 for (i = 0, j = 1; j < count; ++j) {
94 if (rates[j].idx == rates[i].idx) {
95 rates[i].count += rates[j].count;
96 } else if (rates[j].idx > rates[i].idx) {
97 break;
98 } else {
99 ++i;
100 if (i != j)
101 rates[i] = rates[j];
102 }
103 total += rates[j].count;
104 }
105 count = i + 1;
106
107 /* Re-fill policy trying to keep every requested rate and with
Solomon Peachy8b3e7be2013-06-11 09:49:40 -0400108 * respect to the global max tx retransmission count.
109 */
Solomon Peachya910e4a2013-05-24 20:04:38 -0400110 if (limit < count)
111 limit = count;
112 if (total > limit) {
113 for (i = 0; i < count; ++i) {
114 int left = count - i - 1;
115 if (rates[i].count > limit - left)
116 rates[i].count = limit - left;
117 limit -= rates[i].count;
118 }
119 }
120
121 /* HACK!!! Device has problems (at least) switching from
122 * 54Mbps CTS to 1Mbps. This switch takes enormous amount
123 * of time (100-200 ms), leading to valuable throughput drop.
124 * As a workaround, additional g-rates are injected to the
125 * policy.
126 */
127 if (count == 2 && !(rates[0].flags & IEEE80211_TX_RC_MCS) &&
128 rates[0].idx > 4 && rates[0].count > 2 &&
129 rates[1].idx < 2) {
Solomon Peachya910e4a2013-05-24 20:04:38 -0400130 int mid_rate = (rates[0].idx + 4) >> 1;
131
132 /* Decrease number of retries for the initial rate */
133 rates[0].count -= 2;
134
135 if (mid_rate != 4) {
136 /* Keep fallback rate at 1Mbps. */
137 rates[3] = rates[1];
138
139 /* Inject 1 transmission on lowest g-rate */
140 rates[2].idx = 4;
141 rates[2].count = 1;
142 rates[2].flags = rates[1].flags;
143
144 /* Inject 1 transmission on mid-rate */
145 rates[1].idx = mid_rate;
146 rates[1].count = 1;
147
148 /* Fallback to 1 Mbps is a really bad thing,
149 * so let's try to increase probability of
150 * successful transmission on the lowest g rate
Solomon Peachy8b3e7be2013-06-11 09:49:40 -0400151 * even more
152 */
Solomon Peachya910e4a2013-05-24 20:04:38 -0400153 if (rates[0].count >= 3) {
154 --rates[0].count;
155 ++rates[2].count;
156 }
157
158 /* Adjust amount of rates defined */
159 count += 2;
160 } else {
161 /* Keep fallback rate at 1Mbps. */
162 rates[2] = rates[1];
163
164 /* Inject 2 transmissions on lowest g-rate */
165 rates[1].idx = 4;
166 rates[1].count = 2;
167
168 /* Adjust amount of rates defined */
169 count += 1;
170 }
171 }
172
173 policy->defined = cw1200_get_tx_rate(priv, &rates[0])->hw_value + 1;
174
175 for (i = 0; i < count; ++i) {
176 register unsigned rateid, off, shift, retries;
177
178 rateid = cw1200_get_tx_rate(priv, &rates[i])->hw_value;
179 off = rateid >> 3; /* eq. rateid / 8 */
180 shift = (rateid & 0x07) << 2; /* eq. (rateid % 8) * 4 */
181
182 retries = rates[i].count;
183 if (retries > 0x0F) {
184 rates[i].count = 0x0f;
185 retries = 0x0F;
186 }
187 policy->tbl[off] |= __cpu_to_le32(retries << shift);
188 policy->retry_count += retries;
189 }
190
Dan Carpenterf28bc922013-06-06 10:43:00 +0300191 pr_debug("[TX policy] Policy (%zu): %d:%d, %d:%d, %d:%d, %d:%d\n",
Solomon Peachya910e4a2013-05-24 20:04:38 -0400192 count,
193 rates[0].idx, rates[0].count,
194 rates[1].idx, rates[1].count,
195 rates[2].idx, rates[2].count,
Dan Carpenterf28bc922013-06-06 10:43:00 +0300196 rates[3].idx, rates[3].count);
Solomon Peachya910e4a2013-05-24 20:04:38 -0400197}
198
199static inline bool tx_policy_is_equal(const struct tx_policy *wanted,
200 const struct tx_policy *cached)
201{
202 size_t count = wanted->defined >> 1;
203 if (wanted->defined > cached->defined)
204 return false;
205 if (count) {
206 if (memcmp(wanted->raw, cached->raw, count))
207 return false;
208 }
209 if (wanted->defined & 1) {
210 if ((wanted->raw[count] & 0x0F) != (cached->raw[count] & 0x0F))
211 return false;
212 }
213 return true;
214}
215
216static int tx_policy_find(struct tx_policy_cache *cache,
217 const struct tx_policy *wanted)
218{
219 /* O(n) complexity. Not so good, but there's only 8 entries in
220 * the cache.
Solomon Peachy8b3e7be2013-06-11 09:49:40 -0400221 * Also lru helps to reduce search time.
222 */
Solomon Peachya910e4a2013-05-24 20:04:38 -0400223 struct tx_policy_cache_entry *it;
224 /* First search for policy in "used" list */
225 list_for_each_entry(it, &cache->used, link) {
226 if (tx_policy_is_equal(wanted, &it->policy))
227 return it - cache->cache;
228 }
229 /* Then - in "free list" */
230 list_for_each_entry(it, &cache->free, link) {
231 if (tx_policy_is_equal(wanted, &it->policy))
232 return it - cache->cache;
233 }
234 return -1;
235}
236
237static inline void tx_policy_use(struct tx_policy_cache *cache,
238 struct tx_policy_cache_entry *entry)
239{
240 ++entry->policy.usage_count;
241 list_move(&entry->link, &cache->used);
242}
243
244static inline int tx_policy_release(struct tx_policy_cache *cache,
245 struct tx_policy_cache_entry *entry)
246{
247 int ret = --entry->policy.usage_count;
248 if (!ret)
249 list_move(&entry->link, &cache->free);
250 return ret;
251}
252
253void tx_policy_clean(struct cw1200_common *priv)
254{
255 int idx, locked;
256 struct tx_policy_cache *cache = &priv->tx_policy_cache;
257 struct tx_policy_cache_entry *entry;
258
259 cw1200_tx_queues_lock(priv);
260 spin_lock_bh(&cache->lock);
261 locked = list_empty(&cache->free);
262
263 for (idx = 0; idx < TX_POLICY_CACHE_SIZE; idx++) {
264 entry = &cache->cache[idx];
265 /* Policy usage count should be 0 at this time as all queues
Solomon Peachy8b3e7be2013-06-11 09:49:40 -0400266 should be empty
267 */
Solomon Peachya910e4a2013-05-24 20:04:38 -0400268 if (WARN_ON(entry->policy.usage_count)) {
269 entry->policy.usage_count = 0;
270 list_move(&entry->link, &cache->free);
271 }
272 memset(&entry->policy, 0, sizeof(entry->policy));
273 }
274 if (locked)
275 cw1200_tx_queues_unlock(priv);
276
277 cw1200_tx_queues_unlock(priv);
278 spin_unlock_bh(&cache->lock);
279}
280
281/* ******************************************************************** */
282/* External TX policy cache API */
283
284void tx_policy_init(struct cw1200_common *priv)
285{
286 struct tx_policy_cache *cache = &priv->tx_policy_cache;
287 int i;
288
289 memset(cache, 0, sizeof(*cache));
290
291 spin_lock_init(&cache->lock);
292 INIT_LIST_HEAD(&cache->used);
293 INIT_LIST_HEAD(&cache->free);
294
295 for (i = 0; i < TX_POLICY_CACHE_SIZE; ++i)
296 list_add(&cache->cache[i].link, &cache->free);
297}
298
299static int tx_policy_get(struct cw1200_common *priv,
300 struct ieee80211_tx_rate *rates,
301 size_t count, bool *renew)
302{
303 int idx;
304 struct tx_policy_cache *cache = &priv->tx_policy_cache;
305 struct tx_policy wanted;
306
307 tx_policy_build(priv, &wanted, rates, count);
308
309 spin_lock_bh(&cache->lock);
310 if (WARN_ON_ONCE(list_empty(&cache->free))) {
311 spin_unlock_bh(&cache->lock);
312 return CW1200_INVALID_RATE_ID;
313 }
314 idx = tx_policy_find(cache, &wanted);
315 if (idx >= 0) {
316 pr_debug("[TX policy] Used TX policy: %d\n", idx);
317 *renew = false;
318 } else {
319 struct tx_policy_cache_entry *entry;
320 *renew = true;
321 /* If policy is not found create a new one
Solomon Peachy8b3e7be2013-06-11 09:49:40 -0400322 * using the oldest entry in "free" list
323 */
Solomon Peachya910e4a2013-05-24 20:04:38 -0400324 entry = list_entry(cache->free.prev,
325 struct tx_policy_cache_entry, link);
326 entry->policy = wanted;
327 idx = entry - cache->cache;
328 pr_debug("[TX policy] New TX policy: %d\n", idx);
329 tx_policy_dump(&entry->policy);
330 }
331 tx_policy_use(cache, &cache->cache[idx]);
332 if (list_empty(&cache->free)) {
333 /* Lock TX queues. */
334 cw1200_tx_queues_lock(priv);
335 }
336 spin_unlock_bh(&cache->lock);
337 return idx;
338}
339
340static void tx_policy_put(struct cw1200_common *priv, int idx)
341{
342 int usage, locked;
343 struct tx_policy_cache *cache = &priv->tx_policy_cache;
344
345 spin_lock_bh(&cache->lock);
346 locked = list_empty(&cache->free);
347 usage = tx_policy_release(cache, &cache->cache[idx]);
348 if (locked && !usage) {
349 /* Unlock TX queues. */
350 cw1200_tx_queues_unlock(priv);
351 }
352 spin_unlock_bh(&cache->lock);
353}
354
355static int tx_policy_upload(struct cw1200_common *priv)
356{
357 struct tx_policy_cache *cache = &priv->tx_policy_cache;
358 int i;
359 struct wsm_set_tx_rate_retry_policy arg = {
360 .num = 0,
361 };
362 spin_lock_bh(&cache->lock);
363
364 /* Upload only modified entries. */
365 for (i = 0; i < TX_POLICY_CACHE_SIZE; ++i) {
366 struct tx_policy *src = &cache->cache[i].policy;
367 if (src->retry_count && !src->uploaded) {
368 struct wsm_tx_rate_retry_policy *dst =
369 &arg.tbl[arg.num];
370 dst->index = i;
371 dst->short_retries = priv->short_frame_max_tx_count;
372 dst->long_retries = priv->long_frame_max_tx_count;
373
374 dst->flags = WSM_TX_RATE_POLICY_FLAG_TERMINATE_WHEN_FINISHED |
375 WSM_TX_RATE_POLICY_FLAG_COUNT_INITIAL_TRANSMIT;
376 memcpy(dst->rate_count_indices, src->tbl,
377 sizeof(dst->rate_count_indices));
378 src->uploaded = 1;
379 ++arg.num;
380 }
381 }
382 spin_unlock_bh(&cache->lock);
383 cw1200_debug_tx_cache_miss(priv);
384 pr_debug("[TX policy] Upload %d policies\n", arg.num);
385 return wsm_set_tx_rate_retry_policy(priv, &arg);
386}
387
388void tx_policy_upload_work(struct work_struct *work)
389{
390 struct cw1200_common *priv =
391 container_of(work, struct cw1200_common, tx_policy_upload_work);
392
393 pr_debug("[TX] TX policy upload.\n");
394 tx_policy_upload(priv);
395
396 wsm_unlock_tx(priv);
397 cw1200_tx_queues_unlock(priv);
398}
399
400/* ******************************************************************** */
401/* cw1200 TX implementation */
402
403struct cw1200_txinfo {
404 struct sk_buff *skb;
405 unsigned queue;
406 struct ieee80211_tx_info *tx_info;
407 const struct ieee80211_rate *rate;
408 struct ieee80211_hdr *hdr;
409 size_t hdrlen;
410 const u8 *da;
411 struct cw1200_sta_priv *sta_priv;
412 struct ieee80211_sta *sta;
413 struct cw1200_txpriv txpriv;
414};
415
416u32 cw1200_rate_mask_to_wsm(struct cw1200_common *priv, u32 rates)
417{
418 u32 ret = 0;
419 int i;
420 for (i = 0; i < 32; ++i) {
421 if (rates & BIT(i))
422 ret |= BIT(priv->rates[i].hw_value);
423 }
424 return ret;
425}
426
427static const struct ieee80211_rate *
428cw1200_get_tx_rate(const struct cw1200_common *priv,
429 const struct ieee80211_tx_rate *rate)
430{
431 if (rate->idx < 0)
432 return NULL;
433 if (rate->flags & IEEE80211_TX_RC_MCS)
434 return &priv->mcs_rates[rate->idx];
435 return &priv->hw->wiphy->bands[priv->channel->band]->
436 bitrates[rate->idx];
437}
438
439static int
440cw1200_tx_h_calc_link_ids(struct cw1200_common *priv,
441 struct cw1200_txinfo *t)
442{
443 if (t->sta && t->sta_priv->link_id)
444 t->txpriv.raw_link_id =
445 t->txpriv.link_id =
446 t->sta_priv->link_id;
447 else if (priv->mode != NL80211_IFTYPE_AP)
448 t->txpriv.raw_link_id =
449 t->txpriv.link_id = 0;
450 else if (is_multicast_ether_addr(t->da)) {
451 if (priv->enable_beacon) {
452 t->txpriv.raw_link_id = 0;
453 t->txpriv.link_id = CW1200_LINK_ID_AFTER_DTIM;
454 } else {
455 t->txpriv.raw_link_id = 0;
456 t->txpriv.link_id = 0;
457 }
458 } else {
459 t->txpriv.link_id = cw1200_find_link_id(priv, t->da);
460 if (!t->txpriv.link_id)
461 t->txpriv.link_id = cw1200_alloc_link_id(priv, t->da);
462 if (!t->txpriv.link_id) {
463 wiphy_err(priv->hw->wiphy,
464 "No more link IDs available.\n");
465 return -ENOENT;
466 }
467 t->txpriv.raw_link_id = t->txpriv.link_id;
468 }
469 if (t->txpriv.raw_link_id)
470 priv->link_id_db[t->txpriv.raw_link_id - 1].timestamp =
471 jiffies;
472 if (t->sta && (t->sta->uapsd_queues & BIT(t->queue)))
473 t->txpriv.link_id = CW1200_LINK_ID_UAPSD;
474 return 0;
475}
476
477static void
478cw1200_tx_h_pm(struct cw1200_common *priv,
479 struct cw1200_txinfo *t)
480{
481 if (ieee80211_is_auth(t->hdr->frame_control)) {
482 u32 mask = ~BIT(t->txpriv.raw_link_id);
483 spin_lock_bh(&priv->ps_state_lock);
484 priv->sta_asleep_mask &= mask;
485 priv->pspoll_mask &= mask;
486 spin_unlock_bh(&priv->ps_state_lock);
487 }
488}
489
490static void
491cw1200_tx_h_calc_tid(struct cw1200_common *priv,
492 struct cw1200_txinfo *t)
493{
494 if (ieee80211_is_data_qos(t->hdr->frame_control)) {
495 u8 *qos = ieee80211_get_qos_ctl(t->hdr);
496 t->txpriv.tid = qos[0] & IEEE80211_QOS_CTL_TID_MASK;
497 } else if (ieee80211_is_data(t->hdr->frame_control)) {
498 t->txpriv.tid = 0;
499 }
500}
501
502static int
503cw1200_tx_h_crypt(struct cw1200_common *priv,
504 struct cw1200_txinfo *t)
505{
506 if (!t->tx_info->control.hw_key ||
507 !ieee80211_has_protected(t->hdr->frame_control))
508 return 0;
509
510 t->hdrlen += t->tx_info->control.hw_key->iv_len;
511 skb_put(t->skb, t->tx_info->control.hw_key->icv_len);
512
513 if (t->tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
514 skb_put(t->skb, 8); /* MIC space */
515
516 return 0;
517}
518
519static int
520cw1200_tx_h_align(struct cw1200_common *priv,
521 struct cw1200_txinfo *t,
522 u8 *flags)
523{
524 size_t offset = (size_t)t->skb->data & 3;
525
526 if (!offset)
527 return 0;
528
529 if (offset & 1) {
530 wiphy_err(priv->hw->wiphy,
531 "Bug: attempt to transmit a frame with wrong alignment: %zu\n",
532 offset);
533 return -EINVAL;
534 }
535
536 if (skb_headroom(t->skb) < offset) {
537 wiphy_err(priv->hw->wiphy,
538 "Bug: no space allocated for DMA alignment. headroom: %d\n",
539 skb_headroom(t->skb));
540 return -ENOMEM;
541 }
542 skb_push(t->skb, offset);
543 t->hdrlen += offset;
544 t->txpriv.offset += offset;
545 *flags |= WSM_TX_2BYTES_SHIFT;
546 cw1200_debug_tx_align(priv);
547 return 0;
548}
549
550static int
551cw1200_tx_h_action(struct cw1200_common *priv,
552 struct cw1200_txinfo *t)
553{
554 struct ieee80211_mgmt *mgmt =
555 (struct ieee80211_mgmt *)t->hdr;
556 if (ieee80211_is_action(t->hdr->frame_control) &&
557 mgmt->u.action.category == WLAN_CATEGORY_BACK)
558 return 1;
559 else
560 return 0;
561}
562
563/* Add WSM header */
564static struct wsm_tx *
565cw1200_tx_h_wsm(struct cw1200_common *priv,
566 struct cw1200_txinfo *t)
567{
568 struct wsm_tx *wsm;
569
570 if (skb_headroom(t->skb) < sizeof(struct wsm_tx)) {
571 wiphy_err(priv->hw->wiphy,
572 "Bug: no space allocated for WSM header. headroom: %d\n",
573 skb_headroom(t->skb));
574 return NULL;
575 }
576
577 wsm = (struct wsm_tx *)skb_push(t->skb, sizeof(struct wsm_tx));
578 t->txpriv.offset += sizeof(struct wsm_tx);
579 memset(wsm, 0, sizeof(*wsm));
580 wsm->hdr.len = __cpu_to_le16(t->skb->len);
581 wsm->hdr.id = __cpu_to_le16(0x0004);
582 wsm->queue_id = wsm_queue_id_to_wsm(t->queue);
583 return wsm;
584}
585
586/* BT Coex specific handling */
587static void
588cw1200_tx_h_bt(struct cw1200_common *priv,
589 struct cw1200_txinfo *t,
590 struct wsm_tx *wsm)
591{
592 u8 priority = 0;
593
594 if (!priv->bt_present)
595 return;
596
597 if (ieee80211_is_nullfunc(t->hdr->frame_control)) {
598 priority = WSM_EPTA_PRIORITY_MGT;
599 } else if (ieee80211_is_data(t->hdr->frame_control)) {
600 /* Skip LLC SNAP header (+6) */
601 u8 *payload = &t->skb->data[t->hdrlen];
602 u16 *ethertype = (u16 *)&payload[6];
603 if (*ethertype == __be16_to_cpu(ETH_P_PAE))
604 priority = WSM_EPTA_PRIORITY_EAPOL;
605 } else if (ieee80211_is_assoc_req(t->hdr->frame_control) ||
606 ieee80211_is_reassoc_req(t->hdr->frame_control)) {
607 struct ieee80211_mgmt *mgt_frame =
608 (struct ieee80211_mgmt *)t->hdr;
609
610 if (mgt_frame->u.assoc_req.listen_interval <
611 priv->listen_interval) {
612 pr_debug("Modified Listen Interval to %d from %d\n",
613 priv->listen_interval,
614 mgt_frame->u.assoc_req.listen_interval);
615 /* Replace listen interval derieved from
Solomon Peachy8b3e7be2013-06-11 09:49:40 -0400616 * the one read from SDD
617 */
Solomon Peachya910e4a2013-05-24 20:04:38 -0400618 mgt_frame->u.assoc_req.listen_interval =
619 priv->listen_interval;
620 }
621 }
622
623 if (!priority) {
624 if (ieee80211_is_action(t->hdr->frame_control))
625 priority = WSM_EPTA_PRIORITY_ACTION;
626 else if (ieee80211_is_mgmt(t->hdr->frame_control))
627 priority = WSM_EPTA_PRIORITY_MGT;
628 else if ((wsm->queue_id == WSM_QUEUE_VOICE))
629 priority = WSM_EPTA_PRIORITY_VOICE;
630 else if ((wsm->queue_id == WSM_QUEUE_VIDEO))
631 priority = WSM_EPTA_PRIORITY_VIDEO;
632 else
633 priority = WSM_EPTA_PRIORITY_DATA;
634 }
635
636 pr_debug("[TX] EPTA priority %d.\n", priority);
637
638 wsm->flags |= priority << 1;
639}
640
641static int
642cw1200_tx_h_rate_policy(struct cw1200_common *priv,
643 struct cw1200_txinfo *t,
644 struct wsm_tx *wsm)
645{
646 bool tx_policy_renew = false;
647
648 t->txpriv.rate_id = tx_policy_get(priv,
649 t->tx_info->control.rates, IEEE80211_TX_MAX_RATES,
650 &tx_policy_renew);
651 if (t->txpriv.rate_id == CW1200_INVALID_RATE_ID)
652 return -EFAULT;
653
654 wsm->flags |= t->txpriv.rate_id << 4;
655
656 t->rate = cw1200_get_tx_rate(priv,
657 &t->tx_info->control.rates[0]),
658 wsm->max_tx_rate = t->rate->hw_value;
659 if (t->rate->flags & IEEE80211_TX_RC_MCS) {
660 if (cw1200_ht_greenfield(&priv->ht_info))
661 wsm->ht_tx_parameters |=
662 __cpu_to_le32(WSM_HT_TX_GREENFIELD);
663 else
664 wsm->ht_tx_parameters |=
665 __cpu_to_le32(WSM_HT_TX_MIXED);
666 }
667
668 if (tx_policy_renew) {
669 pr_debug("[TX] TX policy renew.\n");
670 /* It's not so optimal to stop TX queues every now and then.
671 * Better to reimplement task scheduling with
Solomon Peachy8b3e7be2013-06-11 09:49:40 -0400672 * a counter. TODO.
673 */
Solomon Peachya910e4a2013-05-24 20:04:38 -0400674 wsm_lock_tx_async(priv);
675 cw1200_tx_queues_lock(priv);
676 if (queue_work(priv->workqueue,
677 &priv->tx_policy_upload_work) <= 0) {
678 cw1200_tx_queues_unlock(priv);
679 wsm_unlock_tx(priv);
680 }
681 }
682 return 0;
683}
684
685static bool
686cw1200_tx_h_pm_state(struct cw1200_common *priv,
687 struct cw1200_txinfo *t)
688{
689 int was_buffered = 1;
690
691 if (t->txpriv.link_id == CW1200_LINK_ID_AFTER_DTIM &&
692 !priv->buffered_multicasts) {
693 priv->buffered_multicasts = true;
694 if (priv->sta_asleep_mask)
695 queue_work(priv->workqueue,
696 &priv->multicast_start_work);
697 }
698
699 if (t->txpriv.raw_link_id && t->txpriv.tid < CW1200_MAX_TID)
700 was_buffered = priv->link_id_db[t->txpriv.raw_link_id - 1].buffered[t->txpriv.tid]++;
701
702 return !was_buffered;
703}
704
705/* ******************************************************************** */
706
707void cw1200_tx(struct ieee80211_hw *dev,
708 struct ieee80211_tx_control *control,
709 struct sk_buff *skb)
710{
711 struct cw1200_common *priv = dev->priv;
712 struct cw1200_txinfo t = {
713 .skb = skb,
714 .queue = skb_get_queue_mapping(skb),
715 .tx_info = IEEE80211_SKB_CB(skb),
716 .hdr = (struct ieee80211_hdr *)skb->data,
717 .txpriv.tid = CW1200_MAX_TID,
718 .txpriv.rate_id = CW1200_INVALID_RATE_ID,
719 };
720 struct ieee80211_sta *sta;
721 struct wsm_tx *wsm;
722 bool tid_update = 0;
723 u8 flags = 0;
724 int ret;
725
726 if (priv->bh_error)
727 goto drop;
728
729 t.hdrlen = ieee80211_hdrlen(t.hdr->frame_control);
730 t.da = ieee80211_get_DA(t.hdr);
731 if (control) {
732 t.sta = control->sta;
733 t.sta_priv = (struct cw1200_sta_priv *)&t.sta->drv_priv;
734 }
735
736 if (WARN_ON(t.queue >= 4))
737 goto drop;
738
739 ret = cw1200_tx_h_calc_link_ids(priv, &t);
740 if (ret)
741 goto drop;
742
743 pr_debug("[TX] TX %d bytes (queue: %d, link_id: %d (%d)).\n",
744 skb->len, t.queue, t.txpriv.link_id,
745 t.txpriv.raw_link_id);
746
747 cw1200_tx_h_pm(priv, &t);
748 cw1200_tx_h_calc_tid(priv, &t);
749 ret = cw1200_tx_h_crypt(priv, &t);
750 if (ret)
751 goto drop;
752 ret = cw1200_tx_h_align(priv, &t, &flags);
753 if (ret)
754 goto drop;
755 ret = cw1200_tx_h_action(priv, &t);
756 if (ret)
757 goto drop;
758 wsm = cw1200_tx_h_wsm(priv, &t);
759 if (!wsm) {
760 ret = -ENOMEM;
761 goto drop;
762 }
763 wsm->flags |= flags;
764 cw1200_tx_h_bt(priv, &t, wsm);
765 ret = cw1200_tx_h_rate_policy(priv, &t, wsm);
766 if (ret)
767 goto drop;
768
769 rcu_read_lock();
770 sta = rcu_dereference(t.sta);
771
772 spin_lock_bh(&priv->ps_state_lock);
773 {
774 tid_update = cw1200_tx_h_pm_state(priv, &t);
775 BUG_ON(cw1200_queue_put(&priv->tx_queue[t.queue],
776 t.skb, &t.txpriv));
777 }
778 spin_unlock_bh(&priv->ps_state_lock);
779
780 if (tid_update && sta)
781 ieee80211_sta_set_buffered(sta, t.txpriv.tid, true);
782
783 rcu_read_unlock();
784
785 cw1200_bh_wakeup(priv);
786
787 return;
788
789drop:
790 cw1200_skb_dtor(priv, skb, &t.txpriv);
791 return;
792}
793
794/* ******************************************************************** */
795
796static int cw1200_handle_action_rx(struct cw1200_common *priv,
797 struct sk_buff *skb)
798{
799 struct ieee80211_mgmt *mgmt = (void *)skb->data;
800
801 /* Filter block ACK negotiation: fully controlled by firmware */
802 if (mgmt->u.action.category == WLAN_CATEGORY_BACK)
803 return 1;
804
805 return 0;
806}
807
808static int cw1200_handle_pspoll(struct cw1200_common *priv,
809 struct sk_buff *skb)
810{
811 struct ieee80211_sta *sta;
812 struct ieee80211_pspoll *pspoll = (struct ieee80211_pspoll *)skb->data;
813 int link_id = 0;
814 u32 pspoll_mask = 0;
815 int drop = 1;
816 int i;
817
818 if (priv->join_status != CW1200_JOIN_STATUS_AP)
819 goto done;
820 if (memcmp(priv->vif->addr, pspoll->bssid, ETH_ALEN))
821 goto done;
822
823 rcu_read_lock();
824 sta = ieee80211_find_sta(priv->vif, pspoll->ta);
825 if (sta) {
826 struct cw1200_sta_priv *sta_priv;
827 sta_priv = (struct cw1200_sta_priv *)&sta->drv_priv;
828 link_id = sta_priv->link_id;
829 pspoll_mask = BIT(sta_priv->link_id);
830 }
831 rcu_read_unlock();
832 if (!link_id)
833 goto done;
834
835 priv->pspoll_mask |= pspoll_mask;
836 drop = 0;
837
Solomon Peachy8b3e7be2013-06-11 09:49:40 -0400838 /* Do not report pspols if data for given link id is queued already. */
Solomon Peachya910e4a2013-05-24 20:04:38 -0400839 for (i = 0; i < 4; ++i) {
840 if (cw1200_queue_get_num_queued(&priv->tx_queue[i],
841 pspoll_mask)) {
842 cw1200_bh_wakeup(priv);
843 drop = 1;
844 break;
845 }
846 }
847 pr_debug("[RX] PSPOLL: %s\n", drop ? "local" : "fwd");
848done:
849 return drop;
850}
851
852/* ******************************************************************** */
853
854void cw1200_tx_confirm_cb(struct cw1200_common *priv,
855 int link_id,
856 struct wsm_tx_confirm *arg)
857{
858 u8 queue_id = cw1200_queue_get_queue_id(arg->packet_id);
859 struct cw1200_queue *queue = &priv->tx_queue[queue_id];
860 struct sk_buff *skb;
861 const struct cw1200_txpriv *txpriv;
862
863 pr_debug("[TX] TX confirm: %d, %d.\n",
864 arg->status, arg->ack_failures);
865
Solomon Peachya910e4a2013-05-24 20:04:38 -0400866 if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
867 /* STA is stopped. */
868 return;
869 }
870
871 if (WARN_ON(queue_id >= 4))
872 return;
873
874 if (arg->status)
875 pr_debug("TX failed: %d.\n", arg->status);
876
877 if ((arg->status == WSM_REQUEUE) &&
878 (arg->flags & WSM_TX_STATUS_REQUEUE)) {
879 /* "Requeue" means "implicit suspend" */
880 struct wsm_suspend_resume suspend = {
881 .link_id = link_id,
882 .stop = 1,
883 .multicast = !link_id,
884 };
885 cw1200_suspend_resume(priv, &suspend);
886 wiphy_warn(priv->hw->wiphy, "Requeue for link_id %d (try %d). STAs asleep: 0x%.8X\n",
887 link_id,
888 cw1200_queue_get_generation(arg->packet_id) + 1,
889 priv->sta_asleep_mask);
890 cw1200_queue_requeue(queue, arg->packet_id);
891 spin_lock_bh(&priv->ps_state_lock);
892 if (!link_id) {
893 priv->buffered_multicasts = true;
894 if (priv->sta_asleep_mask) {
895 queue_work(priv->workqueue,
896 &priv->multicast_start_work);
897 }
898 }
899 spin_unlock_bh(&priv->ps_state_lock);
900 } else if (!cw1200_queue_get_skb(queue, arg->packet_id,
901 &skb, &txpriv)) {
902 struct ieee80211_tx_info *tx = IEEE80211_SKB_CB(skb);
903 int tx_count = arg->ack_failures;
904 u8 ht_flags = 0;
905 int i;
906
907 if (cw1200_ht_greenfield(&priv->ht_info))
908 ht_flags |= IEEE80211_TX_RC_GREEN_FIELD;
909
910 spin_lock(&priv->bss_loss_lock);
911 if (priv->bss_loss_state &&
912 arg->packet_id == priv->bss_loss_confirm_id) {
913 if (arg->status) {
914 /* Recovery failed */
915 __cw1200_cqm_bssloss_sm(priv, 0, 0, 1);
916 } else {
917 /* Recovery succeeded */
918 __cw1200_cqm_bssloss_sm(priv, 0, 1, 0);
919 }
920 }
921 spin_unlock(&priv->bss_loss_lock);
922
923 if (!arg->status) {
924 tx->flags |= IEEE80211_TX_STAT_ACK;
925 ++tx_count;
926 cw1200_debug_txed(priv);
927 if (arg->flags & WSM_TX_STATUS_AGGREGATION) {
928 /* Do not report aggregation to mac80211:
Solomon Peachy8b3e7be2013-06-11 09:49:40 -0400929 * it confuses minstrel a lot.
930 */
Solomon Peachya910e4a2013-05-24 20:04:38 -0400931 /* tx->flags |= IEEE80211_TX_STAT_AMPDU; */
932 cw1200_debug_txed_agg(priv);
933 }
934 } else {
935 if (tx_count)
936 ++tx_count;
937 }
938
939 for (i = 0; i < IEEE80211_TX_MAX_RATES; ++i) {
940 if (tx->status.rates[i].count >= tx_count) {
941 tx->status.rates[i].count = tx_count;
942 break;
943 }
944 tx_count -= tx->status.rates[i].count;
945 if (tx->status.rates[i].flags & IEEE80211_TX_RC_MCS)
946 tx->status.rates[i].flags |= ht_flags;
947 }
948
949 for (++i; i < IEEE80211_TX_MAX_RATES; ++i) {
950 tx->status.rates[i].count = 0;
951 tx->status.rates[i].idx = -1;
952 }
953
954 /* Pull off any crypto trailers that we added on */
955 if (tx->control.hw_key) {
956 skb_trim(skb, skb->len - tx->control.hw_key->icv_len);
957 if (tx->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
958 skb_trim(skb, skb->len - 8); /* MIC space */
959 }
960 cw1200_queue_remove(queue, arg->packet_id);
961 }
962 /* XXX TODO: Only wake if there are pending transmits.. */
963 cw1200_bh_wakeup(priv);
964}
965
966static void cw1200_notify_buffered_tx(struct cw1200_common *priv,
967 struct sk_buff *skb, int link_id, int tid)
968{
969 struct ieee80211_sta *sta;
970 struct ieee80211_hdr *hdr;
971 u8 *buffered;
972 u8 still_buffered = 0;
973
974 if (link_id && tid < CW1200_MAX_TID) {
975 buffered = priv->link_id_db
976 [link_id - 1].buffered;
977
978 spin_lock_bh(&priv->ps_state_lock);
979 if (!WARN_ON(!buffered[tid]))
980 still_buffered = --buffered[tid];
981 spin_unlock_bh(&priv->ps_state_lock);
982
983 if (!still_buffered && tid < CW1200_MAX_TID) {
984 hdr = (struct ieee80211_hdr *)skb->data;
985 rcu_read_lock();
986 sta = ieee80211_find_sta(priv->vif, hdr->addr1);
987 if (sta)
988 ieee80211_sta_set_buffered(sta, tid, false);
989 rcu_read_unlock();
990 }
991 }
992}
993
994void cw1200_skb_dtor(struct cw1200_common *priv,
995 struct sk_buff *skb,
996 const struct cw1200_txpriv *txpriv)
997{
998 skb_pull(skb, txpriv->offset);
999 if (txpriv->rate_id != CW1200_INVALID_RATE_ID) {
1000 cw1200_notify_buffered_tx(priv, skb,
1001 txpriv->raw_link_id, txpriv->tid);
1002 tx_policy_put(priv, txpriv->rate_id);
1003 }
Solomon Peachyfa8eeae2013-06-11 09:49:38 -04001004 ieee80211_tx_status(priv->hw, skb);
Solomon Peachya910e4a2013-05-24 20:04:38 -04001005}
1006
1007void cw1200_rx_cb(struct cw1200_common *priv,
1008 struct wsm_rx *arg,
1009 int link_id,
1010 struct sk_buff **skb_p)
1011{
1012 struct sk_buff *skb = *skb_p;
1013 struct ieee80211_rx_status *hdr = IEEE80211_SKB_RXCB(skb);
1014 struct ieee80211_hdr *frame = (struct ieee80211_hdr *)skb->data;
1015 struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
1016 struct cw1200_link_entry *entry = NULL;
1017 unsigned long grace_period;
1018
1019 bool early_data = false;
1020 bool p2p = priv->vif && priv->vif->p2p;
1021 size_t hdrlen;
1022 hdr->flag = 0;
1023
1024 if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
1025 /* STA is stopped. */
1026 goto drop;
1027 }
1028
1029 if (link_id && link_id <= CW1200_MAX_STA_IN_AP_MODE) {
1030 entry = &priv->link_id_db[link_id - 1];
1031 if (entry->status == CW1200_LINK_SOFT &&
1032 ieee80211_is_data(frame->frame_control))
1033 early_data = true;
1034 entry->timestamp = jiffies;
1035 } else if (p2p &&
1036 ieee80211_is_action(frame->frame_control) &&
1037 (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)) {
1038 pr_debug("[RX] Going to MAP&RESET link ID\n");
1039 WARN_ON(work_pending(&priv->linkid_reset_work));
1040 memcpy(&priv->action_frame_sa[0],
1041 ieee80211_get_SA(frame), ETH_ALEN);
1042 priv->action_linkid = 0;
1043 schedule_work(&priv->linkid_reset_work);
1044 }
1045
1046 if (link_id && p2p &&
1047 ieee80211_is_action(frame->frame_control) &&
1048 (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)) {
1049 /* Link ID already exists for the ACTION frame.
Solomon Peachy8b3e7be2013-06-11 09:49:40 -04001050 * Reset and Remap
1051 */
Solomon Peachya910e4a2013-05-24 20:04:38 -04001052 WARN_ON(work_pending(&priv->linkid_reset_work));
1053 memcpy(&priv->action_frame_sa[0],
1054 ieee80211_get_SA(frame), ETH_ALEN);
1055 priv->action_linkid = link_id;
1056 schedule_work(&priv->linkid_reset_work);
1057 }
1058 if (arg->status) {
1059 if (arg->status == WSM_STATUS_MICFAILURE) {
1060 pr_debug("[RX] MIC failure.\n");
1061 hdr->flag |= RX_FLAG_MMIC_ERROR;
1062 } else if (arg->status == WSM_STATUS_NO_KEY_FOUND) {
1063 pr_debug("[RX] No key found.\n");
1064 goto drop;
1065 } else {
1066 pr_debug("[RX] Receive failure: %d.\n",
1067 arg->status);
1068 goto drop;
1069 }
1070 }
1071
1072 if (skb->len < sizeof(struct ieee80211_pspoll)) {
1073 wiphy_warn(priv->hw->wiphy, "Mailformed SDU rx'ed. Size is lesser than IEEE header.\n");
1074 goto drop;
1075 }
1076
1077 if (ieee80211_is_pspoll(frame->frame_control))
1078 if (cw1200_handle_pspoll(priv, skb))
1079 goto drop;
1080
Solomon Peachya910e4a2013-05-24 20:04:38 -04001081 hdr->band = ((arg->channel_number & 0xff00) ||
1082 (arg->channel_number > 14)) ?
1083 IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
1084 hdr->freq = ieee80211_channel_to_frequency(
1085 arg->channel_number,
1086 hdr->band);
1087
1088 if (arg->rx_rate >= 14) {
1089 hdr->flag |= RX_FLAG_HT;
1090 hdr->rate_idx = arg->rx_rate - 14;
1091 } else if (arg->rx_rate >= 4) {
1092 hdr->rate_idx = arg->rx_rate - 2;
1093 } else {
1094 hdr->rate_idx = arg->rx_rate;
1095 }
1096
1097 hdr->signal = (s8)arg->rcpi_rssi;
1098 hdr->antenna = 0;
1099
1100 hdrlen = ieee80211_hdrlen(frame->frame_control);
1101
1102 if (WSM_RX_STATUS_ENCRYPTION(arg->flags)) {
1103 size_t iv_len = 0, icv_len = 0;
1104
1105 hdr->flag |= RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED;
1106
1107 /* Oops... There is no fast way to ask mac80211 about
Solomon Peachy8b3e7be2013-06-11 09:49:40 -04001108 * IV/ICV lengths. Even defineas are not exposed.
1109 */
Solomon Peachya910e4a2013-05-24 20:04:38 -04001110 switch (WSM_RX_STATUS_ENCRYPTION(arg->flags)) {
1111 case WSM_RX_STATUS_WEP:
1112 iv_len = 4 /* WEP_IV_LEN */;
1113 icv_len = 4 /* WEP_ICV_LEN */;
1114 break;
1115 case WSM_RX_STATUS_TKIP:
1116 iv_len = 8 /* TKIP_IV_LEN */;
1117 icv_len = 4 /* TKIP_ICV_LEN */
1118 + 8 /*MICHAEL_MIC_LEN*/;
1119 hdr->flag |= RX_FLAG_MMIC_STRIPPED;
1120 break;
1121 case WSM_RX_STATUS_AES:
1122 iv_len = 8 /* CCMP_HDR_LEN */;
1123 icv_len = 8 /* CCMP_MIC_LEN */;
1124 break;
1125 case WSM_RX_STATUS_WAPI:
1126 iv_len = 18 /* WAPI_HDR_LEN */;
1127 icv_len = 16 /* WAPI_MIC_LEN */;
1128 break;
1129 default:
1130 pr_warn("Unknown encryption type %d\n",
1131 WSM_RX_STATUS_ENCRYPTION(arg->flags));
1132 goto drop;
1133 }
1134
1135 /* Firmware strips ICV in case of MIC failure. */
1136 if (arg->status == WSM_STATUS_MICFAILURE)
1137 icv_len = 0;
1138
1139 if (skb->len < hdrlen + iv_len + icv_len) {
1140 wiphy_warn(priv->hw->wiphy, "Malformed SDU rx'ed. Size is lesser than crypto headers.\n");
1141 goto drop;
1142 }
1143
1144 /* Remove IV, ICV and MIC */
1145 skb_trim(skb, skb->len - icv_len);
1146 memmove(skb->data + iv_len, skb->data, hdrlen);
1147 skb_pull(skb, iv_len);
1148 }
1149
1150 /* Remove TSF from the end of frame */
1151 if (arg->flags & WSM_RX_STATUS_TSF_INCLUDED) {
1152 memcpy(&hdr->mactime, skb->data + skb->len - 8, 8);
1153 hdr->mactime = le64_to_cpu(hdr->mactime);
1154 if (skb->len >= 8)
1155 skb_trim(skb, skb->len - 8);
Solomon Peachy8b3e7be2013-06-11 09:49:40 -04001156 } else {
1157 hdr->mactime = 0;
Solomon Peachya910e4a2013-05-24 20:04:38 -04001158 }
1159
1160 cw1200_debug_rxed(priv);
1161 if (arg->flags & WSM_RX_STATUS_AGGREGATE)
1162 cw1200_debug_rxed_agg(priv);
1163
1164 if (ieee80211_is_action(frame->frame_control) &&
1165 (arg->flags & WSM_RX_STATUS_ADDRESS1)) {
1166 if (cw1200_handle_action_rx(priv, skb))
1167 return;
1168 } else if (ieee80211_is_beacon(frame->frame_control) &&
1169 !arg->status &&
1170 !memcmp(ieee80211_get_SA(frame), priv->vif->bss_conf.bssid,
1171 ETH_ALEN)) {
1172 const u8 *tim_ie;
1173 u8 *ies = ((struct ieee80211_mgmt *)
1174 (skb->data))->u.beacon.variable;
1175 size_t ies_len = skb->len - (ies - (u8 *)(skb->data));
1176
1177 tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies, ies_len);
1178 if (tim_ie) {
1179 struct ieee80211_tim_ie *tim =
1180 (struct ieee80211_tim_ie *)&tim_ie[2];
1181
1182 if (priv->join_dtim_period != tim->dtim_period) {
1183 priv->join_dtim_period = tim->dtim_period;
1184 queue_work(priv->workqueue,
1185 &priv->set_beacon_wakeup_period_work);
1186 }
1187 }
1188
1189 /* Disable beacon filter once we're associated... */
1190 if (priv->disable_beacon_filter &&
1191 (priv->vif->bss_conf.assoc ||
1192 priv->vif->bss_conf.ibss_joined)) {
1193 priv->disable_beacon_filter = false;
1194 queue_work(priv->workqueue,
1195 &priv->update_filtering_work);
1196 }
1197 }
1198
1199 /* Stay awake after frame is received to give
1200 * userspace chance to react and acquire appropriate
Solomon Peachy8b3e7be2013-06-11 09:49:40 -04001201 * wakelock.
1202 */
Solomon Peachya910e4a2013-05-24 20:04:38 -04001203 if (ieee80211_is_auth(frame->frame_control))
1204 grace_period = 5 * HZ;
1205 else if (ieee80211_is_deauth(frame->frame_control))
1206 grace_period = 5 * HZ;
1207 else
1208 grace_period = 1 * HZ;
1209 cw1200_pm_stay_awake(&priv->pm_state, grace_period);
1210
Solomon Peachyfa8eeae2013-06-11 09:49:38 -04001211 if (early_data) {
Solomon Peachya910e4a2013-05-24 20:04:38 -04001212 spin_lock_bh(&priv->ps_state_lock);
1213 /* Double-check status with lock held */
1214 if (entry->status == CW1200_LINK_SOFT)
1215 skb_queue_tail(&entry->rx_queue, skb);
1216 else
1217 ieee80211_rx_irqsafe(priv->hw, skb);
1218 spin_unlock_bh(&priv->ps_state_lock);
1219 } else {
1220 ieee80211_rx_irqsafe(priv->hw, skb);
1221 }
1222 *skb_p = NULL;
1223
1224 return;
1225
1226drop:
1227 /* TODO: update failure counters */
1228 return;
1229}
1230
1231/* ******************************************************************** */
1232/* Security */
1233
1234int cw1200_alloc_key(struct cw1200_common *priv)
1235{
1236 int idx;
1237
1238 idx = ffs(~priv->key_map) - 1;
1239 if (idx < 0 || idx > WSM_KEY_MAX_INDEX)
1240 return -1;
1241
1242 priv->key_map |= BIT(idx);
1243 priv->keys[idx].index = idx;
1244 return idx;
1245}
1246
1247void cw1200_free_key(struct cw1200_common *priv, int idx)
1248{
1249 BUG_ON(!(priv->key_map & BIT(idx)));
1250 memset(&priv->keys[idx], 0, sizeof(priv->keys[idx]));
1251 priv->key_map &= ~BIT(idx);
1252}
1253
1254void cw1200_free_keys(struct cw1200_common *priv)
1255{
1256 memset(&priv->keys, 0, sizeof(priv->keys));
1257 priv->key_map = 0;
1258}
1259
1260int cw1200_upload_keys(struct cw1200_common *priv)
1261{
1262 int idx, ret = 0;
1263 for (idx = 0; idx <= WSM_KEY_MAX_INDEX; ++idx)
1264 if (priv->key_map & BIT(idx)) {
1265 ret = wsm_add_key(priv, &priv->keys[idx]);
1266 if (ret < 0)
1267 break;
1268 }
1269 return ret;
1270}
1271
1272/* Workaround for WFD test case 6.1.10 */
1273void cw1200_link_id_reset(struct work_struct *work)
1274{
1275 struct cw1200_common *priv =
1276 container_of(work, struct cw1200_common, linkid_reset_work);
1277 int temp_linkid;
1278
1279 if (!priv->action_linkid) {
1280 /* In GO mode we can receive ACTION frames without a linkID */
1281 temp_linkid = cw1200_alloc_link_id(priv,
1282 &priv->action_frame_sa[0]);
1283 WARN_ON(!temp_linkid);
1284 if (temp_linkid) {
1285 /* Make sure we execute the WQ */
1286 flush_workqueue(priv->workqueue);
1287 /* Release the link ID */
1288 spin_lock_bh(&priv->ps_state_lock);
1289 priv->link_id_db[temp_linkid - 1].prev_status =
1290 priv->link_id_db[temp_linkid - 1].status;
1291 priv->link_id_db[temp_linkid - 1].status =
1292 CW1200_LINK_RESET;
1293 spin_unlock_bh(&priv->ps_state_lock);
1294 wsm_lock_tx_async(priv);
1295 if (queue_work(priv->workqueue,
1296 &priv->link_id_work) <= 0)
1297 wsm_unlock_tx(priv);
1298 }
1299 } else {
1300 spin_lock_bh(&priv->ps_state_lock);
1301 priv->link_id_db[priv->action_linkid - 1].prev_status =
1302 priv->link_id_db[priv->action_linkid - 1].status;
1303 priv->link_id_db[priv->action_linkid - 1].status =
1304 CW1200_LINK_RESET_REMAP;
1305 spin_unlock_bh(&priv->ps_state_lock);
1306 wsm_lock_tx_async(priv);
1307 if (queue_work(priv->workqueue, &priv->link_id_work) <= 0)
1308 wsm_unlock_tx(priv);
1309 flush_workqueue(priv->workqueue);
1310 }
1311}
1312
1313int cw1200_find_link_id(struct cw1200_common *priv, const u8 *mac)
1314{
1315 int i, ret = 0;
1316 spin_lock_bh(&priv->ps_state_lock);
1317 for (i = 0; i < CW1200_MAX_STA_IN_AP_MODE; ++i) {
1318 if (!memcmp(mac, priv->link_id_db[i].mac, ETH_ALEN) &&
1319 priv->link_id_db[i].status) {
1320 priv->link_id_db[i].timestamp = jiffies;
1321 ret = i + 1;
1322 break;
1323 }
1324 }
1325 spin_unlock_bh(&priv->ps_state_lock);
1326 return ret;
1327}
1328
1329int cw1200_alloc_link_id(struct cw1200_common *priv, const u8 *mac)
1330{
1331 int i, ret = 0;
1332 unsigned long max_inactivity = 0;
1333 unsigned long now = jiffies;
1334
1335 spin_lock_bh(&priv->ps_state_lock);
1336 for (i = 0; i < CW1200_MAX_STA_IN_AP_MODE; ++i) {
1337 if (!priv->link_id_db[i].status) {
1338 ret = i + 1;
1339 break;
1340 } else if (priv->link_id_db[i].status != CW1200_LINK_HARD &&
1341 !priv->tx_queue_stats.link_map_cache[i + 1]) {
1342 unsigned long inactivity =
1343 now - priv->link_id_db[i].timestamp;
1344 if (inactivity < max_inactivity)
1345 continue;
1346 max_inactivity = inactivity;
1347 ret = i + 1;
1348 }
1349 }
1350 if (ret) {
1351 struct cw1200_link_entry *entry = &priv->link_id_db[ret - 1];
1352 pr_debug("[AP] STA added, link_id: %d\n", ret);
1353 entry->status = CW1200_LINK_RESERVE;
1354 memcpy(&entry->mac, mac, ETH_ALEN);
1355 memset(&entry->buffered, 0, CW1200_MAX_TID);
1356 skb_queue_head_init(&entry->rx_queue);
1357 wsm_lock_tx_async(priv);
1358 if (queue_work(priv->workqueue, &priv->link_id_work) <= 0)
1359 wsm_unlock_tx(priv);
1360 } else {
1361 wiphy_info(priv->hw->wiphy,
1362 "[AP] Early: no more link IDs available.\n");
1363 }
1364
1365 spin_unlock_bh(&priv->ps_state_lock);
1366 return ret;
1367}
1368
1369void cw1200_link_id_work(struct work_struct *work)
1370{
1371 struct cw1200_common *priv =
1372 container_of(work, struct cw1200_common, link_id_work);
1373 wsm_flush_tx(priv);
1374 cw1200_link_id_gc_work(&priv->link_id_gc_work.work);
1375 wsm_unlock_tx(priv);
1376}
1377
1378void cw1200_link_id_gc_work(struct work_struct *work)
1379{
1380 struct cw1200_common *priv =
1381 container_of(work, struct cw1200_common, link_id_gc_work.work);
1382 struct wsm_reset reset = {
1383 .reset_statistics = false,
1384 };
1385 struct wsm_map_link map_link = {
1386 .link_id = 0,
1387 };
1388 unsigned long now = jiffies;
1389 unsigned long next_gc = -1;
1390 long ttl;
1391 bool need_reset;
1392 u32 mask;
1393 int i;
1394
1395 if (priv->join_status != CW1200_JOIN_STATUS_AP)
1396 return;
1397
1398 wsm_lock_tx(priv);
1399 spin_lock_bh(&priv->ps_state_lock);
1400 for (i = 0; i < CW1200_MAX_STA_IN_AP_MODE; ++i) {
1401 need_reset = false;
1402 mask = BIT(i + 1);
1403 if (priv->link_id_db[i].status == CW1200_LINK_RESERVE ||
1404 (priv->link_id_db[i].status == CW1200_LINK_HARD &&
1405 !(priv->link_id_map & mask))) {
1406 if (priv->link_id_map & mask) {
1407 priv->sta_asleep_mask &= ~mask;
1408 priv->pspoll_mask &= ~mask;
1409 need_reset = true;
1410 }
1411 priv->link_id_map |= mask;
1412 if (priv->link_id_db[i].status != CW1200_LINK_HARD)
1413 priv->link_id_db[i].status = CW1200_LINK_SOFT;
1414 memcpy(map_link.mac_addr, priv->link_id_db[i].mac,
1415 ETH_ALEN);
1416 spin_unlock_bh(&priv->ps_state_lock);
1417 if (need_reset) {
1418 reset.link_id = i + 1;
1419 wsm_reset(priv, &reset);
1420 }
1421 map_link.link_id = i + 1;
1422 wsm_map_link(priv, &map_link);
1423 next_gc = min(next_gc, CW1200_LINK_ID_GC_TIMEOUT);
1424 spin_lock_bh(&priv->ps_state_lock);
1425 } else if (priv->link_id_db[i].status == CW1200_LINK_SOFT) {
1426 ttl = priv->link_id_db[i].timestamp - now +
1427 CW1200_LINK_ID_GC_TIMEOUT;
1428 if (ttl <= 0) {
1429 need_reset = true;
1430 priv->link_id_db[i].status = CW1200_LINK_OFF;
1431 priv->link_id_map &= ~mask;
1432 priv->sta_asleep_mask &= ~mask;
1433 priv->pspoll_mask &= ~mask;
1434 memset(map_link.mac_addr, 0, ETH_ALEN);
1435 spin_unlock_bh(&priv->ps_state_lock);
1436 reset.link_id = i + 1;
1437 wsm_reset(priv, &reset);
1438 spin_lock_bh(&priv->ps_state_lock);
1439 } else {
1440 next_gc = min_t(unsigned long, next_gc, ttl);
1441 }
1442 } else if (priv->link_id_db[i].status == CW1200_LINK_RESET ||
1443 priv->link_id_db[i].status ==
1444 CW1200_LINK_RESET_REMAP) {
1445 int status = priv->link_id_db[i].status;
1446 priv->link_id_db[i].status =
1447 priv->link_id_db[i].prev_status;
1448 priv->link_id_db[i].timestamp = now;
1449 reset.link_id = i + 1;
1450 spin_unlock_bh(&priv->ps_state_lock);
1451 wsm_reset(priv, &reset);
1452 if (status == CW1200_LINK_RESET_REMAP) {
1453 memcpy(map_link.mac_addr,
1454 priv->link_id_db[i].mac,
1455 ETH_ALEN);
1456 map_link.link_id = i + 1;
1457 wsm_map_link(priv, &map_link);
1458 next_gc = min(next_gc,
1459 CW1200_LINK_ID_GC_TIMEOUT);
1460 }
1461 spin_lock_bh(&priv->ps_state_lock);
1462 }
1463 if (need_reset) {
1464 skb_queue_purge(&priv->link_id_db[i].rx_queue);
1465 pr_debug("[AP] STA removed, link_id: %d\n",
1466 reset.link_id);
1467 }
1468 }
1469 spin_unlock_bh(&priv->ps_state_lock);
1470 if (next_gc != -1)
1471 queue_delayed_work(priv->workqueue,
1472 &priv->link_id_gc_work, next_gc);
1473 wsm_unlock_tx(priv);
1474}