1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (C) 2012-2014, 2018-2024 Intel Corporation
4 * Copyright (C) 2013-2014 Intel Mobile Communications GmbH
5 * Copyright (C) 2015-2017 Intel Deutschland GmbH
7 #include <net/mac80211.h>
14 #include "fw/api/rs.h"
18 * Will return 0 even if the cmd failed when RFKILL is asserted unless
19 * CMD_WANT_SKB is set in cmd->flags.
21 int iwl_mvm_send_cmd(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd)
25 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
26 if (WARN_ON(mvm->d3_test_active))
31 * Synchronous commands from this op-mode must hold
32 * the mutex, this ensures we don't try to send two
33 * (or more) synchronous commands at a time.
35 if (!(cmd->flags & CMD_ASYNC))
36 lockdep_assert_held(&mvm->mutex);
38 ret = iwl_trans_send_cmd(mvm->trans, cmd);
41 * If the caller wants the SKB, then don't hide any problems, the
42 * caller might access the response buffer which will be NULL if
45 if (cmd->flags & CMD_WANT_SKB)
49 * Silently ignore failures if RFKILL is asserted or
50 * we are in suspend\resume process
52 if (!ret || ret == -ERFKILL || ret == -EHOSTDOWN)
57 int iwl_mvm_send_cmd_pdu(struct iwl_mvm *mvm, u32 id,
58 u32 flags, u16 len, const void *data)
60 struct iwl_host_cmd cmd = {
67 return iwl_mvm_send_cmd(mvm, &cmd);
71 * We assume that the caller set the status to the success value
73 int iwl_mvm_send_cmd_status(struct iwl_mvm *mvm, struct iwl_host_cmd *cmd,
76 struct iwl_rx_packet *pkt;
77 struct iwl_cmd_response *resp;
80 lockdep_assert_held(&mvm->mutex);
82 #if defined(CONFIG_IWLWIFI_DEBUGFS) && defined(CONFIG_PM_SLEEP)
83 if (WARN_ON(mvm->d3_test_active))
88 * Only synchronous commands can wait for status,
89 * we use WANT_SKB so the caller can't.
91 if (WARN_ONCE(cmd->flags & (CMD_ASYNC | CMD_WANT_SKB),
92 "cmd flags %x", cmd->flags))
95 cmd->flags |= CMD_WANT_SKB;
97 ret = iwl_trans_send_cmd(mvm->trans, cmd);
98 if (ret == -ERFKILL) {
100 * The command failed because of RFKILL, don't update
101 * the status, leave it as success and return 0.
110 resp_len = iwl_rx_packet_payload_len(pkt);
111 if (WARN_ON_ONCE(resp_len != sizeof(*resp))) {
116 resp = (void *)pkt->data;
117 *status = le32_to_cpu(resp->status);
124 * We assume that the caller set the status to the sucess value
126 int iwl_mvm_send_cmd_pdu_status(struct iwl_mvm *mvm, u32 id, u16 len,
127 const void *data, u32 *status)
129 struct iwl_host_cmd cmd = {
135 return iwl_mvm_send_cmd_status(mvm, &cmd, status);
138 int iwl_mvm_legacy_hw_idx_to_mac80211_idx(u32 rate_n_flags,
139 enum nl80211_band band)
141 int format = rate_n_flags & RATE_MCS_MOD_TYPE_MSK;
142 int rate = rate_n_flags & RATE_LEGACY_RATE_MSK;
143 bool is_LB = band == NL80211_BAND_2GHZ;
145 if (format == RATE_MCS_LEGACY_OFDM_MSK)
146 return is_LB ? rate + IWL_FIRST_OFDM_RATE :
149 /* CCK is not allowed in HB */
150 return is_LB ? rate : -1;
153 int iwl_mvm_legacy_rate_to_mac80211_idx(u32 rate_n_flags,
154 enum nl80211_band band)
156 int rate = rate_n_flags & RATE_LEGACY_RATE_MSK_V1;
160 /* Legacy rate format, search for match in table */
161 if (band != NL80211_BAND_2GHZ)
162 band_offset = IWL_FIRST_OFDM_RATE;
163 for (idx = band_offset; idx < IWL_RATE_COUNT_LEGACY; idx++)
164 if (iwl_fw_rate_idx_to_plcp(idx) == rate)
165 return idx - band_offset;
170 u8 iwl_mvm_mac80211_idx_to_hwrate(const struct iwl_fw *fw, int rate_idx)
172 if (iwl_fw_lookup_cmd_ver(fw, TX_CMD, 0) > 8)
173 /* In the new rate legacy rates are indexed:
174 * 0 - 3 for CCK and 0 - 7 for OFDM.
176 return (rate_idx >= IWL_FIRST_OFDM_RATE ?
177 rate_idx - IWL_FIRST_OFDM_RATE :
180 return iwl_fw_rate_idx_to_plcp(rate_idx);
183 u8 iwl_mvm_mac80211_ac_to_ucode_ac(enum ieee80211_ac_numbers ac)
185 static const u8 mac80211_ac_to_ucode_ac[] = {
192 return mac80211_ac_to_ucode_ac[ac];
195 void iwl_mvm_rx_fw_error(struct iwl_mvm *mvm, struct iwl_rx_cmd_buffer *rxb)
197 struct iwl_rx_packet *pkt = rxb_addr(rxb);
198 struct iwl_error_resp *err_resp = (void *)pkt->data;
200 IWL_ERR(mvm, "FW Error notification: type 0x%08X cmd_id 0x%02X\n",
201 le32_to_cpu(err_resp->error_type), err_resp->cmd_id);
202 IWL_ERR(mvm, "FW Error notification: seq 0x%04X service 0x%08X\n",
203 le16_to_cpu(err_resp->bad_cmd_seq_num),
204 le32_to_cpu(err_resp->error_service));
205 IWL_ERR(mvm, "FW Error notification: timestamp 0x%016llX\n",
206 le64_to_cpu(err_resp->timestamp));
210 * Returns the first antenna as ANT_[ABC], as defined in iwl-config.h.
211 * The parameter should also be a combination of ANT_[ABC].
213 u8 first_antenna(u8 mask)
215 BUILD_BUG_ON(ANT_A != BIT(0)); /* using ffs is wrong if not */
216 if (WARN_ON_ONCE(!mask)) /* ffs will return 0 if mask is zeroed */
218 return BIT(ffs(mask) - 1);
221 #define MAX_ANT_NUM 2
223 * Toggles between TX antennas to send the probe request on.
224 * Receives the bitmask of valid TX antennas and the *index* used
225 * for the last TX, and returns the next valid *index* to use.
226 * In order to set it in the tx_cmd, must do BIT(idx).
228 u8 iwl_mvm_next_antenna(struct iwl_mvm *mvm, u8 valid, u8 last_idx)
233 for (i = 0; i < MAX_ANT_NUM; i++) {
234 ind = (ind + 1) % MAX_ANT_NUM;
235 if (valid & BIT(ind))
239 WARN_ONCE(1, "Failed to toggle between antennas 0x%x", valid);
244 * iwl_mvm_send_lq_cmd() - Send link quality command
246 * @lq: Link quality command to send.
248 * The link quality command is sent as the last step of station creation.
249 * This is the special case in which init is set and we call a callback in
250 * this case to clear the state indicating that station creation is in
253 * Returns: an error code indicating success or failure
255 int iwl_mvm_send_lq_cmd(struct iwl_mvm *mvm, struct iwl_lq_cmd *lq)
257 struct iwl_host_cmd cmd = {
259 .len = { sizeof(struct iwl_lq_cmd), },
264 if (WARN_ON(lq->sta_id == IWL_INVALID_STA ||
265 iwl_mvm_has_tlc_offload(mvm)))
268 return iwl_mvm_send_cmd(mvm, &cmd);
272 * iwl_mvm_update_smps - Get a request to change the SMPS mode
274 * @vif: Pointer to the ieee80211_vif structure
275 * @req_type: The part of the driver who call for a change.
276 * @smps_request: The request to change the SMPS mode.
277 * @link_id: for MLO link_id, otherwise 0 (deflink)
279 * Get a requst to change the SMPS mode,
280 * and change it according to all other requests in the driver.
282 void iwl_mvm_update_smps(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
283 enum iwl_mvm_smps_type_request req_type,
284 enum ieee80211_smps_mode smps_request,
285 unsigned int link_id)
287 struct iwl_mvm_vif *mvmvif;
288 enum ieee80211_smps_mode smps_mode = IEEE80211_SMPS_AUTOMATIC;
291 lockdep_assert_held(&mvm->mutex);
293 /* SMPS is irrelevant for NICs that don't have at least 2 RX antenna */
294 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
297 if (vif->type != NL80211_IFTYPE_STATION)
300 /* SMPS is handled by firmware */
301 if (iwl_mvm_has_rlc_offload(mvm))
304 mvmvif = iwl_mvm_vif_from_mac80211(vif);
306 if (WARN_ON_ONCE(!mvmvif->link[link_id]))
309 mvmvif->link[link_id]->smps_requests[req_type] = smps_request;
310 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
311 if (mvmvif->link[link_id]->smps_requests[i] ==
312 IEEE80211_SMPS_STATIC) {
313 smps_mode = IEEE80211_SMPS_STATIC;
316 if (mvmvif->link[link_id]->smps_requests[i] ==
317 IEEE80211_SMPS_DYNAMIC)
318 smps_mode = IEEE80211_SMPS_DYNAMIC;
321 /* SMPS is disabled in eSR */
322 if (mvmvif->esr_active)
323 smps_mode = IEEE80211_SMPS_OFF;
325 ieee80211_request_smps(vif, link_id, smps_mode);
328 void iwl_mvm_update_smps_on_active_links(struct iwl_mvm *mvm,
329 struct ieee80211_vif *vif,
330 enum iwl_mvm_smps_type_request req_type,
331 enum ieee80211_smps_mode smps_request)
333 struct ieee80211_bss_conf *link_conf;
334 unsigned int link_id;
337 for_each_vif_active_link(vif, link_conf, link_id)
338 iwl_mvm_update_smps(mvm, vif, req_type, smps_request,
343 static bool iwl_wait_stats_complete(struct iwl_notif_wait_data *notif_wait,
344 struct iwl_rx_packet *pkt, void *data)
346 WARN_ON(pkt->hdr.cmd != STATISTICS_NOTIFICATION);
351 #define PERIODIC_STAT_RATE 5
353 int iwl_mvm_request_periodic_system_statistics(struct iwl_mvm *mvm, bool enable)
355 u32 flags = enable ? 0 : IWL_STATS_CFG_FLG_DISABLE_NTFY_MSK;
356 u32 type = enable ? (IWL_STATS_NTFY_TYPE_ID_OPER |
357 IWL_STATS_NTFY_TYPE_ID_OPER_PART1) : 0;
358 struct iwl_system_statistics_cmd system_cmd = {
359 .cfg_mask = cpu_to_le32(flags),
360 .config_time_sec = cpu_to_le32(enable ?
361 PERIODIC_STAT_RATE : 0),
362 .type_id_mask = cpu_to_le32(type),
365 return iwl_mvm_send_cmd_pdu(mvm,
366 WIDE_ID(SYSTEM_GROUP,
367 SYSTEM_STATISTICS_CMD),
368 0, sizeof(system_cmd), &system_cmd);
371 static int iwl_mvm_request_system_statistics(struct iwl_mvm *mvm, bool clear,
374 struct iwl_system_statistics_cmd system_cmd = {
376 cpu_to_le32(IWL_STATS_CFG_FLG_ON_DEMAND_NTFY_MSK) :
377 cpu_to_le32(IWL_STATS_CFG_FLG_RESET_MSK |
378 IWL_STATS_CFG_FLG_ON_DEMAND_NTFY_MSK),
379 .type_id_mask = cpu_to_le32(IWL_STATS_NTFY_TYPE_ID_OPER |
380 IWL_STATS_NTFY_TYPE_ID_OPER_PART1),
382 struct iwl_host_cmd cmd = {
383 .id = WIDE_ID(SYSTEM_GROUP, SYSTEM_STATISTICS_CMD),
384 .len[0] = sizeof(system_cmd),
385 .data[0] = &system_cmd,
387 struct iwl_notification_wait stats_wait;
388 static const u16 stats_complete[] = {
389 WIDE_ID(SYSTEM_GROUP, SYSTEM_STATISTICS_END_NOTIF),
394 IWL_FW_CHECK_FAILED(mvm,
395 "Invalid system statistics command version:%d\n",
400 iwl_init_notification_wait(&mvm->notif_wait, &stats_wait,
401 stats_complete, ARRAY_SIZE(stats_complete),
404 mvm->statistics_clear = clear;
405 ret = iwl_mvm_send_cmd(mvm, &cmd);
407 iwl_remove_notification(&mvm->notif_wait, &stats_wait);
411 /* 500ms for OPERATIONAL, PART1 and END notification should be enough
412 * for FW to collect data from all LMACs and send
413 * STATISTICS_NOTIFICATION to host
415 ret = iwl_wait_notification(&mvm->notif_wait, &stats_wait, HZ / 2);
420 iwl_mvm_accu_radio_stats(mvm);
425 int iwl_mvm_request_statistics(struct iwl_mvm *mvm, bool clear)
427 struct iwl_statistics_cmd scmd = {
428 .flags = clear ? cpu_to_le32(IWL_STATISTICS_FLG_CLEAR) : 0,
431 struct iwl_host_cmd cmd = {
432 .id = STATISTICS_CMD,
433 .len[0] = sizeof(scmd),
436 u8 cmd_ver = iwl_fw_lookup_cmd_ver(mvm->fw,
437 WIDE_ID(SYSTEM_GROUP,
438 SYSTEM_STATISTICS_CMD),
439 IWL_FW_CMD_VER_UNKNOWN);
443 * Don't request statistics during restart, they'll not have any useful
444 * information right after restart, nor is clearing needed
446 if (test_bit(IWL_MVM_STATUS_IN_HW_RESTART, &mvm->status))
449 if (cmd_ver != IWL_FW_CMD_VER_UNKNOWN)
450 return iwl_mvm_request_system_statistics(mvm, clear, cmd_ver);
452 /* From version 15 - STATISTICS_NOTIFICATION, the reply for
453 * STATISTICS_CMD is empty, and the response is with
454 * STATISTICS_NOTIFICATION notification
456 if (iwl_fw_lookup_notif_ver(mvm->fw, LEGACY_GROUP,
457 STATISTICS_NOTIFICATION, 0) < 15) {
458 cmd.flags = CMD_WANT_SKB;
460 ret = iwl_mvm_send_cmd(mvm, &cmd);
464 iwl_mvm_handle_rx_statistics(mvm, cmd.resp_pkt);
467 struct iwl_notification_wait stats_wait;
468 static const u16 stats_complete[] = {
469 STATISTICS_NOTIFICATION,
472 iwl_init_notification_wait(&mvm->notif_wait, &stats_wait,
473 stats_complete, ARRAY_SIZE(stats_complete),
474 iwl_wait_stats_complete, NULL);
476 ret = iwl_mvm_send_cmd(mvm, &cmd);
478 iwl_remove_notification(&mvm->notif_wait, &stats_wait);
482 /* 200ms should be enough for FW to collect data from all
483 * LMACs and send STATISTICS_NOTIFICATION to host
485 ret = iwl_wait_notification(&mvm->notif_wait, &stats_wait, HZ / 5);
491 iwl_mvm_accu_radio_stats(mvm);
496 void iwl_mvm_accu_radio_stats(struct iwl_mvm *mvm)
498 mvm->accu_radio_stats.rx_time += mvm->radio_stats.rx_time;
499 mvm->accu_radio_stats.tx_time += mvm->radio_stats.tx_time;
500 mvm->accu_radio_stats.on_time_rf += mvm->radio_stats.on_time_rf;
501 mvm->accu_radio_stats.on_time_scan += mvm->radio_stats.on_time_scan;
504 struct iwl_mvm_diversity_iter_data {
505 struct iwl_mvm_phy_ctxt *ctxt;
509 static void iwl_mvm_diversity_iter(void *_data, u8 *mac,
510 struct ieee80211_vif *vif)
512 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
513 struct iwl_mvm_diversity_iter_data *data = _data;
516 for_each_mvm_vif_valid_link(mvmvif, link_id) {
517 struct iwl_mvm_vif_link_info *link_info = mvmvif->link[link_id];
519 if (link_info->phy_ctxt != data->ctxt)
522 for (i = 0; i < NUM_IWL_MVM_SMPS_REQ; i++) {
523 if (link_info->smps_requests[i] == IEEE80211_SMPS_STATIC ||
524 link_info->smps_requests[i] == IEEE80211_SMPS_DYNAMIC) {
525 data->result = false;
532 bool iwl_mvm_rx_diversity_allowed(struct iwl_mvm *mvm,
533 struct iwl_mvm_phy_ctxt *ctxt)
535 struct iwl_mvm_diversity_iter_data data = {
540 lockdep_assert_held(&mvm->mutex);
542 if (iwlmvm_mod_params.power_scheme != IWL_POWER_SCHEME_CAM)
545 if (num_of_ant(iwl_mvm_get_valid_rx_ant(mvm)) == 1)
548 if (mvm->cfg->rx_with_siso_diversity)
551 ieee80211_iterate_active_interfaces_atomic(
552 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
553 iwl_mvm_diversity_iter, &data);
558 void iwl_mvm_send_low_latency_cmd(struct iwl_mvm *mvm,
559 bool low_latency, u16 mac_id)
561 struct iwl_mac_low_latency_cmd cmd = {
562 .mac_id = cpu_to_le32(mac_id)
565 if (!fw_has_capa(&mvm->fw->ucode_capa,
566 IWL_UCODE_TLV_CAPA_DYNAMIC_QUOTA))
570 /* currently we don't care about the direction */
571 cmd.low_latency_rx = 1;
572 cmd.low_latency_tx = 1;
575 if (iwl_mvm_send_cmd_pdu(mvm, WIDE_ID(MAC_CONF_GROUP, LOW_LATENCY_CMD),
576 0, sizeof(cmd), &cmd))
577 IWL_ERR(mvm, "Failed to send low latency command\n");
580 int iwl_mvm_update_low_latency(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
582 enum iwl_mvm_low_latency_cause cause)
584 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
588 lockdep_assert_held(&mvm->mutex);
590 prev = iwl_mvm_vif_low_latency(mvmvif);
591 iwl_mvm_vif_set_low_latency(mvmvif, low_latency, cause);
593 low_latency = iwl_mvm_vif_low_latency(mvmvif);
595 if (low_latency == prev)
598 iwl_mvm_send_low_latency_cmd(mvm, low_latency, mvmvif->id);
600 res = iwl_mvm_update_quotas(mvm, false, NULL);
604 iwl_mvm_bt_coex_vif_change(mvm);
606 return iwl_mvm_power_update_mac(mvm);
609 struct iwl_mvm_low_latency_iter {
611 bool result_per_band[NUM_NL80211_BANDS];
614 static void iwl_mvm_ll_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
616 struct iwl_mvm_low_latency_iter *result = _data;
617 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
618 enum nl80211_band band;
620 if (iwl_mvm_vif_low_latency(mvmvif)) {
621 result->result = true;
623 if (!mvmvif->deflink.phy_ctxt)
626 band = mvmvif->deflink.phy_ctxt->channel->band;
627 result->result_per_band[band] = true;
631 bool iwl_mvm_low_latency(struct iwl_mvm *mvm)
633 struct iwl_mvm_low_latency_iter data = {};
635 ieee80211_iterate_active_interfaces_atomic(
636 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
637 iwl_mvm_ll_iter, &data);
642 bool iwl_mvm_low_latency_band(struct iwl_mvm *mvm, enum nl80211_band band)
644 struct iwl_mvm_low_latency_iter data = {};
646 ieee80211_iterate_active_interfaces_atomic(
647 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
648 iwl_mvm_ll_iter, &data);
650 return data.result_per_band[band];
653 struct iwl_bss_iter_data {
654 struct ieee80211_vif *vif;
658 static void iwl_mvm_bss_iface_iterator(void *_data, u8 *mac,
659 struct ieee80211_vif *vif)
661 struct iwl_bss_iter_data *data = _data;
663 if (vif->type != NL80211_IFTYPE_STATION || vif->p2p)
674 struct ieee80211_vif *iwl_mvm_get_bss_vif(struct iwl_mvm *mvm)
676 struct iwl_bss_iter_data bss_iter_data = {};
678 ieee80211_iterate_active_interfaces_atomic(
679 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
680 iwl_mvm_bss_iface_iterator, &bss_iter_data);
682 if (bss_iter_data.error)
683 return ERR_PTR(-EINVAL);
685 return bss_iter_data.vif;
688 struct iwl_bss_find_iter_data {
689 struct ieee80211_vif *vif;
693 static void iwl_mvm_bss_find_iface_iterator(void *_data, u8 *mac,
694 struct ieee80211_vif *vif)
696 struct iwl_bss_find_iter_data *data = _data;
697 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
699 if (mvmvif->id == data->macid)
703 struct ieee80211_vif *iwl_mvm_get_vif_by_macid(struct iwl_mvm *mvm, u32 macid)
705 struct iwl_bss_find_iter_data data = {
709 lockdep_assert_held(&mvm->mutex);
711 ieee80211_iterate_active_interfaces_atomic(
712 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
713 iwl_mvm_bss_find_iface_iterator, &data);
718 struct iwl_sta_iter_data {
722 static void iwl_mvm_sta_iface_iterator(void *_data, u8 *mac,
723 struct ieee80211_vif *vif)
725 struct iwl_sta_iter_data *data = _data;
727 if (vif->type != NL80211_IFTYPE_STATION)
734 bool iwl_mvm_is_vif_assoc(struct iwl_mvm *mvm)
736 struct iwl_sta_iter_data data = {
740 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
741 IEEE80211_IFACE_ITER_NORMAL,
742 iwl_mvm_sta_iface_iterator,
747 unsigned int iwl_mvm_get_wd_timeout(struct iwl_mvm *mvm,
748 struct ieee80211_vif *vif)
750 unsigned int default_timeout =
751 mvm->trans->trans_cfg->base_params->wd_timeout;
754 * We can't know when the station is asleep or awake, so we
755 * must disable the queue hang detection.
757 if (fw_has_capa(&mvm->fw->ucode_capa,
758 IWL_UCODE_TLV_CAPA_STA_PM_NOTIF) &&
759 vif->type == NL80211_IFTYPE_AP)
760 return IWL_WATCHDOG_DISABLED;
761 return default_timeout;
764 void iwl_mvm_connection_loss(struct iwl_mvm *mvm, struct ieee80211_vif *vif,
767 struct iwl_fw_dbg_trigger_tlv *trig;
768 struct iwl_fw_dbg_trigger_mlme *trig_mlme;
770 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
771 FW_DBG_TRIGGER_MLME);
775 trig_mlme = (void *)trig->data;
777 if (trig_mlme->stop_connection_loss &&
778 --trig_mlme->stop_connection_loss)
781 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig, "%s", errmsg);
784 ieee80211_connection_loss(vif);
787 void iwl_mvm_event_frame_timeout_callback(struct iwl_mvm *mvm,
788 struct ieee80211_vif *vif,
789 const struct ieee80211_sta *sta,
792 struct iwl_fw_dbg_trigger_tlv *trig;
793 struct iwl_fw_dbg_trigger_ba *ba_trig;
795 trig = iwl_fw_dbg_trigger_on(&mvm->fwrt, ieee80211_vif_to_wdev(vif),
800 ba_trig = (void *)trig->data;
802 if (!(le16_to_cpu(ba_trig->frame_timeout) & BIT(tid)))
805 iwl_fw_dbg_collect_trig(&mvm->fwrt, trig,
806 "Frame from %pM timed out, tid %d",
810 u8 iwl_mvm_tcm_load_percentage(u32 airtime, u32 elapsed)
815 return (100 * airtime / elapsed) / USEC_PER_MSEC;
818 static enum iwl_mvm_traffic_load
819 iwl_mvm_tcm_load(struct iwl_mvm *mvm, u32 airtime, unsigned long elapsed)
821 u8 load = iwl_mvm_tcm_load_percentage(airtime, elapsed);
823 if (load > IWL_MVM_TCM_LOAD_HIGH_THRESH)
824 return IWL_MVM_TRAFFIC_HIGH;
825 if (load > IWL_MVM_TCM_LOAD_MEDIUM_THRESH)
826 return IWL_MVM_TRAFFIC_MEDIUM;
828 return IWL_MVM_TRAFFIC_LOW;
831 static void iwl_mvm_tcm_iter(void *_data, u8 *mac, struct ieee80211_vif *vif)
833 struct iwl_mvm *mvm = _data;
834 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
835 bool low_latency, prev = mvmvif->low_latency & LOW_LATENCY_TRAFFIC;
837 if (mvmvif->id >= NUM_MAC_INDEX_DRIVER)
840 low_latency = mvm->tcm.result.low_latency[mvmvif->id];
842 if (!mvm->tcm.result.change[mvmvif->id] &&
843 prev == low_latency) {
844 iwl_mvm_update_quotas(mvm, false, NULL);
848 if (prev != low_latency) {
849 /* this sends traffic load and updates quota as well */
850 iwl_mvm_update_low_latency(mvm, vif, low_latency,
851 LOW_LATENCY_TRAFFIC);
853 iwl_mvm_update_quotas(mvm, false, NULL);
857 static void iwl_mvm_tcm_results(struct iwl_mvm *mvm)
861 ieee80211_iterate_active_interfaces(
862 mvm->hw, IEEE80211_IFACE_ITER_NORMAL,
863 iwl_mvm_tcm_iter, mvm);
865 if (fw_has_capa(&mvm->fw->ucode_capa, IWL_UCODE_TLV_CAPA_UMAC_SCAN))
866 iwl_mvm_config_scan(mvm);
869 static void iwl_mvm_tcm_uapsd_nonagg_detected_wk(struct work_struct *wk)
872 struct iwl_mvm_vif *mvmvif;
873 struct ieee80211_vif *vif;
875 mvmvif = container_of(wk, struct iwl_mvm_vif,
876 uapsd_nonagg_detected_wk.work);
877 vif = container_of((void *)mvmvif, struct ieee80211_vif, drv_priv);
880 if (mvm->tcm.data[mvmvif->id].opened_rx_ba_sessions)
883 /* remember that this AP is broken */
884 memcpy(mvm->uapsd_noagg_bssids[mvm->uapsd_noagg_bssid_write_idx].addr,
885 vif->bss_conf.bssid, ETH_ALEN);
886 mvm->uapsd_noagg_bssid_write_idx++;
887 if (mvm->uapsd_noagg_bssid_write_idx >= IWL_MVM_UAPSD_NOAGG_LIST_LEN)
888 mvm->uapsd_noagg_bssid_write_idx = 0;
890 iwl_mvm_connection_loss(mvm, vif,
891 "AP isn't using AMPDU with uAPSD enabled");
894 static void iwl_mvm_uapsd_agg_disconnect(struct iwl_mvm *mvm,
895 struct ieee80211_vif *vif)
897 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
899 if (vif->type != NL80211_IFTYPE_STATION)
905 if (!mvmvif->deflink.queue_params[IEEE80211_AC_VO].uapsd &&
906 !mvmvif->deflink.queue_params[IEEE80211_AC_VI].uapsd &&
907 !mvmvif->deflink.queue_params[IEEE80211_AC_BE].uapsd &&
908 !mvmvif->deflink.queue_params[IEEE80211_AC_BK].uapsd)
911 if (mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected)
914 mvm->tcm.data[mvmvif->id].uapsd_nonagg_detect.detected = true;
916 "detected AP should do aggregation but isn't, likely due to U-APSD\n");
917 schedule_delayed_work(&mvmvif->uapsd_nonagg_detected_wk,
921 static void iwl_mvm_check_uapsd_agg_expected_tpt(struct iwl_mvm *mvm,
922 unsigned int elapsed,
925 u64 bytes = mvm->tcm.data[mac].uapsd_nonagg_detect.rx_bytes;
928 struct ieee80211_vif *vif;
930 rate = ewma_rate_read(&mvm->tcm.data[mac].uapsd_nonagg_detect.rate);
932 if (!rate || mvm->tcm.data[mac].opened_rx_ba_sessions ||
933 mvm->tcm.data[mac].uapsd_nonagg_detect.detected)
936 if (iwl_mvm_has_new_rx_api(mvm)) {
937 tpt = 8 * bytes; /* kbps */
938 do_div(tpt, elapsed);
939 rate *= 1000; /* kbps */
940 if (tpt < 22 * rate / 100)
944 * the rate here is actually the threshold, in 100Kbps units,
945 * so do the needed conversion from bytes to 100Kbps:
946 * 100kb = bits / (100 * 1000),
947 * 100kbps = 100kb / (msecs / 1000) ==
948 * (bits / (100 * 1000)) / (msecs / 1000) ==
949 * bits / (100 * msecs)
952 do_div(tpt, elapsed * 100);
958 vif = rcu_dereference(mvm->vif_id_to_mac[mac]);
960 iwl_mvm_uapsd_agg_disconnect(mvm, vif);
964 static void iwl_mvm_tcm_iterator(void *_data, u8 *mac,
965 struct ieee80211_vif *vif)
967 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
970 if (!mvmvif->deflink.phy_ctxt)
973 band[mvmvif->id] = mvmvif->deflink.phy_ctxt->channel->band;
976 static unsigned long iwl_mvm_calc_tcm_stats(struct iwl_mvm *mvm,
980 unsigned int elapsed = jiffies_to_msecs(ts - mvm->tcm.ts);
981 unsigned int uapsd_elapsed =
982 jiffies_to_msecs(ts - mvm->tcm.uapsd_nonagg_ts);
983 u32 total_airtime = 0;
984 u32 band_airtime[NUM_NL80211_BANDS] = {0};
985 u32 band[NUM_MAC_INDEX_DRIVER] = {0};
987 bool low_latency = false;
988 enum iwl_mvm_traffic_load load, band_load;
989 bool handle_ll = time_after(ts, mvm->tcm.ll_ts + MVM_LL_PERIOD);
994 mvm->tcm.uapsd_nonagg_ts = ts;
996 mvm->tcm.result.elapsed = elapsed;
998 ieee80211_iterate_active_interfaces_atomic(mvm->hw,
999 IEEE80211_IFACE_ITER_NORMAL,
1000 iwl_mvm_tcm_iterator,
1003 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1004 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1006 u32 airtime = mdata->rx.airtime + mdata->tx.airtime;
1008 total_airtime += airtime;
1009 band_airtime[band[mac]] += airtime;
1011 load = iwl_mvm_tcm_load(mvm, airtime, elapsed);
1012 mvm->tcm.result.change[mac] = load != mvm->tcm.result.load[mac];
1013 mvm->tcm.result.load[mac] = load;
1014 mvm->tcm.result.airtime[mac] = airtime;
1016 for (ac = IEEE80211_AC_VO; ac <= IEEE80211_AC_VI; ac++)
1017 vo_vi_pkts += mdata->rx.pkts[ac] +
1020 /* enable immediately with enough packets but defer disabling */
1021 if (vo_vi_pkts > IWL_MVM_TCM_LOWLAT_ENABLE_THRESH)
1022 mvm->tcm.result.low_latency[mac] = true;
1024 mvm->tcm.result.low_latency[mac] = false;
1027 /* clear old data */
1028 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1029 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1031 low_latency |= mvm->tcm.result.low_latency[mac];
1033 if (!mvm->tcm.result.low_latency[mac] && handle_uapsd)
1034 iwl_mvm_check_uapsd_agg_expected_tpt(mvm, uapsd_elapsed,
1036 /* clear old data */
1038 mdata->uapsd_nonagg_detect.rx_bytes = 0;
1039 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1040 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1043 load = iwl_mvm_tcm_load(mvm, total_airtime, elapsed);
1044 mvm->tcm.result.global_load = load;
1046 for (i = 0; i < NUM_NL80211_BANDS; i++) {
1047 band_load = iwl_mvm_tcm_load(mvm, band_airtime[i], elapsed);
1048 mvm->tcm.result.band_load[i] = band_load;
1052 * If the current load isn't low we need to force re-evaluation
1053 * in the TCM period, so that we can return to low load if there
1054 * was no traffic at all (and thus iwl_mvm_recalc_tcm didn't get
1055 * triggered by traffic).
1057 if (load != IWL_MVM_TRAFFIC_LOW)
1058 return MVM_TCM_PERIOD;
1060 * If low-latency is active we need to force re-evaluation after
1061 * (the longer) MVM_LL_PERIOD, so that we can disable low-latency
1062 * when there's no traffic at all.
1065 return MVM_LL_PERIOD;
1067 * Otherwise, we don't need to run the work struct because we're
1068 * in the default "idle" state - traffic indication is low (which
1069 * also covers the "no traffic" case) and low-latency is disabled
1070 * so there's no state that may need to be disabled when there's
1071 * no traffic at all.
1073 * Note that this has no impact on the regular scheduling of the
1074 * updates triggered by traffic - those happen whenever one of the
1075 * two timeouts expire (if there's traffic at all.)
1080 void iwl_mvm_recalc_tcm(struct iwl_mvm *mvm)
1082 unsigned long ts = jiffies;
1084 time_after(ts, mvm->tcm.uapsd_nonagg_ts +
1085 msecs_to_jiffies(IWL_MVM_UAPSD_NONAGG_PERIOD));
1087 spin_lock(&mvm->tcm.lock);
1088 if (mvm->tcm.paused || !time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1089 spin_unlock(&mvm->tcm.lock);
1092 spin_unlock(&mvm->tcm.lock);
1094 if (handle_uapsd && iwl_mvm_has_new_rx_api(mvm)) {
1096 if (iwl_mvm_request_statistics(mvm, true))
1097 handle_uapsd = false;
1100 spin_lock(&mvm->tcm.lock);
1101 /* re-check if somebody else won the recheck race */
1102 if (!mvm->tcm.paused && time_after(ts, mvm->tcm.ts + MVM_TCM_PERIOD)) {
1103 /* calculate statistics */
1104 unsigned long work_delay = iwl_mvm_calc_tcm_stats(mvm, ts,
1107 /* the memset needs to be visible before the timestamp */
1111 schedule_delayed_work(&mvm->tcm.work, work_delay);
1113 spin_unlock(&mvm->tcm.lock);
1115 iwl_mvm_tcm_results(mvm);
1118 void iwl_mvm_tcm_work(struct work_struct *work)
1120 struct delayed_work *delayed_work = to_delayed_work(work);
1121 struct iwl_mvm *mvm = container_of(delayed_work, struct iwl_mvm,
1124 iwl_mvm_recalc_tcm(mvm);
1127 void iwl_mvm_pause_tcm(struct iwl_mvm *mvm, bool with_cancel)
1129 spin_lock_bh(&mvm->tcm.lock);
1130 mvm->tcm.paused = true;
1131 spin_unlock_bh(&mvm->tcm.lock);
1133 cancel_delayed_work_sync(&mvm->tcm.work);
1136 void iwl_mvm_resume_tcm(struct iwl_mvm *mvm)
1139 bool low_latency = false;
1141 spin_lock_bh(&mvm->tcm.lock);
1142 mvm->tcm.ts = jiffies;
1143 mvm->tcm.ll_ts = jiffies;
1144 for (mac = 0; mac < NUM_MAC_INDEX_DRIVER; mac++) {
1145 struct iwl_mvm_tcm_mac *mdata = &mvm->tcm.data[mac];
1147 memset(&mdata->rx.pkts, 0, sizeof(mdata->rx.pkts));
1148 memset(&mdata->tx.pkts, 0, sizeof(mdata->tx.pkts));
1149 memset(&mdata->rx.airtime, 0, sizeof(mdata->rx.airtime));
1150 memset(&mdata->tx.airtime, 0, sizeof(mdata->tx.airtime));
1152 if (mvm->tcm.result.low_latency[mac])
1155 /* The TCM data needs to be reset before "paused" flag changes */
1157 mvm->tcm.paused = false;
1160 * if the current load is not low or low latency is active, force
1161 * re-evaluation to cover the case of no traffic.
1163 if (mvm->tcm.result.global_load > IWL_MVM_TRAFFIC_LOW)
1164 schedule_delayed_work(&mvm->tcm.work, MVM_TCM_PERIOD);
1165 else if (low_latency)
1166 schedule_delayed_work(&mvm->tcm.work, MVM_LL_PERIOD);
1168 spin_unlock_bh(&mvm->tcm.lock);
1171 void iwl_mvm_tcm_add_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1173 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1175 INIT_DELAYED_WORK(&mvmvif->uapsd_nonagg_detected_wk,
1176 iwl_mvm_tcm_uapsd_nonagg_detected_wk);
1179 void iwl_mvm_tcm_rm_vif(struct iwl_mvm *mvm, struct ieee80211_vif *vif)
1181 struct iwl_mvm_vif *mvmvif = iwl_mvm_vif_from_mac80211(vif);
1183 cancel_delayed_work_sync(&mvmvif->uapsd_nonagg_detected_wk);
1186 u32 iwl_mvm_get_systime(struct iwl_mvm *mvm)
1188 u32 reg_addr = DEVICE_SYSTEM_TIME_REG;
1190 if (mvm->trans->trans_cfg->device_family >= IWL_DEVICE_FAMILY_22000 &&
1191 mvm->trans->cfg->gp2_reg_addr)
1192 reg_addr = mvm->trans->cfg->gp2_reg_addr;
1194 return iwl_read_prph(mvm->trans, reg_addr);
1197 void iwl_mvm_get_sync_time(struct iwl_mvm *mvm, int clock_type,
1198 u32 *gp2, u64 *boottime, ktime_t *realtime)
1202 lockdep_assert_held(&mvm->mutex);
1204 /* Disable power save when reading GP2 */
1205 ps_disabled = mvm->ps_disabled;
1207 mvm->ps_disabled = true;
1208 iwl_mvm_power_update_device(mvm);
1211 *gp2 = iwl_mvm_get_systime(mvm);
1213 if (clock_type == CLOCK_BOOTTIME && boottime)
1214 *boottime = ktime_get_boottime_ns();
1215 else if (clock_type == CLOCK_REALTIME && realtime)
1216 *realtime = ktime_get_real();
1219 mvm->ps_disabled = ps_disabled;
1220 iwl_mvm_power_update_device(mvm);
1224 /* Find if at least two links from different vifs use same channel
1225 * FIXME: consider having a refcount array in struct iwl_mvm_vif for
1226 * used phy_ctxt ids.
1228 bool iwl_mvm_have_links_same_channel(struct iwl_mvm_vif *vif1,
1229 struct iwl_mvm_vif *vif2)
1233 for_each_mvm_vif_valid_link(vif1, i) {
1234 for_each_mvm_vif_valid_link(vif2, j) {
1235 if (vif1->link[i]->phy_ctxt == vif2->link[j]->phy_ctxt)
1243 bool iwl_mvm_vif_is_active(struct iwl_mvm_vif *mvmvif)
1247 /* FIXME: can it fail when phy_ctxt is assigned? */
1248 for_each_mvm_vif_valid_link(mvmvif, i) {
1249 if (mvmvif->link[i]->phy_ctxt &&
1250 mvmvif->link[i]->phy_ctxt->id < NUM_PHY_CTX)