txrx.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /*
  2. * Common code for mac80211 Prism54 drivers
  3. *
  4. * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net>
  5. * Copyright (c) 2007-2009, Christian Lamparter <chunkeey@web.de>
  6. * Copyright 2008, Johannes Berg <johannes@sipsolutions.net>
  7. *
  8. * Based on:
  9. * - the islsm (softmac prism54) driver, which is:
  10. * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al.
  11. * - stlc45xx driver
  12. * Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License version 2 as
  16. * published by the Free Software Foundation.
  17. */
  18. #include <linux/export.h>
  19. #include <linux/init.h>
  20. #include <linux/firmware.h>
  21. #include <linux/etherdevice.h>
  22. #include <asm/div64.h>
  23. #include <net/mac80211.h>
  24. #include "p54.h"
  25. #include "lmac.h"
  26. #ifdef P54_MM_DEBUG
  27. static void p54_dump_tx_queue(struct p54_common *priv)
  28. {
  29. unsigned long flags;
  30. struct ieee80211_tx_info *info;
  31. struct p54_tx_info *range;
  32. struct sk_buff *skb;
  33. struct p54_hdr *hdr;
  34. unsigned int i = 0;
  35. u32 prev_addr;
  36. u32 largest_hole = 0, free;
  37. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  38. wiphy_debug(priv->hw->wiphy, "/ --- tx queue dump (%d entries) ---\n",
  39. skb_queue_len(&priv->tx_queue));
  40. prev_addr = priv->rx_start;
  41. skb_queue_walk(&priv->tx_queue, skb) {
  42. info = IEEE80211_SKB_CB(skb);
  43. range = (void *) info->rate_driver_data;
  44. hdr = (void *) skb->data;
  45. free = range->start_addr - prev_addr;
  46. wiphy_debug(priv->hw->wiphy,
  47. "| [%02d] => [skb:%p skb_len:0x%04x "
  48. "hdr:{flags:%02x len:%04x req_id:%04x type:%02x} "
  49. "mem:{start:%04x end:%04x, free:%d}]\n",
  50. i++, skb, skb->len,
  51. le16_to_cpu(hdr->flags), le16_to_cpu(hdr->len),
  52. le32_to_cpu(hdr->req_id), le16_to_cpu(hdr->type),
  53. range->start_addr, range->end_addr, free);
  54. prev_addr = range->end_addr;
  55. largest_hole = max(largest_hole, free);
  56. }
  57. free = priv->rx_end - prev_addr;
  58. largest_hole = max(largest_hole, free);
  59. wiphy_debug(priv->hw->wiphy,
  60. "\\ --- [free: %d], largest free block: %d ---\n",
  61. free, largest_hole);
  62. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  63. }
  64. #endif /* P54_MM_DEBUG */
  65. /*
  66. * So, the firmware is somewhat stupid and doesn't know what places in its
  67. * memory incoming data should go to. By poking around in the firmware, we
  68. * can find some unused memory to upload our packets to. However, data that we
  69. * want the card to TX needs to stay intact until the card has told us that
  70. * it is done with it. This function finds empty places we can upload to and
  71. * marks allocated areas as reserved if necessary. p54_find_and_unlink_skb or
  72. * p54_free_skb frees allocated areas.
  73. */
  74. static int p54_assign_address(struct p54_common *priv, struct sk_buff *skb)
  75. {
  76. struct sk_buff *entry, *target_skb = NULL;
  77. struct ieee80211_tx_info *info;
  78. struct p54_tx_info *range;
  79. struct p54_hdr *data = (void *) skb->data;
  80. unsigned long flags;
  81. u32 last_addr = priv->rx_start;
  82. u32 target_addr = priv->rx_start;
  83. u16 len = priv->headroom + skb->len + priv->tailroom + 3;
  84. info = IEEE80211_SKB_CB(skb);
  85. range = (void *) info->rate_driver_data;
  86. len = (range->extra_len + len) & ~0x3;
  87. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  88. if (unlikely(skb_queue_len(&priv->tx_queue) == 32)) {
  89. /*
  90. * The tx_queue is now really full.
  91. *
  92. * TODO: check if the device has crashed and reset it.
  93. */
  94. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  95. return -EBUSY;
  96. }
  97. skb_queue_walk(&priv->tx_queue, entry) {
  98. u32 hole_size;
  99. info = IEEE80211_SKB_CB(entry);
  100. range = (void *) info->rate_driver_data;
  101. hole_size = range->start_addr - last_addr;
  102. if (!target_skb && hole_size >= len) {
  103. target_skb = entry->prev;
  104. hole_size -= len;
  105. target_addr = last_addr;
  106. break;
  107. }
  108. last_addr = range->end_addr;
  109. }
  110. if (unlikely(!target_skb)) {
  111. if (priv->rx_end - last_addr >= len) {
  112. target_skb = priv->tx_queue.prev;
  113. if (!skb_queue_empty(&priv->tx_queue)) {
  114. info = IEEE80211_SKB_CB(target_skb);
  115. range = (void *)info->rate_driver_data;
  116. target_addr = range->end_addr;
  117. }
  118. } else {
  119. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  120. return -ENOSPC;
  121. }
  122. }
  123. info = IEEE80211_SKB_CB(skb);
  124. range = (void *) info->rate_driver_data;
  125. range->start_addr = target_addr;
  126. range->end_addr = target_addr + len;
  127. data->req_id = cpu_to_le32(target_addr + priv->headroom);
  128. if (IS_DATA_FRAME(skb) &&
  129. unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON))
  130. priv->beacon_req_id = data->req_id;
  131. __skb_queue_after(&priv->tx_queue, target_skb, skb);
  132. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  133. return 0;
  134. }
  135. static void p54_tx_pending(struct p54_common *priv)
  136. {
  137. struct sk_buff *skb;
  138. int ret;
  139. skb = skb_dequeue(&priv->tx_pending);
  140. if (unlikely(!skb))
  141. return ;
  142. ret = p54_assign_address(priv, skb);
  143. if (unlikely(ret))
  144. skb_queue_head(&priv->tx_pending, skb);
  145. else
  146. priv->tx(priv->hw, skb);
  147. }
  148. static void p54_wake_queues(struct p54_common *priv)
  149. {
  150. unsigned long flags;
  151. unsigned int i;
  152. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  153. return ;
  154. p54_tx_pending(priv);
  155. spin_lock_irqsave(&priv->tx_stats_lock, flags);
  156. for (i = 0; i < priv->hw->queues; i++) {
  157. if (priv->tx_stats[i + P54_QUEUE_DATA].len <
  158. priv->tx_stats[i + P54_QUEUE_DATA].limit)
  159. ieee80211_wake_queue(priv->hw, i);
  160. }
  161. spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
  162. }
  163. static int p54_tx_qos_accounting_alloc(struct p54_common *priv,
  164. struct sk_buff *skb,
  165. const u16 p54_queue)
  166. {
  167. struct p54_tx_queue_stats *queue;
  168. unsigned long flags;
  169. if (WARN_ON(p54_queue >= P54_QUEUE_NUM))
  170. return -EINVAL;
  171. queue = &priv->tx_stats[p54_queue];
  172. spin_lock_irqsave(&priv->tx_stats_lock, flags);
  173. if (unlikely(queue->len >= queue->limit && IS_QOS_QUEUE(p54_queue))) {
  174. spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
  175. return -ENOSPC;
  176. }
  177. queue->len++;
  178. queue->count++;
  179. if (unlikely(queue->len == queue->limit && IS_QOS_QUEUE(p54_queue))) {
  180. u16 ac_queue = p54_queue - P54_QUEUE_DATA;
  181. ieee80211_stop_queue(priv->hw, ac_queue);
  182. }
  183. spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
  184. return 0;
  185. }
  186. static void p54_tx_qos_accounting_free(struct p54_common *priv,
  187. struct sk_buff *skb)
  188. {
  189. if (IS_DATA_FRAME(skb)) {
  190. unsigned long flags;
  191. spin_lock_irqsave(&priv->tx_stats_lock, flags);
  192. priv->tx_stats[GET_HW_QUEUE(skb)].len--;
  193. spin_unlock_irqrestore(&priv->tx_stats_lock, flags);
  194. if (unlikely(GET_HW_QUEUE(skb) == P54_QUEUE_BEACON)) {
  195. if (priv->beacon_req_id == GET_REQ_ID(skb)) {
  196. /* this is the active beacon set anymore */
  197. priv->beacon_req_id = 0;
  198. }
  199. complete(&priv->beacon_comp);
  200. }
  201. }
  202. p54_wake_queues(priv);
  203. }
  204. void p54_free_skb(struct ieee80211_hw *dev, struct sk_buff *skb)
  205. {
  206. struct p54_common *priv = dev->priv;
  207. if (unlikely(!skb))
  208. return ;
  209. skb_unlink(skb, &priv->tx_queue);
  210. p54_tx_qos_accounting_free(priv, skb);
  211. ieee80211_free_txskb(dev, skb);
  212. }
  213. EXPORT_SYMBOL_GPL(p54_free_skb);
  214. static struct sk_buff *p54_find_and_unlink_skb(struct p54_common *priv,
  215. const __le32 req_id)
  216. {
  217. struct sk_buff *entry;
  218. unsigned long flags;
  219. spin_lock_irqsave(&priv->tx_queue.lock, flags);
  220. skb_queue_walk(&priv->tx_queue, entry) {
  221. struct p54_hdr *hdr = (struct p54_hdr *) entry->data;
  222. if (hdr->req_id == req_id) {
  223. __skb_unlink(entry, &priv->tx_queue);
  224. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  225. p54_tx_qos_accounting_free(priv, entry);
  226. return entry;
  227. }
  228. }
  229. spin_unlock_irqrestore(&priv->tx_queue.lock, flags);
  230. return NULL;
  231. }
  232. void p54_tx(struct p54_common *priv, struct sk_buff *skb)
  233. {
  234. skb_queue_tail(&priv->tx_pending, skb);
  235. p54_tx_pending(priv);
  236. }
  237. static int p54_rssi_to_dbm(struct p54_common *priv, int rssi)
  238. {
  239. if (priv->rxhw != 5) {
  240. return ((rssi * priv->cur_rssi->mul) / 64 +
  241. priv->cur_rssi->add) / 4;
  242. } else {
  243. /*
  244. * TODO: find the correct formula
  245. */
  246. return rssi / 2 - 110;
  247. }
  248. }
  249. /*
  250. * Even if the firmware is capable of dealing with incoming traffic,
  251. * while dozing, we have to prepared in case mac80211 uses PS-POLL
  252. * to retrieve outstanding frames from our AP.
  253. * (see comment in net/mac80211/mlme.c @ line 1993)
  254. */
  255. static void p54_pspoll_workaround(struct p54_common *priv, struct sk_buff *skb)
  256. {
  257. struct ieee80211_hdr *hdr = (void *) skb->data;
  258. struct ieee80211_tim_ie *tim_ie;
  259. u8 *tim;
  260. u8 tim_len;
  261. bool new_psm;
  262. /* only beacons have a TIM IE */
  263. if (!ieee80211_is_beacon(hdr->frame_control))
  264. return;
  265. if (!priv->aid)
  266. return;
  267. /* only consider beacons from the associated BSSID */
  268. if (!ether_addr_equal(hdr->addr3, priv->bssid))
  269. return;
  270. tim = p54_find_ie(skb, WLAN_EID_TIM);
  271. if (!tim)
  272. return;
  273. tim_len = tim[1];
  274. tim_ie = (struct ieee80211_tim_ie *) &tim[2];
  275. new_psm = ieee80211_check_tim(tim_ie, tim_len, priv->aid);
  276. if (new_psm != priv->powersave_override) {
  277. priv->powersave_override = new_psm;
  278. p54_set_ps(priv);
  279. }
  280. }
  281. static int p54_rx_data(struct p54_common *priv, struct sk_buff *skb)
  282. {
  283. struct p54_rx_data *hdr = (struct p54_rx_data *) skb->data;
  284. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  285. u16 freq = le16_to_cpu(hdr->freq);
  286. size_t header_len = sizeof(*hdr);
  287. u32 tsf32;
  288. u8 rate = hdr->rate & 0xf;
  289. /*
  290. * If the device is in a unspecified state we have to
  291. * ignore all data frames. Else we could end up with a
  292. * nasty crash.
  293. */
  294. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  295. return 0;
  296. if (!(hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_IN_FCS_GOOD)))
  297. return 0;
  298. if (hdr->decrypt_status == P54_DECRYPT_OK)
  299. rx_status->flag |= RX_FLAG_DECRYPTED;
  300. if ((hdr->decrypt_status == P54_DECRYPT_FAIL_MICHAEL) ||
  301. (hdr->decrypt_status == P54_DECRYPT_FAIL_TKIP))
  302. rx_status->flag |= RX_FLAG_MMIC_ERROR;
  303. rx_status->signal = p54_rssi_to_dbm(priv, hdr->rssi);
  304. if (hdr->rate & 0x10)
  305. rx_status->flag |= RX_FLAG_SHORTPRE;
  306. if (priv->hw->conf.chandef.chan->band == IEEE80211_BAND_5GHZ)
  307. rx_status->rate_idx = (rate < 4) ? 0 : rate - 4;
  308. else
  309. rx_status->rate_idx = rate;
  310. rx_status->freq = freq;
  311. rx_status->band = priv->hw->conf.chandef.chan->band;
  312. rx_status->antenna = hdr->antenna;
  313. tsf32 = le32_to_cpu(hdr->tsf32);
  314. if (tsf32 < priv->tsf_low32)
  315. priv->tsf_high32++;
  316. rx_status->mactime = ((u64)priv->tsf_high32) << 32 | tsf32;
  317. priv->tsf_low32 = tsf32;
  318. /* LMAC API Page 10/29 - s_lm_data_in - clock
  319. * "usec accurate timestamp of hardware clock
  320. * at end of frame (before OFDM SIFS EOF padding"
  321. */
  322. rx_status->flag |= RX_FLAG_MACTIME_END;
  323. if (hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  324. header_len += hdr->align[0];
  325. skb_pull(skb, header_len);
  326. skb_trim(skb, le16_to_cpu(hdr->len));
  327. if (unlikely(priv->hw->conf.flags & IEEE80211_CONF_PS))
  328. p54_pspoll_workaround(priv, skb);
  329. ieee80211_rx_irqsafe(priv->hw, skb);
  330. ieee80211_queue_delayed_work(priv->hw, &priv->work,
  331. msecs_to_jiffies(P54_STATISTICS_UPDATE));
  332. return -1;
  333. }
  334. static void p54_rx_frame_sent(struct p54_common *priv, struct sk_buff *skb)
  335. {
  336. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  337. struct p54_frame_sent *payload = (struct p54_frame_sent *) hdr->data;
  338. struct ieee80211_tx_info *info;
  339. struct p54_hdr *entry_hdr;
  340. struct p54_tx_data *entry_data;
  341. struct sk_buff *entry;
  342. unsigned int pad = 0, frame_len;
  343. int count, idx;
  344. entry = p54_find_and_unlink_skb(priv, hdr->req_id);
  345. if (unlikely(!entry))
  346. return ;
  347. frame_len = entry->len;
  348. info = IEEE80211_SKB_CB(entry);
  349. entry_hdr = (struct p54_hdr *) entry->data;
  350. entry_data = (struct p54_tx_data *) entry_hdr->data;
  351. priv->stats.dot11ACKFailureCount += payload->tries - 1;
  352. /*
  353. * Frames in P54_QUEUE_FWSCAN and P54_QUEUE_BEACON are
  354. * generated by the driver. Therefore tx_status is bogus
  355. * and we don't want to confuse the mac80211 stack.
  356. */
  357. if (unlikely(entry_data->hw_queue < P54_QUEUE_FWSCAN)) {
  358. dev_kfree_skb_any(entry);
  359. return ;
  360. }
  361. /*
  362. * Clear manually, ieee80211_tx_info_clear_status would
  363. * clear the counts too and we need them.
  364. */
  365. memset(&info->status.ack_signal, 0,
  366. sizeof(struct ieee80211_tx_info) -
  367. offsetof(struct ieee80211_tx_info, status.ack_signal));
  368. BUILD_BUG_ON(offsetof(struct ieee80211_tx_info,
  369. status.ack_signal) != 20);
  370. if (entry_hdr->flags & cpu_to_le16(P54_HDR_FLAG_DATA_ALIGN))
  371. pad = entry_data->align[0];
  372. /* walk through the rates array and adjust the counts */
  373. count = payload->tries;
  374. for (idx = 0; idx < 4; idx++) {
  375. if (count >= info->status.rates[idx].count) {
  376. count -= info->status.rates[idx].count;
  377. } else if (count > 0) {
  378. info->status.rates[idx].count = count;
  379. count = 0;
  380. } else {
  381. info->status.rates[idx].idx = -1;
  382. info->status.rates[idx].count = 0;
  383. }
  384. }
  385. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK) &&
  386. !(payload->status & P54_TX_FAILED))
  387. info->flags |= IEEE80211_TX_STAT_ACK;
  388. if (payload->status & P54_TX_PSM_CANCELLED)
  389. info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
  390. info->status.ack_signal = p54_rssi_to_dbm(priv,
  391. (int)payload->ack_rssi);
  392. /* Undo all changes to the frame. */
  393. switch (entry_data->key_type) {
  394. case P54_CRYPTO_TKIPMICHAEL: {
  395. u8 *iv = (u8 *)(entry_data->align + pad +
  396. entry_data->crypt_offset);
  397. /* Restore the original TKIP IV. */
  398. iv[2] = iv[0];
  399. iv[0] = iv[1];
  400. iv[1] = (iv[0] | 0x20) & 0x7f; /* WEPSeed - 8.3.2.2 */
  401. frame_len -= 12; /* remove TKIP_MMIC + TKIP_ICV */
  402. break;
  403. }
  404. case P54_CRYPTO_AESCCMP:
  405. frame_len -= 8; /* remove CCMP_MIC */
  406. break;
  407. case P54_CRYPTO_WEP:
  408. frame_len -= 4; /* remove WEP_ICV */
  409. break;
  410. }
  411. skb_trim(entry, frame_len);
  412. skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));
  413. ieee80211_tx_status_irqsafe(priv->hw, entry);
  414. }
  415. static void p54_rx_eeprom_readback(struct p54_common *priv,
  416. struct sk_buff *skb)
  417. {
  418. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  419. struct p54_eeprom_lm86 *eeprom = (struct p54_eeprom_lm86 *) hdr->data;
  420. struct sk_buff *tmp;
  421. if (!priv->eeprom)
  422. return ;
  423. if (priv->fw_var >= 0x509) {
  424. memcpy(priv->eeprom, eeprom->v2.data,
  425. le16_to_cpu(eeprom->v2.len));
  426. } else {
  427. memcpy(priv->eeprom, eeprom->v1.data,
  428. le16_to_cpu(eeprom->v1.len));
  429. }
  430. priv->eeprom = NULL;
  431. tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
  432. dev_kfree_skb_any(tmp);
  433. complete(&priv->eeprom_comp);
  434. }
  435. static void p54_rx_stats(struct p54_common *priv, struct sk_buff *skb)
  436. {
  437. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  438. struct p54_statistics *stats = (struct p54_statistics *) hdr->data;
  439. struct sk_buff *tmp;
  440. struct ieee80211_channel *chan;
  441. unsigned int i, rssi, tx, cca, dtime, dtotal, dcca, dtx, drssi, unit;
  442. u32 tsf32;
  443. if (unlikely(priv->mode == NL80211_IFTYPE_UNSPECIFIED))
  444. return ;
  445. tsf32 = le32_to_cpu(stats->tsf32);
  446. if (tsf32 < priv->tsf_low32)
  447. priv->tsf_high32++;
  448. priv->tsf_low32 = tsf32;
  449. priv->stats.dot11RTSFailureCount = le32_to_cpu(stats->rts_fail);
  450. priv->stats.dot11RTSSuccessCount = le32_to_cpu(stats->rts_success);
  451. priv->stats.dot11FCSErrorCount = le32_to_cpu(stats->rx_bad_fcs);
  452. priv->noise = p54_rssi_to_dbm(priv, le32_to_cpu(stats->noise));
  453. /*
  454. * STSW450X LMAC API page 26 - 3.8 Statistics
  455. * "The exact measurement period can be derived from the
  456. * timestamp member".
  457. */
  458. dtime = tsf32 - priv->survey_raw.timestamp;
  459. /*
  460. * STSW450X LMAC API page 26 - 3.8.1 Noise histogram
  461. * The LMAC samples RSSI, CCA and transmit state at regular
  462. * periods (typically 8 times per 1k [as in 1024] usec).
  463. */
  464. cca = le32_to_cpu(stats->sample_cca);
  465. tx = le32_to_cpu(stats->sample_tx);
  466. rssi = 0;
  467. for (i = 0; i < ARRAY_SIZE(stats->sample_noise); i++)
  468. rssi += le32_to_cpu(stats->sample_noise[i]);
  469. dcca = cca - priv->survey_raw.cached_cca;
  470. drssi = rssi - priv->survey_raw.cached_rssi;
  471. dtx = tx - priv->survey_raw.cached_tx;
  472. dtotal = dcca + drssi + dtx;
  473. /*
  474. * update statistics when more than a second is over since the
  475. * last call, or when a update is badly needed.
  476. */
  477. if (dtotal && (priv->update_stats || dtime >= USEC_PER_SEC) &&
  478. dtime >= dtotal) {
  479. priv->survey_raw.timestamp = tsf32;
  480. priv->update_stats = false;
  481. unit = dtime / dtotal;
  482. if (dcca) {
  483. priv->survey_raw.cca += dcca * unit;
  484. priv->survey_raw.cached_cca = cca;
  485. }
  486. if (dtx) {
  487. priv->survey_raw.tx += dtx * unit;
  488. priv->survey_raw.cached_tx = tx;
  489. }
  490. if (drssi) {
  491. priv->survey_raw.rssi += drssi * unit;
  492. priv->survey_raw.cached_rssi = rssi;
  493. }
  494. /* 1024 usec / 8 times = 128 usec / time */
  495. if (!(priv->phy_ps || priv->phy_idle))
  496. priv->survey_raw.active += dtotal * unit;
  497. else
  498. priv->survey_raw.active += (dcca + dtx) * unit;
  499. }
  500. chan = priv->curchan;
  501. if (chan) {
  502. struct survey_info *survey = &priv->survey[chan->hw_value];
  503. survey->noise = clamp_t(s8, priv->noise, -128, 127);
  504. survey->channel_time = priv->survey_raw.active;
  505. survey->channel_time_tx = priv->survey_raw.tx;
  506. survey->channel_time_busy = priv->survey_raw.tx +
  507. priv->survey_raw.cca;
  508. do_div(survey->channel_time, 1024);
  509. do_div(survey->channel_time_tx, 1024);
  510. do_div(survey->channel_time_busy, 1024);
  511. }
  512. tmp = p54_find_and_unlink_skb(priv, hdr->req_id);
  513. dev_kfree_skb_any(tmp);
  514. complete(&priv->stat_comp);
  515. }
  516. static void p54_rx_trap(struct p54_common *priv, struct sk_buff *skb)
  517. {
  518. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  519. struct p54_trap *trap = (struct p54_trap *) hdr->data;
  520. u16 event = le16_to_cpu(trap->event);
  521. u16 freq = le16_to_cpu(trap->frequency);
  522. switch (event) {
  523. case P54_TRAP_BEACON_TX:
  524. break;
  525. case P54_TRAP_RADAR:
  526. wiphy_info(priv->hw->wiphy, "radar (freq:%d MHz)\n", freq);
  527. break;
  528. case P54_TRAP_NO_BEACON:
  529. if (priv->vif)
  530. ieee80211_beacon_loss(priv->vif);
  531. break;
  532. case P54_TRAP_SCAN:
  533. break;
  534. case P54_TRAP_TBTT:
  535. break;
  536. case P54_TRAP_TIMER:
  537. break;
  538. case P54_TRAP_FAA_RADIO_OFF:
  539. wiphy_rfkill_set_hw_state(priv->hw->wiphy, true);
  540. break;
  541. case P54_TRAP_FAA_RADIO_ON:
  542. wiphy_rfkill_set_hw_state(priv->hw->wiphy, false);
  543. break;
  544. default:
  545. wiphy_info(priv->hw->wiphy, "received event:%x freq:%d\n",
  546. event, freq);
  547. break;
  548. }
  549. }
  550. static int p54_rx_control(struct p54_common *priv, struct sk_buff *skb)
  551. {
  552. struct p54_hdr *hdr = (struct p54_hdr *) skb->data;
  553. switch (le16_to_cpu(hdr->type)) {
  554. case P54_CONTROL_TYPE_TXDONE:
  555. p54_rx_frame_sent(priv, skb);
  556. break;
  557. case P54_CONTROL_TYPE_TRAP:
  558. p54_rx_trap(priv, skb);
  559. break;
  560. case P54_CONTROL_TYPE_BBP:
  561. break;
  562. case P54_CONTROL_TYPE_STAT_READBACK:
  563. p54_rx_stats(priv, skb);
  564. break;
  565. case P54_CONTROL_TYPE_EEPROM_READBACK:
  566. p54_rx_eeprom_readback(priv, skb);
  567. break;
  568. default:
  569. wiphy_debug(priv->hw->wiphy,
  570. "not handling 0x%02x type control frame\n",
  571. le16_to_cpu(hdr->type));
  572. break;
  573. }
  574. return 0;
  575. }
  576. /* returns zero if skb can be reused */
  577. int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb)
  578. {
  579. struct p54_common *priv = dev->priv;
  580. u16 type = le16_to_cpu(*((__le16 *)skb->data));
  581. if (type & P54_HDR_FLAG_CONTROL)
  582. return p54_rx_control(priv, skb);
  583. else
  584. return p54_rx_data(priv, skb);
  585. }
  586. EXPORT_SYMBOL_GPL(p54_rx);
  587. static void p54_tx_80211_header(struct p54_common *priv, struct sk_buff *skb,
  588. struct ieee80211_tx_info *info,
  589. struct ieee80211_sta *sta,
  590. u8 *queue, u32 *extra_len, u16 *flags, u16 *aid,
  591. bool *burst_possible)
  592. {
  593. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  594. if (ieee80211_is_data_qos(hdr->frame_control))
  595. *burst_possible = true;
  596. else
  597. *burst_possible = false;
  598. if (!(info->flags & IEEE80211_TX_CTL_ASSIGN_SEQ))
  599. *flags |= P54_HDR_FLAG_DATA_OUT_SEQNR;
  600. if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER)
  601. *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  602. if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT)
  603. *flags |= P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  604. *queue = skb_get_queue_mapping(skb) + P54_QUEUE_DATA;
  605. switch (priv->mode) {
  606. case NL80211_IFTYPE_MONITOR:
  607. /*
  608. * We have to set P54_HDR_FLAG_DATA_OUT_PROMISC for
  609. * every frame in promiscuous/monitor mode.
  610. * see STSW45x0C LMAC API - page 12.
  611. */
  612. *aid = 0;
  613. *flags |= P54_HDR_FLAG_DATA_OUT_PROMISC;
  614. break;
  615. case NL80211_IFTYPE_STATION:
  616. *aid = 1;
  617. break;
  618. case NL80211_IFTYPE_AP:
  619. case NL80211_IFTYPE_ADHOC:
  620. case NL80211_IFTYPE_MESH_POINT:
  621. if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM) {
  622. *aid = 0;
  623. *queue = P54_QUEUE_CAB;
  624. return;
  625. }
  626. if (unlikely(ieee80211_is_mgmt(hdr->frame_control))) {
  627. if (ieee80211_is_probe_resp(hdr->frame_control)) {
  628. *aid = 0;
  629. *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP |
  630. P54_HDR_FLAG_DATA_OUT_NOCANCEL;
  631. return;
  632. } else if (ieee80211_is_beacon(hdr->frame_control)) {
  633. *aid = 0;
  634. if (info->flags & IEEE80211_TX_CTL_INJECTED) {
  635. /*
  636. * Injecting beacons on top of a AP is
  637. * not a good idea... nevertheless,
  638. * it should be doable.
  639. */
  640. return;
  641. }
  642. *flags |= P54_HDR_FLAG_DATA_OUT_TIMESTAMP;
  643. *queue = P54_QUEUE_BEACON;
  644. *extra_len = IEEE80211_MAX_TIM_LEN;
  645. return;
  646. }
  647. }
  648. if (sta)
  649. *aid = sta->aid;
  650. break;
  651. }
  652. }
  653. static u8 p54_convert_algo(u32 cipher)
  654. {
  655. switch (cipher) {
  656. case WLAN_CIPHER_SUITE_WEP40:
  657. case WLAN_CIPHER_SUITE_WEP104:
  658. return P54_CRYPTO_WEP;
  659. case WLAN_CIPHER_SUITE_TKIP:
  660. return P54_CRYPTO_TKIPMICHAEL;
  661. case WLAN_CIPHER_SUITE_CCMP:
  662. return P54_CRYPTO_AESCCMP;
  663. default:
  664. return 0;
  665. }
  666. }
  667. void p54_tx_80211(struct ieee80211_hw *dev,
  668. struct ieee80211_tx_control *control,
  669. struct sk_buff *skb)
  670. {
  671. struct p54_common *priv = dev->priv;
  672. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  673. struct p54_tx_info *p54info;
  674. struct p54_hdr *hdr;
  675. struct p54_tx_data *txhdr;
  676. unsigned int padding, len, extra_len = 0;
  677. int i, j, ridx;
  678. u16 hdr_flags = 0, aid = 0;
  679. u8 rate, queue = 0, crypt_offset = 0;
  680. u8 cts_rate = 0x20;
  681. u8 rc_flags;
  682. u8 calculated_tries[4];
  683. u8 nrates = 0, nremaining = 8;
  684. bool burst_allowed = false;
  685. p54_tx_80211_header(priv, skb, info, control->sta, &queue, &extra_len,
  686. &hdr_flags, &aid, &burst_allowed);
  687. if (p54_tx_qos_accounting_alloc(priv, skb, queue)) {
  688. ieee80211_free_txskb(dev, skb);
  689. return;
  690. }
  691. padding = (unsigned long)(skb->data - (sizeof(*hdr) + sizeof(*txhdr))) & 3;
  692. len = skb->len;
  693. if (info->control.hw_key) {
  694. crypt_offset = ieee80211_get_hdrlen_from_skb(skb);
  695. if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  696. u8 *iv = (u8 *)(skb->data + crypt_offset);
  697. /*
  698. * The firmware excepts that the IV has to have
  699. * this special format
  700. */
  701. iv[1] = iv[0];
  702. iv[0] = iv[2];
  703. iv[2] = 0;
  704. }
  705. }
  706. txhdr = (struct p54_tx_data *) skb_push(skb, sizeof(*txhdr) + padding);
  707. hdr = (struct p54_hdr *) skb_push(skb, sizeof(*hdr));
  708. if (padding)
  709. hdr_flags |= P54_HDR_FLAG_DATA_ALIGN;
  710. hdr->type = cpu_to_le16(aid);
  711. hdr->rts_tries = info->control.rates[0].count;
  712. /*
  713. * we register the rates in perfect order, and
  714. * RTS/CTS won't happen on 5 GHz
  715. */
  716. cts_rate = info->control.rts_cts_rate_idx;
  717. memset(&txhdr->rateset, 0, sizeof(txhdr->rateset));
  718. /* see how many rates got used */
  719. for (i = 0; i < dev->max_rates; i++) {
  720. if (info->control.rates[i].idx < 0)
  721. break;
  722. nrates++;
  723. }
  724. /* limit tries to 8/nrates per rate */
  725. for (i = 0; i < nrates; i++) {
  726. /*
  727. * The magic expression here is equivalent to 8/nrates for
  728. * all values that matter, but avoids division and jumps.
  729. * Note that nrates can only take the values 1 through 4.
  730. */
  731. calculated_tries[i] = min_t(int, ((15 >> nrates) | 1) + 1,
  732. info->control.rates[i].count);
  733. nremaining -= calculated_tries[i];
  734. }
  735. /* if there are tries left, distribute from back to front */
  736. for (i = nrates - 1; nremaining > 0 && i >= 0; i--) {
  737. int tmp = info->control.rates[i].count - calculated_tries[i];
  738. if (tmp <= 0)
  739. continue;
  740. /* RC requested more tries at this rate */
  741. tmp = min_t(int, tmp, nremaining);
  742. calculated_tries[i] += tmp;
  743. nremaining -= tmp;
  744. }
  745. ridx = 0;
  746. for (i = 0; i < nrates && ridx < 8; i++) {
  747. /* we register the rates in perfect order */
  748. rate = info->control.rates[i].idx;
  749. if (info->band == IEEE80211_BAND_5GHZ)
  750. rate += 4;
  751. /* store the count we actually calculated for TX status */
  752. info->control.rates[i].count = calculated_tries[i];
  753. rc_flags = info->control.rates[i].flags;
  754. if (rc_flags & IEEE80211_TX_RC_USE_SHORT_PREAMBLE) {
  755. rate |= 0x10;
  756. cts_rate |= 0x10;
  757. }
  758. if (rc_flags & IEEE80211_TX_RC_USE_RTS_CTS) {
  759. burst_allowed = false;
  760. rate |= 0x40;
  761. } else if (rc_flags & IEEE80211_TX_RC_USE_CTS_PROTECT) {
  762. rate |= 0x20;
  763. burst_allowed = false;
  764. }
  765. for (j = 0; j < calculated_tries[i] && ridx < 8; j++) {
  766. txhdr->rateset[ridx] = rate;
  767. ridx++;
  768. }
  769. }
  770. if (burst_allowed)
  771. hdr_flags |= P54_HDR_FLAG_DATA_OUT_BURST;
  772. /* TODO: enable bursting */
  773. hdr->flags = cpu_to_le16(hdr_flags);
  774. hdr->tries = ridx;
  775. txhdr->rts_rate_idx = 0;
  776. if (info->control.hw_key) {
  777. txhdr->key_type = p54_convert_algo(info->control.hw_key->cipher);
  778. txhdr->key_len = min((u8)16, info->control.hw_key->keylen);
  779. memcpy(txhdr->key, info->control.hw_key->key, txhdr->key_len);
  780. if (info->control.hw_key->cipher == WLAN_CIPHER_SUITE_TKIP) {
  781. /* reserve space for the MIC key */
  782. len += 8;
  783. memcpy(skb_put(skb, 8), &(info->control.hw_key->key
  784. [NL80211_TKIP_DATA_OFFSET_TX_MIC_KEY]), 8);
  785. }
  786. /* reserve some space for ICV */
  787. len += info->control.hw_key->icv_len;
  788. memset(skb_put(skb, info->control.hw_key->icv_len), 0,
  789. info->control.hw_key->icv_len);
  790. } else {
  791. txhdr->key_type = 0;
  792. txhdr->key_len = 0;
  793. }
  794. txhdr->crypt_offset = crypt_offset;
  795. txhdr->hw_queue = queue;
  796. txhdr->backlog = priv->tx_stats[queue].len - 1;
  797. memset(txhdr->durations, 0, sizeof(txhdr->durations));
  798. txhdr->tx_antenna = 2 & priv->tx_diversity_mask;
  799. if (priv->rxhw == 5) {
  800. txhdr->longbow.cts_rate = cts_rate;
  801. txhdr->longbow.output_power = cpu_to_le16(priv->output_power);
  802. } else {
  803. txhdr->normal.output_power = priv->output_power;
  804. txhdr->normal.cts_rate = cts_rate;
  805. }
  806. if (padding)
  807. txhdr->align[0] = padding;
  808. hdr->len = cpu_to_le16(len);
  809. /* modifies skb->cb and with it info, so must be last! */
  810. p54info = (void *) info->rate_driver_data;
  811. p54info->extra_len = extra_len;
  812. p54_tx(priv, skb);
  813. }