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(ar, "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(ar, 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 ath10k *ar = htt->ar;
51 	struct device *dev = ar->dev;
52 	struct ieee80211_tx_info *info;
53 	struct ath10k_skb_cb *skb_cb;
54 	struct sk_buff *msdu;
55 
56 	ath10k_dbg(ar, ATH10K_DBG_HTT,
57 		   "htt tx completion msdu_id %u discard %d no_ack %d success %d\n",
58 		   tx_done->msdu_id, !!tx_done->discard,
59 		   !!tx_done->no_ack, !!tx_done->success);
60 
61 	if (tx_done->msdu_id >= htt->max_num_pending_tx) {
62 		ath10k_warn(ar, "warning: msdu_id %d too big, ignoring\n",
63 			    tx_done->msdu_id);
64 		return;
65 	}
66 
67 	spin_lock_bh(&htt->tx_lock);
68 	msdu = idr_find(&htt->pending_tx, tx_done->msdu_id);
69 	if (!msdu) {
70 		ath10k_warn(ar, "received tx completion for invalid msdu_id: %d\n",
71 			    tx_done->msdu_id);
72 		spin_unlock_bh(&htt->tx_lock);
73 		return;
74 	}
75 	ath10k_htt_tx_free_msdu_id(htt, tx_done->msdu_id);
76 	__ath10k_htt_tx_dec_pending(htt);
77 	if (htt->num_pending_tx == 0)
78 		wake_up(&htt->empty_tx_wq);
79 	spin_unlock_bh(&htt->tx_lock);
80 
81 	skb_cb = ATH10K_SKB_CB(msdu);
82 
83 	dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
84 
85 	if (skb_cb->htt.txbuf)
86 		dma_pool_free(htt->tx_pool,
87 			      skb_cb->htt.txbuf,
88 			      skb_cb->htt.txbuf_paddr);
89 
90 	ath10k_report_offchan_tx(htt->ar, msdu);
91 
92 	info = IEEE80211_SKB_CB(msdu);
93 	memset(&info->status, 0, sizeof(info->status));
94 	trace_ath10k_txrx_tx_unref(ar, tx_done->msdu_id);
95 
96 	if (tx_done->discard) {
97 		ieee80211_free_txskb(htt->ar->hw, msdu);
98 		return;
99 	}
100 
101 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
102 		info->flags |= IEEE80211_TX_STAT_ACK;
103 
104 	if (tx_done->no_ack)
105 		info->flags &= ~IEEE80211_TX_STAT_ACK;
106 
107 	if (tx_done->success && (info->flags & IEEE80211_TX_CTL_NO_ACK))
108 		info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
109 
110 	ieee80211_tx_status(htt->ar->hw, msdu);
111 	/* we do not own the msdu anymore */
112 }
113 
114 struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
115 				     const u8 *addr)
116 {
117 	struct ath10k_peer *peer;
118 
119 	lockdep_assert_held(&ar->data_lock);
120 
121 	list_for_each_entry(peer, &ar->peers, list) {
122 		if (peer->vdev_id != vdev_id)
123 			continue;
124 		if (memcmp(peer->addr, addr, ETH_ALEN))
125 			continue;
126 
127 		return peer;
128 	}
129 
130 	return NULL;
131 }
132 
133 struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar, int peer_id)
134 {
135 	struct ath10k_peer *peer;
136 
137 	lockdep_assert_held(&ar->data_lock);
138 
139 	list_for_each_entry(peer, &ar->peers, list)
140 		if (test_bit(peer_id, peer->peer_ids))
141 			return peer;
142 
143 	return NULL;
144 }
145 
146 static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
147 				       const u8 *addr, bool expect_mapped)
148 {
149 	long time_left;
150 
151 	time_left = wait_event_timeout(ar->peer_mapping_wq, ({
152 			bool mapped;
153 
154 			spin_lock_bh(&ar->data_lock);
155 			mapped = !!ath10k_peer_find(ar, vdev_id, addr);
156 			spin_unlock_bh(&ar->data_lock);
157 
158 			(mapped == expect_mapped ||
159 			 test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags));
160 		}), 3*HZ);
161 
162 	if (time_left == 0)
163 		return -ETIMEDOUT;
164 
165 	return 0;
166 }
167 
168 int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
169 {
170 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
171 }
172 
173 int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
174 {
175 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
176 }
177 
178 void ath10k_peer_map_event(struct ath10k_htt *htt,
179 			   struct htt_peer_map_event *ev)
180 {
181 	struct ath10k *ar = htt->ar;
182 	struct ath10k_peer *peer;
183 
184 	spin_lock_bh(&ar->data_lock);
185 	peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
186 	if (!peer) {
187 		peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
188 		if (!peer)
189 			goto exit;
190 
191 		peer->vdev_id = ev->vdev_id;
192 		ether_addr_copy(peer->addr, ev->addr);
193 		list_add(&peer->list, &ar->peers);
194 		wake_up(&ar->peer_mapping_wq);
195 	}
196 
197 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
198 		   ev->vdev_id, ev->addr, ev->peer_id);
199 
200 	set_bit(ev->peer_id, peer->peer_ids);
201 exit:
202 	spin_unlock_bh(&ar->data_lock);
203 }
204 
205 void ath10k_peer_unmap_event(struct ath10k_htt *htt,
206 			     struct htt_peer_unmap_event *ev)
207 {
208 	struct ath10k *ar = htt->ar;
209 	struct ath10k_peer *peer;
210 
211 	spin_lock_bh(&ar->data_lock);
212 	peer = ath10k_peer_find_by_id(ar, ev->peer_id);
213 	if (!peer) {
214 		ath10k_warn(ar, "peer-unmap-event: unknown peer id %d\n",
215 			    ev->peer_id);
216 		goto exit;
217 	}
218 
219 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
220 		   peer->vdev_id, peer->addr, ev->peer_id);
221 
222 	clear_bit(ev->peer_id, peer->peer_ids);
223 
224 	if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
225 		list_del(&peer->list);
226 		kfree(peer);
227 		wake_up(&ar->peer_mapping_wq);
228 	}
229 
230 exit:
231 	spin_unlock_bh(&ar->data_lock);
232 }
233