txrx.c 38 KB

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