xref: /linux/drivers/net/wireless/ath/ath10k/txrx.c (revision 0883c2c06fb5bcf5b9e008270827e63c09a88c1e)
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 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
27 
28 	if (likely(!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)))
29 		return;
30 
31 	if (ath10k_mac_tx_frm_has_freq(ar))
32 		return;
33 
34 	/* If the original wait_for_completion() timed out before
35 	 * {data,mgmt}_tx_completed() was called then we could complete
36 	 * offchan_tx_completed for a different skb. Prevent this by using
37 	 * offchan_tx_skb. */
38 	spin_lock_bh(&ar->data_lock);
39 	if (ar->offchan_tx_skb != skb) {
40 		ath10k_warn(ar, "completed old offchannel frame\n");
41 		goto out;
42 	}
43 
44 	complete(&ar->offchan_tx_completed);
45 	ar->offchan_tx_skb = NULL; /* just for sanity */
46 
47 	ath10k_dbg(ar, ATH10K_DBG_HTT, "completed offchannel skb %p\n", skb);
48 out:
49 	spin_unlock_bh(&ar->data_lock);
50 }
51 
52 int ath10k_txrx_tx_unref(struct ath10k_htt *htt,
53 			 const struct htt_tx_done *tx_done)
54 {
55 	struct ath10k *ar = htt->ar;
56 	struct device *dev = ar->dev;
57 	struct ieee80211_tx_info *info;
58 	struct ieee80211_txq *txq;
59 	struct ath10k_skb_cb *skb_cb;
60 	struct ath10k_txq *artxq;
61 	struct sk_buff *msdu;
62 
63 	ath10k_dbg(ar, ATH10K_DBG_HTT,
64 		   "htt tx completion msdu_id %u status %d\n",
65 		   tx_done->msdu_id, tx_done->status);
66 
67 	if (tx_done->msdu_id >= htt->max_num_pending_tx) {
68 		ath10k_warn(ar, "warning: msdu_id %d too big, ignoring\n",
69 			    tx_done->msdu_id);
70 		return -EINVAL;
71 	}
72 
73 	spin_lock_bh(&htt->tx_lock);
74 	msdu = idr_find(&htt->pending_tx, tx_done->msdu_id);
75 	if (!msdu) {
76 		ath10k_warn(ar, "received tx completion for invalid msdu_id: %d\n",
77 			    tx_done->msdu_id);
78 		spin_unlock_bh(&htt->tx_lock);
79 		return -ENOENT;
80 	}
81 
82 	skb_cb = ATH10K_SKB_CB(msdu);
83 	txq = skb_cb->txq;
84 	artxq = (void *)txq->drv_priv;
85 
86 	if (txq)
87 		artxq->num_fw_queued--;
88 
89 	ath10k_htt_tx_free_msdu_id(htt, tx_done->msdu_id);
90 	ath10k_htt_tx_dec_pending(htt);
91 	if (htt->num_pending_tx == 0)
92 		wake_up(&htt->empty_tx_wq);
93 	spin_unlock_bh(&htt->tx_lock);
94 
95 	dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
96 
97 	ath10k_report_offchan_tx(htt->ar, msdu);
98 
99 	info = IEEE80211_SKB_CB(msdu);
100 	memset(&info->status, 0, sizeof(info->status));
101 	trace_ath10k_txrx_tx_unref(ar, tx_done->msdu_id);
102 
103 	if (tx_done->status == HTT_TX_COMPL_STATE_DISCARD) {
104 		ieee80211_free_txskb(htt->ar->hw, msdu);
105 		return 0;
106 	}
107 
108 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
109 		info->flags |= IEEE80211_TX_STAT_ACK;
110 
111 	if (tx_done->status == HTT_TX_COMPL_STATE_NOACK)
112 		info->flags &= ~IEEE80211_TX_STAT_ACK;
113 
114 	if ((tx_done->status == HTT_TX_COMPL_STATE_ACK) &&
115 	    (info->flags & IEEE80211_TX_CTL_NO_ACK))
116 		info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
117 
118 	ieee80211_tx_status(htt->ar->hw, msdu);
119 	/* we do not own the msdu anymore */
120 	return 0;
121 }
122 
123 struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
124 				     const u8 *addr)
125 {
126 	struct ath10k_peer *peer;
127 
128 	lockdep_assert_held(&ar->data_lock);
129 
130 	list_for_each_entry(peer, &ar->peers, list) {
131 		if (peer->vdev_id != vdev_id)
132 			continue;
133 		if (!ether_addr_equal(peer->addr, addr))
134 			continue;
135 
136 		return peer;
137 	}
138 
139 	return NULL;
140 }
141 
142 struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar, int peer_id)
143 {
144 	struct ath10k_peer *peer;
145 
146 	lockdep_assert_held(&ar->data_lock);
147 
148 	list_for_each_entry(peer, &ar->peers, list)
149 		if (test_bit(peer_id, peer->peer_ids))
150 			return peer;
151 
152 	return NULL;
153 }
154 
155 static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
156 				       const u8 *addr, bool expect_mapped)
157 {
158 	long time_left;
159 
160 	time_left = wait_event_timeout(ar->peer_mapping_wq, ({
161 			bool mapped;
162 
163 			spin_lock_bh(&ar->data_lock);
164 			mapped = !!ath10k_peer_find(ar, vdev_id, addr);
165 			spin_unlock_bh(&ar->data_lock);
166 
167 			(mapped == expect_mapped ||
168 			 test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags));
169 		}), 3 * HZ);
170 
171 	if (time_left == 0)
172 		return -ETIMEDOUT;
173 
174 	return 0;
175 }
176 
177 int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
178 {
179 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
180 }
181 
182 int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
183 {
184 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
185 }
186 
187 void ath10k_peer_map_event(struct ath10k_htt *htt,
188 			   struct htt_peer_map_event *ev)
189 {
190 	struct ath10k *ar = htt->ar;
191 	struct ath10k_peer *peer;
192 
193 	if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
194 		ath10k_warn(ar,
195 			    "received htt peer map event with idx out of bounds: %hu\n",
196 			    ev->peer_id);
197 		return;
198 	}
199 
200 	spin_lock_bh(&ar->data_lock);
201 	peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
202 	if (!peer) {
203 		peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
204 		if (!peer)
205 			goto exit;
206 
207 		peer->vdev_id = ev->vdev_id;
208 		ether_addr_copy(peer->addr, ev->addr);
209 		list_add(&peer->list, &ar->peers);
210 		wake_up(&ar->peer_mapping_wq);
211 	}
212 
213 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
214 		   ev->vdev_id, ev->addr, ev->peer_id);
215 
216 	ar->peer_map[ev->peer_id] = peer;
217 	set_bit(ev->peer_id, peer->peer_ids);
218 exit:
219 	spin_unlock_bh(&ar->data_lock);
220 }
221 
222 void ath10k_peer_unmap_event(struct ath10k_htt *htt,
223 			     struct htt_peer_unmap_event *ev)
224 {
225 	struct ath10k *ar = htt->ar;
226 	struct ath10k_peer *peer;
227 
228 	if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
229 		ath10k_warn(ar,
230 			    "received htt peer unmap event with idx out of bounds: %hu\n",
231 			    ev->peer_id);
232 		return;
233 	}
234 
235 	spin_lock_bh(&ar->data_lock);
236 	peer = ath10k_peer_find_by_id(ar, ev->peer_id);
237 	if (!peer) {
238 		ath10k_warn(ar, "peer-unmap-event: unknown peer id %d\n",
239 			    ev->peer_id);
240 		goto exit;
241 	}
242 
243 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
244 		   peer->vdev_id, peer->addr, ev->peer_id);
245 
246 	ar->peer_map[ev->peer_id] = NULL;
247 	clear_bit(ev->peer_id, peer->peer_ids);
248 
249 	if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
250 		list_del(&peer->list);
251 		kfree(peer);
252 		wake_up(&ar->peer_mapping_wq);
253 	}
254 
255 exit:
256 	spin_unlock_bh(&ar->data_lock);
257 }
258