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