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 	hdr = (struct ieee80211_hdr *) skb->data;
49 	fc = __le16_to_cpu(hdr->frame_control);
50 	sn = IEEE80211_SEQ_TO_SN(__le16_to_cpu(hdr->seq_ctrl));
51 
52 	/* When scanning associate beacons to this */
53 	if (ieee80211_is_beacon(hdr->frame_control) && wcn->scan_freq) {
54 		status.freq = wcn->scan_freq;
55 		status.band = wcn->scan_band;
56 	} else {
57 		status.freq = WCN36XX_CENTER_FREQ(wcn);
58 		status.band = WCN36XX_BAND(wcn);
59 	}
60 
61 	status.mactime = 10;
62 	status.signal = -get_rssi0(bd);
63 	status.antenna = 1;
64 	status.rate_idx = 1;
65 	status.flag = 0;
66 	status.rx_flags = 0;
67 	status.flag |= RX_FLAG_IV_STRIPPED |
68 		       RX_FLAG_MMIC_STRIPPED |
69 		       RX_FLAG_DECRYPTED;
70 
71 	wcn36xx_dbg(WCN36XX_DBG_RX, "status.flags=%x\n", status.flag);
72 
73 	memcpy(IEEE80211_SKB_RXCB(skb), &status, sizeof(status));
74 
75 	if (ieee80211_is_beacon(hdr->frame_control)) {
76 		wcn36xx_dbg(WCN36XX_DBG_BEACON, "beacon skb %p len %d fc %04x sn %d\n",
77 			    skb, skb->len, fc, sn);
78 		wcn36xx_dbg_dump(WCN36XX_DBG_BEACON_DUMP, "SKB <<< ",
79 				 (char *)skb->data, skb->len);
80 	} else {
81 		wcn36xx_dbg(WCN36XX_DBG_RX, "rx skb %p len %d fc %04x sn %d\n",
82 			    skb, skb->len, fc, sn);
83 		wcn36xx_dbg_dump(WCN36XX_DBG_RX_DUMP, "SKB <<< ",
84 				 (char *)skb->data, skb->len);
85 	}
86 
87 	ieee80211_rx_irqsafe(wcn->hw, skb);
88 
89 	return 0;
90 }
91 
92 static void wcn36xx_set_tx_pdu(struct wcn36xx_tx_bd *bd,
93 			       u32 mpdu_header_len,
94 			       u32 len,
95 			       u16 tid)
96 {
97 	bd->pdu.mpdu_header_len = mpdu_header_len;
98 	bd->pdu.mpdu_header_off = sizeof(*bd);
99 	bd->pdu.mpdu_data_off = bd->pdu.mpdu_header_len +
100 		bd->pdu.mpdu_header_off;
101 	bd->pdu.mpdu_len = len;
102 	bd->pdu.tid = tid;
103 	bd->pdu.bd_ssn = WCN36XX_TXBD_SSN_FILL_DPU_QOS;
104 }
105 
106 static inline struct wcn36xx_vif *get_vif_by_addr(struct wcn36xx *wcn,
107 						  u8 *addr)
108 {
109 	struct wcn36xx_vif *vif_priv = NULL;
110 	struct ieee80211_vif *vif = NULL;
111 	list_for_each_entry(vif_priv, &wcn->vif_list, list) {
112 			vif = wcn36xx_priv_to_vif(vif_priv);
113 			if (memcmp(vif->addr, addr, ETH_ALEN) == 0)
114 				return vif_priv;
115 	}
116 	wcn36xx_warn("vif %pM not found\n", addr);
117 	return NULL;
118 }
119 
120 static void wcn36xx_tx_start_ampdu(struct wcn36xx *wcn,
121 				   struct wcn36xx_sta *sta_priv,
122 				   struct sk_buff *skb)
123 {
124 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
125 	struct ieee80211_sta *sta;
126 	u8 *qc, tid;
127 
128 	if (!conf_is_ht(&wcn->hw->conf))
129 		return;
130 
131 	sta = wcn36xx_priv_to_sta(sta_priv);
132 
133 	if (WARN_ON(!ieee80211_is_data_qos(hdr->frame_control)))
134 		return;
135 
136 	if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
137 		return;
138 
139 	qc = ieee80211_get_qos_ctl(hdr);
140 	tid = qc[0] & IEEE80211_QOS_CTL_TID_MASK;
141 
142 	spin_lock(&sta_priv->ampdu_lock);
143 	if (sta_priv->ampdu_state[tid] != WCN36XX_AMPDU_NONE)
144 		goto out_unlock;
145 
146 	if (sta_priv->non_agg_frame_ct++ >= WCN36XX_AMPDU_START_THRESH) {
147 		sta_priv->ampdu_state[tid] = WCN36XX_AMPDU_START;
148 		sta_priv->non_agg_frame_ct = 0;
149 		ieee80211_start_tx_ba_session(sta, tid, 0);
150 	}
151 out_unlock:
152 	spin_unlock(&sta_priv->ampdu_lock);
153 }
154 
155 static void wcn36xx_set_tx_data(struct wcn36xx_tx_bd *bd,
156 				struct wcn36xx *wcn,
157 				struct wcn36xx_vif **vif_priv,
158 				struct wcn36xx_sta *sta_priv,
159 				struct sk_buff *skb,
160 				bool bcast)
161 {
162 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
163 	struct ieee80211_vif *vif = NULL;
164 	struct wcn36xx_vif *__vif_priv = NULL;
165 	bool is_data_qos;
166 
167 	bd->bd_rate = WCN36XX_BD_RATE_DATA;
168 
169 	/*
170 	 * For not unicast frames mac80211 will not set sta pointer so use
171 	 * self_sta_index instead.
172 	 */
173 	if (sta_priv) {
174 		__vif_priv = sta_priv->vif;
175 		vif = wcn36xx_priv_to_vif(__vif_priv);
176 
177 		bd->dpu_sign = sta_priv->ucast_dpu_sign;
178 		if (vif->type == NL80211_IFTYPE_STATION) {
179 			bd->sta_index = sta_priv->bss_sta_index;
180 			bd->dpu_desc_idx = sta_priv->bss_dpu_desc_index;
181 		} else if (vif->type == NL80211_IFTYPE_AP ||
182 			   vif->type == NL80211_IFTYPE_ADHOC ||
183 			   vif->type == NL80211_IFTYPE_MESH_POINT) {
184 			bd->sta_index = sta_priv->sta_index;
185 			bd->dpu_desc_idx = sta_priv->dpu_desc_index;
186 		}
187 	} else {
188 		__vif_priv = get_vif_by_addr(wcn, hdr->addr2);
189 		bd->sta_index = __vif_priv->self_sta_index;
190 		bd->dpu_desc_idx = __vif_priv->self_dpu_desc_index;
191 		bd->dpu_sign = __vif_priv->self_ucast_dpu_sign;
192 	}
193 
194 	if (ieee80211_is_nullfunc(hdr->frame_control) ||
195 	   (sta_priv && !sta_priv->is_data_encrypted))
196 		bd->dpu_ne = 1;
197 
198 	if (bcast) {
199 		bd->ub = 1;
200 		bd->ack_policy = 1;
201 	}
202 	*vif_priv = __vif_priv;
203 
204 	is_data_qos = ieee80211_is_data_qos(hdr->frame_control);
205 
206 	wcn36xx_set_tx_pdu(bd,
207 			   is_data_qos ?
208 			   sizeof(struct ieee80211_qos_hdr) :
209 			   sizeof(struct ieee80211_hdr_3addr),
210 			   skb->len, sta_priv ? sta_priv->tid : 0);
211 
212 	if (sta_priv && is_data_qos)
213 		wcn36xx_tx_start_ampdu(wcn, sta_priv, skb);
214 }
215 
216 static void wcn36xx_set_tx_mgmt(struct wcn36xx_tx_bd *bd,
217 				struct wcn36xx *wcn,
218 				struct wcn36xx_vif **vif_priv,
219 				struct sk_buff *skb,
220 				bool bcast)
221 {
222 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
223 	struct wcn36xx_vif *__vif_priv =
224 		get_vif_by_addr(wcn, hdr->addr2);
225 	bd->sta_index = __vif_priv->self_sta_index;
226 	bd->dpu_desc_idx = __vif_priv->self_dpu_desc_index;
227 	bd->dpu_ne = 1;
228 
229 	/* default rate for unicast */
230 	if (ieee80211_is_mgmt(hdr->frame_control))
231 		bd->bd_rate = (WCN36XX_BAND(wcn) == NL80211_BAND_5GHZ) ?
232 			WCN36XX_BD_RATE_CTRL :
233 			WCN36XX_BD_RATE_MGMT;
234 	else if (ieee80211_is_ctl(hdr->frame_control))
235 		bd->bd_rate = WCN36XX_BD_RATE_CTRL;
236 	else
237 		wcn36xx_warn("frame control type unknown\n");
238 
239 	/*
240 	 * In joining state trick hardware that probe is sent as
241 	 * unicast even if address is broadcast.
242 	 */
243 	if (__vif_priv->is_joining &&
244 	    ieee80211_is_probe_req(hdr->frame_control))
245 		bcast = false;
246 
247 	if (bcast) {
248 		/* broadcast */
249 		bd->ub = 1;
250 		/* No ack needed not unicast */
251 		bd->ack_policy = 1;
252 		bd->queue_id = WCN36XX_TX_B_WQ_ID;
253 	} else
254 		bd->queue_id = WCN36XX_TX_U_WQ_ID;
255 	*vif_priv = __vif_priv;
256 
257 	wcn36xx_set_tx_pdu(bd,
258 			   ieee80211_is_data_qos(hdr->frame_control) ?
259 			   sizeof(struct ieee80211_qos_hdr) :
260 			   sizeof(struct ieee80211_hdr_3addr),
261 			   skb->len, WCN36XX_TID);
262 }
263 
264 int wcn36xx_start_tx(struct wcn36xx *wcn,
265 		     struct wcn36xx_sta *sta_priv,
266 		     struct sk_buff *skb)
267 {
268 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
269 	struct wcn36xx_vif *vif_priv = NULL;
270 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
271 	unsigned long flags;
272 	bool is_low = ieee80211_is_data(hdr->frame_control);
273 	bool bcast = is_broadcast_ether_addr(hdr->addr1) ||
274 		is_multicast_ether_addr(hdr->addr1);
275 	struct wcn36xx_tx_bd bd;
276 	int ret;
277 
278 	memset(&bd, 0, sizeof(bd));
279 
280 	wcn36xx_dbg(WCN36XX_DBG_TX,
281 		    "tx skb %p len %d fc %04x sn %d %s %s\n",
282 		    skb, skb->len, __le16_to_cpu(hdr->frame_control),
283 		    IEEE80211_SEQ_TO_SN(__le16_to_cpu(hdr->seq_ctrl)),
284 		    is_low ? "low" : "high", bcast ? "bcast" : "ucast");
285 
286 	wcn36xx_dbg_dump(WCN36XX_DBG_TX_DUMP, "", skb->data, skb->len);
287 
288 	bd.dpu_rf = WCN36XX_BMU_WQ_TX;
289 
290 	bd.tx_comp = !!(info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS);
291 	if (bd.tx_comp) {
292 		wcn36xx_dbg(WCN36XX_DBG_DXE, "TX_ACK status requested\n");
293 		spin_lock_irqsave(&wcn->dxe_lock, flags);
294 		if (wcn->tx_ack_skb) {
295 			spin_unlock_irqrestore(&wcn->dxe_lock, flags);
296 			wcn36xx_warn("tx_ack_skb already set\n");
297 			return -EINVAL;
298 		}
299 
300 		wcn->tx_ack_skb = skb;
301 		spin_unlock_irqrestore(&wcn->dxe_lock, flags);
302 
303 		/* Only one at a time is supported by fw. Stop the TX queues
304 		 * until the ack status gets back.
305 		 *
306 		 * TODO: Add watchdog in case FW does not answer
307 		 */
308 		ieee80211_stop_queues(wcn->hw);
309 	}
310 
311 	/* Data frames served first*/
312 	if (is_low)
313 		wcn36xx_set_tx_data(&bd, wcn, &vif_priv, sta_priv, skb, bcast);
314 	else
315 		/* MGMT and CTRL frames are handeld here*/
316 		wcn36xx_set_tx_mgmt(&bd, wcn, &vif_priv, skb, bcast);
317 
318 	buff_to_be((u32 *)&bd, sizeof(bd)/sizeof(u32));
319 	bd.tx_bd_sign = 0xbdbdbdbd;
320 
321 	ret = wcn36xx_dxe_tx_frame(wcn, vif_priv, &bd, skb, is_low);
322 	if (ret && bd.tx_comp) {
323 		/* If the skb has not been transmitted,
324 		 * don't keep a reference to it.
325 		 */
326 		spin_lock_irqsave(&wcn->dxe_lock, flags);
327 		wcn->tx_ack_skb = NULL;
328 		spin_unlock_irqrestore(&wcn->dxe_lock, flags);
329 
330 		ieee80211_wake_queues(wcn->hw);
331 	}
332 
333 	return ret;
334 }
335