txrx.c 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467
  1. /*
  2. * Datapath implementation for ST-Ericsson CW1200 mac80211 drivers
  3. *
  4. * Copyright (c) 2010, ST-Ericsson
  5. * Author: Dmitry Tarnyagin <dmitry.tarnyagin@lockless.no>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <net/mac80211.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/skbuff.h>
  14. #include "cw1200.h"
  15. #include "wsm.h"
  16. #include "bh.h"
  17. #include "sta.h"
  18. #include "debug.h"
  19. #define CW1200_INVALID_RATE_ID (0xFF)
  20. static int cw1200_handle_action_rx(struct cw1200_common *priv,
  21. struct sk_buff *skb);
  22. static const struct ieee80211_rate *
  23. cw1200_get_tx_rate(const struct cw1200_common *priv,
  24. const struct ieee80211_tx_rate *rate);
  25. /* ******************************************************************** */
  26. /* TX queue lock / unlock */
  27. static inline void cw1200_tx_queues_lock(struct cw1200_common *priv)
  28. {
  29. int i;
  30. for (i = 0; i < 4; ++i)
  31. cw1200_queue_lock(&priv->tx_queue[i]);
  32. }
  33. static inline void cw1200_tx_queues_unlock(struct cw1200_common *priv)
  34. {
  35. int i;
  36. for (i = 0; i < 4; ++i)
  37. cw1200_queue_unlock(&priv->tx_queue[i]);
  38. }
  39. /* ******************************************************************** */
  40. /* TX policy cache implementation */
  41. static void tx_policy_dump(struct tx_policy *policy)
  42. {
  43. pr_debug("[TX policy] %.1X%.1X%.1X%.1X%.1X%.1X%.1X%.1X %.1X%.1X%.1X%.1X%.1X%.1X%.1X%.1X %.1X%.1X%.1X%.1X%.1X%.1X%.1X%.1X: %d\n",
  44. policy->raw[0] & 0x0F, policy->raw[0] >> 4,
  45. policy->raw[1] & 0x0F, policy->raw[1] >> 4,
  46. policy->raw[2] & 0x0F, policy->raw[2] >> 4,
  47. policy->raw[3] & 0x0F, policy->raw[3] >> 4,
  48. policy->raw[4] & 0x0F, policy->raw[4] >> 4,
  49. policy->raw[5] & 0x0F, policy->raw[5] >> 4,
  50. policy->raw[6] & 0x0F, policy->raw[6] >> 4,
  51. policy->raw[7] & 0x0F, policy->raw[7] >> 4,
  52. policy->raw[8] & 0x0F, policy->raw[8] >> 4,
  53. policy->raw[9] & 0x0F, policy->raw[9] >> 4,
  54. policy->raw[10] & 0x0F, policy->raw[10] >> 4,
  55. policy->raw[11] & 0x0F, policy->raw[11] >> 4,
  56. policy->defined);
  57. }
  58. static void tx_policy_build(const struct cw1200_common *priv,
  59. /* [out] */ struct tx_policy *policy,
  60. struct ieee80211_tx_rate *rates, size_t count)
  61. {
  62. int i, j;
  63. unsigned limit = priv->short_frame_max_tx_count;
  64. unsigned total = 0;
  65. BUG_ON(rates[0].idx < 0);
  66. memset(policy, 0, sizeof(*policy));
  67. /* minstrel is buggy a little bit, so distille
  68. * incoming rates first. */
  69. /* Sort rates in descending order. */
  70. for (i = 1; i < count; ++i) {
  71. if (rates[i].idx < 0) {
  72. count = i;
  73. break;
  74. }
  75. if (rates[i].idx > rates[i - 1].idx) {
  76. struct ieee80211_tx_rate tmp = rates[i - 1];
  77. rates[i - 1] = rates[i];
  78. rates[i] = tmp;
  79. }
  80. }
  81. /* Eliminate duplicates. */
  82. total = rates[0].count;
  83. for (i = 0, j = 1; j < count; ++j) {
  84. if (rates[j].idx == rates[i].idx) {
  85. rates[i].count += rates[j].count;
  86. } else if (rates[j].idx > rates[i].idx) {
  87. break;
  88. } else {
  89. ++i;
  90. if (i != j)
  91. rates[i] = rates[j];
  92. }
  93. total += rates[j].count;
  94. }
  95. count = i + 1;
  96. /* Re-fill policy trying to keep every requested rate and with
  97. * respect to the global max tx retransmission count. */
  98. if (limit < count)
  99. limit = count;
  100. if (total > limit) {
  101. for (i = 0; i < count; ++i) {
  102. int left = count - i - 1;
  103. if (rates[i].count > limit - left)
  104. rates[i].count = limit - left;
  105. limit -= rates[i].count;
  106. }
  107. }
  108. /* HACK!!! Device has problems (at least) switching from
  109. * 54Mbps CTS to 1Mbps. This switch takes enormous amount
  110. * of time (100-200 ms), leading to valuable throughput drop.
  111. * As a workaround, additional g-rates are injected to the
  112. * policy.
  113. */
  114. if (count == 2 && !(rates[0].flags & IEEE80211_TX_RC_MCS) &&
  115. rates[0].idx > 4 && rates[0].count > 2 &&
  116. rates[1].idx < 2) {
  117. /* ">> 1" is an equivalent of "/ 2", but faster */
  118. int mid_rate = (rates[0].idx + 4) >> 1;
  119. /* Decrease number of retries for the initial rate */
  120. rates[0].count -= 2;
  121. if (mid_rate != 4) {
  122. /* Keep fallback rate at 1Mbps. */
  123. rates[3] = rates[1];
  124. /* Inject 1 transmission on lowest g-rate */
  125. rates[2].idx = 4;
  126. rates[2].count = 1;
  127. rates[2].flags = rates[1].flags;
  128. /* Inject 1 transmission on mid-rate */
  129. rates[1].idx = mid_rate;
  130. rates[1].count = 1;
  131. /* Fallback to 1 Mbps is a really bad thing,
  132. * so let's try to increase probability of
  133. * successful transmission on the lowest g rate
  134. * even more */
  135. if (rates[0].count >= 3) {
  136. --rates[0].count;
  137. ++rates[2].count;
  138. }
  139. /* Adjust amount of rates defined */
  140. count += 2;
  141. } else {
  142. /* Keep fallback rate at 1Mbps. */
  143. rates[2] = rates[1];
  144. /* Inject 2 transmissions on lowest g-rate */
  145. rates[1].idx = 4;
  146. rates[1].count = 2;
  147. /* Adjust amount of rates defined */
  148. count += 1;
  149. }
  150. }
  151. policy->defined = cw1200_get_tx_rate(priv, &rates[0])->hw_value + 1;
  152. for (i = 0; i < count; ++i) {
  153. register unsigned rateid, off, shift, retries;
  154. rateid = cw1200_get_tx_rate(priv, &rates[i])->hw_value;
  155. off = rateid >> 3; /* eq. rateid / 8 */
  156. shift = (rateid & 0x07) << 2; /* eq. (rateid % 8) * 4 */
  157. retries = rates[i].count;
  158. if (retries > 0x0F) {
  159. rates[i].count = 0x0f;
  160. retries = 0x0F;
  161. }
  162. policy->tbl[off] |= __cpu_to_le32(retries << shift);
  163. policy->retry_count += retries;
  164. }
  165. pr_debug("[TX policy] Policy (%zu): %d:%d, %d:%d, %d:%d, %d:%d\n",
  166. count,
  167. rates[0].idx, rates[0].count,
  168. rates[1].idx, rates[1].count,
  169. rates[2].idx, rates[2].count,
  170. rates[3].idx, rates[3].count);
  171. }
  172. static inline bool tx_policy_is_equal(const struct tx_policy *wanted,
  173. const struct tx_policy *cached)
  174. {
  175. size_t count = wanted->defined >> 1;
  176. if (wanted->defined > cached->defined)
  177. return false;
  178. if (count) {
  179. if (memcmp(wanted->raw, cached->raw, count))
  180. return false;
  181. }
  182. if (wanted->defined & 1) {
  183. if ((wanted->raw[count] & 0x0F) != (cached->raw[count] & 0x0F))
  184. return false;
  185. }
  186. return true;
  187. }
  188. static int tx_policy_find(struct tx_policy_cache *cache,
  189. const struct tx_policy *wanted)
  190. {
  191. /* O(n) complexity. Not so good, but there's only 8 entries in
  192. * the cache.
  193. * Also lru helps to reduce search time. */
  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. if (WARN_ON(entry->policy.usage_count)) {
  234. entry->policy.usage_count = 0;
  235. list_move(&entry->link, &cache->free);
  236. }
  237. memset(&entry->policy, 0, sizeof(entry->policy));
  238. }
  239. if (locked)
  240. cw1200_tx_queues_unlock(priv);
  241. cw1200_tx_queues_unlock(priv);
  242. spin_unlock_bh(&cache->lock);
  243. }
  244. /* ******************************************************************** */
  245. /* External TX policy cache API */
  246. void tx_policy_init(struct cw1200_common *priv)
  247. {
  248. struct tx_policy_cache *cache = &priv->tx_policy_cache;
  249. int i;
  250. memset(cache, 0, sizeof(*cache));
  251. spin_lock_init(&cache->lock);
  252. INIT_LIST_HEAD(&cache->used);
  253. INIT_LIST_HEAD(&cache->free);
  254. for (i = 0; i < TX_POLICY_CACHE_SIZE; ++i)
  255. list_add(&cache->cache[i].link, &cache->free);
  256. }
  257. static int tx_policy_get(struct cw1200_common *priv,
  258. struct ieee80211_tx_rate *rates,
  259. size_t count, bool *renew)
  260. {
  261. int idx;
  262. struct tx_policy_cache *cache = &priv->tx_policy_cache;
  263. struct tx_policy wanted;
  264. tx_policy_build(priv, &wanted, rates, count);
  265. spin_lock_bh(&cache->lock);
  266. if (WARN_ON_ONCE(list_empty(&cache->free))) {
  267. spin_unlock_bh(&cache->lock);
  268. return CW1200_INVALID_RATE_ID;
  269. }
  270. idx = tx_policy_find(cache, &wanted);
  271. if (idx >= 0) {
  272. pr_debug("[TX policy] Used TX policy: %d\n", idx);
  273. *renew = false;
  274. } else {
  275. struct tx_policy_cache_entry *entry;
  276. *renew = true;
  277. /* If policy is not found create a new one
  278. * using the oldest entry in "free" list */
  279. entry = list_entry(cache->free.prev,
  280. struct tx_policy_cache_entry, link);
  281. entry->policy = wanted;
  282. idx = entry - cache->cache;
  283. pr_debug("[TX policy] New TX policy: %d\n", idx);
  284. tx_policy_dump(&entry->policy);
  285. }
  286. tx_policy_use(cache, &cache->cache[idx]);
  287. if (list_empty(&cache->free)) {
  288. /* Lock TX queues. */
  289. cw1200_tx_queues_lock(priv);
  290. }
  291. spin_unlock_bh(&cache->lock);
  292. return idx;
  293. }
  294. static void tx_policy_put(struct cw1200_common *priv, int idx)
  295. {
  296. int usage, locked;
  297. struct tx_policy_cache *cache = &priv->tx_policy_cache;
  298. spin_lock_bh(&cache->lock);
  299. locked = list_empty(&cache->free);
  300. usage = tx_policy_release(cache, &cache->cache[idx]);
  301. if (locked && !usage) {
  302. /* Unlock TX queues. */
  303. cw1200_tx_queues_unlock(priv);
  304. }
  305. spin_unlock_bh(&cache->lock);
  306. }
  307. static int tx_policy_upload(struct cw1200_common *priv)
  308. {
  309. struct tx_policy_cache *cache = &priv->tx_policy_cache;
  310. int i;
  311. struct wsm_set_tx_rate_retry_policy arg = {
  312. .num = 0,
  313. };
  314. spin_lock_bh(&cache->lock);
  315. /* Upload only modified entries. */
  316. for (i = 0; i < TX_POLICY_CACHE_SIZE; ++i) {
  317. struct tx_policy *src = &cache->cache[i].policy;
  318. if (src->retry_count && !src->uploaded) {
  319. struct wsm_tx_rate_retry_policy *dst =
  320. &arg.tbl[arg.num];
  321. dst->index = i;
  322. dst->short_retries = priv->short_frame_max_tx_count;
  323. dst->long_retries = priv->long_frame_max_tx_count;
  324. dst->flags = WSM_TX_RATE_POLICY_FLAG_TERMINATE_WHEN_FINISHED |
  325. WSM_TX_RATE_POLICY_FLAG_COUNT_INITIAL_TRANSMIT;
  326. memcpy(dst->rate_count_indices, src->tbl,
  327. sizeof(dst->rate_count_indices));
  328. src->uploaded = 1;
  329. ++arg.num;
  330. }
  331. }
  332. spin_unlock_bh(&cache->lock);
  333. cw1200_debug_tx_cache_miss(priv);
  334. pr_debug("[TX policy] Upload %d policies\n", arg.num);
  335. return wsm_set_tx_rate_retry_policy(priv, &arg);
  336. }
  337. void tx_policy_upload_work(struct work_struct *work)
  338. {
  339. struct cw1200_common *priv =
  340. container_of(work, struct cw1200_common, tx_policy_upload_work);
  341. pr_debug("[TX] TX policy upload.\n");
  342. tx_policy_upload(priv);
  343. wsm_unlock_tx(priv);
  344. cw1200_tx_queues_unlock(priv);
  345. }
  346. /* ******************************************************************** */
  347. /* cw1200 TX implementation */
  348. struct cw1200_txinfo {
  349. struct sk_buff *skb;
  350. unsigned queue;
  351. struct ieee80211_tx_info *tx_info;
  352. const struct ieee80211_rate *rate;
  353. struct ieee80211_hdr *hdr;
  354. size_t hdrlen;
  355. const u8 *da;
  356. struct cw1200_sta_priv *sta_priv;
  357. struct ieee80211_sta *sta;
  358. struct cw1200_txpriv txpriv;
  359. };
  360. u32 cw1200_rate_mask_to_wsm(struct cw1200_common *priv, u32 rates)
  361. {
  362. u32 ret = 0;
  363. int i;
  364. for (i = 0; i < 32; ++i) {
  365. if (rates & BIT(i))
  366. ret |= BIT(priv->rates[i].hw_value);
  367. }
  368. return ret;
  369. }
  370. static const struct ieee80211_rate *
  371. cw1200_get_tx_rate(const struct cw1200_common *priv,
  372. const struct ieee80211_tx_rate *rate)
  373. {
  374. if (rate->idx < 0)
  375. return NULL;
  376. if (rate->flags & IEEE80211_TX_RC_MCS)
  377. return &priv->mcs_rates[rate->idx];
  378. return &priv->hw->wiphy->bands[priv->channel->band]->
  379. bitrates[rate->idx];
  380. }
  381. static int
  382. cw1200_tx_h_calc_link_ids(struct cw1200_common *priv,
  383. struct cw1200_txinfo *t)
  384. {
  385. if (t->sta && t->sta_priv->link_id)
  386. t->txpriv.raw_link_id =
  387. t->txpriv.link_id =
  388. t->sta_priv->link_id;
  389. else if (priv->mode != NL80211_IFTYPE_AP)
  390. t->txpriv.raw_link_id =
  391. t->txpriv.link_id = 0;
  392. else if (is_multicast_ether_addr(t->da)) {
  393. if (priv->enable_beacon) {
  394. t->txpriv.raw_link_id = 0;
  395. t->txpriv.link_id = CW1200_LINK_ID_AFTER_DTIM;
  396. } else {
  397. t->txpriv.raw_link_id = 0;
  398. t->txpriv.link_id = 0;
  399. }
  400. } else {
  401. t->txpriv.link_id = cw1200_find_link_id(priv, t->da);
  402. if (!t->txpriv.link_id)
  403. t->txpriv.link_id = cw1200_alloc_link_id(priv, t->da);
  404. if (!t->txpriv.link_id) {
  405. wiphy_err(priv->hw->wiphy,
  406. "No more link IDs available.\n");
  407. return -ENOENT;
  408. }
  409. t->txpriv.raw_link_id = t->txpriv.link_id;
  410. }
  411. if (t->txpriv.raw_link_id)
  412. priv->link_id_db[t->txpriv.raw_link_id - 1].timestamp =
  413. jiffies;
  414. if (t->sta && (t->sta->uapsd_queues & BIT(t->queue)))
  415. t->txpriv.link_id = CW1200_LINK_ID_UAPSD;
  416. return 0;
  417. }
  418. static void
  419. cw1200_tx_h_pm(struct cw1200_common *priv,
  420. struct cw1200_txinfo *t)
  421. {
  422. if (ieee80211_is_auth(t->hdr->frame_control)) {
  423. u32 mask = ~BIT(t->txpriv.raw_link_id);
  424. spin_lock_bh(&priv->ps_state_lock);
  425. priv->sta_asleep_mask &= mask;
  426. priv->pspoll_mask &= mask;
  427. spin_unlock_bh(&priv->ps_state_lock);
  428. }
  429. }
  430. static void
  431. cw1200_tx_h_calc_tid(struct cw1200_common *priv,
  432. struct cw1200_txinfo *t)
  433. {
  434. if (ieee80211_is_data_qos(t->hdr->frame_control)) {
  435. u8 *qos = ieee80211_get_qos_ctl(t->hdr);
  436. t->txpriv.tid = qos[0] & IEEE80211_QOS_CTL_TID_MASK;
  437. } else if (ieee80211_is_data(t->hdr->frame_control)) {
  438. t->txpriv.tid = 0;
  439. }
  440. }
  441. static int
  442. cw1200_tx_h_crypt(struct cw1200_common *priv,
  443. struct cw1200_txinfo *t)
  444. {
  445. if (!t->tx_info->control.hw_key ||
  446. !ieee80211_has_protected(t->hdr->frame_control))
  447. return 0;
  448. t->hdrlen += t->tx_info->control.hw_key->iv_len;
  449. skb_put(t->skb, t->tx_info->control.hw_key->icv_len);
  450. if (t->tx_info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
  451. skb_put(t->skb, 8); /* MIC space */
  452. return 0;
  453. }
  454. static int
  455. cw1200_tx_h_align(struct cw1200_common *priv,
  456. struct cw1200_txinfo *t,
  457. u8 *flags)
  458. {
  459. size_t offset = (size_t)t->skb->data & 3;
  460. if (!offset)
  461. return 0;
  462. if (offset & 1) {
  463. wiphy_err(priv->hw->wiphy,
  464. "Bug: attempt to transmit a frame with wrong alignment: %zu\n",
  465. offset);
  466. return -EINVAL;
  467. }
  468. if (skb_headroom(t->skb) < offset) {
  469. wiphy_err(priv->hw->wiphy,
  470. "Bug: no space allocated for DMA alignment. headroom: %d\n",
  471. skb_headroom(t->skb));
  472. return -ENOMEM;
  473. }
  474. skb_push(t->skb, offset);
  475. t->hdrlen += offset;
  476. t->txpriv.offset += offset;
  477. *flags |= WSM_TX_2BYTES_SHIFT;
  478. cw1200_debug_tx_align(priv);
  479. return 0;
  480. }
  481. static int
  482. cw1200_tx_h_action(struct cw1200_common *priv,
  483. struct cw1200_txinfo *t)
  484. {
  485. struct ieee80211_mgmt *mgmt =
  486. (struct ieee80211_mgmt *)t->hdr;
  487. if (ieee80211_is_action(t->hdr->frame_control) &&
  488. mgmt->u.action.category == WLAN_CATEGORY_BACK)
  489. return 1;
  490. else
  491. return 0;
  492. }
  493. /* Add WSM header */
  494. static struct wsm_tx *
  495. cw1200_tx_h_wsm(struct cw1200_common *priv,
  496. struct cw1200_txinfo *t)
  497. {
  498. struct wsm_tx *wsm;
  499. if (skb_headroom(t->skb) < sizeof(struct wsm_tx)) {
  500. wiphy_err(priv->hw->wiphy,
  501. "Bug: no space allocated for WSM header. headroom: %d\n",
  502. skb_headroom(t->skb));
  503. return NULL;
  504. }
  505. wsm = (struct wsm_tx *)skb_push(t->skb, sizeof(struct wsm_tx));
  506. t->txpriv.offset += sizeof(struct wsm_tx);
  507. memset(wsm, 0, sizeof(*wsm));
  508. wsm->hdr.len = __cpu_to_le16(t->skb->len);
  509. wsm->hdr.id = __cpu_to_le16(0x0004);
  510. wsm->queue_id = wsm_queue_id_to_wsm(t->queue);
  511. return wsm;
  512. }
  513. /* BT Coex specific handling */
  514. static void
  515. cw1200_tx_h_bt(struct cw1200_common *priv,
  516. struct cw1200_txinfo *t,
  517. struct wsm_tx *wsm)
  518. {
  519. u8 priority = 0;
  520. if (!priv->bt_present)
  521. return;
  522. if (ieee80211_is_nullfunc(t->hdr->frame_control)) {
  523. priority = WSM_EPTA_PRIORITY_MGT;
  524. } else if (ieee80211_is_data(t->hdr->frame_control)) {
  525. /* Skip LLC SNAP header (+6) */
  526. u8 *payload = &t->skb->data[t->hdrlen];
  527. u16 *ethertype = (u16 *)&payload[6];
  528. if (*ethertype == __be16_to_cpu(ETH_P_PAE))
  529. priority = WSM_EPTA_PRIORITY_EAPOL;
  530. } else if (ieee80211_is_assoc_req(t->hdr->frame_control) ||
  531. ieee80211_is_reassoc_req(t->hdr->frame_control)) {
  532. struct ieee80211_mgmt *mgt_frame =
  533. (struct ieee80211_mgmt *)t->hdr;
  534. if (mgt_frame->u.assoc_req.listen_interval <
  535. priv->listen_interval) {
  536. pr_debug("Modified Listen Interval to %d from %d\n",
  537. priv->listen_interval,
  538. mgt_frame->u.assoc_req.listen_interval);
  539. /* Replace listen interval derieved from
  540. * the one read from SDD */
  541. mgt_frame->u.assoc_req.listen_interval =
  542. priv->listen_interval;
  543. }
  544. }
  545. if (!priority) {
  546. if (ieee80211_is_action(t->hdr->frame_control))
  547. priority = WSM_EPTA_PRIORITY_ACTION;
  548. else if (ieee80211_is_mgmt(t->hdr->frame_control))
  549. priority = WSM_EPTA_PRIORITY_MGT;
  550. else if ((wsm->queue_id == WSM_QUEUE_VOICE))
  551. priority = WSM_EPTA_PRIORITY_VOICE;
  552. else if ((wsm->queue_id == WSM_QUEUE_VIDEO))
  553. priority = WSM_EPTA_PRIORITY_VIDEO;
  554. else
  555. priority = WSM_EPTA_PRIORITY_DATA;
  556. }
  557. pr_debug("[TX] EPTA priority %d.\n", priority);
  558. wsm->flags |= priority << 1;
  559. }
  560. static int
  561. cw1200_tx_h_rate_policy(struct cw1200_common *priv,
  562. struct cw1200_txinfo *t,
  563. struct wsm_tx *wsm)
  564. {
  565. bool tx_policy_renew = false;
  566. t->txpriv.rate_id = tx_policy_get(priv,
  567. t->tx_info->control.rates, IEEE80211_TX_MAX_RATES,
  568. &tx_policy_renew);
  569. if (t->txpriv.rate_id == CW1200_INVALID_RATE_ID)
  570. return -EFAULT;
  571. wsm->flags |= t->txpriv.rate_id << 4;
  572. t->rate = cw1200_get_tx_rate(priv,
  573. &t->tx_info->control.rates[0]),
  574. wsm->max_tx_rate = t->rate->hw_value;
  575. if (t->rate->flags & IEEE80211_TX_RC_MCS) {
  576. if (cw1200_ht_greenfield(&priv->ht_info))
  577. wsm->ht_tx_parameters |=
  578. __cpu_to_le32(WSM_HT_TX_GREENFIELD);
  579. else
  580. wsm->ht_tx_parameters |=
  581. __cpu_to_le32(WSM_HT_TX_MIXED);
  582. }
  583. if (tx_policy_renew) {
  584. pr_debug("[TX] TX policy renew.\n");
  585. /* It's not so optimal to stop TX queues every now and then.
  586. * Better to reimplement task scheduling with
  587. * a counter. TODO. */
  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 = 0;
  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. rcu_read_lock();
  671. sta = rcu_dereference(t.sta);
  672. spin_lock_bh(&priv->ps_state_lock);
  673. {
  674. tid_update = cw1200_tx_h_pm_state(priv, &t);
  675. BUG_ON(cw1200_queue_put(&priv->tx_queue[t.queue],
  676. t.skb, &t.txpriv));
  677. }
  678. spin_unlock_bh(&priv->ps_state_lock);
  679. if (tid_update && sta)
  680. ieee80211_sta_set_buffered(sta, t.txpriv.tid, true);
  681. rcu_read_unlock();
  682. cw1200_bh_wakeup(priv);
  683. return;
  684. drop:
  685. cw1200_skb_dtor(priv, skb, &t.txpriv);
  686. return;
  687. }
  688. /* ******************************************************************** */
  689. static int cw1200_handle_action_rx(struct cw1200_common *priv,
  690. struct sk_buff *skb)
  691. {
  692. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  693. /* Filter block ACK negotiation: fully controlled by firmware */
  694. if (mgmt->u.action.category == WLAN_CATEGORY_BACK)
  695. return 1;
  696. return 0;
  697. }
  698. static int cw1200_handle_pspoll(struct cw1200_common *priv,
  699. struct sk_buff *skb)
  700. {
  701. struct ieee80211_sta *sta;
  702. struct ieee80211_pspoll *pspoll = (struct ieee80211_pspoll *)skb->data;
  703. int link_id = 0;
  704. u32 pspoll_mask = 0;
  705. int drop = 1;
  706. int i;
  707. if (priv->join_status != CW1200_JOIN_STATUS_AP)
  708. goto done;
  709. if (memcmp(priv->vif->addr, pspoll->bssid, ETH_ALEN))
  710. goto done;
  711. rcu_read_lock();
  712. sta = ieee80211_find_sta(priv->vif, pspoll->ta);
  713. if (sta) {
  714. struct cw1200_sta_priv *sta_priv;
  715. sta_priv = (struct cw1200_sta_priv *)&sta->drv_priv;
  716. link_id = sta_priv->link_id;
  717. pspoll_mask = BIT(sta_priv->link_id);
  718. }
  719. rcu_read_unlock();
  720. if (!link_id)
  721. goto done;
  722. priv->pspoll_mask |= pspoll_mask;
  723. drop = 0;
  724. /* Do not report pspols if data for given link id is
  725. * queued already. */
  726. for (i = 0; i < 4; ++i) {
  727. if (cw1200_queue_get_num_queued(&priv->tx_queue[i],
  728. pspoll_mask)) {
  729. cw1200_bh_wakeup(priv);
  730. drop = 1;
  731. break;
  732. }
  733. }
  734. pr_debug("[RX] PSPOLL: %s\n", drop ? "local" : "fwd");
  735. done:
  736. return drop;
  737. }
  738. /* ******************************************************************** */
  739. void cw1200_tx_confirm_cb(struct cw1200_common *priv,
  740. int link_id,
  741. struct wsm_tx_confirm *arg)
  742. {
  743. u8 queue_id = cw1200_queue_get_queue_id(arg->packet_id);
  744. struct cw1200_queue *queue = &priv->tx_queue[queue_id];
  745. struct sk_buff *skb;
  746. const struct cw1200_txpriv *txpriv;
  747. pr_debug("[TX] TX confirm: %d, %d.\n",
  748. arg->status, arg->ack_failures);
  749. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
  750. /* STA is stopped. */
  751. return;
  752. }
  753. if (WARN_ON(queue_id >= 4))
  754. return;
  755. if (arg->status)
  756. pr_debug("TX failed: %d.\n", arg->status);
  757. if ((arg->status == WSM_REQUEUE) &&
  758. (arg->flags & WSM_TX_STATUS_REQUEUE)) {
  759. /* "Requeue" means "implicit suspend" */
  760. struct wsm_suspend_resume suspend = {
  761. .link_id = link_id,
  762. .stop = 1,
  763. .multicast = !link_id,
  764. };
  765. cw1200_suspend_resume(priv, &suspend);
  766. wiphy_warn(priv->hw->wiphy, "Requeue for link_id %d (try %d). STAs asleep: 0x%.8X\n",
  767. link_id,
  768. cw1200_queue_get_generation(arg->packet_id) + 1,
  769. priv->sta_asleep_mask);
  770. cw1200_queue_requeue(queue, arg->packet_id);
  771. spin_lock_bh(&priv->ps_state_lock);
  772. if (!link_id) {
  773. priv->buffered_multicasts = true;
  774. if (priv->sta_asleep_mask) {
  775. queue_work(priv->workqueue,
  776. &priv->multicast_start_work);
  777. }
  778. }
  779. spin_unlock_bh(&priv->ps_state_lock);
  780. } else if (!cw1200_queue_get_skb(queue, arg->packet_id,
  781. &skb, &txpriv)) {
  782. struct ieee80211_tx_info *tx = IEEE80211_SKB_CB(skb);
  783. int tx_count = arg->ack_failures;
  784. u8 ht_flags = 0;
  785. int i;
  786. if (cw1200_ht_greenfield(&priv->ht_info))
  787. ht_flags |= IEEE80211_TX_RC_GREEN_FIELD;
  788. spin_lock(&priv->bss_loss_lock);
  789. if (priv->bss_loss_state &&
  790. arg->packet_id == priv->bss_loss_confirm_id) {
  791. if (arg->status) {
  792. /* Recovery failed */
  793. __cw1200_cqm_bssloss_sm(priv, 0, 0, 1);
  794. } else {
  795. /* Recovery succeeded */
  796. __cw1200_cqm_bssloss_sm(priv, 0, 1, 0);
  797. }
  798. }
  799. spin_unlock(&priv->bss_loss_lock);
  800. if (!arg->status) {
  801. tx->flags |= IEEE80211_TX_STAT_ACK;
  802. ++tx_count;
  803. cw1200_debug_txed(priv);
  804. if (arg->flags & WSM_TX_STATUS_AGGREGATION) {
  805. /* Do not report aggregation to mac80211:
  806. * it confuses minstrel a lot. */
  807. /* tx->flags |= IEEE80211_TX_STAT_AMPDU; */
  808. cw1200_debug_txed_agg(priv);
  809. }
  810. } else {
  811. if (tx_count)
  812. ++tx_count;
  813. }
  814. for (i = 0; i < IEEE80211_TX_MAX_RATES; ++i) {
  815. if (tx->status.rates[i].count >= tx_count) {
  816. tx->status.rates[i].count = tx_count;
  817. break;
  818. }
  819. tx_count -= tx->status.rates[i].count;
  820. if (tx->status.rates[i].flags & IEEE80211_TX_RC_MCS)
  821. tx->status.rates[i].flags |= ht_flags;
  822. }
  823. for (++i; i < IEEE80211_TX_MAX_RATES; ++i) {
  824. tx->status.rates[i].count = 0;
  825. tx->status.rates[i].idx = -1;
  826. }
  827. /* Pull off any crypto trailers that we added on */
  828. if (tx->control.hw_key) {
  829. skb_trim(skb, skb->len - tx->control.hw_key->icv_len);
  830. if (tx->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
  831. skb_trim(skb, skb->len - 8); /* MIC space */
  832. }
  833. cw1200_queue_remove(queue, arg->packet_id);
  834. }
  835. /* XXX TODO: Only wake if there are pending transmits.. */
  836. cw1200_bh_wakeup(priv);
  837. }
  838. static void cw1200_notify_buffered_tx(struct cw1200_common *priv,
  839. struct sk_buff *skb, int link_id, int tid)
  840. {
  841. struct ieee80211_sta *sta;
  842. struct ieee80211_hdr *hdr;
  843. u8 *buffered;
  844. u8 still_buffered = 0;
  845. if (link_id && tid < CW1200_MAX_TID) {
  846. buffered = priv->link_id_db
  847. [link_id - 1].buffered;
  848. spin_lock_bh(&priv->ps_state_lock);
  849. if (!WARN_ON(!buffered[tid]))
  850. still_buffered = --buffered[tid];
  851. spin_unlock_bh(&priv->ps_state_lock);
  852. if (!still_buffered && tid < CW1200_MAX_TID) {
  853. hdr = (struct ieee80211_hdr *)skb->data;
  854. rcu_read_lock();
  855. sta = ieee80211_find_sta(priv->vif, hdr->addr1);
  856. if (sta)
  857. ieee80211_sta_set_buffered(sta, tid, false);
  858. rcu_read_unlock();
  859. }
  860. }
  861. }
  862. void cw1200_skb_dtor(struct cw1200_common *priv,
  863. struct sk_buff *skb,
  864. const struct cw1200_txpriv *txpriv)
  865. {
  866. skb_pull(skb, txpriv->offset);
  867. if (txpriv->rate_id != CW1200_INVALID_RATE_ID) {
  868. cw1200_notify_buffered_tx(priv, skb,
  869. txpriv->raw_link_id, txpriv->tid);
  870. tx_policy_put(priv, txpriv->rate_id);
  871. }
  872. ieee80211_tx_status(priv->hw, skb);
  873. }
  874. void cw1200_rx_cb(struct cw1200_common *priv,
  875. struct wsm_rx *arg,
  876. int link_id,
  877. struct sk_buff **skb_p)
  878. {
  879. struct sk_buff *skb = *skb_p;
  880. struct ieee80211_rx_status *hdr = IEEE80211_SKB_RXCB(skb);
  881. struct ieee80211_hdr *frame = (struct ieee80211_hdr *)skb->data;
  882. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  883. struct cw1200_link_entry *entry = NULL;
  884. unsigned long grace_period;
  885. bool early_data = false;
  886. bool p2p = priv->vif && priv->vif->p2p;
  887. size_t hdrlen;
  888. hdr->flag = 0;
  889. if (priv->mode == NL80211_IFTYPE_UNSPECIFIED) {
  890. /* STA is stopped. */
  891. goto drop;
  892. }
  893. if (link_id && link_id <= CW1200_MAX_STA_IN_AP_MODE) {
  894. entry = &priv->link_id_db[link_id - 1];
  895. if (entry->status == CW1200_LINK_SOFT &&
  896. ieee80211_is_data(frame->frame_control))
  897. early_data = true;
  898. entry->timestamp = jiffies;
  899. } else if (p2p &&
  900. ieee80211_is_action(frame->frame_control) &&
  901. (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)) {
  902. pr_debug("[RX] Going to MAP&RESET link ID\n");
  903. WARN_ON(work_pending(&priv->linkid_reset_work));
  904. memcpy(&priv->action_frame_sa[0],
  905. ieee80211_get_SA(frame), ETH_ALEN);
  906. priv->action_linkid = 0;
  907. schedule_work(&priv->linkid_reset_work);
  908. }
  909. if (link_id && p2p &&
  910. ieee80211_is_action(frame->frame_control) &&
  911. (mgmt->u.action.category == WLAN_CATEGORY_PUBLIC)) {
  912. /* Link ID already exists for the ACTION frame.
  913. * Reset and Remap */
  914. WARN_ON(work_pending(&priv->linkid_reset_work));
  915. memcpy(&priv->action_frame_sa[0],
  916. ieee80211_get_SA(frame), ETH_ALEN);
  917. priv->action_linkid = link_id;
  918. schedule_work(&priv->linkid_reset_work);
  919. }
  920. if (arg->status) {
  921. if (arg->status == WSM_STATUS_MICFAILURE) {
  922. pr_debug("[RX] MIC failure.\n");
  923. hdr->flag |= RX_FLAG_MMIC_ERROR;
  924. } else if (arg->status == WSM_STATUS_NO_KEY_FOUND) {
  925. pr_debug("[RX] No key found.\n");
  926. goto drop;
  927. } else {
  928. pr_debug("[RX] Receive failure: %d.\n",
  929. arg->status);
  930. goto drop;
  931. }
  932. }
  933. if (skb->len < sizeof(struct ieee80211_pspoll)) {
  934. wiphy_warn(priv->hw->wiphy, "Mailformed SDU rx'ed. Size is lesser than IEEE header.\n");
  935. goto drop;
  936. }
  937. if (ieee80211_is_pspoll(frame->frame_control))
  938. if (cw1200_handle_pspoll(priv, skb))
  939. goto drop;
  940. hdr->mactime = 0; /* Not supported by WSM */
  941. hdr->band = ((arg->channel_number & 0xff00) ||
  942. (arg->channel_number > 14)) ?
  943. IEEE80211_BAND_5GHZ : IEEE80211_BAND_2GHZ;
  944. hdr->freq = ieee80211_channel_to_frequency(
  945. arg->channel_number,
  946. hdr->band);
  947. if (arg->rx_rate >= 14) {
  948. hdr->flag |= RX_FLAG_HT;
  949. hdr->rate_idx = arg->rx_rate - 14;
  950. } else if (arg->rx_rate >= 4) {
  951. hdr->rate_idx = arg->rx_rate - 2;
  952. } else {
  953. hdr->rate_idx = arg->rx_rate;
  954. }
  955. hdr->signal = (s8)arg->rcpi_rssi;
  956. hdr->antenna = 0;
  957. hdrlen = ieee80211_hdrlen(frame->frame_control);
  958. if (WSM_RX_STATUS_ENCRYPTION(arg->flags)) {
  959. size_t iv_len = 0, icv_len = 0;
  960. hdr->flag |= RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED;
  961. /* Oops... There is no fast way to ask mac80211 about
  962. * IV/ICV lengths. Even defineas are not exposed.*/
  963. switch (WSM_RX_STATUS_ENCRYPTION(arg->flags)) {
  964. case WSM_RX_STATUS_WEP:
  965. iv_len = 4 /* WEP_IV_LEN */;
  966. icv_len = 4 /* WEP_ICV_LEN */;
  967. break;
  968. case WSM_RX_STATUS_TKIP:
  969. iv_len = 8 /* TKIP_IV_LEN */;
  970. icv_len = 4 /* TKIP_ICV_LEN */
  971. + 8 /*MICHAEL_MIC_LEN*/;
  972. hdr->flag |= RX_FLAG_MMIC_STRIPPED;
  973. break;
  974. case WSM_RX_STATUS_AES:
  975. iv_len = 8 /* CCMP_HDR_LEN */;
  976. icv_len = 8 /* CCMP_MIC_LEN */;
  977. break;
  978. case WSM_RX_STATUS_WAPI:
  979. iv_len = 18 /* WAPI_HDR_LEN */;
  980. icv_len = 16 /* WAPI_MIC_LEN */;
  981. break;
  982. default:
  983. pr_warn("Unknown encryption type %d\n",
  984. WSM_RX_STATUS_ENCRYPTION(arg->flags));
  985. goto drop;
  986. }
  987. /* Firmware strips ICV in case of MIC failure. */
  988. if (arg->status == WSM_STATUS_MICFAILURE)
  989. icv_len = 0;
  990. if (skb->len < hdrlen + iv_len + icv_len) {
  991. wiphy_warn(priv->hw->wiphy, "Malformed SDU rx'ed. Size is lesser than crypto headers.\n");
  992. goto drop;
  993. }
  994. /* Remove IV, ICV and MIC */
  995. skb_trim(skb, skb->len - icv_len);
  996. memmove(skb->data + iv_len, skb->data, hdrlen);
  997. skb_pull(skb, iv_len);
  998. }
  999. /* Remove TSF from the end of frame */
  1000. if (arg->flags & WSM_RX_STATUS_TSF_INCLUDED) {
  1001. memcpy(&hdr->mactime, skb->data + skb->len - 8, 8);
  1002. hdr->mactime = le64_to_cpu(hdr->mactime);
  1003. if (skb->len >= 8)
  1004. skb_trim(skb, skb->len - 8);
  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 &&
  1015. !memcmp(ieee80211_get_SA(frame), priv->vif->bss_conf.bssid,
  1016. ETH_ALEN)) {
  1017. const u8 *tim_ie;
  1018. u8 *ies = ((struct ieee80211_mgmt *)
  1019. (skb->data))->u.beacon.variable;
  1020. size_t ies_len = skb->len - (ies - (u8 *)(skb->data));
  1021. tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies, ies_len);
  1022. if (tim_ie) {
  1023. struct ieee80211_tim_ie *tim =
  1024. (struct ieee80211_tim_ie *)&tim_ie[2];
  1025. if (priv->join_dtim_period != tim->dtim_period) {
  1026. priv->join_dtim_period = tim->dtim_period;
  1027. queue_work(priv->workqueue,
  1028. &priv->set_beacon_wakeup_period_work);
  1029. }
  1030. }
  1031. /* Disable beacon filter once we're associated... */
  1032. if (priv->disable_beacon_filter &&
  1033. (priv->vif->bss_conf.assoc ||
  1034. priv->vif->bss_conf.ibss_joined)) {
  1035. priv->disable_beacon_filter = false;
  1036. queue_work(priv->workqueue,
  1037. &priv->update_filtering_work);
  1038. }
  1039. }
  1040. /* Stay awake after frame is received to give
  1041. * userspace chance to react and acquire appropriate
  1042. * wakelock. */
  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. memset(map_link.mac_addr, 0, ETH_ALEN);
  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. }