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\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 }
97 
98 static inline struct wcn36xx_vif *get_vif_by_addr(struct wcn36xx *wcn,
99 						  u8 *addr)
100 {
101 	struct wcn36xx_vif *vif_priv = NULL;
102 	struct ieee80211_vif *vif = NULL;
103 	list_for_each_entry(vif_priv, &wcn->vif_list, list) {
104 			vif = container_of((void *)vif_priv,
105 				   struct ieee80211_vif,
106 				   drv_priv);
107 			if (memcmp(vif->addr, addr, ETH_ALEN) == 0)
108 				return vif_priv;
109 	}
110 	wcn36xx_warn("vif %pM not found\n", addr);
111 	return NULL;
112 }
113 static void wcn36xx_set_tx_data(struct wcn36xx_tx_bd *bd,
114 				struct wcn36xx *wcn,
115 				struct wcn36xx_vif **vif_priv,
116 				struct wcn36xx_sta *sta_priv,
117 				struct ieee80211_hdr *hdr,
118 				bool bcast)
119 {
120 	struct ieee80211_vif *vif = NULL;
121 	struct wcn36xx_vif *__vif_priv = NULL;
122 	bd->bd_rate = WCN36XX_BD_RATE_DATA;
123 
124 	/*
125 	 * For not unicast frames mac80211 will not set sta pointer so use
126 	 * self_sta_index instead.
127 	 */
128 	if (sta_priv) {
129 		__vif_priv = sta_priv->vif;
130 		vif = container_of((void *)__vif_priv,
131 				   struct ieee80211_vif,
132 				   drv_priv);
133 
134 		bd->dpu_sign = sta_priv->ucast_dpu_sign;
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 		bd->dpu_sign = __vif_priv->self_ucast_dpu_sign;
149 	}
150 
151 	if (ieee80211_is_nullfunc(hdr->frame_control) ||
152 	   (sta_priv && !sta_priv->is_data_encrypted))
153 		bd->dpu_ne = 1;
154 
155 	if (bcast) {
156 		bd->ub = 1;
157 		bd->ack_policy = 1;
158 	}
159 	*vif_priv = __vif_priv;
160 }
161 
162 static void wcn36xx_set_tx_mgmt(struct wcn36xx_tx_bd *bd,
163 				struct wcn36xx *wcn,
164 				struct wcn36xx_vif **vif_priv,
165 				struct ieee80211_hdr *hdr,
166 				bool bcast)
167 {
168 	struct wcn36xx_vif *__vif_priv =
169 		get_vif_by_addr(wcn, hdr->addr2);
170 	bd->sta_index = __vif_priv->self_sta_index;
171 	bd->dpu_desc_idx = __vif_priv->self_dpu_desc_index;
172 	bd->dpu_ne = 1;
173 
174 	/* default rate for unicast */
175 	if (ieee80211_is_mgmt(hdr->frame_control))
176 		bd->bd_rate = (WCN36XX_BAND(wcn) == IEEE80211_BAND_5GHZ) ?
177 			WCN36XX_BD_RATE_CTRL :
178 			WCN36XX_BD_RATE_MGMT;
179 	else if (ieee80211_is_ctl(hdr->frame_control))
180 		bd->bd_rate = WCN36XX_BD_RATE_CTRL;
181 	else
182 		wcn36xx_warn("frame control type unknown\n");
183 
184 	/*
185 	 * In joining state trick hardware that probe is sent as
186 	 * unicast even if address is broadcast.
187 	 */
188 	if (__vif_priv->is_joining &&
189 	    ieee80211_is_probe_req(hdr->frame_control))
190 		bcast = false;
191 
192 	if (bcast) {
193 		/* broadcast */
194 		bd->ub = 1;
195 		/* No ack needed not unicast */
196 		bd->ack_policy = 1;
197 		bd->queue_id = WCN36XX_TX_B_WQ_ID;
198 	} else
199 		bd->queue_id = WCN36XX_TX_U_WQ_ID;
200 	*vif_priv = __vif_priv;
201 }
202 
203 int wcn36xx_start_tx(struct wcn36xx *wcn,
204 		     struct wcn36xx_sta *sta_priv,
205 		     struct sk_buff *skb)
206 {
207 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
208 	struct wcn36xx_vif *vif_priv = NULL;
209 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
210 	unsigned long flags;
211 	bool is_low = ieee80211_is_data(hdr->frame_control);
212 	bool bcast = is_broadcast_ether_addr(hdr->addr1) ||
213 		is_multicast_ether_addr(hdr->addr1);
214 	struct wcn36xx_tx_bd *bd = wcn36xx_dxe_get_next_bd(wcn, is_low);
215 
216 	if (!bd) {
217 		/*
218 		 * TX DXE are used in pairs. One for the BD and one for the
219 		 * actual frame. The BD DXE's has a preallocated buffer while
220 		 * the skb ones does not. If this isn't true something is really
221 		 * wierd. TODO: Recover from this situation
222 		 */
223 
224 		wcn36xx_err("bd address may not be NULL for BD DXE\n");
225 		return -EINVAL;
226 	}
227 
228 	memset(bd, 0, sizeof(*bd));
229 
230 	wcn36xx_dbg(WCN36XX_DBG_TX,
231 		    "tx skb %p len %d fc %04x sn %d %s %s\n",
232 		    skb, skb->len, __le16_to_cpu(hdr->frame_control),
233 		    IEEE80211_SEQ_TO_SN(__le16_to_cpu(hdr->seq_ctrl)),
234 		    is_low ? "low" : "high", bcast ? "bcast" : "ucast");
235 
236 	wcn36xx_dbg_dump(WCN36XX_DBG_TX_DUMP, "", skb->data, skb->len);
237 
238 	bd->dpu_rf = WCN36XX_BMU_WQ_TX;
239 
240 	bd->tx_comp = info->flags & IEEE80211_TX_CTL_REQ_TX_STATUS;
241 	if (bd->tx_comp) {
242 		wcn36xx_dbg(WCN36XX_DBG_DXE, "TX_ACK status requested\n");
243 		spin_lock_irqsave(&wcn->dxe_lock, flags);
244 		if (wcn->tx_ack_skb) {
245 			spin_unlock_irqrestore(&wcn->dxe_lock, flags);
246 			wcn36xx_warn("tx_ack_skb already set\n");
247 			return -EINVAL;
248 		}
249 
250 		wcn->tx_ack_skb = skb;
251 		spin_unlock_irqrestore(&wcn->dxe_lock, flags);
252 
253 		/* Only one at a time is supported by fw. Stop the TX queues
254 		 * until the ack status gets back.
255 		 *
256 		 * TODO: Add watchdog in case FW does not answer
257 		 */
258 		ieee80211_stop_queues(wcn->hw);
259 	}
260 
261 	/* Data frames served first*/
262 	if (is_low) {
263 		wcn36xx_set_tx_data(bd, wcn, &vif_priv, sta_priv, hdr, bcast);
264 		wcn36xx_set_tx_pdu(bd,
265 			   ieee80211_is_data_qos(hdr->frame_control) ?
266 			   sizeof(struct ieee80211_qos_hdr) :
267 			   sizeof(struct ieee80211_hdr_3addr),
268 			   skb->len, sta_priv ? sta_priv->tid : 0);
269 	} else {
270 		/* MGMT and CTRL frames are handeld here*/
271 		wcn36xx_set_tx_mgmt(bd, wcn, &vif_priv, hdr, bcast);
272 		wcn36xx_set_tx_pdu(bd,
273 			   ieee80211_is_data_qos(hdr->frame_control) ?
274 			   sizeof(struct ieee80211_qos_hdr) :
275 			   sizeof(struct ieee80211_hdr_3addr),
276 			   skb->len, WCN36XX_TID);
277 	}
278 
279 	buff_to_be((u32 *)bd, sizeof(*bd)/sizeof(u32));
280 	bd->tx_bd_sign = 0xbdbdbdbd;
281 
282 	return wcn36xx_dxe_tx_frame(wcn, vif_priv, skb, is_low);
283 }
284