txrx.c 25 KB

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