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