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 
18 static void rtw_fw_c2h_cmd_handle_ext(struct rtw_dev *rtwdev,
19 				      struct sk_buff *skb)
20 {
21 	struct rtw_c2h_cmd *c2h;
22 	u8 sub_cmd_id;
23 
24 	c2h = get_c2h_from_skb(skb);
25 	sub_cmd_id = c2h->payload[0];
26 
27 	switch (sub_cmd_id) {
28 	case C2H_CCX_RPT:
29 		rtw_tx_report_handle(rtwdev, skb, C2H_CCX_RPT);
30 		break;
31 	default:
32 		break;
33 	}
34 }
35 
36 static u16 get_max_amsdu_len(u32 bit_rate)
37 {
38 	/* lower than ofdm, do not aggregate */
39 	if (bit_rate < 550)
40 		return 1;
41 
42 	/* lower than 20M 2ss mcs8, make it small */
43 	if (bit_rate < 1800)
44 		return 1200;
45 
46 	/* lower than 40M 2ss mcs9, make it medium */
47 	if (bit_rate < 4000)
48 		return 2600;
49 
50 	/* not yet 80M 2ss mcs8/9, make it twice regular packet size */
51 	if (bit_rate < 7000)
52 		return 3500;
53 
54 	/* unlimited */
55 	return 0;
56 }
57 
58 struct rtw_fw_iter_ra_data {
59 	struct rtw_dev *rtwdev;
60 	u8 *payload;
61 };
62 
63 static void rtw_fw_ra_report_iter(void *data, struct ieee80211_sta *sta)
64 {
65 	struct rtw_fw_iter_ra_data *ra_data = data;
66 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
67 	u8 mac_id, rate, sgi, bw;
68 	u8 mcs, nss;
69 	u32 bit_rate;
70 
71 	mac_id = GET_RA_REPORT_MACID(ra_data->payload);
72 	if (si->mac_id != mac_id)
73 		return;
74 
75 	si->ra_report.txrate.flags = 0;
76 
77 	rate = GET_RA_REPORT_RATE(ra_data->payload);
78 	sgi = GET_RA_REPORT_SGI(ra_data->payload);
79 	bw = GET_RA_REPORT_BW(ra_data->payload);
80 
81 	if (rate < DESC_RATEMCS0) {
82 		si->ra_report.txrate.legacy = rtw_desc_to_bitrate(rate);
83 		goto legacy;
84 	}
85 
86 	rtw_desc_to_mcsrate(rate, &mcs, &nss);
87 	if (rate >= DESC_RATEVHT1SS_MCS0)
88 		si->ra_report.txrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
89 	else if (rate >= DESC_RATEMCS0)
90 		si->ra_report.txrate.flags |= RATE_INFO_FLAGS_MCS;
91 
92 	if (rate >= DESC_RATEMCS0) {
93 		si->ra_report.txrate.mcs = mcs;
94 		si->ra_report.txrate.nss = nss;
95 	}
96 
97 	if (sgi)
98 		si->ra_report.txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
99 
100 	if (bw == RTW_CHANNEL_WIDTH_80)
101 		si->ra_report.txrate.bw = RATE_INFO_BW_80;
102 	else if (bw == RTW_CHANNEL_WIDTH_40)
103 		si->ra_report.txrate.bw = RATE_INFO_BW_40;
104 	else
105 		si->ra_report.txrate.bw = RATE_INFO_BW_20;
106 
107 legacy:
108 	bit_rate = cfg80211_calculate_bitrate(&si->ra_report.txrate);
109 
110 	si->ra_report.desc_rate = rate;
111 	si->ra_report.bit_rate = bit_rate;
112 
113 	sta->max_rc_amsdu_len = get_max_amsdu_len(bit_rate);
114 }
115 
116 static void rtw_fw_ra_report_handle(struct rtw_dev *rtwdev, u8 *payload,
117 				    u8 length)
118 {
119 	struct rtw_fw_iter_ra_data ra_data;
120 
121 	if (WARN(length < 7, "invalid ra report c2h length\n"))
122 		return;
123 
124 	rtwdev->dm_info.tx_rate = GET_RA_REPORT_RATE(payload);
125 	ra_data.rtwdev = rtwdev;
126 	ra_data.payload = payload;
127 	rtw_iterate_stas_atomic(rtwdev, rtw_fw_ra_report_iter, &ra_data);
128 }
129 
130 void rtw_fw_c2h_cmd_handle(struct rtw_dev *rtwdev, struct sk_buff *skb)
131 {
132 	struct rtw_c2h_cmd *c2h;
133 	u32 pkt_offset;
134 	u8 len;
135 
136 	pkt_offset = *((u32 *)skb->cb);
137 	c2h = (struct rtw_c2h_cmd *)(skb->data + pkt_offset);
138 	len = skb->len - pkt_offset - 2;
139 
140 	mutex_lock(&rtwdev->mutex);
141 
142 	if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags))
143 		goto unlock;
144 
145 	switch (c2h->id) {
146 	case C2H_CCX_TX_RPT:
147 		rtw_tx_report_handle(rtwdev, skb, C2H_CCX_TX_RPT);
148 		break;
149 	case C2H_BT_INFO:
150 		rtw_coex_bt_info_notify(rtwdev, c2h->payload, len);
151 		break;
152 	case C2H_WLAN_INFO:
153 		rtw_coex_wl_fwdbginfo_notify(rtwdev, c2h->payload, len);
154 		break;
155 	case C2H_HALMAC:
156 		rtw_fw_c2h_cmd_handle_ext(rtwdev, skb);
157 		break;
158 	case C2H_RA_RPT:
159 		rtw_fw_ra_report_handle(rtwdev, c2h->payload, len);
160 		break;
161 	default:
162 		rtw_dbg(rtwdev, RTW_DBG_FW, "C2H 0x%x isn't handled\n", c2h->id);
163 		break;
164 	}
165 
166 unlock:
167 	mutex_unlock(&rtwdev->mutex);
168 }
169 
170 void rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev *rtwdev, u32 pkt_offset,
171 			       struct sk_buff *skb)
172 {
173 	struct rtw_c2h_cmd *c2h;
174 	u8 len;
175 
176 	c2h = (struct rtw_c2h_cmd *)(skb->data + pkt_offset);
177 	len = skb->len - pkt_offset - 2;
178 	*((u32 *)skb->cb) = pkt_offset;
179 
180 	rtw_dbg(rtwdev, RTW_DBG_FW, "recv C2H, id=0x%02x, seq=0x%02x, len=%d\n",
181 		c2h->id, c2h->seq, len);
182 
183 	switch (c2h->id) {
184 	case C2H_BT_MP_INFO:
185 		rtw_coex_info_response(rtwdev, skb);
186 		break;
187 	case C2H_WLAN_RFON:
188 		complete(&rtwdev->lps_leave_check);
189 		break;
190 	default:
191 		/* pass offset for further operation */
192 		*((u32 *)skb->cb) = pkt_offset;
193 		skb_queue_tail(&rtwdev->c2h_queue, skb);
194 		ieee80211_queue_work(rtwdev->hw, &rtwdev->c2h_work);
195 		break;
196 	}
197 }
198 EXPORT_SYMBOL(rtw_fw_c2h_cmd_rx_irqsafe);
199 
200 void rtw_fw_c2h_cmd_isr(struct rtw_dev *rtwdev)
201 {
202 	if (rtw_read8(rtwdev, REG_MCU_TST_CFG) == VAL_FW_TRIGGER)
203 		rtw_fw_recovery(rtwdev);
204 	else
205 		rtw_warn(rtwdev, "unhandled firmware c2h interrupt\n");
206 }
207 EXPORT_SYMBOL(rtw_fw_c2h_cmd_isr);
208 
209 static void rtw_fw_send_h2c_command(struct rtw_dev *rtwdev,
210 				    u8 *h2c)
211 {
212 	u8 box;
213 	u8 box_state;
214 	u32 box_reg, box_ex_reg;
215 	int idx;
216 	int ret;
217 
218 	rtw_dbg(rtwdev, RTW_DBG_FW,
219 		"send H2C content %02x%02x%02x%02x %02x%02x%02x%02x\n",
220 		h2c[3], h2c[2], h2c[1], h2c[0],
221 		h2c[7], h2c[6], h2c[5], h2c[4]);
222 
223 	spin_lock(&rtwdev->h2c.lock);
224 
225 	box = rtwdev->h2c.last_box_num;
226 	switch (box) {
227 	case 0:
228 		box_reg = REG_HMEBOX0;
229 		box_ex_reg = REG_HMEBOX0_EX;
230 		break;
231 	case 1:
232 		box_reg = REG_HMEBOX1;
233 		box_ex_reg = REG_HMEBOX1_EX;
234 		break;
235 	case 2:
236 		box_reg = REG_HMEBOX2;
237 		box_ex_reg = REG_HMEBOX2_EX;
238 		break;
239 	case 3:
240 		box_reg = REG_HMEBOX3;
241 		box_ex_reg = REG_HMEBOX3_EX;
242 		break;
243 	default:
244 		WARN(1, "invalid h2c mail box number\n");
245 		goto out;
246 	}
247 
248 	ret = read_poll_timeout_atomic(rtw_read8, box_state,
249 				       !((box_state >> box) & 0x1), 100, 3000,
250 				       false, rtwdev, REG_HMETFR);
251 
252 	if (ret) {
253 		rtw_err(rtwdev, "failed to send h2c command\n");
254 		goto out;
255 	}
256 
257 	for (idx = 0; idx < 4; idx++)
258 		rtw_write8(rtwdev, box_reg + idx, h2c[idx]);
259 	for (idx = 0; idx < 4; idx++)
260 		rtw_write8(rtwdev, box_ex_reg + idx, h2c[idx + 4]);
261 
262 	if (++rtwdev->h2c.last_box_num >= 4)
263 		rtwdev->h2c.last_box_num = 0;
264 
265 out:
266 	spin_unlock(&rtwdev->h2c.lock);
267 }
268 
269 void rtw_fw_h2c_cmd_dbg(struct rtw_dev *rtwdev, u8 *h2c)
270 {
271 	rtw_fw_send_h2c_command(rtwdev, h2c);
272 }
273 
274 static void rtw_fw_send_h2c_packet(struct rtw_dev *rtwdev, u8 *h2c_pkt)
275 {
276 	int ret;
277 
278 	spin_lock(&rtwdev->h2c.lock);
279 
280 	FW_OFFLOAD_H2C_SET_SEQ_NUM(h2c_pkt, rtwdev->h2c.seq);
281 	ret = rtw_hci_write_data_h2c(rtwdev, h2c_pkt, H2C_PKT_SIZE);
282 	if (ret)
283 		rtw_err(rtwdev, "failed to send h2c packet\n");
284 	rtwdev->h2c.seq++;
285 
286 	spin_unlock(&rtwdev->h2c.lock);
287 }
288 
289 void
290 rtw_fw_send_general_info(struct rtw_dev *rtwdev)
291 {
292 	struct rtw_fifo_conf *fifo = &rtwdev->fifo;
293 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
294 	u16 total_size = H2C_PKT_HDR_SIZE + 4;
295 
296 	if (rtw_chip_wcpu_11n(rtwdev))
297 		return;
298 
299 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_GENERAL_INFO);
300 
301 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
302 
303 	GENERAL_INFO_SET_FW_TX_BOUNDARY(h2c_pkt,
304 					fifo->rsvd_fw_txbuf_addr -
305 					fifo->rsvd_boundary);
306 
307 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
308 }
309 
310 void
311 rtw_fw_send_phydm_info(struct rtw_dev *rtwdev)
312 {
313 	struct rtw_hal *hal = &rtwdev->hal;
314 	struct rtw_efuse *efuse = &rtwdev->efuse;
315 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
316 	u16 total_size = H2C_PKT_HDR_SIZE + 8;
317 	u8 fw_rf_type = 0;
318 
319 	if (rtw_chip_wcpu_11n(rtwdev))
320 		return;
321 
322 	if (hal->rf_type == RF_1T1R)
323 		fw_rf_type = FW_RF_1T1R;
324 	else if (hal->rf_type == RF_2T2R)
325 		fw_rf_type = FW_RF_2T2R;
326 
327 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_PHYDM_INFO);
328 
329 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
330 	PHYDM_INFO_SET_REF_TYPE(h2c_pkt, efuse->rfe_option);
331 	PHYDM_INFO_SET_RF_TYPE(h2c_pkt, fw_rf_type);
332 	PHYDM_INFO_SET_CUT_VER(h2c_pkt, hal->cut_version);
333 	PHYDM_INFO_SET_RX_ANT_STATUS(h2c_pkt, hal->antenna_tx);
334 	PHYDM_INFO_SET_TX_ANT_STATUS(h2c_pkt, hal->antenna_rx);
335 
336 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
337 }
338 
339 void rtw_fw_do_iqk(struct rtw_dev *rtwdev, struct rtw_iqk_para *para)
340 {
341 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
342 	u16 total_size = H2C_PKT_HDR_SIZE + 1;
343 
344 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_IQK);
345 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
346 	IQK_SET_CLEAR(h2c_pkt, para->clear);
347 	IQK_SET_SEGMENT_IQK(h2c_pkt, para->segment_iqk);
348 
349 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
350 }
351 EXPORT_SYMBOL(rtw_fw_do_iqk);
352 
353 void rtw_fw_inform_rfk_status(struct rtw_dev *rtwdev, bool start)
354 {
355 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
356 
357 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WIFI_CALIBRATION);
358 
359 	RFK_SET_INFORM_START(h2c_pkt, start);
360 
361 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
362 }
363 EXPORT_SYMBOL(rtw_fw_inform_rfk_status);
364 
365 void rtw_fw_query_bt_info(struct rtw_dev *rtwdev)
366 {
367 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
368 
369 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_INFO);
370 
371 	SET_QUERY_BT_INFO(h2c_pkt, true);
372 
373 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
374 }
375 
376 void rtw_fw_wl_ch_info(struct rtw_dev *rtwdev, u8 link, u8 ch, u8 bw)
377 {
378 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
379 
380 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WL_CH_INFO);
381 
382 	SET_WL_CH_INFO_LINK(h2c_pkt, link);
383 	SET_WL_CH_INFO_CHNL(h2c_pkt, ch);
384 	SET_WL_CH_INFO_BW(h2c_pkt, bw);
385 
386 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
387 }
388 
389 void rtw_fw_query_bt_mp_info(struct rtw_dev *rtwdev,
390 			     struct rtw_coex_info_req *req)
391 {
392 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
393 
394 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_MP_INFO);
395 
396 	SET_BT_MP_INFO_SEQ(h2c_pkt, req->seq);
397 	SET_BT_MP_INFO_OP_CODE(h2c_pkt, req->op_code);
398 	SET_BT_MP_INFO_PARA1(h2c_pkt, req->para1);
399 	SET_BT_MP_INFO_PARA2(h2c_pkt, req->para2);
400 	SET_BT_MP_INFO_PARA3(h2c_pkt, req->para3);
401 
402 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
403 }
404 
405 void rtw_fw_force_bt_tx_power(struct rtw_dev *rtwdev, u8 bt_pwr_dec_lvl)
406 {
407 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
408 	u8 index = 0 - bt_pwr_dec_lvl;
409 
410 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_FORCE_BT_TX_POWER);
411 
412 	SET_BT_TX_POWER_INDEX(h2c_pkt, index);
413 
414 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
415 }
416 
417 void rtw_fw_bt_ignore_wlan_action(struct rtw_dev *rtwdev, bool enable)
418 {
419 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
420 
421 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_IGNORE_WLAN_ACTION);
422 
423 	SET_IGNORE_WLAN_ACTION_EN(h2c_pkt, enable);
424 
425 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
426 }
427 
428 void rtw_fw_coex_tdma_type(struct rtw_dev *rtwdev,
429 			   u8 para1, u8 para2, u8 para3, u8 para4, u8 para5)
430 {
431 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
432 
433 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_COEX_TDMA_TYPE);
434 
435 	SET_COEX_TDMA_TYPE_PARA1(h2c_pkt, para1);
436 	SET_COEX_TDMA_TYPE_PARA2(h2c_pkt, para2);
437 	SET_COEX_TDMA_TYPE_PARA3(h2c_pkt, para3);
438 	SET_COEX_TDMA_TYPE_PARA4(h2c_pkt, para4);
439 	SET_COEX_TDMA_TYPE_PARA5(h2c_pkt, para5);
440 
441 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
442 }
443 
444 void rtw_fw_bt_wifi_control(struct rtw_dev *rtwdev, u8 op_code, u8 *data)
445 {
446 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
447 
448 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BT_WIFI_CONTROL);
449 
450 	SET_BT_WIFI_CONTROL_OP_CODE(h2c_pkt, op_code);
451 
452 	SET_BT_WIFI_CONTROL_DATA1(h2c_pkt, *data);
453 	SET_BT_WIFI_CONTROL_DATA2(h2c_pkt, *(data + 1));
454 	SET_BT_WIFI_CONTROL_DATA3(h2c_pkt, *(data + 2));
455 	SET_BT_WIFI_CONTROL_DATA4(h2c_pkt, *(data + 3));
456 	SET_BT_WIFI_CONTROL_DATA5(h2c_pkt, *(data + 4));
457 
458 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
459 }
460 
461 void rtw_fw_send_rssi_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si)
462 {
463 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
464 	u8 rssi = ewma_rssi_read(&si->avg_rssi);
465 	bool stbc_en = si->stbc_en ? true : false;
466 
467 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSSI_MONITOR);
468 
469 	SET_RSSI_INFO_MACID(h2c_pkt, si->mac_id);
470 	SET_RSSI_INFO_RSSI(h2c_pkt, rssi);
471 	SET_RSSI_INFO_STBC(h2c_pkt, stbc_en);
472 
473 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
474 }
475 
476 void rtw_fw_send_ra_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si)
477 {
478 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
479 	bool no_update = si->updated;
480 	bool disable_pt = true;
481 
482 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RA_INFO);
483 
484 	SET_RA_INFO_MACID(h2c_pkt, si->mac_id);
485 	SET_RA_INFO_RATE_ID(h2c_pkt, si->rate_id);
486 	SET_RA_INFO_INIT_RA_LVL(h2c_pkt, si->init_ra_lv);
487 	SET_RA_INFO_SGI_EN(h2c_pkt, si->sgi_enable);
488 	SET_RA_INFO_BW_MODE(h2c_pkt, si->bw_mode);
489 	SET_RA_INFO_LDPC(h2c_pkt, !!si->ldpc_en);
490 	SET_RA_INFO_NO_UPDATE(h2c_pkt, no_update);
491 	SET_RA_INFO_VHT_EN(h2c_pkt, si->vht_enable);
492 	SET_RA_INFO_DIS_PT(h2c_pkt, disable_pt);
493 	SET_RA_INFO_RA_MASK0(h2c_pkt, (si->ra_mask & 0xff));
494 	SET_RA_INFO_RA_MASK1(h2c_pkt, (si->ra_mask & 0xff00) >> 8);
495 	SET_RA_INFO_RA_MASK2(h2c_pkt, (si->ra_mask & 0xff0000) >> 16);
496 	SET_RA_INFO_RA_MASK3(h2c_pkt, (si->ra_mask & 0xff000000) >> 24);
497 
498 	si->init_ra_lv = 0;
499 	si->updated = true;
500 
501 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
502 }
503 
504 void rtw_fw_media_status_report(struct rtw_dev *rtwdev, u8 mac_id, bool connect)
505 {
506 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
507 
508 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_MEDIA_STATUS_RPT);
509 	MEDIA_STATUS_RPT_SET_OP_MODE(h2c_pkt, connect);
510 	MEDIA_STATUS_RPT_SET_MACID(h2c_pkt, mac_id);
511 
512 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
513 }
514 
515 void rtw_fw_update_wl_phy_info(struct rtw_dev *rtwdev)
516 {
517 	struct rtw_traffic_stats *stats = &rtwdev->stats;
518 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
519 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
520 
521 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WL_PHY_INFO);
522 	SET_WL_PHY_INFO_TX_TP(h2c_pkt, stats->tx_throughput);
523 	SET_WL_PHY_INFO_RX_TP(h2c_pkt, stats->rx_throughput);
524 	SET_WL_PHY_INFO_TX_RATE_DESC(h2c_pkt, dm_info->tx_rate);
525 	SET_WL_PHY_INFO_RX_RATE_DESC(h2c_pkt, dm_info->curr_rx_rate);
526 	SET_WL_PHY_INFO_RX_EVM(h2c_pkt, dm_info->rx_evm_dbm[RF_PATH_A]);
527 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
528 }
529 
530 void rtw_fw_set_pwr_mode(struct rtw_dev *rtwdev)
531 {
532 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
533 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
534 
535 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_SET_PWR_MODE);
536 
537 	SET_PWR_MODE_SET_MODE(h2c_pkt, conf->mode);
538 	SET_PWR_MODE_SET_RLBM(h2c_pkt, conf->rlbm);
539 	SET_PWR_MODE_SET_SMART_PS(h2c_pkt, conf->smart_ps);
540 	SET_PWR_MODE_SET_AWAKE_INTERVAL(h2c_pkt, conf->awake_interval);
541 	SET_PWR_MODE_SET_PORT_ID(h2c_pkt, conf->port_id);
542 	SET_PWR_MODE_SET_PWR_STATE(h2c_pkt, conf->state);
543 
544 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
545 }
546 
547 void rtw_fw_set_keep_alive_cmd(struct rtw_dev *rtwdev, bool enable)
548 {
549 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
550 	struct rtw_fw_wow_keep_alive_para mode = {
551 		.adopt = true,
552 		.pkt_type = KEEP_ALIVE_NULL_PKT,
553 		.period = 5,
554 	};
555 
556 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_KEEP_ALIVE);
557 	SET_KEEP_ALIVE_ENABLE(h2c_pkt, enable);
558 	SET_KEEP_ALIVE_ADOPT(h2c_pkt, mode.adopt);
559 	SET_KEEP_ALIVE_PKT_TYPE(h2c_pkt, mode.pkt_type);
560 	SET_KEEP_ALIVE_CHECK_PERIOD(h2c_pkt, mode.period);
561 
562 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
563 }
564 
565 void rtw_fw_set_disconnect_decision_cmd(struct rtw_dev *rtwdev, bool enable)
566 {
567 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
568 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
569 	struct rtw_fw_wow_disconnect_para mode = {
570 		.adopt = true,
571 		.period = 30,
572 		.retry_count = 5,
573 	};
574 
575 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_DISCONNECT_DECISION);
576 
577 	if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags)) {
578 		SET_DISCONNECT_DECISION_ENABLE(h2c_pkt, enable);
579 		SET_DISCONNECT_DECISION_ADOPT(h2c_pkt, mode.adopt);
580 		SET_DISCONNECT_DECISION_CHECK_PERIOD(h2c_pkt, mode.period);
581 		SET_DISCONNECT_DECISION_TRY_PKT_NUM(h2c_pkt, mode.retry_count);
582 	}
583 
584 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
585 }
586 
587 void rtw_fw_set_wowlan_ctrl_cmd(struct rtw_dev *rtwdev, bool enable)
588 {
589 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
590 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
591 
592 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WOWLAN);
593 
594 	SET_WOWLAN_FUNC_ENABLE(h2c_pkt, enable);
595 	if (rtw_wow_mgd_linked(rtwdev)) {
596 		if (test_bit(RTW_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags))
597 			SET_WOWLAN_MAGIC_PKT_ENABLE(h2c_pkt, enable);
598 		if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags))
599 			SET_WOWLAN_DEAUTH_WAKEUP_ENABLE(h2c_pkt, enable);
600 		if (test_bit(RTW_WOW_FLAG_EN_REKEY_PKT, rtw_wow->flags))
601 			SET_WOWLAN_REKEY_WAKEUP_ENABLE(h2c_pkt, enable);
602 		if (rtw_wow->pattern_cnt)
603 			SET_WOWLAN_PATTERN_MATCH_ENABLE(h2c_pkt, enable);
604 	}
605 
606 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
607 }
608 
609 void rtw_fw_set_aoac_global_info_cmd(struct rtw_dev *rtwdev,
610 				     u8 pairwise_key_enc,
611 				     u8 group_key_enc)
612 {
613 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
614 
615 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_AOAC_GLOBAL_INFO);
616 
617 	SET_AOAC_GLOBAL_INFO_PAIRWISE_ENC_ALG(h2c_pkt, pairwise_key_enc);
618 	SET_AOAC_GLOBAL_INFO_GROUP_ENC_ALG(h2c_pkt, group_key_enc);
619 
620 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
621 }
622 
623 void rtw_fw_set_remote_wake_ctrl_cmd(struct rtw_dev *rtwdev, bool enable)
624 {
625 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
626 
627 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_REMOTE_WAKE_CTRL);
628 
629 	SET_REMOTE_WAKECTRL_ENABLE(h2c_pkt, enable);
630 
631 	if (rtw_wow_no_link(rtwdev))
632 		SET_REMOTE_WAKE_CTRL_NLO_OFFLOAD_EN(h2c_pkt, enable);
633 
634 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
635 }
636 
637 static u8 rtw_get_rsvd_page_location(struct rtw_dev *rtwdev,
638 				     enum rtw_rsvd_packet_type type)
639 {
640 	struct rtw_rsvd_page *rsvd_pkt;
641 	u8 location = 0;
642 
643 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
644 		if (type == rsvd_pkt->type)
645 			location = rsvd_pkt->page;
646 	}
647 
648 	return location;
649 }
650 
651 void rtw_fw_set_nlo_info(struct rtw_dev *rtwdev, bool enable)
652 {
653 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
654 	u8 loc_nlo;
655 
656 	loc_nlo = rtw_get_rsvd_page_location(rtwdev, RSVD_NLO_INFO);
657 
658 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_NLO_INFO);
659 
660 	SET_NLO_FUN_EN(h2c_pkt, enable);
661 	if (enable) {
662 		if (rtw_get_lps_deep_mode(rtwdev) != LPS_DEEP_MODE_NONE)
663 			SET_NLO_PS_32K(h2c_pkt, enable);
664 		SET_NLO_IGNORE_SECURITY(h2c_pkt, enable);
665 		SET_NLO_LOC_NLO_INFO(h2c_pkt, loc_nlo);
666 	}
667 
668 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
669 }
670 
671 void rtw_fw_set_pg_info(struct rtw_dev *rtwdev)
672 {
673 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
674 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
675 	u8 loc_pg, loc_dpk;
676 
677 	loc_pg = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_INFO);
678 	loc_dpk = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_DPK);
679 
680 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_LPS_PG_INFO);
681 
682 	LPS_PG_INFO_LOC(h2c_pkt, loc_pg);
683 	LPS_PG_DPK_LOC(h2c_pkt, loc_dpk);
684 	LPS_PG_SEC_CAM_EN(h2c_pkt, conf->sec_cam_backup);
685 	LPS_PG_PATTERN_CAM_EN(h2c_pkt, conf->pattern_cam_backup);
686 
687 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
688 }
689 
690 static u8 rtw_get_rsvd_page_probe_req_location(struct rtw_dev *rtwdev,
691 					       struct cfg80211_ssid *ssid)
692 {
693 	struct rtw_rsvd_page *rsvd_pkt;
694 	u8 location = 0;
695 
696 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
697 		if (rsvd_pkt->type != RSVD_PROBE_REQ)
698 			continue;
699 		if ((!ssid && !rsvd_pkt->ssid) ||
700 		    rtw_ssid_equal(rsvd_pkt->ssid, ssid))
701 			location = rsvd_pkt->page;
702 	}
703 
704 	return location;
705 }
706 
707 static u16 rtw_get_rsvd_page_probe_req_size(struct rtw_dev *rtwdev,
708 					    struct cfg80211_ssid *ssid)
709 {
710 	struct rtw_rsvd_page *rsvd_pkt;
711 	u16 size = 0;
712 
713 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
714 		if (rsvd_pkt->type != RSVD_PROBE_REQ)
715 			continue;
716 		if ((!ssid && !rsvd_pkt->ssid) ||
717 		    rtw_ssid_equal(rsvd_pkt->ssid, ssid))
718 			size = rsvd_pkt->skb->len;
719 	}
720 
721 	return size;
722 }
723 
724 void rtw_send_rsvd_page_h2c(struct rtw_dev *rtwdev)
725 {
726 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
727 	u8 location = 0;
728 
729 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSVD_PAGE);
730 
731 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_PROBE_RESP);
732 	*(h2c_pkt + 1) = location;
733 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PROBE_RESP loc: %d\n", location);
734 
735 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_PS_POLL);
736 	*(h2c_pkt + 2) = location;
737 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PS_POLL loc: %d\n", location);
738 
739 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_NULL);
740 	*(h2c_pkt + 3) = location;
741 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_NULL loc: %d\n", location);
742 
743 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_QOS_NULL);
744 	*(h2c_pkt + 4) = location;
745 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_QOS_NULL loc: %d\n", location);
746 
747 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
748 }
749 
750 static struct sk_buff *rtw_nlo_info_get(struct ieee80211_hw *hw)
751 {
752 	struct rtw_dev *rtwdev = hw->priv;
753 	struct rtw_chip_info *chip = rtwdev->chip;
754 	struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req;
755 	struct rtw_nlo_info_hdr *nlo_hdr;
756 	struct cfg80211_ssid *ssid;
757 	struct sk_buff *skb;
758 	u8 *pos, loc;
759 	u32 size;
760 	int i;
761 
762 	if (!pno_req->inited || !pno_req->match_set_cnt)
763 		return NULL;
764 
765 	size = sizeof(struct rtw_nlo_info_hdr) + pno_req->match_set_cnt *
766 		      IEEE80211_MAX_SSID_LEN + chip->tx_pkt_desc_sz;
767 
768 	skb = alloc_skb(size, GFP_KERNEL);
769 	if (!skb)
770 		return NULL;
771 
772 	skb_reserve(skb, chip->tx_pkt_desc_sz);
773 
774 	nlo_hdr = skb_put_zero(skb, sizeof(struct rtw_nlo_info_hdr));
775 
776 	nlo_hdr->nlo_count = pno_req->match_set_cnt;
777 	nlo_hdr->hidden_ap_count = pno_req->match_set_cnt;
778 
779 	/* pattern check for firmware */
780 	memset(nlo_hdr->pattern_check, 0xA5, FW_NLO_INFO_CHECK_SIZE);
781 
782 	for (i = 0; i < pno_req->match_set_cnt; i++)
783 		nlo_hdr->ssid_len[i] = pno_req->match_sets[i].ssid.ssid_len;
784 
785 	for (i = 0; i < pno_req->match_set_cnt; i++) {
786 		ssid = &pno_req->match_sets[i].ssid;
787 		loc  = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid);
788 		if (!loc) {
789 			rtw_err(rtwdev, "failed to get probe req rsvd loc\n");
790 			kfree_skb(skb);
791 			return NULL;
792 		}
793 		nlo_hdr->location[i] = loc;
794 	}
795 
796 	for (i = 0; i < pno_req->match_set_cnt; i++) {
797 		pos = skb_put_zero(skb, IEEE80211_MAX_SSID_LEN);
798 		memcpy(pos, pno_req->match_sets[i].ssid.ssid,
799 		       pno_req->match_sets[i].ssid.ssid_len);
800 	}
801 
802 	return skb;
803 }
804 
805 static struct sk_buff *rtw_cs_channel_info_get(struct ieee80211_hw *hw)
806 {
807 	struct rtw_dev *rtwdev = hw->priv;
808 	struct rtw_chip_info *chip = rtwdev->chip;
809 	struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req;
810 	struct ieee80211_channel *channels = pno_req->channels;
811 	struct sk_buff *skb;
812 	int count =  pno_req->channel_cnt;
813 	u8 *pos;
814 	int i = 0;
815 
816 	skb = alloc_skb(4 * count + chip->tx_pkt_desc_sz, GFP_KERNEL);
817 	if (!skb)
818 		return NULL;
819 
820 	skb_reserve(skb, chip->tx_pkt_desc_sz);
821 
822 	for (i = 0; i < count; i++) {
823 		pos = skb_put_zero(skb, 4);
824 
825 		CHSW_INFO_SET_CH(pos, channels[i].hw_value);
826 
827 		if (channels[i].flags & IEEE80211_CHAN_RADAR)
828 			CHSW_INFO_SET_ACTION_ID(pos, 0);
829 		else
830 			CHSW_INFO_SET_ACTION_ID(pos, 1);
831 		CHSW_INFO_SET_TIMEOUT(pos, 1);
832 		CHSW_INFO_SET_PRI_CH_IDX(pos, 1);
833 		CHSW_INFO_SET_BW(pos, 0);
834 	}
835 
836 	return skb;
837 }
838 
839 static struct sk_buff *rtw_lps_pg_dpk_get(struct ieee80211_hw *hw)
840 {
841 	struct rtw_dev *rtwdev = hw->priv;
842 	struct rtw_chip_info *chip = rtwdev->chip;
843 	struct rtw_dpk_info *dpk_info = &rtwdev->dm_info.dpk_info;
844 	struct rtw_lps_pg_dpk_hdr *dpk_hdr;
845 	struct sk_buff *skb;
846 	u32 size;
847 
848 	size = chip->tx_pkt_desc_sz + sizeof(*dpk_hdr);
849 	skb = alloc_skb(size, GFP_KERNEL);
850 	if (!skb)
851 		return NULL;
852 
853 	skb_reserve(skb, chip->tx_pkt_desc_sz);
854 	dpk_hdr = skb_put_zero(skb, sizeof(*dpk_hdr));
855 	dpk_hdr->dpk_ch = dpk_info->dpk_ch;
856 	dpk_hdr->dpk_path_ok = dpk_info->dpk_path_ok[0];
857 	memcpy(dpk_hdr->dpk_txagc, dpk_info->dpk_txagc, 2);
858 	memcpy(dpk_hdr->dpk_gs, dpk_info->dpk_gs, 4);
859 	memcpy(dpk_hdr->coef, dpk_info->coef, 160);
860 
861 	return skb;
862 }
863 
864 static struct sk_buff *rtw_lps_pg_info_get(struct ieee80211_hw *hw)
865 {
866 	struct rtw_dev *rtwdev = hw->priv;
867 	struct rtw_chip_info *chip = rtwdev->chip;
868 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
869 	struct rtw_lps_pg_info_hdr *pg_info_hdr;
870 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
871 	struct sk_buff *skb;
872 	u32 size;
873 
874 	size = chip->tx_pkt_desc_sz + sizeof(*pg_info_hdr);
875 	skb = alloc_skb(size, GFP_KERNEL);
876 	if (!skb)
877 		return NULL;
878 
879 	skb_reserve(skb, chip->tx_pkt_desc_sz);
880 	pg_info_hdr = skb_put_zero(skb, sizeof(*pg_info_hdr));
881 	pg_info_hdr->tx_bu_page_count = rtwdev->fifo.rsvd_drv_pg_num;
882 	pg_info_hdr->macid = find_first_bit(rtwdev->mac_id_map, RTW_MAX_MAC_ID_NUM);
883 	pg_info_hdr->sec_cam_count =
884 		rtw_sec_cam_pg_backup(rtwdev, pg_info_hdr->sec_cam);
885 	pg_info_hdr->pattern_count = rtw_wow->pattern_cnt;
886 
887 	conf->sec_cam_backup = pg_info_hdr->sec_cam_count != 0;
888 	conf->pattern_cam_backup = rtw_wow->pattern_cnt != 0;
889 
890 	return skb;
891 }
892 
893 static struct sk_buff *rtw_get_rsvd_page_skb(struct ieee80211_hw *hw,
894 					     struct rtw_rsvd_page *rsvd_pkt)
895 {
896 	struct ieee80211_vif *vif;
897 	struct rtw_vif *rtwvif;
898 	struct sk_buff *skb_new;
899 	struct cfg80211_ssid *ssid;
900 
901 	if (rsvd_pkt->type == RSVD_DUMMY) {
902 		skb_new = alloc_skb(1, GFP_KERNEL);
903 		if (!skb_new)
904 			return NULL;
905 
906 		skb_put(skb_new, 1);
907 		return skb_new;
908 	}
909 
910 	rtwvif = rsvd_pkt->rtwvif;
911 	if (!rtwvif)
912 		return NULL;
913 
914 	vif = rtwvif_to_vif(rtwvif);
915 
916 	switch (rsvd_pkt->type) {
917 	case RSVD_BEACON:
918 		skb_new = ieee80211_beacon_get(hw, vif);
919 		break;
920 	case RSVD_PS_POLL:
921 		skb_new = ieee80211_pspoll_get(hw, vif);
922 		break;
923 	case RSVD_PROBE_RESP:
924 		skb_new = ieee80211_proberesp_get(hw, vif);
925 		break;
926 	case RSVD_NULL:
927 		skb_new = ieee80211_nullfunc_get(hw, vif, false);
928 		break;
929 	case RSVD_QOS_NULL:
930 		skb_new = ieee80211_nullfunc_get(hw, vif, true);
931 		break;
932 	case RSVD_LPS_PG_DPK:
933 		skb_new = rtw_lps_pg_dpk_get(hw);
934 		break;
935 	case RSVD_LPS_PG_INFO:
936 		skb_new = rtw_lps_pg_info_get(hw);
937 		break;
938 	case RSVD_PROBE_REQ:
939 		ssid = (struct cfg80211_ssid *)rsvd_pkt->ssid;
940 		if (ssid)
941 			skb_new = ieee80211_probereq_get(hw, vif->addr,
942 							 ssid->ssid,
943 							 ssid->ssid_len, 0);
944 		else
945 			skb_new = ieee80211_probereq_get(hw, vif->addr, NULL, 0, 0);
946 		break;
947 	case RSVD_NLO_INFO:
948 		skb_new = rtw_nlo_info_get(hw);
949 		break;
950 	case RSVD_CH_INFO:
951 		skb_new = rtw_cs_channel_info_get(hw);
952 		break;
953 	default:
954 		return NULL;
955 	}
956 
957 	if (!skb_new)
958 		return NULL;
959 
960 	return skb_new;
961 }
962 
963 static void rtw_fill_rsvd_page_desc(struct rtw_dev *rtwdev, struct sk_buff *skb,
964 				    enum rtw_rsvd_packet_type type)
965 {
966 	struct rtw_tx_pkt_info pkt_info = {0};
967 	struct rtw_chip_info *chip = rtwdev->chip;
968 	u8 *pkt_desc;
969 
970 	rtw_tx_rsvd_page_pkt_info_update(rtwdev, &pkt_info, skb, type);
971 	pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz);
972 	memset(pkt_desc, 0, chip->tx_pkt_desc_sz);
973 	rtw_tx_fill_tx_desc(&pkt_info, skb);
974 }
975 
976 static inline u8 rtw_len_to_page(unsigned int len, u8 page_size)
977 {
978 	return DIV_ROUND_UP(len, page_size);
979 }
980 
981 static void rtw_rsvd_page_list_to_buf(struct rtw_dev *rtwdev, u8 page_size,
982 				      u8 page_margin, u32 page, u8 *buf,
983 				      struct rtw_rsvd_page *rsvd_pkt)
984 {
985 	struct sk_buff *skb = rsvd_pkt->skb;
986 
987 	if (page >= 1)
988 		memcpy(buf + page_margin + page_size * (page - 1),
989 		       skb->data, skb->len);
990 	else
991 		memcpy(buf, skb->data, skb->len);
992 }
993 
994 static struct rtw_rsvd_page *rtw_alloc_rsvd_page(struct rtw_dev *rtwdev,
995 						 enum rtw_rsvd_packet_type type,
996 						 bool txdesc)
997 {
998 	struct rtw_rsvd_page *rsvd_pkt = NULL;
999 
1000 	rsvd_pkt = kzalloc(sizeof(*rsvd_pkt), GFP_KERNEL);
1001 
1002 	if (!rsvd_pkt)
1003 		return NULL;
1004 
1005 	INIT_LIST_HEAD(&rsvd_pkt->vif_list);
1006 	INIT_LIST_HEAD(&rsvd_pkt->build_list);
1007 	rsvd_pkt->type = type;
1008 	rsvd_pkt->add_txdesc = txdesc;
1009 
1010 	return rsvd_pkt;
1011 }
1012 
1013 static void rtw_insert_rsvd_page(struct rtw_dev *rtwdev,
1014 				 struct rtw_vif *rtwvif,
1015 				 struct rtw_rsvd_page *rsvd_pkt)
1016 {
1017 	lockdep_assert_held(&rtwdev->mutex);
1018 
1019 	list_add_tail(&rsvd_pkt->vif_list, &rtwvif->rsvd_page_list);
1020 }
1021 
1022 static void rtw_add_rsvd_page(struct rtw_dev *rtwdev,
1023 			      struct rtw_vif *rtwvif,
1024 			      enum rtw_rsvd_packet_type type,
1025 			      bool txdesc)
1026 {
1027 	struct rtw_rsvd_page *rsvd_pkt;
1028 
1029 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, type, txdesc);
1030 	if (!rsvd_pkt) {
1031 		rtw_err(rtwdev, "failed to alloc rsvd page %d\n", type);
1032 		return;
1033 	}
1034 
1035 	rsvd_pkt->rtwvif = rtwvif;
1036 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1037 }
1038 
1039 static void rtw_add_rsvd_page_probe_req(struct rtw_dev *rtwdev,
1040 					struct rtw_vif *rtwvif,
1041 					struct cfg80211_ssid *ssid)
1042 {
1043 	struct rtw_rsvd_page *rsvd_pkt;
1044 
1045 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_PROBE_REQ, true);
1046 	if (!rsvd_pkt) {
1047 		rtw_err(rtwdev, "failed to alloc probe req rsvd page\n");
1048 		return;
1049 	}
1050 
1051 	rsvd_pkt->rtwvif = rtwvif;
1052 	rsvd_pkt->ssid = ssid;
1053 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1054 }
1055 
1056 void rtw_remove_rsvd_page(struct rtw_dev *rtwdev,
1057 			  struct rtw_vif *rtwvif)
1058 {
1059 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1060 
1061 	lockdep_assert_held(&rtwdev->mutex);
1062 
1063 	/* remove all of the rsvd pages for vif */
1064 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwvif->rsvd_page_list,
1065 				 vif_list) {
1066 		list_del(&rsvd_pkt->vif_list);
1067 		if (!list_empty(&rsvd_pkt->build_list))
1068 			list_del(&rsvd_pkt->build_list);
1069 		kfree(rsvd_pkt);
1070 	}
1071 }
1072 
1073 void rtw_add_rsvd_page_bcn(struct rtw_dev *rtwdev,
1074 			   struct rtw_vif *rtwvif)
1075 {
1076 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1077 
1078 	if (vif->type != NL80211_IFTYPE_AP &&
1079 	    vif->type != NL80211_IFTYPE_ADHOC &&
1080 	    vif->type != NL80211_IFTYPE_MESH_POINT) {
1081 		rtw_warn(rtwdev, "Cannot add beacon rsvd page for %d\n",
1082 			 vif->type);
1083 		return;
1084 	}
1085 
1086 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_BEACON, false);
1087 }
1088 
1089 void rtw_add_rsvd_page_pno(struct rtw_dev *rtwdev,
1090 			   struct rtw_vif *rtwvif)
1091 {
1092 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1093 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
1094 	struct rtw_pno_request *rtw_pno_req = &rtw_wow->pno_req;
1095 	struct cfg80211_ssid *ssid;
1096 	int i;
1097 
1098 	if (vif->type != NL80211_IFTYPE_STATION) {
1099 		rtw_warn(rtwdev, "Cannot add PNO rsvd page for %d\n",
1100 			 vif->type);
1101 		return;
1102 	}
1103 
1104 	for (i = 0 ; i < rtw_pno_req->match_set_cnt; i++) {
1105 		ssid = &rtw_pno_req->match_sets[i].ssid;
1106 		rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, ssid);
1107 	}
1108 
1109 	rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, NULL);
1110 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NLO_INFO, false);
1111 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_CH_INFO, true);
1112 }
1113 
1114 void rtw_add_rsvd_page_sta(struct rtw_dev *rtwdev,
1115 			   struct rtw_vif *rtwvif)
1116 {
1117 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1118 
1119 	if (vif->type != NL80211_IFTYPE_STATION) {
1120 		rtw_warn(rtwdev, "Cannot add sta rsvd page for %d\n",
1121 			 vif->type);
1122 		return;
1123 	}
1124 
1125 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_PS_POLL, true);
1126 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_QOS_NULL, true);
1127 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NULL, true);
1128 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_DPK, true);
1129 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_INFO, true);
1130 }
1131 
1132 int rtw_fw_write_data_rsvd_page(struct rtw_dev *rtwdev, u16 pg_addr,
1133 				u8 *buf, u32 size)
1134 {
1135 	u8 bckp[2];
1136 	u8 val;
1137 	u16 rsvd_pg_head;
1138 	u32 bcn_valid_addr;
1139 	u32 bcn_valid_mask;
1140 	int ret;
1141 
1142 	lockdep_assert_held(&rtwdev->mutex);
1143 
1144 	if (!size)
1145 		return -EINVAL;
1146 
1147 	if (rtw_chip_wcpu_11n(rtwdev)) {
1148 		rtw_write32_set(rtwdev, REG_DWBCN0_CTRL, BIT_BCN_VALID);
1149 	} else {
1150 		pg_addr &= BIT_MASK_BCN_HEAD_1_V1;
1151 		pg_addr |= BIT_BCN_VALID_V1;
1152 		rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2, pg_addr);
1153 	}
1154 
1155 	val = rtw_read8(rtwdev, REG_CR + 1);
1156 	bckp[0] = val;
1157 	val |= BIT_ENSWBCN >> 8;
1158 	rtw_write8(rtwdev, REG_CR + 1, val);
1159 
1160 	val = rtw_read8(rtwdev, REG_FWHW_TXQ_CTRL + 2);
1161 	bckp[1] = val;
1162 	val &= ~(BIT_EN_BCNQ_DL >> 16);
1163 	rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, val);
1164 
1165 	ret = rtw_hci_write_data_rsvd_page(rtwdev, buf, size);
1166 	if (ret) {
1167 		rtw_err(rtwdev, "failed to write data to rsvd page\n");
1168 		goto restore;
1169 	}
1170 
1171 	if (rtw_chip_wcpu_11n(rtwdev)) {
1172 		bcn_valid_addr = REG_DWBCN0_CTRL;
1173 		bcn_valid_mask = BIT_BCN_VALID;
1174 	} else {
1175 		bcn_valid_addr = REG_FIFOPAGE_CTRL_2;
1176 		bcn_valid_mask = BIT_BCN_VALID_V1;
1177 	}
1178 
1179 	if (!check_hw_ready(rtwdev, bcn_valid_addr, bcn_valid_mask, 1)) {
1180 		rtw_err(rtwdev, "error beacon valid\n");
1181 		ret = -EBUSY;
1182 	}
1183 
1184 restore:
1185 	rsvd_pg_head = rtwdev->fifo.rsvd_boundary;
1186 	rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2,
1187 		    rsvd_pg_head | BIT_BCN_VALID_V1);
1188 	rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, bckp[1]);
1189 	rtw_write8(rtwdev, REG_CR + 1, bckp[0]);
1190 
1191 	return ret;
1192 }
1193 
1194 static int rtw_download_drv_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, u32 size)
1195 {
1196 	u32 pg_size;
1197 	u32 pg_num = 0;
1198 	u16 pg_addr = 0;
1199 
1200 	pg_size = rtwdev->chip->page_size;
1201 	pg_num = size / pg_size + ((size & (pg_size - 1)) ? 1 : 0);
1202 	if (pg_num > rtwdev->fifo.rsvd_drv_pg_num)
1203 		return -ENOMEM;
1204 
1205 	pg_addr = rtwdev->fifo.rsvd_drv_addr;
1206 
1207 	return rtw_fw_write_data_rsvd_page(rtwdev, pg_addr, buf, size);
1208 }
1209 
1210 static void __rtw_build_rsvd_page_reset(struct rtw_dev *rtwdev)
1211 {
1212 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1213 
1214 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwdev->rsvd_page_list,
1215 				 build_list) {
1216 		list_del_init(&rsvd_pkt->build_list);
1217 
1218 		/* Don't free except for the dummy rsvd page,
1219 		 * others will be freed when removing vif
1220 		 */
1221 		if (rsvd_pkt->type == RSVD_DUMMY)
1222 			kfree(rsvd_pkt);
1223 	}
1224 }
1225 
1226 static void rtw_build_rsvd_page_iter(void *data, u8 *mac,
1227 				     struct ieee80211_vif *vif)
1228 {
1229 	struct rtw_dev *rtwdev = data;
1230 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
1231 	struct rtw_rsvd_page *rsvd_pkt;
1232 
1233 	list_for_each_entry(rsvd_pkt, &rtwvif->rsvd_page_list, vif_list) {
1234 		if (rsvd_pkt->type == RSVD_BEACON)
1235 			list_add(&rsvd_pkt->build_list,
1236 				 &rtwdev->rsvd_page_list);
1237 		else
1238 			list_add_tail(&rsvd_pkt->build_list,
1239 				      &rtwdev->rsvd_page_list);
1240 	}
1241 }
1242 
1243 static int  __rtw_build_rsvd_page_from_vifs(struct rtw_dev *rtwdev)
1244 {
1245 	struct rtw_rsvd_page *rsvd_pkt;
1246 
1247 	__rtw_build_rsvd_page_reset(rtwdev);
1248 
1249 	/* gather rsvd page from vifs */
1250 	rtw_iterate_vifs_atomic(rtwdev, rtw_build_rsvd_page_iter, rtwdev);
1251 
1252 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
1253 					    struct rtw_rsvd_page, build_list);
1254 	if (!rsvd_pkt) {
1255 		WARN(1, "Should not have an empty reserved page\n");
1256 		return -EINVAL;
1257 	}
1258 
1259 	/* the first rsvd should be beacon, otherwise add a dummy one */
1260 	if (rsvd_pkt->type != RSVD_BEACON) {
1261 		struct rtw_rsvd_page *dummy_pkt;
1262 
1263 		dummy_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_DUMMY, false);
1264 		if (!dummy_pkt) {
1265 			rtw_err(rtwdev, "failed to alloc dummy rsvd page\n");
1266 			return -ENOMEM;
1267 		}
1268 
1269 		list_add(&dummy_pkt->build_list, &rtwdev->rsvd_page_list);
1270 	}
1271 
1272 	return 0;
1273 }
1274 
1275 static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size)
1276 {
1277 	struct ieee80211_hw *hw = rtwdev->hw;
1278 	struct rtw_chip_info *chip = rtwdev->chip;
1279 	struct sk_buff *iter;
1280 	struct rtw_rsvd_page *rsvd_pkt;
1281 	u32 page = 0;
1282 	u8 total_page = 0;
1283 	u8 page_size, page_margin, tx_desc_sz;
1284 	u8 *buf;
1285 	int ret;
1286 
1287 	page_size = chip->page_size;
1288 	tx_desc_sz = chip->tx_pkt_desc_sz;
1289 	page_margin = page_size - tx_desc_sz;
1290 
1291 	ret = __rtw_build_rsvd_page_from_vifs(rtwdev);
1292 	if (ret) {
1293 		rtw_err(rtwdev,
1294 			"failed to build rsvd page from vifs, ret %d\n", ret);
1295 		return NULL;
1296 	}
1297 
1298 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1299 		iter = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1300 		if (!iter) {
1301 			rtw_err(rtwdev, "failed to build rsvd packet\n");
1302 			goto release_skb;
1303 		}
1304 
1305 		/* Fill the tx_desc for the rsvd pkt that requires one.
1306 		 * And iter->len will be added with size of tx_desc_sz.
1307 		 */
1308 		if (rsvd_pkt->add_txdesc)
1309 			rtw_fill_rsvd_page_desc(rtwdev, iter, rsvd_pkt->type);
1310 
1311 		rsvd_pkt->skb = iter;
1312 		rsvd_pkt->page = total_page;
1313 
1314 		/* Reserved page is downloaded via TX path, and TX path will
1315 		 * generate a tx_desc at the header to describe length of
1316 		 * the buffer. If we are not counting page numbers with the
1317 		 * size of tx_desc added at the first rsvd_pkt (usually a
1318 		 * beacon, firmware default refer to the first page as the
1319 		 * content of beacon), we could generate a buffer which size
1320 		 * is smaller than the actual size of the whole rsvd_page
1321 		 */
1322 		if (total_page == 0) {
1323 			if (rsvd_pkt->type != RSVD_BEACON &&
1324 			    rsvd_pkt->type != RSVD_DUMMY) {
1325 				rtw_err(rtwdev, "first page should be a beacon\n");
1326 				goto release_skb;
1327 			}
1328 			total_page += rtw_len_to_page(iter->len + tx_desc_sz,
1329 						      page_size);
1330 		} else {
1331 			total_page += rtw_len_to_page(iter->len, page_size);
1332 		}
1333 	}
1334 
1335 	if (total_page > rtwdev->fifo.rsvd_drv_pg_num) {
1336 		rtw_err(rtwdev, "rsvd page over size: %d\n", total_page);
1337 		goto release_skb;
1338 	}
1339 
1340 	*size = (total_page - 1) * page_size + page_margin;
1341 	buf = kzalloc(*size, GFP_KERNEL);
1342 	if (!buf)
1343 		goto release_skb;
1344 
1345 	/* Copy the content of each rsvd_pkt to the buf, and they should
1346 	 * be aligned to the pages.
1347 	 *
1348 	 * Note that the first rsvd_pkt is a beacon no matter what vif->type.
1349 	 * And that rsvd_pkt does not require tx_desc because when it goes
1350 	 * through TX path, the TX path will generate one for it.
1351 	 */
1352 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1353 		rtw_rsvd_page_list_to_buf(rtwdev, page_size, page_margin,
1354 					  page, buf, rsvd_pkt);
1355 		if (page == 0)
1356 			page += rtw_len_to_page(rsvd_pkt->skb->len +
1357 						tx_desc_sz, page_size);
1358 		else
1359 			page += rtw_len_to_page(rsvd_pkt->skb->len, page_size);
1360 
1361 		kfree_skb(rsvd_pkt->skb);
1362 		rsvd_pkt->skb = NULL;
1363 	}
1364 
1365 	return buf;
1366 
1367 release_skb:
1368 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1369 		kfree_skb(rsvd_pkt->skb);
1370 		rsvd_pkt->skb = NULL;
1371 	}
1372 
1373 	return NULL;
1374 }
1375 
1376 static int rtw_download_beacon(struct rtw_dev *rtwdev)
1377 {
1378 	struct ieee80211_hw *hw = rtwdev->hw;
1379 	struct rtw_rsvd_page *rsvd_pkt;
1380 	struct sk_buff *skb;
1381 	int ret = 0;
1382 
1383 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
1384 					    struct rtw_rsvd_page, build_list);
1385 	if (!rsvd_pkt) {
1386 		rtw_err(rtwdev, "failed to get rsvd page from build list\n");
1387 		return -ENOENT;
1388 	}
1389 
1390 	if (rsvd_pkt->type != RSVD_BEACON &&
1391 	    rsvd_pkt->type != RSVD_DUMMY) {
1392 		rtw_err(rtwdev, "invalid rsvd page type %d, should be beacon or dummy\n",
1393 			rsvd_pkt->type);
1394 		return -EINVAL;
1395 	}
1396 
1397 	skb = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1398 	if (!skb) {
1399 		rtw_err(rtwdev, "failed to get beacon skb\n");
1400 		return -ENOMEM;
1401 	}
1402 
1403 	ret = rtw_download_drv_rsvd_page(rtwdev, skb->data, skb->len);
1404 	if (ret)
1405 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1406 
1407 	dev_kfree_skb(skb);
1408 
1409 	return ret;
1410 }
1411 
1412 int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev)
1413 {
1414 	u8 *buf;
1415 	u32 size;
1416 	int ret;
1417 
1418 	buf = rtw_build_rsvd_page(rtwdev, &size);
1419 	if (!buf) {
1420 		rtw_err(rtwdev, "failed to build rsvd page pkt\n");
1421 		return -ENOMEM;
1422 	}
1423 
1424 	ret = rtw_download_drv_rsvd_page(rtwdev, buf, size);
1425 	if (ret) {
1426 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1427 		goto free;
1428 	}
1429 
1430 	/* The last thing is to download the *ONLY* beacon again, because
1431 	 * the previous tx_desc is to describe the total rsvd page. Download
1432 	 * the beacon again to replace the TX desc header, and we will get
1433 	 * a correct tx_desc for the beacon in the rsvd page.
1434 	 */
1435 	ret = rtw_download_beacon(rtwdev);
1436 	if (ret) {
1437 		rtw_err(rtwdev, "failed to download beacon\n");
1438 		goto free;
1439 	}
1440 
1441 free:
1442 	kfree(buf);
1443 
1444 	return ret;
1445 }
1446 
1447 static void rtw_fw_read_fifo_page(struct rtw_dev *rtwdev, u32 offset, u32 size,
1448 				  u32 *buf, u32 residue, u16 start_pg)
1449 {
1450 	u32 i;
1451 	u16 idx = 0;
1452 	u16 ctl;
1453 	u8 rcr;
1454 
1455 	rcr = rtw_read8(rtwdev, REG_RCR + 2);
1456 	ctl = rtw_read16(rtwdev, REG_PKTBUF_DBG_CTRL) & 0xf000;
1457 	/* disable rx clock gate */
1458 	rtw_write8(rtwdev, REG_RCR, rcr | BIT(3));
1459 
1460 	do {
1461 		rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, start_pg | ctl);
1462 
1463 		for (i = FIFO_DUMP_ADDR + residue;
1464 		     i < FIFO_DUMP_ADDR + FIFO_PAGE_SIZE; i += 4) {
1465 			buf[idx++] = rtw_read32(rtwdev, i);
1466 			size -= 4;
1467 			if (size == 0)
1468 				goto out;
1469 		}
1470 
1471 		residue = 0;
1472 		start_pg++;
1473 	} while (size);
1474 
1475 out:
1476 	rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, ctl);
1477 	rtw_write8(rtwdev, REG_RCR + 2, rcr);
1478 }
1479 
1480 static void rtw_fw_read_fifo(struct rtw_dev *rtwdev, enum rtw_fw_fifo_sel sel,
1481 			     u32 offset, u32 size, u32 *buf)
1482 {
1483 	struct rtw_chip_info *chip = rtwdev->chip;
1484 	u32 start_pg, residue;
1485 
1486 	if (sel >= RTW_FW_FIFO_MAX) {
1487 		rtw_dbg(rtwdev, RTW_DBG_FW, "wrong fw fifo sel\n");
1488 		return;
1489 	}
1490 	if (sel == RTW_FW_FIFO_SEL_RSVD_PAGE)
1491 		offset += rtwdev->fifo.rsvd_boundary << TX_PAGE_SIZE_SHIFT;
1492 	residue = offset & (FIFO_PAGE_SIZE - 1);
1493 	start_pg = (offset >> FIFO_PAGE_SIZE_SHIFT) + chip->fw_fifo_addr[sel];
1494 
1495 	rtw_fw_read_fifo_page(rtwdev, offset, size, buf, residue, start_pg);
1496 }
1497 
1498 static bool rtw_fw_dump_check_size(struct rtw_dev *rtwdev,
1499 				   enum rtw_fw_fifo_sel sel,
1500 				   u32 start_addr, u32 size)
1501 {
1502 	switch (sel) {
1503 	case RTW_FW_FIFO_SEL_TX:
1504 	case RTW_FW_FIFO_SEL_RX:
1505 		if ((start_addr + size) > rtwdev->chip->fw_fifo_addr[sel])
1506 			return false;
1507 		fallthrough;
1508 	default:
1509 		return true;
1510 	}
1511 }
1512 
1513 int rtw_fw_dump_fifo(struct rtw_dev *rtwdev, u8 fifo_sel, u32 addr, u32 size,
1514 		     u32 *buffer)
1515 {
1516 	if (!rtwdev->chip->fw_fifo_addr[0]) {
1517 		rtw_dbg(rtwdev, RTW_DBG_FW, "chip not support dump fw fifo\n");
1518 		return -ENOTSUPP;
1519 	}
1520 
1521 	if (size == 0 || !buffer)
1522 		return -EINVAL;
1523 
1524 	if (size & 0x3) {
1525 		rtw_dbg(rtwdev, RTW_DBG_FW, "not 4byte alignment\n");
1526 		return -EINVAL;
1527 	}
1528 
1529 	if (!rtw_fw_dump_check_size(rtwdev, fifo_sel, addr, size)) {
1530 		rtw_dbg(rtwdev, RTW_DBG_FW, "fw fifo dump size overflow\n");
1531 		return -EINVAL;
1532 	}
1533 
1534 	rtw_fw_read_fifo(rtwdev, fifo_sel, addr, size, buffer);
1535 
1536 	return 0;
1537 }
1538 
1539 static void __rtw_fw_update_pkt(struct rtw_dev *rtwdev, u8 pkt_id, u16 size,
1540 				u8 location)
1541 {
1542 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1543 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_UPDATE_PKT_LEN;
1544 
1545 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_UPDATE_PKT);
1546 
1547 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1548 	UPDATE_PKT_SET_PKT_ID(h2c_pkt, pkt_id);
1549 	UPDATE_PKT_SET_LOCATION(h2c_pkt, location);
1550 
1551 	/* include txdesc size */
1552 	UPDATE_PKT_SET_SIZE(h2c_pkt, size);
1553 
1554 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1555 }
1556 
1557 void rtw_fw_update_pkt_probe_req(struct rtw_dev *rtwdev,
1558 				 struct cfg80211_ssid *ssid)
1559 {
1560 	u8 loc;
1561 	u32 size;
1562 
1563 	loc = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid);
1564 	if (!loc) {
1565 		rtw_err(rtwdev, "failed to get probe_req rsvd loc\n");
1566 		return;
1567 	}
1568 
1569 	size = rtw_get_rsvd_page_probe_req_size(rtwdev, ssid);
1570 	if (!size) {
1571 		rtw_err(rtwdev, "failed to get probe_req rsvd size\n");
1572 		return;
1573 	}
1574 
1575 	__rtw_fw_update_pkt(rtwdev, RTW_PACKET_PROBE_REQ, size, loc);
1576 }
1577 
1578 void rtw_fw_channel_switch(struct rtw_dev *rtwdev, bool enable)
1579 {
1580 	struct rtw_pno_request *rtw_pno_req = &rtwdev->wow.pno_req;
1581 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1582 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_CH_SWITCH_LEN;
1583 	u8 loc_ch_info;
1584 	const struct rtw_ch_switch_option cs_option = {
1585 		.dest_ch_en = 1,
1586 		.dest_ch = 1,
1587 		.periodic_option = 2,
1588 		.normal_period = 5,
1589 		.normal_period_sel = 0,
1590 		.normal_cycle = 10,
1591 		.slow_period = 1,
1592 		.slow_period_sel = 1,
1593 	};
1594 
1595 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_CH_SWITCH);
1596 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1597 
1598 	CH_SWITCH_SET_START(h2c_pkt, enable);
1599 	CH_SWITCH_SET_DEST_CH_EN(h2c_pkt, cs_option.dest_ch_en);
1600 	CH_SWITCH_SET_DEST_CH(h2c_pkt, cs_option.dest_ch);
1601 	CH_SWITCH_SET_NORMAL_PERIOD(h2c_pkt, cs_option.normal_period);
1602 	CH_SWITCH_SET_NORMAL_PERIOD_SEL(h2c_pkt, cs_option.normal_period_sel);
1603 	CH_SWITCH_SET_SLOW_PERIOD(h2c_pkt, cs_option.slow_period);
1604 	CH_SWITCH_SET_SLOW_PERIOD_SEL(h2c_pkt, cs_option.slow_period_sel);
1605 	CH_SWITCH_SET_NORMAL_CYCLE(h2c_pkt, cs_option.normal_cycle);
1606 	CH_SWITCH_SET_PERIODIC_OPT(h2c_pkt, cs_option.periodic_option);
1607 
1608 	CH_SWITCH_SET_CH_NUM(h2c_pkt, rtw_pno_req->channel_cnt);
1609 	CH_SWITCH_SET_INFO_SIZE(h2c_pkt, rtw_pno_req->channel_cnt * 4);
1610 
1611 	loc_ch_info = rtw_get_rsvd_page_location(rtwdev, RSVD_CH_INFO);
1612 	CH_SWITCH_SET_INFO_LOC(h2c_pkt, loc_ch_info);
1613 
1614 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1615 }
1616