txrx.c 39 KB

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