txrx.c 23 KB

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