txrx.c 39 KB

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