xref: /linux/drivers/net/wireless/morsemicro/mm81x/mac.c (revision 2d7eaeb05ae265bd64270376e6a2c0d80244771f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2017-2026 Morse Micro
4  */
5 #include "core.h"
6 #include <linux/slab.h>
7 #include <linux/jiffies.h>
8 #include <linux/crc32.h>
9 #include <net/mac80211.h>
10 #include <net/ieee80211_radiotap.h>
11 #include <asm/div64.h>
12 #include <linux/kernel.h>
13 #include "hif.h"
14 #include "mac.h"
15 #include "bus.h"
16 #include "ps.h"
17 #include "rc.h"
18 
19 /*
20  * Arbitrary size limit for the filter command address list, to ensure that
21  * the command does not exceed page/MTU size. This will be far greater than
22  * the number of filters supported by the firmware.
23  */
24 #define MCAST_FILTER_COUNT_MAX (1024 / sizeof(filter->addr_list[0]))
25 
26 /* Calculate average RSSI for Rx status */
27 #define CALC_AVG_RSSI(_avg, _sample) ((((_avg) * 9 + (_sample)) / 10))
28 
29 /*
30  * When automatically trying MCS0 before MCS10, this is how many
31  * MCS0 attempts to make
32  */
33 #define MCS0_BEFORE_MCS10_COUNT (1)
34 
35 /* Maximum TX power (default) */
36 #define MAX_TX_POWER_MBM (2200)
37 
38 /*
39  * Since S1G runs at 1/10th the clockrate of VHT, the worst-case
40  * transmission time is significantly longer then that of non-S1G
41  * PHYs.
42  */
43 #define MM81X_FLUSH_TIMEOUT (16 * HZ)
44 
45 /* Default queue count */
46 #define MM81X_HW_QUEUE_COUNT (4)
47 
48 /* Max rates per skb */
49 #define MM81X_HW_MAX_RATES (4)
50 
51 /* Max reported rates */
52 #define MM81X_HW_MAX_REPORT_RATES (4)
53 
54 /* Max rate attempts */
55 #define MM81X_HW_MAX_RATE_TRIES (1)
56 
57 /* Max sk pacing shift */
58 #define MM81X_HW_TX_SK_PACING_SHIFT (3)
59 
60 /* NSS/MCS map values */
61 #define MM81X_NSS_MCS_BYTE_0 0xfe /* 1SS */
62 #define MM81X_NSS_MCS_BYTE_1 0x00
63 #define MM81X_NSS_MCS_BYTE_2 0xfc /* 1SS */
64 #define MM81X_NSS_MCS_BYTE_3 0x01
65 #define MM81X_NSS_MCS_BYTE_4 0x00
66 
67 /* HW restart delay time before terminating hardware IF work items */
68 #define MM81X_HW_RESTART_DELAY_MS 20
69 
70 /* Maximum number of multicast frames to release after a DTIM beacon */
71 #define MM81X_MAX_MC_FRAMES_AFTER_DTIM 4
72 
73 /* clang-format off */
74 
75 /* mm81x chips do not support 16MHz */
76 #define CHANS1G(channel, frequency, offset, chan_flags)		\
77 {								\
78 	.band = NL80211_BAND_S1GHZ,				\
79 	.center_freq = (frequency),				\
80 	.freq_offset = (offset),				\
81 	.hw_value = (channel),					\
82 	.flags = ((chan_flags) | IEEE80211_CHAN_NO_16MHZ),	\
83 	.max_antenna_gain = 0,					\
84 	.max_power = 30,					\
85 }
86 
87 static const struct ieee80211_channel mors_s1ghz_channels[] = {
88 	CHANS1G(1, 902, 500, IEEE80211_CHAN_S1G_NO_PRIMARY),
89 	CHANS1G(3, 903, 500, 0),
90 	CHANS1G(5, 904, 500, 0),
91 	CHANS1G(7, 905, 500, 0),
92 	CHANS1G(9, 906, 500, 0),
93 	CHANS1G(11, 907, 500, 0),
94 	CHANS1G(13, 908, 500, 0),
95 	CHANS1G(15, 909, 500, 0),
96 	CHANS1G(17, 910, 500, 0),
97 	CHANS1G(19, 911, 500, 0),
98 	CHANS1G(21, 912, 500, 0),
99 	CHANS1G(23, 913, 500, 0),
100 	CHANS1G(25, 914, 500, 0),
101 	CHANS1G(27, 915, 500, 0),
102 	CHANS1G(29, 916, 500, 0),
103 	CHANS1G(31, 917, 500, 0),
104 	CHANS1G(33, 918, 500, 0),
105 	CHANS1G(35, 919, 500, 0),
106 	CHANS1G(37, 920, 500, 0),
107 	CHANS1G(39, 921, 500, 0),
108 	CHANS1G(41, 922, 500, 0),
109 	CHANS1G(43, 923, 500, 0),
110 	CHANS1G(45, 924, 500, 0),
111 	CHANS1G(47, 925, 500, 0),
112 	CHANS1G(49, 926, 500, 0),
113 	CHANS1G(51, 927, 500, IEEE80211_CHAN_S1G_NO_PRIMARY),
114 };
115 
116 /* clang-format on */
117 
118 static const struct ieee80211_supported_band mors_band_s1ghz = {
119 	.band = NL80211_BAND_S1GHZ,
120 	.s1g_cap.s1g = true,
121 	.bitrates = NULL,
122 	.n_bitrates = 0,
123 	.s1g_cap.cap[4] = 0x80 /* STA type sensor only for AP & STA */
124 };
125 
126 static struct ieee80211_iface_limit mors_if_limits[] = {
127 	{
128 		.max = MM81X_MAX_IF,
129 		.types = BIT(NL80211_IFTYPE_STATION) | BIT(NL80211_IFTYPE_AP),
130 	},
131 };
132 
133 static struct ieee80211_iface_combination mors_if_combs[] = {
134 	{
135 		.limits = mors_if_limits,
136 		.n_limits = ARRAY_SIZE(mors_if_limits),
137 		.max_interfaces = MM81X_MAX_IF,
138 		.num_different_channels = 1,
139 	},
140 };
141 
142 /* Convert from a time in time units (1024us) to us */
143 #define MM81X_TU_TO_US(x) ((x) * 1024UL)
144 
145 /* Convert from a time in time units (1024us) to ms */
146 #define MM81X_TU_TO_MS(x) (MM81X_TU_TO_US(x) / 1000UL)
147 
148 /* Default time to dwell on a scan channel */
149 #define MM81X_HWSCAN_DEFAULT_DWELL_TIME_MS (30)
150 
151 /* Default time to dwell on a scan channel for passive scan */
152 #define MM81X_HWSCAN_DEFAULT_PASSIVE_DWELL_TIME_MS (110)
153 
154 /* Default time to dwell on home channel, in between scan channels */
155 #define MM81X_HWSCAN_DEFAULT_DWELL_ON_HOME_MS (200)
156 
157 /* Typical time it takes to send the probe */
158 #define MM81X_HWSCAN_PROBE_DELAY_MS (30)
159 
160 /* A margin to account for event/command processing */
161 #define MM81X_HWSCAN_TIMEOUT_OVERHEAD_MS (2000)
162 
163 /* Scan channel frequency mask */
164 #define HW_SCAN_CH_LIST_FREQ_KHZ GENMASK(19, 0)
165 
166 /*
167  * Scan channel bandwidth mask.
168  * Encoded as: 0 = 1MHz, 1 = 2MHz, 2 = 4MHz, 3 = 8MHz
169  */
170 #define HW_SCAN_CH_LIST_OP_BW GENMASK(21, 20)
171 
172 /*
173  * Scan channel primary channel width.
174  * Encoded as: 0 = 1MHz, 1 = 2MHz
175  */
176 #define HW_SCAN_CH_LIST_PRIM_CH_WIDTH BIT(22)
177 
178 /* Index into power_list for tx power of channel */
179 #define HW_SCAN_CH_LIST_PWR_LIST_IDX GENMASK(31, 26)
180 
181 struct hw_scan_tlv_hdr {
182 	__le16 tag;
183 	__le16 len;
184 } __packed;
185 
186 struct hw_scan_tlv_channel_list {
187 	struct hw_scan_tlv_hdr hdr;
188 	__le32 channels[];
189 } __packed;
190 
191 struct hw_scan_tlv_power_list {
192 	struct hw_scan_tlv_hdr hdr;
193 	s32 tx_power_qdbm[];
194 } __packed;
195 
196 struct hw_scan_tlv_probe_req {
197 	struct hw_scan_tlv_hdr hdr;
198 	/* Probe request frame template (including SSIDs) */
199 	u8 buf[];
200 } __packed;
201 
202 struct hw_scan_tlv_dwell_on_home {
203 	struct hw_scan_tlv_hdr hdr;
204 	/* Time to dwell on home between scan channels */
205 	__le32 home_dwell_time_ms;
206 } __packed;
207 
208 #define DOT11AH_BA_MAX_MPDU_PER_AMPDU (32)
209 
210 /* wiphy scan params */
211 #define MM81X_MAX_SCAN_IE_LEN 512
212 #define MM81X_MAX_SCAN_SSIDS 1
213 #define MM81X_MAX_REMAIN_ON_CHAN_DURATION 10000
214 
215 static bool mm81x_reg_h_cc_equal(const char *cc1, const char *cc2)
216 {
217 	return (cc1[0] == cc2[0]) && (cc1[1] == cc2[1]);
218 }
219 
220 static bool mm81x_tx_h_pkt_over_rts_threshold(struct mm81x *mors,
221 					      struct ieee80211_tx_info *info,
222 					      struct sk_buff *skb)
223 {
224 	u8 ccmp_len;
225 
226 	if (!info->control.hw_key)
227 		return ((skb->len + FCS_LEN) > mors->rts_threshold);
228 
229 	if (info->control.hw_key->keylen == 32)
230 		ccmp_len =
231 			IEEE80211_CCMP_256_HDR_LEN + IEEE80211_CCMP_256_MIC_LEN;
232 	else if (info->control.hw_key->keylen == 16)
233 		ccmp_len = IEEE80211_CCMP_HDR_LEN + IEEE80211_CCMP_MIC_LEN;
234 	else
235 		ccmp_len = 0;
236 
237 	return ((skb->len + FCS_LEN + ccmp_len) > mors->rts_threshold);
238 }
239 
240 static bool mm81x_tx_h_ps_filtered_for_sta(struct mm81x *mors,
241 					   struct sk_buff *skb,
242 					   struct ieee80211_sta *sta)
243 {
244 	struct mm81x_sta *mors_sta;
245 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
246 
247 	if (!sta)
248 		return false;
249 
250 	mors_sta = (struct mm81x_sta *)sta->drv_priv;
251 
252 	if (!mors_sta->tx_ps_filter_en)
253 		return false;
254 
255 	dev_dbg(mors->dev, "Frame for sta[%pM] PS filtered", mors_sta->addr);
256 
257 	info->flags |= IEEE80211_TX_STAT_TX_FILTERED;
258 	info->flags &= ~IEEE80211_TX_CTL_AMPDU;
259 
260 	ieee80211_tx_status_skb(mors->hw, skb);
261 	return true;
262 }
263 
264 static void mm81x_mac_check_fw_disabled_chans(struct ieee80211_hw *hw)
265 {
266 	int ret = 0;
267 	u32 i;
268 	struct mm81x *mors = hw->priv;
269 	struct host_cmd_resp_get_disabled_channels *resp;
270 	struct ieee80211_supported_band *sband =
271 		hw->wiphy->bands[NL80211_BAND_S1GHZ];
272 	u32 resp_len = sizeof(struct host_cmd_disabled_channel_entry) *
273 			       sband->n_channels +
274 		       sizeof(*resp);
275 
276 	resp = kzalloc(resp_len, GFP_KERNEL);
277 	if (!resp) {
278 		ret = -ENOMEM;
279 		goto out;
280 	}
281 
282 	ret = mm81x_cmd_get_disabled_channels(mors, resp, resp_len);
283 	if (ret)
284 		goto out;
285 
286 	if (le32_to_cpu(resp->n_channels) > sband->n_channels) {
287 		ret = -EINVAL;
288 		goto out;
289 	}
290 
291 	for (i = 0; i < sband->n_channels; i++) {
292 		struct ieee80211_channel *ch = &sband->channels[i];
293 
294 		if (ch->flags & IEEE80211_CHAN_DISABLED)
295 			continue;
296 
297 		ch->flags &= ~IEEE80211_CHAN_S1G_NO_PRIMARY;
298 	}
299 
300 	for (i = 0; i < le32_to_cpu(resp->n_channels); i++) {
301 		struct ieee80211_channel *ch;
302 		struct host_cmd_disabled_channel_entry *entry =
303 			&resp->channels[i];
304 
305 		if (entry->bw_mhz != 1)
306 			continue;
307 
308 		ch = ieee80211_get_channel_khz(
309 			hw->wiphy,
310 			KHZ100_TO_KHZ(le16_to_cpu(entry->freq_100khz)));
311 		if (!ch)
312 			continue;
313 
314 		ch->flags |= IEEE80211_CHAN_S1G_NO_PRIMARY;
315 		dev_dbg(mors->dev, "set NO_PRIMARY on %u KHz",
316 			ieee80211_channel_to_khz(ch));
317 	}
318 
319 out:
320 	if (ret)
321 		dev_err(mors->dev, "failed to set disabled primary channels");
322 
323 	kfree(resp);
324 }
325 
326 static int mm81x_mac_ops_start(struct ieee80211_hw *hw)
327 {
328 	struct mm81x *mors = hw->priv;
329 
330 	mors->started = true;
331 	return 0;
332 }
333 
334 static int mm81x_tx_h_get_max_tx_bw(struct mm81x *mors)
335 {
336 	return MM81X_FW_SUPP(&mors->fw_caps, 8MHZ) ? 8 :
337 	       MM81X_FW_SUPP(&mors->fw_caps, 4MHZ) ? 4 :
338 	       MM81X_FW_SUPP(&mors->fw_caps, 2MHZ) ? 2 :
339 						     1;
340 }
341 
342 static void mm81x_mac_caps_init(struct mm81x *mors)
343 {
344 	struct mm81x_fw_caps *fw_caps = &mors->fw_caps;
345 	struct ieee80211_sta_s1g_cap *s1g =
346 		&mors->hw->wiphy->bands[NL80211_BAND_S1GHZ]->s1g_cap;
347 
348 #define __FW_CAP_N(_n, _cap, _bit)                \
349 	do {                                      \
350 		if (MM81X_FW_SUPP(fw_caps, _cap)) \
351 			s1g->cap[_n] |= (_bit);   \
352 	} while (0)
353 
354 #define FW_CAP0(_cap, _bit) __FW_CAP_N(0, _cap, _bit)
355 #define FW_CAP3(_cap, _bit) __FW_CAP_N(3, _cap, _bit)
356 #define FW_CAP5(_cap, _bit) __FW_CAP_N(5, _cap, _bit)
357 #define FW_CAP6(_cap, _bit) __FW_CAP_N(6, _cap, _bit)
358 #define FW_CAP7(_cap, _bit) __FW_CAP_N(7, _cap, _bit)
359 #define FW_CAP8(_cap, _bit) __FW_CAP_N(8, _cap, _bit)
360 #define FW_CAP9(_cap, _bit) __FW_CAP_N(9, _cap, _bit)
361 
362 	FW_CAP0(S1G_LONG, S1G_CAP0_S1G_LONG);
363 
364 	s1g->cap[0] |= S1G_CAP0_SGI_1MHZ;
365 	if (MM81X_FW_SUPP(fw_caps, SGI)) {
366 		FW_CAP0(2MHZ, S1G_CAP0_SGI_2MHZ);
367 		FW_CAP0(4MHZ, S1G_CAP0_SGI_4MHZ);
368 		FW_CAP0(8MHZ, S1G_CAP0_SGI_8MHZ);
369 	}
370 
371 	if (MM81X_FW_SUPP(fw_caps, 8MHZ))
372 		s1g->cap[0] |= S1G_SUPP_CH_WIDTH_8;
373 	else if (MM81X_FW_SUPP(fw_caps, 4MHZ))
374 		s1g->cap[0] |= S1G_SUPP_CH_WIDTH_4;
375 	else if (MM81X_FW_SUPP(fw_caps, 2MHZ))
376 		s1g->cap[0] |= S1G_SUPP_CH_WIDTH_2;
377 
378 	FW_CAP3(RD_RESPONDER, S1G_CAP3_RD_RESPONDER);
379 	FW_CAP3(LONG_MPDU, S1G_CAP3_MAX_MPDU_LEN);
380 
381 	FW_CAP5(AMSDU, S1G_CAP5_AMSDU);
382 	FW_CAP5(AMPDU, S1G_CAP5_AMPDU);
383 	FW_CAP5(ASYMMETRIC_BA_SUPPORT, S1G_CAP5_ASYMMETRIC_BA);
384 	FW_CAP5(FLOW_CONTROL, S1G_CAP5_FLOW_CONTROL);
385 
386 	FW_CAP6(OBSS_MITIGATION, S1G_CAP6_OBSS_MITIGATION);
387 	FW_CAP6(FRAGMENT_BA, S1G_CAP6_FRAGMENT_BA);
388 	FW_CAP6(NDP_PSPOLL, S1G_CAP6_NDP_PS_POLL);
389 	FW_CAP6(TXOP_SHARING_IMPLICIT_ACK, S1G_CAP6_TXOP_SHARING_IMP_ACK);
390 	FW_CAP6(HTC_VHT_MFB, S1G_CAP6_VHT_LINK_ADAPT);
391 
392 	FW_CAP7(TACK_AS_PSPOLL, S1G_CAP7_TACK_AS_PS_POLL);
393 	FW_CAP7(DUPLICATE_1MHZ, S1G_CAP7_DUP_1MHZ);
394 	FW_CAP7(MCS_NEGOTIATION, S1G_CAP7_MCS_NEGOTIATION);
395 	FW_CAP7(1MHZ_CONTROL_RESPONSE_PREAMBLE,
396 		S1G_CAP7_1MHZ_CTL_RESPONSE_PREAMBLE);
397 	FW_CAP7(SECTOR_TRAINING, S1G_CAP7_SECTOR_TRAINING_OPERATION);
398 	FW_CAP7(TMP_PS_MODE_SWITCH, S1G_CAP7_TEMP_PS_MODE_SWITCH);
399 
400 	FW_CAP8(BDT, S1G_CAP8_BDT);
401 
402 	FW_CAP9(LINK_ADAPTATION_WO_NDP_CMAC,
403 		S1G_CAP9_LINK_ADAPT_PER_CONTROL_RESPONSE);
404 
405 	/* 1SS MCS 9 for Rx / Tx map */
406 	s1g->nss_mcs[0] = MM81X_NSS_MCS_BYTE_0;
407 	s1g->nss_mcs[1] = MM81X_NSS_MCS_BYTE_1;
408 	s1g->nss_mcs[2] = MM81X_NSS_MCS_BYTE_2;
409 	s1g->nss_mcs[3] = MM81X_NSS_MCS_BYTE_3;
410 	s1g->nss_mcs[4] = MM81X_NSS_MCS_BYTE_4;
411 
412 #undef FW_CAP0
413 #undef FW_CAP3
414 #undef FW_CAP5
415 #undef FW_CAP6
416 #undef FW_CAP7
417 #undef FW_CAP8
418 #undef FW_CAP9
419 #undef __FW_CAP_N
420 }
421 
422 static void mm81x_mac_beacon_irq_enable(struct mm81x_vif *mors_vif, bool enable)
423 {
424 	struct mm81x *mors = mm81x_vif_to_mors(mors_vif);
425 	u8 beacon_irq_num = MM81X_INT_BEACON_BASE_NUM + mors_vif->id;
426 
427 	enable ? set_bit(beacon_irq_num, &mors->beacon_irqs_enabled) :
428 		 clear_bit(beacon_irq_num, &mors->beacon_irqs_enabled);
429 
430 	mm81x_hw_irq_enable(mors, beacon_irq_num, enable);
431 }
432 
433 static void mm81x_beacon_h_fill_tx_info(struct mm81x *mors,
434 					struct mm81x_skb_tx_info *tx_info,
435 					struct mm81x_vif *mors_vif,
436 					int tx_bw_mhz)
437 {
438 	enum dot11_bandwidth bw_idx =
439 		mm81x_ratecode_bw_mhz_to_bw_index(tx_bw_mhz);
440 	enum mm81x_rate_preamble pream = MM81X_RATE_PREAMBLE_S1G_SHORT;
441 
442 	tx_info->flags |=
443 		cpu_to_le32(MM81X_TX_CONF_FLAGS_VIF_ID_SET(mors_vif->id));
444 
445 	if (bw_idx == DOT11_BANDWIDTH_1MHZ)
446 		pream = MM81X_RATE_PREAMBLE_S1G_1M;
447 
448 	tx_info->rates[0].count = 1;
449 	tx_info->rates[1].count = 0;
450 	tx_info->rates[0].mm81x_ratecode =
451 		mm81x_ratecode_init(bw_idx, 0, 0, pream);
452 
453 	if (mors->fw_flags & MM81X_FW_FLAGS_REPORTS_TX_BEACON_COMPLETION)
454 		tx_info->flags |=
455 			cpu_to_le32(MM81X_TX_CONF_FLAGS_IMMEDIATE_REPORT);
456 }
457 
458 void mm81x_mac_beacon_irq_handle(struct mm81x *mors, u32 status)
459 {
460 	int vif_id;
461 	unsigned long masked_status = (status & mors->beacon_irqs_enabled) >>
462 				      MM81X_INT_BEACON_BASE_NUM;
463 
464 	guard(rcu)();
465 	for_each_set_bit(vif_id, &masked_status, MM81X_MAX_IF) {
466 		struct mm81x_vif *mors_vif;
467 		struct ieee80211_vif *vif;
468 
469 		vif = mm81x_rcu_dereference_vif_id(mors, vif_id, true);
470 		if (vif) {
471 			mors_vif = ieee80211_vif_to_mors_vif(vif);
472 			queue_work(system_bh_wq, &mors_vif->u.ap.beacon_work);
473 		}
474 	}
475 }
476 
477 static struct hw_scan_tlv_hdr mm81x_hw_scan_h_pack_tlv_hdr(u16 tag, u16 len)
478 {
479 	struct hw_scan_tlv_hdr hdr = { .tag = cpu_to_le16(tag),
480 				       .len = cpu_to_le16(len) };
481 	return hdr;
482 }
483 
484 static __le32 mm81x_hw_scan_h_pack_channel(struct ieee80211_channel *chan,
485 					   u8 pwr_idx)
486 {
487 	__le32 packed = 0;
488 	u32 freq_khz = ieee80211_channel_to_khz(chan);
489 
490 	packed |= le32_encode_bits(freq_khz, HW_SCAN_CH_LIST_FREQ_KHZ);
491 	packed |= le32_encode_bits(mm81x_ratecode_bw_mhz_to_bw_index(1),
492 				   HW_SCAN_CH_LIST_OP_BW);
493 	packed |= le32_encode_bits(mm81x_ratecode_bw_mhz_to_bw_index(1),
494 				   HW_SCAN_CH_LIST_PRIM_CH_WIDTH);
495 	packed |= le32_encode_bits(pwr_idx, HW_SCAN_CH_LIST_PWR_LIST_IDX);
496 
497 	return packed;
498 }
499 
500 static u8 *
501 mm81x_hw_scan_h_add_channel_list_tlv(u8 *buf,
502 				     struct mm81x_hw_scan_params *params)
503 {
504 	int i;
505 	struct hw_scan_tlv_channel_list *ch_list =
506 		(struct hw_scan_tlv_channel_list *)buf;
507 
508 	ch_list->hdr = mm81x_hw_scan_h_pack_tlv_hdr(
509 		HOST_CMD_HW_SCAN_TLV_TAG_CHAN_LIST,
510 		params->num_chans * sizeof(ch_list->channels[0]));
511 
512 	for (i = 0; i < params->num_chans; i++) {
513 		struct ieee80211_channel *chan = params->channels[i].channel;
514 
515 		ch_list->channels[i] = mm81x_hw_scan_h_pack_channel(
516 			chan, params->channels[i].power_idx);
517 	}
518 
519 	return (u8 *)&ch_list->channels[i];
520 }
521 
522 static u8 *
523 mm81x_hw_scan_h_add_power_list_tlv(u8 *buf, struct mm81x_hw_scan_params *params)
524 {
525 	int i;
526 	struct hw_scan_tlv_power_list *pwr_list =
527 		(struct hw_scan_tlv_power_list *)buf;
528 	size_t size = sizeof(pwr_list->tx_power_qdbm[0]) * params->n_powers;
529 
530 	pwr_list->hdr = mm81x_hw_scan_h_pack_tlv_hdr(
531 		HOST_CMD_HW_SCAN_TLV_TAG_POWER_LIST, size);
532 
533 	for (i = 0; i < params->n_powers; i++)
534 		pwr_list->tx_power_qdbm[i] = params->powers_qdbm[i];
535 
536 	return (u8 *)&pwr_list->tx_power_qdbm[i];
537 }
538 
539 static u8 *
540 mm81x_hw_scan_h_add_probe_req_tlv(u8 *buf, struct mm81x_hw_scan_params *params)
541 {
542 	struct sk_buff *skb = params->probe_req;
543 	struct hw_scan_tlv_probe_req *probe_req =
544 		(struct hw_scan_tlv_probe_req *)buf;
545 
546 	probe_req->hdr = mm81x_hw_scan_h_pack_tlv_hdr(
547 		HOST_CMD_HW_SCAN_TLV_TAG_PROBE_REQ, skb->len);
548 	memcpy(probe_req->buf, skb->data, skb->len);
549 
550 	return buf + sizeof(*probe_req) + skb->len;
551 }
552 
553 static u8 *
554 mm81x_hw_scan_h_insert_dwell_time_tlv(u8 *buf,
555 				      struct mm81x_hw_scan_params *params)
556 {
557 	struct hw_scan_tlv_dwell_on_home *dwell =
558 		(struct hw_scan_tlv_dwell_on_home *)buf;
559 
560 	dwell->hdr = mm81x_hw_scan_h_pack_tlv_hdr(
561 		HOST_CMD_HW_SCAN_TLV_TAG_DWELL_ON_HOME,
562 		sizeof(*dwell) - sizeof(dwell->hdr));
563 	dwell->home_dwell_time_ms = cpu_to_le32(params->dwell_on_home_ms);
564 
565 	return buf + sizeof(*dwell);
566 }
567 
568 static int __mm81x_hw_scan_h_init_probe_req(struct mm81x_hw_scan_params *params,
569 					    u8 *ssid, u8 ssid_len,
570 					    struct ieee80211_scan_ies *ies)
571 {
572 	u8 *pos;
573 	struct sk_buff *probe_req;
574 	struct ieee80211_tx_info *info;
575 	u16 ies_len = ies->len[NL80211_BAND_S1GHZ] + ies->common_ie_len;
576 
577 	probe_req = ieee80211_probereq_get(params->hw, params->vif->addr, ssid,
578 					   ssid_len, ies_len);
579 	if (!probe_req)
580 		return -ENOMEM;
581 
582 	pos = skb_put(probe_req, ies_len);
583 	memcpy(pos, ies->common_ies, ies->common_ie_len);
584 	pos += ies->common_ie_len;
585 	memcpy(pos, ies->ies[NL80211_BAND_S1GHZ], ies->len[NL80211_BAND_S1GHZ]);
586 
587 	info = IEEE80211_SKB_CB(probe_req);
588 	info->control.vif = params->vif;
589 	params->probe_req = probe_req;
590 
591 	return 0;
592 }
593 
594 static void mm81x_hw_scan_h_init_ssid(struct mm81x *mors,
595 				      struct cfg80211_ssid *ssids, int n_ssids,
596 				      u8 **out_ssid, u8 *out_ssid_len)
597 {
598 	*out_ssid = NULL;
599 	*out_ssid_len = 0;
600 
601 	if (n_ssids > 0) {
602 		if (n_ssids > 1) {
603 			dev_warn(
604 				mors->dev,
605 				"Multiple SSIDs found when only one supported. Using the first only.");
606 		}
607 		*out_ssid_len = ssids[0].ssid_len;
608 		*out_ssid = ssids[0].ssid;
609 	}
610 }
611 
612 static int
613 mm81x_hw_scan_h_init_probe_req(struct mm81x_hw_scan_params *params,
614 			       struct ieee80211_scan_request *scan_req)
615 {
616 	struct mm81x *mors = params->hw->priv;
617 	struct cfg80211_scan_request *req = &scan_req->req;
618 	struct ieee80211_scan_ies *ies = &scan_req->ies;
619 	u8 ssid_len = 0;
620 	u8 *ssid = NULL;
621 
622 	mm81x_hw_scan_h_init_ssid(mors, req->ssids, req->n_ssids, &ssid,
623 				  &ssid_len);
624 
625 	return __mm81x_hw_scan_h_init_probe_req(params, ssid, ssid_len, ies);
626 }
627 
628 static bool
629 mm81x_hw_scan_h_is_chan_present(const struct mm81x_hw_scan_params *params,
630 				const struct ieee80211_channel *chan)
631 {
632 	int channel;
633 
634 	for (channel = 0; channel < params->num_chans; channel++) {
635 		if (params->channels[channel].channel == chan)
636 			return true;
637 	}
638 
639 	return false;
640 }
641 
642 static int mm81x_hw_scan_h_insert_chan(struct mm81x_hw_scan_params *params,
643 				       struct ieee80211_channel *chan)
644 {
645 	if (!params->channels)
646 		return -EFAULT;
647 
648 	if (!chan)
649 		return -EFAULT;
650 
651 	if (params->num_chans >= params->allocated_chans)
652 		return -ENOMEM;
653 
654 	if (mm81x_hw_scan_h_is_chan_present(params, chan))
655 		return 0;
656 
657 	params->channels[params->num_chans].channel = chan;
658 	params->num_chans++;
659 	return 0;
660 }
661 
662 static int mm81x_hw_scan_h_init_chan_list(struct mm81x_hw_scan_params *params,
663 					  struct ieee80211_channel **chans,
664 					  u32 n_channels)
665 {
666 	int i, j;
667 	int num_pwrs_coarse = 0;
668 	int last_pwr = INT_MIN;
669 	int chans_to_allocate = 0;
670 
671 	for (i = 0; i < n_channels; i++)
672 		if (chans[i])
673 			chans_to_allocate++;
674 
675 	params->num_chans = 0;
676 	params->allocated_chans = 0;
677 	params->channels = kzalloc_objs(*params->channels, chans_to_allocate);
678 	if (!params->channels)
679 		return -ENOMEM;
680 
681 	params->allocated_chans = chans_to_allocate;
682 
683 	for (i = 0; i < n_channels; i++)
684 		if (chans[i])
685 			mm81x_hw_scan_h_insert_chan(params, chans[i]);
686 
687 	/*
688 	 * Calculate a rough estimate of number of different channel
689 	 * powers required
690 	 */
691 	for (i = 0; i < params->num_chans; i++) {
692 		if (chans[i]->max_reg_power != last_pwr) {
693 			last_pwr = chans[i]->max_reg_power;
694 			num_pwrs_coarse++;
695 		}
696 	}
697 
698 	params->powers_qdbm = kmalloc_objs(*params->powers_qdbm,
699 					   num_pwrs_coarse);
700 	if (!params->powers_qdbm)
701 		return -ENOMEM;
702 
703 	params->n_powers = 0;
704 
705 	for (i = 0; i < params->num_chans; i++) {
706 		s32 power_qdbm =
707 			MBM_TO_QDBM(DBM_TO_MBM(chans[i]->max_reg_power));
708 
709 		/* Try and find the power in the list */
710 		for (j = 0; j < params->n_powers; j++)
711 			if (params->powers_qdbm[j] == power_qdbm)
712 				break;
713 
714 		/* Reached the end of the list - add the new power option */
715 		if (j == params->n_powers) {
716 			params->powers_qdbm[j] = power_qdbm;
717 			params->n_powers++;
718 			if (params->n_powers > num_pwrs_coarse) {
719 				WARN_ON(1);
720 				return -EFAULT;
721 			}
722 		}
723 
724 		/* Give the index of the power level to the channel */
725 		params->channels[i].power_idx = j;
726 	}
727 	return 0;
728 }
729 
730 static void mm81x_hw_scan_h_clean_params(struct mm81x_hw_scan_params *params)
731 {
732 	if (params->probe_req)
733 		dev_kfree_skb_any(params->probe_req);
734 	kfree(params->channels);
735 	kfree(params->powers_qdbm);
736 
737 	params->num_chans = 0;
738 	params->allocated_chans = 0;
739 }
740 
741 size_t mm81x_hw_scan_h_get_cmd_size(struct mm81x_hw_scan_params *params)
742 {
743 	struct hw_scan_tlv_channel_list *ch_list;
744 	struct hw_scan_tlv_power_list *pwr_list;
745 	struct hw_scan_tlv_probe_req *probe_req;
746 	struct hw_scan_tlv_dwell_on_home *dwell;
747 	struct host_cmd_req_hw_scan *req;
748 	size_t cmd_size = sizeof(*req);
749 
750 	/* No TLVs if simple abort command */
751 	if (params->operation != MM81X_HW_SCAN_OP_START)
752 		return cmd_size;
753 
754 	cmd_size += struct_size(ch_list, channels, params->num_chans);
755 	cmd_size += struct_size(pwr_list, tx_power_qdbm, params->n_powers);
756 
757 	if (params->probe_req)
758 		cmd_size += struct_size(probe_req, buf, params->probe_req->len);
759 	if (params->dwell_on_home_ms)
760 		cmd_size += sizeof(*dwell);
761 
762 	return cmd_size;
763 }
764 
765 u8 *mm81x_hw_scan_h_insert_tlvs(struct mm81x_hw_scan_params *params, u8 *buf)
766 {
767 	buf = mm81x_hw_scan_h_add_channel_list_tlv(buf, params);
768 	buf = mm81x_hw_scan_h_add_power_list_tlv(buf, params);
769 
770 	if (params->dwell_on_home_ms)
771 		buf = mm81x_hw_scan_h_insert_dwell_time_tlv(buf, params);
772 	if (params->probe_req)
773 		buf = mm81x_hw_scan_h_add_probe_req_tlv(buf, params);
774 
775 	return buf;
776 }
777 
778 static u32 mm81x_hw_scan_h_get_dwell_on_home(struct mm81x *mors,
779 					     struct ieee80211_vif *vif)
780 {
781 	if (vif->type == NL80211_IFTYPE_STATION && vif->cfg.assoc)
782 		return mors->hw_scan.home_dwell_ms;
783 	return 0;
784 }
785 
786 static struct mm81x_hw_scan_params *
787 __mm81x_hw_scan_h_init_params(struct mm81x *mors)
788 {
789 	struct mm81x_hw_scan_params *params = mors->hw_scan.params;
790 
791 	if (!params) {
792 		params = kzalloc_obj(*params);
793 		if (params)
794 			mors->hw_scan.params = params;
795 	} else {
796 		mm81x_hw_scan_h_clean_params(params);
797 		memset(params, 0, sizeof(*params));
798 	}
799 
800 	return params;
801 }
802 
803 static int mm81x_hw_scan_h_init_params(struct mm81x *mors,
804 				       struct ieee80211_hw *hw,
805 				       struct ieee80211_vif *vif,
806 				       struct cfg80211_scan_request *req)
807 {
808 	struct mm81x_hw_scan_params *params = mors->hw_scan.params;
809 
810 	params = __mm81x_hw_scan_h_init_params(mors);
811 	if (!params) {
812 		mors->hw_scan.state = HW_SCAN_STATE_IDLE;
813 		return -ENOMEM;
814 	}
815 
816 	params->hw = hw;
817 	params->vif = vif;
818 	params->has_directed_ssid = (req->ssids && req->ssids[0].ssid_len > 0);
819 	params->operation = MM81X_HW_SCAN_OP_START;
820 	params->dwell_on_home_ms = mm81x_hw_scan_h_get_dwell_on_home(mors, vif);
821 
822 	if (req->duration)
823 		params->dwell_time_ms = MM81X_TU_TO_MS(req->duration);
824 	else if (req->n_ssids == 0)
825 		params->dwell_time_ms =
826 			MM81X_HWSCAN_DEFAULT_PASSIVE_DWELL_TIME_MS;
827 	else
828 		params->dwell_time_ms = MM81X_HWSCAN_DEFAULT_DWELL_TIME_MS;
829 
830 	return 0;
831 }
832 
833 static u32 mm81x_hw_scan_h_calc_timeout(struct mm81x_hw_scan_params *params)
834 {
835 	u32 ret = 0;
836 
837 	ret = params->dwell_time_ms + params->dwell_on_home_ms;
838 	if (params->probe_req)
839 		ret += MM81X_HWSCAN_PROBE_DELAY_MS;
840 
841 	ret *= params->num_chans;
842 	ret += MM81X_HWSCAN_TIMEOUT_OVERHEAD_MS;
843 
844 	return ret;
845 }
846 
847 static int mm81x_mac_ops_hw_scan(struct ieee80211_hw *hw,
848 				 struct ieee80211_vif *vif,
849 				 struct ieee80211_scan_request *hw_req)
850 {
851 	int ret = 0;
852 	struct mm81x *mors = hw->priv;
853 	struct cfg80211_scan_request *req = &hw_req->req;
854 	struct mm81x_hw_scan_params *params;
855 	struct ieee80211_channel **chans = hw_req->req.channels;
856 
857 	dev_dbg(mors->dev, "state %d", mors->hw_scan.state);
858 
859 	if (!mors->started) {
860 		dev_warn(mors->dev, "device not ready");
861 		ret = -ENODEV;
862 		goto exit;
863 	}
864 
865 	switch (mors->hw_scan.state) {
866 	case HW_SCAN_STATE_IDLE:
867 		mors->hw_scan.state = HW_SCAN_STATE_RUNNING;
868 		reinit_completion(&mors->hw_scan.scan_done);
869 		break;
870 	case HW_SCAN_STATE_RUNNING:
871 	case HW_SCAN_STATE_ABORTING:
872 		ret = -EBUSY;
873 		goto exit;
874 	}
875 
876 	ret = mm81x_hw_scan_h_init_params(mors, hw, vif, req);
877 	if (ret)
878 		goto exit;
879 
880 	params = mors->hw_scan.params;
881 
882 	ret = mm81x_hw_scan_h_init_chan_list(params, chans,
883 					     hw_req->req.n_channels);
884 	if (ret)
885 		goto exit;
886 
887 	/* Only init the probe request template if this is an active scan */
888 	if (req->n_ssids > 0) {
889 		ret = mm81x_hw_scan_h_init_probe_req(params, hw_req);
890 		if (ret) {
891 			dev_err(mors->dev, "Failed to init probe req %d", ret);
892 			goto exit;
893 		}
894 	}
895 
896 	ret = mm81x_cmd_hw_scan(mors, params, false);
897 	if (ret) {
898 		mors->hw_scan.state = HW_SCAN_STATE_IDLE;
899 		goto exit;
900 	}
901 
902 	ieee80211_queue_delayed_work(
903 		mors->hw, &mors->hw_scan.timeout,
904 		msecs_to_jiffies(mm81x_hw_scan_h_calc_timeout(params)));
905 exit:
906 	return ret;
907 }
908 
909 static void mm81x_hw_scan_abort(struct mm81x *mors)
910 {
911 	int ret;
912 	struct mm81x_hw_scan_params params = { 0 };
913 
914 	switch (mors->hw_scan.state) {
915 	case HW_SCAN_STATE_IDLE:
916 	case HW_SCAN_STATE_ABORTING:
917 		/* scan not running */
918 		return;
919 	case HW_SCAN_STATE_RUNNING:
920 		mors->hw_scan.state = HW_SCAN_STATE_ABORTING;
921 		break;
922 	}
923 
924 	params.operation = MM81X_HW_SCAN_OP_STOP;
925 
926 	ret = mm81x_cmd_hw_scan(mors, &params, false);
927 
928 	if (ret || !mors->started ||
929 	    !wait_for_completion_timeout(&mors->hw_scan.scan_done, 1 * HZ)) {
930 		/*
931 		 * We may have lost the event on the bus, the chip could be
932 		 * wedged, or the cmd failed for another reason. Nevertheless,
933 		 * we should call the done event so mac80211 knows to unblock
934 		 * itself.
935 		 */
936 		struct cfg80211_scan_info info = { .aborted = true };
937 
938 		ieee80211_scan_completed(mors->hw, &info);
939 		mors->hw_scan.state = HW_SCAN_STATE_IDLE;
940 	}
941 }
942 
943 static void mm81x_mac_ops_cancel_hw_scan(struct ieee80211_hw *hw,
944 					 struct ieee80211_vif *vif)
945 {
946 	struct mm81x *mors = hw->priv;
947 
948 	cancel_delayed_work_sync(&mors->hw_scan.timeout);
949 	mm81x_hw_scan_abort(mors);
950 }
951 
952 static void mm81x_mac_hw_scan_done_event(struct ieee80211_hw *hw)
953 {
954 	struct mm81x *mors = hw->priv;
955 	struct cfg80211_scan_info info = { 0 };
956 
957 	dev_dbg(mors->dev, "completing hw scan");
958 
959 	switch (mors->hw_scan.state) {
960 	case HW_SCAN_STATE_IDLE:
961 		/* Scan has already been stopped. Just continue */
962 		goto exit;
963 	case HW_SCAN_STATE_RUNNING:
964 	case HW_SCAN_STATE_ABORTING:
965 		info.aborted = (mors->hw_scan.state == HW_SCAN_STATE_ABORTING);
966 		mors->hw_scan.state = HW_SCAN_STATE_IDLE;
967 	}
968 
969 	ieee80211_scan_completed(mors->hw, &info);
970 exit:
971 	complete(&mors->hw_scan.scan_done);
972 	cancel_delayed_work_sync(&mors->hw_scan.timeout);
973 }
974 
975 static void mm81x_mac_hw_scan_timeout_work(struct work_struct *work)
976 {
977 	struct mm81x *mors =
978 		container_of(work, struct mm81x, hw_scan.timeout.work);
979 
980 	dev_err(mors->dev, "hw scan timed out, aborting");
981 	mm81x_hw_scan_abort(mors);
982 }
983 
984 static void mm81x_mac_hw_scan_init(struct mm81x *mors)
985 {
986 	mors->hw_scan.state = HW_SCAN_STATE_IDLE;
987 	mors->hw_scan.params = NULL;
988 	mors->hw_scan.home_dwell_ms = MM81X_HWSCAN_DEFAULT_DWELL_ON_HOME_MS;
989 
990 	init_completion(&mors->hw_scan.scan_done);
991 	INIT_DELAYED_WORK(&mors->hw_scan.timeout,
992 			  mm81x_mac_hw_scan_timeout_work);
993 }
994 
995 static void mm81x_mac_hw_scan_destroy(struct mm81x *mors)
996 {
997 	cancel_delayed_work_sync(&mors->hw_scan.timeout);
998 	if (mors->hw_scan.params)
999 		mm81x_hw_scan_h_clean_params(mors->hw_scan.params);
1000 	kfree(mors->hw_scan.params);
1001 	mors->hw_scan.params = NULL;
1002 }
1003 
1004 static void mm81x_mac_hw_scan_finish(struct mm81x *mors)
1005 {
1006 	struct cfg80211_scan_info info = {
1007 		.aborted = true,
1008 	};
1009 
1010 	if (mors->hw_scan.state == HW_SCAN_STATE_IDLE)
1011 		return;
1012 
1013 	ieee80211_scan_completed(mors->hw, &info);
1014 	complete(&mors->hw_scan.scan_done);
1015 	mors->hw_scan.state = HW_SCAN_STATE_IDLE;
1016 	cancel_delayed_work_sync(&mors->hw_scan.timeout);
1017 }
1018 
1019 int mm81x_mac_event_recv(struct mm81x *mors, struct sk_buff *skb)
1020 {
1021 	struct host_cmd_event *event = (struct host_cmd_event *)(skb->data);
1022 	u16 event_id = le16_to_cpu(event->hdr.message_id);
1023 	u16 event_iid = le16_to_cpu(event->hdr.host_id);
1024 	u16 vif_id = le16_to_cpu(event->hdr.vif_id);
1025 	struct ieee80211_vif *vif;
1026 
1027 	if (!HOST_CMD_IS_EVT(event) || event_iid != 0)
1028 		return -EINVAL;
1029 
1030 	switch (event_id) {
1031 	case HOST_CMD_ID_EVT_HW_SCAN_DONE:
1032 		dev_dbg(mors->dev,
1033 			"Event: HOST_CMD_ID_EVT_HW_SCAN_DONE Received.");
1034 		mm81x_mac_hw_scan_done_event(mors->hw);
1035 		break;
1036 	case HOST_CMD_ID_EVT_BEACON_LOSS:
1037 		dev_dbg(mors->dev,
1038 			"Event: HOST_CMD_ID_EVT_BEACON_LOSS Received");
1039 		scoped_guard(rcu) {
1040 			vif = mm81x_rcu_dereference_vif_id(mors, vif_id, true);
1041 			if (vif)
1042 				ieee80211_beacon_loss(vif);
1043 		}
1044 		break;
1045 	default:
1046 		break;
1047 	}
1048 
1049 	return 0;
1050 }
1051 
1052 static void mm81x_tx_h_apply_mcs10(struct mm81x *mors,
1053 				   struct mm81x_skb_tx_info *tx_info)
1054 {
1055 	u8 i;
1056 	u8 j;
1057 	int mcs0_first_idx = -1;
1058 	int mcs0_last_idx = -1;
1059 
1060 	/* Find out where our first and last MCS0 entries are. */
1061 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1062 		enum dot11_bandwidth bw_idx = mm81x_ratecode_bw_index_get(
1063 			tx_info->rates[i].mm81x_ratecode);
1064 
1065 		if (bw_idx == DOT11_BANDWIDTH_1MHZ) {
1066 			mcs0_last_idx = i;
1067 			if (mcs0_first_idx == -1)
1068 				mcs0_first_idx = i;
1069 		}
1070 
1071 		/*
1072 		 * If the count is 0 then we are at the end of the table.
1073 		 * Break to allow us to reuse i indicating the end of the
1074 		 * table.
1075 		 */
1076 		if (tx_info->rates[i].count == 0)
1077 			break;
1078 	}
1079 
1080 	/* If there aren't any MCS0 (at 1MHz) entries we are done. */
1081 	if (mcs0_first_idx < 0)
1082 		return;
1083 
1084 	/*
1085 	 * If we are in MCS10_MODE_AUTO add MCS10 counts to the table if they
1086 	 * will fit. There should be three cases:
1087 	 *
1088 	 * - There is one MSC0 entry and the table is full -> do nothing
1089 	 * - There is one MSC0 entry and the table has space -> adjust MSC0
1090 	 *   down and add MCS 10
1091 	 * - There are multiple MCS0 entries -> replace entries after the first
1092 	 *   with MCS 10
1093 	 */
1094 	/* Case 3 - replace additional entries. */
1095 	if (mcs0_last_idx > mcs0_first_idx) {
1096 		for (j = mcs0_first_idx + 1; j < i; j++) {
1097 			enum dot11_bandwidth bw_idx =
1098 				mm81x_ratecode_bw_index_get(
1099 					tx_info->rates[j].mm81x_ratecode);
1100 			u8 mcs_index = mm81x_ratecode_mcs_index_get(
1101 				tx_info->rates[j].mm81x_ratecode);
1102 			if (mcs_index == 0 && bw_idx == DOT11_BANDWIDTH_1MHZ) {
1103 				mm81x_ratecode_mcs_index_set(
1104 					&tx_info->rates[j].mm81x_ratecode, 10);
1105 			}
1106 		}
1107 		/* Case 2 - add additional MCS10 entry. */
1108 	} else if (mcs0_last_idx == mcs0_first_idx &&
1109 		   i < (IEEE80211_TX_MAX_RATES)) {
1110 		int pre_mcs10_mcs0_count =
1111 			min_t(u8, tx_info->rates[mcs0_last_idx].count,
1112 			      MCS0_BEFORE_MCS10_COUNT);
1113 		int mcs10_count = tx_info->rates[mcs0_last_idx].count -
1114 				  pre_mcs10_mcs0_count;
1115 
1116 		/*
1117 		 * If there were less retries than our desired minimum MCS0 we
1118 		 * don't add MCS10 retries.
1119 		 */
1120 		if (mcs10_count > 0) {
1121 			/* Use the same flags for MCS10 as MCS0. */
1122 			tx_info->rates[i].mm81x_ratecode =
1123 				tx_info->rates[mcs0_last_idx].mm81x_ratecode;
1124 			mm81x_ratecode_mcs_index_set(
1125 				&tx_info->rates[i].mm81x_ratecode, 10);
1126 			tx_info->rates[mcs0_last_idx].count =
1127 				pre_mcs10_mcs0_count;
1128 			tx_info->rates[i].count = mcs10_count;
1129 		}
1130 	}
1131 }
1132 
1133 void mm81x_tx_h_check_aggr(struct ieee80211_sta *pubsta, struct sk_buff *skb)
1134 {
1135 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1136 	struct mm81x_sta *mors_sta = (struct mm81x_sta *)pubsta->drv_priv;
1137 	u8 tid = ieee80211_get_tid(hdr);
1138 
1139 	/* we are already aggregating */
1140 	if (mors_sta->tid_tx[tid] || mors_sta->tid_start_tx[tid])
1141 		return;
1142 
1143 	if (mors_sta->state < IEEE80211_STA_AUTHORIZED)
1144 		return;
1145 
1146 	if (skb_get_queue_mapping(skb) == IEEE80211_AC_VO)
1147 		return;
1148 
1149 	if (unlikely(!ieee80211_is_data_qos(hdr->frame_control)))
1150 		return;
1151 
1152 	if (unlikely(skb->protocol == cpu_to_be16(ETH_P_PAE)))
1153 		return;
1154 
1155 	mors_sta->tid_start_tx[tid] = true;
1156 	ieee80211_start_tx_ba_session(pubsta, tid, 0);
1157 }
1158 
1159 int mm81x_tx_h_get_attempts(struct mm81x *mors,
1160 			    struct mm81x_skb_tx_status *tx_sts)
1161 {
1162 	int attempts = 0;
1163 	int i;
1164 	int count = min_t(int, MM81X_SKB_MAX_RATES, IEEE80211_TX_MAX_RATES);
1165 
1166 	for (i = 0; i < count; i++) {
1167 		if (tx_sts->rates[i].count > 0)
1168 			attempts += tx_sts->rates[i].count;
1169 		else
1170 			break;
1171 	}
1172 
1173 	return attempts;
1174 }
1175 
1176 static void mm81x_tx_h_fill_info(struct mm81x *mors,
1177 				 struct mm81x_skb_tx_info *tx_info,
1178 				 struct sk_buff *skb, struct ieee80211_vif *vif,
1179 				 int tx_bw_mhz, struct ieee80211_sta *sta)
1180 {
1181 	int i;
1182 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1183 	struct mm81x_vif *mors_vif = ieee80211_vif_to_mors_vif(vif);
1184 	struct mm81x_sta *mors_sta = NULL;
1185 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1186 	int op_bw_mhz = cfg80211_chandef_get_width(&mors->chandef);
1187 	u8 tid = skb->priority & IEEE80211_QOS_CTL_TAG1D_MASK;
1188 	bool rts_allowed = op_bw_mhz < 8;
1189 
1190 	if (sta)
1191 		mors_sta = (struct mm81x_sta *)sta->drv_priv;
1192 
1193 	rts_allowed &= mm81x_tx_h_pkt_over_rts_threshold(mors, info, skb);
1194 
1195 	mm81x_rc_sta_fill_tx_rates(mors, tx_info, skb, sta, tx_bw_mhz,
1196 				   rts_allowed);
1197 
1198 	for (i = 0; i < IEEE80211_TX_MAX_RATES; i++) {
1199 		if (rts_allowed)
1200 			mm81x_ratecode_enable_rts(
1201 				&tx_info->rates[i].mm81x_ratecode);
1202 
1203 		if (info->control.rates[i].flags & IEEE80211_TX_RC_SHORT_GI)
1204 			mm81x_ratecode_enable_sgi(
1205 				&tx_info->rates[i].mm81x_ratecode);
1206 	}
1207 
1208 	/* Apply change of MCS0 to MCS10 if required. */
1209 	mm81x_tx_h_apply_mcs10(mors, tx_info);
1210 
1211 	tx_info->flags |=
1212 		cpu_to_le32(MM81X_TX_CONF_FLAGS_VIF_ID_SET(mors_vif->id));
1213 
1214 	if (info->flags & IEEE80211_TX_CTL_AMPDU)
1215 		tx_info->flags |= cpu_to_le32(MM81X_TX_CONF_FLAGS_CTL_AMPDU);
1216 
1217 	if (info->flags & IEEE80211_TX_CTL_SEND_AFTER_DTIM)
1218 		tx_info->flags |=
1219 			cpu_to_le32(MM81X_TX_CONF_FLAGS_SEND_AFTER_DTIM);
1220 
1221 	if (info->flags & IEEE80211_TX_CTL_NO_PS_BUFFER) {
1222 		tx_info->flags |= cpu_to_le32(MM81X_TX_CONF_NO_PS_BUFFER);
1223 
1224 		if (info->flags & IEEE80211_TX_STATUS_EOSP)
1225 			tx_info->flags |= cpu_to_le32(
1226 				MM81X_TX_CONF_FLAGS_IMMEDIATE_REPORT);
1227 	} else if (ieee80211_is_mgmt(hdr->frame_control) &&
1228 		   !ieee80211_is_bufferable_mmpdu(skb)) {
1229 		tx_info->flags |= cpu_to_le32(MM81X_TX_CONF_NO_PS_BUFFER);
1230 	}
1231 
1232 	if (info->control.hw_key) {
1233 		tx_info->flags |= cpu_to_le32(MM81X_TX_CONF_FLAGS_HW_ENCRYPT);
1234 		tx_info->flags |= cpu_to_le32(MM81X_TX_CONF_FLAGS_KEY_IDX_SET(
1235 			info->control.hw_key->hw_key_idx));
1236 	}
1237 
1238 	tx_info->tid = tid;
1239 	if (mors_sta) {
1240 		tx_info->tid_params = mors_sta->tid_params[tid];
1241 
1242 		if (info->flags & IEEE80211_TX_CTL_CLEAR_PS_FILT) {
1243 			if (mors_sta->tx_ps_filter_en)
1244 				dev_dbg(mors->dev,
1245 					"TX ps filter cleared sta[%pM]",
1246 					mors_sta->addr);
1247 			mors_sta->tx_ps_filter_en = false;
1248 		}
1249 	}
1250 }
1251 
1252 static int mm81x_tx_h_get_bw(struct mm81x *mors, struct ieee80211_sta *sta,
1253 			     struct sk_buff *skb, bool is_mgmt)
1254 {
1255 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1256 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1257 	struct mm81x_sta *mors_sta = NULL;
1258 	int tx_bw_mhz;
1259 
1260 	if (ieee80211_is_probe_resp(hdr->frame_control))
1261 		return 1;
1262 
1263 	if (is_mgmt || info->control.flags & IEEE80211_TX_CTRL_PORT_CTRL_PROTO)
1264 		return cfg80211_chandef_s1g_pri_width(&mors->chandef);
1265 
1266 	/*
1267 	 * In AP mode group addressed frames go out at the primary width so
1268 	 * that every associated STA can receive them, including any that
1269 	 * cannot support the operating width.
1270 	 */
1271 	if (info->control.vif->type == NL80211_IFTYPE_AP &&
1272 	    is_multicast_ether_addr(ieee80211_get_DA(hdr)))
1273 		return cfg80211_chandef_s1g_pri_width(&mors->chandef);
1274 
1275 	if (sta)
1276 		mors_sta = (struct mm81x_sta *)sta->drv_priv;
1277 
1278 	tx_bw_mhz = min(mm81x_tx_h_get_max_tx_bw(mors),
1279 			cfg80211_chandef_get_width(&mors->chandef));
1280 	if (mors_sta && mors_sta->max_rx_bw_mhz)
1281 		tx_bw_mhz = min(tx_bw_mhz, mors_sta->max_rx_bw_mhz);
1282 
1283 	return tx_bw_mhz;
1284 }
1285 
1286 static void mm81x_mac_ops_tx(struct ieee80211_hw *hw,
1287 			     struct ieee80211_tx_control *control,
1288 			     struct sk_buff *skb)
1289 {
1290 	struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
1291 	struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
1292 	bool is_mgmt = ieee80211_is_mgmt(hdr->frame_control);
1293 	struct ieee80211_sta *sta = control->sta;
1294 	struct mm81x_skb_tx_info tx_info = { 0 };
1295 	struct mm81x *mors = hw->priv;
1296 	struct mm81x_skbq *mq;
1297 
1298 	mm81x_tx_h_fill_info(mors, &tx_info, skb, info->control.vif,
1299 			     mm81x_tx_h_get_bw(mors, sta, skb, is_mgmt), sta);
1300 
1301 	if (mm81x_tx_h_ps_filtered_for_sta(mors, skb, sta))
1302 		return;
1303 
1304 	if (is_mgmt)
1305 		mq = mm81x_hif_get_tx_mgmt_queue(mors);
1306 	else
1307 		mq = mm81x_hif_get_tx_data_queue(mors,
1308 						 dot11_tid_to_ac(tx_info.tid));
1309 
1310 	mm81x_skbq_skb_tx(mq, &skb, &tx_info,
1311 			  (is_mgmt) ? MM81X_SKB_CHAN_MGMT :
1312 				      MM81X_SKB_CHAN_DATA);
1313 }
1314 
1315 static void mm81x_mac_send_buffered_bc(struct mm81x *mors,
1316 				       struct ieee80211_vif *vif)
1317 {
1318 	int count = MM81X_MAX_MC_FRAMES_AFTER_DTIM;
1319 	struct ieee80211_tx_control control = { 0 };
1320 	struct sk_buff *bc_frame;
1321 
1322 	while (count-- &&
1323 	       (bc_frame = ieee80211_get_buffered_bc(mors->hw, vif))) {
1324 		IEEE80211_SKB_CB(bc_frame)->control.vif = vif;
1325 		mm81x_mac_ops_tx(mors->hw, &control, bc_frame);
1326 	}
1327 }
1328 
1329 static void mm81x_mac_beacon_work(struct work_struct *work)
1330 {
1331 	struct mm81x_vif *mors_vif =
1332 		from_work(mors_vif, work, u.ap.beacon_work);
1333 	struct mm81x *mors = mm81x_vif_to_mors(mors_vif);
1334 	struct mm81x_skbq *mq;
1335 	struct sk_buff *beacon;
1336 	struct ieee80211_vif *vif = mm81x_vif_to_ieee80211_vif(mors_vif);
1337 	struct mm81x_skb_tx_info tx_info = { 0 };
1338 	int num_bcn_vifs = atomic_read(&mors->num_bcn_vifs);
1339 
1340 	mq = mm81x_hif_get_tx_beacon_queue(mors);
1341 	if (!mq) {
1342 		dev_err(mors->dev, "no matching beacon Q found");
1343 		return;
1344 	}
1345 
1346 	if (mm81x_skbq_count(mq) >= num_bcn_vifs) {
1347 		dev_err(mors->dev,
1348 			"previous beacon not consumed, dropping req [id:%d]",
1349 			mors_vif->id);
1350 		return;
1351 	}
1352 
1353 	beacon = ieee80211_beacon_get(mors->hw, vif, false);
1354 	if (!beacon)
1355 		return;
1356 
1357 	mm81x_beacon_h_fill_tx_info(mors, &tx_info, mors_vif,
1358 				    cfg80211_chandef_s1g_pri_width(&mors->chandef));
1359 	mm81x_skbq_skb_tx(mq, &beacon, &tx_info, MM81X_SKB_CHAN_BEACON);
1360 
1361 	if (!test_bit(MM81X_STATE_DATA_QS_STOPPED, &mors->state_flags))
1362 		mm81x_mac_send_buffered_bc(mors, vif);
1363 }
1364 
1365 static void mm81x_mac_beacon_start(struct mm81x_vif *mors_vif)
1366 {
1367 	struct mm81x *mors = mm81x_vif_to_mors(mors_vif);
1368 
1369 	mm81x_mac_beacon_irq_enable(mors_vif, true);
1370 	atomic_inc(&mors->num_bcn_vifs);
1371 }
1372 
1373 static void mm81x_mac_ops_stop(struct ieee80211_hw *hw, bool suspend)
1374 {
1375 	struct mm81x *mors = hw->priv;
1376 
1377 	mors->started = false;
1378 }
1379 
1380 static void mm81x_mac_beacon_finish(struct mm81x_vif *mors_vif)
1381 {
1382 	struct mm81x *mors = mm81x_vif_to_mors(mors_vif);
1383 
1384 	mm81x_mac_beacon_irq_enable(mors_vif, false);
1385 	cancel_work_sync(&mors_vif->u.ap.beacon_work);
1386 	/*
1387 	 * Side effect of the restarting required when
1388 	 * reacting to regdom changes...
1389 	 */
1390 	atomic_add_unless(&mors->num_bcn_vifs, -1, 0);
1391 }
1392 
1393 static void mm81x_mac_ops_remove_interface(struct ieee80211_hw *hw,
1394 					   struct ieee80211_vif *vif)
1395 {
1396 	int ret;
1397 	struct mm81x *mors = hw->priv;
1398 	struct mm81x_vif *mors_vif = (struct mm81x_vif *)vif->drv_priv;
1399 
1400 	ret = mm81x_cmd_rm_if(mors, mors_vif->id);
1401 	if (ret)
1402 		dev_err(mors->dev, "mm81x_cmd_rm_if failed %d", ret);
1403 
1404 	RCU_INIT_POINTER(mors->vifs[mors_vif->id], NULL);
1405 
1406 	if (vif->type == NL80211_IFTYPE_MONITOR)
1407 		__clear_bit(IEEE80211_HW_RX_INCLUDES_FCS, hw->flags);
1408 }
1409 
1410 static s32 mm81x_mac_get_max_txpower(struct mm81x *mors)
1411 {
1412 	int ret;
1413 	s32 power_mbm;
1414 
1415 	/* Retrieve maximum TX power the chip can transmit */
1416 	ret = mm81x_cmd_get_max_txpower(mors, &power_mbm);
1417 	if (ret) {
1418 		dev_err(mors->dev, "using default tx max power %d mBm",
1419 			MAX_TX_POWER_MBM);
1420 		return MAX_TX_POWER_MBM;
1421 	}
1422 
1423 	dev_dbg(mors->dev, "Max tx power detected %d mBm", power_mbm);
1424 	return power_mbm;
1425 }
1426 
1427 static s32 mm81x_mac_set_txpower(struct mm81x *mors, s32 power_mbm)
1428 {
1429 	int ret;
1430 	s32 out_power_mbm;
1431 
1432 	if (mors->tx_max_power_mbm == INT_MAX)
1433 		mors->tx_max_power_mbm = mm81x_mac_get_max_txpower(mors);
1434 
1435 	power_mbm = min(power_mbm, mors->tx_max_power_mbm);
1436 	if (power_mbm == mors->tx_power_mbm)
1437 		return mors->tx_power_mbm;
1438 
1439 	ret = mm81x_cmd_set_txpower(mors, &out_power_mbm, power_mbm);
1440 	if (ret) {
1441 		dev_err(mors->dev, "failed, power %d mBm ret %d", power_mbm,
1442 			ret);
1443 		return mors->tx_power_mbm;
1444 	}
1445 
1446 	if (out_power_mbm != mors->tx_power_mbm) {
1447 		dev_dbg(mors->dev, "%d -> %d mBm", mors->tx_power_mbm,
1448 			out_power_mbm);
1449 		mors->tx_power_mbm = out_power_mbm;
1450 	}
1451 
1452 	return mors->tx_power_mbm;
1453 }
1454 
1455 static int mm81x_mac_set_channel(struct mm81x *mors, u32 op_chan_freq_hz,
1456 				 u8 pri_1mhz_chan_idx, u8 op_bw_mhz,
1457 				 u8 pri_bw_mhz)
1458 {
1459 	int ret;
1460 
1461 	ret = mm81x_cmd_set_channel(mors, op_chan_freq_hz, pri_1mhz_chan_idx,
1462 				    op_bw_mhz, pri_bw_mhz, &mors->tx_power_mbm);
1463 	if (ret) {
1464 		dev_err(mors->dev, "mm81x_cmd_set_channel() failed, ret %d",
1465 			ret);
1466 		return ret;
1467 	}
1468 
1469 	mm81x_mac_set_txpower(mors, mors->tx_power_mbm);
1470 	return 0;
1471 }
1472 
1473 static u8 mm81x_mac_pri_chan_to_index(const struct cfg80211_chan_def *chandef)
1474 {
1475 	u32 bw_mhz = cfg80211_chandef_get_width(chandef);
1476 	u32 op_center_khz = ieee80211_chandef_to_khz(chandef);
1477 	u32 first_1mhz_center_khz = op_center_khz - (bw_mhz * 500) + 500;
1478 	u32 pri_1mhz_khz = ieee80211_channel_to_khz(chandef->chan);
1479 
1480 	return (pri_1mhz_khz - first_1mhz_center_khz) / 1000;
1481 }
1482 
1483 static int mm81x_mac_ops_change_channel(struct ieee80211_hw *hw,
1484 					struct cfg80211_chan_def *chandef)
1485 {
1486 	int ret;
1487 	struct mm81x *mors = hw->priv;
1488 	u64 freq_hz = KHZ_TO_HZ(ieee80211_chandef_to_khz(chandef));
1489 	u8 op_bw_mhz = cfg80211_chandef_get_width(chandef);
1490 	u8 pri_1mhz_idx = mm81x_mac_pri_chan_to_index(chandef);
1491 	int pri_chan_width_mhz = cfg80211_chandef_s1g_pri_width(chandef);
1492 
1493 	dev_dbg(mors->dev, "ch: freq=%llu Hz bw=%u pri_idx=%d pri_bw=%d",
1494 		freq_hz, op_bw_mhz, pri_1mhz_idx, pri_chan_width_mhz);
1495 
1496 	ret = mm81x_mac_set_channel(mors, freq_hz, (u8)pri_1mhz_idx, op_bw_mhz,
1497 				    pri_chan_width_mhz);
1498 	if (ret)
1499 		return ret;
1500 
1501 	memcpy(&mors->chandef, chandef, sizeof(mors->chandef));
1502 	return 0;
1503 }
1504 
1505 static int mm81x_mac_ops_config(struct ieee80211_hw *hw, int radio_idx,
1506 				u32 changed)
1507 {
1508 	int ret;
1509 	struct mm81x *mors = hw->priv;
1510 	struct ieee80211_conf *conf = &hw->conf;
1511 	struct ieee80211_channel *channel = conf->chandef.chan;
1512 
1513 	if (!mors->started)
1514 		return 0;
1515 
1516 	if (changed & IEEE80211_CONF_CHANGE_CHANNEL) {
1517 		ret = mm81x_mac_ops_change_channel(hw, &conf->chandef);
1518 		if (ret < 0)
1519 			return ret;
1520 	}
1521 
1522 	if (changed & IEEE80211_CONF_CHANGE_MONITOR)
1523 		mors->monitor_en = conf->flags & IEEE80211_CONF_MONITOR;
1524 
1525 	if ((changed & IEEE80211_CONF_CHANGE_POWER) &&
1526 	    !(changed & IEEE80211_CONF_CHANGE_CHANNEL) &&
1527 	    !(conf->flags & IEEE80211_CONF_MONITOR)) {
1528 		s32 power_mbm = DBM_TO_MBM(conf->power_level);
1529 
1530 		power_mbm = min(DBM_TO_MBM(channel->max_reg_power), power_mbm);
1531 		power_mbm = mm81x_mac_set_txpower(mors, power_mbm);
1532 		conf->power_level = MBM_TO_DBM(power_mbm);
1533 	}
1534 
1535 	return 0;
1536 }
1537 
1538 static int mm81x_mac_ops_get_txpower(struct ieee80211_hw *hw,
1539 				     struct ieee80211_vif *vif,
1540 				     unsigned int link_id, int *dbm)
1541 {
1542 	struct mm81x *mors = hw->priv;
1543 	struct ieee80211_chanctx_conf *chanctx_conf;
1544 	struct cfg80211_chan_def *chandef = &vif->bss_conf.chanreq.oper;
1545 
1546 	scoped_guard(rcu) {
1547 		chanctx_conf = rcu_access_pointer(vif->bss_conf.chanctx_conf);
1548 		if (!chanctx_conf ||
1549 		    !cfg80211_chandef_identical(chandef, &chanctx_conf->def))
1550 			return -ENODATA;
1551 	}
1552 
1553 	*dbm = MBM_TO_DBM(mors->tx_power_mbm);
1554 	return 0;
1555 }
1556 
1557 static void mm81x_mac_config_ps(struct mm81x *mors, struct ieee80211_vif *vif)
1558 {
1559 	bool en_ps = vif->cfg.ps;
1560 
1561 	if (vif->type == NL80211_IFTYPE_AP || !mors->ps.enable)
1562 		return;
1563 
1564 	if (mors->config_ps == en_ps)
1565 		return;
1566 
1567 	dev_dbg(mors->dev, "change powersave mode: %d (current %d)", en_ps,
1568 		mors->config_ps);
1569 
1570 	mors->config_ps = en_ps;
1571 
1572 	if (en_ps) {
1573 		mm81x_cmd_set_ps(mors, true);
1574 		mm81x_ps_enable(mors);
1575 	} else {
1576 		mm81x_ps_disable(mors);
1577 		mm81x_cmd_set_ps(mors, false);
1578 	}
1579 }
1580 
1581 static void mm81x_mac_ops_bss_info_changed(struct ieee80211_hw *hw,
1582 					   struct ieee80211_vif *vif,
1583 					   struct ieee80211_bss_conf *info,
1584 					   u64 changed)
1585 {
1586 	int ret;
1587 	struct mm81x *mors = hw->priv;
1588 	struct mm81x_vif *mors_vif = (struct mm81x_vif *)vif->drv_priv;
1589 
1590 	if (changed & BSS_CHANGED_PS)
1591 		mm81x_mac_config_ps(mors, vif);
1592 
1593 	if (changed & BSS_CHANGED_BEACON_ENABLED) {
1594 		mm81x_cmd_config_beacon_timer(mors, mors_vif,
1595 					      info->enable_beacon);
1596 
1597 		if (info->enable_beacon)
1598 			mm81x_mac_beacon_start(mors_vif);
1599 		else
1600 			mm81x_mac_beacon_finish(mors_vif);
1601 	}
1602 
1603 	if (changed & BSS_CHANGED_BEACON_INT || changed & BSS_CHANGED_SSID) {
1604 		ret = mm81x_cmd_cfg_bss(mors, mors_vif->id, info->beacon_int,
1605 					info->dtim_period,
1606 					mm81x_vif_generate_cssid(vif));
1607 		if (ret)
1608 			dev_err(mors->dev, "mm81x_cmd_cfg_bss failed %d", ret);
1609 	}
1610 }
1611 
1612 static u64 mm81x_mac_ops_prepare_multicast(struct ieee80211_hw *hw,
1613 					   struct netdev_hw_addr_list *mc_list)
1614 {
1615 	struct mm81x *mors = hw->priv;
1616 	struct mm81x_mcast_filter *filter;
1617 	struct netdev_hw_addr *addr;
1618 	u16 addr_count = netdev_hw_addr_list_count(mc_list);
1619 	u16 len = sizeof(*filter) + addr_count * sizeof(filter->addr_list[0]);
1620 
1621 	filter = kzalloc(len, GFP_ATOMIC);
1622 	if (!filter)
1623 		return 0;
1624 
1625 	if (addr_count > MCAST_FILTER_COUNT_MAX) {
1626 		dev_warn(
1627 			mors->dev,
1628 			"Multicast filtering disabled - too many groups (%d) > %u",
1629 			addr_count, (u16)MCAST_FILTER_COUNT_MAX);
1630 		filter->count = 0;
1631 	} else {
1632 		netdev_hw_addr_list_for_each(addr, mc_list) {
1633 			dev_dbg(mors->dev, "mcast whitelist (%d): %pM",
1634 				filter->count, addr->addr);
1635 			filter->addr_list[filter->count++] =
1636 				mac2le32(addr->addr);
1637 		}
1638 	}
1639 
1640 	return (u64)(unsigned long)filter;
1641 }
1642 
1643 static void mm81x_mc_iface_iterator(void *data, u8 *mac,
1644 				    struct ieee80211_vif *vif)
1645 {
1646 	struct mm81x_vif *mors_vif = ieee80211_vif_to_mors_vif(vif);
1647 	struct mm81x *mors = data;
1648 	int ret;
1649 
1650 	if (vif->type == NL80211_IFTYPE_MONITOR)
1651 		return;
1652 
1653 	ret = mm81x_cmd_cfg_multicast_filter(mors, mors_vif);
1654 	if (ret)
1655 		dev_err(mors->dev, "Multicast filtering failed - rc=%d", ret);
1656 }
1657 
1658 #define MM81X_MONITOR_FILTERS		\
1659 	(FIF_ALLMULTI |			\
1660 	FIF_BCN_PRBRESP_PROMISC |	\
1661 	FIF_CONTROL |			\
1662 	FIF_FCSFAIL |			\
1663 	FIF_OTHER_BSS |			\
1664 	FIF_PSPOLL |			\
1665 	FIF_PROBE_REQ |			\
1666 	FIF_MCAST_ACTION)
1667 
1668 static void mm81x_mac_ops_configure_filter(struct ieee80211_hw *hw,
1669 					   unsigned int changed_flags,
1670 					   unsigned int *total_flags,
1671 					   u64 multicast)
1672 {
1673 	struct mm81x_mcast_filter *cmd = (void *)(unsigned long)multicast;
1674 	struct mm81x *mors = hw->priv;
1675 
1676 	if (!cmd)
1677 		goto out;
1678 
1679 	kfree(mors->mcast_filter);
1680 	mors->mcast_filter = cmd;
1681 
1682 	ieee80211_iterate_active_interfaces(mors->hw,
1683 					    IEEE80211_IFACE_ITER_NORMAL,
1684 					    mm81x_mc_iface_iterator, mors);
1685 
1686 out:
1687 	if (mors->monitor_en)
1688 		*total_flags &= MM81X_MONITOR_FILTERS;
1689 	else
1690 		*total_flags = 0;
1691 }
1692 
1693 static int mm81x_mac_ops_conf_tx(struct ieee80211_hw *hw,
1694 				 struct ieee80211_vif *vif,
1695 				 unsigned int link_id, u16 ac,
1696 				 const struct ieee80211_tx_queue_params *params)
1697 {
1698 	int ret;
1699 	struct mm81x *mors = hw->priv;
1700 	struct mm81x_queue_params mqp;
1701 
1702 	mqp.aci = map_mac80211q_2_mm81x_aci(ac);
1703 	mqp.aifs = params->aifs;
1704 	mqp.cw_max = params->cw_max;
1705 	mqp.cw_min = params->cw_min;
1706 	mqp.uapsd = params->uapsd;
1707 	mqp.txop = params->txop << 5;
1708 
1709 	dev_dbg(mors->dev, "queue:%d txop:%d cw_min:%d cw_max:%d aifs:%d",
1710 		mqp.aci, mqp.txop, mqp.cw_min, mqp.cw_max, mqp.aifs);
1711 
1712 	ret = mm81x_cmd_cfg_qos(mors, &mqp);
1713 	if (ret)
1714 		dev_dbg(mors->dev, "mm81x_cmd_cfg_qos failed %d", ret);
1715 	return ret;
1716 }
1717 
1718 static int mm81x_mac_ops_sta_state(struct ieee80211_hw *hw,
1719 				   struct ieee80211_vif *vif,
1720 				   struct ieee80211_sta *sta,
1721 				   enum ieee80211_sta_state old_state,
1722 				   enum ieee80211_sta_state new_state)
1723 {
1724 	u16 aid;
1725 	int ret;
1726 	struct mm81x *mors = hw->priv;
1727 	struct mm81x_vif *mors_vif = (struct mm81x_vif *)vif->drv_priv;
1728 	struct mm81x_sta *mors_sta = (struct mm81x_sta *)sta->drv_priv;
1729 
1730 	/* Ignore both NOTEXIST to NONE and NONE to NOTEXIST */
1731 	if ((old_state == IEEE80211_STA_NOTEXIST &&
1732 	     new_state == IEEE80211_STA_NONE) ||
1733 	    (old_state == IEEE80211_STA_NONE &&
1734 	     new_state == IEEE80211_STA_NOTEXIST))
1735 		return 0;
1736 
1737 	if (vif->type == NL80211_IFTYPE_STATION)
1738 		aid = vif->cfg.aid;
1739 	else
1740 		aid = sta->aid;
1741 
1742 	ret = mm81x_cmd_sta_state(mors, mors_vif, aid, sta, new_state);
1743 	if (ret < 0)
1744 		goto exit;
1745 
1746 	ether_addr_copy(mors_sta->addr, sta->addr);
1747 	mors_sta->state = new_state;
1748 
1749 	if (new_state > old_state && new_state == IEEE80211_STA_ASSOC) {
1750 		if (vif->type == NL80211_IFTYPE_AP)
1751 			mors_vif->u.ap.num_stas++;
1752 		else if (vif->type == NL80211_IFTYPE_STATION)
1753 			mors_vif->u.sta.is_assoc = true;
1754 
1755 		mors_sta->max_rx_bw_mhz =
1756 			S1G_SUPP_CH_WIDTH_MAX(sta->deflink.s1g_cap.cap);
1757 	}
1758 
1759 	if (new_state < old_state && new_state == IEEE80211_STA_NONE) {
1760 		if (vif->type == NL80211_IFTYPE_AP)
1761 			mors_vif->u.ap.num_stas--;
1762 		else if (vif->type == NL80211_IFTYPE_STATION)
1763 			mors_vif->u.sta.is_assoc = false;
1764 	}
1765 
1766 exit:
1767 	/*
1768 	 * Always update our mmrc sta state even on failure to ensure
1769 	 * we don't hold a dangling sta on error
1770 	 */
1771 	mm81x_rc_sta_state_check(mors, vif, sta, old_state, new_state);
1772 	return new_state < old_state ? 0 : ret;
1773 }
1774 
1775 static int mm81x_mac_ops_ampdu_action(struct ieee80211_hw *hw,
1776 				      struct ieee80211_vif *vif,
1777 				      struct ieee80211_ampdu_params *params)
1778 {
1779 	u16 tid = params->tid;
1780 	struct mm81x *mors = hw->priv;
1781 	struct ieee80211_sta *sta = params->sta;
1782 	struct mm81x_sta *mors_sta = (struct mm81x_sta *)sta->drv_priv;
1783 	u16 buf_size =
1784 		min_t(u16, params->buf_size, DOT11AH_BA_MAX_MPDU_PER_AMPDU);
1785 
1786 	switch (params->action) {
1787 	case IEEE80211_AMPDU_TX_START:
1788 		dev_dbg(mors->dev, "%pM.%d A-MPDU TX start", mors_sta->addr,
1789 			tid);
1790 		ieee80211_start_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1791 		break;
1792 	case IEEE80211_AMPDU_TX_STOP_CONT:
1793 	case IEEE80211_AMPDU_TX_STOP_FLUSH:
1794 	case IEEE80211_AMPDU_TX_STOP_FLUSH_CONT:
1795 		dev_dbg(mors->dev, "%pM.%d A-MPDU TX flush", mors_sta->addr,
1796 			tid);
1797 		mors_sta->tid_start_tx[tid] = false;
1798 		mors_sta->tid_tx[tid] = false;
1799 		mors_sta->tid_params[tid] = 0;
1800 		ieee80211_stop_tx_ba_cb_irqsafe(vif, sta->addr, tid);
1801 		break;
1802 	case IEEE80211_AMPDU_TX_OPERATIONAL:
1803 		dev_dbg(mors->dev, "%pM.%d A-MPDU TX oper", mors_sta->addr,
1804 			tid);
1805 		mors_sta->tid_tx[tid] = true;
1806 		if (!buf_size) {
1807 			dev_err(mors->dev, "%pM.%d A-MPDU Invalid buf size",
1808 				mors_sta->addr, tid);
1809 			break;
1810 		}
1811 		mors_sta->tid_params[tid] =
1812 			u8_encode_bits(buf_size - 1,
1813 				       TX_INFO_TID_PARAMS_MAX_REORDER_BUF) |
1814 			u8_encode_bits(1, TX_INFO_TID_PARAMS_AMPDU_ENABLED) |
1815 			u8_encode_bits(params->amsdu,
1816 				       TX_INFO_TID_PARAMS_AMSDU_SUPPORTED);
1817 		break;
1818 	default:
1819 		break;
1820 	}
1821 
1822 	return 0;
1823 }
1824 
1825 static int mm81x_mac_ops_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
1826 				 struct ieee80211_vif *vif,
1827 				 struct ieee80211_sta *sta,
1828 				 struct ieee80211_key_conf *key)
1829 {
1830 	u16 aid;
1831 	int ret = -EOPNOTSUPP;
1832 	struct mm81x *mors = hw->priv;
1833 	struct mm81x_vif *mors_vif = (struct mm81x_vif *)vif->drv_priv;
1834 	enum host_cmd_key_cipher cipher;
1835 	enum host_cmd_aes_key_len length;
1836 
1837 	if (vif->type == NL80211_IFTYPE_STATION) {
1838 		aid = vif->cfg.aid;
1839 	} else if (sta) {
1840 		aid = sta->aid;
1841 	} else {
1842 		/* Is a group key - AID is unused */
1843 		WARN_ON(key->flags & IEEE80211_KEY_FLAG_PAIRWISE);
1844 		aid = 0;
1845 	}
1846 
1847 	switch (cmd) {
1848 	case SET_KEY: {
1849 		switch (key->cipher) {
1850 		case WLAN_CIPHER_SUITE_CCMP:
1851 		case WLAN_CIPHER_SUITE_CCMP_256:
1852 			cipher = HOST_CMD_KEY_CIPHER_AES_CCM;
1853 			break;
1854 		case WLAN_CIPHER_SUITE_GCMP:
1855 		case WLAN_CIPHER_SUITE_GCMP_256:
1856 			cipher = HOST_CMD_KEY_CIPHER_AES_GCM;
1857 			break;
1858 		default:
1859 			/* Cipher suite currently not supported */
1860 			ret = -EOPNOTSUPP;
1861 			goto exit;
1862 		}
1863 
1864 		switch (key->keylen) {
1865 		case 16:
1866 			length = HOST_CMD_AES_KEY_LEN_LENGTH_128;
1867 			break;
1868 		case 32:
1869 			length = HOST_CMD_AES_KEY_LEN_LENGTH_256;
1870 			break;
1871 		default:
1872 			/* Key length not supported */
1873 			ret = -EOPNOTSUPP;
1874 			goto exit;
1875 		}
1876 
1877 		ret = mm81x_cmd_install_key(mors, mors_vif, aid, key, cipher,
1878 					    length);
1879 		break;
1880 	}
1881 	case DISABLE_KEY:
1882 		ret = mm81x_cmd_disable_key(mors, mors_vif, aid, key);
1883 		if (ret) {
1884 			/* Must return 0 */
1885 			dev_warn(mors->dev, "Failed to remove key");
1886 			ret = 0;
1887 		}
1888 		break;
1889 	default:
1890 		WARN_ON(1);
1891 	}
1892 
1893 	if (ret) {
1894 		dev_dbg(mors->dev, "Falling back to software crypto");
1895 		ret = 1;
1896 	}
1897 
1898 exit:
1899 	return ret;
1900 }
1901 
1902 static int mm81x_mac_set_frag_threshold(struct ieee80211_hw *hw, int radio_idx,
1903 					u32 value)
1904 {
1905 	struct mm81x *mors = hw->priv;
1906 
1907 	return mm81x_cmd_set_frag_threshold(mors, value);
1908 }
1909 
1910 static u8 mm81x_rx_h_rc_bw_to_rx_bw(__le32 ratecode)
1911 {
1912 	enum dot11_bandwidth bw = mm81x_ratecode_bw_index_get(ratecode);
1913 
1914 	switch (bw) {
1915 	case DOT11_BANDWIDTH_1MHZ:
1916 		return RATE_INFO_BW_1;
1917 	case DOT11_BANDWIDTH_2MHZ:
1918 		return RATE_INFO_BW_2;
1919 	case DOT11_BANDWIDTH_4MHZ:
1920 		return RATE_INFO_BW_4;
1921 	case DOT11_BANDWIDTH_8MHZ:
1922 		return RATE_INFO_BW_8;
1923 	default:
1924 		return RATE_INFO_BW_1;
1925 	}
1926 }
1927 
1928 static u32 mm81x_rx_h_ampdu_reference(struct mm81x *mors, u64 timestamp)
1929 {
1930 	if (timestamp != mors->rx_ampdu.timestamp) {
1931 		mors->rx_ampdu.timestamp = timestamp;
1932 		mors->rx_ampdu.reference++;
1933 	}
1934 
1935 	return mors->rx_ampdu.reference;
1936 }
1937 
1938 static enum ieee80211_radiotap_s1g_ppdu_format
1939 mm81x_rx_h_ppdu_format(__le32 ratecode)
1940 {
1941 	switch (mm81x_ratecode_preamble_get(ratecode)) {
1942 	case MM81X_RATE_PREAMBLE_S1G_1M:
1943 		return IEEE80211_RADIOTAP_S1G_PPDU_FORMAT_1M;
1944 	case MM81X_RATE_PREAMBLE_S1G_LONG:
1945 		return IEEE80211_RADIOTAP_S1G_PPDU_FORMAT_LONG;
1946 	default:
1947 		return IEEE80211_RADIOTAP_S1G_PPDU_FORMAT_SHORT;
1948 	}
1949 }
1950 
1951 #define S1G_RADIOTAP_TLV_LEN				\
1952 	(sizeof(struct ieee80211_radiotap_tlv) +	\
1953 	 ALIGN(sizeof(struct ieee80211_radiotap_s1g), 4))
1954 
1955 static void mm81x_rx_h_add_rt_s1g_tlv(struct sk_buff *skb,
1956 				      struct mm81x_skb_rx_status *hdr_rx_status,
1957 				      struct ieee80211_rx_status *rx_status)
1958 {
1959 	__le32 ratecode = hdr_rx_status->mm81x_ratecode;
1960 	u32 flags = le32_to_cpu(hdr_rx_status->flags);
1961 	u8 color = hdr_rx_status->bss_color;
1962 	struct ieee80211_radiotap_tlv *tlv;
1963 	struct ieee80211_radiotap_s1g s1g;
1964 
1965 	if (skb_headroom(skb) < S1G_RADIOTAP_TLV_LEN)
1966 		return;
1967 
1968 	s1g.known = cpu_to_le16(IEEE80211_RADIOTAP_S1G_KNOWN_PPDU_FORMAT |
1969 				IEEE80211_RADIOTAP_S1G_KNOWN_RESPONSE_IND |
1970 				IEEE80211_RADIOTAP_S1G_KNOWN_GI |
1971 				IEEE80211_RADIOTAP_S1G_KNOWN_BW |
1972 				IEEE80211_RADIOTAP_S1G_KNOWN_UPLINK_IND |
1973 				IEEE80211_RADIOTAP_S1G_KNOWN_NSS |
1974 				IEEE80211_RADIOTAP_S1G_KNOWN_MCS |
1975 				IEEE80211_RADIOTAP_S1G_KNOWN_COLOR);
1976 
1977 	s1g.data1 = le16_encode_bits(mm81x_rx_h_ppdu_format(ratecode),
1978 				     IEEE80211_RADIOTAP_S1G_DATA1_PPDU_FORMAT) |
1979 		    le16_encode_bits(MM81X_RX_STATUS_FLAGS_RI_GET(flags),
1980 				     IEEE80211_RADIOTAP_S1G_DATA1_RESPONSE_IND) |
1981 		    le16_encode_bits(mm81x_ratecode_sgi_get(ratecode),
1982 				     IEEE80211_RADIOTAP_S1G_DATA1_GI) |
1983 		    le16_encode_bits(mm81x_ratecode_nss_index_get(ratecode),
1984 				     IEEE80211_RADIOTAP_S1G_DATA1_NSS) |
1985 		    le16_encode_bits(mm81x_ratecode_bw_index_get(ratecode),
1986 				     IEEE80211_RADIOTAP_S1G_DATA1_BW) |
1987 		    le16_encode_bits(mm81x_ratecode_mcs_index_get(ratecode),
1988 				     IEEE80211_RADIOTAP_S1G_DATA1_MCS);
1989 
1990 	s1g.data2 = le16_encode_bits(color, IEEE80211_RADIOTAP_S1G_DATA2_COLOR) |
1991 		    le16_encode_bits(MM81X_RX_STATUS_FLAGS_UPL_IND_GET(flags),
1992 				     IEEE80211_RADIOTAP_S1G_DATA2_UPLINK_IND) |
1993 		    le16_encode_bits((u8)rx_status->signal,
1994 				     IEEE80211_RADIOTAP_S1G_DATA2_RSSI);
1995 
1996 	tlv = skb_push(skb, S1G_RADIOTAP_TLV_LEN);
1997 	tlv->type = cpu_to_le16(IEEE80211_RADIOTAP_S1G);
1998 	tlv->len = cpu_to_le16(sizeof(s1g));
1999 
2000 	memcpy(tlv->data, &s1g, sizeof(s1g));
2001 	memset(tlv->data + sizeof(s1g), 0,
2002 	       S1G_RADIOTAP_TLV_LEN - sizeof(*tlv) - sizeof(s1g));
2003 
2004 	skb_set_mac_header(skb, S1G_RADIOTAP_TLV_LEN);
2005 	rx_status->flag |= RX_FLAG_RADIOTAP_TLV_AT_END;
2006 }
2007 
2008 static void mm81x_rx_h_fill_status(struct mm81x *mors,
2009 				   struct mm81x_skb_rx_status *hdr_rx_status,
2010 				   struct ieee80211_rx_status *rx_status,
2011 				   struct sk_buff *skb)
2012 {
2013 	u64 timestamp = le64_to_cpu(hdr_rx_status->rx_timestamp_us);
2014 	u16 freq_100khz = le16_to_cpu(hdr_rx_status->freq_100khz);
2015 	__le32 ratecode = hdr_rx_status->mm81x_ratecode;
2016 	u32 flags = le32_to_cpu(hdr_rx_status->flags);
2017 
2018 	rx_status->signal = le16_to_cpu(hdr_rx_status->rssi);
2019 	rx_status->encoding = RX_ENC_S1G;
2020 	rx_status->band = NL80211_BAND_S1GHZ;
2021 	rx_status->freq = KHZ100_TO_MHZ(freq_100khz);
2022 	rx_status->freq_offset = (freq_100khz % 10) ? 1 : 0;
2023 	rx_status->nss = NSS_IDX_TO_NSS(mm81x_ratecode_nss_index_get(ratecode));
2024 	rx_status->rate_idx = mm81x_ratecode_mcs_index_get(ratecode);
2025 	rx_status->bw = mm81x_rx_h_rc_bw_to_rx_bw(ratecode);
2026 	rx_status->mactime = timestamp;
2027 	rx_status->flag |= RX_FLAG_MACTIME_IS_RTAP_TS64;
2028 
2029 	if (flags & MM81X_RX_STATUS_FLAGS_DECRYPTED)
2030 		rx_status->flag |= RX_FLAG_DECRYPTED;
2031 
2032 	if (mm81x_ratecode_sgi_get(ratecode))
2033 		rx_status->enc_flags |= RX_ENC_FLAG_SHORT_GI;
2034 
2035 	if (mors->monitor_en) {
2036 		if (flags & MM81X_RX_STATUS_FLAGS_CRC_ERROR)
2037 			rx_status->flag |= RX_FLAG_FAILED_FCS_CRC;
2038 
2039 		if (flags & MM81X_RX_STATUS_FLAGS_AMPDU) {
2040 			rx_status->flag |= RX_FLAG_AMPDU_DETAILS |
2041 					   RX_FLAG_AMPDU_EOF_BIT_KNOWN;
2042 			rx_status->ampdu_reference =
2043 				mm81x_rx_h_ampdu_reference(mors, timestamp);
2044 
2045 			if (flags & MM81X_RX_STATUS_FLAGS_EOF)
2046 				rx_status->flag |= RX_FLAG_AMPDU_EOF_BIT;
2047 		}
2048 
2049 		if (flags & MM81X_RX_STATUS_FLAGS_NDP) {
2050 			rx_status->flag |= RX_FLAG_NO_PSDU;
2051 
2052 			/*
2053 			 * S1G PSDUs are not standardised in radiotap yet, so
2054 			 * for now report as sounding PSDUs so that acks etc.
2055 			 * can be passed through.
2056 			 */
2057 			rx_status->zero_length_psdu_type =
2058 				IEEE80211_RADIOTAP_ZERO_LEN_PSDU_SOUNDING;
2059 
2060 			/*
2061 			 * Null Data Packets contain no data, therefore no
2062 			 * mcs encoding. The STF/LTF are usually BPSK, therefore
2063 			 * the NDP mcs rate can always be considered as 0.
2064 			 */
2065 			rx_status->rate_idx = 0;
2066 			rx_status->nss = 1;
2067 		}
2068 	}
2069 }
2070 
2071 static void mm81x_rx_h_update_sta(struct ieee80211_sta *sta,
2072 				  struct ieee80211_rx_status *rx_status)
2073 {
2074 	struct mm81x_sta *msta = (void *)sta->drv_priv;
2075 
2076 	lockdep_assert_in_rcu_read_lock();
2077 
2078 	if (msta->avg_rssi) {
2079 		msta->avg_rssi =
2080 			CALC_AVG_RSSI(msta->avg_rssi, rx_status->signal);
2081 	} else {
2082 		msta->avg_rssi = rx_status->signal;
2083 	}
2084 }
2085 
2086 static struct ieee80211_vif *
2087 mm81x_rx_h_skb_get_vif(struct mm81x *mors, struct sk_buff *skb,
2088 		       struct mm81x_skb_rx_status *hdr_rx_status)
2089 {
2090 	u8 vif_id = u32_get_bits(le32_to_cpu(hdr_rx_status->flags),
2091 				 MM81X_RX_STATUS_FLAGS_VIF_ID);
2092 
2093 	lockdep_assert_in_rcu_read_lock();
2094 
2095 	if (vif_id == INVALID_VIF_INDEX)
2096 		return NULL;
2097 
2098 	return mm81x_rcu_dereference_vif_id(mors, vif_id, true);
2099 }
2100 
2101 void mm81x_mac_rx_skb(struct mm81x *mors, struct sk_buff *skb,
2102 		      struct mm81x_skb_rx_status *hdr_rx_status)
2103 {
2104 	struct ieee80211_vif *vif;
2105 	struct ieee80211_sta *sta = NULL;
2106 	struct ieee80211_hw *hw = mors->hw;
2107 	struct ieee80211_rx_status rx_status;
2108 	struct ieee80211_hdr *hdr = (void *)skb->data;
2109 	u8 *lookup;
2110 
2111 	memset(&rx_status, 0, sizeof(rx_status));
2112 
2113 	if (!mors->started || !skb->data || !skb->len) {
2114 		dev_kfree_skb_any(skb);
2115 		return;
2116 	}
2117 
2118 	lookup = ieee80211_is_s1g_beacon(hdr->frame_control) ? hdr->addr1 :
2119 							       hdr->addr2;
2120 
2121 	mm81x_rx_h_fill_status(mors, hdr_rx_status, &rx_status, skb);
2122 
2123 	scoped_guard(rcu) {
2124 		vif = mm81x_rx_h_skb_get_vif(mors, skb, hdr_rx_status);
2125 		if (vif) {
2126 			sta = ieee80211_find_sta(vif, lookup);
2127 			if (sta)
2128 				mm81x_rx_h_update_sta(sta, &rx_status);
2129 		}
2130 
2131 		if (mors->monitor_en)
2132 			mm81x_rx_h_add_rt_s1g_tlv(skb, hdr_rx_status, &rx_status);
2133 
2134 		memcpy(IEEE80211_SKB_RXCB(skb), &rx_status, sizeof(rx_status));
2135 
2136 		local_bh_disable();
2137 		ieee80211_rx_napi(hw, sta ? &sta->deflink : NULL, skb, NULL);
2138 		local_bh_enable();
2139 	}
2140 }
2141 
2142 static void mm81x_mac_flush_queues(struct mm81x *mors)
2143 {
2144 	/*
2145 	 * No need to call mm81x_skbq_stop_tx_queues as mac80211
2146 	 * has already cancelled each queue prior to calling .flush()
2147 	 */
2148 	mm81x_skbq_data_traffic_pause(mors);
2149 
2150 	flush_work(&mors->hif_work);
2151 	flush_work(&mors->tx_stale_work);
2152 
2153 	mm81x_hif_clear_events(mors);
2154 	mm81x_hif_flush_tx_data(mors);
2155 	mm81x_hif_flush_cmds(mors);
2156 
2157 	/* Re-enable data, not that there will be any */
2158 	mm81x_skbq_data_traffic_resume(mors);
2159 }
2160 
2161 static bool mm81x_mac_has_tx_pending(struct mm81x *mors)
2162 {
2163 	struct mm81x_skbq *mgmt_q = mm81x_hif_get_tx_mgmt_queue(mors);
2164 	struct mm81x_skbq *tx_qs;
2165 	int num_qs, i;
2166 
2167 	mm81x_hif_skbq_get_tx_qs(mors, &tx_qs, &num_qs);
2168 	for (i = 0; i < num_qs; i++)
2169 		if (mm81x_skbq_count(&tx_qs[i]) ||
2170 		    mm81x_skbq_pending_count(&tx_qs[i]))
2171 			return true;
2172 
2173 	if (mm81x_skbq_count(mgmt_q) || mm81x_skbq_pending_count(mgmt_q))
2174 		return true;
2175 
2176 	return false;
2177 }
2178 
2179 static void mm81x_mac_wait_queues(struct mm81x *mors)
2180 {
2181 	if (!wait_event_timeout(mors->tx_empty_waitq,
2182 				!mm81x_mac_has_tx_pending(mors),
2183 				MM81X_FLUSH_TIMEOUT))
2184 		dev_warn(mors->dev, "Unable to empty queues before timeout");
2185 }
2186 
2187 static void mm81x_mac_ops_flush(struct ieee80211_hw *hw,
2188 				struct ieee80211_vif *vif, u32 queues,
2189 				bool drop)
2190 {
2191 	struct mm81x *mors = hw->priv;
2192 
2193 	/* We don't support IEEE80211_HW_QUEUE_CONTROL so flush all queues */
2194 	if (drop)
2195 		mm81x_mac_flush_queues(mors);
2196 	else
2197 		mm81x_mac_wait_queues(mors);
2198 }
2199 
2200 static bool mm81x_mac_ops_tx_frames_pending(struct ieee80211_hw *hw)
2201 {
2202 	struct mm81x *mors = hw->priv;
2203 
2204 	return mm81x_mac_has_tx_pending(mors);
2205 }
2206 
2207 static int mm81x_mac_ops_set_rts_threshold(struct ieee80211_hw *hw,
2208 					   int radio_idx, u32 value)
2209 {
2210 	struct mm81x *mors = hw->priv;
2211 
2212 	mors->rts_threshold = value;
2213 	return 0;
2214 }
2215 
2216 static void mm81x_mac_ops_sta_statistics(struct ieee80211_hw *hw,
2217 					 struct ieee80211_vif *vif,
2218 					 struct ieee80211_sta *sta,
2219 					 struct station_info *sinfo)
2220 {
2221 	struct mm81x_sta *msta = (struct mm81x_sta *)sta->drv_priv;
2222 	struct mm81x *mors = hw->priv;
2223 	const struct mmrc_table *tb = msta->rc.tb;
2224 	struct mmrc_rate rate;
2225 
2226 	if (!tb || tb->best_tp.rate == MMRC_MCS_UNUSED) {
2227 		sinfo->filled &= ~BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2228 		return;
2229 	}
2230 
2231 	rate = tb->best_tp;
2232 	sinfo->txrate.mcs = rate.rate;
2233 	sinfo->txrate.nss = NSS_IDX_TO_NSS(rate.ss);
2234 	sinfo->txrate.flags = RATE_INFO_FLAGS_S1G_MCS;
2235 	switch (rate.bw) {
2236 	case MMRC_BW_1MHZ:
2237 		sinfo->txrate.bw = RATE_INFO_BW_1;
2238 		break;
2239 	case MMRC_BW_2MHZ:
2240 		sinfo->txrate.bw = RATE_INFO_BW_2;
2241 		break;
2242 	case MMRC_BW_4MHZ:
2243 		sinfo->txrate.bw = RATE_INFO_BW_4;
2244 		break;
2245 	case MMRC_BW_8MHZ:
2246 		sinfo->txrate.bw = RATE_INFO_BW_8;
2247 		break;
2248 	default:
2249 		break;
2250 	}
2251 
2252 	if (rate.guard == MMRC_GUARD_SHORT)
2253 		sinfo->txrate.flags |= (RATE_INFO_FLAGS_SHORT_GI);
2254 
2255 	dev_dbg(mors->dev, "mcs: %d, bw: %d, flag: 0x%x", rate.rate, rate.bw,
2256 		sinfo->txrate.flags);
2257 	sinfo->filled |= BIT_ULL(NL80211_STA_INFO_TX_BITRATE);
2258 }
2259 
2260 static u32 mm81x_get_expected_throughput(struct ieee80211_hw *hw,
2261 					 struct ieee80211_sta *sta)
2262 {
2263 	struct mm81x_sta *msta = (struct mm81x_sta *)sta->drv_priv;
2264 	struct mm81x *mors = hw->priv;
2265 	const struct mmrc_table *tb = msta->rc.tb;
2266 	struct mmrc_rate rate;
2267 	u32 tput;
2268 
2269 	if (!tb || tb->best_tp.rate == MMRC_MCS_UNUSED)
2270 		return 0;
2271 
2272 	rate = tb->best_tp;
2273 	tput = BPS_TO_KBPS(mmrc_calculate_theoretical_throughput(rate));
2274 	dev_dbg(mors->dev, "Throughput: MCS: %d, BW: %d, GI: %d -> %u",
2275 		rate.rate, 1 << rate.bw, rate.guard, tput);
2276 
2277 	return tput;
2278 }
2279 
2280 static void mm81x_mac_restart_cleanup_iter(void *data, u8 *mac,
2281 					   struct ieee80211_vif *vif)
2282 {
2283 	if (vif->type == NL80211_IFTYPE_AP)
2284 		mm81x_mac_beacon_finish((struct mm81x_vif *)vif->drv_priv);
2285 }
2286 
2287 static void mm81x_mac_restart_cleanup(struct mm81x *mors)
2288 {
2289 	ieee80211_iterate_active_interfaces(mors->hw,
2290 					    IEEE80211_IFACE_ITER_NORMAL,
2291 					    mm81x_mac_restart_cleanup_iter,
2292 					    NULL);
2293 	mm81x_mac_hw_scan_finish(mors);
2294 }
2295 
2296 static int mm81x_mac_restart(struct mm81x *mors)
2297 {
2298 	int ret;
2299 	u32 chip_id;
2300 
2301 	mors->started = false;
2302 	mm81x_ps_disable(mors);
2303 	mm81x_bus_set_irq(mors, false);
2304 	mm81x_hw_irq_clear(mors);
2305 	ieee80211_stop_queues(mors->hw);
2306 
2307 	set_bit(MM81X_STATE_DATA_TX_STOPPED, &mors->state_flags);
2308 	set_bit(MM81X_STATE_DATA_QS_STOPPED, &mors->state_flags);
2309 
2310 	/* Allow time for in-transit tx/rx packets to settle */
2311 	mdelay(MM81X_HW_RESTART_DELAY_MS);
2312 	flush_work(&mors->hif_work);
2313 	flush_work(&mors->tx_stale_work);
2314 	mm81x_hif_clear_events(mors);
2315 	mm81x_hif_flush_tx_data(mors);
2316 	mm81x_hif_flush_cmds(mors);
2317 
2318 	mm81x_claim_bus(mors);
2319 	ret = mm81x_reg32_read(mors, MM81X_REG_CHIP_ID(mors), &chip_id);
2320 	mm81x_release_bus(mors);
2321 
2322 	if (ret < 0) {
2323 		dev_err(mors->dev, "Failed to access HW: %d", ret);
2324 		goto exit;
2325 	}
2326 
2327 	mm81x_mac_restart_cleanup(mors);
2328 
2329 	ret = mm81x_fw_init(mors, true);
2330 	if (ret < 0) {
2331 		dev_err(mors->dev, "Failed to init firmware: %d", ret);
2332 		goto exit;
2333 	}
2334 
2335 	mm81x_hw_irq_enable(mors, MM81X_INT_HW_STOP_NOTIFICATION_NUM, true);
2336 
2337 	ret = mm81x_fw_parse_ext_host_tbl(mors);
2338 	if (ret) {
2339 		dev_err(mors->dev, "failed to parse extended host table: %d",
2340 			ret);
2341 		goto exit;
2342 	}
2343 
2344 	mm81x_mac_caps_init(mors);
2345 
2346 	mm81x_bus_set_irq(mors, true);
2347 	clear_bit(MM81X_STATE_DATA_TX_STOPPED, &mors->state_flags);
2348 	clear_bit(MM81X_STATE_DATA_QS_STOPPED, &mors->state_flags);
2349 	clear_bit(MM81X_STATE_CHIP_UNRESPONSIVE, &mors->state_flags);
2350 	clear_bit(MM81X_STATE_RELOAD_FW_AFTER_START, &mors->state_flags);
2351 	mm81x_mac_check_fw_disabled_chans(mors->hw);
2352 	ieee80211_restart_hw(mors->hw);
2353 
2354 exit:
2355 	mm81x_ps_enable(mors);
2356 	return ret;
2357 }
2358 
2359 static int mm81x_mac_ops_add_interface(struct ieee80211_hw *hw,
2360 				       struct ieee80211_vif *vif)
2361 {
2362 	int ret = 0;
2363 	struct mm81x *mors = hw->priv;
2364 	struct mm81x_vif *mors_vif = (struct mm81x_vif *)vif->drv_priv;
2365 
2366 	if (test_bit(MM81X_STATE_RELOAD_FW_AFTER_START, &mors->state_flags)) {
2367 		dev_info(mors->dev, "Restarting chip with regdom: %s",
2368 			 mors->country);
2369 
2370 		ret = mm81x_mac_restart(mors);
2371 		if (ret) {
2372 			dev_err(mors->dev, "Failed to restart chip");
2373 			return ret;
2374 		}
2375 
2376 		/*
2377 		 * mac_restart will trigger ieee80211_hw_restart and
2378 		 * add_interface will re-enter. just exit here instead.
2379 		 */
2380 		return 0;
2381 	}
2382 
2383 	vif->driver_flags |= IEEE80211_VIF_BEACON_FILTER;
2384 	mors_vif->mors = mors;
2385 
2386 	ret = mm81x_cmd_add_if(mors, &mors_vif->id, vif->addr, vif->type);
2387 	if (ret) {
2388 		dev_err(mors->dev, "mm81x_cmd_add_if failed %d", ret);
2389 		return ret;
2390 	}
2391 
2392 	if (mors_vif->id >= ARRAY_SIZE(mors->vifs)) {
2393 		dev_err(mors->dev, "vif_id is too large %u", mors_vif->id);
2394 		ret = -EOPNOTSUPP;
2395 		return ret;
2396 	}
2397 
2398 	if (mors_vif->id != (mors_vif->id & MM81X_TX_CONF_FLAGS_VIF_ID_MASK)) {
2399 		dev_err(mors->dev, "invalid vif_id %u", mors_vif->id);
2400 		ret = -EOPNOTSUPP;
2401 		return ret;
2402 	}
2403 
2404 	rcu_assign_pointer(mors->vifs[mors_vif->id], vif);
2405 
2406 	ret = mm81x_cmd_get_capabilities(mors, mors_vif->id, &mors->fw_caps);
2407 	if (ret) {
2408 		dev_err(mors->dev,
2409 			"mm81x_cmd_get_capabilities failed for vif %d",
2410 			mors_vif->id);
2411 		return ret;
2412 	}
2413 
2414 	if (vif->type == NL80211_IFTYPE_MONITOR)
2415 		ieee80211_hw_set(hw, RX_INCLUDES_FCS);
2416 
2417 	if (vif->type == NL80211_IFTYPE_AP)
2418 		INIT_WORK(&mors_vif->u.ap.beacon_work, mm81x_mac_beacon_work);
2419 
2420 	ieee80211_wake_queues(mors->hw);
2421 	return ret;
2422 }
2423 
2424 static const struct ieee80211_ops mm81x_ops = {
2425 	.start = mm81x_mac_ops_start,
2426 	.stop = mm81x_mac_ops_stop,
2427 	.config = mm81x_mac_ops_config,
2428 	.wake_tx_queue = ieee80211_handle_wake_tx_queue,
2429 	.tx = mm81x_mac_ops_tx,
2430 	.add_interface = mm81x_mac_ops_add_interface,
2431 	.remove_interface = mm81x_mac_ops_remove_interface,
2432 	.configure_filter = mm81x_mac_ops_configure_filter,
2433 	.sta_state = mm81x_mac_ops_sta_state,
2434 	.flush = mm81x_mac_ops_flush,
2435 	.tx_frames_pending = mm81x_mac_ops_tx_frames_pending,
2436 	.set_frag_threshold = mm81x_mac_set_frag_threshold,
2437 	.set_rts_threshold = mm81x_mac_ops_set_rts_threshold,
2438 	.sta_statistics = mm81x_mac_ops_sta_statistics,
2439 	.get_expected_throughput = mm81x_get_expected_throughput,
2440 	.hw_scan = mm81x_mac_ops_hw_scan,
2441 	.cancel_hw_scan = mm81x_mac_ops_cancel_hw_scan,
2442 	.get_txpower = mm81x_mac_ops_get_txpower,
2443 	.bss_info_changed = mm81x_mac_ops_bss_info_changed,
2444 	.prepare_multicast = mm81x_mac_ops_prepare_multicast,
2445 	.conf_tx = mm81x_mac_ops_conf_tx,
2446 	.ampdu_action = mm81x_mac_ops_ampdu_action,
2447 	.set_key = mm81x_mac_ops_set_key,
2448 	.add_chanctx = ieee80211_emulate_add_chanctx,
2449 	.remove_chanctx = ieee80211_emulate_remove_chanctx,
2450 	.change_chanctx = ieee80211_emulate_change_chanctx,
2451 	.switch_vif_chanctx = ieee80211_emulate_switch_vif_chanctx,
2452 };
2453 
2454 static void mm81x_reg_notifier(struct wiphy *wiphy,
2455 			       struct regulatory_request *request)
2456 {
2457 	int ret;
2458 	struct mm81x *mors = wiphy_to_ieee80211_hw(wiphy)->priv;
2459 
2460 	if (mm81x_reg_h_cc_equal(request->alpha2, "00") ||
2461 	    mm81x_reg_h_cc_equal(request->alpha2, mors->country))
2462 		return;
2463 
2464 	memcpy(mors->country, request->alpha2, sizeof(mors->country));
2465 
2466 	ret = mm81x_mac_restart(mors);
2467 	if (ret)
2468 		dev_err(mors->dev, "Failed to restart chip: %d", ret);
2469 }
2470 
2471 static void mm81x_mac_config_hw(struct mm81x *mors)
2472 {
2473 	int i;
2474 	struct ieee80211_hw *hw = mors->hw;
2475 	struct wiphy *wiphy;
2476 
2477 	for (i = 0; i < NUM_NL80211_BANDS; i++)
2478 		hw->wiphy->bands[i] = NULL;
2479 
2480 	hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_AP) |
2481 				     BIT(NL80211_IFTYPE_STATION);
2482 	hw->wiphy->reg_notifier = mm81x_reg_notifier;
2483 	hw->queues = MM81X_HW_QUEUE_COUNT;
2484 	hw->max_rates = MM81X_HW_MAX_RATES;
2485 	hw->max_report_rates = MM81X_HW_MAX_REPORT_RATES;
2486 	hw->max_rate_tries = MM81X_HW_MAX_RATE_TRIES;
2487 	hw->tx_sk_pacing_shift = MM81X_HW_TX_SK_PACING_SHIFT;
2488 	hw->vif_data_size = sizeof(struct mm81x_vif);
2489 	hw->sta_data_size = sizeof(struct mm81x_sta);
2490 	hw->extra_tx_headroom =
2491 		sizeof(struct mm81x_skb_hdr) + mm81x_bus_get_alignment(mors);
2492 	hw->radiotap_timestamp.units_pos =
2493 		IEEE80211_RADIOTAP_TIMESTAMP_UNIT_US |
2494 		IEEE80211_RADIOTAP_TIMESTAMP_SPOS_BEGIN_MDPU;
2495 
2496 	mors->wiphy = hw->wiphy;
2497 
2498 	ieee80211_hw_set(hw, SIGNAL_DBM);
2499 	ieee80211_hw_set(hw, MFP_CAPABLE);
2500 	ieee80211_hw_set(hw, REPORTS_TX_ACK_STATUS);
2501 	ieee80211_hw_set(hw, AMPDU_AGGREGATION);
2502 	ieee80211_hw_set(hw, HOST_BROADCAST_PS_BUFFERING);
2503 	ieee80211_hw_set(hw, HAS_RATE_CONTROL);
2504 	ieee80211_hw_set(hw, SUPPORTS_PS);
2505 	ieee80211_hw_set(hw, NEED_DTIM_BEFORE_ASSOC);
2506 	ieee80211_hw_set(hw, PS_NULLFUNC_STACK);
2507 	ieee80211_hw_set(hw, SUPPORTS_TX_FRAG);
2508 	ieee80211_hw_set(hw, SUPPORTS_NDP_BLOCKACK);
2509 	ieee80211_hw_set(hw, WANT_MONITOR_VIF);
2510 
2511 	SET_IEEE80211_PERM_ADDR(hw, mors->macaddr);
2512 
2513 	wiphy = mors->wiphy;
2514 
2515 	wiphy->flags |= WIPHY_FLAG_AP_UAPSD;
2516 	wiphy->flags |= WIPHY_FLAG_PS_ON_BY_DEFAULT;
2517 
2518 	if (!mors->ps.enable)
2519 		wiphy->flags &= ~WIPHY_FLAG_PS_ON_BY_DEFAULT;
2520 
2521 	wiphy->features |= NL80211_FEATURE_AP_MODE_CHAN_WIDTH_CHANGE |
2522 			   NL80211_FEATURE_TX_POWER_INSERTION;
2523 
2524 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_AIRTIME_FAIRNESS);
2525 	wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_SET_SCAN_DWELL);
2526 
2527 	wiphy->iface_combinations = mors_if_combs;
2528 	wiphy->n_iface_combinations = ARRAY_SIZE(mors_if_combs);
2529 	wiphy->max_scan_ie_len = MM81X_MAX_SCAN_IE_LEN;
2530 	wiphy->max_scan_ssids = MM81X_MAX_SCAN_SSIDS;
2531 	wiphy->signal_type = CFG80211_SIGNAL_TYPE_MBM;
2532 	wiphy->max_remain_on_channel_duration =
2533 		MM81X_MAX_REMAIN_ON_CHAN_DURATION;
2534 }
2535 
2536 static void mm81x_stale_tx_status_timer(struct timer_list *t)
2537 {
2538 	struct mm81x *mors = timer_container_of(mors, t, stale_status.timer);
2539 
2540 	spin_lock_bh(&mors->stale_status.lock);
2541 	if (mm81x_hif_get_tx_status_pending_count(mors))
2542 		queue_work(mors->net_wq, &mors->tx_stale_work);
2543 	spin_unlock_bh(&mors->stale_status.lock);
2544 }
2545 
2546 static void mm81x_stale_tx_status_timer_finish(struct mm81x *mors)
2547 {
2548 	timer_shutdown_sync(&mors->stale_status.timer);
2549 }
2550 
2551 static void mm81x_mac_stale_tx_status_timer_init(struct mm81x *mors)
2552 {
2553 	spin_lock_init(&mors->stale_status.lock);
2554 	timer_setup(&mors->stale_status.timer, mm81x_stale_tx_status_timer, 0);
2555 }
2556 
2557 static int mm81x_mac_set_sband(struct mm81x *mors)
2558 {
2559 	struct ieee80211_supported_band *sband;
2560 
2561 	sband = devm_kmemdup(mors->dev, &mors_band_s1ghz, sizeof(*sband),
2562 			     GFP_KERNEL);
2563 	if (!sband)
2564 		return -ENOMEM;
2565 
2566 	sband->channels = devm_kmemdup_array(mors->dev, mors_s1ghz_channels,
2567 					     ARRAY_SIZE(mors_s1ghz_channels),
2568 					     sizeof(*mors_s1ghz_channels),
2569 					     GFP_KERNEL);
2570 	if (!sband->channels)
2571 		return -ENOMEM;
2572 
2573 	sband->n_channels = ARRAY_SIZE(mors_s1ghz_channels);
2574 	mors->hw->wiphy->bands[NL80211_BAND_S1GHZ] = sband;
2575 
2576 	return 0;
2577 }
2578 
2579 int mm81x_mac_register(struct mm81x *mors)
2580 {
2581 	int ret;
2582 	struct ieee80211_hw *hw = mors->hw;
2583 
2584 	mors->tx_power_mbm = INT_MAX;
2585 	mors->tx_max_power_mbm = INT_MAX;
2586 	mors->rts_threshold = IEEE80211_MAX_RTS_THRESHOLD;
2587 
2588 	ret = mm81x_ps_init(mors);
2589 	if (ret)
2590 		return ret;
2591 
2592 	mm81x_mac_config_hw(mors);
2593 
2594 	ret = mm81x_mac_set_sband(mors);
2595 	if (ret)
2596 		return ret;
2597 
2598 	mm81x_mac_hw_scan_init(mors);
2599 	mm81x_mac_stale_tx_status_timer_init(mors);
2600 
2601 	ret = ieee80211_register_hw(hw);
2602 	if (ret) {
2603 		dev_err(mors->dev, "ieee80211_register_hw failed %d", ret);
2604 		mm81x_mac_unregister(mors);
2605 		return ret;
2606 	}
2607 
2608 	mm81x_rc_init(mors);
2609 
2610 	/*
2611 	 * At this stage, we know bus and pager system interrupts are enabled.
2612 	 * Trigger the receive workqueue to drain any incoming chip-to-host
2613 	 * pending packets been pushed in the period between the firmware
2614 	 * initialization and interrupts being enabled.
2615 	 */
2616 	set_bit(MM81X_HIF_EVT_RX_PEND, &mors->hif.event_flags);
2617 	queue_work(mors->chip_wq, &mors->hif_work);
2618 
2619 	return ret;
2620 }
2621 
2622 void mm81x_mac_unregister(struct mm81x *mors)
2623 {
2624 	mm81x_ps_disable(mors);
2625 	mm81x_rc_deinit(mors);
2626 	mm81x_mac_hw_scan_destroy(mors);
2627 
2628 	ieee80211_stop_queues(mors->hw);
2629 	ieee80211_unregister_hw(mors->hw);
2630 
2631 	mm81x_hif_flush_tx_data(mors);
2632 	mm81x_hif_flush_cmds(mors);
2633 	mm81x_stale_tx_status_timer_finish(mors);
2634 	mm81x_ps_finish(mors);
2635 
2636 	kfree(mors->mcast_filter);
2637 }
2638 
2639 struct mm81x *mm81x_mac_alloc(size_t priv_size, struct device *dev)
2640 {
2641 	struct ieee80211_hw *hw;
2642 	struct mm81x *mors;
2643 
2644 	hw = ieee80211_alloc_hw(sizeof(*mors) + priv_size, &mm81x_ops);
2645 	if (!hw) {
2646 		dev_err(dev, "ieee80211_alloc_hw failed\r\n");
2647 		return NULL;
2648 	}
2649 
2650 	SET_IEEE80211_DEV(hw, dev);
2651 	memset(hw->priv, 0, sizeof(*mors));
2652 
2653 	mors = hw->priv;
2654 	mors->hw = hw;
2655 	mors->dev = dev;
2656 	mutex_init(&mors->cmd_lock);
2657 	mutex_init(&mors->cmd_wait);
2658 	init_waitqueue_head(&mors->tx_empty_waitq);
2659 
2660 	return mors;
2661 }
2662 
2663 void mm81x_mac_free(struct mm81x *mors)
2664 {
2665 	ieee80211_free_hw(mors->hw);
2666 }
2667