txrx.c 39 KB

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