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