xref: /linux/drivers/net/wireless/ath/wcn36xx/txrx.c (revision 232ddcd80fa5c878efd631c6562cbaa24535bdc1)
18e84c258SEugene Krasnikov /*
28e84c258SEugene Krasnikov  * Copyright (c) 2013 Eugene Krasnikov <k.eugene.e@gmail.com>
38e84c258SEugene Krasnikov  *
48e84c258SEugene Krasnikov  * Permission to use, copy, modify, and/or distribute this software for any
58e84c258SEugene Krasnikov  * purpose with or without fee is hereby granted, provided that the above
68e84c258SEugene Krasnikov  * copyright notice and this permission notice appear in all copies.
78e84c258SEugene Krasnikov  *
88e84c258SEugene Krasnikov  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
98e84c258SEugene Krasnikov  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
108e84c258SEugene Krasnikov  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
118e84c258SEugene Krasnikov  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
128e84c258SEugene Krasnikov  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
138e84c258SEugene Krasnikov  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
148e84c258SEugene Krasnikov  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
158e84c258SEugene Krasnikov  */
168e84c258SEugene Krasnikov 
178e84c258SEugene Krasnikov #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
188e84c258SEugene Krasnikov 
198e84c258SEugene Krasnikov #include "txrx.h"
208e84c258SEugene Krasnikov 
218e84c258SEugene Krasnikov static inline int get_rssi0(struct wcn36xx_rx_bd *bd)
228e84c258SEugene Krasnikov {
238e84c258SEugene Krasnikov 	return 100 - ((bd->phy_stat0 >> 24) & 0xff);
248e84c258SEugene Krasnikov }
258e84c258SEugene Krasnikov 
268e84c258SEugene Krasnikov int wcn36xx_rx_skb(struct wcn36xx *wcn, struct sk_buff *skb)
278e84c258SEugene Krasnikov {
288e84c258SEugene Krasnikov 	struct ieee80211_rx_status status;
298e84c258SEugene Krasnikov 	struct ieee80211_hdr *hdr;
308e84c258SEugene Krasnikov 	struct wcn36xx_rx_bd *bd;
318e84c258SEugene Krasnikov 	u16 fc, sn;
328e84c258SEugene Krasnikov 
338e84c258SEugene Krasnikov 	/*
348e84c258SEugene Krasnikov 	 * All fields must be 0, otherwise it can lead to
358e84c258SEugene Krasnikov 	 * unexpected consequences.
368e84c258SEugene Krasnikov 	 */
378e84c258SEugene Krasnikov 	memset(&status, 0, sizeof(status));
388e84c258SEugene Krasnikov 
398e84c258SEugene Krasnikov 	bd = (struct wcn36xx_rx_bd *)skb->data;
408e84c258SEugene Krasnikov 	buff_to_be((u32 *)bd, sizeof(*bd)/sizeof(u32));
418e84c258SEugene Krasnikov 	wcn36xx_dbg_dump(WCN36XX_DBG_RX_DUMP,
428e84c258SEugene Krasnikov 			 "BD   <<< ", (char *)bd,
438e84c258SEugene Krasnikov 			 sizeof(struct wcn36xx_rx_bd));
448e84c258SEugene Krasnikov 
458e84c258SEugene Krasnikov 	skb_put(skb, bd->pdu.mpdu_header_off + bd->pdu.mpdu_len);
468e84c258SEugene Krasnikov 	skb_pull(skb, bd->pdu.mpdu_header_off);
478e84c258SEugene Krasnikov 
488e84c258SEugene Krasnikov 	status.mactime = 10;
498e84c258SEugene Krasnikov 	status.freq = WCN36XX_CENTER_FREQ(wcn);
508e84c258SEugene Krasnikov 	status.band = WCN36XX_BAND(wcn);
518e84c258SEugene Krasnikov 	status.signal = -get_rssi0(bd);
528e84c258SEugene Krasnikov 	status.antenna = 1;
538e84c258SEugene Krasnikov 	status.rate_idx = 1;
548e84c258SEugene Krasnikov 	status.flag = 0;
558e84c258SEugene Krasnikov 	status.rx_flags = 0;
568e84c258SEugene Krasnikov 	status.flag |= RX_FLAG_IV_STRIPPED |
578e84c258SEugene Krasnikov 		       RX_FLAG_MMIC_STRIPPED |
588e84c258SEugene Krasnikov 		       RX_FLAG_DECRYPTED;
598e84c258SEugene Krasnikov 
600059b2b1SEmmanuel Grumbach 	wcn36xx_dbg(WCN36XX_DBG_RX, "status.flags=%x\n", status.flag);
618e84c258SEugene Krasnikov 
628e84c258SEugene Krasnikov 	memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
638e84c258SEugene Krasnikov 
648e84c258SEugene Krasnikov 	hdr = (struct ieee80211_hdr *) skb->data;
658e84c258SEugene Krasnikov 	fc = __le16_to_cpu(hdr->frame_control);
668e84c258SEugene Krasnikov 	sn = IEEE80211_SEQ_TO_SN(__le16_to_cpu(hdr->seq_ctrl));
678e84c258SEugene Krasnikov 
688e84c258SEugene Krasnikov 	if (ieee80211_is_beacon(hdr->frame_control)) {
698e84c258SEugene Krasnikov 		wcn36xx_dbg(WCN36XX_DBG_BEACON, "beacon skb %p len %d fc %04x sn %d\n",
708e84c258SEugene Krasnikov 			    skb, skb->len, fc, sn);
718e84c258SEugene Krasnikov 		wcn36xx_dbg_dump(WCN36XX_DBG_BEACON_DUMP, "SKB <<< ",
728e84c258SEugene Krasnikov 				 (char *)skb->data, skb->len);
738e84c258SEugene Krasnikov 	} else {
748e84c258SEugene Krasnikov 		wcn36xx_dbg(WCN36XX_DBG_RX, "rx skb %p len %d fc %04x sn %d\n",
758e84c258SEugene Krasnikov 			    skb, skb->len, fc, sn);
768e84c258SEugene Krasnikov 		wcn36xx_dbg_dump(WCN36XX_DBG_RX_DUMP, "SKB <<< ",
778e84c258SEugene Krasnikov 				 (char *)skb->data, skb->len);
788e84c258SEugene Krasnikov 	}
798e84c258SEugene Krasnikov 
808e84c258SEugene Krasnikov 	ieee80211_rx_irqsafe(wcn->hw, skb);
818e84c258SEugene Krasnikov 
828e84c258SEugene Krasnikov 	return 0;
838e84c258SEugene Krasnikov }
848e84c258SEugene Krasnikov 
858e84c258SEugene Krasnikov static void wcn36xx_set_tx_pdu(struct wcn36xx_tx_bd *bd,
868e84c258SEugene Krasnikov 			       u32 mpdu_header_len,
878e84c258SEugene Krasnikov 			       u32 len,
888e84c258SEugene Krasnikov 			       u16 tid)
898e84c258SEugene Krasnikov {
908e84c258SEugene Krasnikov 	bd->pdu.mpdu_header_len = mpdu_header_len;
918e84c258SEugene Krasnikov 	bd->pdu.mpdu_header_off = sizeof(*bd);
928e84c258SEugene Krasnikov 	bd->pdu.mpdu_data_off = bd->pdu.mpdu_header_len +
938e84c258SEugene Krasnikov 		bd->pdu.mpdu_header_off;
948e84c258SEugene Krasnikov 	bd->pdu.mpdu_len = len;
958e84c258SEugene Krasnikov 	bd->pdu.tid = tid;
96*232ddcd8SBob Copeland 	bd->pdu.bd_ssn = WCN36XX_TXBD_SSN_FILL_DPU_QOS;
978e84c258SEugene Krasnikov }
988e84c258SEugene Krasnikov 
998e84c258SEugene Krasnikov static inline struct wcn36xx_vif *get_vif_by_addr(struct wcn36xx *wcn,
1008e84c258SEugene Krasnikov 						  u8 *addr)
1018e84c258SEugene Krasnikov {
1028e84c258SEugene Krasnikov 	struct wcn36xx_vif *vif_priv = NULL;
1038e84c258SEugene Krasnikov 	struct ieee80211_vif *vif = NULL;
1048e84c258SEugene Krasnikov 	list_for_each_entry(vif_priv, &wcn->vif_list, list) {
1058e84c258SEugene Krasnikov 			vif = container_of((void *)vif_priv,
1068e84c258SEugene Krasnikov 				   struct ieee80211_vif,
1078e84c258SEugene Krasnikov 				   drv_priv);
1088e84c258SEugene Krasnikov 			if (memcmp(vif->addr, addr, ETH_ALEN) == 0)
1098e84c258SEugene Krasnikov 				return vif_priv;
1108e84c258SEugene Krasnikov 	}
1118e84c258SEugene Krasnikov 	wcn36xx_warn("vif %pM not found\n", addr);
1128e84c258SEugene Krasnikov 	return NULL;
1138e84c258SEugene Krasnikov }
1148e84c258SEugene Krasnikov static void wcn36xx_set_tx_data(struct wcn36xx_tx_bd *bd,
1158e84c258SEugene Krasnikov 				struct wcn36xx *wcn,
1168e84c258SEugene Krasnikov 				struct wcn36xx_vif **vif_priv,
1178e84c258SEugene Krasnikov 				struct wcn36xx_sta *sta_priv,
1188e84c258SEugene Krasnikov 				struct ieee80211_hdr *hdr,
1198e84c258SEugene Krasnikov 				bool bcast)
1208e84c258SEugene Krasnikov {
1218e84c258SEugene Krasnikov 	struct ieee80211_vif *vif = NULL;
1228e84c258SEugene Krasnikov 	struct wcn36xx_vif *__vif_priv = NULL;
1238e84c258SEugene Krasnikov 	bd->bd_rate = WCN36XX_BD_RATE_DATA;
1248e84c258SEugene Krasnikov 
1258e84c258SEugene Krasnikov 	/*
1268e84c258SEugene Krasnikov 	 * For not unicast frames mac80211 will not set sta pointer so use
1278e84c258SEugene Krasnikov 	 * self_sta_index instead.
1288e84c258SEugene Krasnikov 	 */
1298e84c258SEugene Krasnikov 	if (sta_priv) {
1308e84c258SEugene Krasnikov 		__vif_priv = sta_priv->vif;
1318e84c258SEugene Krasnikov 		vif = container_of((void *)__vif_priv,
1328e84c258SEugene Krasnikov 				   struct ieee80211_vif,
1338e84c258SEugene Krasnikov 				   drv_priv);
1348e84c258SEugene Krasnikov 
13582cad2a0SPontus Fuchs 		bd->dpu_sign = sta_priv->ucast_dpu_sign;
1368e84c258SEugene Krasnikov 		if (vif->type == NL80211_IFTYPE_STATION) {
1378e84c258SEugene Krasnikov 			bd->sta_index = sta_priv->bss_sta_index;
1388e84c258SEugene Krasnikov 			bd->dpu_desc_idx = sta_priv->bss_dpu_desc_index;
1398e84c258SEugene Krasnikov 		} else if (vif->type == NL80211_IFTYPE_AP ||
1408e84c258SEugene Krasnikov 			   vif->type == NL80211_IFTYPE_ADHOC ||
1418e84c258SEugene Krasnikov 			   vif->type == NL80211_IFTYPE_MESH_POINT) {
1428e84c258SEugene Krasnikov 			bd->sta_index = sta_priv->sta_index;
1438e84c258SEugene Krasnikov 			bd->dpu_desc_idx = sta_priv->dpu_desc_index;
1448e84c258SEugene Krasnikov 		}
1458e84c258SEugene Krasnikov 	} else {
1468e84c258SEugene Krasnikov 		__vif_priv = get_vif_by_addr(wcn, hdr->addr2);
1478e84c258SEugene Krasnikov 		bd->sta_index = __vif_priv->self_sta_index;
1488e84c258SEugene Krasnikov 		bd->dpu_desc_idx = __vif_priv->self_dpu_desc_index;
1492ba0b461SPontus Fuchs 		bd->dpu_sign = __vif_priv->self_ucast_dpu_sign;
15082cad2a0SPontus Fuchs 	}
1518e84c258SEugene Krasnikov 
1528e84c258SEugene Krasnikov 	if (ieee80211_is_nullfunc(hdr->frame_control) ||
1538e84c258SEugene Krasnikov 	   (sta_priv && !sta_priv->is_data_encrypted))
1548e84c258SEugene Krasnikov 		bd->dpu_ne = 1;
1558e84c258SEugene Krasnikov 
1568e84c258SEugene Krasnikov 	if (bcast) {
1578e84c258SEugene Krasnikov 		bd->ub = 1;
1588e84c258SEugene Krasnikov 		bd->ack_policy = 1;
1598e84c258SEugene Krasnikov 	}
1608e84c258SEugene Krasnikov 	*vif_priv = __vif_priv;
1618e84c258SEugene Krasnikov }
1628e84c258SEugene Krasnikov 
1638e84c258SEugene Krasnikov static void wcn36xx_set_tx_mgmt(struct wcn36xx_tx_bd *bd,
1648e84c258SEugene Krasnikov 				struct wcn36xx *wcn,
1658e84c258SEugene Krasnikov 				struct wcn36xx_vif **vif_priv,
1668e84c258SEugene Krasnikov 				struct ieee80211_hdr *hdr,
1678e84c258SEugene Krasnikov 				bool bcast)
1688e84c258SEugene Krasnikov {
1698e84c258SEugene Krasnikov 	struct wcn36xx_vif *__vif_priv =
1708e84c258SEugene Krasnikov 		get_vif_by_addr(wcn, hdr->addr2);
1718e84c258SEugene Krasnikov 	bd->sta_index = __vif_priv->self_sta_index;
1728e84c258SEugene Krasnikov 	bd->dpu_desc_idx = __vif_priv->self_dpu_desc_index;
1738e84c258SEugene Krasnikov 	bd->dpu_ne = 1;
1748e84c258SEugene Krasnikov 
1758e84c258SEugene Krasnikov 	/* default rate for unicast */
1768e84c258SEugene Krasnikov 	if (ieee80211_is_mgmt(hdr->frame_control))
1778e84c258SEugene Krasnikov 		bd->bd_rate = (WCN36XX_BAND(wcn) == IEEE80211_BAND_5GHZ) ?
1788e84c258SEugene Krasnikov 			WCN36XX_BD_RATE_CTRL :
1798e84c258SEugene Krasnikov 			WCN36XX_BD_RATE_MGMT;
1808e84c258SEugene Krasnikov 	else if (ieee80211_is_ctl(hdr->frame_control))
1818e84c258SEugene Krasnikov 		bd->bd_rate = WCN36XX_BD_RATE_CTRL;
1828e84c258SEugene Krasnikov 	else
1838e84c258SEugene Krasnikov 		wcn36xx_warn("frame control type unknown\n");
1848e84c258SEugene Krasnikov 
1858e84c258SEugene Krasnikov 	/*
1868e84c258SEugene Krasnikov 	 * In joining state trick hardware that probe is sent as
1878e84c258SEugene Krasnikov 	 * unicast even if address is broadcast.
1888e84c258SEugene Krasnikov 	 */
1898e84c258SEugene Krasnikov 	if (__vif_priv->is_joining &&
1908e84c258SEugene Krasnikov 	    ieee80211_is_probe_req(hdr->frame_control))
1918e84c258SEugene Krasnikov 		bcast = false;
1928e84c258SEugene Krasnikov 
1938e84c258SEugene Krasnikov 	if (bcast) {
1948e84c258SEugene Krasnikov 		/* broadcast */
1958e84c258SEugene Krasnikov 		bd->ub = 1;
1968e84c258SEugene Krasnikov 		/* No ack needed not unicast */
1978e84c258SEugene Krasnikov 		bd->ack_policy = 1;
1988e84c258SEugene Krasnikov 		bd->queue_id = WCN36XX_TX_B_WQ_ID;
1998e84c258SEugene Krasnikov 	} else
2008e84c258SEugene Krasnikov 		bd->queue_id = WCN36XX_TX_U_WQ_ID;
2018e84c258SEugene Krasnikov 	*vif_priv = __vif_priv;
2028e84c258SEugene Krasnikov }
2038e84c258SEugene Krasnikov 
2048e84c258SEugene Krasnikov int wcn36xx_start_tx(struct wcn36xx *wcn,
2058e84c258SEugene Krasnikov 		     struct wcn36xx_sta *sta_priv,
2068e84c258SEugene Krasnikov 		     struct sk_buff *skb)
2078e84c258SEugene Krasnikov {
2088e84c258SEugene Krasnikov 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
2098e84c258SEugene Krasnikov 	struct wcn36xx_vif *vif_priv = NULL;
2108e84c258SEugene Krasnikov 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
2118e84c258SEugene Krasnikov 	unsigned long flags;
2128e84c258SEugene Krasnikov 	bool is_low = ieee80211_is_data(hdr->frame_control);
2138e84c258SEugene Krasnikov 	bool bcast = is_broadcast_ether_addr(hdr->addr1) ||
2148e84c258SEugene Krasnikov 		is_multicast_ether_addr(hdr->addr1);
2158e84c258SEugene Krasnikov 	struct wcn36xx_tx_bd *bd = wcn36xx_dxe_get_next_bd(wcn, is_low);
2168e84c258SEugene Krasnikov 
2178e84c258SEugene Krasnikov 	if (!bd) {
2188e84c258SEugene Krasnikov 		/*
2198e84c258SEugene Krasnikov 		 * TX DXE are used in pairs. One for the BD and one for the
2208e84c258SEugene Krasnikov 		 * actual frame. The BD DXE's has a preallocated buffer while
2218e84c258SEugene Krasnikov 		 * the skb ones does not. If this isn't true something is really
2228e84c258SEugene Krasnikov 		 * wierd. TODO: Recover from this situation
2238e84c258SEugene Krasnikov 		 */
2248e84c258SEugene Krasnikov 
2258e84c258SEugene Krasnikov 		wcn36xx_err("bd address may not be NULL for BD DXE\n");
2268e84c258SEugene Krasnikov 		return -EINVAL;
2278e84c258SEugene Krasnikov 	}
2288e84c258SEugene Krasnikov 
2298e84c258SEugene Krasnikov 	memset(bd, 0, sizeof(*bd));
2308e84c258SEugene Krasnikov 
2318e84c258SEugene Krasnikov 	wcn36xx_dbg(WCN36XX_DBG_TX,
2328e84c258SEugene Krasnikov 		    "tx skb %p len %d fc %04x sn %d %s %s\n",
2338e84c258SEugene Krasnikov 		    skb, skb->len, __le16_to_cpu(hdr->frame_control),
2348e84c258SEugene Krasnikov 		    IEEE80211_SEQ_TO_SN(__le16_to_cpu(hdr->seq_ctrl)),
2358e84c258SEugene Krasnikov 		    is_low ? "low" : "high", bcast ? "bcast" : "ucast");
2368e84c258SEugene Krasnikov 
2378e84c258SEugene Krasnikov 	wcn36xx_dbg_dump(WCN36XX_DBG_TX_DUMP, "", skb->data, skb->len);
2388e84c258SEugene Krasnikov 
2398e84c258SEugene Krasnikov 	bd->dpu_rf = WCN36XX_BMU_WQ_TX;
2408e84c258SEugene Krasnikov 
241f786a6d4SBob Copeland 	bd->tx_comp = !!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS);
2428e84c258SEugene Krasnikov 	if (bd->tx_comp) {
2438e84c258SEugene Krasnikov 		wcn36xx_dbg(WCN36XX_DBG_DXE, "TX_ACK status requested\n");
2448e84c258SEugene Krasnikov 		spin_lock_irqsave(&wcn->dxe_lock, flags);
2458e84c258SEugene Krasnikov 		if (wcn->tx_ack_skb) {
2468e84c258SEugene Krasnikov 			spin_unlock_irqrestore(&wcn->dxe_lock, flags);
2478e84c258SEugene Krasnikov 			wcn36xx_warn("tx_ack_skb already set\n");
2488e84c258SEugene Krasnikov 			return -EINVAL;
2498e84c258SEugene Krasnikov 		}
2508e84c258SEugene Krasnikov 
2518e84c258SEugene Krasnikov 		wcn->tx_ack_skb = skb;
2528e84c258SEugene Krasnikov 		spin_unlock_irqrestore(&wcn->dxe_lock, flags);
2538e84c258SEugene Krasnikov 
2548e84c258SEugene Krasnikov 		/* Only one at a time is supported by fw. Stop the TX queues
2558e84c258SEugene Krasnikov 		 * until the ack status gets back.
2568e84c258SEugene Krasnikov 		 *
2578e84c258SEugene Krasnikov 		 * TODO: Add watchdog in case FW does not answer
2588e84c258SEugene Krasnikov 		 */
2598e84c258SEugene Krasnikov 		ieee80211_stop_queues(wcn->hw);
2608e84c258SEugene Krasnikov 	}
2618e84c258SEugene Krasnikov 
2628e84c258SEugene Krasnikov 	/* Data frames served first*/
2638e84c258SEugene Krasnikov 	if (is_low) {
2648e84c258SEugene Krasnikov 		wcn36xx_set_tx_data(bd, wcn, &vif_priv, sta_priv, hdr, bcast);
2658e84c258SEugene Krasnikov 		wcn36xx_set_tx_pdu(bd,
2668e84c258SEugene Krasnikov 			   ieee80211_is_data_qos(hdr->frame_control) ?
2678e84c258SEugene Krasnikov 			   sizeof(struct ieee80211_qos_hdr) :
2688e84c258SEugene Krasnikov 			   sizeof(struct ieee80211_hdr_3addr),
2698e84c258SEugene Krasnikov 			   skb->len, sta_priv ? sta_priv->tid : 0);
2708e84c258SEugene Krasnikov 	} else {
2718e84c258SEugene Krasnikov 		/* MGMT and CTRL frames are handeld here*/
2728e84c258SEugene Krasnikov 		wcn36xx_set_tx_mgmt(bd, wcn, &vif_priv, hdr, bcast);
2738e84c258SEugene Krasnikov 		wcn36xx_set_tx_pdu(bd,
2748e84c258SEugene Krasnikov 			   ieee80211_is_data_qos(hdr->frame_control) ?
2758e84c258SEugene Krasnikov 			   sizeof(struct ieee80211_qos_hdr) :
2768e84c258SEugene Krasnikov 			   sizeof(struct ieee80211_hdr_3addr),
2778e84c258SEugene Krasnikov 			   skb->len, WCN36XX_TID);
2788e84c258SEugene Krasnikov 	}
2798e84c258SEugene Krasnikov 
2808e84c258SEugene Krasnikov 	buff_to_be((u32 *)bd, sizeof(*bd)/sizeof(u32));
2818e84c258SEugene Krasnikov 	bd->tx_bd_sign = 0xbdbdbdbd;
2828e84c258SEugene Krasnikov 
2838e84c258SEugene Krasnikov 	return wcn36xx_dxe_tx_frame(wcn, vif_priv, skb, is_low);
2848e84c258SEugene Krasnikov }
285