a555fa38d834850f023213c75014d75827bfb29c
[platform/kernel/linux-rpi.git] / drivers / net / wireless / ath / ath10k / txrx.c
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         if (txq && txq->sta && skb_cb->airtime_est)
99                 ieee80211_sta_register_airtime(txq->sta, txq->tid,
100                                                skb_cb->airtime_est, 0);
101
102         if (ar->bus_param.dev_type != ATH10K_DEV_TYPE_HL)
103                 dma_unmap_single(dev, skb_cb->paddr, msdu->len, DMA_TO_DEVICE);
104
105         ath10k_report_offchan_tx(htt->ar, msdu);
106
107         info = IEEE80211_SKB_CB(msdu);
108         memset(&info->status, 0, sizeof(info->status));
109         trace_ath10k_txrx_tx_unref(ar, tx_done->msdu_id);
110
111         if (!(info->flags & IEEE80211_TX_CTL_NO_ACK))
112                 info->flags |= IEEE80211_TX_STAT_ACK;
113
114         if (tx_done->status == HTT_TX_COMPL_STATE_NOACK)
115                 info->flags &= ~IEEE80211_TX_STAT_ACK;
116
117         if ((tx_done->status == HTT_TX_COMPL_STATE_ACK) &&
118             (info->flags & IEEE80211_TX_CTL_NO_ACK))
119                 info->flags |= IEEE80211_TX_STAT_NOACK_TRANSMITTED;
120
121         if (tx_done->status == HTT_TX_COMPL_STATE_DISCARD) {
122                 if (info->flags & IEEE80211_TX_CTL_NO_ACK)
123                         info->flags &= ~IEEE80211_TX_STAT_NOACK_TRANSMITTED;
124                 else
125                         info->flags &= ~IEEE80211_TX_STAT_ACK;
126         }
127
128         if (tx_done->status == HTT_TX_COMPL_STATE_ACK &&
129             tx_done->ack_rssi != ATH10K_INVALID_RSSI) {
130                 info->status.ack_signal = ATH10K_DEFAULT_NOISE_FLOOR +
131                                                 tx_done->ack_rssi;
132                 info->status.is_valid_ack_signal = true;
133         }
134
135         ieee80211_tx_status(htt->ar->hw, msdu);
136         /* we do not own the msdu anymore */
137
138         return 0;
139 }
140
141 struct ath10k_peer *ath10k_peer_find(struct ath10k *ar, int vdev_id,
142                                      const u8 *addr)
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 (peer->vdev_id != vdev_id)
150                         continue;
151                 if (!ether_addr_equal(peer->addr, addr))
152                         continue;
153
154                 return peer;
155         }
156
157         return NULL;
158 }
159
160 struct ath10k_peer *ath10k_peer_find_by_id(struct ath10k *ar, int peer_id)
161 {
162         struct ath10k_peer *peer;
163
164         lockdep_assert_held(&ar->data_lock);
165
166         list_for_each_entry(peer, &ar->peers, list)
167                 if (test_bit(peer_id, peer->peer_ids))
168                         return peer;
169
170         return NULL;
171 }
172
173 static int ath10k_wait_for_peer_common(struct ath10k *ar, int vdev_id,
174                                        const u8 *addr, bool expect_mapped)
175 {
176         long time_left;
177
178         time_left = wait_event_timeout(ar->peer_mapping_wq, ({
179                         bool mapped;
180
181                         spin_lock_bh(&ar->data_lock);
182                         mapped = !!ath10k_peer_find(ar, vdev_id, addr);
183                         spin_unlock_bh(&ar->data_lock);
184
185                         (mapped == expect_mapped ||
186                          test_bit(ATH10K_FLAG_CRASH_FLUSH, &ar->dev_flags));
187                 }), 3 * HZ);
188
189         if (time_left == 0)
190                 return -ETIMEDOUT;
191
192         return 0;
193 }
194
195 int ath10k_wait_for_peer_created(struct ath10k *ar, int vdev_id, const u8 *addr)
196 {
197         return ath10k_wait_for_peer_common(ar, vdev_id, addr, true);
198 }
199
200 int ath10k_wait_for_peer_deleted(struct ath10k *ar, int vdev_id, const u8 *addr)
201 {
202         return ath10k_wait_for_peer_common(ar, vdev_id, addr, false);
203 }
204
205 void ath10k_peer_map_event(struct ath10k_htt *htt,
206                            struct htt_peer_map_event *ev)
207 {
208         struct ath10k *ar = htt->ar;
209         struct ath10k_peer *peer;
210
211         if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
212                 ath10k_warn(ar,
213                             "received htt peer map event with idx out of bounds: %hu\n",
214                             ev->peer_id);
215                 return;
216         }
217
218         spin_lock_bh(&ar->data_lock);
219         peer = ath10k_peer_find(ar, ev->vdev_id, ev->addr);
220         if (!peer) {
221                 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
222                 if (!peer)
223                         goto exit;
224
225                 peer->vdev_id = ev->vdev_id;
226                 ether_addr_copy(peer->addr, ev->addr);
227                 list_add(&peer->list, &ar->peers);
228                 wake_up(&ar->peer_mapping_wq);
229         }
230
231         ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer map vdev %d peer %pM id %d\n",
232                    ev->vdev_id, ev->addr, ev->peer_id);
233
234         WARN_ON(ar->peer_map[ev->peer_id] && (ar->peer_map[ev->peer_id] != peer));
235         ar->peer_map[ev->peer_id] = peer;
236         set_bit(ev->peer_id, peer->peer_ids);
237 exit:
238         spin_unlock_bh(&ar->data_lock);
239 }
240
241 void ath10k_peer_unmap_event(struct ath10k_htt *htt,
242                              struct htt_peer_unmap_event *ev)
243 {
244         struct ath10k *ar = htt->ar;
245         struct ath10k_peer *peer;
246
247         if (ev->peer_id >= ATH10K_MAX_NUM_PEER_IDS) {
248                 ath10k_warn(ar,
249                             "received htt peer unmap event with idx out of bounds: %hu\n",
250                             ev->peer_id);
251                 return;
252         }
253
254         spin_lock_bh(&ar->data_lock);
255         peer = ath10k_peer_find_by_id(ar, ev->peer_id);
256         if (!peer) {
257                 ath10k_warn(ar, "peer-unmap-event: unknown peer id %d\n",
258                             ev->peer_id);
259                 goto exit;
260         }
261
262         ath10k_dbg(ar, ATH10K_DBG_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
263                    peer->vdev_id, peer->addr, ev->peer_id);
264
265         ar->peer_map[ev->peer_id] = NULL;
266         clear_bit(ev->peer_id, peer->peer_ids);
267
268         if (bitmap_empty(peer->peer_ids, ATH10K_MAX_NUM_PEER_IDS)) {
269                 list_del(&peer->list);
270                 kfree(peer);
271                 wake_up(&ar->peer_mapping_wq);
272         }
273
274 exit:
275         spin_unlock_bh(&ar->data_lock);
276 }