txrx.c 9.8 KB

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