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