txrx.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (c) 2013 Eugene Krasnikov <k.eugene.e@gmail.com>
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  11. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  13. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include "txrx.h"
  18. static inline int get_rssi0(struct wcn36xx_rx_bd *bd)
  19. {
  20. return 100 - ((bd->phy_stat0 >> 24) & 0xff);
  21. }
  22. int wcn36xx_rx_skb(struct wcn36xx *wcn, struct sk_buff *skb)
  23. {
  24. struct ieee80211_rx_status status;
  25. struct ieee80211_hdr *hdr;
  26. struct wcn36xx_rx_bd *bd;
  27. u16 fc, sn;
  28. /*
  29. * All fields must be 0, otherwise it can lead to
  30. * unexpected consequences.
  31. */
  32. memset(&status, 0, sizeof(status));
  33. bd = (struct wcn36xx_rx_bd *)skb->data;
  34. buff_to_be((u32 *)bd, sizeof(*bd)/sizeof(u32));
  35. wcn36xx_dbg_dump(WCN36XX_DBG_RX_DUMP,
  36. "BD <<< ", (char *)bd,
  37. sizeof(struct wcn36xx_rx_bd));
  38. skb_put(skb, bd->pdu.mpdu_header_off + bd->pdu.mpdu_len);
  39. skb_pull(skb, bd->pdu.mpdu_header_off);
  40. status.mactime = 10;
  41. status.freq = WCN36XX_CENTER_FREQ(wcn);
  42. status.band = WCN36XX_BAND(wcn);
  43. status.signal = -get_rssi0(bd);
  44. status.antenna = 1;
  45. status.rate_idx = 1;
  46. status.flag = 0;
  47. status.rx_flags = 0;
  48. status.flag |= RX_FLAG_IV_STRIPPED |
  49. RX_FLAG_MMIC_STRIPPED |
  50. RX_FLAG_DECRYPTED;
  51. wcn36xx_dbg(WCN36XX_DBG_RX, "status.flags=%llx\n", status.flag);
  52. memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
  53. hdr = (struct ieee80211_hdr *) skb->data;
  54. fc = __le16_to_cpu(hdr->frame_control);
  55. sn = IEEE80211_SEQ_TO_SN(__le16_to_cpu(hdr->seq_ctrl));
  56. if (ieee80211_is_beacon(hdr->frame_control)) {
  57. wcn36xx_dbg(WCN36XX_DBG_BEACON, "beacon skb %p len %d fc %04x sn %d\n",
  58. skb, skb->len, fc, sn);
  59. wcn36xx_dbg_dump(WCN36XX_DBG_BEACON_DUMP, "SKB <<< ",
  60. (char *)skb->data, skb->len);
  61. } else {
  62. wcn36xx_dbg(WCN36XX_DBG_RX, "rx skb %p len %d fc %04x sn %d\n",
  63. skb, skb->len, fc, sn);
  64. wcn36xx_dbg_dump(WCN36XX_DBG_RX_DUMP, "SKB <<< ",
  65. (char *)skb->data, skb->len);
  66. }
  67. ieee80211_rx_irqsafe(wcn->hw, skb);
  68. return 0;
  69. }
  70. static void wcn36xx_set_tx_pdu(struct wcn36xx_tx_bd *bd,
  71. u32 mpdu_header_len,
  72. u32 len,
  73. u16 tid)
  74. {
  75. bd->pdu.mpdu_header_len = mpdu_header_len;
  76. bd->pdu.mpdu_header_off = sizeof(*bd);
  77. bd->pdu.mpdu_data_off = bd->pdu.mpdu_header_len +
  78. bd->pdu.mpdu_header_off;
  79. bd->pdu.mpdu_len = len;
  80. bd->pdu.tid = tid;
  81. bd->pdu.bd_ssn = WCN36XX_TXBD_SSN_FILL_DPU_QOS;
  82. }
  83. static inline struct wcn36xx_vif *get_vif_by_addr(struct wcn36xx *wcn,
  84. u8 *addr)
  85. {
  86. struct wcn36xx_vif *vif_priv = NULL;
  87. struct ieee80211_vif *vif = NULL;
  88. list_for_each_entry(vif_priv, &wcn->vif_list, list) {
  89. vif = container_of((void *)vif_priv,
  90. struct ieee80211_vif,
  91. drv_priv);
  92. if (memcmp(vif->addr, addr, ETH_ALEN) == 0)
  93. return vif_priv;
  94. }
  95. wcn36xx_warn("vif %pM not found\n", addr);
  96. return NULL;
  97. }
  98. static void wcn36xx_tx_start_ampdu(struct wcn36xx *wcn,
  99. struct wcn36xx_sta *sta_priv,
  100. struct sk_buff *skb)
  101. {
  102. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  103. struct ieee80211_sta *sta;
  104. u8 *qc, tid;
  105. if (!conf_is_ht(&wcn->hw->conf))
  106. return;
  107. sta = wcn36xx_priv_to_sta(sta_priv);
  108. if (WARN_ON(!ieee80211_is_data_qos(hdr->frame_control)))
  109. return;
  110. if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
  111. return;
  112. qc = ieee80211_get_qos_ctl(hdr);
  113. tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
  114. spin_lock(&sta_priv->ampdu_lock);
  115. if (sta_priv->ampdu_state[tid] != WCN36XX_AMPDU_NONE)
  116. goto out_unlock;
  117. if (sta_priv->non_agg_frame_ct++ >= WCN36XX_AMPDU_START_THRESH) {
  118. sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_START;
  119. sta_priv->non_agg_frame_ct = 0;
  120. ieee80211_start_tx_ba_session(sta, tid, 0);
  121. }
  122. out_unlock:
  123. spin_unlock(&sta_priv->ampdu_lock);
  124. }
  125. static void wcn36xx_set_tx_data(struct wcn36xx_tx_bd *bd,
  126. struct wcn36xx *wcn,
  127. struct wcn36xx_vif **vif_priv,
  128. struct wcn36xx_sta *sta_priv,
  129. struct sk_buff *skb,
  130. bool bcast)
  131. {
  132. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  133. struct ieee80211_vif *vif = NULL;
  134. struct wcn36xx_vif *__vif_priv = NULL;
  135. bool is_data_qos;
  136. bd->bd_rate = WCN36XX_BD_RATE_DATA;
  137. /*
  138. * For not unicast frames mac80211 will not set sta pointer so use
  139. * self_sta_index instead.
  140. */
  141. if (sta_priv) {
  142. __vif_priv = sta_priv->vif;
  143. vif = container_of((void *)__vif_priv,
  144. struct ieee80211_vif,
  145. drv_priv);
  146. bd->dpu_sign = sta_priv->ucast_dpu_sign;
  147. if (vif->type == NL80211_IFTYPE_STATION) {
  148. bd->sta_index = sta_priv->bss_sta_index;
  149. bd->dpu_desc_idx = sta_priv->bss_dpu_desc_index;
  150. } else if (vif->type == NL80211_IFTYPE_AP ||
  151. vif->type == NL80211_IFTYPE_ADHOC ||
  152. vif->type == NL80211_IFTYPE_MESH_POINT) {
  153. bd->sta_index = sta_priv->sta_index;
  154. bd->dpu_desc_idx = sta_priv->dpu_desc_index;
  155. }
  156. } else {
  157. __vif_priv = get_vif_by_addr(wcn, hdr->addr2);
  158. bd->sta_index = __vif_priv->self_sta_index;
  159. bd->dpu_desc_idx = __vif_priv->self_dpu_desc_index;
  160. bd->dpu_sign = __vif_priv->self_ucast_dpu_sign;
  161. }
  162. if (ieee80211_is_nullfunc(hdr->frame_control) ||
  163. (sta_priv && !sta_priv->is_data_encrypted))
  164. bd->dpu_ne = 1;
  165. if (bcast) {
  166. bd->ub = 1;
  167. bd->ack_policy = 1;
  168. }
  169. *vif_priv = __vif_priv;
  170. is_data_qos = ieee80211_is_data_qos(hdr->frame_control);
  171. wcn36xx_set_tx_pdu(bd,
  172. is_data_qos ?
  173. sizeof(struct ieee80211_qos_hdr) :
  174. sizeof(struct ieee80211_hdr_3addr),
  175. skb->len, sta_priv ? sta_priv->tid : 0);
  176. if (sta_priv && is_data_qos)
  177. wcn36xx_tx_start_ampdu(wcn, sta_priv, skb);
  178. }
  179. static void wcn36xx_set_tx_mgmt(struct wcn36xx_tx_bd *bd,
  180. struct wcn36xx *wcn,
  181. struct wcn36xx_vif **vif_priv,
  182. struct sk_buff *skb,
  183. bool bcast)
  184. {
  185. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  186. struct wcn36xx_vif *__vif_priv =
  187. get_vif_by_addr(wcn, hdr->addr2);
  188. bd->sta_index = __vif_priv->self_sta_index;
  189. bd->dpu_desc_idx = __vif_priv->self_dpu_desc_index;
  190. bd->dpu_ne = 1;
  191. /* default rate for unicast */
  192. if (ieee80211_is_mgmt(hdr->frame_control))
  193. bd->bd_rate = (WCN36XX_BAND(wcn) == IEEE80211_BAND_5GHZ) ?
  194. WCN36XX_BD_RATE_CTRL :
  195. WCN36XX_BD_RATE_MGMT;
  196. else if (ieee80211_is_ctl(hdr->frame_control))
  197. bd->bd_rate = WCN36XX_BD_RATE_CTRL;
  198. else
  199. wcn36xx_warn("frame control type unknown\n");
  200. /*
  201. * In joining state trick hardware that probe is sent as
  202. * unicast even if address is broadcast.
  203. */
  204. if (__vif_priv->is_joining &&
  205. ieee80211_is_probe_req(hdr->frame_control))
  206. bcast = false;
  207. if (bcast) {
  208. /* broadcast */
  209. bd->ub = 1;
  210. /* No ack needed not unicast */
  211. bd->ack_policy = 1;
  212. bd->queue_id = WCN36XX_TX_B_WQ_ID;
  213. } else
  214. bd->queue_id = WCN36XX_TX_U_WQ_ID;
  215. *vif_priv = __vif_priv;
  216. wcn36xx_set_tx_pdu(bd,
  217. ieee80211_is_data_qos(hdr->frame_control) ?
  218. sizeof(struct ieee80211_qos_hdr) :
  219. sizeof(struct ieee80211_hdr_3addr),
  220. skb->len, WCN36XX_TID);
  221. }
  222. int wcn36xx_start_tx(struct wcn36xx *wcn,
  223. struct wcn36xx_sta *sta_priv,
  224. struct sk_buff *skb)
  225. {
  226. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  227. struct wcn36xx_vif *vif_priv = NULL;
  228. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  229. unsigned long flags;
  230. bool is_low = ieee80211_is_data(hdr->frame_control);
  231. bool bcast = is_broadcast_ether_addr(hdr->addr1) ||
  232. is_multicast_ether_addr(hdr->addr1);
  233. struct wcn36xx_tx_bd *bd = wcn36xx_dxe_get_next_bd(wcn, is_low);
  234. if (!bd) {
  235. /*
  236. * TX DXE are used in pairs. One for the BD and one for the
  237. * actual frame. The BD DXE's has a preallocated buffer while
  238. * the skb ones does not. If this isn't true something is really
  239. * wierd. TODO: Recover from this situation
  240. */
  241. wcn36xx_err("bd address may not be NULL for BD DXE\n");
  242. return -EINVAL;
  243. }
  244. memset(bd, 0, sizeof(*bd));
  245. wcn36xx_dbg(WCN36XX_DBG_TX,
  246. "tx skb %p len %d fc %04x sn %d %s %s\n",
  247. skb, skb->len, __le16_to_cpu(hdr->frame_control),
  248. IEEE80211_SEQ_TO_SN(__le16_to_cpu(hdr->seq_ctrl)),
  249. is_low ? "low" : "high", bcast ? "bcast" : "ucast");
  250. wcn36xx_dbg_dump(WCN36XX_DBG_TX_DUMP, "", skb->data, skb->len);
  251. bd->dpu_rf = WCN36XX_BMU_WQ_TX;
  252. bd->tx_comp = !!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS);
  253. if (bd->tx_comp) {
  254. wcn36xx_dbg(WCN36XX_DBG_DXE, "TX_ACK status requested\n");
  255. spin_lock_irqsave(&wcn->dxe_lock, flags);
  256. if (wcn->tx_ack_skb) {
  257. spin_unlock_irqrestore(&wcn->dxe_lock, flags);
  258. wcn36xx_warn("tx_ack_skb already set\n");
  259. return -EINVAL;
  260. }
  261. wcn->tx_ack_skb = skb;
  262. spin_unlock_irqrestore(&wcn->dxe_lock, flags);
  263. /* Only one at a time is supported by fw. Stop the TX queues
  264. * until the ack status gets back.
  265. *
  266. * TODO: Add watchdog in case FW does not answer
  267. */
  268. ieee80211_stop_queues(wcn->hw);
  269. }
  270. /* Data frames served first*/
  271. if (is_low)
  272. wcn36xx_set_tx_data(bd, wcn, &vif_priv, sta_priv, skb, bcast);
  273. else
  274. /* MGMT and CTRL frames are handeld here*/
  275. wcn36xx_set_tx_mgmt(bd, wcn, &vif_priv, skb, bcast);
  276. buff_to_be((u32 *)bd, sizeof(*bd)/sizeof(u32));
  277. bd->tx_bd_sign = 0xbdbdbdbd;
  278. return wcn36xx_dxe_tx_frame(wcn, vif_priv, skb, is_low);
  279. }