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