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 struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
104 				     const u8 *addr)
105 {
106 	struct ath10k_peer *peer;
107 
108 	lockdep_assert_held(&ar->data_lock);
109 
110 	list_for_each_entry(peer, &ar->peers, list) {
111 		if (peer->vdev_id != vdev_id)
112 			continue;
113 		if (memcmp(peer->addr, addr, ETH_ALEN))
114 			continue;
115 
116 		return peer;
117 	}
118 
119 	return NULL;
120 }
121 
122 static struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar,
123 						  int peer_id)
124 {
125 	struct ath10k_peer *peer;
126 
127 	lockdep_assert_held(&ar->data_lock);
128 
129 	list_for_each_entry(peer, &ar->peers, list)
130 		if (test_bit(peer_id, peer->peer_ids))
131 			return peer;
132 
133 	return NULL;
134 }
135 
136 static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
137 				       const u8 *addr, bool expect_mapped)
138 {
139 	int ret;
140 
141 	ret = wait_event_timeout(ar->peer_mapping_wq, ({
142 			bool mapped;
143 
144 			spin_lock_bh(&ar->data_lock);
145 			mapped = !!ath10k_peer_find(ar, vdev_id, addr);
146 			spin_unlock_bh(&ar->data_lock);
147 
148 			mapped == expect_mapped;
149 		}), 3*HZ);
150 
151 	if (ret <= 0)
152 		return -ETIMEDOUT;
153 
154 	return 0;
155 }
156 
157 int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
158 {
159 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
160 }
161 
162 int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
163 {
164 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
165 }
166 
167 void ath10k_peer_map_event(struct ath10k_htt *htt,
168 			   struct htt_peer_map_event *ev)
169 {
170 	struct ath10k *ar = htt->ar;
171 	struct ath10k_peer *peer;
172 
173 	spin_lock_bh(&ar->data_lock);
174 	peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
175 	if (!peer) {
176 		peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
177 		if (!peer)
178 			goto exit;
179 
180 		peer->vdev_id = ev->vdev_id;
181 		memcpy(peer->addr, ev->addr, ETH_ALEN);
182 		list_add(&peer->list, &ar->peers);
183 		wake_up(&ar->peer_mapping_wq);
184 	}
185 
186 	ath10k_dbg(ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
187 		   ev->vdev_id, ev->addr, ev->peer_id);
188 
189 	set_bit(ev->peer_id, peer->peer_ids);
190 exit:
191 	spin_unlock_bh(&ar->data_lock);
192 }
193 
194 void ath10k_peer_unmap_event(struct ath10k_htt *htt,
195 			     struct htt_peer_unmap_event *ev)
196 {
197 	struct ath10k *ar = htt->ar;
198 	struct ath10k_peer *peer;
199 
200 	spin_lock_bh(&ar->data_lock);
201 	peer = ath10k_peer_find_by_id(ar, ev->peer_id);
202 	if (!peer) {
203 		ath10k_warn("peer-unmap-event: unknown peer id %d\n",
204 			    ev->peer_id);
205 		goto exit;
206 	}
207 
208 	ath10k_dbg(ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
209 		   peer->vdev_id, peer->addr, ev->peer_id);
210 
211 	clear_bit(ev->peer_id, peer->peer_ids);
212 
213 	if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
214 		list_del(&peer->list);
215 		kfree(peer);
216 		wake_up(&ar->peer_mapping_wq);
217 	}
218 
219 exit:
220 	spin_unlock_bh(&ar->data_lock);
221 }
222