xref: /linux/drivers/net/wireless/realtek/rtw88/fw.c (revision 8a922b7728a93d837954315c98b84f6b78de0c4f)
1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright(c) 2018-2019  Realtek Corporation
3  */
4 
5 #include <linux/iopoll.h>
6 
7 #include "main.h"
8 #include "coex.h"
9 #include "fw.h"
10 #include "tx.h"
11 #include "reg.h"
12 #include "sec.h"
13 #include "debug.h"
14 #include "util.h"
15 #include "wow.h"
16 #include "ps.h"
17 #include "phy.h"
18 #include "mac.h"
19 
20 static void rtw_fw_c2h_cmd_handle_ext(struct rtw_dev *rtwdev,
21 				      struct sk_buff *skb)
22 {
23 	struct rtw_c2h_cmd *c2h;
24 	u8 sub_cmd_id;
25 
26 	c2h = get_c2h_from_skb(skb);
27 	sub_cmd_id = c2h->payload[0];
28 
29 	switch (sub_cmd_id) {
30 	case C2H_CCX_RPT:
31 		rtw_tx_report_handle(rtwdev, skb, C2H_CCX_RPT);
32 		break;
33 	case C2H_SCAN_STATUS_RPT:
34 		rtw_hw_scan_status_report(rtwdev, skb);
35 		break;
36 	case C2H_CHAN_SWITCH:
37 		rtw_hw_scan_chan_switch(rtwdev, skb);
38 		break;
39 	default:
40 		break;
41 	}
42 }
43 
44 static u16 get_max_amsdu_len(u32 bit_rate)
45 {
46 	/* lower than ofdm, do not aggregate */
47 	if (bit_rate < 550)
48 		return 1;
49 
50 	/* lower than 20M 2ss mcs8, make it small */
51 	if (bit_rate < 1800)
52 		return 1200;
53 
54 	/* lower than 40M 2ss mcs9, make it medium */
55 	if (bit_rate < 4000)
56 		return 2600;
57 
58 	/* not yet 80M 2ss mcs8/9, make it twice regular packet size */
59 	if (bit_rate < 7000)
60 		return 3500;
61 
62 	/* unlimited */
63 	return 0;
64 }
65 
66 struct rtw_fw_iter_ra_data {
67 	struct rtw_dev *rtwdev;
68 	u8 *payload;
69 };
70 
71 static void rtw_fw_ra_report_iter(void *data, struct ieee80211_sta *sta)
72 {
73 	struct rtw_fw_iter_ra_data *ra_data = data;
74 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
75 	u8 mac_id, rate, sgi, bw;
76 	u8 mcs, nss;
77 	u32 bit_rate;
78 
79 	mac_id = GET_RA_REPORT_MACID(ra_data->payload);
80 	if (si->mac_id != mac_id)
81 		return;
82 
83 	si->ra_report.txrate.flags = 0;
84 
85 	rate = GET_RA_REPORT_RATE(ra_data->payload);
86 	sgi = GET_RA_REPORT_SGI(ra_data->payload);
87 	bw = GET_RA_REPORT_BW(ra_data->payload);
88 
89 	if (rate < DESC_RATEMCS0) {
90 		si->ra_report.txrate.legacy = rtw_desc_to_bitrate(rate);
91 		goto legacy;
92 	}
93 
94 	rtw_desc_to_mcsrate(rate, &mcs, &nss);
95 	if (rate >= DESC_RATEVHT1SS_MCS0)
96 		si->ra_report.txrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
97 	else if (rate >= DESC_RATEMCS0)
98 		si->ra_report.txrate.flags |= RATE_INFO_FLAGS_MCS;
99 
100 	if (rate >= DESC_RATEMCS0) {
101 		si->ra_report.txrate.mcs = mcs;
102 		si->ra_report.txrate.nss = nss;
103 	}
104 
105 	if (sgi)
106 		si->ra_report.txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
107 
108 	if (bw == RTW_CHANNEL_WIDTH_80)
109 		si->ra_report.txrate.bw = RATE_INFO_BW_80;
110 	else if (bw == RTW_CHANNEL_WIDTH_40)
111 		si->ra_report.txrate.bw = RATE_INFO_BW_40;
112 	else
113 		si->ra_report.txrate.bw = RATE_INFO_BW_20;
114 
115 legacy:
116 	bit_rate = cfg80211_calculate_bitrate(&si->ra_report.txrate);
117 
118 	si->ra_report.desc_rate = rate;
119 	si->ra_report.bit_rate = bit_rate;
120 
121 	sta->deflink.agg.max_rc_amsdu_len = get_max_amsdu_len(bit_rate);
122 }
123 
124 static void rtw_fw_ra_report_handle(struct rtw_dev *rtwdev, u8 *payload,
125 				    u8 length)
126 {
127 	struct rtw_fw_iter_ra_data ra_data;
128 
129 	if (WARN(length < 7, "invalid ra report c2h length\n"))
130 		return;
131 
132 	rtwdev->dm_info.tx_rate = GET_RA_REPORT_RATE(payload);
133 	ra_data.rtwdev = rtwdev;
134 	ra_data.payload = payload;
135 	rtw_iterate_stas_atomic(rtwdev, rtw_fw_ra_report_iter, &ra_data);
136 }
137 
138 struct rtw_beacon_filter_iter_data {
139 	struct rtw_dev *rtwdev;
140 	u8 *payload;
141 };
142 
143 static void rtw_fw_bcn_filter_notify_vif_iter(void *data, u8 *mac,
144 					      struct ieee80211_vif *vif)
145 {
146 	struct rtw_beacon_filter_iter_data *iter_data = data;
147 	struct rtw_dev *rtwdev = iter_data->rtwdev;
148 	u8 *payload = iter_data->payload;
149 	u8 type = GET_BCN_FILTER_NOTIFY_TYPE(payload);
150 	u8 event = GET_BCN_FILTER_NOTIFY_EVENT(payload);
151 	s8 sig = (s8)GET_BCN_FILTER_NOTIFY_RSSI(payload);
152 
153 	switch (type) {
154 	case BCN_FILTER_NOTIFY_SIGNAL_CHANGE:
155 		event = event ? NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH :
156 			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
157 		ieee80211_cqm_rssi_notify(vif, event, sig, GFP_KERNEL);
158 		break;
159 	case BCN_FILTER_CONNECTION_LOSS:
160 		ieee80211_connection_loss(vif);
161 		break;
162 	case BCN_FILTER_CONNECTED:
163 		rtwdev->beacon_loss = false;
164 		break;
165 	case BCN_FILTER_NOTIFY_BEACON_LOSS:
166 		rtwdev->beacon_loss = true;
167 		rtw_leave_lps(rtwdev);
168 		break;
169 	}
170 }
171 
172 static void rtw_fw_bcn_filter_notify(struct rtw_dev *rtwdev, u8 *payload,
173 				     u8 length)
174 {
175 	struct rtw_beacon_filter_iter_data dev_iter_data;
176 
177 	dev_iter_data.rtwdev = rtwdev;
178 	dev_iter_data.payload = payload;
179 	rtw_iterate_vifs(rtwdev, rtw_fw_bcn_filter_notify_vif_iter,
180 			 &dev_iter_data);
181 }
182 
183 static void rtw_fw_scan_result(struct rtw_dev *rtwdev, u8 *payload,
184 			       u8 length)
185 {
186 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
187 
188 	dm_info->scan_density = payload[0];
189 
190 	rtw_dbg(rtwdev, RTW_DBG_FW, "scan.density = %x\n",
191 		dm_info->scan_density);
192 }
193 
194 static void rtw_fw_adaptivity_result(struct rtw_dev *rtwdev, u8 *payload,
195 				     u8 length)
196 {
197 	struct rtw_hw_reg_offset *edcca_th = rtwdev->chip->edcca_th;
198 	struct rtw_c2h_adaptivity *result = (struct rtw_c2h_adaptivity *)payload;
199 
200 	rtw_dbg(rtwdev, RTW_DBG_ADAPTIVITY,
201 		"Adaptivity: density %x igi %x l2h_th_init %x l2h %x h2l %x option %x\n",
202 		result->density, result->igi, result->l2h_th_init, result->l2h,
203 		result->h2l, result->option);
204 
205 	rtw_dbg(rtwdev, RTW_DBG_ADAPTIVITY, "Reg Setting: L2H %x H2L %x\n",
206 		rtw_read32_mask(rtwdev, edcca_th[EDCCA_TH_L2H_IDX].hw_reg.addr,
207 				edcca_th[EDCCA_TH_L2H_IDX].hw_reg.mask),
208 		rtw_read32_mask(rtwdev, edcca_th[EDCCA_TH_H2L_IDX].hw_reg.addr,
209 				edcca_th[EDCCA_TH_H2L_IDX].hw_reg.mask));
210 
211 	rtw_dbg(rtwdev, RTW_DBG_ADAPTIVITY, "EDCCA Flag %s\n",
212 		rtw_read32_mask(rtwdev, REG_EDCCA_REPORT, BIT_EDCCA_FLAG) ?
213 		"Set" : "Unset");
214 }
215 
216 void rtw_fw_c2h_cmd_handle(struct rtw_dev *rtwdev, struct sk_buff *skb)
217 {
218 	struct rtw_c2h_cmd *c2h;
219 	u32 pkt_offset;
220 	u8 len;
221 
222 	pkt_offset = *((u32 *)skb->cb);
223 	c2h = (struct rtw_c2h_cmd *)(skb->data + pkt_offset);
224 	len = skb->len - pkt_offset - 2;
225 
226 	mutex_lock(&rtwdev->mutex);
227 
228 	if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags))
229 		goto unlock;
230 
231 	switch (c2h->id) {
232 	case C2H_CCX_TX_RPT:
233 		rtw_tx_report_handle(rtwdev, skb, C2H_CCX_TX_RPT);
234 		break;
235 	case C2H_BT_INFO:
236 		rtw_coex_bt_info_notify(rtwdev, c2h->payload, len);
237 		break;
238 	case C2H_BT_HID_INFO:
239 		rtw_coex_bt_hid_info_notify(rtwdev, c2h->payload, len);
240 		break;
241 	case C2H_WLAN_INFO:
242 		rtw_coex_wl_fwdbginfo_notify(rtwdev, c2h->payload, len);
243 		break;
244 	case C2H_BCN_FILTER_NOTIFY:
245 		rtw_fw_bcn_filter_notify(rtwdev, c2h->payload, len);
246 		break;
247 	case C2H_HALMAC:
248 		rtw_fw_c2h_cmd_handle_ext(rtwdev, skb);
249 		break;
250 	case C2H_RA_RPT:
251 		rtw_fw_ra_report_handle(rtwdev, c2h->payload, len);
252 		break;
253 	default:
254 		rtw_dbg(rtwdev, RTW_DBG_FW, "C2H 0x%x isn't handled\n", c2h->id);
255 		break;
256 	}
257 
258 unlock:
259 	mutex_unlock(&rtwdev->mutex);
260 }
261 
262 void rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev *rtwdev, u32 pkt_offset,
263 			       struct sk_buff *skb)
264 {
265 	struct rtw_c2h_cmd *c2h;
266 	u8 len;
267 
268 	c2h = (struct rtw_c2h_cmd *)(skb->data + pkt_offset);
269 	len = skb->len - pkt_offset - 2;
270 	*((u32 *)skb->cb) = pkt_offset;
271 
272 	rtw_dbg(rtwdev, RTW_DBG_FW, "recv C2H, id=0x%02x, seq=0x%02x, len=%d\n",
273 		c2h->id, c2h->seq, len);
274 
275 	switch (c2h->id) {
276 	case C2H_BT_MP_INFO:
277 		rtw_coex_info_response(rtwdev, skb);
278 		break;
279 	case C2H_WLAN_RFON:
280 		complete(&rtwdev->lps_leave_check);
281 		dev_kfree_skb_any(skb);
282 		break;
283 	case C2H_SCAN_RESULT:
284 		complete(&rtwdev->fw_scan_density);
285 		rtw_fw_scan_result(rtwdev, c2h->payload, len);
286 		dev_kfree_skb_any(skb);
287 		break;
288 	case C2H_ADAPTIVITY:
289 		rtw_fw_adaptivity_result(rtwdev, c2h->payload, len);
290 		dev_kfree_skb_any(skb);
291 		break;
292 	default:
293 		/* pass offset for further operation */
294 		*((u32 *)skb->cb) = pkt_offset;
295 		skb_queue_tail(&rtwdev->c2h_queue, skb);
296 		ieee80211_queue_work(rtwdev->hw, &rtwdev->c2h_work);
297 		break;
298 	}
299 }
300 EXPORT_SYMBOL(rtw_fw_c2h_cmd_rx_irqsafe);
301 
302 void rtw_fw_c2h_cmd_isr(struct rtw_dev *rtwdev)
303 {
304 	if (rtw_read8(rtwdev, REG_MCU_TST_CFG) == VAL_FW_TRIGGER)
305 		rtw_fw_recovery(rtwdev);
306 	else
307 		rtw_warn(rtwdev, "unhandled firmware c2h interrupt\n");
308 }
309 EXPORT_SYMBOL(rtw_fw_c2h_cmd_isr);
310 
311 static void rtw_fw_send_h2c_command(struct rtw_dev *rtwdev,
312 				    u8 *h2c)
313 {
314 	struct rtw_h2c_cmd *h2c_cmd = (struct rtw_h2c_cmd *)h2c;
315 	u8 box;
316 	u8 box_state;
317 	u32 box_reg, box_ex_reg;
318 	int ret;
319 
320 	rtw_dbg(rtwdev, RTW_DBG_FW,
321 		"send H2C content %02x%02x%02x%02x %02x%02x%02x%02x\n",
322 		h2c[3], h2c[2], h2c[1], h2c[0],
323 		h2c[7], h2c[6], h2c[5], h2c[4]);
324 
325 	lockdep_assert_held(&rtwdev->mutex);
326 
327 	box = rtwdev->h2c.last_box_num;
328 	switch (box) {
329 	case 0:
330 		box_reg = REG_HMEBOX0;
331 		box_ex_reg = REG_HMEBOX0_EX;
332 		break;
333 	case 1:
334 		box_reg = REG_HMEBOX1;
335 		box_ex_reg = REG_HMEBOX1_EX;
336 		break;
337 	case 2:
338 		box_reg = REG_HMEBOX2;
339 		box_ex_reg = REG_HMEBOX2_EX;
340 		break;
341 	case 3:
342 		box_reg = REG_HMEBOX3;
343 		box_ex_reg = REG_HMEBOX3_EX;
344 		break;
345 	default:
346 		WARN(1, "invalid h2c mail box number\n");
347 		return;
348 	}
349 
350 	ret = read_poll_timeout_atomic(rtw_read8, box_state,
351 				       !((box_state >> box) & 0x1), 100, 3000,
352 				       false, rtwdev, REG_HMETFR);
353 
354 	if (ret) {
355 		rtw_err(rtwdev, "failed to send h2c command\n");
356 		return;
357 	}
358 
359 	rtw_write32(rtwdev, box_ex_reg, le32_to_cpu(h2c_cmd->msg_ext));
360 	rtw_write32(rtwdev, box_reg, le32_to_cpu(h2c_cmd->msg));
361 
362 	if (++rtwdev->h2c.last_box_num >= 4)
363 		rtwdev->h2c.last_box_num = 0;
364 }
365 
366 void rtw_fw_h2c_cmd_dbg(struct rtw_dev *rtwdev, u8 *h2c)
367 {
368 	rtw_fw_send_h2c_command(rtwdev, h2c);
369 }
370 
371 static void rtw_fw_send_h2c_packet(struct rtw_dev *rtwdev, u8 *h2c_pkt)
372 {
373 	int ret;
374 
375 	lockdep_assert_held(&rtwdev->mutex);
376 
377 	FW_OFFLOAD_H2C_SET_SEQ_NUM(h2c_pkt, rtwdev->h2c.seq);
378 	ret = rtw_hci_write_data_h2c(rtwdev, h2c_pkt, H2C_PKT_SIZE);
379 	if (ret)
380 		rtw_err(rtwdev, "failed to send h2c packet\n");
381 	rtwdev->h2c.seq++;
382 }
383 
384 void
385 rtw_fw_send_general_info(struct rtw_dev *rtwdev)
386 {
387 	struct rtw_fifo_conf *fifo = &rtwdev->fifo;
388 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
389 	u16 total_size = H2C_PKT_HDR_SIZE + 4;
390 
391 	if (rtw_chip_wcpu_11n(rtwdev))
392 		return;
393 
394 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_GENERAL_INFO);
395 
396 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
397 
398 	GENERAL_INFO_SET_FW_TX_BOUNDARY(h2c_pkt,
399 					fifo->rsvd_fw_txbuf_addr -
400 					fifo->rsvd_boundary);
401 
402 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
403 }
404 
405 void
406 rtw_fw_send_phydm_info(struct rtw_dev *rtwdev)
407 {
408 	struct rtw_hal *hal = &rtwdev->hal;
409 	struct rtw_efuse *efuse = &rtwdev->efuse;
410 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
411 	u16 total_size = H2C_PKT_HDR_SIZE + 8;
412 	u8 fw_rf_type = 0;
413 
414 	if (rtw_chip_wcpu_11n(rtwdev))
415 		return;
416 
417 	if (hal->rf_type == RF_1T1R)
418 		fw_rf_type = FW_RF_1T1R;
419 	else if (hal->rf_type == RF_2T2R)
420 		fw_rf_type = FW_RF_2T2R;
421 
422 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_PHYDM_INFO);
423 
424 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
425 	PHYDM_INFO_SET_REF_TYPE(h2c_pkt, efuse->rfe_option);
426 	PHYDM_INFO_SET_RF_TYPE(h2c_pkt, fw_rf_type);
427 	PHYDM_INFO_SET_CUT_VER(h2c_pkt, hal->cut_version);
428 	PHYDM_INFO_SET_RX_ANT_STATUS(h2c_pkt, hal->antenna_tx);
429 	PHYDM_INFO_SET_TX_ANT_STATUS(h2c_pkt, hal->antenna_rx);
430 
431 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
432 }
433 
434 void rtw_fw_do_iqk(struct rtw_dev *rtwdev, struct rtw_iqk_para *para)
435 {
436 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
437 	u16 total_size = H2C_PKT_HDR_SIZE + 1;
438 
439 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_IQK);
440 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
441 	IQK_SET_CLEAR(h2c_pkt, para->clear);
442 	IQK_SET_SEGMENT_IQK(h2c_pkt, para->segment_iqk);
443 
444 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
445 }
446 EXPORT_SYMBOL(rtw_fw_do_iqk);
447 
448 void rtw_fw_inform_rfk_status(struct rtw_dev *rtwdev, bool start)
449 {
450 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
451 
452 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WIFI_CALIBRATION);
453 
454 	RFK_SET_INFORM_START(h2c_pkt, start);
455 
456 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
457 }
458 EXPORT_SYMBOL(rtw_fw_inform_rfk_status);
459 
460 void rtw_fw_query_bt_info(struct rtw_dev *rtwdev)
461 {
462 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
463 
464 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_INFO);
465 
466 	SET_QUERY_BT_INFO(h2c_pkt, true);
467 
468 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
469 }
470 
471 void rtw_fw_wl_ch_info(struct rtw_dev *rtwdev, u8 link, u8 ch, u8 bw)
472 {
473 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
474 
475 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WL_CH_INFO);
476 
477 	SET_WL_CH_INFO_LINK(h2c_pkt, link);
478 	SET_WL_CH_INFO_CHNL(h2c_pkt, ch);
479 	SET_WL_CH_INFO_BW(h2c_pkt, bw);
480 
481 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
482 }
483 
484 void rtw_fw_query_bt_mp_info(struct rtw_dev *rtwdev,
485 			     struct rtw_coex_info_req *req)
486 {
487 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
488 
489 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_MP_INFO);
490 
491 	SET_BT_MP_INFO_SEQ(h2c_pkt, req->seq);
492 	SET_BT_MP_INFO_OP_CODE(h2c_pkt, req->op_code);
493 	SET_BT_MP_INFO_PARA1(h2c_pkt, req->para1);
494 	SET_BT_MP_INFO_PARA2(h2c_pkt, req->para2);
495 	SET_BT_MP_INFO_PARA3(h2c_pkt, req->para3);
496 
497 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
498 }
499 
500 void rtw_fw_force_bt_tx_power(struct rtw_dev *rtwdev, u8 bt_pwr_dec_lvl)
501 {
502 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
503 	u8 index = 0 - bt_pwr_dec_lvl;
504 
505 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_FORCE_BT_TX_POWER);
506 
507 	SET_BT_TX_POWER_INDEX(h2c_pkt, index);
508 
509 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
510 }
511 
512 void rtw_fw_bt_ignore_wlan_action(struct rtw_dev *rtwdev, bool enable)
513 {
514 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
515 
516 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_IGNORE_WLAN_ACTION);
517 
518 	SET_IGNORE_WLAN_ACTION_EN(h2c_pkt, enable);
519 
520 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
521 }
522 
523 void rtw_fw_coex_tdma_type(struct rtw_dev *rtwdev,
524 			   u8 para1, u8 para2, u8 para3, u8 para4, u8 para5)
525 {
526 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
527 
528 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_COEX_TDMA_TYPE);
529 
530 	SET_COEX_TDMA_TYPE_PARA1(h2c_pkt, para1);
531 	SET_COEX_TDMA_TYPE_PARA2(h2c_pkt, para2);
532 	SET_COEX_TDMA_TYPE_PARA3(h2c_pkt, para3);
533 	SET_COEX_TDMA_TYPE_PARA4(h2c_pkt, para4);
534 	SET_COEX_TDMA_TYPE_PARA5(h2c_pkt, para5);
535 
536 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
537 }
538 
539 void rtw_fw_coex_query_hid_info(struct rtw_dev *rtwdev, u8 sub_id, u8 data)
540 {
541 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
542 
543 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_HID_INFO);
544 
545 	SET_COEX_QUERY_HID_INFO_SUBID(h2c_pkt, sub_id);
546 	SET_COEX_QUERY_HID_INFO_DATA1(h2c_pkt, data);
547 
548 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
549 }
550 
551 void rtw_fw_bt_wifi_control(struct rtw_dev *rtwdev, u8 op_code, u8 *data)
552 {
553 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
554 
555 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BT_WIFI_CONTROL);
556 
557 	SET_BT_WIFI_CONTROL_OP_CODE(h2c_pkt, op_code);
558 
559 	SET_BT_WIFI_CONTROL_DATA1(h2c_pkt, *data);
560 	SET_BT_WIFI_CONTROL_DATA2(h2c_pkt, *(data + 1));
561 	SET_BT_WIFI_CONTROL_DATA3(h2c_pkt, *(data + 2));
562 	SET_BT_WIFI_CONTROL_DATA4(h2c_pkt, *(data + 3));
563 	SET_BT_WIFI_CONTROL_DATA5(h2c_pkt, *(data + 4));
564 
565 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
566 }
567 
568 void rtw_fw_send_rssi_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si)
569 {
570 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
571 	u8 rssi = ewma_rssi_read(&si->avg_rssi);
572 	bool stbc_en = si->stbc_en ? true : false;
573 
574 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSSI_MONITOR);
575 
576 	SET_RSSI_INFO_MACID(h2c_pkt, si->mac_id);
577 	SET_RSSI_INFO_RSSI(h2c_pkt, rssi);
578 	SET_RSSI_INFO_STBC(h2c_pkt, stbc_en);
579 
580 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
581 }
582 
583 void rtw_fw_send_ra_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si,
584 			 bool reset_ra_mask)
585 {
586 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
587 	bool disable_pt = true;
588 
589 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RA_INFO);
590 
591 	SET_RA_INFO_MACID(h2c_pkt, si->mac_id);
592 	SET_RA_INFO_RATE_ID(h2c_pkt, si->rate_id);
593 	SET_RA_INFO_INIT_RA_LVL(h2c_pkt, si->init_ra_lv);
594 	SET_RA_INFO_SGI_EN(h2c_pkt, si->sgi_enable);
595 	SET_RA_INFO_BW_MODE(h2c_pkt, si->bw_mode);
596 	SET_RA_INFO_LDPC(h2c_pkt, !!si->ldpc_en);
597 	SET_RA_INFO_NO_UPDATE(h2c_pkt, !reset_ra_mask);
598 	SET_RA_INFO_VHT_EN(h2c_pkt, si->vht_enable);
599 	SET_RA_INFO_DIS_PT(h2c_pkt, disable_pt);
600 	SET_RA_INFO_RA_MASK0(h2c_pkt, (si->ra_mask & 0xff));
601 	SET_RA_INFO_RA_MASK1(h2c_pkt, (si->ra_mask & 0xff00) >> 8);
602 	SET_RA_INFO_RA_MASK2(h2c_pkt, (si->ra_mask & 0xff0000) >> 16);
603 	SET_RA_INFO_RA_MASK3(h2c_pkt, (si->ra_mask & 0xff000000) >> 24);
604 
605 	si->init_ra_lv = 0;
606 
607 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
608 }
609 
610 void rtw_fw_media_status_report(struct rtw_dev *rtwdev, u8 mac_id, bool connect)
611 {
612 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
613 
614 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_MEDIA_STATUS_RPT);
615 	MEDIA_STATUS_RPT_SET_OP_MODE(h2c_pkt, connect);
616 	MEDIA_STATUS_RPT_SET_MACID(h2c_pkt, mac_id);
617 
618 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
619 }
620 
621 void rtw_fw_update_wl_phy_info(struct rtw_dev *rtwdev)
622 {
623 	struct rtw_traffic_stats *stats = &rtwdev->stats;
624 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
625 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
626 
627 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WL_PHY_INFO);
628 	SET_WL_PHY_INFO_TX_TP(h2c_pkt, stats->tx_throughput);
629 	SET_WL_PHY_INFO_RX_TP(h2c_pkt, stats->rx_throughput);
630 	SET_WL_PHY_INFO_TX_RATE_DESC(h2c_pkt, dm_info->tx_rate);
631 	SET_WL_PHY_INFO_RX_RATE_DESC(h2c_pkt, dm_info->curr_rx_rate);
632 	SET_WL_PHY_INFO_RX_EVM(h2c_pkt, dm_info->rx_evm_dbm[RF_PATH_A]);
633 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
634 }
635 
636 void rtw_fw_beacon_filter_config(struct rtw_dev *rtwdev, bool connect,
637 				 struct ieee80211_vif *vif)
638 {
639 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
640 	struct ieee80211_sta *sta = ieee80211_find_sta(vif, bss_conf->bssid);
641 	static const u8 rssi_min = 0, rssi_max = 100, rssi_offset = 100;
642 	struct rtw_sta_info *si =
643 		sta ? (struct rtw_sta_info *)sta->drv_priv : NULL;
644 	s32 threshold = bss_conf->cqm_rssi_thold + rssi_offset;
645 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
646 
647 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_BCN_FILTER))
648 		return;
649 
650 	if (!connect) {
651 		SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BCN_FILTER_OFFLOAD_P1);
652 		SET_BCN_FILTER_OFFLOAD_P1_ENABLE(h2c_pkt, connect);
653 		rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
654 
655 		return;
656 	}
657 
658 	if (!si)
659 		return;
660 
661 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BCN_FILTER_OFFLOAD_P0);
662 	ether_addr_copy(&h2c_pkt[1], bss_conf->bssid);
663 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
664 
665 	memset(h2c_pkt, 0, sizeof(h2c_pkt));
666 	threshold = clamp_t(s32, threshold, rssi_min, rssi_max);
667 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BCN_FILTER_OFFLOAD_P1);
668 	SET_BCN_FILTER_OFFLOAD_P1_ENABLE(h2c_pkt, connect);
669 	SET_BCN_FILTER_OFFLOAD_P1_OFFLOAD_MODE(h2c_pkt,
670 					       BCN_FILTER_OFFLOAD_MODE_DEFAULT);
671 	SET_BCN_FILTER_OFFLOAD_P1_THRESHOLD(h2c_pkt, (u8)threshold);
672 	SET_BCN_FILTER_OFFLOAD_P1_BCN_LOSS_CNT(h2c_pkt, BCN_LOSS_CNT);
673 	SET_BCN_FILTER_OFFLOAD_P1_MACID(h2c_pkt, si->mac_id);
674 	SET_BCN_FILTER_OFFLOAD_P1_HYST(h2c_pkt, bss_conf->cqm_rssi_hyst);
675 	SET_BCN_FILTER_OFFLOAD_P1_BCN_INTERVAL(h2c_pkt, bss_conf->beacon_int);
676 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
677 }
678 
679 void rtw_fw_set_pwr_mode(struct rtw_dev *rtwdev)
680 {
681 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
682 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
683 
684 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_SET_PWR_MODE);
685 
686 	SET_PWR_MODE_SET_MODE(h2c_pkt, conf->mode);
687 	SET_PWR_MODE_SET_RLBM(h2c_pkt, conf->rlbm);
688 	SET_PWR_MODE_SET_SMART_PS(h2c_pkt, conf->smart_ps);
689 	SET_PWR_MODE_SET_AWAKE_INTERVAL(h2c_pkt, conf->awake_interval);
690 	SET_PWR_MODE_SET_PORT_ID(h2c_pkt, conf->port_id);
691 	SET_PWR_MODE_SET_PWR_STATE(h2c_pkt, conf->state);
692 
693 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
694 }
695 
696 void rtw_fw_set_keep_alive_cmd(struct rtw_dev *rtwdev, bool enable)
697 {
698 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
699 	struct rtw_fw_wow_keep_alive_para mode = {
700 		.adopt = true,
701 		.pkt_type = KEEP_ALIVE_NULL_PKT,
702 		.period = 5,
703 	};
704 
705 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_KEEP_ALIVE);
706 	SET_KEEP_ALIVE_ENABLE(h2c_pkt, enable);
707 	SET_KEEP_ALIVE_ADOPT(h2c_pkt, mode.adopt);
708 	SET_KEEP_ALIVE_PKT_TYPE(h2c_pkt, mode.pkt_type);
709 	SET_KEEP_ALIVE_CHECK_PERIOD(h2c_pkt, mode.period);
710 
711 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
712 }
713 
714 void rtw_fw_set_disconnect_decision_cmd(struct rtw_dev *rtwdev, bool enable)
715 {
716 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
717 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
718 	struct rtw_fw_wow_disconnect_para mode = {
719 		.adopt = true,
720 		.period = 30,
721 		.retry_count = 5,
722 	};
723 
724 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_DISCONNECT_DECISION);
725 
726 	if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags)) {
727 		SET_DISCONNECT_DECISION_ENABLE(h2c_pkt, enable);
728 		SET_DISCONNECT_DECISION_ADOPT(h2c_pkt, mode.adopt);
729 		SET_DISCONNECT_DECISION_CHECK_PERIOD(h2c_pkt, mode.period);
730 		SET_DISCONNECT_DECISION_TRY_PKT_NUM(h2c_pkt, mode.retry_count);
731 	}
732 
733 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
734 }
735 
736 void rtw_fw_set_wowlan_ctrl_cmd(struct rtw_dev *rtwdev, bool enable)
737 {
738 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
739 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
740 
741 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WOWLAN);
742 
743 	SET_WOWLAN_FUNC_ENABLE(h2c_pkt, enable);
744 	if (rtw_wow_mgd_linked(rtwdev)) {
745 		if (test_bit(RTW_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags))
746 			SET_WOWLAN_MAGIC_PKT_ENABLE(h2c_pkt, enable);
747 		if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags))
748 			SET_WOWLAN_DEAUTH_WAKEUP_ENABLE(h2c_pkt, enable);
749 		if (test_bit(RTW_WOW_FLAG_EN_REKEY_PKT, rtw_wow->flags))
750 			SET_WOWLAN_REKEY_WAKEUP_ENABLE(h2c_pkt, enable);
751 		if (rtw_wow->pattern_cnt)
752 			SET_WOWLAN_PATTERN_MATCH_ENABLE(h2c_pkt, enable);
753 	}
754 
755 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
756 }
757 
758 void rtw_fw_set_aoac_global_info_cmd(struct rtw_dev *rtwdev,
759 				     u8 pairwise_key_enc,
760 				     u8 group_key_enc)
761 {
762 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
763 
764 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_AOAC_GLOBAL_INFO);
765 
766 	SET_AOAC_GLOBAL_INFO_PAIRWISE_ENC_ALG(h2c_pkt, pairwise_key_enc);
767 	SET_AOAC_GLOBAL_INFO_GROUP_ENC_ALG(h2c_pkt, group_key_enc);
768 
769 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
770 }
771 
772 void rtw_fw_set_remote_wake_ctrl_cmd(struct rtw_dev *rtwdev, bool enable)
773 {
774 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
775 
776 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_REMOTE_WAKE_CTRL);
777 
778 	SET_REMOTE_WAKECTRL_ENABLE(h2c_pkt, enable);
779 
780 	if (rtw_wow_no_link(rtwdev))
781 		SET_REMOTE_WAKE_CTRL_NLO_OFFLOAD_EN(h2c_pkt, enable);
782 
783 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
784 }
785 
786 static u8 rtw_get_rsvd_page_location(struct rtw_dev *rtwdev,
787 				     enum rtw_rsvd_packet_type type)
788 {
789 	struct rtw_rsvd_page *rsvd_pkt;
790 	u8 location = 0;
791 
792 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
793 		if (type == rsvd_pkt->type)
794 			location = rsvd_pkt->page;
795 	}
796 
797 	return location;
798 }
799 
800 void rtw_fw_set_nlo_info(struct rtw_dev *rtwdev, bool enable)
801 {
802 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
803 	u8 loc_nlo;
804 
805 	loc_nlo = rtw_get_rsvd_page_location(rtwdev, RSVD_NLO_INFO);
806 
807 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_NLO_INFO);
808 
809 	SET_NLO_FUN_EN(h2c_pkt, enable);
810 	if (enable) {
811 		if (rtw_get_lps_deep_mode(rtwdev) != LPS_DEEP_MODE_NONE)
812 			SET_NLO_PS_32K(h2c_pkt, enable);
813 		SET_NLO_IGNORE_SECURITY(h2c_pkt, enable);
814 		SET_NLO_LOC_NLO_INFO(h2c_pkt, loc_nlo);
815 	}
816 
817 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
818 }
819 
820 void rtw_fw_set_recover_bt_device(struct rtw_dev *rtwdev)
821 {
822 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
823 
824 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RECOVER_BT_DEV);
825 	SET_RECOVER_BT_DEV_EN(h2c_pkt, 1);
826 
827 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
828 }
829 
830 void rtw_fw_set_pg_info(struct rtw_dev *rtwdev)
831 {
832 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
833 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
834 	u8 loc_pg, loc_dpk;
835 
836 	loc_pg = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_INFO);
837 	loc_dpk = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_DPK);
838 
839 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_LPS_PG_INFO);
840 
841 	LPS_PG_INFO_LOC(h2c_pkt, loc_pg);
842 	LPS_PG_DPK_LOC(h2c_pkt, loc_dpk);
843 	LPS_PG_SEC_CAM_EN(h2c_pkt, conf->sec_cam_backup);
844 	LPS_PG_PATTERN_CAM_EN(h2c_pkt, conf->pattern_cam_backup);
845 
846 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
847 }
848 
849 static u8 rtw_get_rsvd_page_probe_req_location(struct rtw_dev *rtwdev,
850 					       struct cfg80211_ssid *ssid)
851 {
852 	struct rtw_rsvd_page *rsvd_pkt;
853 	u8 location = 0;
854 
855 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
856 		if (rsvd_pkt->type != RSVD_PROBE_REQ)
857 			continue;
858 		if ((!ssid && !rsvd_pkt->ssid) ||
859 		    rtw_ssid_equal(rsvd_pkt->ssid, ssid))
860 			location = rsvd_pkt->page;
861 	}
862 
863 	return location;
864 }
865 
866 static u16 rtw_get_rsvd_page_probe_req_size(struct rtw_dev *rtwdev,
867 					    struct cfg80211_ssid *ssid)
868 {
869 	struct rtw_rsvd_page *rsvd_pkt;
870 	u16 size = 0;
871 
872 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
873 		if (rsvd_pkt->type != RSVD_PROBE_REQ)
874 			continue;
875 		if ((!ssid && !rsvd_pkt->ssid) ||
876 		    rtw_ssid_equal(rsvd_pkt->ssid, ssid))
877 			size = rsvd_pkt->probe_req_size;
878 	}
879 
880 	return size;
881 }
882 
883 void rtw_send_rsvd_page_h2c(struct rtw_dev *rtwdev)
884 {
885 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
886 	u8 location = 0;
887 
888 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSVD_PAGE);
889 
890 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_PROBE_RESP);
891 	*(h2c_pkt + 1) = location;
892 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PROBE_RESP loc: %d\n", location);
893 
894 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_PS_POLL);
895 	*(h2c_pkt + 2) = location;
896 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PS_POLL loc: %d\n", location);
897 
898 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_NULL);
899 	*(h2c_pkt + 3) = location;
900 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_NULL loc: %d\n", location);
901 
902 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_QOS_NULL);
903 	*(h2c_pkt + 4) = location;
904 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_QOS_NULL loc: %d\n", location);
905 
906 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
907 }
908 
909 static struct sk_buff *rtw_nlo_info_get(struct ieee80211_hw *hw)
910 {
911 	struct rtw_dev *rtwdev = hw->priv;
912 	const struct rtw_chip_info *chip = rtwdev->chip;
913 	struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req;
914 	struct rtw_nlo_info_hdr *nlo_hdr;
915 	struct cfg80211_ssid *ssid;
916 	struct sk_buff *skb;
917 	u8 *pos, loc;
918 	u32 size;
919 	int i;
920 
921 	if (!pno_req->inited || !pno_req->match_set_cnt)
922 		return NULL;
923 
924 	size = sizeof(struct rtw_nlo_info_hdr) + pno_req->match_set_cnt *
925 		      IEEE80211_MAX_SSID_LEN + chip->tx_pkt_desc_sz;
926 
927 	skb = alloc_skb(size, GFP_KERNEL);
928 	if (!skb)
929 		return NULL;
930 
931 	skb_reserve(skb, chip->tx_pkt_desc_sz);
932 
933 	nlo_hdr = skb_put_zero(skb, sizeof(struct rtw_nlo_info_hdr));
934 
935 	nlo_hdr->nlo_count = pno_req->match_set_cnt;
936 	nlo_hdr->hidden_ap_count = pno_req->match_set_cnt;
937 
938 	/* pattern check for firmware */
939 	memset(nlo_hdr->pattern_check, 0xA5, FW_NLO_INFO_CHECK_SIZE);
940 
941 	for (i = 0; i < pno_req->match_set_cnt; i++)
942 		nlo_hdr->ssid_len[i] = pno_req->match_sets[i].ssid.ssid_len;
943 
944 	for (i = 0; i < pno_req->match_set_cnt; i++) {
945 		ssid = &pno_req->match_sets[i].ssid;
946 		loc  = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid);
947 		if (!loc) {
948 			rtw_err(rtwdev, "failed to get probe req rsvd loc\n");
949 			kfree_skb(skb);
950 			return NULL;
951 		}
952 		nlo_hdr->location[i] = loc;
953 	}
954 
955 	for (i = 0; i < pno_req->match_set_cnt; i++) {
956 		pos = skb_put_zero(skb, IEEE80211_MAX_SSID_LEN);
957 		memcpy(pos, pno_req->match_sets[i].ssid.ssid,
958 		       pno_req->match_sets[i].ssid.ssid_len);
959 	}
960 
961 	return skb;
962 }
963 
964 static struct sk_buff *rtw_cs_channel_info_get(struct ieee80211_hw *hw)
965 {
966 	struct rtw_dev *rtwdev = hw->priv;
967 	const struct rtw_chip_info *chip = rtwdev->chip;
968 	struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req;
969 	struct ieee80211_channel *channels = pno_req->channels;
970 	struct sk_buff *skb;
971 	int count =  pno_req->channel_cnt;
972 	u8 *pos;
973 	int i = 0;
974 
975 	skb = alloc_skb(4 * count + chip->tx_pkt_desc_sz, GFP_KERNEL);
976 	if (!skb)
977 		return NULL;
978 
979 	skb_reserve(skb, chip->tx_pkt_desc_sz);
980 
981 	for (i = 0; i < count; i++) {
982 		pos = skb_put_zero(skb, 4);
983 
984 		CHSW_INFO_SET_CH(pos, channels[i].hw_value);
985 
986 		if (channels[i].flags & IEEE80211_CHAN_RADAR)
987 			CHSW_INFO_SET_ACTION_ID(pos, 0);
988 		else
989 			CHSW_INFO_SET_ACTION_ID(pos, 1);
990 		CHSW_INFO_SET_TIMEOUT(pos, 1);
991 		CHSW_INFO_SET_PRI_CH_IDX(pos, 1);
992 		CHSW_INFO_SET_BW(pos, 0);
993 	}
994 
995 	return skb;
996 }
997 
998 static struct sk_buff *rtw_lps_pg_dpk_get(struct ieee80211_hw *hw)
999 {
1000 	struct rtw_dev *rtwdev = hw->priv;
1001 	const struct rtw_chip_info *chip = rtwdev->chip;
1002 	struct rtw_dpk_info *dpk_info = &rtwdev->dm_info.dpk_info;
1003 	struct rtw_lps_pg_dpk_hdr *dpk_hdr;
1004 	struct sk_buff *skb;
1005 	u32 size;
1006 
1007 	size = chip->tx_pkt_desc_sz + sizeof(*dpk_hdr);
1008 	skb = alloc_skb(size, GFP_KERNEL);
1009 	if (!skb)
1010 		return NULL;
1011 
1012 	skb_reserve(skb, chip->tx_pkt_desc_sz);
1013 	dpk_hdr = skb_put_zero(skb, sizeof(*dpk_hdr));
1014 	dpk_hdr->dpk_ch = dpk_info->dpk_ch;
1015 	dpk_hdr->dpk_path_ok = dpk_info->dpk_path_ok[0];
1016 	memcpy(dpk_hdr->dpk_txagc, dpk_info->dpk_txagc, 2);
1017 	memcpy(dpk_hdr->dpk_gs, dpk_info->dpk_gs, 4);
1018 	memcpy(dpk_hdr->coef, dpk_info->coef, 160);
1019 
1020 	return skb;
1021 }
1022 
1023 static struct sk_buff *rtw_lps_pg_info_get(struct ieee80211_hw *hw)
1024 {
1025 	struct rtw_dev *rtwdev = hw->priv;
1026 	const struct rtw_chip_info *chip = rtwdev->chip;
1027 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
1028 	struct rtw_lps_pg_info_hdr *pg_info_hdr;
1029 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
1030 	struct sk_buff *skb;
1031 	u32 size;
1032 
1033 	size = chip->tx_pkt_desc_sz + sizeof(*pg_info_hdr);
1034 	skb = alloc_skb(size, GFP_KERNEL);
1035 	if (!skb)
1036 		return NULL;
1037 
1038 	skb_reserve(skb, chip->tx_pkt_desc_sz);
1039 	pg_info_hdr = skb_put_zero(skb, sizeof(*pg_info_hdr));
1040 	pg_info_hdr->tx_bu_page_count = rtwdev->fifo.rsvd_drv_pg_num;
1041 	pg_info_hdr->macid = find_first_bit(rtwdev->mac_id_map, RTW_MAX_MAC_ID_NUM);
1042 	pg_info_hdr->sec_cam_count =
1043 		rtw_sec_cam_pg_backup(rtwdev, pg_info_hdr->sec_cam);
1044 	pg_info_hdr->pattern_count = rtw_wow->pattern_cnt;
1045 
1046 	conf->sec_cam_backup = pg_info_hdr->sec_cam_count != 0;
1047 	conf->pattern_cam_backup = rtw_wow->pattern_cnt != 0;
1048 
1049 	return skb;
1050 }
1051 
1052 static struct sk_buff *rtw_get_rsvd_page_skb(struct ieee80211_hw *hw,
1053 					     struct rtw_rsvd_page *rsvd_pkt)
1054 {
1055 	struct ieee80211_vif *vif;
1056 	struct rtw_vif *rtwvif;
1057 	struct sk_buff *skb_new;
1058 	struct cfg80211_ssid *ssid;
1059 	u16 tim_offset = 0;
1060 
1061 	if (rsvd_pkt->type == RSVD_DUMMY) {
1062 		skb_new = alloc_skb(1, GFP_KERNEL);
1063 		if (!skb_new)
1064 			return NULL;
1065 
1066 		skb_put(skb_new, 1);
1067 		return skb_new;
1068 	}
1069 
1070 	rtwvif = rsvd_pkt->rtwvif;
1071 	if (!rtwvif)
1072 		return NULL;
1073 
1074 	vif = rtwvif_to_vif(rtwvif);
1075 
1076 	switch (rsvd_pkt->type) {
1077 	case RSVD_BEACON:
1078 		skb_new = ieee80211_beacon_get_tim(hw, vif, &tim_offset, NULL, 0);
1079 		rsvd_pkt->tim_offset = tim_offset;
1080 		break;
1081 	case RSVD_PS_POLL:
1082 		skb_new = ieee80211_pspoll_get(hw, vif);
1083 		break;
1084 	case RSVD_PROBE_RESP:
1085 		skb_new = ieee80211_proberesp_get(hw, vif);
1086 		break;
1087 	case RSVD_NULL:
1088 		skb_new = ieee80211_nullfunc_get(hw, vif, -1, false);
1089 		break;
1090 	case RSVD_QOS_NULL:
1091 		skb_new = ieee80211_nullfunc_get(hw, vif, -1, true);
1092 		break;
1093 	case RSVD_LPS_PG_DPK:
1094 		skb_new = rtw_lps_pg_dpk_get(hw);
1095 		break;
1096 	case RSVD_LPS_PG_INFO:
1097 		skb_new = rtw_lps_pg_info_get(hw);
1098 		break;
1099 	case RSVD_PROBE_REQ:
1100 		ssid = (struct cfg80211_ssid *)rsvd_pkt->ssid;
1101 		if (ssid)
1102 			skb_new = ieee80211_probereq_get(hw, vif->addr,
1103 							 ssid->ssid,
1104 							 ssid->ssid_len, 0);
1105 		else
1106 			skb_new = ieee80211_probereq_get(hw, vif->addr, NULL, 0, 0);
1107 		if (skb_new)
1108 			rsvd_pkt->probe_req_size = (u16)skb_new->len;
1109 		break;
1110 	case RSVD_NLO_INFO:
1111 		skb_new = rtw_nlo_info_get(hw);
1112 		break;
1113 	case RSVD_CH_INFO:
1114 		skb_new = rtw_cs_channel_info_get(hw);
1115 		break;
1116 	default:
1117 		return NULL;
1118 	}
1119 
1120 	if (!skb_new)
1121 		return NULL;
1122 
1123 	return skb_new;
1124 }
1125 
1126 static void rtw_fill_rsvd_page_desc(struct rtw_dev *rtwdev, struct sk_buff *skb,
1127 				    enum rtw_rsvd_packet_type type)
1128 {
1129 	struct rtw_tx_pkt_info pkt_info = {0};
1130 	const struct rtw_chip_info *chip = rtwdev->chip;
1131 	u8 *pkt_desc;
1132 
1133 	rtw_tx_rsvd_page_pkt_info_update(rtwdev, &pkt_info, skb, type);
1134 	pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz);
1135 	memset(pkt_desc, 0, chip->tx_pkt_desc_sz);
1136 	rtw_tx_fill_tx_desc(&pkt_info, skb);
1137 }
1138 
1139 static inline u8 rtw_len_to_page(unsigned int len, u8 page_size)
1140 {
1141 	return DIV_ROUND_UP(len, page_size);
1142 }
1143 
1144 static void rtw_rsvd_page_list_to_buf(struct rtw_dev *rtwdev, u8 page_size,
1145 				      u8 page_margin, u32 page, u8 *buf,
1146 				      struct rtw_rsvd_page *rsvd_pkt)
1147 {
1148 	struct sk_buff *skb = rsvd_pkt->skb;
1149 
1150 	if (page >= 1)
1151 		memcpy(buf + page_margin + page_size * (page - 1),
1152 		       skb->data, skb->len);
1153 	else
1154 		memcpy(buf, skb->data, skb->len);
1155 }
1156 
1157 static struct rtw_rsvd_page *rtw_alloc_rsvd_page(struct rtw_dev *rtwdev,
1158 						 enum rtw_rsvd_packet_type type,
1159 						 bool txdesc)
1160 {
1161 	struct rtw_rsvd_page *rsvd_pkt = NULL;
1162 
1163 	rsvd_pkt = kzalloc(sizeof(*rsvd_pkt), GFP_KERNEL);
1164 
1165 	if (!rsvd_pkt)
1166 		return NULL;
1167 
1168 	INIT_LIST_HEAD(&rsvd_pkt->vif_list);
1169 	INIT_LIST_HEAD(&rsvd_pkt->build_list);
1170 	rsvd_pkt->type = type;
1171 	rsvd_pkt->add_txdesc = txdesc;
1172 
1173 	return rsvd_pkt;
1174 }
1175 
1176 static void rtw_insert_rsvd_page(struct rtw_dev *rtwdev,
1177 				 struct rtw_vif *rtwvif,
1178 				 struct rtw_rsvd_page *rsvd_pkt)
1179 {
1180 	lockdep_assert_held(&rtwdev->mutex);
1181 
1182 	list_add_tail(&rsvd_pkt->vif_list, &rtwvif->rsvd_page_list);
1183 }
1184 
1185 static void rtw_add_rsvd_page(struct rtw_dev *rtwdev,
1186 			      struct rtw_vif *rtwvif,
1187 			      enum rtw_rsvd_packet_type type,
1188 			      bool txdesc)
1189 {
1190 	struct rtw_rsvd_page *rsvd_pkt;
1191 
1192 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, type, txdesc);
1193 	if (!rsvd_pkt) {
1194 		rtw_err(rtwdev, "failed to alloc rsvd page %d\n", type);
1195 		return;
1196 	}
1197 
1198 	rsvd_pkt->rtwvif = rtwvif;
1199 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1200 }
1201 
1202 static void rtw_add_rsvd_page_probe_req(struct rtw_dev *rtwdev,
1203 					struct rtw_vif *rtwvif,
1204 					struct cfg80211_ssid *ssid)
1205 {
1206 	struct rtw_rsvd_page *rsvd_pkt;
1207 
1208 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_PROBE_REQ, true);
1209 	if (!rsvd_pkt) {
1210 		rtw_err(rtwdev, "failed to alloc probe req rsvd page\n");
1211 		return;
1212 	}
1213 
1214 	rsvd_pkt->rtwvif = rtwvif;
1215 	rsvd_pkt->ssid = ssid;
1216 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1217 }
1218 
1219 void rtw_remove_rsvd_page(struct rtw_dev *rtwdev,
1220 			  struct rtw_vif *rtwvif)
1221 {
1222 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1223 
1224 	lockdep_assert_held(&rtwdev->mutex);
1225 
1226 	/* remove all of the rsvd pages for vif */
1227 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwvif->rsvd_page_list,
1228 				 vif_list) {
1229 		list_del(&rsvd_pkt->vif_list);
1230 		if (!list_empty(&rsvd_pkt->build_list))
1231 			list_del(&rsvd_pkt->build_list);
1232 		kfree(rsvd_pkt);
1233 	}
1234 }
1235 
1236 void rtw_add_rsvd_page_bcn(struct rtw_dev *rtwdev,
1237 			   struct rtw_vif *rtwvif)
1238 {
1239 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1240 
1241 	if (vif->type != NL80211_IFTYPE_AP &&
1242 	    vif->type != NL80211_IFTYPE_ADHOC &&
1243 	    vif->type != NL80211_IFTYPE_MESH_POINT) {
1244 		rtw_warn(rtwdev, "Cannot add beacon rsvd page for %d\n",
1245 			 vif->type);
1246 		return;
1247 	}
1248 
1249 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_BEACON, false);
1250 }
1251 
1252 void rtw_add_rsvd_page_pno(struct rtw_dev *rtwdev,
1253 			   struct rtw_vif *rtwvif)
1254 {
1255 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1256 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
1257 	struct rtw_pno_request *rtw_pno_req = &rtw_wow->pno_req;
1258 	struct cfg80211_ssid *ssid;
1259 	int i;
1260 
1261 	if (vif->type != NL80211_IFTYPE_STATION) {
1262 		rtw_warn(rtwdev, "Cannot add PNO rsvd page for %d\n",
1263 			 vif->type);
1264 		return;
1265 	}
1266 
1267 	for (i = 0 ; i < rtw_pno_req->match_set_cnt; i++) {
1268 		ssid = &rtw_pno_req->match_sets[i].ssid;
1269 		rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, ssid);
1270 	}
1271 
1272 	rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, NULL);
1273 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NLO_INFO, false);
1274 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_CH_INFO, true);
1275 }
1276 
1277 void rtw_add_rsvd_page_sta(struct rtw_dev *rtwdev,
1278 			   struct rtw_vif *rtwvif)
1279 {
1280 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1281 
1282 	if (vif->type != NL80211_IFTYPE_STATION) {
1283 		rtw_warn(rtwdev, "Cannot add sta rsvd page for %d\n",
1284 			 vif->type);
1285 		return;
1286 	}
1287 
1288 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_PS_POLL, true);
1289 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_QOS_NULL, true);
1290 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NULL, true);
1291 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_DPK, true);
1292 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_INFO, true);
1293 }
1294 
1295 int rtw_fw_write_data_rsvd_page(struct rtw_dev *rtwdev, u16 pg_addr,
1296 				u8 *buf, u32 size)
1297 {
1298 	u8 bckp[2];
1299 	u8 val;
1300 	u16 rsvd_pg_head;
1301 	u32 bcn_valid_addr;
1302 	u32 bcn_valid_mask;
1303 	int ret;
1304 
1305 	lockdep_assert_held(&rtwdev->mutex);
1306 
1307 	if (!size)
1308 		return -EINVAL;
1309 
1310 	if (rtw_chip_wcpu_11n(rtwdev)) {
1311 		rtw_write32_set(rtwdev, REG_DWBCN0_CTRL, BIT_BCN_VALID);
1312 	} else {
1313 		pg_addr &= BIT_MASK_BCN_HEAD_1_V1;
1314 		pg_addr |= BIT_BCN_VALID_V1;
1315 		rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2, pg_addr);
1316 	}
1317 
1318 	val = rtw_read8(rtwdev, REG_CR + 1);
1319 	bckp[0] = val;
1320 	val |= BIT_ENSWBCN >> 8;
1321 	rtw_write8(rtwdev, REG_CR + 1, val);
1322 
1323 	val = rtw_read8(rtwdev, REG_FWHW_TXQ_CTRL + 2);
1324 	bckp[1] = val;
1325 	val &= ~(BIT_EN_BCNQ_DL >> 16);
1326 	rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, val);
1327 
1328 	ret = rtw_hci_write_data_rsvd_page(rtwdev, buf, size);
1329 	if (ret) {
1330 		rtw_err(rtwdev, "failed to write data to rsvd page\n");
1331 		goto restore;
1332 	}
1333 
1334 	if (rtw_chip_wcpu_11n(rtwdev)) {
1335 		bcn_valid_addr = REG_DWBCN0_CTRL;
1336 		bcn_valid_mask = BIT_BCN_VALID;
1337 	} else {
1338 		bcn_valid_addr = REG_FIFOPAGE_CTRL_2;
1339 		bcn_valid_mask = BIT_BCN_VALID_V1;
1340 	}
1341 
1342 	if (!check_hw_ready(rtwdev, bcn_valid_addr, bcn_valid_mask, 1)) {
1343 		rtw_err(rtwdev, "error beacon valid\n");
1344 		ret = -EBUSY;
1345 	}
1346 
1347 restore:
1348 	rsvd_pg_head = rtwdev->fifo.rsvd_boundary;
1349 	rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2,
1350 		    rsvd_pg_head | BIT_BCN_VALID_V1);
1351 	rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, bckp[1]);
1352 	rtw_write8(rtwdev, REG_CR + 1, bckp[0]);
1353 
1354 	return ret;
1355 }
1356 
1357 static int rtw_download_drv_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, u32 size)
1358 {
1359 	u32 pg_size;
1360 	u32 pg_num = 0;
1361 	u16 pg_addr = 0;
1362 
1363 	pg_size = rtwdev->chip->page_size;
1364 	pg_num = size / pg_size + ((size & (pg_size - 1)) ? 1 : 0);
1365 	if (pg_num > rtwdev->fifo.rsvd_drv_pg_num)
1366 		return -ENOMEM;
1367 
1368 	pg_addr = rtwdev->fifo.rsvd_drv_addr;
1369 
1370 	return rtw_fw_write_data_rsvd_page(rtwdev, pg_addr, buf, size);
1371 }
1372 
1373 static void __rtw_build_rsvd_page_reset(struct rtw_dev *rtwdev)
1374 {
1375 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1376 
1377 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwdev->rsvd_page_list,
1378 				 build_list) {
1379 		list_del_init(&rsvd_pkt->build_list);
1380 
1381 		/* Don't free except for the dummy rsvd page,
1382 		 * others will be freed when removing vif
1383 		 */
1384 		if (rsvd_pkt->type == RSVD_DUMMY)
1385 			kfree(rsvd_pkt);
1386 	}
1387 }
1388 
1389 static void rtw_build_rsvd_page_iter(void *data, u8 *mac,
1390 				     struct ieee80211_vif *vif)
1391 {
1392 	struct rtw_dev *rtwdev = data;
1393 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
1394 	struct rtw_rsvd_page *rsvd_pkt;
1395 
1396 	list_for_each_entry(rsvd_pkt, &rtwvif->rsvd_page_list, vif_list) {
1397 		if (rsvd_pkt->type == RSVD_BEACON)
1398 			list_add(&rsvd_pkt->build_list,
1399 				 &rtwdev->rsvd_page_list);
1400 		else
1401 			list_add_tail(&rsvd_pkt->build_list,
1402 				      &rtwdev->rsvd_page_list);
1403 	}
1404 }
1405 
1406 static int  __rtw_build_rsvd_page_from_vifs(struct rtw_dev *rtwdev)
1407 {
1408 	struct rtw_rsvd_page *rsvd_pkt;
1409 
1410 	__rtw_build_rsvd_page_reset(rtwdev);
1411 
1412 	/* gather rsvd page from vifs */
1413 	rtw_iterate_vifs_atomic(rtwdev, rtw_build_rsvd_page_iter, rtwdev);
1414 
1415 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
1416 					    struct rtw_rsvd_page, build_list);
1417 	if (!rsvd_pkt) {
1418 		WARN(1, "Should not have an empty reserved page\n");
1419 		return -EINVAL;
1420 	}
1421 
1422 	/* the first rsvd should be beacon, otherwise add a dummy one */
1423 	if (rsvd_pkt->type != RSVD_BEACON) {
1424 		struct rtw_rsvd_page *dummy_pkt;
1425 
1426 		dummy_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_DUMMY, false);
1427 		if (!dummy_pkt) {
1428 			rtw_err(rtwdev, "failed to alloc dummy rsvd page\n");
1429 			return -ENOMEM;
1430 		}
1431 
1432 		list_add(&dummy_pkt->build_list, &rtwdev->rsvd_page_list);
1433 	}
1434 
1435 	return 0;
1436 }
1437 
1438 static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size)
1439 {
1440 	struct ieee80211_hw *hw = rtwdev->hw;
1441 	const struct rtw_chip_info *chip = rtwdev->chip;
1442 	struct sk_buff *iter;
1443 	struct rtw_rsvd_page *rsvd_pkt;
1444 	u32 page = 0;
1445 	u8 total_page = 0;
1446 	u8 page_size, page_margin, tx_desc_sz;
1447 	u8 *buf;
1448 	int ret;
1449 
1450 	page_size = chip->page_size;
1451 	tx_desc_sz = chip->tx_pkt_desc_sz;
1452 	page_margin = page_size - tx_desc_sz;
1453 
1454 	ret = __rtw_build_rsvd_page_from_vifs(rtwdev);
1455 	if (ret) {
1456 		rtw_err(rtwdev,
1457 			"failed to build rsvd page from vifs, ret %d\n", ret);
1458 		return NULL;
1459 	}
1460 
1461 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1462 		iter = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1463 		if (!iter) {
1464 			rtw_err(rtwdev, "failed to build rsvd packet\n");
1465 			goto release_skb;
1466 		}
1467 
1468 		/* Fill the tx_desc for the rsvd pkt that requires one.
1469 		 * And iter->len will be added with size of tx_desc_sz.
1470 		 */
1471 		if (rsvd_pkt->add_txdesc)
1472 			rtw_fill_rsvd_page_desc(rtwdev, iter, rsvd_pkt->type);
1473 
1474 		rsvd_pkt->skb = iter;
1475 		rsvd_pkt->page = total_page;
1476 
1477 		/* Reserved page is downloaded via TX path, and TX path will
1478 		 * generate a tx_desc at the header to describe length of
1479 		 * the buffer. If we are not counting page numbers with the
1480 		 * size of tx_desc added at the first rsvd_pkt (usually a
1481 		 * beacon, firmware default refer to the first page as the
1482 		 * content of beacon), we could generate a buffer which size
1483 		 * is smaller than the actual size of the whole rsvd_page
1484 		 */
1485 		if (total_page == 0) {
1486 			if (rsvd_pkt->type != RSVD_BEACON &&
1487 			    rsvd_pkt->type != RSVD_DUMMY) {
1488 				rtw_err(rtwdev, "first page should be a beacon\n");
1489 				goto release_skb;
1490 			}
1491 			total_page += rtw_len_to_page(iter->len + tx_desc_sz,
1492 						      page_size);
1493 		} else {
1494 			total_page += rtw_len_to_page(iter->len, page_size);
1495 		}
1496 	}
1497 
1498 	if (total_page > rtwdev->fifo.rsvd_drv_pg_num) {
1499 		rtw_err(rtwdev, "rsvd page over size: %d\n", total_page);
1500 		goto release_skb;
1501 	}
1502 
1503 	*size = (total_page - 1) * page_size + page_margin;
1504 	buf = kzalloc(*size, GFP_KERNEL);
1505 	if (!buf)
1506 		goto release_skb;
1507 
1508 	/* Copy the content of each rsvd_pkt to the buf, and they should
1509 	 * be aligned to the pages.
1510 	 *
1511 	 * Note that the first rsvd_pkt is a beacon no matter what vif->type.
1512 	 * And that rsvd_pkt does not require tx_desc because when it goes
1513 	 * through TX path, the TX path will generate one for it.
1514 	 */
1515 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1516 		rtw_rsvd_page_list_to_buf(rtwdev, page_size, page_margin,
1517 					  page, buf, rsvd_pkt);
1518 		if (page == 0)
1519 			page += rtw_len_to_page(rsvd_pkt->skb->len +
1520 						tx_desc_sz, page_size);
1521 		else
1522 			page += rtw_len_to_page(rsvd_pkt->skb->len, page_size);
1523 
1524 		kfree_skb(rsvd_pkt->skb);
1525 		rsvd_pkt->skb = NULL;
1526 	}
1527 
1528 	return buf;
1529 
1530 release_skb:
1531 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1532 		kfree_skb(rsvd_pkt->skb);
1533 		rsvd_pkt->skb = NULL;
1534 	}
1535 
1536 	return NULL;
1537 }
1538 
1539 static int rtw_download_beacon(struct rtw_dev *rtwdev)
1540 {
1541 	struct ieee80211_hw *hw = rtwdev->hw;
1542 	struct rtw_rsvd_page *rsvd_pkt;
1543 	struct sk_buff *skb;
1544 	int ret = 0;
1545 
1546 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
1547 					    struct rtw_rsvd_page, build_list);
1548 	if (!rsvd_pkt) {
1549 		rtw_err(rtwdev, "failed to get rsvd page from build list\n");
1550 		return -ENOENT;
1551 	}
1552 
1553 	if (rsvd_pkt->type != RSVD_BEACON &&
1554 	    rsvd_pkt->type != RSVD_DUMMY) {
1555 		rtw_err(rtwdev, "invalid rsvd page type %d, should be beacon or dummy\n",
1556 			rsvd_pkt->type);
1557 		return -EINVAL;
1558 	}
1559 
1560 	skb = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1561 	if (!skb) {
1562 		rtw_err(rtwdev, "failed to get beacon skb\n");
1563 		return -ENOMEM;
1564 	}
1565 
1566 	ret = rtw_download_drv_rsvd_page(rtwdev, skb->data, skb->len);
1567 	if (ret)
1568 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1569 
1570 	dev_kfree_skb(skb);
1571 
1572 	return ret;
1573 }
1574 
1575 int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev)
1576 {
1577 	u8 *buf;
1578 	u32 size;
1579 	int ret;
1580 
1581 	buf = rtw_build_rsvd_page(rtwdev, &size);
1582 	if (!buf) {
1583 		rtw_err(rtwdev, "failed to build rsvd page pkt\n");
1584 		return -ENOMEM;
1585 	}
1586 
1587 	ret = rtw_download_drv_rsvd_page(rtwdev, buf, size);
1588 	if (ret) {
1589 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1590 		goto free;
1591 	}
1592 
1593 	/* The last thing is to download the *ONLY* beacon again, because
1594 	 * the previous tx_desc is to describe the total rsvd page. Download
1595 	 * the beacon again to replace the TX desc header, and we will get
1596 	 * a correct tx_desc for the beacon in the rsvd page.
1597 	 */
1598 	ret = rtw_download_beacon(rtwdev);
1599 	if (ret) {
1600 		rtw_err(rtwdev, "failed to download beacon\n");
1601 		goto free;
1602 	}
1603 
1604 free:
1605 	kfree(buf);
1606 
1607 	return ret;
1608 }
1609 
1610 void rtw_fw_update_beacon_work(struct work_struct *work)
1611 {
1612 	struct rtw_dev *rtwdev = container_of(work, struct rtw_dev,
1613 					      update_beacon_work);
1614 
1615 	mutex_lock(&rtwdev->mutex);
1616 	rtw_fw_download_rsvd_page(rtwdev);
1617 	mutex_unlock(&rtwdev->mutex);
1618 }
1619 
1620 static void rtw_fw_read_fifo_page(struct rtw_dev *rtwdev, u32 offset, u32 size,
1621 				  u32 *buf, u32 residue, u16 start_pg)
1622 {
1623 	u32 i;
1624 	u16 idx = 0;
1625 	u16 ctl;
1626 
1627 	ctl = rtw_read16(rtwdev, REG_PKTBUF_DBG_CTRL) & 0xf000;
1628 	/* disable rx clock gate */
1629 	rtw_write32_set(rtwdev, REG_RCR, BIT_DISGCLK);
1630 
1631 	do {
1632 		rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, start_pg | ctl);
1633 
1634 		for (i = FIFO_DUMP_ADDR + residue;
1635 		     i < FIFO_DUMP_ADDR + FIFO_PAGE_SIZE; i += 4) {
1636 			buf[idx++] = rtw_read32(rtwdev, i);
1637 			size -= 4;
1638 			if (size == 0)
1639 				goto out;
1640 		}
1641 
1642 		residue = 0;
1643 		start_pg++;
1644 	} while (size);
1645 
1646 out:
1647 	rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, ctl);
1648 	/* restore rx clock gate */
1649 	rtw_write32_clr(rtwdev, REG_RCR, BIT_DISGCLK);
1650 }
1651 
1652 static void rtw_fw_read_fifo(struct rtw_dev *rtwdev, enum rtw_fw_fifo_sel sel,
1653 			     u32 offset, u32 size, u32 *buf)
1654 {
1655 	const struct rtw_chip_info *chip = rtwdev->chip;
1656 	u32 start_pg, residue;
1657 
1658 	if (sel >= RTW_FW_FIFO_MAX) {
1659 		rtw_dbg(rtwdev, RTW_DBG_FW, "wrong fw fifo sel\n");
1660 		return;
1661 	}
1662 	if (sel == RTW_FW_FIFO_SEL_RSVD_PAGE)
1663 		offset += rtwdev->fifo.rsvd_boundary << TX_PAGE_SIZE_SHIFT;
1664 	residue = offset & (FIFO_PAGE_SIZE - 1);
1665 	start_pg = (offset >> FIFO_PAGE_SIZE_SHIFT) + chip->fw_fifo_addr[sel];
1666 
1667 	rtw_fw_read_fifo_page(rtwdev, offset, size, buf, residue, start_pg);
1668 }
1669 
1670 static bool rtw_fw_dump_check_size(struct rtw_dev *rtwdev,
1671 				   enum rtw_fw_fifo_sel sel,
1672 				   u32 start_addr, u32 size)
1673 {
1674 	switch (sel) {
1675 	case RTW_FW_FIFO_SEL_TX:
1676 	case RTW_FW_FIFO_SEL_RX:
1677 		if ((start_addr + size) > rtwdev->chip->fw_fifo_addr[sel])
1678 			return false;
1679 		fallthrough;
1680 	default:
1681 		return true;
1682 	}
1683 }
1684 
1685 int rtw_fw_dump_fifo(struct rtw_dev *rtwdev, u8 fifo_sel, u32 addr, u32 size,
1686 		     u32 *buffer)
1687 {
1688 	if (!rtwdev->chip->fw_fifo_addr[0]) {
1689 		rtw_dbg(rtwdev, RTW_DBG_FW, "chip not support dump fw fifo\n");
1690 		return -ENOTSUPP;
1691 	}
1692 
1693 	if (size == 0 || !buffer)
1694 		return -EINVAL;
1695 
1696 	if (size & 0x3) {
1697 		rtw_dbg(rtwdev, RTW_DBG_FW, "not 4byte alignment\n");
1698 		return -EINVAL;
1699 	}
1700 
1701 	if (!rtw_fw_dump_check_size(rtwdev, fifo_sel, addr, size)) {
1702 		rtw_dbg(rtwdev, RTW_DBG_FW, "fw fifo dump size overflow\n");
1703 		return -EINVAL;
1704 	}
1705 
1706 	rtw_fw_read_fifo(rtwdev, fifo_sel, addr, size, buffer);
1707 
1708 	return 0;
1709 }
1710 
1711 static void __rtw_fw_update_pkt(struct rtw_dev *rtwdev, u8 pkt_id, u16 size,
1712 				u8 location)
1713 {
1714 	const struct rtw_chip_info *chip = rtwdev->chip;
1715 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1716 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_UPDATE_PKT_LEN;
1717 
1718 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_UPDATE_PKT);
1719 
1720 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1721 	UPDATE_PKT_SET_PKT_ID(h2c_pkt, pkt_id);
1722 	UPDATE_PKT_SET_LOCATION(h2c_pkt, location);
1723 
1724 	/* include txdesc size */
1725 	size += chip->tx_pkt_desc_sz;
1726 	UPDATE_PKT_SET_SIZE(h2c_pkt, size);
1727 
1728 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1729 }
1730 
1731 void rtw_fw_update_pkt_probe_req(struct rtw_dev *rtwdev,
1732 				 struct cfg80211_ssid *ssid)
1733 {
1734 	u8 loc;
1735 	u16 size;
1736 
1737 	loc = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid);
1738 	if (!loc) {
1739 		rtw_err(rtwdev, "failed to get probe_req rsvd loc\n");
1740 		return;
1741 	}
1742 
1743 	size = rtw_get_rsvd_page_probe_req_size(rtwdev, ssid);
1744 	if (!size) {
1745 		rtw_err(rtwdev, "failed to get probe_req rsvd size\n");
1746 		return;
1747 	}
1748 
1749 	__rtw_fw_update_pkt(rtwdev, RTW_PACKET_PROBE_REQ, size, loc);
1750 }
1751 
1752 void rtw_fw_channel_switch(struct rtw_dev *rtwdev, bool enable)
1753 {
1754 	struct rtw_pno_request *rtw_pno_req = &rtwdev->wow.pno_req;
1755 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1756 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_CH_SWITCH_LEN;
1757 	u8 loc_ch_info;
1758 	const struct rtw_ch_switch_option cs_option = {
1759 		.dest_ch_en = 1,
1760 		.dest_ch = 1,
1761 		.periodic_option = 2,
1762 		.normal_period = 5,
1763 		.normal_period_sel = 0,
1764 		.normal_cycle = 10,
1765 		.slow_period = 1,
1766 		.slow_period_sel = 1,
1767 	};
1768 
1769 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_CH_SWITCH);
1770 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1771 
1772 	CH_SWITCH_SET_START(h2c_pkt, enable);
1773 	CH_SWITCH_SET_DEST_CH_EN(h2c_pkt, cs_option.dest_ch_en);
1774 	CH_SWITCH_SET_DEST_CH(h2c_pkt, cs_option.dest_ch);
1775 	CH_SWITCH_SET_NORMAL_PERIOD(h2c_pkt, cs_option.normal_period);
1776 	CH_SWITCH_SET_NORMAL_PERIOD_SEL(h2c_pkt, cs_option.normal_period_sel);
1777 	CH_SWITCH_SET_SLOW_PERIOD(h2c_pkt, cs_option.slow_period);
1778 	CH_SWITCH_SET_SLOW_PERIOD_SEL(h2c_pkt, cs_option.slow_period_sel);
1779 	CH_SWITCH_SET_NORMAL_CYCLE(h2c_pkt, cs_option.normal_cycle);
1780 	CH_SWITCH_SET_PERIODIC_OPT(h2c_pkt, cs_option.periodic_option);
1781 
1782 	CH_SWITCH_SET_CH_NUM(h2c_pkt, rtw_pno_req->channel_cnt);
1783 	CH_SWITCH_SET_INFO_SIZE(h2c_pkt, rtw_pno_req->channel_cnt * 4);
1784 
1785 	loc_ch_info = rtw_get_rsvd_page_location(rtwdev, RSVD_CH_INFO);
1786 	CH_SWITCH_SET_INFO_LOC(h2c_pkt, loc_ch_info);
1787 
1788 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1789 }
1790 
1791 void rtw_fw_adaptivity(struct rtw_dev *rtwdev)
1792 {
1793 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
1794 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1795 
1796 	if (!rtw_edcca_enabled) {
1797 		dm_info->edcca_mode = RTW_EDCCA_NORMAL;
1798 		rtw_dbg(rtwdev, RTW_DBG_ADAPTIVITY,
1799 			"EDCCA disabled by debugfs\n");
1800 	}
1801 
1802 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_ADAPTIVITY);
1803 	SET_ADAPTIVITY_MODE(h2c_pkt, dm_info->edcca_mode);
1804 	SET_ADAPTIVITY_OPTION(h2c_pkt, 1);
1805 	SET_ADAPTIVITY_IGI(h2c_pkt, dm_info->igi_history[0]);
1806 	SET_ADAPTIVITY_L2H(h2c_pkt, dm_info->l2h_th_ini);
1807 	SET_ADAPTIVITY_DENSITY(h2c_pkt, dm_info->scan_density);
1808 
1809 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
1810 }
1811 
1812 void rtw_fw_scan_notify(struct rtw_dev *rtwdev, bool start)
1813 {
1814 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1815 
1816 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_SCAN);
1817 	SET_SCAN_START(h2c_pkt, start);
1818 
1819 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
1820 }
1821 
1822 static int rtw_append_probe_req_ie(struct rtw_dev *rtwdev, struct sk_buff *skb,
1823 				   struct sk_buff_head *list, u8 *bands,
1824 				   struct rtw_vif *rtwvif)
1825 {
1826 	const struct rtw_chip_info *chip = rtwdev->chip;
1827 	struct ieee80211_scan_ies *ies = rtwvif->scan_ies;
1828 	struct sk_buff *new;
1829 	u8 idx;
1830 
1831 	for (idx = NL80211_BAND_2GHZ; idx < NUM_NL80211_BANDS; idx++) {
1832 		if (!(BIT(idx) & chip->band))
1833 			continue;
1834 		new = skb_copy(skb, GFP_KERNEL);
1835 		if (!new)
1836 			return -ENOMEM;
1837 		skb_put_data(new, ies->ies[idx], ies->len[idx]);
1838 		skb_put_data(new, ies->common_ies, ies->common_ie_len);
1839 		skb_queue_tail(list, new);
1840 		(*bands)++;
1841 	}
1842 
1843 	return 0;
1844 }
1845 
1846 static int _rtw_hw_scan_update_probe_req(struct rtw_dev *rtwdev, u8 num_probes,
1847 					 struct sk_buff_head *probe_req_list)
1848 {
1849 	const struct rtw_chip_info *chip = rtwdev->chip;
1850 	struct sk_buff *skb, *tmp;
1851 	u8 page_offset = 1, *buf, page_size = chip->page_size;
1852 	u16 pg_addr = rtwdev->fifo.rsvd_h2c_info_addr, loc;
1853 	u16 buf_offset = page_size * page_offset;
1854 	u8 tx_desc_sz = chip->tx_pkt_desc_sz;
1855 	u8 page_cnt, pages;
1856 	unsigned int pkt_len;
1857 	int ret;
1858 
1859 	if (rtw_fw_feature_ext_check(&rtwdev->fw, FW_FEATURE_EXT_OLD_PAGE_NUM))
1860 		page_cnt = RTW_OLD_PROBE_PG_CNT;
1861 	else
1862 		page_cnt = RTW_PROBE_PG_CNT;
1863 
1864 	pages = page_offset + num_probes * page_cnt;
1865 
1866 	buf = kzalloc(page_size * pages, GFP_KERNEL);
1867 	if (!buf)
1868 		return -ENOMEM;
1869 
1870 	buf_offset -= tx_desc_sz;
1871 	skb_queue_walk_safe(probe_req_list, skb, tmp) {
1872 		skb_unlink(skb, probe_req_list);
1873 		rtw_fill_rsvd_page_desc(rtwdev, skb, RSVD_PROBE_REQ);
1874 		if (skb->len > page_size * page_cnt) {
1875 			ret = -EINVAL;
1876 			goto out;
1877 		}
1878 
1879 		memcpy(buf + buf_offset, skb->data, skb->len);
1880 		pkt_len = skb->len - tx_desc_sz;
1881 		loc = pg_addr - rtwdev->fifo.rsvd_boundary + page_offset;
1882 		__rtw_fw_update_pkt(rtwdev, RTW_PACKET_PROBE_REQ, pkt_len, loc);
1883 
1884 		buf_offset += page_cnt * page_size;
1885 		page_offset += page_cnt;
1886 		kfree_skb(skb);
1887 	}
1888 
1889 	ret = rtw_fw_write_data_rsvd_page(rtwdev, pg_addr, buf, buf_offset);
1890 	if (ret) {
1891 		rtw_err(rtwdev, "Download probe request to firmware failed\n");
1892 		goto out;
1893 	}
1894 
1895 	rtwdev->scan_info.probe_pg_size = page_offset;
1896 out:
1897 	kfree(buf);
1898 	skb_queue_walk_safe(probe_req_list, skb, tmp)
1899 		kfree_skb(skb);
1900 
1901 	return ret;
1902 }
1903 
1904 static int rtw_hw_scan_update_probe_req(struct rtw_dev *rtwdev,
1905 					struct rtw_vif *rtwvif)
1906 {
1907 	struct cfg80211_scan_request *req = rtwvif->scan_req;
1908 	struct sk_buff_head list;
1909 	struct sk_buff *skb, *tmp;
1910 	u8 num = req->n_ssids, i, bands = 0;
1911 	int ret;
1912 
1913 	skb_queue_head_init(&list);
1914 	for (i = 0; i < num; i++) {
1915 		skb = ieee80211_probereq_get(rtwdev->hw, rtwvif->mac_addr,
1916 					     req->ssids[i].ssid,
1917 					     req->ssids[i].ssid_len,
1918 					     req->ie_len);
1919 		if (!skb) {
1920 			ret = -ENOMEM;
1921 			goto out;
1922 		}
1923 		ret = rtw_append_probe_req_ie(rtwdev, skb, &list, &bands,
1924 					      rtwvif);
1925 		if (ret)
1926 			goto out;
1927 
1928 		kfree_skb(skb);
1929 	}
1930 
1931 	return _rtw_hw_scan_update_probe_req(rtwdev, num * bands, &list);
1932 
1933 out:
1934 	skb_queue_walk_safe(&list, skb, tmp)
1935 		kfree_skb(skb);
1936 
1937 	return ret;
1938 }
1939 
1940 static int rtw_add_chan_info(struct rtw_dev *rtwdev, struct rtw_chan_info *info,
1941 			     struct rtw_chan_list *list, u8 *buf)
1942 {
1943 	u8 *chan = &buf[list->size];
1944 	u8 info_size = RTW_CH_INFO_SIZE;
1945 
1946 	if (list->size > list->buf_size)
1947 		return -ENOMEM;
1948 
1949 	CH_INFO_SET_CH(chan, info->channel);
1950 	CH_INFO_SET_PRI_CH_IDX(chan, info->pri_ch_idx);
1951 	CH_INFO_SET_BW(chan, info->bw);
1952 	CH_INFO_SET_TIMEOUT(chan, info->timeout);
1953 	CH_INFO_SET_ACTION_ID(chan, info->action_id);
1954 	CH_INFO_SET_EXTRA_INFO(chan, info->extra_info);
1955 	if (info->extra_info) {
1956 		EXTRA_CH_INFO_SET_ID(chan, RTW_SCAN_EXTRA_ID_DFS);
1957 		EXTRA_CH_INFO_SET_INFO(chan, RTW_SCAN_EXTRA_ACTION_SCAN);
1958 		EXTRA_CH_INFO_SET_SIZE(chan, RTW_EX_CH_INFO_SIZE -
1959 				       RTW_EX_CH_INFO_HDR_SIZE);
1960 		EXTRA_CH_INFO_SET_DFS_EXT_TIME(chan, RTW_DFS_CHAN_TIME);
1961 		info_size += RTW_EX_CH_INFO_SIZE;
1962 	}
1963 	list->size += info_size;
1964 	list->ch_num++;
1965 
1966 	return 0;
1967 }
1968 
1969 static int rtw_add_chan_list(struct rtw_dev *rtwdev, struct rtw_vif *rtwvif,
1970 			     struct rtw_chan_list *list, u8 *buf)
1971 {
1972 	struct cfg80211_scan_request *req = rtwvif->scan_req;
1973 	struct rtw_fifo_conf *fifo = &rtwdev->fifo;
1974 	struct ieee80211_channel *channel;
1975 	int i, ret = 0;
1976 
1977 	for (i = 0; i < req->n_channels; i++) {
1978 		struct rtw_chan_info ch_info = {0};
1979 
1980 		channel = req->channels[i];
1981 		ch_info.channel = channel->hw_value;
1982 		ch_info.bw = RTW_SCAN_WIDTH;
1983 		ch_info.pri_ch_idx = RTW_PRI_CH_IDX;
1984 		ch_info.timeout = req->duration_mandatory ?
1985 				  req->duration : RTW_CHANNEL_TIME;
1986 
1987 		if (channel->flags & (IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IR)) {
1988 			ch_info.action_id = RTW_CHANNEL_RADAR;
1989 			ch_info.extra_info = 1;
1990 			/* Overwrite duration for passive scans if necessary */
1991 			ch_info.timeout = ch_info.timeout > RTW_PASS_CHAN_TIME ?
1992 					  ch_info.timeout : RTW_PASS_CHAN_TIME;
1993 		} else {
1994 			ch_info.action_id = RTW_CHANNEL_ACTIVE;
1995 		}
1996 
1997 		ret = rtw_add_chan_info(rtwdev, &ch_info, list, buf);
1998 		if (ret)
1999 			return ret;
2000 	}
2001 
2002 	if (list->size > fifo->rsvd_pg_num << TX_PAGE_SIZE_SHIFT) {
2003 		rtw_err(rtwdev, "List exceeds rsvd page total size\n");
2004 		return -EINVAL;
2005 	}
2006 
2007 	list->addr = fifo->rsvd_h2c_info_addr + rtwdev->scan_info.probe_pg_size;
2008 	ret = rtw_fw_write_data_rsvd_page(rtwdev, list->addr, buf, list->size);
2009 	if (ret)
2010 		rtw_err(rtwdev, "Download channel list failed\n");
2011 
2012 	return ret;
2013 }
2014 
2015 static void rtw_fw_set_scan_offload(struct rtw_dev *rtwdev,
2016 				    struct rtw_ch_switch_option *opt,
2017 				    struct rtw_vif *rtwvif,
2018 				    struct rtw_chan_list *list)
2019 {
2020 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2021 	struct cfg80211_scan_request *req = rtwvif->scan_req;
2022 	struct rtw_fifo_conf *fifo = &rtwdev->fifo;
2023 	/* reserve one dummy page at the beginning for tx descriptor */
2024 	u8 pkt_loc = fifo->rsvd_h2c_info_addr - fifo->rsvd_boundary + 1;
2025 	bool random_seq = req->flags & NL80211_SCAN_FLAG_RANDOM_SN;
2026 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
2027 
2028 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_SCAN_OFFLOAD);
2029 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, H2C_PKT_CH_SWITCH_LEN);
2030 
2031 	SCAN_OFFLOAD_SET_START(h2c_pkt, opt->switch_en);
2032 	SCAN_OFFLOAD_SET_BACK_OP_EN(h2c_pkt, opt->back_op_en);
2033 	SCAN_OFFLOAD_SET_RANDOM_SEQ_EN(h2c_pkt, random_seq);
2034 	SCAN_OFFLOAD_SET_NO_CCK_EN(h2c_pkt, req->no_cck);
2035 	SCAN_OFFLOAD_SET_CH_NUM(h2c_pkt, list->ch_num);
2036 	SCAN_OFFLOAD_SET_CH_INFO_SIZE(h2c_pkt, list->size);
2037 	SCAN_OFFLOAD_SET_CH_INFO_LOC(h2c_pkt, list->addr - fifo->rsvd_boundary);
2038 	SCAN_OFFLOAD_SET_OP_CH(h2c_pkt, scan_info->op_chan);
2039 	SCAN_OFFLOAD_SET_OP_PRI_CH_IDX(h2c_pkt, scan_info->op_pri_ch_idx);
2040 	SCAN_OFFLOAD_SET_OP_BW(h2c_pkt, scan_info->op_bw);
2041 	SCAN_OFFLOAD_SET_OP_PORT_ID(h2c_pkt, rtwvif->port);
2042 	SCAN_OFFLOAD_SET_OP_DWELL_TIME(h2c_pkt, req->duration_mandatory ?
2043 				       req->duration : RTW_CHANNEL_TIME);
2044 	SCAN_OFFLOAD_SET_OP_GAP_TIME(h2c_pkt, RTW_OFF_CHAN_TIME);
2045 	SCAN_OFFLOAD_SET_SSID_NUM(h2c_pkt, req->n_ssids);
2046 	SCAN_OFFLOAD_SET_PKT_LOC(h2c_pkt, pkt_loc);
2047 
2048 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
2049 }
2050 
2051 void rtw_hw_scan_start(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
2052 		       struct ieee80211_scan_request *scan_req)
2053 {
2054 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
2055 	struct cfg80211_scan_request *req = &scan_req->req;
2056 	u8 mac_addr[ETH_ALEN];
2057 
2058 	rtwdev->scan_info.scanning_vif = vif;
2059 	rtwvif->scan_ies = &scan_req->ies;
2060 	rtwvif->scan_req = req;
2061 
2062 	ieee80211_stop_queues(rtwdev->hw);
2063 	rtw_leave_lps_deep(rtwdev);
2064 	rtw_hci_flush_all_queues(rtwdev, false);
2065 	rtw_mac_flush_all_queues(rtwdev, false);
2066 	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2067 		get_random_mask_addr(mac_addr, req->mac_addr,
2068 				     req->mac_addr_mask);
2069 	else
2070 		ether_addr_copy(mac_addr, vif->addr);
2071 
2072 	rtw_core_scan_start(rtwdev, rtwvif, mac_addr, true);
2073 
2074 	rtwdev->hal.rcr &= ~BIT_CBSSID_BCN;
2075 	rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr);
2076 }
2077 
2078 void rtw_hw_scan_complete(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
2079 			  bool aborted)
2080 {
2081 	struct cfg80211_scan_info info = {
2082 		.aborted = aborted,
2083 	};
2084 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2085 	struct rtw_hal *hal = &rtwdev->hal;
2086 	struct rtw_vif *rtwvif;
2087 	u8 chan = scan_info->op_chan;
2088 
2089 	if (!vif)
2090 		return;
2091 
2092 	rtwdev->hal.rcr |= BIT_CBSSID_BCN;
2093 	rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr);
2094 
2095 	rtw_core_scan_complete(rtwdev, vif, true);
2096 
2097 	rtwvif = (struct rtw_vif *)vif->drv_priv;
2098 	if (chan)
2099 		rtw_store_op_chan(rtwdev, false);
2100 	rtw_phy_set_tx_power_level(rtwdev, hal->current_channel);
2101 	ieee80211_wake_queues(rtwdev->hw);
2102 	ieee80211_scan_completed(rtwdev->hw, &info);
2103 
2104 	rtwvif->scan_req = NULL;
2105 	rtwvif->scan_ies = NULL;
2106 	rtwdev->scan_info.scanning_vif = NULL;
2107 }
2108 
2109 static int rtw_hw_scan_prehandle(struct rtw_dev *rtwdev, struct rtw_vif *rtwvif,
2110 				 struct rtw_chan_list *list)
2111 {
2112 	struct cfg80211_scan_request *req = rtwvif->scan_req;
2113 	int size = req->n_channels * (RTW_CH_INFO_SIZE + RTW_EX_CH_INFO_SIZE);
2114 	u8 *buf;
2115 	int ret;
2116 
2117 	buf = kmalloc(size, GFP_KERNEL);
2118 	if (!buf)
2119 		return -ENOMEM;
2120 
2121 	ret = rtw_hw_scan_update_probe_req(rtwdev, rtwvif);
2122 	if (ret) {
2123 		rtw_err(rtwdev, "Update probe request failed\n");
2124 		goto out;
2125 	}
2126 
2127 	list->buf_size = size;
2128 	list->size = 0;
2129 	list->ch_num = 0;
2130 	ret = rtw_add_chan_list(rtwdev, rtwvif, list, buf);
2131 out:
2132 	kfree(buf);
2133 
2134 	return ret;
2135 }
2136 
2137 int rtw_hw_scan_offload(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
2138 			bool enable)
2139 {
2140 	struct rtw_vif *rtwvif = vif ? (struct rtw_vif *)vif->drv_priv : NULL;
2141 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2142 	struct rtw_ch_switch_option cs_option = {0};
2143 	struct rtw_chan_list chan_list = {0};
2144 	int ret = 0;
2145 
2146 	if (!rtwvif)
2147 		return -EINVAL;
2148 
2149 	cs_option.switch_en = enable;
2150 	cs_option.back_op_en = scan_info->op_chan != 0;
2151 	if (enable) {
2152 		ret = rtw_hw_scan_prehandle(rtwdev, rtwvif, &chan_list);
2153 		if (ret)
2154 			goto out;
2155 	}
2156 	rtw_fw_set_scan_offload(rtwdev, &cs_option, rtwvif, &chan_list);
2157 out:
2158 	return ret;
2159 }
2160 
2161 void rtw_hw_scan_abort(struct rtw_dev *rtwdev, struct ieee80211_vif *vif)
2162 {
2163 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
2164 		return;
2165 
2166 	rtw_hw_scan_offload(rtwdev, vif, false);
2167 	rtw_hw_scan_complete(rtwdev, vif, true);
2168 }
2169 
2170 void rtw_hw_scan_status_report(struct rtw_dev *rtwdev, struct sk_buff *skb)
2171 {
2172 	struct ieee80211_vif *vif = rtwdev->scan_info.scanning_vif;
2173 	struct rtw_c2h_cmd *c2h;
2174 	bool aborted;
2175 	u8 rc;
2176 
2177 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
2178 		return;
2179 
2180 	c2h = get_c2h_from_skb(skb);
2181 	rc = GET_SCAN_REPORT_RETURN_CODE(c2h->payload);
2182 	aborted = rc != RTW_SCAN_REPORT_SUCCESS;
2183 	rtw_hw_scan_complete(rtwdev, vif, aborted);
2184 
2185 	if (aborted)
2186 		rtw_dbg(rtwdev, RTW_DBG_HW_SCAN, "HW scan aborted with code: %d\n", rc);
2187 }
2188 
2189 void rtw_store_op_chan(struct rtw_dev *rtwdev, bool backup)
2190 {
2191 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2192 	struct rtw_hal *hal = &rtwdev->hal;
2193 	u8 band;
2194 
2195 	if (backup) {
2196 		scan_info->op_chan = hal->current_channel;
2197 		scan_info->op_bw = hal->current_band_width;
2198 		scan_info->op_pri_ch_idx = hal->current_primary_channel_index;
2199 		scan_info->op_pri_ch = hal->primary_channel;
2200 	} else {
2201 		band = scan_info->op_chan > 14 ? RTW_BAND_5G : RTW_BAND_2G;
2202 		rtw_update_channel(rtwdev, scan_info->op_chan,
2203 				   scan_info->op_pri_ch,
2204 				   band, scan_info->op_bw);
2205 	}
2206 }
2207 
2208 void rtw_clear_op_chan(struct rtw_dev *rtwdev)
2209 {
2210 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2211 
2212 	scan_info->op_chan = 0;
2213 	scan_info->op_bw = 0;
2214 	scan_info->op_pri_ch_idx = 0;
2215 	scan_info->op_pri_ch = 0;
2216 }
2217 
2218 static bool rtw_is_op_chan(struct rtw_dev *rtwdev, u8 channel)
2219 {
2220 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2221 
2222 	return channel == scan_info->op_chan;
2223 }
2224 
2225 void rtw_hw_scan_chan_switch(struct rtw_dev *rtwdev, struct sk_buff *skb)
2226 {
2227 	struct rtw_hal *hal = &rtwdev->hal;
2228 	struct rtw_c2h_cmd *c2h;
2229 	enum rtw_scan_notify_id id;
2230 	u8 chan, band, status;
2231 
2232 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
2233 		return;
2234 
2235 	c2h = get_c2h_from_skb(skb);
2236 	chan = GET_CHAN_SWITCH_CENTRAL_CH(c2h->payload);
2237 	id = GET_CHAN_SWITCH_ID(c2h->payload);
2238 	status = GET_CHAN_SWITCH_STATUS(c2h->payload);
2239 
2240 	if (id == RTW_SCAN_NOTIFY_ID_POSTSWITCH) {
2241 		band = chan > 14 ? RTW_BAND_5G : RTW_BAND_2G;
2242 		rtw_update_channel(rtwdev, chan, chan, band,
2243 				   RTW_CHANNEL_WIDTH_20);
2244 		if (rtw_is_op_chan(rtwdev, chan)) {
2245 			rtw_store_op_chan(rtwdev, false);
2246 			ieee80211_wake_queues(rtwdev->hw);
2247 		}
2248 	} else if (id == RTW_SCAN_NOTIFY_ID_PRESWITCH) {
2249 		if (IS_CH_5G_BAND(chan)) {
2250 			rtw_coex_switchband_notify(rtwdev, COEX_SWITCH_TO_5G);
2251 		} else if (IS_CH_2G_BAND(chan)) {
2252 			u8 chan_type;
2253 
2254 			if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
2255 				chan_type = COEX_SWITCH_TO_24G;
2256 			else
2257 				chan_type = COEX_SWITCH_TO_24G_NOFORSCAN;
2258 			rtw_coex_switchband_notify(rtwdev, chan_type);
2259 		}
2260 		/* The channel of C2H RTW_SCAN_NOTIFY_ID_PRESWITCH is next
2261 		 * channel that hardware will switch. We need to stop queue
2262 		 * if next channel is non-op channel.
2263 		 */
2264 		if (!rtw_is_op_chan(rtwdev, chan) &&
2265 		    rtw_is_op_chan(rtwdev, hal->current_channel))
2266 			ieee80211_stop_queues(rtwdev->hw);
2267 	}
2268 
2269 	rtw_dbg(rtwdev, RTW_DBG_HW_SCAN,
2270 		"Chan switch: %x, id: %x, status: %x\n", chan, id, status);
2271 }
2272