1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause 2 /* 3 * Copyright (C) 2012-2014, 2018-2020 Intel Corporation 4 * Copyright (C) 2013-2015 Intel Mobile Communications GmbH 5 * Copyright (C) 2016-2017 Intel Deutschland GmbH 6 */ 7 #include <asm/unaligned.h> 8 #include <linux/etherdevice.h> 9 #include <linux/skbuff.h> 10 #include "iwl-trans.h" 11 #include "mvm.h" 12 #include "fw-api.h" 13 14 /* 15 * iwl_mvm_rx_rx_phy_cmd - REPLY_RX_PHY_CMD handler 16 * 17 * Copies the phy information in mvm->last_phy_info, it will be used when the 18 * actual data will come from the fw in the next packet. 19 */ 20 void iwl_mvm_rx_rx_phy_cmd(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 21 { 22 struct iwl_rx_packet *pkt = rxb_addr(rxb); 23 24 memcpy(&mvm->last_phy_info, pkt->data, sizeof(mvm->last_phy_info)); 25 mvm->ampdu_ref++; 26 27 #ifdef CONFIG_IWLWIFI_DEBUGFS 28 if (mvm->last_phy_info.phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) { 29 spin_lock(&mvm->drv_stats_lock); 30 mvm->drv_rx_stats.ampdu_count++; 31 spin_unlock(&mvm->drv_stats_lock); 32 } 33 #endif 34 } 35 36 /* 37 * iwl_mvm_pass_packet_to_mac80211 - builds the packet for mac80211 38 * 39 * Adds the rxb to a new skb and give it to mac80211 40 */ 41 static void iwl_mvm_pass_packet_to_mac80211(struct iwl_mvm *mvm, 42 struct ieee80211_sta *sta, 43 struct napi_struct *napi, 44 struct sk_buff *skb, 45 struct ieee80211_hdr *hdr, u16 len, 46 u8 crypt_len, 47 struct iwl_rx_cmd_buffer *rxb) 48 { 49 unsigned int hdrlen = ieee80211_hdrlen(hdr->frame_control); 50 unsigned int fraglen; 51 52 /* 53 * The 'hdrlen' (plus the 8 bytes for the SNAP and the crypt_len, 54 * but those are all multiples of 4 long) all goes away, but we 55 * want the *end* of it, which is going to be the start of the IP 56 * header, to be aligned when it gets pulled in. 57 * The beginning of the skb->data is aligned on at least a 4-byte 58 * boundary after allocation. Everything here is aligned at least 59 * on a 2-byte boundary so we can just take hdrlen & 3 and pad by 60 * the result. 61 */ 62 skb_reserve(skb, hdrlen & 3); 63 64 /* If frame is small enough to fit in skb->head, pull it completely. 65 * If not, only pull ieee80211_hdr (including crypto if present, and 66 * an additional 8 bytes for SNAP/ethertype, see below) so that 67 * splice() or TCP coalesce are more efficient. 68 * 69 * Since, in addition, ieee80211_data_to_8023() always pull in at 70 * least 8 bytes (possibly more for mesh) we can do the same here 71 * to save the cost of doing it later. That still doesn't pull in 72 * the actual IP header since the typical case has a SNAP header. 73 * If the latter changes (there are efforts in the standards group 74 * to do so) we should revisit this and ieee80211_data_to_8023(). 75 */ 76 hdrlen = (len <= skb_tailroom(skb)) ? len : hdrlen + crypt_len + 8; 77 78 skb_put_data(skb, hdr, hdrlen); 79 fraglen = len - hdrlen; 80 81 if (fraglen) { 82 int offset = (void *)hdr + hdrlen - 83 rxb_addr(rxb) + rxb_offset(rxb); 84 85 skb_add_rx_frag(skb, 0, rxb_steal_page(rxb), offset, 86 fraglen, rxb->truesize); 87 } 88 89 ieee80211_rx_napi(mvm->hw, sta, skb, napi); 90 } 91 92 /* 93 * iwl_mvm_get_signal_strength - use new rx PHY INFO API 94 * values are reported by the fw as positive values - need to negate 95 * to obtain their dBM. Account for missing antennas by replacing 0 96 * values by -256dBm: practically 0 power and a non-feasible 8 bit value. 97 */ 98 static void iwl_mvm_get_signal_strength(struct iwl_mvm *mvm, 99 struct iwl_rx_phy_info *phy_info, 100 struct ieee80211_rx_status *rx_status) 101 { 102 int energy_a, energy_b, energy_c, max_energy; 103 u32 val; 104 105 val = 106 le32_to_cpu(phy_info->non_cfg_phy[IWL_RX_INFO_ENERGY_ANT_ABC_IDX]); 107 energy_a = (val & IWL_RX_INFO_ENERGY_ANT_A_MSK) >> 108 IWL_RX_INFO_ENERGY_ANT_A_POS; 109 energy_a = energy_a ? -energy_a : S8_MIN; 110 energy_b = (val & IWL_RX_INFO_ENERGY_ANT_B_MSK) >> 111 IWL_RX_INFO_ENERGY_ANT_B_POS; 112 energy_b = energy_b ? -energy_b : S8_MIN; 113 energy_c = (val & IWL_RX_INFO_ENERGY_ANT_C_MSK) >> 114 IWL_RX_INFO_ENERGY_ANT_C_POS; 115 energy_c = energy_c ? -energy_c : S8_MIN; 116 max_energy = max(energy_a, energy_b); 117 max_energy = max(max_energy, energy_c); 118 119 IWL_DEBUG_STATS(mvm, "energy In A %d B %d C %d , and max %d\n", 120 energy_a, energy_b, energy_c, max_energy); 121 122 rx_status->signal = max_energy; 123 rx_status->chains = (le16_to_cpu(phy_info->phy_flags) & 124 RX_RES_PHY_FLAGS_ANTENNA) 125 >> RX_RES_PHY_FLAGS_ANTENNA_POS; 126 rx_status->chain_signal[0] = energy_a; 127 rx_status->chain_signal[1] = energy_b; 128 rx_status->chain_signal[2] = energy_c; 129 } 130 131 /* 132 * iwl_mvm_set_mac80211_rx_flag - translate fw status to mac80211 format 133 * @mvm: the mvm object 134 * @hdr: 80211 header 135 * @stats: status in mac80211's format 136 * @rx_pkt_status: status coming from fw 137 * 138 * returns non 0 value if the packet should be dropped 139 */ 140 static u32 iwl_mvm_set_mac80211_rx_flag(struct iwl_mvm *mvm, 141 struct ieee80211_hdr *hdr, 142 struct ieee80211_rx_status *stats, 143 u32 rx_pkt_status, 144 u8 *crypt_len) 145 { 146 if (!ieee80211_has_protected(hdr->frame_control) || 147 (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) == 148 RX_MPDU_RES_STATUS_SEC_NO_ENC) 149 return 0; 150 151 /* packet was encrypted with unknown alg */ 152 if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) == 153 RX_MPDU_RES_STATUS_SEC_ENC_ERR) 154 return 0; 155 156 switch (rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) { 157 case RX_MPDU_RES_STATUS_SEC_CCM_ENC: 158 /* alg is CCM: check MIC only */ 159 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK)) 160 return -1; 161 162 stats->flag |= RX_FLAG_DECRYPTED; 163 *crypt_len = IEEE80211_CCMP_HDR_LEN; 164 return 0; 165 166 case RX_MPDU_RES_STATUS_SEC_TKIP_ENC: 167 /* Don't drop the frame and decrypt it in SW */ 168 if (!fw_has_api(&mvm->fw->ucode_capa, 169 IWL_UCODE_TLV_API_DEPRECATE_TTAK) && 170 !(rx_pkt_status & RX_MPDU_RES_STATUS_TTAK_OK)) 171 return 0; 172 *crypt_len = IEEE80211_TKIP_IV_LEN; 173 /* fall through */ 174 175 case RX_MPDU_RES_STATUS_SEC_WEP_ENC: 176 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_ICV_OK)) 177 return -1; 178 179 stats->flag |= RX_FLAG_DECRYPTED; 180 if ((rx_pkt_status & RX_MPDU_RES_STATUS_SEC_ENC_MSK) == 181 RX_MPDU_RES_STATUS_SEC_WEP_ENC) 182 *crypt_len = IEEE80211_WEP_IV_LEN; 183 return 0; 184 185 case RX_MPDU_RES_STATUS_SEC_EXT_ENC: 186 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_MIC_OK)) 187 return -1; 188 stats->flag |= RX_FLAG_DECRYPTED; 189 return 0; 190 191 default: 192 /* Expected in monitor (not having the keys) */ 193 if (!mvm->monitor_on) 194 IWL_ERR(mvm, "Unhandled alg: 0x%x\n", rx_pkt_status); 195 } 196 197 return 0; 198 } 199 200 static void iwl_mvm_rx_handle_tcm(struct iwl_mvm *mvm, 201 struct ieee80211_sta *sta, 202 struct ieee80211_hdr *hdr, u32 len, 203 struct iwl_rx_phy_info *phy_info, 204 u32 rate_n_flags) 205 { 206 struct iwl_mvm_sta *mvmsta; 207 struct iwl_mvm_tcm_mac *mdata; 208 int mac; 209 int ac = IEEE80211_AC_BE; /* treat non-QoS as BE */ 210 struct iwl_mvm_vif *mvmvif; 211 /* expected throughput in 100Kbps, single stream, 20 MHz */ 212 static const u8 thresh_tpt[] = { 213 9, 18, 30, 42, 60, 78, 90, 96, 120, 135, 214 }; 215 u16 thr; 216 217 if (ieee80211_is_data_qos(hdr->frame_control)) 218 ac = tid_to_mac80211_ac[ieee80211_get_tid(hdr)]; 219 220 mvmsta = iwl_mvm_sta_from_mac80211(sta); 221 mac = mvmsta->mac_id_n_color & FW_CTXT_ID_MSK; 222 223 if (time_after(jiffies, mvm->tcm.ts + MVM_TCM_PERIOD)) 224 schedule_delayed_work(&mvm->tcm.work, 0); 225 mdata = &mvm->tcm.data[mac]; 226 mdata->rx.pkts[ac]++; 227 228 /* count the airtime only once for each ampdu */ 229 if (mdata->rx.last_ampdu_ref != mvm->ampdu_ref) { 230 mdata->rx.last_ampdu_ref = mvm->ampdu_ref; 231 mdata->rx.airtime += le16_to_cpu(phy_info->frame_time); 232 } 233 234 if (!(rate_n_flags & (RATE_MCS_HT_MSK | RATE_MCS_VHT_MSK))) 235 return; 236 237 mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif); 238 239 if (mdata->opened_rx_ba_sessions || 240 mdata->uapsd_nonagg_detect.detected || 241 (!mvmvif->queue_params[IEEE80211_AC_VO].uapsd && 242 !mvmvif->queue_params[IEEE80211_AC_VI].uapsd && 243 !mvmvif->queue_params[IEEE80211_AC_BE].uapsd && 244 !mvmvif->queue_params[IEEE80211_AC_BK].uapsd) || 245 mvmsta->sta_id != mvmvif->ap_sta_id) 246 return; 247 248 if (rate_n_flags & RATE_MCS_HT_MSK) { 249 thr = thresh_tpt[rate_n_flags & RATE_HT_MCS_RATE_CODE_MSK]; 250 thr *= 1 + ((rate_n_flags & RATE_HT_MCS_NSS_MSK) >> 251 RATE_HT_MCS_NSS_POS); 252 } else { 253 if (WARN_ON((rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK) >= 254 ARRAY_SIZE(thresh_tpt))) 255 return; 256 thr = thresh_tpt[rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK]; 257 thr *= 1 + ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >> 258 RATE_VHT_MCS_NSS_POS); 259 } 260 261 thr <<= ((rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) >> 262 RATE_MCS_CHAN_WIDTH_POS); 263 264 mdata->uapsd_nonagg_detect.rx_bytes += len; 265 ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, thr); 266 } 267 268 static void iwl_mvm_rx_csum(struct ieee80211_sta *sta, 269 struct sk_buff *skb, 270 u32 status) 271 { 272 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 273 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(mvmsta->vif); 274 275 if (mvmvif->features & NETIF_F_RXCSUM && 276 status & RX_MPDU_RES_STATUS_CSUM_DONE && 277 status & RX_MPDU_RES_STATUS_CSUM_OK) 278 skb->ip_summed = CHECKSUM_UNNECESSARY; 279 } 280 281 /* 282 * iwl_mvm_rx_rx_mpdu - REPLY_RX_MPDU_CMD handler 283 * 284 * Handles the actual data of the Rx packet from the fw 285 */ 286 void iwl_mvm_rx_rx_mpdu(struct iwl_mvm *mvm, struct napi_struct *napi, 287 struct iwl_rx_cmd_buffer *rxb) 288 { 289 struct ieee80211_hdr *hdr; 290 struct ieee80211_rx_status *rx_status; 291 struct iwl_rx_packet *pkt = rxb_addr(rxb); 292 struct iwl_rx_phy_info *phy_info; 293 struct iwl_rx_mpdu_res_start *rx_res; 294 struct ieee80211_sta *sta = NULL; 295 struct sk_buff *skb; 296 u32 len, pkt_len = iwl_rx_packet_payload_len(pkt); 297 u32 rate_n_flags; 298 u32 rx_pkt_status; 299 u8 crypt_len = 0; 300 301 phy_info = &mvm->last_phy_info; 302 rx_res = (struct iwl_rx_mpdu_res_start *)pkt->data; 303 hdr = (struct ieee80211_hdr *)(pkt->data + sizeof(*rx_res)); 304 len = le16_to_cpu(rx_res->byte_count); 305 306 if (unlikely(len + sizeof(*rx_res) + sizeof(__le32) > pkt_len)) { 307 IWL_DEBUG_DROP(mvm, "FW lied about packet len\n"); 308 return; 309 } 310 311 rx_pkt_status = get_unaligned_le32((__le32 *) 312 (pkt->data + sizeof(*rx_res) + len)); 313 314 /* Dont use dev_alloc_skb(), we'll have enough headroom once 315 * ieee80211_hdr pulled. 316 */ 317 skb = alloc_skb(128, GFP_ATOMIC); 318 if (!skb) { 319 IWL_ERR(mvm, "alloc_skb failed\n"); 320 return; 321 } 322 323 rx_status = IEEE80211_SKB_RXCB(skb); 324 325 /* 326 * drop the packet if it has failed being decrypted by HW 327 */ 328 if (iwl_mvm_set_mac80211_rx_flag(mvm, hdr, rx_status, rx_pkt_status, 329 &crypt_len)) { 330 IWL_DEBUG_DROP(mvm, "Bad decryption results 0x%08x\n", 331 rx_pkt_status); 332 kfree_skb(skb); 333 return; 334 } 335 336 /* 337 * Keep packets with CRC errors (and with overrun) for monitor mode 338 * (otherwise the firmware discards them) but mark them as bad. 339 */ 340 if (!(rx_pkt_status & RX_MPDU_RES_STATUS_CRC_OK) || 341 !(rx_pkt_status & RX_MPDU_RES_STATUS_OVERRUN_OK)) { 342 IWL_DEBUG_RX(mvm, "Bad CRC or FIFO: 0x%08X.\n", rx_pkt_status); 343 rx_status->flag |= RX_FLAG_FAILED_FCS_CRC; 344 } 345 346 /* This will be used in several places later */ 347 rate_n_flags = le32_to_cpu(phy_info->rate_n_flags); 348 349 /* rx_status carries information about the packet to mac80211 */ 350 rx_status->mactime = le64_to_cpu(phy_info->timestamp); 351 rx_status->device_timestamp = le32_to_cpu(phy_info->system_timestamp); 352 rx_status->band = 353 (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_BAND_24)) ? 354 NL80211_BAND_2GHZ : NL80211_BAND_5GHZ; 355 rx_status->freq = 356 ieee80211_channel_to_frequency(le16_to_cpu(phy_info->channel), 357 rx_status->band); 358 359 /* TSF as indicated by the firmware is at INA time */ 360 rx_status->flag |= RX_FLAG_MACTIME_PLCP_START; 361 362 iwl_mvm_get_signal_strength(mvm, phy_info, rx_status); 363 364 IWL_DEBUG_STATS_LIMIT(mvm, "Rssi %d, TSF %llu\n", rx_status->signal, 365 (unsigned long long)rx_status->mactime); 366 367 rcu_read_lock(); 368 if (rx_pkt_status & RX_MPDU_RES_STATUS_SRC_STA_FOUND) { 369 u32 id = rx_pkt_status & RX_MPDU_RES_STATUS_STA_ID_MSK; 370 371 id >>= RX_MDPU_RES_STATUS_STA_ID_SHIFT; 372 373 if (!WARN_ON_ONCE(id >= mvm->fw->ucode_capa.num_stations)) { 374 sta = rcu_dereference(mvm->fw_id_to_mac_id[id]); 375 if (IS_ERR(sta)) 376 sta = NULL; 377 } 378 } else if (!is_multicast_ether_addr(hdr->addr2)) { 379 /* This is fine since we prevent two stations with the same 380 * address from being added. 381 */ 382 sta = ieee80211_find_sta_by_ifaddr(mvm->hw, hdr->addr2, NULL); 383 } 384 385 if (sta) { 386 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 387 struct ieee80211_vif *tx_blocked_vif = 388 rcu_dereference(mvm->csa_tx_blocked_vif); 389 struct iwl_fw_dbg_trigger_tlv *trig; 390 struct ieee80211_vif *vif = mvmsta->vif; 391 392 /* We have tx blocked stations (with CS bit). If we heard 393 * frames from a blocked station on a new channel we can 394 * TX to it again. 395 */ 396 if (unlikely(tx_blocked_vif) && vif == tx_blocked_vif) { 397 struct iwl_mvm_vif *mvmvif = 398 iwl_mvm_vif_from_mac80211(tx_blocked_vif); 399 400 if (mvmvif->csa_target_freq == rx_status->freq) 401 iwl_mvm_sta_modify_disable_tx_ap(mvm, sta, 402 false); 403 } 404 405 rs_update_last_rssi(mvm, mvmsta, rx_status); 406 407 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, 408 ieee80211_vif_to_wdev(vif), 409 FW_DBG_TRIGGER_RSSI); 410 411 if (trig && ieee80211_is_beacon(hdr->frame_control)) { 412 struct iwl_fw_dbg_trigger_low_rssi *rssi_trig; 413 s32 rssi; 414 415 rssi_trig = (void *)trig->data; 416 rssi = le32_to_cpu(rssi_trig->rssi); 417 418 if (rx_status->signal < rssi) 419 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, 420 NULL); 421 } 422 423 if (!mvm->tcm.paused && len >= sizeof(*hdr) && 424 !is_multicast_ether_addr(hdr->addr1) && 425 ieee80211_is_data(hdr->frame_control)) 426 iwl_mvm_rx_handle_tcm(mvm, sta, hdr, len, phy_info, 427 rate_n_flags); 428 429 if (ieee80211_is_data(hdr->frame_control)) 430 iwl_mvm_rx_csum(sta, skb, rx_pkt_status); 431 } 432 rcu_read_unlock(); 433 434 /* set the preamble flag if appropriate */ 435 if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_SHORT_PREAMBLE)) 436 rx_status->enc_flags |= RX_ENC_FLAG_SHORTPRE; 437 438 if (phy_info->phy_flags & cpu_to_le16(RX_RES_PHY_FLAGS_AGG)) { 439 /* 440 * We know which subframes of an A-MPDU belong 441 * together since we get a single PHY response 442 * from the firmware for all of them 443 */ 444 rx_status->flag |= RX_FLAG_AMPDU_DETAILS; 445 rx_status->ampdu_reference = mvm->ampdu_ref; 446 } 447 448 /* Set up the HT phy flags */ 449 switch (rate_n_flags & RATE_MCS_CHAN_WIDTH_MSK) { 450 case RATE_MCS_CHAN_WIDTH_20: 451 break; 452 case RATE_MCS_CHAN_WIDTH_40: 453 rx_status->bw = RATE_INFO_BW_40; 454 break; 455 case RATE_MCS_CHAN_WIDTH_80: 456 rx_status->bw = RATE_INFO_BW_80; 457 break; 458 case RATE_MCS_CHAN_WIDTH_160: 459 rx_status->bw = RATE_INFO_BW_160; 460 break; 461 } 462 if (!(rate_n_flags & RATE_MCS_CCK_MSK) && 463 rate_n_flags & RATE_MCS_SGI_MSK) 464 rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI; 465 if (rate_n_flags & RATE_HT_MCS_GF_MSK) 466 rx_status->enc_flags |= RX_ENC_FLAG_HT_GF; 467 if (rate_n_flags & RATE_MCS_LDPC_MSK) 468 rx_status->enc_flags |= RX_ENC_FLAG_LDPC; 469 if (rate_n_flags & RATE_MCS_HT_MSK) { 470 u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >> 471 RATE_MCS_STBC_POS; 472 rx_status->encoding = RX_ENC_HT; 473 rx_status->rate_idx = rate_n_flags & RATE_HT_MCS_INDEX_MSK; 474 rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT; 475 } else if (rate_n_flags & RATE_MCS_VHT_MSK) { 476 u8 stbc = (rate_n_flags & RATE_MCS_STBC_MSK) >> 477 RATE_MCS_STBC_POS; 478 rx_status->nss = 479 ((rate_n_flags & RATE_VHT_MCS_NSS_MSK) >> 480 RATE_VHT_MCS_NSS_POS) + 1; 481 rx_status->rate_idx = rate_n_flags & RATE_VHT_MCS_RATE_CODE_MSK; 482 rx_status->encoding = RX_ENC_VHT; 483 rx_status->enc_flags |= stbc << RX_ENC_FLAG_STBC_SHIFT; 484 if (rate_n_flags & RATE_MCS_BF_MSK) 485 rx_status->enc_flags |= RX_ENC_FLAG_BF; 486 } else { 487 int rate = iwl_mvm_legacy_rate_to_mac80211_idx(rate_n_flags, 488 rx_status->band); 489 490 if (WARN(rate < 0 || rate > 0xFF, 491 "Invalid rate flags 0x%x, band %d,\n", 492 rate_n_flags, rx_status->band)) { 493 kfree_skb(skb); 494 return; 495 } 496 rx_status->rate_idx = rate; 497 } 498 499 #ifdef CONFIG_IWLWIFI_DEBUGFS 500 iwl_mvm_update_frame_stats(mvm, rate_n_flags, 501 rx_status->flag & RX_FLAG_AMPDU_DETAILS); 502 #endif 503 504 if (unlikely((ieee80211_is_beacon(hdr->frame_control) || 505 ieee80211_is_probe_resp(hdr->frame_control)) && 506 mvm->sched_scan_pass_all == SCHED_SCAN_PASS_ALL_ENABLED)) 507 mvm->sched_scan_pass_all = SCHED_SCAN_PASS_ALL_FOUND; 508 509 if (unlikely(ieee80211_is_beacon(hdr->frame_control) || 510 ieee80211_is_probe_resp(hdr->frame_control))) 511 rx_status->boottime_ns = ktime_get_boottime_ns(); 512 513 iwl_mvm_pass_packet_to_mac80211(mvm, sta, napi, skb, hdr, len, 514 crypt_len, rxb); 515 } 516 517 struct iwl_mvm_stat_data { 518 struct iwl_mvm *mvm; 519 __le32 flags; 520 __le32 mac_id; 521 u8 beacon_filter_average_energy; 522 __le32 *beacon_counter; 523 u8 *beacon_average_energy; 524 }; 525 526 static void iwl_mvm_stat_iterator(void *_data, u8 *mac, 527 struct ieee80211_vif *vif) 528 { 529 struct iwl_mvm_stat_data *data = _data; 530 struct iwl_mvm *mvm = data->mvm; 531 int sig = -data->beacon_filter_average_energy; 532 int last_event; 533 int thold = vif->bss_conf.cqm_rssi_thold; 534 int hyst = vif->bss_conf.cqm_rssi_hyst; 535 u16 id = le32_to_cpu(data->mac_id); 536 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif); 537 u16 vif_id = mvmvif->id; 538 539 /* This doesn't need the MAC ID check since it's not taking the 540 * data copied into the "data" struct, but rather the data from 541 * the notification directly. 542 */ 543 mvmvif->beacon_stats.num_beacons = 544 le32_to_cpu(data->beacon_counter[vif_id]); 545 mvmvif->beacon_stats.avg_signal = 546 -data->beacon_average_energy[vif_id]; 547 548 /* make sure that beacon statistics don't go backwards with TCM 549 * request to clear statistics 550 */ 551 if (le32_to_cpu(data->flags) & IWL_STATISTICS_REPLY_FLG_CLEAR) 552 mvmvif->beacon_stats.accu_num_beacons += 553 mvmvif->beacon_stats.num_beacons; 554 555 if (mvmvif->id != id) 556 return; 557 558 if (vif->type != NL80211_IFTYPE_STATION) 559 return; 560 561 if (sig == 0) { 562 IWL_DEBUG_RX(mvm, "RSSI is 0 - skip signal based decision\n"); 563 return; 564 } 565 566 mvmvif->bf_data.ave_beacon_signal = sig; 567 568 /* BT Coex */ 569 if (mvmvif->bf_data.bt_coex_min_thold != 570 mvmvif->bf_data.bt_coex_max_thold) { 571 last_event = mvmvif->bf_data.last_bt_coex_event; 572 if (sig > mvmvif->bf_data.bt_coex_max_thold && 573 (last_event <= mvmvif->bf_data.bt_coex_min_thold || 574 last_event == 0)) { 575 mvmvif->bf_data.last_bt_coex_event = sig; 576 IWL_DEBUG_RX(mvm, "cqm_iterator bt coex high %d\n", 577 sig); 578 iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_HIGH); 579 } else if (sig < mvmvif->bf_data.bt_coex_min_thold && 580 (last_event >= mvmvif->bf_data.bt_coex_max_thold || 581 last_event == 0)) { 582 mvmvif->bf_data.last_bt_coex_event = sig; 583 IWL_DEBUG_RX(mvm, "cqm_iterator bt coex low %d\n", 584 sig); 585 iwl_mvm_bt_rssi_event(mvm, vif, RSSI_EVENT_LOW); 586 } 587 } 588 589 if (!(vif->driver_flags & IEEE80211_VIF_SUPPORTS_CQM_RSSI)) 590 return; 591 592 /* CQM Notification */ 593 last_event = mvmvif->bf_data.last_cqm_event; 594 if (thold && sig < thold && (last_event == 0 || 595 sig < last_event - hyst)) { 596 mvmvif->bf_data.last_cqm_event = sig; 597 IWL_DEBUG_RX(mvm, "cqm_iterator cqm low %d\n", 598 sig); 599 ieee80211_cqm_rssi_notify( 600 vif, 601 NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW, 602 sig, 603 GFP_KERNEL); 604 } else if (sig > thold && 605 (last_event == 0 || sig > last_event + hyst)) { 606 mvmvif->bf_data.last_cqm_event = sig; 607 IWL_DEBUG_RX(mvm, "cqm_iterator cqm high %d\n", 608 sig); 609 ieee80211_cqm_rssi_notify( 610 vif, 611 NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH, 612 sig, 613 GFP_KERNEL); 614 } 615 } 616 617 static inline void 618 iwl_mvm_rx_stats_check_trigger(struct iwl_mvm *mvm, struct iwl_rx_packet *pkt) 619 { 620 struct iwl_fw_dbg_trigger_tlv *trig; 621 struct iwl_fw_dbg_trigger_stats *trig_stats; 622 u32 trig_offset, trig_thold; 623 624 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, NULL, FW_DBG_TRIGGER_STATS); 625 if (!trig) 626 return; 627 628 trig_stats = (void *)trig->data; 629 630 trig_offset = le32_to_cpu(trig_stats->stop_offset); 631 trig_thold = le32_to_cpu(trig_stats->stop_threshold); 632 633 if (WARN_ON_ONCE(trig_offset >= iwl_rx_packet_payload_len(pkt))) 634 return; 635 636 if (le32_to_cpup((__le32 *) (pkt->data + trig_offset)) < trig_thold) 637 return; 638 639 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, NULL); 640 } 641 642 static void iwl_mvm_stats_energy_iter(void *_data, 643 struct ieee80211_sta *sta) 644 { 645 struct iwl_mvm_sta *mvmsta = iwl_mvm_sta_from_mac80211(sta); 646 u8 *energy = _data; 647 u32 sta_id = mvmsta->sta_id; 648 649 if (WARN_ONCE(sta_id >= IWL_MVM_STATION_COUNT_MAX, "sta_id %d >= %d", 650 sta_id, IWL_MVM_STATION_COUNT_MAX)) 651 return; 652 653 if (energy[sta_id]) 654 mvmsta->avg_energy = energy[sta_id]; 655 656 } 657 658 static void 659 iwl_mvm_update_tcm_from_stats(struct iwl_mvm *mvm, __le32 *air_time_le, 660 __le32 *rx_bytes_le) 661 { 662 int i; 663 664 spin_lock(&mvm->tcm.lock); 665 for (i = 0; i < NUM_MAC_INDEX_DRIVER; i++) { 666 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[i]; 667 u32 rx_bytes = le32_to_cpu(rx_bytes_le[i]); 668 u32 airtime = le32_to_cpu(air_time_le[i]); 669 670 mdata->rx.airtime += airtime; 671 mdata->uapsd_nonagg_detect.rx_bytes += rx_bytes; 672 if (airtime) { 673 /* re-init every time to store rate from FW */ 674 ewma_rate_init(&mdata->uapsd_nonagg_detect.rate); 675 ewma_rate_add(&mdata->uapsd_nonagg_detect.rate, 676 rx_bytes * 8 / airtime); 677 } 678 } 679 spin_unlock(&mvm->tcm.lock); 680 } 681 682 static void 683 iwl_mvm_handle_rx_statistics_tlv(struct iwl_mvm *mvm, 684 struct iwl_rx_packet *pkt) 685 { 686 struct iwl_mvm_stat_data data = { 687 .mvm = mvm, 688 }; 689 u8 beacon_average_energy[MAC_INDEX_AUX]; 690 u8 average_energy[IWL_MVM_STATION_COUNT_MAX]; 691 struct iwl_statistics_operational_ntfy *stats; 692 int expected_size; 693 __le32 flags; 694 int i; 695 696 expected_size = sizeof(*stats); 697 if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) < expected_size, 698 "received invalid statistics size (%d)!, expected_size: %d\n", 699 iwl_rx_packet_payload_len(pkt), expected_size)) 700 return; 701 702 stats = (void *)&pkt->data; 703 704 if (WARN_ONCE(stats->hdr.type != FW_STATISTICS_OPERATIONAL || 705 stats->hdr.version != 1, 706 "received unsupported hdr type %d, version %d\n", 707 stats->hdr.type, stats->hdr.version)) 708 return; 709 710 flags = stats->flags; 711 mvm->radio_stats.rx_time = le64_to_cpu(stats->rx_time); 712 mvm->radio_stats.tx_time = le64_to_cpu(stats->tx_time); 713 mvm->radio_stats.on_time_rf = le64_to_cpu(stats->on_time_rf); 714 mvm->radio_stats.on_time_scan = le64_to_cpu(stats->on_time_scan); 715 716 iwl_mvm_rx_stats_check_trigger(mvm, pkt); 717 718 data.mac_id = stats->mac_id; 719 data.beacon_filter_average_energy = 720 le32_to_cpu(stats->beacon_filter_average_energy); 721 data.flags = flags; 722 data.beacon_counter = stats->beacon_counter; 723 for (i = 0; i < ARRAY_SIZE(beacon_average_energy); i++) 724 beacon_average_energy[i] = 725 le32_to_cpu(stats->beacon_average_energy[i]); 726 727 data.beacon_average_energy = beacon_average_energy; 728 729 ieee80211_iterate_active_interfaces(mvm->hw, 730 IEEE80211_IFACE_ITER_NORMAL, 731 iwl_mvm_stat_iterator, 732 &data); 733 734 for (i = 0; i < ARRAY_SIZE(average_energy); i++) 735 average_energy[i] = le32_to_cpu(stats->average_energy[i]); 736 ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter, 737 average_energy); 738 /* 739 * Don't update in case the statistics are not cleared, since 740 * we will end up counting twice the same airtime, once in TCM 741 * request and once in statistics notification. 742 */ 743 if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR) 744 iwl_mvm_update_tcm_from_stats(mvm, stats->air_time, 745 stats->rx_bytes); 746 } 747 748 void iwl_mvm_handle_rx_statistics(struct iwl_mvm *mvm, 749 struct iwl_rx_packet *pkt) 750 { 751 struct iwl_mvm_stat_data data = { 752 .mvm = mvm, 753 }; 754 __le32 *bytes, *air_time, flags; 755 int expected_size; 756 u8 *energy; 757 758 /* From ver 14 and up we use TLV statistics format */ 759 if (iwl_fw_lookup_notif_ver(mvm->fw, LONG_GROUP, 760 STATISTICS_CMD, 0) >= 14) 761 return iwl_mvm_handle_rx_statistics_tlv(mvm, pkt); 762 763 if (!iwl_mvm_has_new_rx_stats_api(mvm)) { 764 if (iwl_mvm_has_new_rx_api(mvm)) 765 expected_size = sizeof(struct iwl_notif_statistics_v11); 766 else 767 expected_size = sizeof(struct iwl_notif_statistics_v10); 768 } else { 769 expected_size = sizeof(struct iwl_notif_statistics); 770 } 771 772 if (WARN_ONCE(iwl_rx_packet_payload_len(pkt) != expected_size, 773 "received invalid statistics size (%d)!\n", 774 iwl_rx_packet_payload_len(pkt))) 775 return; 776 777 if (!iwl_mvm_has_new_rx_stats_api(mvm)) { 778 struct iwl_notif_statistics_v11 *stats = (void *)&pkt->data; 779 780 data.mac_id = stats->rx.general.mac_id; 781 data.beacon_filter_average_energy = 782 stats->general.common.beacon_filter_average_energy; 783 784 mvm->rx_stats_v3 = stats->rx; 785 786 mvm->radio_stats.rx_time = 787 le64_to_cpu(stats->general.common.rx_time); 788 mvm->radio_stats.tx_time = 789 le64_to_cpu(stats->general.common.tx_time); 790 mvm->radio_stats.on_time_rf = 791 le64_to_cpu(stats->general.common.on_time_rf); 792 mvm->radio_stats.on_time_scan = 793 le64_to_cpu(stats->general.common.on_time_scan); 794 795 data.beacon_counter = stats->general.beacon_counter; 796 data.beacon_average_energy = 797 stats->general.beacon_average_energy; 798 flags = stats->flag; 799 } else { 800 struct iwl_notif_statistics *stats = (void *)&pkt->data; 801 802 data.mac_id = stats->rx.general.mac_id; 803 data.beacon_filter_average_energy = 804 stats->general.common.beacon_filter_average_energy; 805 806 mvm->rx_stats = stats->rx; 807 808 mvm->radio_stats.rx_time = 809 le64_to_cpu(stats->general.common.rx_time); 810 mvm->radio_stats.tx_time = 811 le64_to_cpu(stats->general.common.tx_time); 812 mvm->radio_stats.on_time_rf = 813 le64_to_cpu(stats->general.common.on_time_rf); 814 mvm->radio_stats.on_time_scan = 815 le64_to_cpu(stats->general.common.on_time_scan); 816 817 data.beacon_counter = stats->general.beacon_counter; 818 data.beacon_average_energy = 819 stats->general.beacon_average_energy; 820 flags = stats->flag; 821 } 822 data.flags = flags; 823 824 iwl_mvm_rx_stats_check_trigger(mvm, pkt); 825 826 ieee80211_iterate_active_interfaces(mvm->hw, 827 IEEE80211_IFACE_ITER_NORMAL, 828 iwl_mvm_stat_iterator, 829 &data); 830 831 if (!iwl_mvm_has_new_rx_api(mvm)) 832 return; 833 834 if (!iwl_mvm_has_new_rx_stats_api(mvm)) { 835 struct iwl_notif_statistics_v11 *v11 = (void *)&pkt->data; 836 837 energy = (void *)&v11->load_stats.avg_energy; 838 bytes = (void *)&v11->load_stats.byte_count; 839 air_time = (void *)&v11->load_stats.air_time; 840 } else { 841 struct iwl_notif_statistics *stats = (void *)&pkt->data; 842 843 energy = (void *)&stats->load_stats.avg_energy; 844 bytes = (void *)&stats->load_stats.byte_count; 845 air_time = (void *)&stats->load_stats.air_time; 846 } 847 ieee80211_iterate_stations_atomic(mvm->hw, iwl_mvm_stats_energy_iter, 848 energy); 849 850 /* 851 * Don't update in case the statistics are not cleared, since 852 * we will end up counting twice the same airtime, once in TCM 853 * request and once in statistics notification. 854 */ 855 if (le32_to_cpu(flags) & IWL_STATISTICS_REPLY_FLG_CLEAR) 856 iwl_mvm_update_tcm_from_stats(mvm, air_time, bytes); 857 858 } 859 860 void iwl_mvm_rx_statistics(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb) 861 { 862 iwl_mvm_handle_rx_statistics(mvm, rxb_addr(rxb)); 863 } 864 865 void iwl_mvm_window_status_notif(struct iwl_mvm *mvm, 866 struct iwl_rx_cmd_buffer *rxb) 867 { 868 struct iwl_rx_packet *pkt = rxb_addr(rxb); 869 struct iwl_ba_window_status_notif *notif = (void *)pkt->data; 870 int i; 871 u32 pkt_len = iwl_rx_packet_payload_len(pkt); 872 873 if (WARN_ONCE(pkt_len != sizeof(*notif), 874 "Received window status notification of wrong size (%u)\n", 875 pkt_len)) 876 return; 877 878 rcu_read_lock(); 879 for (i = 0; i < BA_WINDOW_STREAMS_MAX; i++) { 880 struct ieee80211_sta *sta; 881 u8 sta_id, tid; 882 u64 bitmap; 883 u32 ssn; 884 u16 ratid; 885 u16 received_mpdu; 886 887 ratid = le16_to_cpu(notif->ra_tid[i]); 888 /* check that this TID is valid */ 889 if (!(ratid & BA_WINDOW_STATUS_VALID_MSK)) 890 continue; 891 892 received_mpdu = le16_to_cpu(notif->mpdu_rx_count[i]); 893 if (received_mpdu == 0) 894 continue; 895 896 tid = ratid & BA_WINDOW_STATUS_TID_MSK; 897 /* get the station */ 898 sta_id = (ratid & BA_WINDOW_STATUS_STA_ID_MSK) 899 >> BA_WINDOW_STATUS_STA_ID_POS; 900 sta = rcu_dereference(mvm->fw_id_to_mac_id[sta_id]); 901 if (IS_ERR_OR_NULL(sta)) 902 continue; 903 bitmap = le64_to_cpu(notif->bitmap[i]); 904 ssn = le32_to_cpu(notif->start_seq_num[i]); 905 906 /* update mac80211 with the bitmap for the reordering buffer */ 907 ieee80211_mark_rx_ba_filtered_frames(sta, tid, ssn, bitmap, 908 received_mpdu); 909 } 910 rcu_read_unlock(); 911 } 912