txrx.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. memset(&info->status, 0, sizeof(info->status));
  65. if (ATH10K_SKB_CB(txdesc)->htt.discard) {
  66. ieee80211_free_txskb(htt->ar->hw, msdu);
  67. goto exit;
  68. }
  69. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
  70. info->flags |= IEEE80211_TX_STAT_ACK;
  71. if (ATH10K_SKB_CB(txdesc)->htt.no_ack)
  72. info->flags &= ~IEEE80211_TX_STAT_ACK;
  73. ieee80211_tx_status(htt->ar->hw, msdu);
  74. /* we do not own the msdu anymore */
  75. exit:
  76. spin_lock_bh(&htt->tx_lock);
  77. htt->pending_tx[ATH10K_SKB_CB(txdesc)->htt.msdu_id] = NULL;
  78. ath10k_htt_tx_free_msdu_id(htt, ATH10K_SKB_CB(txdesc)->htt.msdu_id);
  79. __ath10k_htt_tx_dec_pending(htt);
  80. if (bitmap_empty(htt->used_msdu_ids, htt->max_num_pending_tx))
  81. wake_up(&htt->empty_tx_wq);
  82. spin_unlock_bh(&htt->tx_lock);
  83. dev_kfree_skb_any(txdesc);
  84. }
  85. void ath10k_txrx_tx_completed(struct ath10k_htt *htt,
  86. const struct htt_tx_done *tx_done)
  87. {
  88. struct sk_buff *txdesc;
  89. ath10k_dbg(ATH10K_DBG_HTT, "htt tx completion msdu_id %u discard %d no_ack %d\n",
  90. tx_done->msdu_id, !!tx_done->discard, !!tx_done->no_ack);
  91. if (tx_done->msdu_id >= htt->max_num_pending_tx) {
  92. ath10k_warn("warning: msdu_id %d too big, ignoring\n",
  93. tx_done->msdu_id);
  94. return;
  95. }
  96. txdesc = htt->pending_tx[tx_done->msdu_id];
  97. ATH10K_SKB_CB(txdesc)->htt.discard = tx_done->discard;
  98. ATH10K_SKB_CB(txdesc)->htt.no_ack = tx_done->no_ack;
  99. ath10k_txrx_tx_unref(htt, txdesc);
  100. }
  101. static const u8 rx_legacy_rate_idx[] = {
  102. 3, /* 0x00 - 11Mbps */
  103. 2, /* 0x01 - 5.5Mbps */
  104. 1, /* 0x02 - 2Mbps */
  105. 0, /* 0x03 - 1Mbps */
  106. 3, /* 0x04 - 11Mbps */
  107. 2, /* 0x05 - 5.5Mbps */
  108. 1, /* 0x06 - 2Mbps */
  109. 0, /* 0x07 - 1Mbps */
  110. 10, /* 0x08 - 48Mbps */
  111. 8, /* 0x09 - 24Mbps */
  112. 6, /* 0x0A - 12Mbps */
  113. 4, /* 0x0B - 6Mbps */
  114. 11, /* 0x0C - 54Mbps */
  115. 9, /* 0x0D - 36Mbps */
  116. 7, /* 0x0E - 18Mbps */
  117. 5, /* 0x0F - 9Mbps */
  118. };
  119. static void process_rx_rates(struct ath10k *ar, struct htt_rx_info *info,
  120. enum ieee80211_band band,
  121. struct ieee80211_rx_status *status)
  122. {
  123. u8 cck, rate, rate_idx, bw, sgi, mcs, nss;
  124. u8 info0 = info->rate.info0;
  125. u32 info1 = info->rate.info1;
  126. u32 info2 = info->rate.info2;
  127. u8 preamble = 0;
  128. /* Check if valid fields */
  129. if (!(info0 & HTT_RX_INDICATION_INFO0_START_VALID))
  130. return;
  131. preamble = MS(info1, HTT_RX_INDICATION_INFO1_PREAMBLE_TYPE);
  132. switch (preamble) {
  133. case HTT_RX_LEGACY:
  134. cck = info0 & HTT_RX_INDICATION_INFO0_LEGACY_RATE_CCK;
  135. rate = MS(info0, HTT_RX_INDICATION_INFO0_LEGACY_RATE);
  136. rate_idx = 0;
  137. if (rate < 0x08 || rate > 0x0F)
  138. break;
  139. switch (band) {
  140. case IEEE80211_BAND_2GHZ:
  141. if (cck)
  142. rate &= ~BIT(3);
  143. rate_idx = rx_legacy_rate_idx[rate];
  144. break;
  145. case IEEE80211_BAND_5GHZ:
  146. rate_idx = rx_legacy_rate_idx[rate];
  147. /* We are using same rate table registering
  148. HW - ath10k_rates[]. In case of 5GHz skip
  149. CCK rates, so -4 here */
  150. rate_idx -= 4;
  151. break;
  152. default:
  153. break;
  154. }
  155. status->rate_idx = rate_idx;
  156. break;
  157. case HTT_RX_HT:
  158. case HTT_RX_HT_WITH_TXBF:
  159. /* HT-SIG - Table 20-11 in info1 and info2 */
  160. mcs = info1 & 0x1F;
  161. nss = mcs >> 3;
  162. bw = (info1 >> 7) & 1;
  163. sgi = (info2 >> 7) & 1;
  164. status->rate_idx = mcs;
  165. status->flag |= RX_FLAG_HT;
  166. if (sgi)
  167. status->flag |= RX_FLAG_SHORT_GI;
  168. if (bw)
  169. status->flag |= RX_FLAG_40MHZ;
  170. break;
  171. case HTT_RX_VHT:
  172. case HTT_RX_VHT_WITH_TXBF:
  173. /* VHT-SIG-A1 in info 1, VHT-SIG-A2 in info2
  174. TODO check this */
  175. mcs = (info2 >> 4) & 0x0F;
  176. nss = (info1 >> 10) & 0x07;
  177. bw = info1 & 3;
  178. sgi = info2 & 1;
  179. status->rate_idx = mcs;
  180. status->vht_nss = nss;
  181. if (sgi)
  182. status->flag |= RX_FLAG_SHORT_GI;
  183. switch (bw) {
  184. /* 20MHZ */
  185. case 0:
  186. break;
  187. /* 40MHZ */
  188. case 1:
  189. status->flag |= RX_FLAG_40MHZ;
  190. break;
  191. /* 80MHZ */
  192. case 2:
  193. status->flag |= RX_FLAG_80MHZ;
  194. }
  195. status->flag |= RX_FLAG_VHT;
  196. break;
  197. default:
  198. break;
  199. }
  200. }
  201. void ath10k_process_rx(struct ath10k *ar, struct htt_rx_info *info)
  202. {
  203. struct ieee80211_rx_status *status;
  204. struct ieee80211_channel *ch;
  205. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)info->skb->data;
  206. status = IEEE80211_SKB_RXCB(info->skb);
  207. memset(status, 0, sizeof(*status));
  208. if (info->encrypt_type != HTT_RX_MPDU_ENCRYPT_NONE) {
  209. status->flag |= RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED |
  210. RX_FLAG_MMIC_STRIPPED;
  211. hdr->frame_control = __cpu_to_le16(
  212. __le16_to_cpu(hdr->frame_control) &
  213. ~IEEE80211_FCTL_PROTECTED);
  214. }
  215. if (info->status == HTT_RX_IND_MPDU_STATUS_TKIP_MIC_ERR)
  216. status->flag |= RX_FLAG_MMIC_ERROR;
  217. if (info->fcs_err)
  218. status->flag |= RX_FLAG_FAILED_FCS_CRC;
  219. status->signal = info->signal;
  220. spin_lock_bh(&ar->data_lock);
  221. ch = ar->scan_channel;
  222. if (!ch)
  223. ch = ar->rx_channel;
  224. spin_unlock_bh(&ar->data_lock);
  225. if (!ch) {
  226. ath10k_warn("no channel configured; ignoring frame!\n");
  227. dev_kfree_skb_any(info->skb);
  228. return;
  229. }
  230. process_rx_rates(ar, info, ch->band, status);
  231. status->band = ch->band;
  232. status->freq = ch->center_freq;
  233. ath10k_dbg(ATH10K_DBG_DATA,
  234. "rx skb %p len %u %s%s%s%s%s %srate_idx %u vht_nss %u freq %u band %u\n",
  235. info->skb,
  236. info->skb->len,
  237. status->flag == 0 ? "legacy" : "",
  238. status->flag & RX_FLAG_HT ? "ht" : "",
  239. status->flag & RX_FLAG_VHT ? "vht" : "",
  240. status->flag & RX_FLAG_40MHZ ? "40" : "",
  241. status->flag & RX_FLAG_80MHZ ? "80" : "",
  242. status->flag & RX_FLAG_SHORT_GI ? "sgi " : "",
  243. status->rate_idx,
  244. status->vht_nss,
  245. status->freq,
  246. status->band);
  247. ieee80211_rx(ar->hw, info->skb);
  248. }
  249. struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
  250. const u8 *addr)
  251. {
  252. struct ath10k_peer *peer;
  253. lockdep_assert_held(&ar->data_lock);
  254. list_for_each_entry(peer, &ar->peers, list) {
  255. if (peer->vdev_id != vdev_id)
  256. continue;
  257. if (memcmp(peer->addr, addr, ETH_ALEN))
  258. continue;
  259. return peer;
  260. }
  261. return NULL;
  262. }
  263. static struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar,
  264. int peer_id)
  265. {
  266. struct ath10k_peer *peer;
  267. lockdep_assert_held(&ar->data_lock);
  268. list_for_each_entry(peer, &ar->peers, list)
  269. if (test_bit(peer_id, peer->peer_ids))
  270. return peer;
  271. return NULL;
  272. }
  273. static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
  274. const u8 *addr, bool expect_mapped)
  275. {
  276. int ret;
  277. ret = wait_event_timeout(ar->peer_mapping_wq, ({
  278. bool mapped;
  279. spin_lock_bh(&ar->data_lock);
  280. mapped = !!ath10k_peer_find(ar, vdev_id, addr);
  281. spin_unlock_bh(&ar->data_lock);
  282. mapped == expect_mapped;
  283. }), 3*HZ);
  284. if (ret <= 0)
  285. return -ETIMEDOUT;
  286. return 0;
  287. }
  288. int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
  289. {
  290. return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
  291. }
  292. int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
  293. {
  294. return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
  295. }
  296. void ath10k_peer_map_event(struct ath10k_htt *htt,
  297. struct htt_peer_map_event *ev)
  298. {
  299. struct ath10k *ar = htt->ar;
  300. struct ath10k_peer *peer;
  301. spin_lock_bh(&ar->data_lock);
  302. peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
  303. if (!peer) {
  304. peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
  305. if (!peer)
  306. goto exit;
  307. peer->vdev_id = ev->vdev_id;
  308. memcpy(peer->addr, ev->addr, ETH_ALEN);
  309. list_add(&peer->list, &ar->peers);
  310. wake_up(&ar->peer_mapping_wq);
  311. }
  312. ath10k_dbg(ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
  313. ev->vdev_id, ev->addr, ev->peer_id);
  314. set_bit(ev->peer_id, peer->peer_ids);
  315. exit:
  316. spin_unlock_bh(&ar->data_lock);
  317. }
  318. void ath10k_peer_unmap_event(struct ath10k_htt *htt,
  319. struct htt_peer_unmap_event *ev)
  320. {
  321. struct ath10k *ar = htt->ar;
  322. struct ath10k_peer *peer;
  323. spin_lock_bh(&ar->data_lock);
  324. peer = ath10k_peer_find_by_id(ar, ev->peer_id);
  325. if (!peer) {
  326. ath10k_warn("unknown peer id %d\n", ev->peer_id);
  327. goto exit;
  328. }
  329. ath10k_dbg(ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
  330. peer->vdev_id, peer->addr, ev->peer_id);
  331. clear_bit(ev->peer_id, peer->peer_ids);
  332. if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
  333. list_del(&peer->list);
  334. kfree(peer);
  335. wake_up(&ar->peer_mapping_wq);
  336. }
  337. exit:
  338. spin_unlock_bh(&ar->data_lock);
  339. }