xref: /openbmc/linux/drivers/net/wireless/ath/wcn36xx/txrx.c (revision f677b30b487ca3763c3de3f1b4d8c976c2961cd1)
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 
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18 
19 #include "txrx.h"
20 
21 static inline int get_rssi0(struct wcn36xx_rx_bd *bd)
22 {
23 	return 100 - ((bd->phy_stat0 >> 24) & 0xff);
24 }
25 
26 int wcn36xx_rx_skb(struct wcn36xx *wcn, struct sk_buff *skb)
27 {
28 	struct ieee80211_rx_status status;
29 	struct ieee80211_hdr *hdr;
30 	struct wcn36xx_rx_bd *bd;
31 	u16 fc, sn;
32 
33 	/*
34 	 * All fields must be 0, otherwise it can lead to
35 	 * unexpected consequences.
36 	 */
37 	memset(&status, 0, sizeof(status));
38 
39 	bd = (struct wcn36xx_rx_bd *)skb->data;
40 	buff_to_be((u32 *)bd, sizeof(*bd)/sizeof(u32));
41 	wcn36xx_dbg_dump(WCN36XX_DBG_RX_DUMP,
42 			 "BD   <<< ", (char *)bd,
43 			 sizeof(struct wcn36xx_rx_bd));
44 
45 	skb_put(skb, bd->pdu.mpdu_header_off + bd->pdu.mpdu_len);
46 	skb_pull(skb, bd->pdu.mpdu_header_off);
47 
48 	status.mactime = 10;
49 	status.freq = WCN36XX_CENTER_FREQ(wcn);
50 	status.band = WCN36XX_BAND(wcn);
51 	status.signal = -get_rssi0(bd);
52 	status.antenna = 1;
53 	status.rate_idx = 1;
54 	status.flag = 0;
55 	status.rx_flags = 0;
56 	status.flag |= RX_FLAG_IV_STRIPPED |
57 		       RX_FLAG_MMIC_STRIPPED |
58 		       RX_FLAG_DECRYPTED;
59 
60 	wcn36xx_dbg(WCN36XX_DBG_RX, "status.flags=%x status->vendor_radiotap_len=%x\n",
61 		    status.flag,  status.vendor_radiotap_len);
62 
63 	memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
64 
65 	hdr = (struct ieee80211_hdr *) skb->data;
66 	fc = __le16_to_cpu(hdr->frame_control);
67 	sn = IEEE80211_SEQ_TO_SN(__le16_to_cpu(hdr->seq_ctrl));
68 
69 	if (ieee80211_is_beacon(hdr->frame_control)) {
70 		wcn36xx_dbg(WCN36XX_DBG_BEACON, "beacon skb %p len %d fc %04x sn %d\n",
71 			    skb, skb->len, fc, sn);
72 		wcn36xx_dbg_dump(WCN36XX_DBG_BEACON_DUMP, "SKB <<< ",
73 				 (char *)skb->data, skb->len);
74 	} else {
75 		wcn36xx_dbg(WCN36XX_DBG_RX, "rx skb %p len %d fc %04x sn %d\n",
76 			    skb, skb->len, fc, sn);
77 		wcn36xx_dbg_dump(WCN36XX_DBG_RX_DUMP, "SKB <<< ",
78 				 (char *)skb->data, skb->len);
79 	}
80 
81 	ieee80211_rx_irqsafe(wcn->hw, skb);
82 
83 	return 0;
84 }
85 
86 static void wcn36xx_set_tx_pdu(struct wcn36xx_tx_bd *bd,
87 			       u32 mpdu_header_len,
88 			       u32 len,
89 			       u16 tid)
90 {
91 	bd->pdu.mpdu_header_len = mpdu_header_len;
92 	bd->pdu.mpdu_header_off = sizeof(*bd);
93 	bd->pdu.mpdu_data_off = bd->pdu.mpdu_header_len +
94 		bd->pdu.mpdu_header_off;
95 	bd->pdu.mpdu_len = len;
96 	bd->pdu.tid = tid;
97 }
98 
99 static inline struct wcn36xx_vif *get_vif_by_addr(struct wcn36xx *wcn,
100 						  u8 *addr)
101 {
102 	struct wcn36xx_vif *vif_priv = NULL;
103 	struct ieee80211_vif *vif = NULL;
104 	list_for_each_entry(vif_priv, &wcn->vif_list, list) {
105 			vif = container_of((void *)vif_priv,
106 				   struct ieee80211_vif,
107 				   drv_priv);
108 			if (memcmp(vif->addr, addr, ETH_ALEN) == 0)
109 				return vif_priv;
110 	}
111 	wcn36xx_warn("vif %pM not found\n", addr);
112 	return NULL;
113 }
114 static void wcn36xx_set_tx_data(struct wcn36xx_tx_bd *bd,
115 				struct wcn36xx *wcn,
116 				struct wcn36xx_vif **vif_priv,
117 				struct wcn36xx_sta *sta_priv,
118 				struct ieee80211_hdr *hdr,
119 				bool bcast)
120 {
121 	struct ieee80211_vif *vif = NULL;
122 	struct wcn36xx_vif *__vif_priv = NULL;
123 	bd->bd_rate = WCN36XX_BD_RATE_DATA;
124 
125 	/*
126 	 * For not unicast frames mac80211 will not set sta pointer so use
127 	 * self_sta_index instead.
128 	 */
129 	if (sta_priv) {
130 		__vif_priv = sta_priv->vif;
131 		vif = container_of((void *)__vif_priv,
132 				   struct ieee80211_vif,
133 				   drv_priv);
134 
135 		if (vif->type == NL80211_IFTYPE_STATION) {
136 			bd->sta_index = sta_priv->bss_sta_index;
137 			bd->dpu_desc_idx = sta_priv->bss_dpu_desc_index;
138 		} else if (vif->type == NL80211_IFTYPE_AP ||
139 			   vif->type == NL80211_IFTYPE_ADHOC ||
140 			   vif->type == NL80211_IFTYPE_MESH_POINT) {
141 			bd->sta_index = sta_priv->sta_index;
142 			bd->dpu_desc_idx = sta_priv->dpu_desc_index;
143 		}
144 	} else {
145 		__vif_priv = get_vif_by_addr(wcn, hdr->addr2);
146 		bd->sta_index = __vif_priv->self_sta_index;
147 		bd->dpu_desc_idx = __vif_priv->self_dpu_desc_index;
148 	}
149 
150 	bd->dpu_sign = __vif_priv->ucast_dpu_signature;
151 
152 	if (ieee80211_is_nullfunc(hdr->frame_control) ||
153 	   (sta_priv && !sta_priv->is_data_encrypted))
154 		bd->dpu_ne = 1;
155 
156 	if (bcast) {
157 		bd->ub = 1;
158 		bd->ack_policy = 1;
159 	}
160 	*vif_priv = __vif_priv;
161 }
162 
163 static void wcn36xx_set_tx_mgmt(struct wcn36xx_tx_bd *bd,
164 				struct wcn36xx *wcn,
165 				struct wcn36xx_vif **vif_priv,
166 				struct ieee80211_hdr *hdr,
167 				bool bcast)
168 {
169 	struct wcn36xx_vif *__vif_priv =
170 		get_vif_by_addr(wcn, hdr->addr2);
171 	bd->sta_index = __vif_priv->self_sta_index;
172 	bd->dpu_desc_idx = __vif_priv->self_dpu_desc_index;
173 	bd->dpu_ne = 1;
174 
175 	/* default rate for unicast */
176 	if (ieee80211_is_mgmt(hdr->frame_control))
177 		bd->bd_rate = (WCN36XX_BAND(wcn) == IEEE80211_BAND_5GHZ) ?
178 			WCN36XX_BD_RATE_CTRL :
179 			WCN36XX_BD_RATE_MGMT;
180 	else if (ieee80211_is_ctl(hdr->frame_control))
181 		bd->bd_rate = WCN36XX_BD_RATE_CTRL;
182 	else
183 		wcn36xx_warn("frame control type unknown\n");
184 
185 	/*
186 	 * In joining state trick hardware that probe is sent as
187 	 * unicast even if address is broadcast.
188 	 */
189 	if (__vif_priv->is_joining &&
190 	    ieee80211_is_probe_req(hdr->frame_control))
191 		bcast = false;
192 
193 	if (bcast) {
194 		/* broadcast */
195 		bd->ub = 1;
196 		/* No ack needed not unicast */
197 		bd->ack_policy = 1;
198 		bd->queue_id = WCN36XX_TX_B_WQ_ID;
199 	} else
200 		bd->queue_id = WCN36XX_TX_U_WQ_ID;
201 	*vif_priv = __vif_priv;
202 }
203 
204 int wcn36xx_start_tx(struct wcn36xx *wcn,
205 		     struct wcn36xx_sta *sta_priv,
206 		     struct sk_buff *skb)
207 {
208 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
209 	struct wcn36xx_vif *vif_priv = NULL;
210 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
211 	unsigned long flags;
212 	bool is_low = ieee80211_is_data(hdr->frame_control);
213 	bool bcast = is_broadcast_ether_addr(hdr->addr1) ||
214 		is_multicast_ether_addr(hdr->addr1);
215 	struct wcn36xx_tx_bd *bd = wcn36xx_dxe_get_next_bd(wcn, is_low);
216 
217 	if (!bd) {
218 		/*
219 		 * TX DXE are used in pairs. One for the BD and one for the
220 		 * actual frame. The BD DXE's has a preallocated buffer while
221 		 * the skb ones does not. If this isn't true something is really
222 		 * wierd. TODO: Recover from this situation
223 		 */
224 
225 		wcn36xx_err("bd address may not be NULL for BD DXE\n");
226 		return -EINVAL;
227 	}
228 
229 	memset(bd, 0, sizeof(*bd));
230 
231 	wcn36xx_dbg(WCN36XX_DBG_TX,
232 		    "tx skb %p len %d fc %04x sn %d %s %s\n",
233 		    skb, skb->len, __le16_to_cpu(hdr->frame_control),
234 		    IEEE80211_SEQ_TO_SN(__le16_to_cpu(hdr->seq_ctrl)),
235 		    is_low ? "low" : "high", bcast ? "bcast" : "ucast");
236 
237 	wcn36xx_dbg_dump(WCN36XX_DBG_TX_DUMP, "", skb->data, skb->len);
238 
239 	bd->dpu_rf = WCN36XX_BMU_WQ_TX;
240 
241 	bd->tx_comp = info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS;
242 	if (bd->tx_comp) {
243 		wcn36xx_dbg(WCN36XX_DBG_DXE, "TX_ACK status requested\n");
244 		spin_lock_irqsave(&wcn->dxe_lock, flags);
245 		if (wcn->tx_ack_skb) {
246 			spin_unlock_irqrestore(&wcn->dxe_lock, flags);
247 			wcn36xx_warn("tx_ack_skb already set\n");
248 			return -EINVAL;
249 		}
250 
251 		wcn->tx_ack_skb = skb;
252 		spin_unlock_irqrestore(&wcn->dxe_lock, flags);
253 
254 		/* Only one at a time is supported by fw. Stop the TX queues
255 		 * until the ack status gets back.
256 		 *
257 		 * TODO: Add watchdog in case FW does not answer
258 		 */
259 		ieee80211_stop_queues(wcn->hw);
260 	}
261 
262 	/* Data frames served first*/
263 	if (is_low) {
264 		wcn36xx_set_tx_data(bd, wcn, &vif_priv, sta_priv, hdr, bcast);
265 		wcn36xx_set_tx_pdu(bd,
266 			   ieee80211_is_data_qos(hdr->frame_control) ?
267 			   sizeof(struct ieee80211_qos_hdr) :
268 			   sizeof(struct ieee80211_hdr_3addr),
269 			   skb->len, sta_priv ? sta_priv->tid : 0);
270 	} else {
271 		/* MGMT and CTRL frames are handeld here*/
272 		wcn36xx_set_tx_mgmt(bd, wcn, &vif_priv, hdr, bcast);
273 		wcn36xx_set_tx_pdu(bd,
274 			   ieee80211_is_data_qos(hdr->frame_control) ?
275 			   sizeof(struct ieee80211_qos_hdr) :
276 			   sizeof(struct ieee80211_hdr_3addr),
277 			   skb->len, WCN36XX_TID);
278 	}
279 
280 	buff_to_be((u32 *)bd, sizeof(*bd)/sizeof(u32));
281 	bd->tx_bd_sign = 0xbdbdbdbd;
282 
283 	return wcn36xx_dxe_tx_frame(wcn, vif_priv, skb, is_low);
284 }
285