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 222 peer->link_id = arsta->link_id; 223 224 /* Fill ML info into created peer */ 225 if (sta->mlo) { 226 ml_peer_id = ahsta->ml_peer_id; 227 peer->ml_id = ml_peer_id | ATH12K_PEER_ML_ID_VALID; 228 ether_addr_copy(peer->ml_addr, sta->addr); 229 230 /* the assoc link is considered primary for now */ 231 peer->primary_link = arsta->is_assoc_link; 232 peer->mlo = true; 233 } else { 234 peer->ml_id = ATH12K_MLO_PEER_ID_INVALID; 235 peer->primary_link = true; 236 peer->mlo = false; 237 } 238 } 239 240 ar->num_peers++; 241 242 spin_unlock_bh(&dp->dp_lock); 243 244 if (arvif->link_id < IEEE80211_MLD_MAX_NUM_LINKS) { 245 ret = ath12k_dp_link_peer_assign(ath12k_ab_to_dp(ar->ab), 246 &(ath12k_ar_to_ah(ar)->dp_hw), 247 arvif->vdev_id, sta, 248 (u8 *)arg->peer_addr, link_id, 249 ar->hw_link_id); 250 } 251 252 return ret; 253 } 254 255 u16 ath12k_peer_ml_alloc(struct ath12k_hw *ah) 256 { 257 u16 ml_peer_id; 258 259 lockdep_assert_wiphy(ah->hw->wiphy); 260 261 for (ml_peer_id = 0; ml_peer_id < ATH12K_MAX_MLO_PEERS; ml_peer_id++) { 262 if (test_bit(ml_peer_id, ah->free_ml_peer_id_map)) 263 continue; 264 265 set_bit(ml_peer_id, ah->free_ml_peer_id_map); 266 break; 267 } 268 269 if (ml_peer_id == ATH12K_MAX_MLO_PEERS) 270 ml_peer_id = ATH12K_MLO_PEER_ID_INVALID; 271 272 return ml_peer_id; 273 } 274 275 int ath12k_peer_mlo_link_peers_delete(struct ath12k_vif *ahvif, struct ath12k_sta *ahsta) 276 { 277 struct ieee80211_sta *sta = ath12k_ahsta_to_sta(ahsta); 278 struct ath12k_hw *ah = ahvif->ah; 279 struct ath12k_link_vif *arvif; 280 struct ath12k_link_sta *arsta; 281 unsigned long links; 282 struct ath12k *ar; 283 int ret, err_ret = 0; 284 u8 link_id; 285 286 lockdep_assert_wiphy(ah->hw->wiphy); 287 288 if (!sta->mlo) 289 return -EINVAL; 290 291 /* FW expects delete of all link peers at once before waiting for reception 292 * of peer unmap or delete responses 293 */ 294 links = ahsta->links_map; 295 for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) { 296 arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]); 297 arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]); 298 if (!arvif || !arsta) 299 continue; 300 301 ar = arvif->ar; 302 if (!ar) 303 continue; 304 305 ath12k_dp_peer_cleanup(ar, arvif->vdev_id, arsta->addr); 306 307 ath12k_dp_link_peer_unassign(ath12k_ab_to_dp(ar->ab), 308 &(ath12k_ar_to_ah(ar)->dp_hw), 309 arvif->vdev_id, arsta->addr, 310 ar->hw_link_id); 311 312 ret = ath12k_peer_delete_send(ar, arvif->vdev_id, arsta->addr); 313 if (ret) { 314 ath12k_warn(ar->ab, 315 "failed to delete peer vdev_id %d addr %pM ret %d\n", 316 arvif->vdev_id, arsta->addr, ret); 317 err_ret = ret; 318 continue; 319 } 320 } 321 322 /* Ensure all link peers are deleted and unmapped */ 323 links = ahsta->links_map; 324 for_each_set_bit(link_id, &links, IEEE80211_MLD_MAX_NUM_LINKS) { 325 arvif = wiphy_dereference(ah->hw->wiphy, ahvif->link[link_id]); 326 arsta = wiphy_dereference(ah->hw->wiphy, ahsta->link[link_id]); 327 if (!arvif || !arsta) 328 continue; 329 330 ar = arvif->ar; 331 if (!ar) 332 continue; 333 334 ret = ath12k_wait_for_peer_delete_done(ar, arvif->vdev_id, arsta->addr); 335 if (ret) { 336 err_ret = ret; 337 continue; 338 } 339 ar->num_peers--; 340 } 341 342 return err_ret; 343 } 344 345 static int ath12k_link_sta_rhash_insert(struct ath12k_base *ab, 346 struct ath12k_link_sta *arsta) 347 { 348 struct ath12k_link_sta *tmp; 349 350 lockdep_assert_held(&ab->base_lock); 351 352 tmp = rhashtable_lookup_get_insert_fast(ab->rhead_sta_addr, &arsta->rhash_addr, 353 ab->rhash_sta_addr_param); 354 if (!tmp) 355 return 0; 356 else if (IS_ERR(tmp)) 357 return PTR_ERR(tmp); 358 else 359 return -EEXIST; 360 } 361 362 static int ath12k_link_sta_rhash_remove(struct ath12k_base *ab, 363 struct ath12k_link_sta *arsta) 364 { 365 int ret; 366 367 lockdep_assert_held(&ab->base_lock); 368 369 ret = rhashtable_remove_fast(ab->rhead_sta_addr, &arsta->rhash_addr, 370 ab->rhash_sta_addr_param); 371 if (ret && ret != -ENOENT) 372 return ret; 373 374 return 0; 375 } 376 377 int ath12k_link_sta_rhash_add(struct ath12k_base *ab, 378 struct ath12k_link_sta *arsta) 379 { 380 int ret; 381 382 lockdep_assert_held(&ab->base_lock); 383 384 ret = ath12k_link_sta_rhash_insert(ab, arsta); 385 if (ret) 386 ath12k_warn(ab, "failed to add arsta %pM in rhash_addr ret %d\n", 387 arsta->addr, ret); 388 389 return ret; 390 } 391 392 void ath12k_link_sta_rhash_delete(struct ath12k_base *ab, 393 struct ath12k_link_sta *arsta) 394 { 395 /* 396 * Return type of this function is void since there is nothing to be 397 * done in failure case 398 */ 399 int ret; 400 401 lockdep_assert_held(&ab->base_lock); 402 403 ret = ath12k_link_sta_rhash_remove(ab, arsta); 404 if (ret) 405 ath12k_warn(ab, 406 "failed to remove arsta %pM in rhash_addr ret %d\n", 407 arsta->addr, ret); 408 } 409 410 int ath12k_link_sta_rhash_tbl_init(struct ath12k_base *ab) 411 { 412 struct rhashtable_params *param; 413 struct rhashtable *rhash_addr_tbl; 414 int ret; 415 416 rhash_addr_tbl = kzalloc(sizeof(*ab->rhead_sta_addr), GFP_KERNEL); 417 if (!rhash_addr_tbl) 418 return -ENOMEM; 419 420 param = &ab->rhash_sta_addr_param; 421 422 param->key_offset = offsetof(struct ath12k_link_sta, addr); 423 param->head_offset = offsetof(struct ath12k_link_sta, rhash_addr); 424 param->key_len = sizeof_field(struct ath12k_link_sta, addr); 425 param->automatic_shrinking = true; 426 param->nelem_hint = ab->num_radios * ath12k_core_get_max_peers_per_radio(ab); 427 428 ret = rhashtable_init(rhash_addr_tbl, param); 429 if (ret) { 430 ath12k_warn(ab, "failed to init peer addr rhash table %d\n", 431 ret); 432 goto err_free; 433 } 434 435 ab->rhead_sta_addr = rhash_addr_tbl; 436 437 return 0; 438 439 err_free: 440 kfree(rhash_addr_tbl); 441 442 return ret; 443 } 444 445 void ath12k_link_sta_rhash_tbl_destroy(struct ath12k_base *ab) 446 { 447 rhashtable_destroy(ab->rhead_sta_addr); 448 kfree(ab->rhead_sta_addr); 449 ab->rhead_sta_addr = NULL; 450 } 451 452 struct ath12k_link_sta *ath12k_link_sta_find_by_addr(struct ath12k_base *ab, 453 const u8 *addr) 454 { 455 lockdep_assert_held(&ab->base_lock); 456 457 return rhashtable_lookup_fast(ab->rhead_sta_addr, addr, 458 ab->rhash_sta_addr_param); 459 } 460