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