txrx.c 9.6 KB

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