1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
|
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
/*
* KUnit tests for channel helper functions
*
* Copyright (C) 2024-2025 Intel Corporation
*/
#include <kunit/test.h>
#include <kunit/test-bug.h>
#include "utils.h"
#include <linux/device.h>
#include "fw/api/scan.h"
#include "fw/api/mac-cfg.h"
#include "iwl-trans.h"
#include "mld.h"
#include "iface.h"
#include "link.h"
#include "phy.h"
#include "sta.h"
int iwlmld_kunit_test_init(struct kunit *test)
{
struct iwl_mld *mld;
struct iwl_trans *trans;
const struct iwl_rf_cfg *cfg;
struct iwl_fw *fw;
struct ieee80211_hw *hw;
KUNIT_ALLOC_AND_ASSERT(test, trans);
KUNIT_ALLOC_AND_ASSERT(test, trans->dev);
KUNIT_ALLOC_AND_ASSERT(test, cfg);
KUNIT_ALLOC_AND_ASSERT(test, fw);
KUNIT_ALLOC_AND_ASSERT(test, hw);
KUNIT_ALLOC_AND_ASSERT(test, hw->wiphy);
mutex_init(&hw->wiphy->mtx);
/* Allocate and initialize the mld structure */
KUNIT_ALLOC_AND_ASSERT(test, mld);
iwl_construct_mld(mld, trans, cfg, fw, hw, NULL);
fw->ucode_capa.num_stations = IWL_STATION_COUNT_MAX;
fw->ucode_capa.num_links = IWL_FW_MAX_LINK_ID + 1;
mld->fwrt.trans = trans;
mld->fwrt.fw = fw;
mld->fwrt.dev = trans->dev;
/* TODO: add priv_size to hw allocation and setup hw->priv to enable
* testing mac80211 callbacks
*/
KUNIT_ALLOC_AND_ASSERT(test, mld->nvm_data);
KUNIT_ALLOC_AND_ASSERT_SIZE(test, mld->scan.cmd,
sizeof(struct iwl_scan_req_umac_v17));
mld->scan.cmd_size = sizeof(struct iwl_scan_req_umac_v17);
/* This is not the state at the end of the regular opmode_start,
* but it is more common to need it. Explicitly undo this if needed.
*/
mld->trans->state = IWL_TRANS_FW_ALIVE;
mld->fw_status.running = true;
/* Avoid passing mld struct around */
test->priv = mld;
return 0;
}
IWL_MLD_ALLOC_FN(link, bss_conf)
static void iwlmld_kunit_init_link(struct ieee80211_vif *vif,
struct ieee80211_bss_conf *link,
struct iwl_mld_link *mld_link, int link_id)
{
struct kunit *test = kunit_get_current_test();
struct iwl_mld *mld = test->priv;
struct iwl_mld_vif *mld_vif = iwl_mld_vif_from_mac80211(vif);
int ret;
/* setup mac80211 link */
rcu_assign_pointer(vif->link_conf[link_id], link);
link->link_id = link_id;
link->vif = vif;
link->beacon_int = 100;
link->dtim_period = 3;
link->qos = true;
/* and mld_link */
ret = iwl_mld_allocate_link_fw_id(mld, &mld_link->fw_id, link);
KUNIT_ASSERT_EQ(test, ret, 0);
rcu_assign_pointer(mld_vif->link[link_id], mld_link);
rcu_assign_pointer(vif->link_conf[link_id], link);
}
IWL_MLD_ALLOC_FN(vif, vif)
/* Helper function to add and initialize a VIF for KUnit tests */
struct ieee80211_vif *iwlmld_kunit_add_vif(bool mlo, enum nl80211_iftype type)
{
struct kunit *test = kunit_get_current_test();
struct iwl_mld *mld = test->priv;
struct ieee80211_vif *vif;
struct iwl_mld_vif *mld_vif;
int ret;
/* TODO: support more types */
KUNIT_ASSERT_EQ(test, type, NL80211_IFTYPE_STATION);
KUNIT_ALLOC_AND_ASSERT_SIZE(test, vif,
sizeof(*vif) + sizeof(*mld_vif));
vif->type = type;
mld_vif = iwl_mld_vif_from_mac80211(vif);
mld_vif->mld = mld;
ret = iwl_mld_allocate_vif_fw_id(mld, &mld_vif->fw_id, vif);
KUNIT_ASSERT_EQ(test, ret, 0);
/* TODO: revisit (task=EHT) */
if (mlo)
return vif;
/* Initialize the default link */
iwlmld_kunit_init_link(vif, &vif->bss_conf, &mld_vif->deflink, 0);
return vif;
}
/* Use only for MLO vif */
struct ieee80211_bss_conf *
iwlmld_kunit_add_link(struct ieee80211_vif *vif, int link_id)
{
struct kunit *test = kunit_get_current_test();
struct ieee80211_bss_conf *link;
struct iwl_mld_link *mld_link;
KUNIT_ALLOC_AND_ASSERT(test, link);
KUNIT_ALLOC_AND_ASSERT(test, mld_link);
iwlmld_kunit_init_link(vif, link, mld_link, link_id);
vif->valid_links |= BIT(link_id);
return link;
}
struct ieee80211_chanctx_conf *
iwlmld_kunit_add_chanctx(const struct cfg80211_chan_def *def)
{
struct kunit *test = kunit_get_current_test();
struct iwl_mld *mld = test->priv;
struct ieee80211_chanctx_conf *ctx;
struct iwl_mld_phy *phy;
int fw_id;
KUNIT_ALLOC_AND_ASSERT_SIZE(test, ctx, sizeof(*ctx) + sizeof(*phy));
/* Setup the chanctx conf */
ctx->def = *def;
ctx->min_def = *def;
ctx->ap = *def;
/* and the iwl_mld_phy */
phy = iwl_mld_phy_from_mac80211(ctx);
fw_id = iwl_mld_allocate_fw_phy_id(mld);
KUNIT_ASSERT_GE(test, fw_id, 0);
phy->fw_id = fw_id;
phy->mld = mld;
phy->chandef = *def;
return ctx;
}
void iwlmld_kunit_assign_chanctx_to_link(struct ieee80211_vif *vif,
struct ieee80211_bss_conf *link,
struct ieee80211_chanctx_conf *ctx)
{
struct kunit *test = kunit_get_current_test();
struct iwl_mld *mld = test->priv;
struct iwl_mld_link *mld_link;
KUNIT_EXPECT_NULL(test, rcu_access_pointer(link->chanctx_conf));
rcu_assign_pointer(link->chanctx_conf, ctx);
lockdep_assert_wiphy(mld->wiphy);
mld_link = iwl_mld_link_from_mac80211(link);
KUNIT_EXPECT_NULL(test, rcu_access_pointer(mld_link->chan_ctx));
KUNIT_EXPECT_FALSE(test, mld_link->active);
rcu_assign_pointer(mld_link->chan_ctx, ctx);
mld_link->active = true;
if (ieee80211_vif_is_mld(vif))
vif->active_links |= BIT(link->link_id);
}
IWL_MLD_ALLOC_FN(link_sta, link_sta)
static void iwlmld_kunit_add_link_sta(struct ieee80211_sta *sta,
struct ieee80211_link_sta *link_sta,
struct iwl_mld_link_sta *mld_link_sta,
u8 link_id)
{
struct kunit *test = kunit_get_current_test();
struct iwl_mld_sta *mld_sta = iwl_mld_sta_from_mac80211(sta);
struct iwl_mld *mld = test->priv;
u8 fw_id;
int ret;
/* initialize mac80211's link_sta */
link_sta->link_id = link_id;
rcu_assign_pointer(sta->link[link_id], link_sta);
link_sta->sta = sta;
/* and the iwl_mld_link_sta */
ret = iwl_mld_allocate_link_sta_fw_id(mld, &fw_id, link_sta);
KUNIT_ASSERT_EQ(test, ret, 0);
mld_link_sta->fw_id = fw_id;
rcu_assign_pointer(mld_sta->link[link_id], mld_link_sta);
}
static struct ieee80211_link_sta *
iwlmld_kunit_alloc_link_sta(struct ieee80211_sta *sta, int link_id)
{
struct kunit *test = kunit_get_current_test();
struct ieee80211_link_sta *link_sta;
struct iwl_mld_link_sta *mld_link_sta;
/* Only valid for MLO */
KUNIT_ASSERT_TRUE(test, sta->valid_links);
KUNIT_ALLOC_AND_ASSERT(test, link_sta);
KUNIT_ALLOC_AND_ASSERT(test, mld_link_sta);
iwlmld_kunit_add_link_sta(sta, link_sta, mld_link_sta, link_id);
sta->valid_links |= BIT(link_id);
return link_sta;
}
/* Allocate and initialize a STA with the first link_sta */
static struct ieee80211_sta *
iwlmld_kunit_add_sta(struct ieee80211_vif *vif, int link_id)
{
struct kunit *test = kunit_get_current_test();
struct ieee80211_sta *sta;
struct iwl_mld_sta *mld_sta;
/* Allocate memory for ieee80211_sta with embedded iwl_mld_sta */
KUNIT_ALLOC_AND_ASSERT_SIZE(test, sta, sizeof(*sta) + sizeof(*mld_sta));
/* TODO: allocate and initialize the TXQs ? */
mld_sta = iwl_mld_sta_from_mac80211(sta);
mld_sta->vif = vif;
mld_sta->mld = test->priv;
/* TODO: adjust for internal stations */
mld_sta->sta_type = STATION_TYPE_PEER;
if (link_id >= 0) {
iwlmld_kunit_add_link_sta(sta, &sta->deflink,
&mld_sta->deflink, link_id);
sta->valid_links = BIT(link_id);
} else {
iwlmld_kunit_add_link_sta(sta, &sta->deflink,
&mld_sta->deflink, 0);
}
return sta;
}
/* Move s STA to a state */
static void iwlmld_kunit_move_sta_state(struct ieee80211_vif *vif,
struct ieee80211_sta *sta,
enum ieee80211_sta_state state)
{
struct kunit *test = kunit_get_current_test();
struct iwl_mld_sta *mld_sta;
struct iwl_mld_vif *mld_vif;
/* The sta will be removed automatically at the end of the test */
KUNIT_ASSERT_NE(test, state, IEEE80211_STA_NOTEXIST);
mld_sta = iwl_mld_sta_from_mac80211(sta);
mld_sta->sta_state = state;
mld_vif = iwl_mld_vif_from_mac80211(mld_sta->vif);
mld_vif->authorized = state == IEEE80211_STA_AUTHORIZED;
if (vif->type == NL80211_IFTYPE_STATION && !sta->tdls)
mld_vif->ap_sta = sta;
}
struct ieee80211_sta *iwlmld_kunit_setup_sta(struct ieee80211_vif *vif,
enum ieee80211_sta_state state,
int link_id)
{
struct kunit *test = kunit_get_current_test();
struct ieee80211_sta *sta;
/* The sta will be removed automatically at the end of the test */
KUNIT_ASSERT_NE(test, state, IEEE80211_STA_NOTEXIST);
/* First - allocate and init the STA */
sta = iwlmld_kunit_add_sta(vif, link_id);
/* Now move it all the way to the wanted state */
for (enum ieee80211_sta_state _state = IEEE80211_STA_NONE;
_state <= state; _state++)
iwlmld_kunit_move_sta_state(vif, sta, state);
return sta;
}
static void iwlmld_kunit_set_vif_associated(struct ieee80211_vif *vif)
{
/* TODO: setup chanreq */
/* TODO setup capabilities */
vif->cfg.assoc = 1;
}
static struct ieee80211_vif *
iwlmld_kunit_setup_assoc(bool mlo, struct iwl_mld_kunit_link *assoc_link)
{
struct kunit *test = kunit_get_current_test();
struct iwl_mld *mld = test->priv;
struct ieee80211_vif *vif;
struct ieee80211_bss_conf *link;
struct ieee80211_chanctx_conf *chan_ctx;
KUNIT_ASSERT_TRUE(test, mlo || assoc_link->id == 0);
vif = iwlmld_kunit_add_vif(mlo, NL80211_IFTYPE_STATION);
if (mlo)
link = iwlmld_kunit_add_link(vif, assoc_link->id);
else
link = &vif->bss_conf;
chan_ctx = iwlmld_kunit_add_chanctx(assoc_link->chandef);
wiphy_lock(mld->wiphy);
iwlmld_kunit_assign_chanctx_to_link(vif, link, chan_ctx);
wiphy_unlock(mld->wiphy);
/* The AP sta will now be pointer to by mld_vif->ap_sta */
iwlmld_kunit_setup_sta(vif, IEEE80211_STA_AUTHORIZED, assoc_link->id);
iwlmld_kunit_set_vif_associated(vif);
return vif;
}
struct ieee80211_vif *
iwlmld_kunit_setup_mlo_assoc(u16 valid_links,
struct iwl_mld_kunit_link *assoc_link)
{
struct kunit *test = kunit_get_current_test();
struct ieee80211_vif *vif;
KUNIT_ASSERT_TRUE(test,
hweight16(valid_links) == 1 ||
hweight16(valid_links) == 2);
KUNIT_ASSERT_TRUE(test, valid_links & BIT(assoc_link->id));
vif = iwlmld_kunit_setup_assoc(true, assoc_link);
/* Add the other link, if applicable */
if (hweight16(valid_links) > 1) {
u8 other_link_id = ffs(valid_links & ~BIT(assoc_link->id)) - 1;
iwlmld_kunit_add_link(vif, other_link_id);
}
return vif;
}
struct ieee80211_vif *
iwlmld_kunit_setup_non_mlo_assoc(struct iwl_mld_kunit_link *assoc_link)
{
return iwlmld_kunit_setup_assoc(false, assoc_link);
}
struct iwl_rx_packet *
_iwl_mld_kunit_create_pkt(const void *notif, size_t notif_sz)
{
struct kunit *test = kunit_get_current_test();
struct iwl_rx_packet *pkt;
KUNIT_ALLOC_AND_ASSERT_SIZE(test, pkt, sizeof(pkt) + notif_sz);
memcpy(pkt->data, notif, notif_sz);
pkt->len_n_flags = cpu_to_le32(sizeof(pkt->hdr) + notif_sz);
return pkt;
}
struct ieee80211_vif *iwlmld_kunit_assoc_emlsr(struct iwl_mld_kunit_link *link1,
struct iwl_mld_kunit_link *link2)
{
struct kunit *test = kunit_get_current_test();
struct iwl_mld *mld = test->priv;
struct ieee80211_vif *vif;
struct ieee80211_bss_conf *link;
struct ieee80211_chanctx_conf *chan_ctx;
struct ieee80211_sta *sta;
struct iwl_mld_vif *mld_vif;
u16 valid_links = BIT(link1->id) | BIT(link2->id);
KUNIT_ASSERT_TRUE(test, hweight16(valid_links) == 2);
vif = iwlmld_kunit_setup_mlo_assoc(valid_links, link1);
mld_vif = iwl_mld_vif_from_mac80211(vif);
/* Activate second link */
wiphy_lock(mld->wiphy);
link = wiphy_dereference(mld->wiphy, vif->link_conf[link2->id]);
KUNIT_EXPECT_NOT_NULL(test, link);
chan_ctx = iwlmld_kunit_add_chanctx(link2->chandef);
iwlmld_kunit_assign_chanctx_to_link(vif, link, chan_ctx);
wiphy_unlock(mld->wiphy);
/* And other link sta */
sta = mld_vif->ap_sta;
KUNIT_EXPECT_NOT_NULL(test, sta);
iwlmld_kunit_alloc_link_sta(sta, link2->id);
return vif;
}
struct element *iwlmld_kunit_gen_element(u8 id, const void *data, size_t len)
{
struct kunit *test = kunit_get_current_test();
struct element *elem;
KUNIT_ALLOC_AND_ASSERT_SIZE(test, elem, sizeof(*elem) + len);
elem->id = id;
elem->datalen = len;
memcpy(elem->data, data, len);
return elem;
}
struct iwl_mld_phy *iwlmld_kunit_get_phy_of_link(struct ieee80211_vif *vif,
u8 link_id)
{
struct kunit *test = kunit_get_current_test();
struct iwl_mld *mld = test->priv;
struct ieee80211_chanctx_conf *chanctx;
struct ieee80211_bss_conf *link =
wiphy_dereference(mld->wiphy, vif->link_conf[link_id]);
KUNIT_EXPECT_NOT_NULL(test, link);
chanctx = wiphy_dereference(mld->wiphy, link->chanctx_conf);
KUNIT_EXPECT_NOT_NULL(test, chanctx);
return iwl_mld_phy_from_mac80211(chanctx);
}
static const struct chandef_case {
const char *desc;
const struct cfg80211_chan_def *chandef;
} chandef_cases[] = {
#define CHANDEF(c, ...) { .desc = "chandef " #c " valid", .chandef = &c, },
CHANDEF_LIST
#undef CHANDEF
};
KUNIT_ARRAY_PARAM_DESC(chandef, chandef_cases, desc);
static void test_iwl_mld_chandef_valid(struct kunit *test)
{
const struct chandef_case *params = test->param_value;
KUNIT_EXPECT_EQ(test, true, cfg80211_chandef_valid(params->chandef));
}
static struct kunit_case chandef_test_cases[] = {
KUNIT_CASE_PARAM(test_iwl_mld_chandef_valid, chandef_gen_params),
{}
};
static struct kunit_suite chandef_tests = {
.name = "iwlmld_valid_test_chandefs",
.test_cases = chandef_test_cases,
};
kunit_test_suite(chandef_tests);
|