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