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. /* 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 (cw1200_itp_tx_running(priv))
  750. return;
  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. /* tx->flags |= IEEE80211_TX_STAT_AMPDU; */
  810. cw1200_debug_txed_agg(priv);
  811. }
  812. } else {
  813. if (tx_count)
  814. ++tx_count;
  815. }
  816. for (i = 0; i < IEEE80211_TX_MAX_RATES; ++i) {
  817. if (tx->status.rates[i].count >= tx_count) {
  818. tx->status.rates[i].count = tx_count;
  819. break;
  820. }
  821. tx_count -= tx->status.rates[i].count;
  822. if (tx->status.rates[i].flags & IEEE80211_TX_RC_MCS)
  823. tx->status.rates[i].flags |= ht_flags;
  824. }
  825. for (++i; i < IEEE80211_TX_MAX_RATES; ++i) {
  826. tx->status.rates[i].count = 0;
  827. tx->status.rates[i].idx = -1;
  828. }
  829. /* Pull off any crypto trailers that we added on */
  830. if (tx->control.hw_key) {
  831. skb_trim(skb, skb->len - tx->control.hw_key->icv_len);
  832. if (tx->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP)
  833. skb_trim(skb, skb->len - 8); /* MIC space */
  834. }
  835. cw1200_queue_remove(queue, arg->packet_id);
  836. }
  837. /* XXX TODO: Only wake if there are pending transmits.. */
  838. cw1200_bh_wakeup(priv);
  839. }
  840. static void cw1200_notify_buffered_tx(struct cw1200_common *priv,
  841. struct sk_buff *skb, int link_id, int tid)
  842. {
  843. struct ieee80211_sta *sta;
  844. struct ieee80211_hdr *hdr;
  845. u8 *buffered;
  846. u8 still_buffered = 0;
  847. if (link_id && tid < CW1200_MAX_TID) {
  848. buffered = priv->link_id_db
  849. [link_id - 1].buffered;
  850. spin_lock_bh(&priv->ps_state_lock);
  851. if (!WARN_ON(!buffered[tid]))
  852. still_buffered = --buffered[tid];
  853. spin_unlock_bh(&priv->ps_state_lock);
  854. if (!still_buffered && tid < CW1200_MAX_TID) {
  855. hdr = (struct ieee80211_hdr *)skb->data;
  856. rcu_read_lock();
  857. sta = ieee80211_find_sta(priv->vif, hdr->addr1);
  858. if (sta)
  859. ieee80211_sta_set_buffered(sta, tid, false);
  860. rcu_read_unlock();
  861. }
  862. }
  863. }
  864. void cw1200_skb_dtor(struct cw1200_common *priv,
  865. struct sk_buff *skb,
  866. const struct cw1200_txpriv *txpriv)
  867. {
  868. skb_pull(skb, txpriv->offset);
  869. if (txpriv->rate_id != CW1200_INVALID_RATE_ID) {
  870. cw1200_notify_buffered_tx(priv, skb,
  871. txpriv->raw_link_id, txpriv->tid);
  872. tx_policy_put(priv, txpriv->rate_id);
  873. }
  874. if (!cw1200_is_itp(priv))
  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. WARN_ON(work_pending(&priv->linkid_reset_work));
  918. memcpy(&priv->action_frame_sa[0],
  919. ieee80211_get_SA(frame), ETH_ALEN);
  920. priv->action_linkid = link_id;
  921. schedule_work(&priv->linkid_reset_work);
  922. }
  923. if (arg->status) {
  924. if (arg->status == WSM_STATUS_MICFAILURE) {
  925. pr_debug("[RX] MIC failure.\n");
  926. hdr->flag |= RX_FLAG_MMIC_ERROR;
  927. } else if (arg->status == WSM_STATUS_NO_KEY_FOUND) {
  928. pr_debug("[RX] No key found.\n");
  929. goto drop;
  930. } else {
  931. pr_debug("[RX] Receive failure: %d.\n",
  932. arg->status);
  933. goto drop;
  934. }
  935. }
  936. if (skb->len < sizeof(struct ieee80211_pspoll)) {
  937. wiphy_warn(priv->hw->wiphy, "Mailformed SDU rx'ed. Size is lesser than IEEE header.\n");
  938. goto drop;
  939. }
  940. if (ieee80211_is_pspoll(frame->frame_control))
  941. if (cw1200_handle_pspoll(priv, skb))
  942. goto drop;
  943. hdr->mactime = 0; /* Not supported by WSM */
  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. switch (WSM_RX_STATUS_ENCRYPTION(arg->flags)) {
  967. case WSM_RX_STATUS_WEP:
  968. iv_len = 4 /* WEP_IV_LEN */;
  969. icv_len = 4 /* WEP_ICV_LEN */;
  970. break;
  971. case WSM_RX_STATUS_TKIP:
  972. iv_len = 8 /* TKIP_IV_LEN */;
  973. icv_len = 4 /* TKIP_ICV_LEN */
  974. + 8 /*MICHAEL_MIC_LEN*/;
  975. hdr->flag |= RX_FLAG_MMIC_STRIPPED;
  976. break;
  977. case WSM_RX_STATUS_AES:
  978. iv_len = 8 /* CCMP_HDR_LEN */;
  979. icv_len = 8 /* CCMP_MIC_LEN */;
  980. break;
  981. case WSM_RX_STATUS_WAPI:
  982. iv_len = 18 /* WAPI_HDR_LEN */;
  983. icv_len = 16 /* WAPI_MIC_LEN */;
  984. break;
  985. default:
  986. pr_warn("Unknown encryption type %d\n",
  987. WSM_RX_STATUS_ENCRYPTION(arg->flags));
  988. goto drop;
  989. }
  990. /* Firmware strips ICV in case of MIC failure. */
  991. if (arg->status == WSM_STATUS_MICFAILURE)
  992. icv_len = 0;
  993. if (skb->len < hdrlen + iv_len + icv_len) {
  994. wiphy_warn(priv->hw->wiphy, "Malformed SDU rx'ed. Size is lesser than crypto headers.\n");
  995. goto drop;
  996. }
  997. /* Remove IV, ICV and MIC */
  998. skb_trim(skb, skb->len - icv_len);
  999. memmove(skb->data + iv_len, skb->data, hdrlen);
  1000. skb_pull(skb, iv_len);
  1001. }
  1002. /* Remove TSF from the end of frame */
  1003. if (arg->flags & WSM_RX_STATUS_TSF_INCLUDED) {
  1004. memcpy(&hdr->mactime, skb->data + skb->len - 8, 8);
  1005. hdr->mactime = le64_to_cpu(hdr->mactime);
  1006. if (skb->len >= 8)
  1007. skb_trim(skb, skb->len - 8);
  1008. }
  1009. cw1200_debug_rxed(priv);
  1010. if (arg->flags & WSM_RX_STATUS_AGGREGATE)
  1011. cw1200_debug_rxed_agg(priv);
  1012. if (ieee80211_is_action(frame->frame_control) &&
  1013. (arg->flags & WSM_RX_STATUS_ADDRESS1)) {
  1014. if (cw1200_handle_action_rx(priv, skb))
  1015. return;
  1016. } else if (ieee80211_is_beacon(frame->frame_control) &&
  1017. !arg->status &&
  1018. !memcmp(ieee80211_get_SA(frame), priv->vif->bss_conf.bssid,
  1019. ETH_ALEN)) {
  1020. const u8 *tim_ie;
  1021. u8 *ies = ((struct ieee80211_mgmt *)
  1022. (skb->data))->u.beacon.variable;
  1023. size_t ies_len = skb->len - (ies - (u8 *)(skb->data));
  1024. tim_ie = cfg80211_find_ie(WLAN_EID_TIM, ies, ies_len);
  1025. if (tim_ie) {
  1026. struct ieee80211_tim_ie *tim =
  1027. (struct ieee80211_tim_ie *)&tim_ie[2];
  1028. if (priv->join_dtim_period != tim->dtim_period) {
  1029. priv->join_dtim_period = tim->dtim_period;
  1030. queue_work(priv->workqueue,
  1031. &priv->set_beacon_wakeup_period_work);
  1032. }
  1033. }
  1034. /* Disable beacon filter once we're associated... */
  1035. if (priv->disable_beacon_filter &&
  1036. (priv->vif->bss_conf.assoc ||
  1037. priv->vif->bss_conf.ibss_joined)) {
  1038. priv->disable_beacon_filter = false;
  1039. queue_work(priv->workqueue,
  1040. &priv->update_filtering_work);
  1041. }
  1042. }
  1043. /* Stay awake after frame is received to give
  1044. * userspace chance to react and acquire appropriate
  1045. * wakelock. */
  1046. if (ieee80211_is_auth(frame->frame_control))
  1047. grace_period = 5 * HZ;
  1048. else if (ieee80211_is_deauth(frame->frame_control))
  1049. grace_period = 5 * HZ;
  1050. else
  1051. grace_period = 1 * HZ;
  1052. cw1200_pm_stay_awake(&priv->pm_state, grace_period);
  1053. if (cw1200_itp_rxed(priv, skb)) {
  1054. consume_skb(skb);
  1055. } else if (early_data) {
  1056. spin_lock_bh(&priv->ps_state_lock);
  1057. /* Double-check status with lock held */
  1058. if (entry->status == CW1200_LINK_SOFT)
  1059. skb_queue_tail(&entry->rx_queue, skb);
  1060. else
  1061. ieee80211_rx_irqsafe(priv->hw, skb);
  1062. spin_unlock_bh(&priv->ps_state_lock);
  1063. } else {
  1064. ieee80211_rx_irqsafe(priv->hw, skb);
  1065. }
  1066. *skb_p = NULL;
  1067. return;
  1068. drop:
  1069. /* TODO: update failure counters */
  1070. return;
  1071. }
  1072. /* ******************************************************************** */
  1073. /* Security */
  1074. int cw1200_alloc_key(struct cw1200_common *priv)
  1075. {
  1076. int idx;
  1077. idx = ffs(~priv->key_map) - 1;
  1078. if (idx < 0 || idx > WSM_KEY_MAX_INDEX)
  1079. return -1;
  1080. priv->key_map |= BIT(idx);
  1081. priv->keys[idx].index = idx;
  1082. return idx;
  1083. }
  1084. void cw1200_free_key(struct cw1200_common *priv, int idx)
  1085. {
  1086. BUG_ON(!(priv->key_map & BIT(idx)));
  1087. memset(&priv->keys[idx], 0, sizeof(priv->keys[idx]));
  1088. priv->key_map &= ~BIT(idx);
  1089. }
  1090. void cw1200_free_keys(struct cw1200_common *priv)
  1091. {
  1092. memset(&priv->keys, 0, sizeof(priv->keys));
  1093. priv->key_map = 0;
  1094. }
  1095. int cw1200_upload_keys(struct cw1200_common *priv)
  1096. {
  1097. int idx, ret = 0;
  1098. for (idx = 0; idx <= WSM_KEY_MAX_INDEX; ++idx)
  1099. if (priv->key_map & BIT(idx)) {
  1100. ret = wsm_add_key(priv, &priv->keys[idx]);
  1101. if (ret < 0)
  1102. break;
  1103. }
  1104. return ret;
  1105. }
  1106. /* Workaround for WFD test case 6.1.10 */
  1107. void cw1200_link_id_reset(struct work_struct *work)
  1108. {
  1109. struct cw1200_common *priv =
  1110. container_of(work, struct cw1200_common, linkid_reset_work);
  1111. int temp_linkid;
  1112. if (!priv->action_linkid) {
  1113. /* In GO mode we can receive ACTION frames without a linkID */
  1114. temp_linkid = cw1200_alloc_link_id(priv,
  1115. &priv->action_frame_sa[0]);
  1116. WARN_ON(!temp_linkid);
  1117. if (temp_linkid) {
  1118. /* Make sure we execute the WQ */
  1119. flush_workqueue(priv->workqueue);
  1120. /* Release the link ID */
  1121. spin_lock_bh(&priv->ps_state_lock);
  1122. priv->link_id_db[temp_linkid - 1].prev_status =
  1123. priv->link_id_db[temp_linkid - 1].status;
  1124. priv->link_id_db[temp_linkid - 1].status =
  1125. CW1200_LINK_RESET;
  1126. spin_unlock_bh(&priv->ps_state_lock);
  1127. wsm_lock_tx_async(priv);
  1128. if (queue_work(priv->workqueue,
  1129. &priv->link_id_work) <= 0)
  1130. wsm_unlock_tx(priv);
  1131. }
  1132. } else {
  1133. spin_lock_bh(&priv->ps_state_lock);
  1134. priv->link_id_db[priv->action_linkid - 1].prev_status =
  1135. priv->link_id_db[priv->action_linkid - 1].status;
  1136. priv->link_id_db[priv->action_linkid - 1].status =
  1137. CW1200_LINK_RESET_REMAP;
  1138. spin_unlock_bh(&priv->ps_state_lock);
  1139. wsm_lock_tx_async(priv);
  1140. if (queue_work(priv->workqueue, &priv->link_id_work) <= 0)
  1141. wsm_unlock_tx(priv);
  1142. flush_workqueue(priv->workqueue);
  1143. }
  1144. }
  1145. int cw1200_find_link_id(struct cw1200_common *priv, const u8 *mac)
  1146. {
  1147. int i, ret = 0;
  1148. spin_lock_bh(&priv->ps_state_lock);
  1149. for (i = 0; i < CW1200_MAX_STA_IN_AP_MODE; ++i) {
  1150. if (!memcmp(mac, priv->link_id_db[i].mac, ETH_ALEN) &&
  1151. priv->link_id_db[i].status) {
  1152. priv->link_id_db[i].timestamp = jiffies;
  1153. ret = i + 1;
  1154. break;
  1155. }
  1156. }
  1157. spin_unlock_bh(&priv->ps_state_lock);
  1158. return ret;
  1159. }
  1160. int cw1200_alloc_link_id(struct cw1200_common *priv, const u8 *mac)
  1161. {
  1162. int i, ret = 0;
  1163. unsigned long max_inactivity = 0;
  1164. unsigned long now = jiffies;
  1165. spin_lock_bh(&priv->ps_state_lock);
  1166. for (i = 0; i < CW1200_MAX_STA_IN_AP_MODE; ++i) {
  1167. if (!priv->link_id_db[i].status) {
  1168. ret = i + 1;
  1169. break;
  1170. } else if (priv->link_id_db[i].status != CW1200_LINK_HARD &&
  1171. !priv->tx_queue_stats.link_map_cache[i + 1]) {
  1172. unsigned long inactivity =
  1173. now - priv->link_id_db[i].timestamp;
  1174. if (inactivity < max_inactivity)
  1175. continue;
  1176. max_inactivity = inactivity;
  1177. ret = i + 1;
  1178. }
  1179. }
  1180. if (ret) {
  1181. struct cw1200_link_entry *entry = &priv->link_id_db[ret - 1];
  1182. pr_debug("[AP] STA added, link_id: %d\n", ret);
  1183. entry->status = CW1200_LINK_RESERVE;
  1184. memcpy(&entry->mac, mac, ETH_ALEN);
  1185. memset(&entry->buffered, 0, CW1200_MAX_TID);
  1186. skb_queue_head_init(&entry->rx_queue);
  1187. wsm_lock_tx_async(priv);
  1188. if (queue_work(priv->workqueue, &priv->link_id_work) <= 0)
  1189. wsm_unlock_tx(priv);
  1190. } else {
  1191. wiphy_info(priv->hw->wiphy,
  1192. "[AP] Early: no more link IDs available.\n");
  1193. }
  1194. spin_unlock_bh(&priv->ps_state_lock);
  1195. return ret;
  1196. }
  1197. void cw1200_link_id_work(struct work_struct *work)
  1198. {
  1199. struct cw1200_common *priv =
  1200. container_of(work, struct cw1200_common, link_id_work);
  1201. wsm_flush_tx(priv);
  1202. cw1200_link_id_gc_work(&priv->link_id_gc_work.work);
  1203. wsm_unlock_tx(priv);
  1204. }
  1205. void cw1200_link_id_gc_work(struct work_struct *work)
  1206. {
  1207. struct cw1200_common *priv =
  1208. container_of(work, struct cw1200_common, link_id_gc_work.work);
  1209. struct wsm_reset reset = {
  1210. .reset_statistics = false,
  1211. };
  1212. struct wsm_map_link map_link = {
  1213. .link_id = 0,
  1214. };
  1215. unsigned long now = jiffies;
  1216. unsigned long next_gc = -1;
  1217. long ttl;
  1218. bool need_reset;
  1219. u32 mask;
  1220. int i;
  1221. if (priv->join_status != CW1200_JOIN_STATUS_AP)
  1222. return;
  1223. wsm_lock_tx(priv);
  1224. spin_lock_bh(&priv->ps_state_lock);
  1225. for (i = 0; i < CW1200_MAX_STA_IN_AP_MODE; ++i) {
  1226. need_reset = false;
  1227. mask = BIT(i + 1);
  1228. if (priv->link_id_db[i].status == CW1200_LINK_RESERVE ||
  1229. (priv->link_id_db[i].status == CW1200_LINK_HARD &&
  1230. !(priv->link_id_map & mask))) {
  1231. if (priv->link_id_map & mask) {
  1232. priv->sta_asleep_mask &= ~mask;
  1233. priv->pspoll_mask &= ~mask;
  1234. need_reset = true;
  1235. }
  1236. priv->link_id_map |= mask;
  1237. if (priv->link_id_db[i].status != CW1200_LINK_HARD)
  1238. priv->link_id_db[i].status = CW1200_LINK_SOFT;
  1239. memcpy(map_link.mac_addr, priv->link_id_db[i].mac,
  1240. ETH_ALEN);
  1241. spin_unlock_bh(&priv->ps_state_lock);
  1242. if (need_reset) {
  1243. reset.link_id = i + 1;
  1244. wsm_reset(priv, &reset);
  1245. }
  1246. map_link.link_id = i + 1;
  1247. wsm_map_link(priv, &map_link);
  1248. next_gc = min(next_gc, CW1200_LINK_ID_GC_TIMEOUT);
  1249. spin_lock_bh(&priv->ps_state_lock);
  1250. } else if (priv->link_id_db[i].status == CW1200_LINK_SOFT) {
  1251. ttl = priv->link_id_db[i].timestamp - now +
  1252. CW1200_LINK_ID_GC_TIMEOUT;
  1253. if (ttl <= 0) {
  1254. need_reset = true;
  1255. priv->link_id_db[i].status = CW1200_LINK_OFF;
  1256. priv->link_id_map &= ~mask;
  1257. priv->sta_asleep_mask &= ~mask;
  1258. priv->pspoll_mask &= ~mask;
  1259. memset(map_link.mac_addr, 0, ETH_ALEN);
  1260. spin_unlock_bh(&priv->ps_state_lock);
  1261. reset.link_id = i + 1;
  1262. wsm_reset(priv, &reset);
  1263. spin_lock_bh(&priv->ps_state_lock);
  1264. } else {
  1265. next_gc = min_t(unsigned long, next_gc, ttl);
  1266. }
  1267. } else if (priv->link_id_db[i].status == CW1200_LINK_RESET ||
  1268. priv->link_id_db[i].status ==
  1269. CW1200_LINK_RESET_REMAP) {
  1270. int status = priv->link_id_db[i].status;
  1271. priv->link_id_db[i].status =
  1272. priv->link_id_db[i].prev_status;
  1273. priv->link_id_db[i].timestamp = now;
  1274. reset.link_id = i + 1;
  1275. spin_unlock_bh(&priv->ps_state_lock);
  1276. wsm_reset(priv, &reset);
  1277. if (status == CW1200_LINK_RESET_REMAP) {
  1278. memcpy(map_link.mac_addr,
  1279. priv->link_id_db[i].mac,
  1280. ETH_ALEN);
  1281. map_link.link_id = i + 1;
  1282. wsm_map_link(priv, &map_link);
  1283. next_gc = min(next_gc,
  1284. CW1200_LINK_ID_GC_TIMEOUT);
  1285. }
  1286. spin_lock_bh(&priv->ps_state_lock);
  1287. }
  1288. if (need_reset) {
  1289. skb_queue_purge(&priv->link_id_db[i].rx_queue);
  1290. pr_debug("[AP] STA removed, link_id: %d\n",
  1291. reset.link_id);
  1292. }
  1293. }
  1294. spin_unlock_bh(&priv->ps_state_lock);
  1295. if (next_gc != -1)
  1296. queue_delayed_work(priv->workqueue,
  1297. &priv->link_id_gc_work, next_gc);
  1298. wsm_unlock_tx(priv);
  1299. }