xref: /linux/drivers/net/wireless/ath/ath12k/peer.c (revision 8bec01c80e798eca1ae7863cf29bc6befd759db7)
1 // SPDX-License-Identifier: BSD-3-Clause-Clear
2 /*
3  * Copyright (c) 2018-2021 The Linux Foundation. All rights reserved.
4  * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
5  */
6 
7 #include "core.h"
8 #include "peer.h"
9 #include "debug.h"
10 #include "debugfs.h"
11 
12 static int ath12k_wait_for_dp_link_peer_common(struct ath12k_base *ab, int vdev_id,
13 					       const u8 *addr, bool expect_mapped)
14 {
15 	int ret;
16 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
17 
18 	ret = wait_event_timeout(ab->peer_mapping_wq, ({
19 				bool mapped;
20 
21 				spin_lock_bh(&dp->dp_lock);
22 				mapped = !!ath12k_dp_link_peer_find_by_vdev_and_addr(dp,
23 										vdev_id,
24 										addr);
25 				spin_unlock_bh(&dp->dp_lock);
26 
27 				(mapped == expect_mapped ||
28 				 test_bit(ATH12K_FLAG_CRASH_FLUSH, &ab->dev_flags));
29 				}), 3 * HZ);
30 
31 	if (ret <= 0)
32 		return -ETIMEDOUT;
33 
34 	return 0;
35 }
36 
37 void ath12k_peer_cleanup(struct ath12k *ar, u32 vdev_id)
38 {
39 	struct ath12k_dp_link_peer *peer, *tmp;
40 	struct ath12k_base *ab = ar->ab;
41 	struct ath12k_dp *dp = ath12k_ab_to_dp(ab);
42 
43 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
44 
45 	spin_lock_bh(&dp->dp_lock);
46 	list_for_each_entry_safe(peer, tmp, &dp->peers, list) {
47 		if (peer->vdev_id != vdev_id)
48 			continue;
49 
50 		ath12k_warn(ab, "removing stale peer %pM from vdev_id %d\n",
51 			    peer->addr, vdev_id);
52 
53 		ath12k_dp_link_peer_free(peer);
54 		ar->num_peers--;
55 	}
56 
57 	spin_unlock_bh(&dp->dp_lock);
58 }
59 
60 static int ath12k_wait_for_peer_deleted(struct ath12k *ar, int vdev_id, const u8 *addr)
61 {
62 	return ath12k_wait_for_dp_link_peer_common(ar->ab, vdev_id, addr, false);
63 }
64 
65 int ath12k_wait_for_peer_delete_done(struct ath12k *ar, u32 vdev_id,
66 				     const u8 *addr)
67 {
68 	int ret;
69 	unsigned long time_left;
70 
71 	ret = ath12k_wait_for_peer_deleted(ar, vdev_id, addr);
72 	if (ret) {
73 		ath12k_warn(ar->ab, "failed wait for peer deleted");
74 		return ret;
75 	}
76 
77 	time_left = wait_for_completion_timeout(&ar->peer_delete_done,
78 						3 * HZ);
79 	if (time_left == 0) {
80 		ath12k_warn(ar->ab, "Timeout in receiving peer delete response\n");
81 		return -ETIMEDOUT;
82 	}
83 
84 	return 0;
85 }
86 
87 static int ath12k_peer_delete_send(struct ath12k *ar, u32 vdev_id, const u8 *addr)
88 {
89 	struct ath12k_base *ab = ar->ab;
90 	int ret;
91 
92 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
93 
94 	reinit_completion(&ar->peer_delete_done);
95 
96 	ret = ath12k_wmi_send_peer_delete_cmd(ar, addr, vdev_id);
97 	if (ret) {
98 		ath12k_warn(ab,
99 			    "failed to delete peer vdev_id %d addr %pM ret %d\n",
100 			    vdev_id, addr, ret);
101 		return ret;
102 	}
103 
104 	return 0;
105 }
106 
107 int ath12k_peer_delete(struct ath12k *ar, u32 vdev_id, u8 *addr)
108 {
109 	int ret;
110 
111 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
112 
113 	ath12k_dp_link_peer_unassign(ath12k_ab_to_dp(ar->ab),
114 				     &(ath12k_ar_to_ah(ar)->dp_hw), vdev_id,
115 				     addr, ar->hw_link_id);
116 
117 	ret = ath12k_peer_delete_send(ar, vdev_id, addr);
118 	if (ret)
119 		return ret;
120 
121 	ret = ath12k_wait_for_peer_delete_done(ar, vdev_id, addr);
122 	if (ret)
123 		return ret;
124 
125 	ar->num_peers--;
126 
127 	return 0;
128 }
129 
130 static int ath12k_wait_for_peer_created(struct ath12k *ar, int vdev_id, const u8 *addr)
131 {
132 	return ath12k_wait_for_dp_link_peer_common(ar->ab, vdev_id, addr, true);
133 }
134 
135 int ath12k_peer_create(struct ath12k *ar, struct ath12k_link_vif *arvif,
136 		       struct ieee80211_sta *sta,
137 		       struct ath12k_wmi_peer_create_arg *arg)
138 {
139 	struct ieee80211_vif *vif = ath12k_ahvif_to_vif(arvif->ahvif);
140 	struct ath12k_vif *ahvif = arvif->ahvif;
141 	struct ath12k_dp_link_vif *dp_link_vif;
142 	struct ath12k_link_sta *arsta;
143 	u8 link_id = arvif->link_id;
144 	struct ath12k_dp_link_peer *peer;
145 	struct ath12k_sta *ahsta;
146 	u16 ml_peer_id;
147 	int ret;
148 	struct ath12k_dp *dp = ath12k_ab_to_dp(ar->ab);
149 
150 	lockdep_assert_wiphy(ath12k_ar_to_hw(ar)->wiphy);
151 
152 	dp_link_vif = ath12k_dp_vif_to_dp_link_vif(&ahvif->dp_vif, link_id);
153 
154 	if (ar->num_peers > (ar->max_num_peers - 1)) {
155 		ath12k_warn(ar->ab,
156 			    "failed to create peer due to insufficient peer entry resource in firmware\n");
157 		return -ENOBUFS;
158 	}
159 
160 	spin_lock_bh(&dp->dp_lock);
161 	peer = ath12k_dp_link_peer_find_by_pdev_and_addr(dp, ar->pdev_idx,
162 							 arg->peer_addr);
163 	if (peer) {
164 		spin_unlock_bh(&dp->dp_lock);
165 		return -EINVAL;
166 	}
167 	spin_unlock_bh(&dp->dp_lock);
168 
169 	ret = ath12k_wmi_send_peer_create_cmd(ar, arg);
170 	if (ret) {
171 		ath12k_warn(ar->ab,
172 			    "failed to send peer create vdev_id %d ret %d\n",
173 			    arg->vdev_id, ret);
174 		return ret;
175 	}
176 
177 	ret = ath12k_wait_for_peer_created(ar, arg->vdev_id,
178 					   arg->peer_addr);
179 	if (ret)
180 		return ret;
181 
182 	spin_lock_bh(&dp->dp_lock);
183 
184 	peer = ath12k_dp_link_peer_find_by_vdev_and_addr(dp, arg->vdev_id,
185 							 arg->peer_addr);
186 	if (!peer) {
187 		spin_unlock_bh(&dp->dp_lock);
188 		ath12k_warn(ar->ab, "failed to find peer %pM on vdev %i after creation\n",
189 			    arg->peer_addr, arg->vdev_id);
190 
191 		reinit_completion(&ar->peer_delete_done);
192 
193 		ret = ath12k_wmi_send_peer_delete_cmd(ar, arg->peer_addr,
194 						      arg->vdev_id);
195 		if (ret) {
196 			ath12k_warn(ar->ab, "failed to delete peer vdev_id %d addr %pM\n",
197 				    arg->vdev_id, arg->peer_addr);
198 			return ret;
199 		}
200 
201 		ret = ath12k_wait_for_peer_delete_done(ar, arg->vdev_id,
202 						       arg->peer_addr);
203 		if (ret)
204 			return ret;
205 
206 		return -ENOENT;
207 	}
208 
209 	peer->pdev_idx = ar->pdev_idx;
210 	peer->sta = sta;
211 
212 	if (vif->type == NL80211_IFTYPE_STATION) {
213 		dp_link_vif->ast_hash = peer->ast_hash;
214 		dp_link_vif->ast_idx = peer->hw_peer_id;
215 	}
216 
217 	if (sta) {
218 		ahsta = ath12k_sta_to_ahsta(sta);
219 		arsta = wiphy_dereference(ath12k_ar_to_hw(ar)->wiphy,
220 					  ahsta->link[link_id]);
221 		/* TODO: Split DP related field usage to DP peer structure */
222 		arsta->tcl_metadata = u16_encode_bits(0, HTT_TCL_META_DATA_TYPE) |
223 				      u16_encode_bits(peer->peer_id,
224 						      HTT_TCL_META_DATA_PEER_ID) |
225 				      u16_encode_bits(0, HTT_TCL_META_DATA_VALID_HTT);
226 		arsta->ast_hash = peer->ast_hash;
227 		arsta->ast_idx = peer->hw_peer_id;
228 		peer->link_id = arsta->link_id;
229 
230 		/* Fill ML info into created peer */
231 		if (sta->mlo) {
232 			ml_peer_id = ahsta->ml_peer_id;
233 			peer->ml_id = ml_peer_id | ATH12K_PEER_ML_ID_VALID;
234 			ether_addr_copy(peer->ml_addr, sta->addr);
235 
236 			/* the assoc link is considered primary for now */
237 			peer->primary_link = arsta->is_assoc_link;
238 			peer->mlo = true;
239 		} else {
240 			peer->ml_id = ATH12K_MLO_PEER_ID_INVALID;
241 			peer->primary_link = true;
242 			peer->mlo = false;
243 		}
244 	}
245 
246 	ar->num_peers++;
247 
248 	spin_unlock_bh(&dp->dp_lock);
249 
250 	if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) {
251 		ret = ath12k_dp_link_peer_assign(ath12k_ab_to_dp(ar->ab),
252 						 &(ath12k_ar_to_ah(ar)->dp_hw),
253 						 arvif->vdev_id, sta,
254 						 (u8 *)arg->peer_addr, link_id,
255 						 ar->hw_link_id);
256 	}
257 
258 	if (vif->type == NL80211_IFTYPE_AP && peer->dp_peer)
259 		peer->dp_peer->ucast_ra_only = true;
260 
261 	return ret;
262 }
263 
264 u16 ath12k_peer_ml_alloc(struct ath12k_hw *ah)
265 {
266 	u16 ml_peer_id;
267 
268 	lockdep_assert_wiphy(ah->hw->wiphy);
269 
270 	for (ml_peer_id = 0; ml_peer_id < ATH12K_MAX_MLO_PEERS; ml_peer_id++) {
271 		if (test_bit(ml_peer_id, ah->free_ml_peer_id_map))
272 			continue;
273 
274 		set_bit(ml_peer_id, ah->free_ml_peer_id_map);
275 		break;
276 	}
277 
278 	if (ml_peer_id == ATH12K_MAX_MLO_PEERS)
279 		ml_peer_id = ATH12K_MLO_PEER_ID_INVALID;
280 
281 	return ml_peer_id;
282 }
283 
284 int ath12k_peer_mlo_link_peers_delete(struct ath12k_vif *ahvif, struct ath12k_sta *ahsta)
285 {
286 	struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta);
287 	struct ath12k_hw *ah = ahvif->ah;
288 	struct ath12k_link_vif *arvif;
289 	struct ath12k_link_sta *arsta;
290 	unsigned long links;
291 	struct ath12k *ar;
292 	int ret, err_ret = 0;
293 	u8 link_id;
294 
295 	lockdep_assert_wiphy(ah->hw->wiphy);
296 
297 	if (!sta->mlo)
298 		return -EINVAL;
299 
300 	/* FW expects delete of all link peers at once before waiting for reception
301 	 * of peer unmap or delete responses
302 	 */
303 	links = ahsta->links_map;
304 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
305 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
306 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
307 		if (!arvif || !arsta)
308 			continue;
309 
310 		ar = arvif->ar;
311 		if (!ar)
312 			continue;
313 
314 		ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr);
315 
316 		ath12k_dp_link_peer_unassign(ath12k_ab_to_dp(ar->ab),
317 					     &(ath12k_ar_to_ah(ar)->dp_hw),
318 					     arvif->vdev_id, arsta->addr,
319 					     ar->hw_link_id);
320 
321 		ret = ath12k_peer_delete_send(ar, arvif->vdev_id, arsta->addr);
322 		if (ret) {
323 			ath12k_warn(ar->ab,
324 				    "failed to delete peer vdev_id %d addr %pM ret %d\n",
325 				    arvif->vdev_id, arsta->addr, ret);
326 			err_ret = ret;
327 			continue;
328 		}
329 	}
330 
331 	/* Ensure all link peers are deleted and unmapped */
332 	links = ahsta->links_map;
333 	for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) {
334 		arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]);
335 		arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]);
336 		if (!arvif || !arsta)
337 			continue;
338 
339 		ar = arvif->ar;
340 		if (!ar)
341 			continue;
342 
343 		ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id, arsta->addr);
344 		if (ret) {
345 			err_ret = ret;
346 			continue;
347 		}
348 		ar->num_peers--;
349 	}
350 
351 	return err_ret;
352 }
353 
354 static int ath12k_link_sta_rhash_insert(struct ath12k_base *ab,
355 					struct ath12k_link_sta *arsta)
356 {
357 	struct ath12k_link_sta *tmp;
358 
359 	lockdep_assert_held(&ab->base_lock);
360 
361 	tmp = rhashtable_lookup_get_insert_fast(ab->rhead_sta_addr, &arsta->rhash_addr,
362 						ab->rhash_sta_addr_param);
363 	if (!tmp)
364 		return 0;
365 	else if (IS_ERR(tmp))
366 		return PTR_ERR(tmp);
367 	else
368 		return -EEXIST;
369 }
370 
371 static int ath12k_link_sta_rhash_remove(struct ath12k_base *ab,
372 					struct ath12k_link_sta *arsta)
373 {
374 	int ret;
375 
376 	lockdep_assert_held(&ab->base_lock);
377 
378 	ret = rhashtable_remove_fast(ab->rhead_sta_addr, &arsta->rhash_addr,
379 				     ab->rhash_sta_addr_param);
380 	if (ret && ret != -ENOENT)
381 		return ret;
382 
383 	return 0;
384 }
385 
386 int ath12k_link_sta_rhash_add(struct ath12k_base *ab,
387 			      struct ath12k_link_sta *arsta)
388 {
389 	int ret;
390 
391 	lockdep_assert_held(&ab->base_lock);
392 
393 	ret = ath12k_link_sta_rhash_insert(ab, arsta);
394 	if (ret)
395 		ath12k_warn(ab, "failed to add arsta %pM in rhash_addr ret %d\n",
396 			    arsta->addr, ret);
397 
398 	return ret;
399 }
400 
401 void ath12k_link_sta_rhash_delete(struct ath12k_base *ab,
402 				  struct ath12k_link_sta *arsta)
403 {
404 	/*
405 	 * Return type of this function is void since there is nothing to be
406 	 * done in failure case
407 	 */
408 	int ret;
409 
410 	lockdep_assert_held(&ab->base_lock);
411 
412 	ret = ath12k_link_sta_rhash_remove(ab, arsta);
413 	if (ret)
414 		ath12k_warn(ab,
415 			    "failed to remove arsta %pM in rhash_addr ret %d\n",
416 			    arsta->addr, ret);
417 }
418 
419 int ath12k_link_sta_rhash_tbl_init(struct ath12k_base *ab)
420 {
421 	struct rhashtable_params *param;
422 	struct rhashtable *rhash_addr_tbl;
423 	int ret;
424 
425 	rhash_addr_tbl = kzalloc_obj(*ab->rhead_sta_addr);
426 	if (!rhash_addr_tbl)
427 		return -ENOMEM;
428 
429 	param = &ab->rhash_sta_addr_param;
430 
431 	param->key_offset = offsetof(struct ath12k_link_sta, addr);
432 	param->head_offset = offsetof(struct ath12k_link_sta, rhash_addr);
433 	param->key_len = sizeof_field(struct ath12k_link_sta, addr);
434 	param->automatic_shrinking = true;
435 	param->nelem_hint = ab->num_radios * ath12k_core_get_max_peers_per_radio(ab);
436 
437 	ret = rhashtable_init(rhash_addr_tbl, param);
438 	if (ret) {
439 		ath12k_warn(ab, "failed to init peer addr rhash table %d\n",
440 			    ret);
441 		goto err_free;
442 	}
443 
444 	ab->rhead_sta_addr = rhash_addr_tbl;
445 
446 	return 0;
447 
448 err_free:
449 	kfree(rhash_addr_tbl);
450 
451 	return ret;
452 }
453 
454 void ath12k_link_sta_rhash_tbl_destroy(struct ath12k_base *ab)
455 {
456 	if (!ab->rhead_sta_addr)
457 		return;
458 
459 	rhashtable_destroy(ab->rhead_sta_addr);
460 	kfree(ab->rhead_sta_addr);
461 	ab->rhead_sta_addr = NULL;
462 }
463 
464 struct ath12k_link_sta *ath12k_link_sta_find_by_addr(struct ath12k_base *ab,
465 						     const u8 *addr)
466 {
467 	lockdep_assert_held(&ab->base_lock);
468 
469 	return rhashtable_lookup_fast(ab->rhead_sta_addr, addr,
470 				      ab->rhash_sta_addr_param);
471 }
472