txrx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. * Copyright (c) 2005-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  15. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "core.h"
  18. #include "txrx.h"
  19. #include "htt.h"
  20. #include "mac.h"
  21. #include "debug.h"
  22. static void ath10k_report_offchan_tx(struct ath10k *ar, struct sk_buff *skb)
  23. {
  24. if (!ATH10K_SKB_CB(skb)->htt.is_offchan)
  25. return;
  26. /* If the original wait_for_completion() timed out before
  27. * {data,mgmt}_tx_completed() was called then we could complete
  28. * offchan_tx_completed for a different skb. Prevent this by using
  29. * offchan_tx_skb. */
  30. spin_lock_bh(&ar->data_lock);
  31. if (ar->offchan_tx_skb != skb) {
  32. ath10k_warn("completed old offchannel frame\n");
  33. goto out;
  34. }
  35. complete(&ar->offchan_tx_completed);
  36. ar->offchan_tx_skb = NULL; /* just for sanity */
  37. ath10k_dbg(ATH10K_DBG_HTT, "completed offchannel skb %p\n", skb);
  38. out:
  39. spin_unlock_bh(&ar->data_lock);
  40. }
  41. void ath10k_txrx_tx_unref(struct ath10k_htt *htt, struct sk_buff *txdesc)
  42. {
  43. struct device *dev = htt->ar->dev;
  44. struct ieee80211_tx_info *info;
  45. struct sk_buff *txfrag = ATH10K_SKB_CB(txdesc)->htt.txfrag;
  46. struct sk_buff *msdu = ATH10K_SKB_CB(txdesc)->htt.msdu;
  47. int ret;
  48. if (ATH10K_SKB_CB(txdesc)->htt.refcount == 0)
  49. return;
  50. ATH10K_SKB_CB(txdesc)->htt.refcount--;
  51. if (ATH10K_SKB_CB(txdesc)->htt.refcount > 0)
  52. return;
  53. if (txfrag) {
  54. ret = ath10k_skb_unmap(dev, txfrag);
  55. if (ret)
  56. ath10k_warn("txfrag unmap failed (%d)\n", ret);
  57. dev_kfree_skb_any(txfrag);
  58. }
  59. ret = ath10k_skb_unmap(dev, msdu);
  60. if (ret)
  61. ath10k_warn("data skb unmap failed (%d)\n", ret);
  62. ath10k_report_offchan_tx(htt->ar, msdu);
  63. info = IEEE80211_SKB_CB(msdu);
  64. if (ATH10K_SKB_CB(txdesc)->htt.discard) {
  65. ieee80211_free_txskb(htt->ar->hw, msdu);
  66. goto exit;
  67. }
  68. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  69. info->flags |= IEEE80211_TX_STAT_ACK;
  70. if (ATH10K_SKB_CB(txdesc)->htt.no_ack)
  71. info->flags &= ~IEEE80211_TX_STAT_ACK;
  72. ieee80211_tx_status(htt->ar->hw, msdu);
  73. /* we do not own the msdu anymore */
  74. exit:
  75. spin_lock_bh(&htt->tx_lock);
  76. htt->pending_tx[ATH10K_SKB_CB(txdesc)->htt.msdu_id] = NULL;
  77. ath10k_htt_tx_free_msdu_id(htt, ATH10K_SKB_CB(txdesc)->htt.msdu_id);
  78. __ath10k_htt_tx_dec_pending(htt);
  79. if (htt->num_pending_tx == 0)
  80. wake_up(&htt->empty_tx_wq);
  81. spin_unlock_bh(&htt->tx_lock);
  82. dev_kfree_skb_any(txdesc);
  83. }
  84. void ath10k_txrx_tx_completed(struct ath10k_htt *htt,
  85. const struct htt_tx_done *tx_done)
  86. {
  87. struct sk_buff *txdesc;
  88. ath10k_dbg(ATH10K_DBG_HTT, "htt tx completion msdu_id %u discard %d no_ack %d\n",
  89. tx_done->msdu_id, !!tx_done->discard, !!tx_done->no_ack);
  90. if (tx_done->msdu_id >= htt->max_num_pending_tx) {
  91. ath10k_warn("warning: msdu_id %d too big, ignoring\n",
  92. tx_done->msdu_id);
  93. return;
  94. }
  95. txdesc = htt->pending_tx[tx_done->msdu_id];
  96. ATH10K_SKB_CB(txdesc)->htt.discard = tx_done->discard;
  97. ATH10K_SKB_CB(txdesc)->htt.no_ack = tx_done->no_ack;
  98. ath10k_txrx_tx_unref(htt, txdesc);
  99. }
  100. static const u8 rx_legacy_rate_idx[] = {
  101. 3, /* 0x00 - 11Mbps */
  102. 2, /* 0x01 - 5.5Mbps */
  103. 1, /* 0x02 - 2Mbps */
  104. 0, /* 0x03 - 1Mbps */
  105. 3, /* 0x04 - 11Mbps */
  106. 2, /* 0x05 - 5.5Mbps */
  107. 1, /* 0x06 - 2Mbps */
  108. 0, /* 0x07 - 1Mbps */
  109. 10, /* 0x08 - 48Mbps */
  110. 8, /* 0x09 - 24Mbps */
  111. 6, /* 0x0A - 12Mbps */
  112. 4, /* 0x0B - 6Mbps */
  113. 11, /* 0x0C - 54Mbps */
  114. 9, /* 0x0D - 36Mbps */
  115. 7, /* 0x0E - 18Mbps */
  116. 5, /* 0x0F - 9Mbps */
  117. };
  118. static void process_rx_rates(struct ath10k *ar, struct htt_rx_info *info,
  119. enum ieee80211_band band,
  120. struct ieee80211_rx_status *status)
  121. {
  122. u8 cck, rate, rate_idx, bw, sgi, mcs, nss;
  123. u8 info0 = info->rate.info0;
  124. u32 info1 = info->rate.info1;
  125. u32 info2 = info->rate.info2;
  126. u8 preamble = 0;
  127. /* Check if valid fields */
  128. if (!(info0 & HTT_RX_INDICATION_INFO0_START_VALID))
  129. return;
  130. preamble = MS(info1, HTT_RX_INDICATION_INFO1_PREAMBLE_TYPE);
  131. switch (preamble) {
  132. case HTT_RX_LEGACY:
  133. cck = info0 & HTT_RX_INDICATION_INFO0_LEGACY_RATE_CCK;
  134. rate = MS(info0, HTT_RX_INDICATION_INFO0_LEGACY_RATE);
  135. rate_idx = 0;
  136. if (rate < 0x08 || rate > 0x0F)
  137. break;
  138. switch (band) {
  139. case IEEE80211_BAND_2GHZ:
  140. if (cck)
  141. rate &= ~BIT(3);
  142. rate_idx = rx_legacy_rate_idx[rate];
  143. break;
  144. case IEEE80211_BAND_5GHZ:
  145. rate_idx = rx_legacy_rate_idx[rate];
  146. /* We are using same rate table registering
  147. HW - ath10k_rates[]. In case of 5GHz skip
  148. CCK rates, so -4 here */
  149. rate_idx -= 4;
  150. break;
  151. default:
  152. break;
  153. }
  154. status->rate_idx = rate_idx;
  155. break;
  156. case HTT_RX_HT:
  157. case HTT_RX_HT_WITH_TXBF:
  158. /* HT-SIG - Table 20-11 in info1 and info2 */
  159. mcs = info1 & 0x1F;
  160. nss = mcs >> 3;
  161. bw = (info1 >> 7) & 1;
  162. sgi = (info2 >> 7) & 1;
  163. status->rate_idx = mcs;
  164. status->flag |= RX_FLAG_HT;
  165. if (sgi)
  166. status->flag |= RX_FLAG_SHORT_GI;
  167. if (bw)
  168. status->flag |= RX_FLAG_40MHZ;
  169. break;
  170. case HTT_RX_VHT:
  171. case HTT_RX_VHT_WITH_TXBF:
  172. /* VHT-SIG-A1 in info 1, VHT-SIG-A2 in info2
  173. TODO check this */
  174. mcs = (info2 >> 4) & 0x0F;
  175. nss = (info1 >> 10) & 0x07;
  176. bw = info1 & 3;
  177. sgi = info2 & 1;
  178. status->rate_idx = mcs;
  179. status->vht_nss = nss;
  180. if (sgi)
  181. status->flag |= RX_FLAG_SHORT_GI;
  182. switch (bw) {
  183. /* 20MHZ */
  184. case 0:
  185. break;
  186. /* 40MHZ */
  187. case 1:
  188. status->flag |= RX_FLAG_40MHZ;
  189. break;
  190. /* 80MHZ */
  191. case 2:
  192. status->flag |= RX_FLAG_80MHZ;
  193. }
  194. status->flag |= RX_FLAG_VHT;
  195. break;
  196. default:
  197. break;
  198. }
  199. }
  200. void ath10k_process_rx(struct ath10k *ar, struct htt_rx_info *info)
  201. {
  202. struct ieee80211_rx_status *status;
  203. struct ieee80211_channel *ch;
  204. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)info->skb->data;
  205. status = IEEE80211_SKB_RXCB(info->skb);
  206. memset(status, 0, sizeof(*status));
  207. if (info->encrypt_type != HTT_RX_MPDU_ENCRYPT_NONE) {
  208. status->flag |= RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED |
  209. RX_FLAG_MMIC_STRIPPED;
  210. hdr->frame_control = __cpu_to_le16(
  211. __le16_to_cpu(hdr->frame_control) &
  212. ~IEEE80211_FCTL_PROTECTED);
  213. }
  214. if (info->status == HTT_RX_IND_MPDU_STATUS_TKIP_MIC_ERR)
  215. status->flag |= RX_FLAG_MMIC_ERROR;
  216. if (info->fcs_err)
  217. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  218. status->signal = info->signal;
  219. spin_lock_bh(&ar->data_lock);
  220. ch = ar->scan_channel;
  221. if (!ch)
  222. ch = ar->rx_channel;
  223. spin_unlock_bh(&ar->data_lock);
  224. if (!ch) {
  225. ath10k_warn("no channel configured; ignoring frame!\n");
  226. dev_kfree_skb_any(info->skb);
  227. return;
  228. }
  229. process_rx_rates(ar, info, ch->band, status);
  230. status->band = ch->band;
  231. status->freq = ch->center_freq;
  232. ath10k_dbg(ATH10K_DBG_DATA,
  233. "rx skb %p len %u %s%s%s%s%s %srate_idx %u vht_nss %u freq %u band %u\n",
  234. info->skb,
  235. info->skb->len,
  236. status->flag == 0 ? "legacy" : "",
  237. status->flag & RX_FLAG_HT ? "ht" : "",
  238. status->flag & RX_FLAG_VHT ? "vht" : "",
  239. status->flag & RX_FLAG_40MHZ ? "40" : "",
  240. status->flag & RX_FLAG_80MHZ ? "80" : "",
  241. status->flag & RX_FLAG_SHORT_GI ? "sgi " : "",
  242. status->rate_idx,
  243. status->vht_nss,
  244. status->freq,
  245. status->band);
  246. ieee80211_rx(ar->hw, info->skb);
  247. }
  248. struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
  249. const u8 *addr)
  250. {
  251. struct ath10k_peer *peer;
  252. lockdep_assert_held(&ar->data_lock);
  253. list_for_each_entry(peer, &ar->peers, list) {
  254. if (peer->vdev_id != vdev_id)
  255. continue;
  256. if (memcmp(peer->addr, addr, ETH_ALEN))
  257. continue;
  258. return peer;
  259. }
  260. return NULL;
  261. }
  262. static struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar,
  263. int peer_id)
  264. {
  265. struct ath10k_peer *peer;
  266. lockdep_assert_held(&ar->data_lock);
  267. list_for_each_entry(peer, &ar->peers, list)
  268. if (test_bit(peer_id, peer->peer_ids))
  269. return peer;
  270. return NULL;
  271. }
  272. static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
  273. const u8 *addr, bool expect_mapped)
  274. {
  275. int ret;
  276. ret = wait_event_timeout(ar->peer_mapping_wq, ({
  277. bool mapped;
  278. spin_lock_bh(&ar->data_lock);
  279. mapped = !!ath10k_peer_find(ar, vdev_id, addr);
  280. spin_unlock_bh(&ar->data_lock);
  281. mapped == expect_mapped;
  282. }), 3*HZ);
  283. if (ret <= 0)
  284. return -ETIMEDOUT;
  285. return 0;
  286. }
  287. int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
  288. {
  289. return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
  290. }
  291. int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
  292. {
  293. return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
  294. }
  295. void ath10k_peer_map_event(struct ath10k_htt *htt,
  296. struct htt_peer_map_event *ev)
  297. {
  298. struct ath10k *ar = htt->ar;
  299. struct ath10k_peer *peer;
  300. spin_lock_bh(&ar->data_lock);
  301. peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
  302. if (!peer) {
  303. peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
  304. if (!peer)
  305. goto exit;
  306. peer->vdev_id = ev->vdev_id;
  307. memcpy(peer->addr, ev->addr, ETH_ALEN);
  308. list_add(&peer->list, &ar->peers);
  309. wake_up(&ar->peer_mapping_wq);
  310. }
  311. ath10k_dbg(ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
  312. ev->vdev_id, ev->addr, ev->peer_id);
  313. set_bit(ev->peer_id, peer->peer_ids);
  314. exit:
  315. spin_unlock_bh(&ar->data_lock);
  316. }
  317. void ath10k_peer_unmap_event(struct ath10k_htt *htt,
  318. struct htt_peer_unmap_event *ev)
  319. {
  320. struct ath10k *ar = htt->ar;
  321. struct ath10k_peer *peer;
  322. spin_lock_bh(&ar->data_lock);
  323. peer = ath10k_peer_find_by_id(ar, ev->peer_id);
  324. if (!peer) {
  325. ath10k_warn("unknown peer id %d\n", ev->peer_id);
  326. goto exit;
  327. }
  328. ath10k_dbg(ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
  329. peer->vdev_id, peer->addr, ev->peer_id);
  330. clear_bit(ev->peer_id, peer->peer_ids);
  331. if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
  332. list_del(&peer->list);
  333. kfree(peer);
  334. wake_up(&ar->peer_mapping_wq);
  335. }
  336. exit:
  337. spin_unlock_bh(&ar->data_lock);
  338. }