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