1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2013 Qualcomm Atheros, Inc.
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include "core.h"
19 #include "txrx.h"
20 #include "htt.h"
21 #include "mac.h"
22 #include "debug.h"
23 
24 static void ath10k_report_offchan_tx(struct ath10k *ar, struct sk_buff *skb)
25 {
26 	if (!ATH10K_SKB_CB(skb)->htt.is_offchan)
27 		return;
28 
29 	/* If the original wait_for_completion() timed out before
30 	 * {data,mgmt}_tx_completed() was called then we could complete
31 	 * offchan_tx_completed for a different skb. Prevent this by using
32 	 * offchan_tx_skb. */
33 	spin_lock_bh(&ar->data_lock);
34 	if (ar->offchan_tx_skb != skb) {
35 		ath10k_warn("completed old offchannel frame\n");
36 		goto out;
37 	}
38 
39 	complete(&ar->offchan_tx_completed);
40 	ar->offchan_tx_skb = NULL; /* just for sanity */
41 
42 	ath10k_dbg(ATH10K_DBG_HTT, "completed offchannel skb %p\n", skb);
43 out:
44 	spin_unlock_bh(&ar->data_lock);
45 }
46 
47 void ath10k_txrx_tx_unref(struct ath10k_htt *htt,
48 			  const struct htt_tx_done *tx_done)
49 {
50 	struct device *dev = htt->ar->dev;
51 	struct ieee80211_tx_info *info;
52 	struct ath10k_skb_cb *skb_cb;
53 	struct sk_buff *msdu;
54 
55 	lockdep_assert_held(&htt->tx_lock);
56 
57 	ath10k_dbg(ATH10K_DBG_HTT, "htt tx completion msdu_id %u discard %d no_ack %d\n",
58 		   tx_done->msdu_id, !!tx_done->discard, !!tx_done->no_ack);
59 
60 	if (tx_done->msdu_id >= htt->max_num_pending_tx) {
61 		ath10k_warn("warning: msdu_id %d too big, ignoring\n",
62 			    tx_done->msdu_id);
63 		return;
64 	}
65 
66 	msdu = htt->pending_tx[tx_done->msdu_id];
67 	skb_cb = ATH10K_SKB_CB(msdu);
68 
69 	dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
70 
71 	if (skb_cb->htt.txbuf)
72 		dma_pool_free(htt->tx_pool,
73 			      skb_cb->htt.txbuf,
74 			      skb_cb->htt.txbuf_paddr);
75 
76 	ath10k_report_offchan_tx(htt->ar, msdu);
77 
78 	info = IEEE80211_SKB_CB(msdu);
79 	memset(&info->status, 0, sizeof(info->status));
80 
81 	if (tx_done->discard) {
82 		ieee80211_free_txskb(htt->ar->hw, msdu);
83 		goto exit;
84 	}
85 
86 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
87 		info->flags |= IEEE80211_TX_STAT_ACK;
88 
89 	if (tx_done->no_ack)
90 		info->flags &= ~IEEE80211_TX_STAT_ACK;
91 
92 	ieee80211_tx_status(htt->ar->hw, msdu);
93 	/* we do not own the msdu anymore */
94 
95 exit:
96 	htt->pending_tx[tx_done->msdu_id] = NULL;
97 	ath10k_htt_tx_free_msdu_id(htt, tx_done->msdu_id);
98 	__ath10k_htt_tx_dec_pending(htt);
99 	if (htt->num_pending_tx == 0)
100 		wake_up(&htt->empty_tx_wq);
101 }
102 
103 static const u8 rx_legacy_rate_idx[] = {
104 	3,	/* 0x00  - 11Mbps  */
105 	2,	/* 0x01  - 5.5Mbps */
106 	1,	/* 0x02  - 2Mbps   */
107 	0,	/* 0x03  - 1Mbps   */
108 	3,	/* 0x04  - 11Mbps  */
109 	2,	/* 0x05  - 5.5Mbps */
110 	1,	/* 0x06  - 2Mbps   */
111 	0,	/* 0x07  - 1Mbps   */
112 	10,	/* 0x08  - 48Mbps  */
113 	8,	/* 0x09  - 24Mbps  */
114 	6,	/* 0x0A  - 12Mbps  */
115 	4,	/* 0x0B  - 6Mbps   */
116 	11,	/* 0x0C  - 54Mbps  */
117 	9,	/* 0x0D  - 36Mbps  */
118 	7,	/* 0x0E  - 18Mbps  */
119 	5,	/* 0x0F  - 9Mbps   */
120 };
121 
122 static void process_rx_rates(struct ath10k *ar, struct htt_rx_info *info,
123 			     enum ieee80211_band band,
124 			     struct ieee80211_rx_status *status)
125 {
126 	u8 cck, rate, rate_idx, bw, sgi, mcs, nss;
127 	u8 info0 = info->rate.info0;
128 	u32 info1 = info->rate.info1;
129 	u32 info2 = info->rate.info2;
130 	u8 preamble = 0;
131 
132 	/* Check if valid fields */
133 	if (!(info0 & HTT_RX_INDICATION_INFO0_START_VALID))
134 		return;
135 
136 	preamble = MS(info1, HTT_RX_INDICATION_INFO1_PREAMBLE_TYPE);
137 
138 	switch (preamble) {
139 	case HTT_RX_LEGACY:
140 		cck = info0 & HTT_RX_INDICATION_INFO0_LEGACY_RATE_CCK;
141 		rate = MS(info0, HTT_RX_INDICATION_INFO0_LEGACY_RATE);
142 		rate_idx = 0;
143 
144 		if (rate < 0x08 || rate > 0x0F)
145 			break;
146 
147 		switch (band) {
148 		case IEEE80211_BAND_2GHZ:
149 			if (cck)
150 				rate &= ~BIT(3);
151 			rate_idx = rx_legacy_rate_idx[rate];
152 			break;
153 		case IEEE80211_BAND_5GHZ:
154 			rate_idx = rx_legacy_rate_idx[rate];
155 			/* We are using same rate table registering
156 			   HW - ath10k_rates[]. In case of 5GHz skip
157 			   CCK rates, so -4 here */
158 			rate_idx -= 4;
159 			break;
160 		default:
161 			break;
162 		}
163 
164 		status->rate_idx = rate_idx;
165 		break;
166 	case HTT_RX_HT:
167 	case HTT_RX_HT_WITH_TXBF:
168 		/* HT-SIG - Table 20-11 in info1 and info2 */
169 		mcs = info1 & 0x1F;
170 		nss = mcs >> 3;
171 		bw = (info1 >> 7) & 1;
172 		sgi = (info2 >> 7) & 1;
173 
174 		status->rate_idx = mcs;
175 		status->flag |= RX_FLAG_HT;
176 		if (sgi)
177 			status->flag |= RX_FLAG_SHORT_GI;
178 		if (bw)
179 			status->flag |= RX_FLAG_40MHZ;
180 		break;
181 	case HTT_RX_VHT:
182 	case HTT_RX_VHT_WITH_TXBF:
183 		/* VHT-SIG-A1 in info 1, VHT-SIG-A2 in info2
184 		   TODO check this */
185 		mcs = (info2 >> 4) & 0x0F;
186 		nss = ((info1 >> 10) & 0x07) + 1;
187 		bw = info1 & 3;
188 		sgi = info2 & 1;
189 
190 		status->rate_idx = mcs;
191 		status->vht_nss = nss;
192 
193 		if (sgi)
194 			status->flag |= RX_FLAG_SHORT_GI;
195 
196 		switch (bw) {
197 		/* 20MHZ */
198 		case 0:
199 			break;
200 		/* 40MHZ */
201 		case 1:
202 			status->flag |= RX_FLAG_40MHZ;
203 			break;
204 		/* 80MHZ */
205 		case 2:
206 			status->vht_flag |= RX_VHT_FLAG_80MHZ;
207 		}
208 
209 		status->flag |= RX_FLAG_VHT;
210 		break;
211 	default:
212 		break;
213 	}
214 }
215 
216 void ath10k_process_rx(struct ath10k *ar, struct htt_rx_info *info)
217 {
218 	struct ieee80211_rx_status *status;
219 	struct ieee80211_channel *ch;
220 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)info->skb->data;
221 
222 	status = IEEE80211_SKB_RXCB(info->skb);
223 	memset(status, 0, sizeof(*status));
224 
225 	if (info->encrypt_type != HTT_RX_MPDU_ENCRYPT_NONE) {
226 		status->flag |= RX_FLAG_DECRYPTED | RX_FLAG_IV_STRIPPED |
227 				RX_FLAG_MMIC_STRIPPED;
228 		hdr->frame_control = __cpu_to_le16(
229 				__le16_to_cpu(hdr->frame_control) &
230 				~IEEE80211_FCTL_PROTECTED);
231 	}
232 
233 	if (info->mic_err)
234 		status->flag |= RX_FLAG_MMIC_ERROR;
235 
236 	if (info->fcs_err)
237 		status->flag |= RX_FLAG_FAILED_FCS_CRC;
238 
239 	if (info->amsdu_more)
240 		status->flag |= RX_FLAG_AMSDU_MORE;
241 
242 	status->signal = info->signal;
243 
244 	spin_lock_bh(&ar->data_lock);
245 	ch = ar->scan_channel;
246 	if (!ch)
247 		ch = ar->rx_channel;
248 	spin_unlock_bh(&ar->data_lock);
249 
250 	if (!ch) {
251 		ath10k_warn("no channel configured; ignoring frame!\n");
252 		dev_kfree_skb_any(info->skb);
253 		return;
254 	}
255 
256 	process_rx_rates(ar, info, ch->band, status);
257 	status->band = ch->band;
258 	status->freq = ch->center_freq;
259 
260 	if (info->rate.info0 & HTT_RX_INDICATION_INFO0_END_VALID) {
261 		/* TSF available only in 32-bit */
262 		status->mactime = info->tsf & 0xffffffff;
263 		status->flag |= RX_FLAG_MACTIME_END;
264 	}
265 
266 	ath10k_dbg(ATH10K_DBG_DATA,
267 		   "rx skb %p len %u %s%s%s%s%s %srate_idx %u vht_nss %u freq %u band %u flag 0x%x fcs-err %i\n",
268 		   info->skb,
269 		   info->skb->len,
270 		   status->flag == 0 ? "legacy" : "",
271 		   status->flag & RX_FLAG_HT ? "ht" : "",
272 		   status->flag & RX_FLAG_VHT ? "vht" : "",
273 		   status->flag & RX_FLAG_40MHZ ? "40" : "",
274 		   status->vht_flag & RX_VHT_FLAG_80MHZ ? "80" : "",
275 		   status->flag & RX_FLAG_SHORT_GI ? "sgi " : "",
276 		   status->rate_idx,
277 		   status->vht_nss,
278 		   status->freq,
279 		   status->band, status->flag, info->fcs_err);
280 	ath10k_dbg_dump(ATH10K_DBG_HTT_DUMP, NULL, "rx skb: ",
281 			info->skb->data, info->skb->len);
282 
283 	ieee80211_rx(ar->hw, info->skb);
284 }
285 
286 struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
287 				     const u8 *addr)
288 {
289 	struct ath10k_peer *peer;
290 
291 	lockdep_assert_held(&ar->data_lock);
292 
293 	list_for_each_entry(peer, &ar->peers, list) {
294 		if (peer->vdev_id != vdev_id)
295 			continue;
296 		if (memcmp(peer->addr, addr, ETH_ALEN))
297 			continue;
298 
299 		return peer;
300 	}
301 
302 	return NULL;
303 }
304 
305 static struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar,
306 						  int peer_id)
307 {
308 	struct ath10k_peer *peer;
309 
310 	lockdep_assert_held(&ar->data_lock);
311 
312 	list_for_each_entry(peer, &ar->peers, list)
313 		if (test_bit(peer_id, peer->peer_ids))
314 			return peer;
315 
316 	return NULL;
317 }
318 
319 static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
320 				       const u8 *addr, bool expect_mapped)
321 {
322 	int ret;
323 
324 	ret = wait_event_timeout(ar->peer_mapping_wq, ({
325 			bool mapped;
326 
327 			spin_lock_bh(&ar->data_lock);
328 			mapped = !!ath10k_peer_find(ar, vdev_id, addr);
329 			spin_unlock_bh(&ar->data_lock);
330 
331 			mapped == expect_mapped;
332 		}), 3*HZ);
333 
334 	if (ret <= 0)
335 		return -ETIMEDOUT;
336 
337 	return 0;
338 }
339 
340 int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
341 {
342 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
343 }
344 
345 int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
346 {
347 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
348 }
349 
350 void ath10k_peer_map_event(struct ath10k_htt *htt,
351 			   struct htt_peer_map_event *ev)
352 {
353 	struct ath10k *ar = htt->ar;
354 	struct ath10k_peer *peer;
355 
356 	spin_lock_bh(&ar->data_lock);
357 	peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
358 	if (!peer) {
359 		peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
360 		if (!peer)
361 			goto exit;
362 
363 		peer->vdev_id = ev->vdev_id;
364 		memcpy(peer->addr, ev->addr, ETH_ALEN);
365 		list_add(&peer->list, &ar->peers);
366 		wake_up(&ar->peer_mapping_wq);
367 	}
368 
369 	ath10k_dbg(ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
370 		   ev->vdev_id, ev->addr, ev->peer_id);
371 
372 	set_bit(ev->peer_id, peer->peer_ids);
373 exit:
374 	spin_unlock_bh(&ar->data_lock);
375 }
376 
377 void ath10k_peer_unmap_event(struct ath10k_htt *htt,
378 			     struct htt_peer_unmap_event *ev)
379 {
380 	struct ath10k *ar = htt->ar;
381 	struct ath10k_peer *peer;
382 
383 	spin_lock_bh(&ar->data_lock);
384 	peer = ath10k_peer_find_by_id(ar, ev->peer_id);
385 	if (!peer) {
386 		ath10k_warn("peer-unmap-event: unknown peer id %d\n",
387 			    ev->peer_id);
388 		goto exit;
389 	}
390 
391 	ath10k_dbg(ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
392 		   peer->vdev_id, peer->addr, ev->peer_id);
393 
394 	clear_bit(ev->peer_id, peer->peer_ids);
395 
396 	if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
397 		list_del(&peer->list);
398 		kfree(peer);
399 		wake_up(&ar->peer_mapping_wq);
400 	}
401 
402 exit:
403 	spin_unlock_bh(&ar->data_lock);
404 }
405