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->skb->len;
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 		break;
1051 	case RSVD_NLO_INFO:
1052 		skb_new = rtw_nlo_info_get(hw);
1053 		break;
1054 	case RSVD_CH_INFO:
1055 		skb_new = rtw_cs_channel_info_get(hw);
1056 		break;
1057 	default:
1058 		return NULL;
1059 	}
1060 
1061 	if (!skb_new)
1062 		return NULL;
1063 
1064 	return skb_new;
1065 }
1066 
1067 static void rtw_fill_rsvd_page_desc(struct rtw_dev *rtwdev, struct sk_buff *skb,
1068 				    enum rtw_rsvd_packet_type type)
1069 {
1070 	struct rtw_tx_pkt_info pkt_info = {0};
1071 	struct rtw_chip_info *chip = rtwdev->chip;
1072 	u8 *pkt_desc;
1073 
1074 	rtw_tx_rsvd_page_pkt_info_update(rtwdev, &pkt_info, skb, type);
1075 	pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz);
1076 	memset(pkt_desc, 0, chip->tx_pkt_desc_sz);
1077 	rtw_tx_fill_tx_desc(&pkt_info, skb);
1078 }
1079 
1080 static inline u8 rtw_len_to_page(unsigned int len, u8 page_size)
1081 {
1082 	return DIV_ROUND_UP(len, page_size);
1083 }
1084 
1085 static void rtw_rsvd_page_list_to_buf(struct rtw_dev *rtwdev, u8 page_size,
1086 				      u8 page_margin, u32 page, u8 *buf,
1087 				      struct rtw_rsvd_page *rsvd_pkt)
1088 {
1089 	struct sk_buff *skb = rsvd_pkt->skb;
1090 
1091 	if (page >= 1)
1092 		memcpy(buf + page_margin + page_size * (page - 1),
1093 		       skb->data, skb->len);
1094 	else
1095 		memcpy(buf, skb->data, skb->len);
1096 }
1097 
1098 static struct rtw_rsvd_page *rtw_alloc_rsvd_page(struct rtw_dev *rtwdev,
1099 						 enum rtw_rsvd_packet_type type,
1100 						 bool txdesc)
1101 {
1102 	struct rtw_rsvd_page *rsvd_pkt = NULL;
1103 
1104 	rsvd_pkt = kzalloc(sizeof(*rsvd_pkt), GFP_KERNEL);
1105 
1106 	if (!rsvd_pkt)
1107 		return NULL;
1108 
1109 	INIT_LIST_HEAD(&rsvd_pkt->vif_list);
1110 	INIT_LIST_HEAD(&rsvd_pkt->build_list);
1111 	rsvd_pkt->type = type;
1112 	rsvd_pkt->add_txdesc = txdesc;
1113 
1114 	return rsvd_pkt;
1115 }
1116 
1117 static void rtw_insert_rsvd_page(struct rtw_dev *rtwdev,
1118 				 struct rtw_vif *rtwvif,
1119 				 struct rtw_rsvd_page *rsvd_pkt)
1120 {
1121 	lockdep_assert_held(&rtwdev->mutex);
1122 
1123 	list_add_tail(&rsvd_pkt->vif_list, &rtwvif->rsvd_page_list);
1124 }
1125 
1126 static void rtw_add_rsvd_page(struct rtw_dev *rtwdev,
1127 			      struct rtw_vif *rtwvif,
1128 			      enum rtw_rsvd_packet_type type,
1129 			      bool txdesc)
1130 {
1131 	struct rtw_rsvd_page *rsvd_pkt;
1132 
1133 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, type, txdesc);
1134 	if (!rsvd_pkt) {
1135 		rtw_err(rtwdev, "failed to alloc rsvd page %d\n", type);
1136 		return;
1137 	}
1138 
1139 	rsvd_pkt->rtwvif = rtwvif;
1140 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1141 }
1142 
1143 static void rtw_add_rsvd_page_probe_req(struct rtw_dev *rtwdev,
1144 					struct rtw_vif *rtwvif,
1145 					struct cfg80211_ssid *ssid)
1146 {
1147 	struct rtw_rsvd_page *rsvd_pkt;
1148 
1149 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_PROBE_REQ, true);
1150 	if (!rsvd_pkt) {
1151 		rtw_err(rtwdev, "failed to alloc probe req rsvd page\n");
1152 		return;
1153 	}
1154 
1155 	rsvd_pkt->rtwvif = rtwvif;
1156 	rsvd_pkt->ssid = ssid;
1157 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1158 }
1159 
1160 void rtw_remove_rsvd_page(struct rtw_dev *rtwdev,
1161 			  struct rtw_vif *rtwvif)
1162 {
1163 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1164 
1165 	lockdep_assert_held(&rtwdev->mutex);
1166 
1167 	/* remove all of the rsvd pages for vif */
1168 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwvif->rsvd_page_list,
1169 				 vif_list) {
1170 		list_del(&rsvd_pkt->vif_list);
1171 		if (!list_empty(&rsvd_pkt->build_list))
1172 			list_del(&rsvd_pkt->build_list);
1173 		kfree(rsvd_pkt);
1174 	}
1175 }
1176 
1177 void rtw_add_rsvd_page_bcn(struct rtw_dev *rtwdev,
1178 			   struct rtw_vif *rtwvif)
1179 {
1180 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1181 
1182 	if (vif->type != NL80211_IFTYPE_AP &&
1183 	    vif->type != NL80211_IFTYPE_ADHOC &&
1184 	    vif->type != NL80211_IFTYPE_MESH_POINT) {
1185 		rtw_warn(rtwdev, "Cannot add beacon rsvd page for %d\n",
1186 			 vif->type);
1187 		return;
1188 	}
1189 
1190 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_BEACON, false);
1191 }
1192 
1193 void rtw_add_rsvd_page_pno(struct rtw_dev *rtwdev,
1194 			   struct rtw_vif *rtwvif)
1195 {
1196 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1197 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
1198 	struct rtw_pno_request *rtw_pno_req = &rtw_wow->pno_req;
1199 	struct cfg80211_ssid *ssid;
1200 	int i;
1201 
1202 	if (vif->type != NL80211_IFTYPE_STATION) {
1203 		rtw_warn(rtwdev, "Cannot add PNO rsvd page for %d\n",
1204 			 vif->type);
1205 		return;
1206 	}
1207 
1208 	for (i = 0 ; i < rtw_pno_req->match_set_cnt; i++) {
1209 		ssid = &rtw_pno_req->match_sets[i].ssid;
1210 		rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, ssid);
1211 	}
1212 
1213 	rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, NULL);
1214 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NLO_INFO, false);
1215 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_CH_INFO, true);
1216 }
1217 
1218 void rtw_add_rsvd_page_sta(struct rtw_dev *rtwdev,
1219 			   struct rtw_vif *rtwvif)
1220 {
1221 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1222 
1223 	if (vif->type != NL80211_IFTYPE_STATION) {
1224 		rtw_warn(rtwdev, "Cannot add sta rsvd page for %d\n",
1225 			 vif->type);
1226 		return;
1227 	}
1228 
1229 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_PS_POLL, true);
1230 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_QOS_NULL, true);
1231 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NULL, true);
1232 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_DPK, true);
1233 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_INFO, true);
1234 }
1235 
1236 int rtw_fw_write_data_rsvd_page(struct rtw_dev *rtwdev, u16 pg_addr,
1237 				u8 *buf, u32 size)
1238 {
1239 	u8 bckp[2];
1240 	u8 val;
1241 	u16 rsvd_pg_head;
1242 	u32 bcn_valid_addr;
1243 	u32 bcn_valid_mask;
1244 	int ret;
1245 
1246 	lockdep_assert_held(&rtwdev->mutex);
1247 
1248 	if (!size)
1249 		return -EINVAL;
1250 
1251 	if (rtw_chip_wcpu_11n(rtwdev)) {
1252 		rtw_write32_set(rtwdev, REG_DWBCN0_CTRL, BIT_BCN_VALID);
1253 	} else {
1254 		pg_addr &= BIT_MASK_BCN_HEAD_1_V1;
1255 		pg_addr |= BIT_BCN_VALID_V1;
1256 		rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2, pg_addr);
1257 	}
1258 
1259 	val = rtw_read8(rtwdev, REG_CR + 1);
1260 	bckp[0] = val;
1261 	val |= BIT_ENSWBCN >> 8;
1262 	rtw_write8(rtwdev, REG_CR + 1, val);
1263 
1264 	val = rtw_read8(rtwdev, REG_FWHW_TXQ_CTRL + 2);
1265 	bckp[1] = val;
1266 	val &= ~(BIT_EN_BCNQ_DL >> 16);
1267 	rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, val);
1268 
1269 	ret = rtw_hci_write_data_rsvd_page(rtwdev, buf, size);
1270 	if (ret) {
1271 		rtw_err(rtwdev, "failed to write data to rsvd page\n");
1272 		goto restore;
1273 	}
1274 
1275 	if (rtw_chip_wcpu_11n(rtwdev)) {
1276 		bcn_valid_addr = REG_DWBCN0_CTRL;
1277 		bcn_valid_mask = BIT_BCN_VALID;
1278 	} else {
1279 		bcn_valid_addr = REG_FIFOPAGE_CTRL_2;
1280 		bcn_valid_mask = BIT_BCN_VALID_V1;
1281 	}
1282 
1283 	if (!check_hw_ready(rtwdev, bcn_valid_addr, bcn_valid_mask, 1)) {
1284 		rtw_err(rtwdev, "error beacon valid\n");
1285 		ret = -EBUSY;
1286 	}
1287 
1288 restore:
1289 	rsvd_pg_head = rtwdev->fifo.rsvd_boundary;
1290 	rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2,
1291 		    rsvd_pg_head | BIT_BCN_VALID_V1);
1292 	rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, bckp[1]);
1293 	rtw_write8(rtwdev, REG_CR + 1, bckp[0]);
1294 
1295 	return ret;
1296 }
1297 
1298 static int rtw_download_drv_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, u32 size)
1299 {
1300 	u32 pg_size;
1301 	u32 pg_num = 0;
1302 	u16 pg_addr = 0;
1303 
1304 	pg_size = rtwdev->chip->page_size;
1305 	pg_num = size / pg_size + ((size & (pg_size - 1)) ? 1 : 0);
1306 	if (pg_num > rtwdev->fifo.rsvd_drv_pg_num)
1307 		return -ENOMEM;
1308 
1309 	pg_addr = rtwdev->fifo.rsvd_drv_addr;
1310 
1311 	return rtw_fw_write_data_rsvd_page(rtwdev, pg_addr, buf, size);
1312 }
1313 
1314 static void __rtw_build_rsvd_page_reset(struct rtw_dev *rtwdev)
1315 {
1316 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1317 
1318 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwdev->rsvd_page_list,
1319 				 build_list) {
1320 		list_del_init(&rsvd_pkt->build_list);
1321 
1322 		/* Don't free except for the dummy rsvd page,
1323 		 * others will be freed when removing vif
1324 		 */
1325 		if (rsvd_pkt->type == RSVD_DUMMY)
1326 			kfree(rsvd_pkt);
1327 	}
1328 }
1329 
1330 static void rtw_build_rsvd_page_iter(void *data, u8 *mac,
1331 				     struct ieee80211_vif *vif)
1332 {
1333 	struct rtw_dev *rtwdev = data;
1334 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
1335 	struct rtw_rsvd_page *rsvd_pkt;
1336 
1337 	list_for_each_entry(rsvd_pkt, &rtwvif->rsvd_page_list, vif_list) {
1338 		if (rsvd_pkt->type == RSVD_BEACON)
1339 			list_add(&rsvd_pkt->build_list,
1340 				 &rtwdev->rsvd_page_list);
1341 		else
1342 			list_add_tail(&rsvd_pkt->build_list,
1343 				      &rtwdev->rsvd_page_list);
1344 	}
1345 }
1346 
1347 static int  __rtw_build_rsvd_page_from_vifs(struct rtw_dev *rtwdev)
1348 {
1349 	struct rtw_rsvd_page *rsvd_pkt;
1350 
1351 	__rtw_build_rsvd_page_reset(rtwdev);
1352 
1353 	/* gather rsvd page from vifs */
1354 	rtw_iterate_vifs_atomic(rtwdev, rtw_build_rsvd_page_iter, rtwdev);
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 		WARN(1, "Should not have an empty reserved page\n");
1360 		return -EINVAL;
1361 	}
1362 
1363 	/* the first rsvd should be beacon, otherwise add a dummy one */
1364 	if (rsvd_pkt->type != RSVD_BEACON) {
1365 		struct rtw_rsvd_page *dummy_pkt;
1366 
1367 		dummy_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_DUMMY, false);
1368 		if (!dummy_pkt) {
1369 			rtw_err(rtwdev, "failed to alloc dummy rsvd page\n");
1370 			return -ENOMEM;
1371 		}
1372 
1373 		list_add(&dummy_pkt->build_list, &rtwdev->rsvd_page_list);
1374 	}
1375 
1376 	return 0;
1377 }
1378 
1379 static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size)
1380 {
1381 	struct ieee80211_hw *hw = rtwdev->hw;
1382 	struct rtw_chip_info *chip = rtwdev->chip;
1383 	struct sk_buff *iter;
1384 	struct rtw_rsvd_page *rsvd_pkt;
1385 	u32 page = 0;
1386 	u8 total_page = 0;
1387 	u8 page_size, page_margin, tx_desc_sz;
1388 	u8 *buf;
1389 	int ret;
1390 
1391 	page_size = chip->page_size;
1392 	tx_desc_sz = chip->tx_pkt_desc_sz;
1393 	page_margin = page_size - tx_desc_sz;
1394 
1395 	ret = __rtw_build_rsvd_page_from_vifs(rtwdev);
1396 	if (ret) {
1397 		rtw_err(rtwdev,
1398 			"failed to build rsvd page from vifs, ret %d\n", ret);
1399 		return NULL;
1400 	}
1401 
1402 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1403 		iter = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1404 		if (!iter) {
1405 			rtw_err(rtwdev, "failed to build rsvd packet\n");
1406 			goto release_skb;
1407 		}
1408 
1409 		/* Fill the tx_desc for the rsvd pkt that requires one.
1410 		 * And iter->len will be added with size of tx_desc_sz.
1411 		 */
1412 		if (rsvd_pkt->add_txdesc)
1413 			rtw_fill_rsvd_page_desc(rtwdev, iter, rsvd_pkt->type);
1414 
1415 		rsvd_pkt->skb = iter;
1416 		rsvd_pkt->page = total_page;
1417 
1418 		/* Reserved page is downloaded via TX path, and TX path will
1419 		 * generate a tx_desc at the header to describe length of
1420 		 * the buffer. If we are not counting page numbers with the
1421 		 * size of tx_desc added at the first rsvd_pkt (usually a
1422 		 * beacon, firmware default refer to the first page as the
1423 		 * content of beacon), we could generate a buffer which size
1424 		 * is smaller than the actual size of the whole rsvd_page
1425 		 */
1426 		if (total_page == 0) {
1427 			if (rsvd_pkt->type != RSVD_BEACON &&
1428 			    rsvd_pkt->type != RSVD_DUMMY) {
1429 				rtw_err(rtwdev, "first page should be a beacon\n");
1430 				goto release_skb;
1431 			}
1432 			total_page += rtw_len_to_page(iter->len + tx_desc_sz,
1433 						      page_size);
1434 		} else {
1435 			total_page += rtw_len_to_page(iter->len, page_size);
1436 		}
1437 	}
1438 
1439 	if (total_page > rtwdev->fifo.rsvd_drv_pg_num) {
1440 		rtw_err(rtwdev, "rsvd page over size: %d\n", total_page);
1441 		goto release_skb;
1442 	}
1443 
1444 	*size = (total_page - 1) * page_size + page_margin;
1445 	buf = kzalloc(*size, GFP_KERNEL);
1446 	if (!buf)
1447 		goto release_skb;
1448 
1449 	/* Copy the content of each rsvd_pkt to the buf, and they should
1450 	 * be aligned to the pages.
1451 	 *
1452 	 * Note that the first rsvd_pkt is a beacon no matter what vif->type.
1453 	 * And that rsvd_pkt does not require tx_desc because when it goes
1454 	 * through TX path, the TX path will generate one for it.
1455 	 */
1456 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1457 		rtw_rsvd_page_list_to_buf(rtwdev, page_size, page_margin,
1458 					  page, buf, rsvd_pkt);
1459 		if (page == 0)
1460 			page += rtw_len_to_page(rsvd_pkt->skb->len +
1461 						tx_desc_sz, page_size);
1462 		else
1463 			page += rtw_len_to_page(rsvd_pkt->skb->len, page_size);
1464 
1465 		kfree_skb(rsvd_pkt->skb);
1466 		rsvd_pkt->skb = NULL;
1467 	}
1468 
1469 	return buf;
1470 
1471 release_skb:
1472 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1473 		kfree_skb(rsvd_pkt->skb);
1474 		rsvd_pkt->skb = NULL;
1475 	}
1476 
1477 	return NULL;
1478 }
1479 
1480 static int rtw_download_beacon(struct rtw_dev *rtwdev)
1481 {
1482 	struct ieee80211_hw *hw = rtwdev->hw;
1483 	struct rtw_rsvd_page *rsvd_pkt;
1484 	struct sk_buff *skb;
1485 	int ret = 0;
1486 
1487 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
1488 					    struct rtw_rsvd_page, build_list);
1489 	if (!rsvd_pkt) {
1490 		rtw_err(rtwdev, "failed to get rsvd page from build list\n");
1491 		return -ENOENT;
1492 	}
1493 
1494 	if (rsvd_pkt->type != RSVD_BEACON &&
1495 	    rsvd_pkt->type != RSVD_DUMMY) {
1496 		rtw_err(rtwdev, "invalid rsvd page type %d, should be beacon or dummy\n",
1497 			rsvd_pkt->type);
1498 		return -EINVAL;
1499 	}
1500 
1501 	skb = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1502 	if (!skb) {
1503 		rtw_err(rtwdev, "failed to get beacon skb\n");
1504 		return -ENOMEM;
1505 	}
1506 
1507 	ret = rtw_download_drv_rsvd_page(rtwdev, skb->data, skb->len);
1508 	if (ret)
1509 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1510 
1511 	dev_kfree_skb(skb);
1512 
1513 	return ret;
1514 }
1515 
1516 int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev)
1517 {
1518 	u8 *buf;
1519 	u32 size;
1520 	int ret;
1521 
1522 	buf = rtw_build_rsvd_page(rtwdev, &size);
1523 	if (!buf) {
1524 		rtw_err(rtwdev, "failed to build rsvd page pkt\n");
1525 		return -ENOMEM;
1526 	}
1527 
1528 	ret = rtw_download_drv_rsvd_page(rtwdev, buf, size);
1529 	if (ret) {
1530 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1531 		goto free;
1532 	}
1533 
1534 	/* The last thing is to download the *ONLY* beacon again, because
1535 	 * the previous tx_desc is to describe the total rsvd page. Download
1536 	 * the beacon again to replace the TX desc header, and we will get
1537 	 * a correct tx_desc for the beacon in the rsvd page.
1538 	 */
1539 	ret = rtw_download_beacon(rtwdev);
1540 	if (ret) {
1541 		rtw_err(rtwdev, "failed to download beacon\n");
1542 		goto free;
1543 	}
1544 
1545 free:
1546 	kfree(buf);
1547 
1548 	return ret;
1549 }
1550 
1551 static void rtw_fw_read_fifo_page(struct rtw_dev *rtwdev, u32 offset, u32 size,
1552 				  u32 *buf, u32 residue, u16 start_pg)
1553 {
1554 	u32 i;
1555 	u16 idx = 0;
1556 	u16 ctl;
1557 	u8 rcr;
1558 
1559 	rcr = rtw_read8(rtwdev, REG_RCR + 2);
1560 	ctl = rtw_read16(rtwdev, REG_PKTBUF_DBG_CTRL) & 0xf000;
1561 	/* disable rx clock gate */
1562 	rtw_write8(rtwdev, REG_RCR, rcr | BIT(3));
1563 
1564 	do {
1565 		rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, start_pg | ctl);
1566 
1567 		for (i = FIFO_DUMP_ADDR + residue;
1568 		     i < FIFO_DUMP_ADDR + FIFO_PAGE_SIZE; i += 4) {
1569 			buf[idx++] = rtw_read32(rtwdev, i);
1570 			size -= 4;
1571 			if (size == 0)
1572 				goto out;
1573 		}
1574 
1575 		residue = 0;
1576 		start_pg++;
1577 	} while (size);
1578 
1579 out:
1580 	rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, ctl);
1581 	rtw_write8(rtwdev, REG_RCR + 2, rcr);
1582 }
1583 
1584 static void rtw_fw_read_fifo(struct rtw_dev *rtwdev, enum rtw_fw_fifo_sel sel,
1585 			     u32 offset, u32 size, u32 *buf)
1586 {
1587 	struct rtw_chip_info *chip = rtwdev->chip;
1588 	u32 start_pg, residue;
1589 
1590 	if (sel >= RTW_FW_FIFO_MAX) {
1591 		rtw_dbg(rtwdev, RTW_DBG_FW, "wrong fw fifo sel\n");
1592 		return;
1593 	}
1594 	if (sel == RTW_FW_FIFO_SEL_RSVD_PAGE)
1595 		offset += rtwdev->fifo.rsvd_boundary << TX_PAGE_SIZE_SHIFT;
1596 	residue = offset & (FIFO_PAGE_SIZE - 1);
1597 	start_pg = (offset >> FIFO_PAGE_SIZE_SHIFT) + chip->fw_fifo_addr[sel];
1598 
1599 	rtw_fw_read_fifo_page(rtwdev, offset, size, buf, residue, start_pg);
1600 }
1601 
1602 static bool rtw_fw_dump_check_size(struct rtw_dev *rtwdev,
1603 				   enum rtw_fw_fifo_sel sel,
1604 				   u32 start_addr, u32 size)
1605 {
1606 	switch (sel) {
1607 	case RTW_FW_FIFO_SEL_TX:
1608 	case RTW_FW_FIFO_SEL_RX:
1609 		if ((start_addr + size) > rtwdev->chip->fw_fifo_addr[sel])
1610 			return false;
1611 		fallthrough;
1612 	default:
1613 		return true;
1614 	}
1615 }
1616 
1617 int rtw_fw_dump_fifo(struct rtw_dev *rtwdev, u8 fifo_sel, u32 addr, u32 size,
1618 		     u32 *buffer)
1619 {
1620 	if (!rtwdev->chip->fw_fifo_addr[0]) {
1621 		rtw_dbg(rtwdev, RTW_DBG_FW, "chip not support dump fw fifo\n");
1622 		return -ENOTSUPP;
1623 	}
1624 
1625 	if (size == 0 || !buffer)
1626 		return -EINVAL;
1627 
1628 	if (size & 0x3) {
1629 		rtw_dbg(rtwdev, RTW_DBG_FW, "not 4byte alignment\n");
1630 		return -EINVAL;
1631 	}
1632 
1633 	if (!rtw_fw_dump_check_size(rtwdev, fifo_sel, addr, size)) {
1634 		rtw_dbg(rtwdev, RTW_DBG_FW, "fw fifo dump size overflow\n");
1635 		return -EINVAL;
1636 	}
1637 
1638 	rtw_fw_read_fifo(rtwdev, fifo_sel, addr, size, buffer);
1639 
1640 	return 0;
1641 }
1642 
1643 static void __rtw_fw_update_pkt(struct rtw_dev *rtwdev, u8 pkt_id, u16 size,
1644 				u8 location)
1645 {
1646 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1647 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_UPDATE_PKT_LEN;
1648 
1649 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_UPDATE_PKT);
1650 
1651 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1652 	UPDATE_PKT_SET_PKT_ID(h2c_pkt, pkt_id);
1653 	UPDATE_PKT_SET_LOCATION(h2c_pkt, location);
1654 
1655 	/* include txdesc size */
1656 	UPDATE_PKT_SET_SIZE(h2c_pkt, size);
1657 
1658 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1659 }
1660 
1661 void rtw_fw_update_pkt_probe_req(struct rtw_dev *rtwdev,
1662 				 struct cfg80211_ssid *ssid)
1663 {
1664 	u8 loc;
1665 	u32 size;
1666 
1667 	loc = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid);
1668 	if (!loc) {
1669 		rtw_err(rtwdev, "failed to get probe_req rsvd loc\n");
1670 		return;
1671 	}
1672 
1673 	size = rtw_get_rsvd_page_probe_req_size(rtwdev, ssid);
1674 	if (!size) {
1675 		rtw_err(rtwdev, "failed to get probe_req rsvd size\n");
1676 		return;
1677 	}
1678 
1679 	__rtw_fw_update_pkt(rtwdev, RTW_PACKET_PROBE_REQ, size, loc);
1680 }
1681 
1682 void rtw_fw_channel_switch(struct rtw_dev *rtwdev, bool enable)
1683 {
1684 	struct rtw_pno_request *rtw_pno_req = &rtwdev->wow.pno_req;
1685 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1686 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_CH_SWITCH_LEN;
1687 	u8 loc_ch_info;
1688 	const struct rtw_ch_switch_option cs_option = {
1689 		.dest_ch_en = 1,
1690 		.dest_ch = 1,
1691 		.periodic_option = 2,
1692 		.normal_period = 5,
1693 		.normal_period_sel = 0,
1694 		.normal_cycle = 10,
1695 		.slow_period = 1,
1696 		.slow_period_sel = 1,
1697 	};
1698 
1699 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_CH_SWITCH);
1700 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1701 
1702 	CH_SWITCH_SET_START(h2c_pkt, enable);
1703 	CH_SWITCH_SET_DEST_CH_EN(h2c_pkt, cs_option.dest_ch_en);
1704 	CH_SWITCH_SET_DEST_CH(h2c_pkt, cs_option.dest_ch);
1705 	CH_SWITCH_SET_NORMAL_PERIOD(h2c_pkt, cs_option.normal_period);
1706 	CH_SWITCH_SET_NORMAL_PERIOD_SEL(h2c_pkt, cs_option.normal_period_sel);
1707 	CH_SWITCH_SET_SLOW_PERIOD(h2c_pkt, cs_option.slow_period);
1708 	CH_SWITCH_SET_SLOW_PERIOD_SEL(h2c_pkt, cs_option.slow_period_sel);
1709 	CH_SWITCH_SET_NORMAL_CYCLE(h2c_pkt, cs_option.normal_cycle);
1710 	CH_SWITCH_SET_PERIODIC_OPT(h2c_pkt, cs_option.periodic_option);
1711 
1712 	CH_SWITCH_SET_CH_NUM(h2c_pkt, rtw_pno_req->channel_cnt);
1713 	CH_SWITCH_SET_INFO_SIZE(h2c_pkt, rtw_pno_req->channel_cnt * 4);
1714 
1715 	loc_ch_info = rtw_get_rsvd_page_location(rtwdev, RSVD_CH_INFO);
1716 	CH_SWITCH_SET_INFO_LOC(h2c_pkt, loc_ch_info);
1717 
1718 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1719 }
1720 
1721 void rtw_fw_scan_notify(struct rtw_dev *rtwdev, bool start)
1722 {
1723 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1724 
1725 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_SCAN);
1726 	SET_SCAN_START(h2c_pkt, start);
1727 
1728 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
1729 }
1730