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