xref: /linux/drivers/net/wireless/ath/ath10k/txrx.c (revision 307797159ac25fe5a2048bf5c6a5718298edca57)
1 /*
2  * Copyright (c) 2005-2011 Atheros Communications Inc.
3  * Copyright (c) 2011-2016 Qualcomm Atheros, Inc.
4  * Copyright (c) 2018, The Linux Foundation. All rights reserved.
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "core.h"
20 #include "txrx.h"
21 #include "htt.h"
22 #include "mac.h"
23 #include "debug.h"
24 
25 static void ath10k_report_offchan_tx(struct ath10k *ar, struct sk_buff *skb)
26 {
27 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
28 
29 	if (likely(!(info->flags & IEEE80211_TX_CTL_TX_OFFCHAN)))
30 		return;
31 
32 	if (ath10k_mac_tx_frm_has_freq(ar))
33 		return;
34 
35 	/* If the original wait_for_completion() timed out before
36 	 * {data,mgmt}_tx_completed() was called then we could complete
37 	 * offchan_tx_completed for a different skb. Prevent this by using
38 	 * offchan_tx_skb.
39 	 */
40 	spin_lock_bh(&ar->data_lock);
41 	if (ar->offchan_tx_skb != skb) {
42 		ath10k_warn(ar, "completed old offchannel frame\n");
43 		goto out;
44 	}
45 
46 	complete(&ar->offchan_tx_completed);
47 	ar->offchan_tx_skb = NULL; /* just for sanity */
48 
49 	ath10k_dbg(ar, ATH10K_DBG_HTT, "completed offchannel skb %pK\n", skb);
50 out:
51 	spin_unlock_bh(&ar->data_lock);
52 }
53 
54 int ath10k_txrx_tx_unref(struct ath10k_htt *htt,
55 			 const struct htt_tx_done *tx_done)
56 {
57 	struct ath10k *ar = htt->ar;
58 	struct device *dev = ar->dev;
59 	struct ieee80211_tx_info *info;
60 	struct ieee80211_txq *txq;
61 	struct ath10k_skb_cb *skb_cb;
62 	struct ath10k_txq *artxq;
63 	struct sk_buff *msdu;
64 
65 	ath10k_dbg(ar, ATH10K_DBG_HTT,
66 		   "htt tx completion msdu_id %u status %d\n",
67 		   tx_done->msdu_id, tx_done->status);
68 
69 	if (tx_done->msdu_id >= htt->max_num_pending_tx) {
70 		ath10k_warn(ar, "warning: msdu_id %d too big, ignoring\n",
71 			    tx_done->msdu_id);
72 		return -EINVAL;
73 	}
74 
75 	spin_lock_bh(&htt->tx_lock);
76 	msdu = idr_find(&htt->pending_tx, tx_done->msdu_id);
77 	if (!msdu) {
78 		ath10k_warn(ar, "received tx completion for invalid msdu_id: %d\n",
79 			    tx_done->msdu_id);
80 		spin_unlock_bh(&htt->tx_lock);
81 		return -ENOENT;
82 	}
83 
84 	skb_cb = ATH10K_SKB_CB(msdu);
85 	txq = skb_cb->txq;
86 
87 	if (txq) {
88 		artxq = (void *)txq->drv_priv;
89 		artxq->num_fw_queued--;
90 	}
91 
92 	ath10k_htt_tx_free_msdu_id(htt, tx_done->msdu_id);
93 	ath10k_htt_tx_dec_pending(htt);
94 	if (htt->num_pending_tx == 0)
95 		wake_up(&htt->empty_tx_wq);
96 	spin_unlock_bh(&htt->tx_lock);
97 
98 	dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
99 
100 	ath10k_report_offchan_tx(htt->ar, msdu);
101 
102 	info = IEEE80211_SKB_CB(msdu);
103 	memset(&info->status, 0, sizeof(info->status));
104 	trace_ath10k_txrx_tx_unref(ar, tx_done->msdu_id);
105 
106 	if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
107 		info->flags |= IEEE80211_TX_STAT_ACK;
108 
109 	if (tx_done->status == HTT_TX_COMPL_STATE_NOACK)
110 		info->flags &= ~IEEE80211_TX_STAT_ACK;
111 
112 	if ((tx_done->status == HTT_TX_COMPL_STATE_ACK) &&
113 	    (info->flags & IEEE80211_TX_CTL_NO_ACK))
114 		info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
115 
116 	if (tx_done->status == HTT_TX_COMPL_STATE_DISCARD) {
117 		if (info->flags & IEEE80211_TX_CTL_NO_ACK)
118 			info->flags &= ~IEEE80211_TX_STAT_NOACK_TRANSMITTED;
119 		else
120 			info->flags &= ~IEEE80211_TX_STAT_ACK;
121 	}
122 
123 	if (tx_done->status == HTT_TX_COMPL_STATE_ACK &&
124 	    tx_done->ack_rssi != ATH10K_INVALID_RSSI) {
125 		info->status.ack_signal = ATH10K_DEFAULT_NOISE_FLOOR +
126 						tx_done->ack_rssi;
127 		info->status.is_valid_ack_signal = true;
128 	}
129 
130 	ieee80211_tx_status(htt->ar->hw, msdu);
131 	/* we do not own the msdu anymore */
132 
133 	return 0;
134 }
135 
136 struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
137 				     const u8 *addr)
138 {
139 	struct ath10k_peer *peer;
140 
141 	lockdep_assert_held(&ar->data_lock);
142 
143 	list_for_each_entry(peer, &ar->peers, list) {
144 		if (peer->vdev_id != vdev_id)
145 			continue;
146 		if (!ether_addr_equal(peer->addr, addr))
147 			continue;
148 
149 		return peer;
150 	}
151 
152 	return NULL;
153 }
154 
155 struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar, int peer_id)
156 {
157 	struct ath10k_peer *peer;
158 
159 	lockdep_assert_held(&ar->data_lock);
160 
161 	list_for_each_entry(peer, &ar->peers, list)
162 		if (test_bit(peer_id, peer->peer_ids))
163 			return peer;
164 
165 	return NULL;
166 }
167 
168 static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
169 				       const u8 *addr, bool expect_mapped)
170 {
171 	long time_left;
172 
173 	time_left = wait_event_timeout(ar->peer_mapping_wq, ({
174 			bool mapped;
175 
176 			spin_lock_bh(&ar->data_lock);
177 			mapped = !!ath10k_peer_find(ar, vdev_id, addr);
178 			spin_unlock_bh(&ar->data_lock);
179 
180 			(mapped == expect_mapped ||
181 			 test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags));
182 		}), 3 * HZ);
183 
184 	if (time_left == 0)
185 		return -ETIMEDOUT;
186 
187 	return 0;
188 }
189 
190 int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
191 {
192 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
193 }
194 
195 int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
196 {
197 	return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
198 }
199 
200 void ath10k_peer_map_event(struct ath10k_htt *htt,
201 			   struct htt_peer_map_event *ev)
202 {
203 	struct ath10k *ar = htt->ar;
204 	struct ath10k_peer *peer;
205 
206 	if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
207 		ath10k_warn(ar,
208 			    "received htt peer map event with idx out of bounds: %hu\n",
209 			    ev->peer_id);
210 		return;
211 	}
212 
213 	spin_lock_bh(&ar->data_lock);
214 	peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
215 	if (!peer) {
216 		peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
217 		if (!peer)
218 			goto exit;
219 
220 		peer->vdev_id = ev->vdev_id;
221 		ether_addr_copy(peer->addr, ev->addr);
222 		list_add(&peer->list, &ar->peers);
223 		wake_up(&ar->peer_mapping_wq);
224 	}
225 
226 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
227 		   ev->vdev_id, ev->addr, ev->peer_id);
228 
229 	WARN_ON(ar->peer_map[ev->peer_id] && (ar->peer_map[ev->peer_id] != peer));
230 	ar->peer_map[ev->peer_id] = peer;
231 	set_bit(ev->peer_id, peer->peer_ids);
232 exit:
233 	spin_unlock_bh(&ar->data_lock);
234 }
235 
236 void ath10k_peer_unmap_event(struct ath10k_htt *htt,
237 			     struct htt_peer_unmap_event *ev)
238 {
239 	struct ath10k *ar = htt->ar;
240 	struct ath10k_peer *peer;
241 
242 	if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
243 		ath10k_warn(ar,
244 			    "received htt peer unmap event with idx out of bounds: %hu\n",
245 			    ev->peer_id);
246 		return;
247 	}
248 
249 	spin_lock_bh(&ar->data_lock);
250 	peer = ath10k_peer_find_by_id(ar, ev->peer_id);
251 	if (!peer) {
252 		ath10k_warn(ar, "peer-unmap-event: unknown peer id %d\n",
253 			    ev->peer_id);
254 		goto exit;
255 	}
256 
257 	ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
258 		   peer->vdev_id, peer->addr, ev->peer_id);
259 
260 	ar->peer_map[ev->peer_id] = NULL;
261 	clear_bit(ev->peer_id, peer->peer_ids);
262 
263 	if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
264 		list_del(&peer->list);
265 		kfree(peer);
266 		wake_up(&ar->peer_mapping_wq);
267 	}
268 
269 exit:
270 	spin_unlock_bh(&ar->data_lock);
271 }
272