Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / net / wireless / ath / ath11k / peer.c
blobb69e7ebfa930327fdf8b10260c312d5888cf43bc
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3 * Copyright (c) 2018-2019 The Linux Foundation. All rights reserved.
4 */
6 #include "core.h"
7 #include "peer.h"
8 #include "debug.h"
10 struct ath11k_peer *ath11k_peer_find(struct ath11k_base *ab, int vdev_id,
11 const u8 *addr)
13 struct ath11k_peer *peer;
15 lockdep_assert_held(&ab->base_lock);
17 list_for_each_entry(peer, &ab->peers, list) {
18 if (peer->vdev_id != vdev_id)
19 continue;
20 if (!ether_addr_equal(peer->addr, addr))
21 continue;
23 return peer;
26 return NULL;
29 static struct ath11k_peer *ath11k_peer_find_by_pdev_idx(struct ath11k_base *ab,
30 u8 pdev_idx, const u8 *addr)
32 struct ath11k_peer *peer;
34 lockdep_assert_held(&ab->base_lock);
36 list_for_each_entry(peer, &ab->peers, list) {
37 if (peer->pdev_idx != pdev_idx)
38 continue;
39 if (!ether_addr_equal(peer->addr, addr))
40 continue;
42 return peer;
45 return NULL;
48 struct ath11k_peer *ath11k_peer_find_by_addr(struct ath11k_base *ab,
49 const u8 *addr)
51 struct ath11k_peer *peer;
53 lockdep_assert_held(&ab->base_lock);
55 list_for_each_entry(peer, &ab->peers, list) {
56 if (!ether_addr_equal(peer->addr, addr))
57 continue;
59 return peer;
62 return NULL;
65 struct ath11k_peer *ath11k_peer_find_by_id(struct ath11k_base *ab,
66 int peer_id)
68 struct ath11k_peer *peer;
70 lockdep_assert_held(&ab->base_lock);
72 list_for_each_entry(peer, &ab->peers, list)
73 if (peer_id == peer->peer_id)
74 return peer;
76 return NULL;
79 struct ath11k_peer *ath11k_peer_find_by_vdev_id(struct ath11k_base *ab,
80 int vdev_id)
82 struct ath11k_peer *peer;
84 spin_lock_bh(&ab->base_lock);
86 list_for_each_entry(peer, &ab->peers, list) {
87 if (vdev_id == peer->vdev_id) {
88 spin_unlock_bh(&ab->base_lock);
89 return peer;
92 spin_unlock_bh(&ab->base_lock);
93 return NULL;
96 void ath11k_peer_unmap_event(struct ath11k_base *ab, u16 peer_id)
98 struct ath11k_peer *peer;
100 spin_lock_bh(&ab->base_lock);
102 peer = ath11k_peer_find_by_id(ab, peer_id);
103 if (!peer) {
104 ath11k_warn(ab, "peer-unmap-event: unknown peer id %d\n",
105 peer_id);
106 goto exit;
109 ath11k_dbg(ab, ATH11K_DBG_DP_HTT, "htt peer unmap vdev %d peer %pM id %d\n",
110 peer->vdev_id, peer->addr, peer_id);
112 list_del(&peer->list);
113 kfree(peer);
114 wake_up(&ab->peer_mapping_wq);
116 exit:
117 spin_unlock_bh(&ab->base_lock);
120 void ath11k_peer_map_event(struct ath11k_base *ab, u8 vdev_id, u16 peer_id,
121 u8 *mac_addr, u16 ast_hash)
123 struct ath11k_peer *peer;
125 spin_lock_bh(&ab->base_lock);
126 peer = ath11k_peer_find(ab, vdev_id, mac_addr);
127 if (!peer) {
128 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
129 if (!peer)
130 goto exit;
132 peer->vdev_id = vdev_id;
133 peer->peer_id = peer_id;
134 peer->ast_hash = ast_hash;
135 ether_addr_copy(peer->addr, mac_addr);
136 list_add(&peer->list, &ab->peers);
137 wake_up(&ab->peer_mapping_wq);
140 ath11k_dbg(ab, ATH11K_DBG_DP_HTT, "htt peer map vdev %d peer %pM id %d\n",
141 vdev_id, mac_addr, peer_id);
143 exit:
144 spin_unlock_bh(&ab->base_lock);
147 static int ath11k_wait_for_peer_common(struct ath11k_base *ab, int vdev_id,
148 const u8 *addr, bool expect_mapped)
150 int ret;
152 ret = wait_event_timeout(ab->peer_mapping_wq, ({
153 bool mapped;
155 spin_lock_bh(&ab->base_lock);
156 mapped = !!ath11k_peer_find(ab, vdev_id, addr);
157 spin_unlock_bh(&ab->base_lock);
159 (mapped == expect_mapped ||
160 test_bit(ATH11K_FLAG_CRASH_FLUSH, &ab->dev_flags));
161 }), 3 * HZ);
163 if (ret <= 0)
164 return -ETIMEDOUT;
166 return 0;
169 void ath11k_peer_cleanup(struct ath11k *ar, u32 vdev_id)
171 struct ath11k_peer *peer, *tmp;
172 struct ath11k_base *ab = ar->ab;
174 lockdep_assert_held(&ar->conf_mutex);
176 spin_lock_bh(&ab->base_lock);
177 list_for_each_entry_safe(peer, tmp, &ab->peers, list) {
178 if (peer->vdev_id != vdev_id)
179 continue;
181 ath11k_warn(ab, "removing stale peer %pM from vdev_id %d\n",
182 peer->addr, vdev_id);
184 list_del(&peer->list);
185 kfree(peer);
186 ar->num_peers--;
189 spin_unlock_bh(&ab->base_lock);
192 static int ath11k_wait_for_peer_deleted(struct ath11k *ar, int vdev_id, const u8 *addr)
194 return ath11k_wait_for_peer_common(ar->ab, vdev_id, addr, false);
197 int ath11k_wait_for_peer_delete_done(struct ath11k *ar, u32 vdev_id,
198 const u8 *addr)
200 int ret;
201 unsigned long time_left;
203 ret = ath11k_wait_for_peer_deleted(ar, vdev_id, addr);
204 if (ret) {
205 ath11k_warn(ar->ab, "failed wait for peer deleted");
206 return ret;
209 time_left = wait_for_completion_timeout(&ar->peer_delete_done,
210 3 * HZ);
211 if (time_left == 0) {
212 ath11k_warn(ar->ab, "Timeout in receiving peer delete response\n");
213 return -ETIMEDOUT;
216 return 0;
219 int ath11k_peer_delete(struct ath11k *ar, u32 vdev_id, u8 *addr)
221 int ret;
223 lockdep_assert_held(&ar->conf_mutex);
225 reinit_completion(&ar->peer_delete_done);
227 ret = ath11k_wmi_send_peer_delete_cmd(ar, addr, vdev_id);
228 if (ret) {
229 ath11k_warn(ar->ab,
230 "failed to delete peer vdev_id %d addr %pM ret %d\n",
231 vdev_id, addr, ret);
232 return ret;
235 ret = ath11k_wait_for_peer_delete_done(ar, vdev_id, addr);
236 if (ret)
237 return ret;
239 ar->num_peers--;
241 return 0;
244 static int ath11k_wait_for_peer_created(struct ath11k *ar, int vdev_id, const u8 *addr)
246 return ath11k_wait_for_peer_common(ar->ab, vdev_id, addr, true);
249 int ath11k_peer_create(struct ath11k *ar, struct ath11k_vif *arvif,
250 struct ieee80211_sta *sta, struct peer_create_params *param)
252 struct ath11k_peer *peer;
253 int ret;
255 lockdep_assert_held(&ar->conf_mutex);
257 if (ar->num_peers > (ar->max_num_peers - 1)) {
258 ath11k_warn(ar->ab,
259 "failed to create peer due to insufficient peer entry resource in firmware\n");
260 return -ENOBUFS;
263 spin_lock_bh(&ar->ab->base_lock);
264 peer = ath11k_peer_find_by_pdev_idx(ar->ab, ar->pdev_idx, param->peer_addr);
265 if (peer) {
266 spin_unlock_bh(&ar->ab->base_lock);
267 return -EINVAL;
269 spin_unlock_bh(&ar->ab->base_lock);
271 ret = ath11k_wmi_send_peer_create_cmd(ar, param);
272 if (ret) {
273 ath11k_warn(ar->ab,
274 "failed to send peer create vdev_id %d ret %d\n",
275 param->vdev_id, ret);
276 return ret;
279 ret = ath11k_wait_for_peer_created(ar, param->vdev_id,
280 param->peer_addr);
281 if (ret)
282 return ret;
284 spin_lock_bh(&ar->ab->base_lock);
286 peer = ath11k_peer_find(ar->ab, param->vdev_id, param->peer_addr);
287 if (!peer) {
288 spin_unlock_bh(&ar->ab->base_lock);
289 ath11k_warn(ar->ab, "failed to find peer %pM on vdev %i after creation\n",
290 param->peer_addr, param->vdev_id);
292 reinit_completion(&ar->peer_delete_done);
294 ret = ath11k_wmi_send_peer_delete_cmd(ar, param->peer_addr,
295 param->vdev_id);
296 if (ret) {
297 ath11k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
298 param->vdev_id, param->peer_addr);
299 return ret;
302 ret = ath11k_wait_for_peer_delete_done(ar, param->vdev_id,
303 param->peer_addr);
304 if (ret)
305 return ret;
307 return -ENOENT;
310 peer->pdev_idx = ar->pdev_idx;
311 peer->sta = sta;
312 arvif->ast_hash = peer->ast_hash;
314 peer->sec_type = HAL_ENCRYPT_TYPE_OPEN;
315 peer->sec_type_grp = HAL_ENCRYPT_TYPE_OPEN;
317 ar->num_peers++;
319 spin_unlock_bh(&ar->ab->base_lock);
321 return 0;