1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2017-2026 Morse Micro 4 */ 5 #include <linux/slab.h> 6 #include <linux/timer.h> 7 #include "core.h" 8 #include "mac.h" 9 #include "bus.h" 10 #include "rc.h" 11 12 #define MM81X_RC_BW_TO_MMRC_BW(X) \ 13 (((X) == 1) ? MMRC_BW_1MHZ : \ 14 ((X) == 2) ? MMRC_BW_2MHZ : \ 15 ((X) == 4) ? MMRC_BW_4MHZ : \ 16 ((X) == 8) ? MMRC_BW_8MHZ : \ 17 MMRC_BW_2MHZ) 18 19 static void mm81x_rc_work(struct work_struct *work) 20 { 21 struct mm81x_rc *mrc = container_of(work, struct mm81x_rc, work); 22 struct list_head *pos; 23 24 spin_lock_bh(&mrc->lock); 25 26 list_for_each(pos, &mrc->stas) { 27 struct mm81x_rc_sta *mrc_sta = 28 container_of(pos, struct mm81x_rc_sta, list); 29 unsigned long now = jiffies; 30 31 mrc_sta->last_update = now; 32 33 mmrc_update(mrc_sta->tb); 34 } 35 36 spin_unlock_bh(&mrc->lock); 37 38 mod_timer(&mrc->timer, jiffies + msecs_to_jiffies(100)); 39 } 40 41 static void mm81x_rc_timer(struct timer_list *t) 42 { 43 struct mm81x_rc *mrc = timer_container_of(mrc, t, timer); 44 struct mm81x *mors = mrc->mors; 45 46 queue_work(mors->net_wq, &mors->mrc.work); 47 } 48 49 void mm81x_rc_init(struct mm81x *mors) 50 { 51 INIT_LIST_HEAD(&mors->mrc.stas); 52 spin_lock_init(&mors->mrc.lock); 53 54 INIT_WORK(&mors->mrc.work, mm81x_rc_work); 55 timer_setup(&mors->mrc.timer, mm81x_rc_timer, 0); 56 57 mors->mrc.mors = mors; 58 mod_timer(&mors->mrc.timer, jiffies + msecs_to_jiffies(100)); 59 } 60 61 void mm81x_rc_deinit(struct mm81x *mors) 62 { 63 timer_shutdown_sync(&mors->mrc.timer); 64 cancel_work_sync(&mors->mrc.work); 65 } 66 67 static void mm81x_rc_sta_config_guard_per_bw(struct ieee80211_sta *sta, 68 struct mmrc_sta_capabilities *caps) 69 { 70 caps->guard = BIT(MMRC_GUARD_LONG); 71 72 if (caps->bandwidth & BIT(MMRC_BW_1MHZ)) { 73 caps->sgi_per_bw |= SGI_PER_BW(MMRC_BW_1MHZ); 74 caps->guard |= BIT(MMRC_GUARD_SHORT); 75 } 76 77 if (caps->bandwidth & BIT(MMRC_BW_2MHZ)) { 78 caps->sgi_per_bw |= SGI_PER_BW(MMRC_BW_2MHZ); 79 caps->guard |= BIT(MMRC_GUARD_SHORT); 80 } 81 82 if (caps->bandwidth & BIT(MMRC_BW_4MHZ)) { 83 caps->sgi_per_bw |= SGI_PER_BW(MMRC_BW_4MHZ); 84 caps->guard |= BIT(MMRC_GUARD_SHORT); 85 } 86 87 if (caps->bandwidth & BIT(MMRC_BW_8MHZ)) { 88 caps->sgi_per_bw |= SGI_PER_BW(MMRC_BW_8MHZ); 89 caps->guard |= BIT(MMRC_GUARD_SHORT); 90 } 91 } 92 93 static void mm81x_rc_sta_add_s1g_sta_caps(struct mm81x *mors, 94 struct mmrc_sta_capabilities *caps, 95 struct ieee80211_sta_s1g_cap *s1g_cap) 96 { 97 int nss_idx = 0; 98 u8 rx_mcs = s1g_cap->nss_mcs[0] & 0x3; /* 1SS */ 99 u8 tx_mcs = (s1g_cap->nss_mcs[2] >> 1) & 0x3; /* 1SS */ 100 u8 mcs = min(rx_mcs, tx_mcs); 101 102 switch (mcs) { 103 case IEEE80211_VHT_MCS_SUPPORT_0_9: /* VHT 9 -> S1G 9 */ 104 caps->rates |= BIT(MMRC_MCS9) | BIT(MMRC_MCS8); 105 fallthrough; 106 case IEEE80211_VHT_MCS_SUPPORT_0_8: /* VHT 8 -> S1G 7 */ 107 caps->rates |= BIT(MMRC_MCS7) | BIT(MMRC_MCS6) | 108 BIT(MMRC_MCS5) | BIT(MMRC_MCS4) | BIT(MMRC_MCS3); 109 fallthrough; 110 case IEEE80211_VHT_MCS_SUPPORT_0_7: /* VHT 7 -> S1G 2 */ 111 caps->rates |= BIT(MMRC_MCS2) | BIT(MMRC_MCS1) | 112 BIT(MMRC_MCS0) | BIT(MMRC_MCS10); 113 caps->spatial_streams |= (BIT(nss_idx) & 0x0F); 114 break; 115 116 default: 117 dev_warn(mors->dev, "Invalid MCS encoding 0x%02x for stream %d", 118 mcs, nss_idx); 119 } 120 } 121 122 int mm81x_rc_sta_add(struct mm81x *mors, struct ieee80211_vif *vif, 123 struct ieee80211_sta *sta) 124 { 125 struct ieee80211_sta_s1g_cap *s1g_cap = &sta->deflink.s1g_cap; 126 struct mm81x_sta *msta = (struct mm81x_sta *)sta->drv_priv; 127 struct mmrc_sta_capabilities caps; 128 int oper_bw_mhz = cfg80211_chandef_get_width(&mors->chandef); 129 size_t table_mem_size; 130 struct mmrc_table *tb; 131 132 memset(&caps, 0, sizeof(caps)); 133 134 mm81x_rc_sta_add_s1g_sta_caps(mors, &caps, s1g_cap); 135 136 /* Configure STA for support up to 8MHZ */ 137 while (oper_bw_mhz > 0) { 138 caps.bandwidth |= BIT(MM81X_RC_BW_TO_MMRC_BW(oper_bw_mhz)); 139 oper_bw_mhz >>= 1; 140 } 141 142 /* Configure STA for short and long guard */ 143 mm81x_rc_sta_config_guard_per_bw(sta, &caps); 144 145 /* Set max rates */ 146 if (mors->hw->max_rates > 0 && 147 mors->hw->max_rates < IEEE80211_TX_MAX_RATES) 148 caps.max_rates = mors->hw->max_rates; 149 else 150 caps.max_rates = IEEE80211_TX_MAX_RATES; 151 152 /* Set max reties */ 153 if (mors->hw->max_rate_tries >= MMRC_MIN_CHAIN_ATTEMPTS && 154 mors->hw->max_rate_tries < MMRC_MAX_CHAIN_ATTEMPTS) 155 caps.max_retries = mors->hw->max_rate_tries; 156 else 157 caps.max_retries = MMRC_MAX_CHAIN_ATTEMPTS; 158 159 WARN_ON(msta->rc.tb); 160 table_mem_size = mmrc_memory_required_for_caps(&caps); 161 tb = kzalloc(table_mem_size, GFP_KERNEL); 162 if (!tb) 163 return -ENOMEM; 164 165 /* Initialise the STA rate control table */ 166 mmrc_sta_init(tb, &caps, msta->avg_rssi); 167 168 spin_lock_bh(&mors->mrc.lock); 169 kfree(msta->rc.tb); 170 msta->rc.tb = tb; 171 list_add(&msta->rc.list, &mors->mrc.stas); 172 msta->rc.last_update = jiffies; 173 spin_unlock_bh(&mors->mrc.lock); 174 175 return 0; 176 } 177 178 void mm81x_rc_sta_remove(struct mm81x *mors, struct ieee80211_sta *sta) 179 { 180 struct mm81x_sta *msta = (struct mm81x_sta *)sta->drv_priv; 181 182 spin_lock_bh(&mors->mrc.lock); 183 if (msta->rc.tb) { 184 list_del_init(&msta->rc.list); 185 kfree(msta->rc.tb); 186 msta->rc.tb = NULL; 187 } 188 spin_unlock_bh(&mors->mrc.lock); 189 } 190 191 static void mm81x_rc_sta_fill_basic_rates(struct mm81x_skb_tx_info *tx_info, 192 struct ieee80211_tx_info *info, 193 int tx_bw) 194 { 195 int i; 196 enum dot11_bandwidth bw_idx = mm81x_ratecode_bw_mhz_to_bw_index(tx_bw); 197 enum mm81x_rate_preamble pream = MM81X_RATE_PREAMBLE_S1G_SHORT; 198 199 mm81x_ratecode_mcs_index_set(&tx_info->rates[0].mm81x_ratecode, 0); 200 mm81x_ratecode_nss_index_set(&tx_info->rates[0].mm81x_ratecode, 201 NSS_TO_NSS_IDX(1)); 202 mm81x_ratecode_bw_index_set(&tx_info->rates[0].mm81x_ratecode, bw_idx); 203 if (bw_idx == DOT11_BANDWIDTH_1MHZ) 204 pream = MM81X_RATE_PREAMBLE_S1G_1M; 205 mm81x_ratecode_preamble_set(&tx_info->rates[0].mm81x_ratecode, pream); 206 tx_info->rates[0].count = 4; 207 208 for (i = 1; i < IEEE80211_TX_MAX_RATES; i++) 209 tx_info->rates[i].count = 0; 210 211 info->control.rates[0].idx = 0; 212 info->control.rates[0].count = tx_info->rates[0].count; 213 info->control.rates[0].flags = 0; 214 info->control.rates[1].idx = -1; 215 } 216 217 static int mm81x_rc_sta_get_rates(struct mm81x *mors, struct mm81x_sta *msta, 218 struct mmrc_rate_table *rates, size_t size) 219 { 220 int ret = -ENOENT; 221 struct list_head *pos; 222 223 spin_lock_bh(&mors->mrc.lock); 224 list_for_each(pos, &mors->mrc.stas) { 225 struct mm81x_rc_sta *mrc_sta = 226 list_entry(pos, struct mm81x_rc_sta, list); 227 228 if (&msta->rc == mrc_sta) { 229 ret = 0; 230 mmrc_get_rates(msta->rc.tb, rates, size); 231 break; 232 } 233 } 234 spin_unlock_bh(&mors->mrc.lock); 235 236 return ret; 237 } 238 239 static bool mm81x_rc_use_basic_rates(struct ieee80211_sta *sta, 240 struct sk_buff *skb, 241 struct ieee80211_hdr *hdr) 242 { 243 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 244 245 if (!sta) 246 return true; 247 248 if (ieee80211_is_qos_nullfunc(hdr->frame_control) || 249 ieee80211_is_nullfunc(hdr->frame_control)) 250 return true; 251 252 if (!ieee80211_is_data_qos(hdr->frame_control)) 253 return true; 254 255 /* Use basic rates for EAPOL exchanges or when instructed */ 256 if (unlikely((skb->protocol == cpu_to_be16(ETH_P_PAE) || 257 info->flags & IEEE80211_TX_CTL_USE_MINRATE))) 258 return true; 259 260 return false; 261 } 262 263 void mm81x_rc_sta_fill_tx_rates(struct mm81x *mors, 264 struct mm81x_skb_tx_info *tx_info, 265 struct sk_buff *skb, struct ieee80211_sta *sta, 266 int tx_bw, bool rts_allowed) 267 { 268 int ret, i; 269 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 270 struct mm81x_sta *msta; 271 struct mmrc_rate_table rates; 272 struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb); 273 274 BUILD_BUG_ON((MMRC_BW_1MHZ != (enum mmrc_bw)DOT11_BANDWIDTH_1MHZ || 275 MMRC_BW_2MHZ != (enum mmrc_bw)DOT11_BANDWIDTH_2MHZ || 276 MMRC_BW_4MHZ != (enum mmrc_bw)DOT11_BANDWIDTH_4MHZ || 277 MMRC_BW_16MHZ != (enum mmrc_bw)DOT11_BANDWIDTH_16MHZ)); 278 279 memset(&info->control.rates, 0, sizeof(info->control.rates)); 280 memset(&info->status.rates, 0, sizeof(info->status.rates)); 281 mm81x_rc_sta_fill_basic_rates(tx_info, info, tx_bw); 282 283 /* Use basic rates for non data packets */ 284 if (mm81x_rc_use_basic_rates(sta, skb, hdr)) 285 return; 286 287 msta = (struct mm81x_sta *)sta->drv_priv; 288 if (!msta) 289 return; 290 291 ret = mm81x_rc_sta_get_rates(mors, msta, &rates, skb->len); 292 if (ret != 0) 293 return; 294 295 for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) { 296 info->control.rates[i].flags = 0; 297 if (rates.rates[i].rate != MMRC_MCS_UNUSED) { 298 u8 mcs = rates.rates[i].rate; 299 u8 nss_index = rates.rates[i].ss; 300 enum dot11_bandwidth bw_idx = 301 (enum dot11_bandwidth)rates.rates[i].bw; 302 enum mm81x_rate_preamble pream = 303 MM81X_RATE_PREAMBLE_S1G_SHORT; 304 305 mm81x_ratecode_bw_index_set( 306 &tx_info->rates[i].mm81x_ratecode, bw_idx); 307 mm81x_ratecode_mcs_index_set( 308 &tx_info->rates[i].mm81x_ratecode, mcs); 309 mm81x_ratecode_nss_index_set( 310 &tx_info->rates[i].mm81x_ratecode, nss_index); 311 if (bw_idx == DOT11_BANDWIDTH_1MHZ) 312 pream = MM81X_RATE_PREAMBLE_S1G_1M; 313 mm81x_ratecode_preamble_set( 314 &tx_info->rates[i].mm81x_ratecode, pream); 315 tx_info->rates[i].count = rates.rates[i].attempts; 316 317 if (rts_allowed && 318 (rates.rates[i].flags & BIT(MMRC_FLAGS_CTS_RTS))) { 319 mm81x_ratecode_enable_rts( 320 &tx_info->rates[i].mm81x_ratecode); 321 info->control.rates[i].flags |= 322 IEEE80211_TX_RC_USE_RTS_CTS; 323 } 324 325 if (rates.rates[i].guard == MMRC_GUARD_SHORT) { 326 mm81x_ratecode_enable_sgi( 327 &tx_info->rates[i].mm81x_ratecode); 328 info->control.rates[i].flags |= 329 IEEE80211_TX_RC_SHORT_GI; 330 } 331 332 /* Update skb tx_info */ 333 info->control.rates[i].idx = rates.rates[i].rate; 334 info->control.rates[i].count = rates.rates[i].attempts; 335 } else { 336 info->control.rates[i].idx = -1; 337 info->control.rates[i].count = 0; 338 tx_info->rates[i].count = 0; 339 } 340 } 341 } 342 343 static void mm81x_rc_sta_set_rates(struct mm81x *mors, struct mm81x_sta *msta, 344 struct mmrc_rate_table *rates, int attempts, 345 bool was_aggregated) 346 { 347 struct list_head *pos; 348 349 spin_lock_bh(&mors->mrc.lock); 350 list_for_each(pos, &mors->mrc.stas) { 351 struct mm81x_rc_sta *mrc_sta = 352 list_entry(pos, struct mm81x_rc_sta, list); 353 354 if (&msta->rc == mrc_sta) { 355 mmrc_feedback(msta->rc.tb, rates, attempts, 356 was_aggregated); 357 break; 358 } 359 } 360 spin_unlock_bh(&mors->mrc.lock); 361 } 362 363 void mm81x_rc_sta_feedback_rates(struct mm81x *mors, struct sk_buff *skb, 364 struct ieee80211_sta *sta, 365 struct mm81x_skb_tx_status *tx_sts, 366 int attempts) 367 { 368 int i; 369 u32 tx_airtime = 0; 370 struct mmrc_rate_table rates; 371 struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data; 372 struct ieee80211_tx_info *txi = IEEE80211_SKB_CB(skb); 373 struct ieee80211_tx_rate *r = &txi->status.rates[0]; 374 int count = min_t(int, MM81X_SKB_MAX_RATES, IEEE80211_TX_MAX_RATES); 375 struct mm81x_sta *msta = msta = (struct mm81x_sta *)sta->drv_priv; 376 377 /* Don't update rate info if basic rates were used */ 378 if (mm81x_rc_use_basic_rates(sta, skb, hdr)) 379 goto exit; 380 381 if (attempts <= 0) 382 /* Did we really send the packet? */ 383 goto exit; 384 385 for (i = 0; i < count; i++) { 386 rates.rates[i].rate = mm81x_ratecode_mcs_index_get( 387 tx_sts->rates[i].mm81x_ratecode); 388 rates.rates[i].ss = mm81x_ratecode_nss_index_get( 389 tx_sts->rates[i].mm81x_ratecode); 390 rates.rates[i].guard = 391 mm81x_ratecode_sgi_get(tx_sts->rates[i].mm81x_ratecode); 392 rates.rates[i].bw = mm81x_ratecode_bw_index_get( 393 tx_sts->rates[i].mm81x_ratecode); 394 rates.rates[i].flags = 395 mm81x_ratecode_rts_get(tx_sts->rates[i].mm81x_ratecode); 396 rates.rates[i].attempts = tx_sts->rates[i].count; 397 398 tx_airtime += 399 mmrc_calculate_rate_tx_time(&rates.rates[i], skb->len); 400 } 401 402 if (msta) { 403 /* 404 * Save the rate information. This will be used to update 405 * station's tx rate stats 406 */ 407 msta->last_sta_tx_rate.bw = rates.rates[0].bw; 408 msta->last_sta_tx_rate.rate = rates.rates[0].rate; 409 msta->last_sta_tx_rate.ss = rates.rates[0].ss; 410 msta->last_sta_tx_rate.guard = rates.rates[0].guard; 411 } 412 413 mm81x_rc_sta_set_rates(mors, msta, &rates, attempts, 414 !!(le32_to_cpu(tx_sts->flags) & 415 MM81X_TX_STATUS_WAS_AGGREGATED)); 416 417 ieee80211_sta_register_airtime(sta, tx_sts->tid, tx_airtime, 0); 418 419 exit: 420 ieee80211_tx_info_clear_status(txi); 421 422 if (!(le32_to_cpu(tx_sts->flags) & MM81X_TX_STATUS_FLAGS_NO_ACK) && 423 !(txi->flags & IEEE80211_TX_CTL_NO_ACK)) 424 txi->flags |= IEEE80211_TX_STAT_ACK; 425 426 if (le32_to_cpu(tx_sts->flags) & MM81X_TX_STATUS_FLAGS_PS_FILTERED) { 427 txi->flags |= IEEE80211_TX_STAT_TX_FILTERED; 428 429 /* 430 * Clear TX CTL AMPDU flag so that this frame gets rescheduled 431 * in ieee80211_handle_filtered_frame(). This flag will get set 432 * again by mac80211's tx path on rescheduling. 433 */ 434 txi->flags &= ~IEEE80211_TX_CTL_AMPDU; 435 if (msta) { 436 if (!msta->tx_ps_filter_en) 437 dev_dbg(mors->dev, "TX ps filter set sta[%pM]", 438 msta->addr); 439 msta->tx_ps_filter_en = true; 440 } 441 } 442 443 for (i = 0; i < count; i++) { 444 if (tx_sts->rates[i].count > 0) { 445 r[i].count = tx_sts->rates[i].count; 446 r[i].flags |= IEEE80211_TX_RC_MCS; 447 } else { 448 r[i].idx = -1; 449 } 450 } 451 452 /* single packet per A-MPDU (for now) */ 453 if (txi->flags & IEEE80211_TX_CTL_AMPDU) { 454 txi->flags |= IEEE80211_TX_STAT_AMPDU; 455 txi->status.ampdu_len = 1; 456 txi->status.ampdu_ack_len = 457 txi->flags & IEEE80211_TX_STAT_ACK ? 1 : 0; 458 } 459 460 /* 461 * Inform mac80211 that the SP (elicited by a PS-Poll or u-APSD) is 462 * over 463 */ 464 if (sta && (txi->flags & IEEE80211_TX_STATUS_EOSP)) { 465 txi->flags &= ~IEEE80211_TX_STATUS_EOSP; 466 ieee80211_sta_eosp(sta); 467 } 468 } 469 470 void mm81x_rc_sta_state_check(struct mm81x *mors, struct ieee80211_vif *vif, 471 struct ieee80211_sta *sta, 472 enum ieee80211_sta_state old_state, 473 enum ieee80211_sta_state new_state) 474 { 475 struct mm81x_sta *msta = (struct mm81x_sta *)sta->drv_priv; 476 477 /* Add to Morse RC STA list */ 478 if (old_state < new_state && new_state == IEEE80211_STA_ASSOC) { 479 /* Newly associated, add to RC */ 480 mm81x_rc_sta_add(mors, vif, sta); 481 } else if (old_state > new_state && (old_state == IEEE80211_STA_ASSOC || 482 old_state == IEEE80211_STA_AUTH)) { 483 /* Lost or failed association; remove from list */ 484 mm81x_rc_sta_remove(mors, sta); 485 } else if (old_state < new_state && old_state == IEEE80211_STA_NONE && 486 msta->rc.list.prev) { 487 /* 488 * Special case for driver warning issue causing a sta to be 489 * left on the list 490 */ 491 dev_dbg(mors->dev, "Remove stale sta from rc list"); 492 mm81x_rc_sta_remove(mors, sta); 493 } 494 } 495