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_query_bt_info(struct rtw_dev *rtwdev)
354 {
355 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
356 
357 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_INFO);
358 
359 	SET_QUERY_BT_INFO(h2c_pkt, true);
360 
361 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
362 }
363 
364 void rtw_fw_wl_ch_info(struct rtw_dev *rtwdev, u8 link, u8 ch, u8 bw)
365 {
366 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
367 
368 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WL_CH_INFO);
369 
370 	SET_WL_CH_INFO_LINK(h2c_pkt, link);
371 	SET_WL_CH_INFO_CHNL(h2c_pkt, ch);
372 	SET_WL_CH_INFO_BW(h2c_pkt, bw);
373 
374 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
375 }
376 
377 void rtw_fw_query_bt_mp_info(struct rtw_dev *rtwdev,
378 			     struct rtw_coex_info_req *req)
379 {
380 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
381 
382 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_MP_INFO);
383 
384 	SET_BT_MP_INFO_SEQ(h2c_pkt, req->seq);
385 	SET_BT_MP_INFO_OP_CODE(h2c_pkt, req->op_code);
386 	SET_BT_MP_INFO_PARA1(h2c_pkt, req->para1);
387 	SET_BT_MP_INFO_PARA2(h2c_pkt, req->para2);
388 	SET_BT_MP_INFO_PARA3(h2c_pkt, req->para3);
389 
390 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
391 }
392 
393 void rtw_fw_force_bt_tx_power(struct rtw_dev *rtwdev, u8 bt_pwr_dec_lvl)
394 {
395 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
396 	u8 index = 0 - bt_pwr_dec_lvl;
397 
398 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_FORCE_BT_TX_POWER);
399 
400 	SET_BT_TX_POWER_INDEX(h2c_pkt, index);
401 
402 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
403 }
404 
405 void rtw_fw_bt_ignore_wlan_action(struct rtw_dev *rtwdev, bool enable)
406 {
407 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
408 
409 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_IGNORE_WLAN_ACTION);
410 
411 	SET_IGNORE_WLAN_ACTION_EN(h2c_pkt, enable);
412 
413 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
414 }
415 
416 void rtw_fw_coex_tdma_type(struct rtw_dev *rtwdev,
417 			   u8 para1, u8 para2, u8 para3, u8 para4, u8 para5)
418 {
419 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
420 
421 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_COEX_TDMA_TYPE);
422 
423 	SET_COEX_TDMA_TYPE_PARA1(h2c_pkt, para1);
424 	SET_COEX_TDMA_TYPE_PARA2(h2c_pkt, para2);
425 	SET_COEX_TDMA_TYPE_PARA3(h2c_pkt, para3);
426 	SET_COEX_TDMA_TYPE_PARA4(h2c_pkt, para4);
427 	SET_COEX_TDMA_TYPE_PARA5(h2c_pkt, para5);
428 
429 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
430 }
431 
432 void rtw_fw_bt_wifi_control(struct rtw_dev *rtwdev, u8 op_code, u8 *data)
433 {
434 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
435 
436 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BT_WIFI_CONTROL);
437 
438 	SET_BT_WIFI_CONTROL_OP_CODE(h2c_pkt, op_code);
439 
440 	SET_BT_WIFI_CONTROL_DATA1(h2c_pkt, *data);
441 	SET_BT_WIFI_CONTROL_DATA2(h2c_pkt, *(data + 1));
442 	SET_BT_WIFI_CONTROL_DATA3(h2c_pkt, *(data + 2));
443 	SET_BT_WIFI_CONTROL_DATA4(h2c_pkt, *(data + 3));
444 	SET_BT_WIFI_CONTROL_DATA5(h2c_pkt, *(data + 4));
445 
446 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
447 }
448 
449 void rtw_fw_send_rssi_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si)
450 {
451 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
452 	u8 rssi = ewma_rssi_read(&si->avg_rssi);
453 	bool stbc_en = si->stbc_en ? true : false;
454 
455 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSSI_MONITOR);
456 
457 	SET_RSSI_INFO_MACID(h2c_pkt, si->mac_id);
458 	SET_RSSI_INFO_RSSI(h2c_pkt, rssi);
459 	SET_RSSI_INFO_STBC(h2c_pkt, stbc_en);
460 
461 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
462 }
463 
464 void rtw_fw_send_ra_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si)
465 {
466 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
467 	bool no_update = si->updated;
468 	bool disable_pt = true;
469 
470 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RA_INFO);
471 
472 	SET_RA_INFO_MACID(h2c_pkt, si->mac_id);
473 	SET_RA_INFO_RATE_ID(h2c_pkt, si->rate_id);
474 	SET_RA_INFO_INIT_RA_LVL(h2c_pkt, si->init_ra_lv);
475 	SET_RA_INFO_SGI_EN(h2c_pkt, si->sgi_enable);
476 	SET_RA_INFO_BW_MODE(h2c_pkt, si->bw_mode);
477 	SET_RA_INFO_LDPC(h2c_pkt, !!si->ldpc_en);
478 	SET_RA_INFO_NO_UPDATE(h2c_pkt, no_update);
479 	SET_RA_INFO_VHT_EN(h2c_pkt, si->vht_enable);
480 	SET_RA_INFO_DIS_PT(h2c_pkt, disable_pt);
481 	SET_RA_INFO_RA_MASK0(h2c_pkt, (si->ra_mask & 0xff));
482 	SET_RA_INFO_RA_MASK1(h2c_pkt, (si->ra_mask & 0xff00) >> 8);
483 	SET_RA_INFO_RA_MASK2(h2c_pkt, (si->ra_mask & 0xff0000) >> 16);
484 	SET_RA_INFO_RA_MASK3(h2c_pkt, (si->ra_mask & 0xff000000) >> 24);
485 
486 	si->init_ra_lv = 0;
487 	si->updated = true;
488 
489 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
490 }
491 
492 void rtw_fw_media_status_report(struct rtw_dev *rtwdev, u8 mac_id, bool connect)
493 {
494 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
495 
496 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_MEDIA_STATUS_RPT);
497 	MEDIA_STATUS_RPT_SET_OP_MODE(h2c_pkt, connect);
498 	MEDIA_STATUS_RPT_SET_MACID(h2c_pkt, mac_id);
499 
500 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
501 }
502 
503 void rtw_fw_set_pwr_mode(struct rtw_dev *rtwdev)
504 {
505 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
506 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
507 
508 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_SET_PWR_MODE);
509 
510 	SET_PWR_MODE_SET_MODE(h2c_pkt, conf->mode);
511 	SET_PWR_MODE_SET_RLBM(h2c_pkt, conf->rlbm);
512 	SET_PWR_MODE_SET_SMART_PS(h2c_pkt, conf->smart_ps);
513 	SET_PWR_MODE_SET_AWAKE_INTERVAL(h2c_pkt, conf->awake_interval);
514 	SET_PWR_MODE_SET_PORT_ID(h2c_pkt, conf->port_id);
515 	SET_PWR_MODE_SET_PWR_STATE(h2c_pkt, conf->state);
516 
517 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
518 }
519 
520 void rtw_fw_set_keep_alive_cmd(struct rtw_dev *rtwdev, bool enable)
521 {
522 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
523 	struct rtw_fw_wow_keep_alive_para mode = {
524 		.adopt = true,
525 		.pkt_type = KEEP_ALIVE_NULL_PKT,
526 		.period = 5,
527 	};
528 
529 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_KEEP_ALIVE);
530 	SET_KEEP_ALIVE_ENABLE(h2c_pkt, enable);
531 	SET_KEEP_ALIVE_ADOPT(h2c_pkt, mode.adopt);
532 	SET_KEEP_ALIVE_PKT_TYPE(h2c_pkt, mode.pkt_type);
533 	SET_KEEP_ALIVE_CHECK_PERIOD(h2c_pkt, mode.period);
534 
535 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
536 }
537 
538 void rtw_fw_set_disconnect_decision_cmd(struct rtw_dev *rtwdev, bool enable)
539 {
540 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
541 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
542 	struct rtw_fw_wow_disconnect_para mode = {
543 		.adopt = true,
544 		.period = 30,
545 		.retry_count = 5,
546 	};
547 
548 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_DISCONNECT_DECISION);
549 
550 	if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags)) {
551 		SET_DISCONNECT_DECISION_ENABLE(h2c_pkt, enable);
552 		SET_DISCONNECT_DECISION_ADOPT(h2c_pkt, mode.adopt);
553 		SET_DISCONNECT_DECISION_CHECK_PERIOD(h2c_pkt, mode.period);
554 		SET_DISCONNECT_DECISION_TRY_PKT_NUM(h2c_pkt, mode.retry_count);
555 	}
556 
557 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
558 }
559 
560 void rtw_fw_set_wowlan_ctrl_cmd(struct rtw_dev *rtwdev, bool enable)
561 {
562 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
563 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
564 
565 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WOWLAN);
566 
567 	SET_WOWLAN_FUNC_ENABLE(h2c_pkt, enable);
568 	if (rtw_wow_mgd_linked(rtwdev)) {
569 		if (test_bit(RTW_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags))
570 			SET_WOWLAN_MAGIC_PKT_ENABLE(h2c_pkt, enable);
571 		if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags))
572 			SET_WOWLAN_DEAUTH_WAKEUP_ENABLE(h2c_pkt, enable);
573 		if (test_bit(RTW_WOW_FLAG_EN_REKEY_PKT, rtw_wow->flags))
574 			SET_WOWLAN_REKEY_WAKEUP_ENABLE(h2c_pkt, enable);
575 		if (rtw_wow->pattern_cnt)
576 			SET_WOWLAN_PATTERN_MATCH_ENABLE(h2c_pkt, enable);
577 	}
578 
579 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
580 }
581 
582 void rtw_fw_set_aoac_global_info_cmd(struct rtw_dev *rtwdev,
583 				     u8 pairwise_key_enc,
584 				     u8 group_key_enc)
585 {
586 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
587 
588 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_AOAC_GLOBAL_INFO);
589 
590 	SET_AOAC_GLOBAL_INFO_PAIRWISE_ENC_ALG(h2c_pkt, pairwise_key_enc);
591 	SET_AOAC_GLOBAL_INFO_GROUP_ENC_ALG(h2c_pkt, group_key_enc);
592 
593 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
594 }
595 
596 void rtw_fw_set_remote_wake_ctrl_cmd(struct rtw_dev *rtwdev, bool enable)
597 {
598 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
599 
600 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_REMOTE_WAKE_CTRL);
601 
602 	SET_REMOTE_WAKECTRL_ENABLE(h2c_pkt, enable);
603 
604 	if (rtw_wow_no_link(rtwdev))
605 		SET_REMOTE_WAKE_CTRL_NLO_OFFLOAD_EN(h2c_pkt, enable);
606 
607 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
608 }
609 
610 static u8 rtw_get_rsvd_page_location(struct rtw_dev *rtwdev,
611 				     enum rtw_rsvd_packet_type type)
612 {
613 	struct rtw_rsvd_page *rsvd_pkt;
614 	u8 location = 0;
615 
616 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
617 		if (type == rsvd_pkt->type)
618 			location = rsvd_pkt->page;
619 	}
620 
621 	return location;
622 }
623 
624 void rtw_fw_set_nlo_info(struct rtw_dev *rtwdev, bool enable)
625 {
626 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
627 	u8 loc_nlo;
628 
629 	loc_nlo = rtw_get_rsvd_page_location(rtwdev, RSVD_NLO_INFO);
630 
631 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_NLO_INFO);
632 
633 	SET_NLO_FUN_EN(h2c_pkt, enable);
634 	if (enable) {
635 		if (rtw_get_lps_deep_mode(rtwdev) != LPS_DEEP_MODE_NONE)
636 			SET_NLO_PS_32K(h2c_pkt, enable);
637 		SET_NLO_IGNORE_SECURITY(h2c_pkt, enable);
638 		SET_NLO_LOC_NLO_INFO(h2c_pkt, loc_nlo);
639 	}
640 
641 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
642 }
643 
644 void rtw_fw_set_pg_info(struct rtw_dev *rtwdev)
645 {
646 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
647 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
648 	u8 loc_pg, loc_dpk;
649 
650 	loc_pg = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_INFO);
651 	loc_dpk = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_DPK);
652 
653 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_LPS_PG_INFO);
654 
655 	LPS_PG_INFO_LOC(h2c_pkt, loc_pg);
656 	LPS_PG_DPK_LOC(h2c_pkt, loc_dpk);
657 	LPS_PG_SEC_CAM_EN(h2c_pkt, conf->sec_cam_backup);
658 	LPS_PG_PATTERN_CAM_EN(h2c_pkt, conf->pattern_cam_backup);
659 
660 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
661 }
662 
663 static u8 rtw_get_rsvd_page_probe_req_location(struct rtw_dev *rtwdev,
664 					       struct cfg80211_ssid *ssid)
665 {
666 	struct rtw_rsvd_page *rsvd_pkt;
667 	u8 location = 0;
668 
669 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
670 		if (rsvd_pkt->type != RSVD_PROBE_REQ)
671 			continue;
672 		if ((!ssid && !rsvd_pkt->ssid) ||
673 		    rtw_ssid_equal(rsvd_pkt->ssid, ssid))
674 			location = rsvd_pkt->page;
675 	}
676 
677 	return location;
678 }
679 
680 static u16 rtw_get_rsvd_page_probe_req_size(struct rtw_dev *rtwdev,
681 					    struct cfg80211_ssid *ssid)
682 {
683 	struct rtw_rsvd_page *rsvd_pkt;
684 	u16 size = 0;
685 
686 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
687 		if (rsvd_pkt->type != RSVD_PROBE_REQ)
688 			continue;
689 		if ((!ssid && !rsvd_pkt->ssid) ||
690 		    rtw_ssid_equal(rsvd_pkt->ssid, ssid))
691 			size = rsvd_pkt->skb->len;
692 	}
693 
694 	return size;
695 }
696 
697 void rtw_send_rsvd_page_h2c(struct rtw_dev *rtwdev)
698 {
699 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
700 	u8 location = 0;
701 
702 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSVD_PAGE);
703 
704 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_PROBE_RESP);
705 	*(h2c_pkt + 1) = location;
706 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PROBE_RESP loc: %d\n", location);
707 
708 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_PS_POLL);
709 	*(h2c_pkt + 2) = location;
710 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PS_POLL loc: %d\n", location);
711 
712 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_NULL);
713 	*(h2c_pkt + 3) = location;
714 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_NULL loc: %d\n", location);
715 
716 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_QOS_NULL);
717 	*(h2c_pkt + 4) = location;
718 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_QOS_NULL loc: %d\n", location);
719 
720 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
721 }
722 
723 static struct sk_buff *rtw_nlo_info_get(struct ieee80211_hw *hw)
724 {
725 	struct rtw_dev *rtwdev = hw->priv;
726 	struct rtw_chip_info *chip = rtwdev->chip;
727 	struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req;
728 	struct rtw_nlo_info_hdr *nlo_hdr;
729 	struct cfg80211_ssid *ssid;
730 	struct sk_buff *skb;
731 	u8 *pos, loc;
732 	u32 size;
733 	int i;
734 
735 	if (!pno_req->inited || !pno_req->match_set_cnt)
736 		return NULL;
737 
738 	size = sizeof(struct rtw_nlo_info_hdr) + pno_req->match_set_cnt *
739 		      IEEE80211_MAX_SSID_LEN + chip->tx_pkt_desc_sz;
740 
741 	skb = alloc_skb(size, GFP_KERNEL);
742 	if (!skb)
743 		return NULL;
744 
745 	skb_reserve(skb, chip->tx_pkt_desc_sz);
746 
747 	nlo_hdr = skb_put_zero(skb, sizeof(struct rtw_nlo_info_hdr));
748 
749 	nlo_hdr->nlo_count = pno_req->match_set_cnt;
750 	nlo_hdr->hidden_ap_count = pno_req->match_set_cnt;
751 
752 	/* pattern check for firmware */
753 	memset(nlo_hdr->pattern_check, 0xA5, FW_NLO_INFO_CHECK_SIZE);
754 
755 	for (i = 0; i < pno_req->match_set_cnt; i++)
756 		nlo_hdr->ssid_len[i] = pno_req->match_sets[i].ssid.ssid_len;
757 
758 	for (i = 0; i < pno_req->match_set_cnt; i++) {
759 		ssid = &pno_req->match_sets[i].ssid;
760 		loc  = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid);
761 		if (!loc) {
762 			rtw_err(rtwdev, "failed to get probe req rsvd loc\n");
763 			kfree_skb(skb);
764 			return NULL;
765 		}
766 		nlo_hdr->location[i] = loc;
767 	}
768 
769 	for (i = 0; i < pno_req->match_set_cnt; i++) {
770 		pos = skb_put_zero(skb, IEEE80211_MAX_SSID_LEN);
771 		memcpy(pos, pno_req->match_sets[i].ssid.ssid,
772 		       pno_req->match_sets[i].ssid.ssid_len);
773 	}
774 
775 	return skb;
776 }
777 
778 static struct sk_buff *rtw_cs_channel_info_get(struct ieee80211_hw *hw)
779 {
780 	struct rtw_dev *rtwdev = hw->priv;
781 	struct rtw_chip_info *chip = rtwdev->chip;
782 	struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req;
783 	struct ieee80211_channel *channels = pno_req->channels;
784 	struct sk_buff *skb;
785 	int count =  pno_req->channel_cnt;
786 	u8 *pos;
787 	int i = 0;
788 
789 	skb = alloc_skb(4 * count + chip->tx_pkt_desc_sz, GFP_KERNEL);
790 	if (!skb)
791 		return NULL;
792 
793 	skb_reserve(skb, chip->tx_pkt_desc_sz);
794 
795 	for (i = 0; i < count; i++) {
796 		pos = skb_put_zero(skb, 4);
797 
798 		CHSW_INFO_SET_CH(pos, channels[i].hw_value);
799 
800 		if (channels[i].flags & IEEE80211_CHAN_RADAR)
801 			CHSW_INFO_SET_ACTION_ID(pos, 0);
802 		else
803 			CHSW_INFO_SET_ACTION_ID(pos, 1);
804 		CHSW_INFO_SET_TIMEOUT(pos, 1);
805 		CHSW_INFO_SET_PRI_CH_IDX(pos, 1);
806 		CHSW_INFO_SET_BW(pos, 0);
807 	}
808 
809 	return skb;
810 }
811 
812 static struct sk_buff *rtw_lps_pg_dpk_get(struct ieee80211_hw *hw)
813 {
814 	struct rtw_dev *rtwdev = hw->priv;
815 	struct rtw_chip_info *chip = rtwdev->chip;
816 	struct rtw_dpk_info *dpk_info = &rtwdev->dm_info.dpk_info;
817 	struct rtw_lps_pg_dpk_hdr *dpk_hdr;
818 	struct sk_buff *skb;
819 	u32 size;
820 
821 	size = chip->tx_pkt_desc_sz + sizeof(*dpk_hdr);
822 	skb = alloc_skb(size, GFP_KERNEL);
823 	if (!skb)
824 		return NULL;
825 
826 	skb_reserve(skb, chip->tx_pkt_desc_sz);
827 	dpk_hdr = skb_put_zero(skb, sizeof(*dpk_hdr));
828 	dpk_hdr->dpk_ch = dpk_info->dpk_ch;
829 	dpk_hdr->dpk_path_ok = dpk_info->dpk_path_ok[0];
830 	memcpy(dpk_hdr->dpk_txagc, dpk_info->dpk_txagc, 2);
831 	memcpy(dpk_hdr->dpk_gs, dpk_info->dpk_gs, 4);
832 	memcpy(dpk_hdr->coef, dpk_info->coef, 160);
833 
834 	return skb;
835 }
836 
837 static struct sk_buff *rtw_lps_pg_info_get(struct ieee80211_hw *hw)
838 {
839 	struct rtw_dev *rtwdev = hw->priv;
840 	struct rtw_chip_info *chip = rtwdev->chip;
841 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
842 	struct rtw_lps_pg_info_hdr *pg_info_hdr;
843 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
844 	struct sk_buff *skb;
845 	u32 size;
846 
847 	size = chip->tx_pkt_desc_sz + sizeof(*pg_info_hdr);
848 	skb = alloc_skb(size, GFP_KERNEL);
849 	if (!skb)
850 		return NULL;
851 
852 	skb_reserve(skb, chip->tx_pkt_desc_sz);
853 	pg_info_hdr = skb_put_zero(skb, sizeof(*pg_info_hdr));
854 	pg_info_hdr->tx_bu_page_count = rtwdev->fifo.rsvd_drv_pg_num;
855 	pg_info_hdr->macid = find_first_bit(rtwdev->mac_id_map, RTW_MAX_MAC_ID_NUM);
856 	pg_info_hdr->sec_cam_count =
857 		rtw_sec_cam_pg_backup(rtwdev, pg_info_hdr->sec_cam);
858 	pg_info_hdr->pattern_count = rtw_wow->pattern_cnt;
859 
860 	conf->sec_cam_backup = pg_info_hdr->sec_cam_count != 0;
861 	conf->pattern_cam_backup = rtw_wow->pattern_cnt != 0;
862 
863 	return skb;
864 }
865 
866 static struct sk_buff *rtw_get_rsvd_page_skb(struct ieee80211_hw *hw,
867 					     struct rtw_rsvd_page *rsvd_pkt)
868 {
869 	struct ieee80211_vif *vif;
870 	struct rtw_vif *rtwvif;
871 	struct sk_buff *skb_new;
872 	struct cfg80211_ssid *ssid;
873 
874 	if (rsvd_pkt->type == RSVD_DUMMY) {
875 		skb_new = alloc_skb(1, GFP_KERNEL);
876 		if (!skb_new)
877 			return NULL;
878 
879 		skb_put(skb_new, 1);
880 		return skb_new;
881 	}
882 
883 	rtwvif = rsvd_pkt->rtwvif;
884 	if (!rtwvif)
885 		return NULL;
886 
887 	vif = rtwvif_to_vif(rtwvif);
888 
889 	switch (rsvd_pkt->type) {
890 	case RSVD_BEACON:
891 		skb_new = ieee80211_beacon_get(hw, vif);
892 		break;
893 	case RSVD_PS_POLL:
894 		skb_new = ieee80211_pspoll_get(hw, vif);
895 		break;
896 	case RSVD_PROBE_RESP:
897 		skb_new = ieee80211_proberesp_get(hw, vif);
898 		break;
899 	case RSVD_NULL:
900 		skb_new = ieee80211_nullfunc_get(hw, vif, false);
901 		break;
902 	case RSVD_QOS_NULL:
903 		skb_new = ieee80211_nullfunc_get(hw, vif, true);
904 		break;
905 	case RSVD_LPS_PG_DPK:
906 		skb_new = rtw_lps_pg_dpk_get(hw);
907 		break;
908 	case RSVD_LPS_PG_INFO:
909 		skb_new = rtw_lps_pg_info_get(hw);
910 		break;
911 	case RSVD_PROBE_REQ:
912 		ssid = (struct cfg80211_ssid *)rsvd_pkt->ssid;
913 		if (ssid)
914 			skb_new = ieee80211_probereq_get(hw, vif->addr,
915 							 ssid->ssid,
916 							 ssid->ssid_len, 0);
917 		else
918 			skb_new = ieee80211_probereq_get(hw, vif->addr, NULL, 0, 0);
919 		break;
920 	case RSVD_NLO_INFO:
921 		skb_new = rtw_nlo_info_get(hw);
922 		break;
923 	case RSVD_CH_INFO:
924 		skb_new = rtw_cs_channel_info_get(hw);
925 		break;
926 	default:
927 		return NULL;
928 	}
929 
930 	if (!skb_new)
931 		return NULL;
932 
933 	return skb_new;
934 }
935 
936 static void rtw_fill_rsvd_page_desc(struct rtw_dev *rtwdev, struct sk_buff *skb,
937 				    enum rtw_rsvd_packet_type type)
938 {
939 	struct rtw_tx_pkt_info pkt_info = {0};
940 	struct rtw_chip_info *chip = rtwdev->chip;
941 	u8 *pkt_desc;
942 
943 	rtw_tx_rsvd_page_pkt_info_update(rtwdev, &pkt_info, skb, type);
944 	pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz);
945 	memset(pkt_desc, 0, chip->tx_pkt_desc_sz);
946 	rtw_tx_fill_tx_desc(&pkt_info, skb);
947 }
948 
949 static inline u8 rtw_len_to_page(unsigned int len, u8 page_size)
950 {
951 	return DIV_ROUND_UP(len, page_size);
952 }
953 
954 static void rtw_rsvd_page_list_to_buf(struct rtw_dev *rtwdev, u8 page_size,
955 				      u8 page_margin, u32 page, u8 *buf,
956 				      struct rtw_rsvd_page *rsvd_pkt)
957 {
958 	struct sk_buff *skb = rsvd_pkt->skb;
959 
960 	if (page >= 1)
961 		memcpy(buf + page_margin + page_size * (page - 1),
962 		       skb->data, skb->len);
963 	else
964 		memcpy(buf, skb->data, skb->len);
965 }
966 
967 static struct rtw_rsvd_page *rtw_alloc_rsvd_page(struct rtw_dev *rtwdev,
968 						 enum rtw_rsvd_packet_type type,
969 						 bool txdesc)
970 {
971 	struct rtw_rsvd_page *rsvd_pkt = NULL;
972 
973 	rsvd_pkt = kzalloc(sizeof(*rsvd_pkt), GFP_KERNEL);
974 
975 	if (!rsvd_pkt)
976 		return NULL;
977 
978 	INIT_LIST_HEAD(&rsvd_pkt->vif_list);
979 	INIT_LIST_HEAD(&rsvd_pkt->build_list);
980 	rsvd_pkt->type = type;
981 	rsvd_pkt->add_txdesc = txdesc;
982 
983 	return rsvd_pkt;
984 }
985 
986 static void rtw_insert_rsvd_page(struct rtw_dev *rtwdev,
987 				 struct rtw_vif *rtwvif,
988 				 struct rtw_rsvd_page *rsvd_pkt)
989 {
990 	lockdep_assert_held(&rtwdev->mutex);
991 
992 	list_add_tail(&rsvd_pkt->vif_list, &rtwvif->rsvd_page_list);
993 }
994 
995 static void rtw_add_rsvd_page(struct rtw_dev *rtwdev,
996 			      struct rtw_vif *rtwvif,
997 			      enum rtw_rsvd_packet_type type,
998 			      bool txdesc)
999 {
1000 	struct rtw_rsvd_page *rsvd_pkt;
1001 
1002 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, type, txdesc);
1003 	if (!rsvd_pkt) {
1004 		rtw_err(rtwdev, "failed to alloc rsvd page %d\n", type);
1005 		return;
1006 	}
1007 
1008 	rsvd_pkt->rtwvif = rtwvif;
1009 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1010 }
1011 
1012 static void rtw_add_rsvd_page_probe_req(struct rtw_dev *rtwdev,
1013 					struct rtw_vif *rtwvif,
1014 					struct cfg80211_ssid *ssid)
1015 {
1016 	struct rtw_rsvd_page *rsvd_pkt;
1017 
1018 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_PROBE_REQ, true);
1019 	if (!rsvd_pkt) {
1020 		rtw_err(rtwdev, "failed to alloc probe req rsvd page\n");
1021 		return;
1022 	}
1023 
1024 	rsvd_pkt->rtwvif = rtwvif;
1025 	rsvd_pkt->ssid = ssid;
1026 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1027 }
1028 
1029 void rtw_remove_rsvd_page(struct rtw_dev *rtwdev,
1030 			  struct rtw_vif *rtwvif)
1031 {
1032 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1033 
1034 	lockdep_assert_held(&rtwdev->mutex);
1035 
1036 	/* remove all of the rsvd pages for vif */
1037 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwvif->rsvd_page_list,
1038 				 vif_list) {
1039 		list_del(&rsvd_pkt->vif_list);
1040 		if (!list_empty(&rsvd_pkt->build_list))
1041 			list_del(&rsvd_pkt->build_list);
1042 		kfree(rsvd_pkt);
1043 	}
1044 }
1045 
1046 void rtw_add_rsvd_page_bcn(struct rtw_dev *rtwdev,
1047 			   struct rtw_vif *rtwvif)
1048 {
1049 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1050 
1051 	if (vif->type != NL80211_IFTYPE_AP &&
1052 	    vif->type != NL80211_IFTYPE_ADHOC &&
1053 	    vif->type != NL80211_IFTYPE_MESH_POINT) {
1054 		rtw_warn(rtwdev, "Cannot add beacon rsvd page for %d\n",
1055 			 vif->type);
1056 		return;
1057 	}
1058 
1059 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_BEACON, false);
1060 }
1061 
1062 void rtw_add_rsvd_page_pno(struct rtw_dev *rtwdev,
1063 			   struct rtw_vif *rtwvif)
1064 {
1065 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1066 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
1067 	struct rtw_pno_request *rtw_pno_req = &rtw_wow->pno_req;
1068 	struct cfg80211_ssid *ssid;
1069 	int i;
1070 
1071 	if (vif->type != NL80211_IFTYPE_STATION) {
1072 		rtw_warn(rtwdev, "Cannot add PNO rsvd page for %d\n",
1073 			 vif->type);
1074 		return;
1075 	}
1076 
1077 	for (i = 0 ; i < rtw_pno_req->match_set_cnt; i++) {
1078 		ssid = &rtw_pno_req->match_sets[i].ssid;
1079 		rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, ssid);
1080 	}
1081 
1082 	rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, NULL);
1083 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NLO_INFO, false);
1084 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_CH_INFO, true);
1085 }
1086 
1087 void rtw_add_rsvd_page_sta(struct rtw_dev *rtwdev,
1088 			   struct rtw_vif *rtwvif)
1089 {
1090 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1091 
1092 	if (vif->type != NL80211_IFTYPE_STATION) {
1093 		rtw_warn(rtwdev, "Cannot add sta rsvd page for %d\n",
1094 			 vif->type);
1095 		return;
1096 	}
1097 
1098 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_PS_POLL, true);
1099 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_QOS_NULL, true);
1100 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NULL, true);
1101 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_DPK, true);
1102 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_INFO, true);
1103 }
1104 
1105 int rtw_fw_write_data_rsvd_page(struct rtw_dev *rtwdev, u16 pg_addr,
1106 				u8 *buf, u32 size)
1107 {
1108 	u8 bckp[2];
1109 	u8 val;
1110 	u16 rsvd_pg_head;
1111 	u32 bcn_valid_addr;
1112 	u32 bcn_valid_mask;
1113 	int ret;
1114 
1115 	lockdep_assert_held(&rtwdev->mutex);
1116 
1117 	if (!size)
1118 		return -EINVAL;
1119 
1120 	if (rtw_chip_wcpu_11n(rtwdev)) {
1121 		rtw_write32_set(rtwdev, REG_DWBCN0_CTRL, BIT_BCN_VALID);
1122 	} else {
1123 		pg_addr &= BIT_MASK_BCN_HEAD_1_V1;
1124 		pg_addr |= BIT_BCN_VALID_V1;
1125 		rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2, pg_addr);
1126 	}
1127 
1128 	val = rtw_read8(rtwdev, REG_CR + 1);
1129 	bckp[0] = val;
1130 	val |= BIT_ENSWBCN >> 8;
1131 	rtw_write8(rtwdev, REG_CR + 1, val);
1132 
1133 	val = rtw_read8(rtwdev, REG_FWHW_TXQ_CTRL + 2);
1134 	bckp[1] = val;
1135 	val &= ~(BIT_EN_BCNQ_DL >> 16);
1136 	rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, val);
1137 
1138 	ret = rtw_hci_write_data_rsvd_page(rtwdev, buf, size);
1139 	if (ret) {
1140 		rtw_err(rtwdev, "failed to write data to rsvd page\n");
1141 		goto restore;
1142 	}
1143 
1144 	if (rtw_chip_wcpu_11n(rtwdev)) {
1145 		bcn_valid_addr = REG_DWBCN0_CTRL;
1146 		bcn_valid_mask = BIT_BCN_VALID;
1147 	} else {
1148 		bcn_valid_addr = REG_FIFOPAGE_CTRL_2;
1149 		bcn_valid_mask = BIT_BCN_VALID_V1;
1150 	}
1151 
1152 	if (!check_hw_ready(rtwdev, bcn_valid_addr, bcn_valid_mask, 1)) {
1153 		rtw_err(rtwdev, "error beacon valid\n");
1154 		ret = -EBUSY;
1155 	}
1156 
1157 restore:
1158 	rsvd_pg_head = rtwdev->fifo.rsvd_boundary;
1159 	rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2,
1160 		    rsvd_pg_head | BIT_BCN_VALID_V1);
1161 	rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, bckp[1]);
1162 	rtw_write8(rtwdev, REG_CR + 1, bckp[0]);
1163 
1164 	return ret;
1165 }
1166 
1167 static int rtw_download_drv_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, u32 size)
1168 {
1169 	u32 pg_size;
1170 	u32 pg_num = 0;
1171 	u16 pg_addr = 0;
1172 
1173 	pg_size = rtwdev->chip->page_size;
1174 	pg_num = size / pg_size + ((size & (pg_size - 1)) ? 1 : 0);
1175 	if (pg_num > rtwdev->fifo.rsvd_drv_pg_num)
1176 		return -ENOMEM;
1177 
1178 	pg_addr = rtwdev->fifo.rsvd_drv_addr;
1179 
1180 	return rtw_fw_write_data_rsvd_page(rtwdev, pg_addr, buf, size);
1181 }
1182 
1183 static void __rtw_build_rsvd_page_reset(struct rtw_dev *rtwdev)
1184 {
1185 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1186 
1187 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwdev->rsvd_page_list,
1188 				 build_list) {
1189 		list_del_init(&rsvd_pkt->build_list);
1190 
1191 		/* Don't free except for the dummy rsvd page,
1192 		 * others will be freed when removing vif
1193 		 */
1194 		if (rsvd_pkt->type == RSVD_DUMMY)
1195 			kfree(rsvd_pkt);
1196 	}
1197 }
1198 
1199 static void rtw_build_rsvd_page_iter(void *data, u8 *mac,
1200 				     struct ieee80211_vif *vif)
1201 {
1202 	struct rtw_dev *rtwdev = data;
1203 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
1204 	struct rtw_rsvd_page *rsvd_pkt;
1205 
1206 	list_for_each_entry(rsvd_pkt, &rtwvif->rsvd_page_list, vif_list) {
1207 		if (rsvd_pkt->type == RSVD_BEACON)
1208 			list_add(&rsvd_pkt->build_list,
1209 				 &rtwdev->rsvd_page_list);
1210 		else
1211 			list_add_tail(&rsvd_pkt->build_list,
1212 				      &rtwdev->rsvd_page_list);
1213 	}
1214 }
1215 
1216 static int  __rtw_build_rsvd_page_from_vifs(struct rtw_dev *rtwdev)
1217 {
1218 	struct rtw_rsvd_page *rsvd_pkt;
1219 
1220 	__rtw_build_rsvd_page_reset(rtwdev);
1221 
1222 	/* gather rsvd page from vifs */
1223 	rtw_iterate_vifs_atomic(rtwdev, rtw_build_rsvd_page_iter, rtwdev);
1224 
1225 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
1226 					    struct rtw_rsvd_page, build_list);
1227 	if (!rsvd_pkt) {
1228 		WARN(1, "Should not have an empty reserved page\n");
1229 		return -EINVAL;
1230 	}
1231 
1232 	/* the first rsvd should be beacon, otherwise add a dummy one */
1233 	if (rsvd_pkt->type != RSVD_BEACON) {
1234 		struct rtw_rsvd_page *dummy_pkt;
1235 
1236 		dummy_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_DUMMY, false);
1237 		if (!dummy_pkt) {
1238 			rtw_err(rtwdev, "failed to alloc dummy rsvd page\n");
1239 			return -ENOMEM;
1240 		}
1241 
1242 		list_add(&dummy_pkt->build_list, &rtwdev->rsvd_page_list);
1243 	}
1244 
1245 	return 0;
1246 }
1247 
1248 static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size)
1249 {
1250 	struct ieee80211_hw *hw = rtwdev->hw;
1251 	struct rtw_chip_info *chip = rtwdev->chip;
1252 	struct sk_buff *iter;
1253 	struct rtw_rsvd_page *rsvd_pkt;
1254 	u32 page = 0;
1255 	u8 total_page = 0;
1256 	u8 page_size, page_margin, tx_desc_sz;
1257 	u8 *buf;
1258 	int ret;
1259 
1260 	page_size = chip->page_size;
1261 	tx_desc_sz = chip->tx_pkt_desc_sz;
1262 	page_margin = page_size - tx_desc_sz;
1263 
1264 	ret = __rtw_build_rsvd_page_from_vifs(rtwdev);
1265 	if (ret) {
1266 		rtw_err(rtwdev,
1267 			"failed to build rsvd page from vifs, ret %d\n", ret);
1268 		return NULL;
1269 	}
1270 
1271 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1272 		iter = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1273 		if (!iter) {
1274 			rtw_err(rtwdev, "failed to build rsvd packet\n");
1275 			goto release_skb;
1276 		}
1277 
1278 		/* Fill the tx_desc for the rsvd pkt that requires one.
1279 		 * And iter->len will be added with size of tx_desc_sz.
1280 		 */
1281 		if (rsvd_pkt->add_txdesc)
1282 			rtw_fill_rsvd_page_desc(rtwdev, iter, rsvd_pkt->type);
1283 
1284 		rsvd_pkt->skb = iter;
1285 		rsvd_pkt->page = total_page;
1286 
1287 		/* Reserved page is downloaded via TX path, and TX path will
1288 		 * generate a tx_desc at the header to describe length of
1289 		 * the buffer. If we are not counting page numbers with the
1290 		 * size of tx_desc added at the first rsvd_pkt (usually a
1291 		 * beacon, firmware default refer to the first page as the
1292 		 * content of beacon), we could generate a buffer which size
1293 		 * is smaller than the actual size of the whole rsvd_page
1294 		 */
1295 		if (total_page == 0) {
1296 			if (rsvd_pkt->type != RSVD_BEACON &&
1297 			    rsvd_pkt->type != RSVD_DUMMY) {
1298 				rtw_err(rtwdev, "first page should be a beacon\n");
1299 				goto release_skb;
1300 			}
1301 			total_page += rtw_len_to_page(iter->len + tx_desc_sz,
1302 						      page_size);
1303 		} else {
1304 			total_page += rtw_len_to_page(iter->len, page_size);
1305 		}
1306 	}
1307 
1308 	if (total_page > rtwdev->fifo.rsvd_drv_pg_num) {
1309 		rtw_err(rtwdev, "rsvd page over size: %d\n", total_page);
1310 		goto release_skb;
1311 	}
1312 
1313 	*size = (total_page - 1) * page_size + page_margin;
1314 	buf = kzalloc(*size, GFP_KERNEL);
1315 	if (!buf)
1316 		goto release_skb;
1317 
1318 	/* Copy the content of each rsvd_pkt to the buf, and they should
1319 	 * be aligned to the pages.
1320 	 *
1321 	 * Note that the first rsvd_pkt is a beacon no matter what vif->type.
1322 	 * And that rsvd_pkt does not require tx_desc because when it goes
1323 	 * through TX path, the TX path will generate one for it.
1324 	 */
1325 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1326 		rtw_rsvd_page_list_to_buf(rtwdev, page_size, page_margin,
1327 					  page, buf, rsvd_pkt);
1328 		if (page == 0)
1329 			page += rtw_len_to_page(rsvd_pkt->skb->len +
1330 						tx_desc_sz, page_size);
1331 		else
1332 			page += rtw_len_to_page(rsvd_pkt->skb->len, page_size);
1333 
1334 		kfree_skb(rsvd_pkt->skb);
1335 		rsvd_pkt->skb = NULL;
1336 	}
1337 
1338 	return buf;
1339 
1340 release_skb:
1341 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1342 		kfree_skb(rsvd_pkt->skb);
1343 		rsvd_pkt->skb = NULL;
1344 	}
1345 
1346 	return NULL;
1347 }
1348 
1349 static int rtw_download_beacon(struct rtw_dev *rtwdev)
1350 {
1351 	struct ieee80211_hw *hw = rtwdev->hw;
1352 	struct rtw_rsvd_page *rsvd_pkt;
1353 	struct sk_buff *skb;
1354 	int ret = 0;
1355 
1356 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
1357 					    struct rtw_rsvd_page, build_list);
1358 	if (!rsvd_pkt) {
1359 		rtw_err(rtwdev, "failed to get rsvd page from build list\n");
1360 		return -ENOENT;
1361 	}
1362 
1363 	if (rsvd_pkt->type != RSVD_BEACON &&
1364 	    rsvd_pkt->type != RSVD_DUMMY) {
1365 		rtw_err(rtwdev, "invalid rsvd page type %d, should be beacon or dummy\n",
1366 			rsvd_pkt->type);
1367 		return -EINVAL;
1368 	}
1369 
1370 	skb = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1371 	if (!skb) {
1372 		rtw_err(rtwdev, "failed to get beacon skb\n");
1373 		return -ENOMEM;
1374 	}
1375 
1376 	ret = rtw_download_drv_rsvd_page(rtwdev, skb->data, skb->len);
1377 	if (ret)
1378 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1379 
1380 	dev_kfree_skb(skb);
1381 
1382 	return ret;
1383 }
1384 
1385 int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev)
1386 {
1387 	u8 *buf;
1388 	u32 size;
1389 	int ret;
1390 
1391 	buf = rtw_build_rsvd_page(rtwdev, &size);
1392 	if (!buf) {
1393 		rtw_err(rtwdev, "failed to build rsvd page pkt\n");
1394 		return -ENOMEM;
1395 	}
1396 
1397 	ret = rtw_download_drv_rsvd_page(rtwdev, buf, size);
1398 	if (ret) {
1399 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1400 		goto free;
1401 	}
1402 
1403 	/* The last thing is to download the *ONLY* beacon again, because
1404 	 * the previous tx_desc is to describe the total rsvd page. Download
1405 	 * the beacon again to replace the TX desc header, and we will get
1406 	 * a correct tx_desc for the beacon in the rsvd page.
1407 	 */
1408 	ret = rtw_download_beacon(rtwdev);
1409 	if (ret) {
1410 		rtw_err(rtwdev, "failed to download beacon\n");
1411 		goto free;
1412 	}
1413 
1414 free:
1415 	kfree(buf);
1416 
1417 	return ret;
1418 }
1419 
1420 static void rtw_fw_read_fifo_page(struct rtw_dev *rtwdev, u32 offset, u32 size,
1421 				  u32 *buf, u32 residue, u16 start_pg)
1422 {
1423 	u32 i;
1424 	u16 idx = 0;
1425 	u16 ctl;
1426 	u8 rcr;
1427 
1428 	rcr = rtw_read8(rtwdev, REG_RCR + 2);
1429 	ctl = rtw_read16(rtwdev, REG_PKTBUF_DBG_CTRL) & 0xf000;
1430 	/* disable rx clock gate */
1431 	rtw_write8(rtwdev, REG_RCR, rcr | BIT(3));
1432 
1433 	do {
1434 		rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, start_pg | ctl);
1435 
1436 		for (i = FIFO_DUMP_ADDR + residue;
1437 		     i < FIFO_DUMP_ADDR + FIFO_PAGE_SIZE; i += 4) {
1438 			buf[idx++] = rtw_read32(rtwdev, i);
1439 			size -= 4;
1440 			if (size == 0)
1441 				goto out;
1442 		}
1443 
1444 		residue = 0;
1445 		start_pg++;
1446 	} while (size);
1447 
1448 out:
1449 	rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, ctl);
1450 	rtw_write8(rtwdev, REG_RCR + 2, rcr);
1451 }
1452 
1453 static void rtw_fw_read_fifo(struct rtw_dev *rtwdev, enum rtw_fw_fifo_sel sel,
1454 			     u32 offset, u32 size, u32 *buf)
1455 {
1456 	struct rtw_chip_info *chip = rtwdev->chip;
1457 	u32 start_pg, residue;
1458 
1459 	if (sel >= RTW_FW_FIFO_MAX) {
1460 		rtw_dbg(rtwdev, RTW_DBG_FW, "wrong fw fifo sel\n");
1461 		return;
1462 	}
1463 	if (sel == RTW_FW_FIFO_SEL_RSVD_PAGE)
1464 		offset += rtwdev->fifo.rsvd_boundary << TX_PAGE_SIZE_SHIFT;
1465 	residue = offset & (FIFO_PAGE_SIZE - 1);
1466 	start_pg = (offset >> FIFO_PAGE_SIZE_SHIFT) + chip->fw_fifo_addr[sel];
1467 
1468 	rtw_fw_read_fifo_page(rtwdev, offset, size, buf, residue, start_pg);
1469 }
1470 
1471 static bool rtw_fw_dump_check_size(struct rtw_dev *rtwdev,
1472 				   enum rtw_fw_fifo_sel sel,
1473 				   u32 start_addr, u32 size)
1474 {
1475 	switch (sel) {
1476 	case RTW_FW_FIFO_SEL_TX:
1477 	case RTW_FW_FIFO_SEL_RX:
1478 		if ((start_addr + size) > rtwdev->chip->fw_fifo_addr[sel])
1479 			return false;
1480 		fallthrough;
1481 	default:
1482 		return true;
1483 	}
1484 }
1485 
1486 int rtw_fw_dump_fifo(struct rtw_dev *rtwdev, u8 fifo_sel, u32 addr, u32 size,
1487 		     u32 *buffer)
1488 {
1489 	if (!rtwdev->chip->fw_fifo_addr[0]) {
1490 		rtw_dbg(rtwdev, RTW_DBG_FW, "chip not support dump fw fifo\n");
1491 		return -ENOTSUPP;
1492 	}
1493 
1494 	if (size == 0 || !buffer)
1495 		return -EINVAL;
1496 
1497 	if (size & 0x3) {
1498 		rtw_dbg(rtwdev, RTW_DBG_FW, "not 4byte alignment\n");
1499 		return -EINVAL;
1500 	}
1501 
1502 	if (!rtw_fw_dump_check_size(rtwdev, fifo_sel, addr, size)) {
1503 		rtw_dbg(rtwdev, RTW_DBG_FW, "fw fifo dump size overflow\n");
1504 		return -EINVAL;
1505 	}
1506 
1507 	rtw_fw_read_fifo(rtwdev, fifo_sel, addr, size, buffer);
1508 
1509 	return 0;
1510 }
1511 
1512 static void __rtw_fw_update_pkt(struct rtw_dev *rtwdev, u8 pkt_id, u16 size,
1513 				u8 location)
1514 {
1515 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1516 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_UPDATE_PKT_LEN;
1517 
1518 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_UPDATE_PKT);
1519 
1520 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1521 	UPDATE_PKT_SET_PKT_ID(h2c_pkt, pkt_id);
1522 	UPDATE_PKT_SET_LOCATION(h2c_pkt, location);
1523 
1524 	/* include txdesc size */
1525 	UPDATE_PKT_SET_SIZE(h2c_pkt, size);
1526 
1527 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1528 }
1529 
1530 void rtw_fw_update_pkt_probe_req(struct rtw_dev *rtwdev,
1531 				 struct cfg80211_ssid *ssid)
1532 {
1533 	u8 loc;
1534 	u32 size;
1535 
1536 	loc = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid);
1537 	if (!loc) {
1538 		rtw_err(rtwdev, "failed to get probe_req rsvd loc\n");
1539 		return;
1540 	}
1541 
1542 	size = rtw_get_rsvd_page_probe_req_size(rtwdev, ssid);
1543 	if (!size) {
1544 		rtw_err(rtwdev, "failed to get probe_req rsvd size\n");
1545 		return;
1546 	}
1547 
1548 	__rtw_fw_update_pkt(rtwdev, RTW_PACKET_PROBE_REQ, size, loc);
1549 }
1550 
1551 void rtw_fw_channel_switch(struct rtw_dev *rtwdev, bool enable)
1552 {
1553 	struct rtw_pno_request *rtw_pno_req = &rtwdev->wow.pno_req;
1554 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1555 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_CH_SWITCH_LEN;
1556 	u8 loc_ch_info;
1557 	const struct rtw_ch_switch_option cs_option = {
1558 		.dest_ch_en = 1,
1559 		.dest_ch = 1,
1560 		.periodic_option = 2,
1561 		.normal_period = 5,
1562 		.normal_period_sel = 0,
1563 		.normal_cycle = 10,
1564 		.slow_period = 1,
1565 		.slow_period_sel = 1,
1566 	};
1567 
1568 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_CH_SWITCH);
1569 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1570 
1571 	CH_SWITCH_SET_START(h2c_pkt, enable);
1572 	CH_SWITCH_SET_DEST_CH_EN(h2c_pkt, cs_option.dest_ch_en);
1573 	CH_SWITCH_SET_DEST_CH(h2c_pkt, cs_option.dest_ch);
1574 	CH_SWITCH_SET_NORMAL_PERIOD(h2c_pkt, cs_option.normal_period);
1575 	CH_SWITCH_SET_NORMAL_PERIOD_SEL(h2c_pkt, cs_option.normal_period_sel);
1576 	CH_SWITCH_SET_SLOW_PERIOD(h2c_pkt, cs_option.slow_period);
1577 	CH_SWITCH_SET_SLOW_PERIOD_SEL(h2c_pkt, cs_option.slow_period_sel);
1578 	CH_SWITCH_SET_NORMAL_CYCLE(h2c_pkt, cs_option.normal_cycle);
1579 	CH_SWITCH_SET_PERIODIC_OPT(h2c_pkt, cs_option.periodic_option);
1580 
1581 	CH_SWITCH_SET_CH_NUM(h2c_pkt, rtw_pno_req->channel_cnt);
1582 	CH_SWITCH_SET_INFO_SIZE(h2c_pkt, rtw_pno_req->channel_cnt * 4);
1583 
1584 	loc_ch_info = rtw_get_rsvd_page_location(rtwdev, RSVD_CH_INFO);
1585 	CH_SWITCH_SET_INFO_LOC(h2c_pkt, loc_ch_info);
1586 
1587 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1588 }
1589