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