xref: /linux/drivers/net/wireless/realtek/rtw88/fw.c (revision 3fd6c59042dbba50391e30862beac979491145fe)
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 #include "phy.h"
18 #include "mac.h"
19 
20 static const struct rtw_hw_reg_desc fw_h2c_regs[] = {
21 	{REG_FWIMR, MASKDWORD, "FWIMR"},
22 	{REG_FWIMR, BIT_FS_H2CCMD_INT_EN, "FWIMR enable"},
23 	{REG_FWISR, MASKDWORD, "FWISR"},
24 	{REG_FWISR, BIT_FS_H2CCMD_INT, "FWISR enable"},
25 	{REG_HMETFR, BIT_INT_BOX_ALL, "BoxBitMap"},
26 	{REG_HMEBOX0, MASKDWORD, "MSG 0"},
27 	{REG_HMEBOX0_EX, MASKDWORD, "MSG_EX 0"},
28 	{REG_HMEBOX1, MASKDWORD, "MSG 1"},
29 	{REG_HMEBOX1_EX, MASKDWORD, "MSG_EX 1"},
30 	{REG_HMEBOX2, MASKDWORD, "MSG 2"},
31 	{REG_HMEBOX2_EX, MASKDWORD, "MSG_EX 2"},
32 	{REG_HMEBOX3, MASKDWORD, "MSG 3"},
33 	{REG_HMEBOX3_EX, MASKDWORD, "MSG_EX 3"},
34 	{REG_FT1IMR, MASKDWORD, "FT1IMR"},
35 	{REG_FT1IMR, BIT_FS_H2C_CMD_OK_INT_EN, "FT1IMR enable"},
36 	{REG_FT1ISR, MASKDWORD, "FT1ISR"},
37 	{REG_FT1ISR, BIT_FS_H2C_CMD_OK_INT, "FT1ISR enable "},
38 };
39 
40 static const struct rtw_hw_reg_desc fw_c2h_regs[] = {
41 	{REG_FWIMR, MASKDWORD, "FWIMR"},
42 	{REG_FWIMR, BIT_FS_H2CCMD_INT_EN, "CPWM"},
43 	{REG_FWIMR, BIT_FS_HRCV_INT_EN, "HRECV"},
44 	{REG_FWISR, MASKDWORD, "FWISR"},
45 	{REG_FWISR, BIT_FS_H2CCMD_INT, "CPWM"},
46 	{REG_FWISR, BIT_FS_HRCV_INT, "HRECV"},
47 	{REG_CPWM, MASKDWORD, "REG_CPWM"},
48 };
49 
50 static const struct rtw_hw_reg_desc fw_core_regs[] = {
51 	{REG_ARFR2_V1, MASKDWORD, "EPC"},
52 	{REG_ARFRH2_V1, MASKDWORD, "BADADDR"},
53 	{REG_ARFR3_V1, MASKDWORD, "CAUSE"},
54 	{REG_ARFR3_V1, BIT_EXC_CODE, "ExcCode"},
55 	{REG_ARFRH3_V1, MASKDWORD, "Status"},
56 	{REG_ARFR4, MASKDWORD, "SP"},
57 	{REG_ARFRH4, MASKDWORD, "RA"},
58 	{REG_FW_DBG6, MASKDWORD, "DBG 6"},
59 	{REG_FW_DBG7, MASKDWORD, "DBG 7"},
60 };
61 
_rtw_fw_dump_dbg_info(struct rtw_dev * rtwdev,const struct rtw_hw_reg_desc regs[],u32 size)62 static void _rtw_fw_dump_dbg_info(struct rtw_dev *rtwdev,
63 				  const struct rtw_hw_reg_desc regs[], u32 size)
64 {
65 	const struct rtw_hw_reg_desc *reg;
66 	u32 val;
67 	int i;
68 
69 	for (i = 0;  i < size; i++) {
70 		reg = &regs[i];
71 		val = rtw_read32_mask(rtwdev, reg->addr, reg->mask);
72 
73 		rtw_dbg(rtwdev, RTW_DBG_FW, "[%s]addr:0x%x mask:0x%x value:0x%x\n",
74 			reg->desc, reg->addr, reg->mask, val);
75 	}
76 }
77 
rtw_fw_dump_dbg_info(struct rtw_dev * rtwdev)78 void rtw_fw_dump_dbg_info(struct rtw_dev *rtwdev)
79 {
80 	int i;
81 
82 	if (!rtw_dbg_is_enabled(rtwdev, RTW_DBG_FW))
83 		return;
84 
85 	_rtw_fw_dump_dbg_info(rtwdev, fw_h2c_regs, ARRAY_SIZE(fw_h2c_regs));
86 	_rtw_fw_dump_dbg_info(rtwdev, fw_c2h_regs, ARRAY_SIZE(fw_c2h_regs));
87 	for (i = 0 ; i < RTW_DEBUG_DUMP_TIMES; i++) {
88 		rtw_dbg(rtwdev, RTW_DBG_FW, "Firmware Coredump %dth\n", i + 1);
89 		_rtw_fw_dump_dbg_info(rtwdev, fw_core_regs, ARRAY_SIZE(fw_core_regs));
90 	}
91 }
92 
rtw_fw_c2h_cmd_handle_ext(struct rtw_dev * rtwdev,struct sk_buff * skb)93 static void rtw_fw_c2h_cmd_handle_ext(struct rtw_dev *rtwdev,
94 				      struct sk_buff *skb)
95 {
96 	struct rtw_c2h_cmd *c2h;
97 	u8 sub_cmd_id;
98 
99 	c2h = get_c2h_from_skb(skb);
100 	sub_cmd_id = c2h->payload[0];
101 
102 	switch (sub_cmd_id) {
103 	case C2H_CCX_RPT:
104 		rtw_tx_report_handle(rtwdev, skb, C2H_CCX_RPT);
105 		break;
106 	case C2H_SCAN_STATUS_RPT:
107 		rtw_hw_scan_status_report(rtwdev, skb);
108 		break;
109 	case C2H_CHAN_SWITCH:
110 		rtw_hw_scan_chan_switch(rtwdev, skb);
111 		break;
112 	default:
113 		break;
114 	}
115 }
116 
get_max_amsdu_len(u32 bit_rate)117 static u16 get_max_amsdu_len(u32 bit_rate)
118 {
119 	/* lower than ofdm, do not aggregate */
120 	if (bit_rate < 550)
121 		return 1;
122 
123 	/* lower than 20M 2ss mcs8, make it small */
124 	if (bit_rate < 1800)
125 		return 1200;
126 
127 	/* lower than 40M 2ss mcs9, make it medium */
128 	if (bit_rate < 4000)
129 		return 2600;
130 
131 	/* not yet 80M 2ss mcs8/9, make it twice regular packet size */
132 	if (bit_rate < 7000)
133 		return 3500;
134 
135 	/* unlimited */
136 	return 0;
137 }
138 
139 struct rtw_fw_iter_ra_data {
140 	struct rtw_dev *rtwdev;
141 	u8 *payload;
142 	u8 length;
143 };
144 
rtw_fw_ra_report_iter(void * data,struct ieee80211_sta * sta)145 static void rtw_fw_ra_report_iter(void *data, struct ieee80211_sta *sta)
146 {
147 	struct rtw_fw_iter_ra_data *ra_data = data;
148 	struct rtw_c2h_ra_rpt *ra_rpt = (struct rtw_c2h_ra_rpt *)ra_data->payload;
149 	struct rtw_sta_info *si = (struct rtw_sta_info *)sta->drv_priv;
150 	u8 mac_id, rate, sgi, bw;
151 	u8 mcs, nss;
152 	u32 bit_rate;
153 
154 	mac_id = ra_rpt->mac_id;
155 	if (si->mac_id != mac_id)
156 		return;
157 
158 	si->ra_report.txrate.flags = 0;
159 
160 	rate = u8_get_bits(ra_rpt->rate_sgi, RTW_C2H_RA_RPT_RATE);
161 	sgi = u8_get_bits(ra_rpt->rate_sgi, RTW_C2H_RA_RPT_SGI);
162 	if (ra_data->length >= offsetofend(typeof(*ra_rpt), bw))
163 		bw = ra_rpt->bw;
164 	else
165 		bw = si->bw_mode;
166 
167 	if (rate < DESC_RATEMCS0) {
168 		si->ra_report.txrate.legacy = rtw_desc_to_bitrate(rate);
169 		goto legacy;
170 	}
171 
172 	rtw_desc_to_mcsrate(rate, &mcs, &nss);
173 	if (rate >= DESC_RATEVHT1SS_MCS0)
174 		si->ra_report.txrate.flags |= RATE_INFO_FLAGS_VHT_MCS;
175 	else if (rate >= DESC_RATEMCS0)
176 		si->ra_report.txrate.flags |= RATE_INFO_FLAGS_MCS;
177 
178 	if (rate >= DESC_RATEMCS0) {
179 		si->ra_report.txrate.mcs = mcs;
180 		si->ra_report.txrate.nss = nss;
181 	}
182 
183 	if (sgi)
184 		si->ra_report.txrate.flags |= RATE_INFO_FLAGS_SHORT_GI;
185 
186 	if (bw == RTW_CHANNEL_WIDTH_80)
187 		si->ra_report.txrate.bw = RATE_INFO_BW_80;
188 	else if (bw == RTW_CHANNEL_WIDTH_40)
189 		si->ra_report.txrate.bw = RATE_INFO_BW_40;
190 	else
191 		si->ra_report.txrate.bw = RATE_INFO_BW_20;
192 
193 legacy:
194 	bit_rate = cfg80211_calculate_bitrate(&si->ra_report.txrate);
195 
196 	si->ra_report.desc_rate = rate;
197 	si->ra_report.bit_rate = bit_rate;
198 
199 	sta->deflink.agg.max_rc_amsdu_len = get_max_amsdu_len(bit_rate);
200 }
201 
rtw_fw_ra_report_handle(struct rtw_dev * rtwdev,u8 * payload,u8 length)202 static void rtw_fw_ra_report_handle(struct rtw_dev *rtwdev, u8 *payload,
203 				    u8 length)
204 {
205 	struct rtw_c2h_ra_rpt *ra_rpt = (struct rtw_c2h_ra_rpt *)payload;
206 	struct rtw_fw_iter_ra_data ra_data;
207 
208 	if (WARN(length < rtwdev->chip->c2h_ra_report_size,
209 		 "invalid ra report c2h length %d\n", length))
210 		return;
211 
212 	rtwdev->dm_info.tx_rate = u8_get_bits(ra_rpt->rate_sgi,
213 					      RTW_C2H_RA_RPT_RATE);
214 	ra_data.rtwdev = rtwdev;
215 	ra_data.payload = payload;
216 	ra_data.length = length;
217 	rtw_iterate_stas_atomic(rtwdev, rtw_fw_ra_report_iter, &ra_data);
218 }
219 
220 struct rtw_beacon_filter_iter_data {
221 	struct rtw_dev *rtwdev;
222 	u8 *payload;
223 };
224 
rtw_fw_bcn_filter_notify_vif_iter(void * data,struct ieee80211_vif * vif)225 static void rtw_fw_bcn_filter_notify_vif_iter(void *data,
226 					      struct ieee80211_vif *vif)
227 {
228 	struct rtw_beacon_filter_iter_data *iter_data = data;
229 	struct rtw_dev *rtwdev = iter_data->rtwdev;
230 	u8 *payload = iter_data->payload;
231 	u8 type = GET_BCN_FILTER_NOTIFY_TYPE(payload);
232 	u8 event = GET_BCN_FILTER_NOTIFY_EVENT(payload);
233 	s8 sig = (s8)GET_BCN_FILTER_NOTIFY_RSSI(payload);
234 
235 	switch (type) {
236 	case BCN_FILTER_NOTIFY_SIGNAL_CHANGE:
237 		event = event ? NL80211_CQM_RSSI_THRESHOLD_EVENT_HIGH :
238 			NL80211_CQM_RSSI_THRESHOLD_EVENT_LOW;
239 		ieee80211_cqm_rssi_notify(vif, event, sig, GFP_KERNEL);
240 		break;
241 	case BCN_FILTER_CONNECTION_LOSS:
242 		ieee80211_connection_loss(vif);
243 		break;
244 	case BCN_FILTER_CONNECTED:
245 		rtwdev->beacon_loss = false;
246 		break;
247 	case BCN_FILTER_NOTIFY_BEACON_LOSS:
248 		rtwdev->beacon_loss = true;
249 		rtw_leave_lps(rtwdev);
250 		break;
251 	}
252 }
253 
rtw_fw_bcn_filter_notify(struct rtw_dev * rtwdev,u8 * payload,u8 length)254 static void rtw_fw_bcn_filter_notify(struct rtw_dev *rtwdev, u8 *payload,
255 				     u8 length)
256 {
257 	struct rtw_beacon_filter_iter_data dev_iter_data;
258 
259 	dev_iter_data.rtwdev = rtwdev;
260 	dev_iter_data.payload = payload;
261 	rtw_iterate_vifs(rtwdev, rtw_fw_bcn_filter_notify_vif_iter,
262 			 &dev_iter_data);
263 }
264 
rtw_fw_scan_result(struct rtw_dev * rtwdev,u8 * payload,u8 length)265 static void rtw_fw_scan_result(struct rtw_dev *rtwdev, u8 *payload,
266 			       u8 length)
267 {
268 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
269 
270 	dm_info->scan_density = payload[0];
271 
272 	rtw_dbg(rtwdev, RTW_DBG_FW, "scan.density = %x\n",
273 		dm_info->scan_density);
274 }
275 
rtw_fw_adaptivity_result(struct rtw_dev * rtwdev,u8 * payload,u8 length)276 static void rtw_fw_adaptivity_result(struct rtw_dev *rtwdev, u8 *payload,
277 				     u8 length)
278 {
279 	const struct rtw_hw_reg_offset *edcca_th = rtwdev->chip->edcca_th;
280 	struct rtw_c2h_adaptivity *result = (struct rtw_c2h_adaptivity *)payload;
281 
282 	rtw_dbg(rtwdev, RTW_DBG_ADAPTIVITY,
283 		"Adaptivity: density %x igi %x l2h_th_init %x l2h %x h2l %x option %x\n",
284 		result->density, result->igi, result->l2h_th_init, result->l2h,
285 		result->h2l, result->option);
286 
287 	rtw_dbg(rtwdev, RTW_DBG_ADAPTIVITY, "Reg Setting: L2H %x H2L %x\n",
288 		rtw_read32_mask(rtwdev, edcca_th[EDCCA_TH_L2H_IDX].hw_reg.addr,
289 				edcca_th[EDCCA_TH_L2H_IDX].hw_reg.mask),
290 		rtw_read32_mask(rtwdev, edcca_th[EDCCA_TH_H2L_IDX].hw_reg.addr,
291 				edcca_th[EDCCA_TH_H2L_IDX].hw_reg.mask));
292 
293 	rtw_dbg(rtwdev, RTW_DBG_ADAPTIVITY, "EDCCA Flag %s\n",
294 		rtw_read32_mask(rtwdev, REG_EDCCA_REPORT, BIT_EDCCA_FLAG) ?
295 		"Set" : "Unset");
296 }
297 
rtw_fw_c2h_cmd_handle(struct rtw_dev * rtwdev,struct sk_buff * skb)298 void rtw_fw_c2h_cmd_handle(struct rtw_dev *rtwdev, struct sk_buff *skb)
299 {
300 	struct rtw_c2h_cmd *c2h;
301 	u32 pkt_offset;
302 	u8 len;
303 
304 	pkt_offset = *((u32 *)skb->cb);
305 	c2h = (struct rtw_c2h_cmd *)(skb->data + pkt_offset);
306 	len = skb->len - pkt_offset - 2;
307 
308 	mutex_lock(&rtwdev->mutex);
309 
310 	if (!test_bit(RTW_FLAG_RUNNING, rtwdev->flags))
311 		goto unlock;
312 
313 	switch (c2h->id) {
314 	case C2H_CCX_TX_RPT:
315 		rtw_tx_report_handle(rtwdev, skb, C2H_CCX_TX_RPT);
316 		break;
317 	case C2H_BT_INFO:
318 		rtw_coex_bt_info_notify(rtwdev, c2h->payload, len);
319 		break;
320 	case C2H_BT_HID_INFO:
321 		rtw_coex_bt_hid_info_notify(rtwdev, c2h->payload, len);
322 		break;
323 	case C2H_WLAN_INFO:
324 		rtw_coex_wl_fwdbginfo_notify(rtwdev, c2h->payload, len);
325 		break;
326 	case C2H_BCN_FILTER_NOTIFY:
327 		rtw_fw_bcn_filter_notify(rtwdev, c2h->payload, len);
328 		break;
329 	case C2H_HALMAC:
330 		rtw_fw_c2h_cmd_handle_ext(rtwdev, skb);
331 		break;
332 	case C2H_RA_RPT:
333 		rtw_fw_ra_report_handle(rtwdev, c2h->payload, len);
334 		break;
335 	default:
336 		rtw_dbg(rtwdev, RTW_DBG_FW, "C2H 0x%x isn't handled\n", c2h->id);
337 		break;
338 	}
339 
340 unlock:
341 	mutex_unlock(&rtwdev->mutex);
342 }
343 
rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev * rtwdev,u32 pkt_offset,struct sk_buff * skb)344 void rtw_fw_c2h_cmd_rx_irqsafe(struct rtw_dev *rtwdev, u32 pkt_offset,
345 			       struct sk_buff *skb)
346 {
347 	struct rtw_c2h_cmd *c2h;
348 	u8 len;
349 
350 	c2h = (struct rtw_c2h_cmd *)(skb->data + pkt_offset);
351 	len = skb->len - pkt_offset - 2;
352 	*((u32 *)skb->cb) = pkt_offset;
353 
354 	rtw_dbg(rtwdev, RTW_DBG_FW, "recv C2H, id=0x%02x, seq=0x%02x, len=%d\n",
355 		c2h->id, c2h->seq, len);
356 
357 	switch (c2h->id) {
358 	case C2H_BT_MP_INFO:
359 		rtw_coex_info_response(rtwdev, skb);
360 		break;
361 	case C2H_WLAN_RFON:
362 		complete(&rtwdev->lps_leave_check);
363 		dev_kfree_skb_any(skb);
364 		break;
365 	case C2H_SCAN_RESULT:
366 		complete(&rtwdev->fw_scan_density);
367 		rtw_fw_scan_result(rtwdev, c2h->payload, len);
368 		dev_kfree_skb_any(skb);
369 		break;
370 	case C2H_ADAPTIVITY:
371 		rtw_fw_adaptivity_result(rtwdev, c2h->payload, len);
372 		dev_kfree_skb_any(skb);
373 		break;
374 	default:
375 		/* pass offset for further operation */
376 		*((u32 *)skb->cb) = pkt_offset;
377 		skb_queue_tail(&rtwdev->c2h_queue, skb);
378 		ieee80211_queue_work(rtwdev->hw, &rtwdev->c2h_work);
379 		break;
380 	}
381 }
382 EXPORT_SYMBOL(rtw_fw_c2h_cmd_rx_irqsafe);
383 
rtw_fw_c2h_cmd_isr(struct rtw_dev * rtwdev)384 void rtw_fw_c2h_cmd_isr(struct rtw_dev *rtwdev)
385 {
386 	if (rtw_read8(rtwdev, REG_MCU_TST_CFG) == VAL_FW_TRIGGER)
387 		rtw_fw_recovery(rtwdev);
388 	else
389 		rtw_warn(rtwdev, "unhandled firmware c2h interrupt\n");
390 }
391 EXPORT_SYMBOL(rtw_fw_c2h_cmd_isr);
392 
rtw_fw_send_h2c_command_register(struct rtw_dev * rtwdev,struct rtw_h2c_register * h2c)393 static void rtw_fw_send_h2c_command_register(struct rtw_dev *rtwdev,
394 					     struct rtw_h2c_register *h2c)
395 {
396 	u32 box_reg, box_ex_reg;
397 	u8 box_state, box;
398 	int ret;
399 
400 	rtw_dbg(rtwdev, RTW_DBG_FW, "send H2C content %08x %08x\n", h2c->w0,
401 		h2c->w1);
402 
403 	lockdep_assert_held(&rtwdev->mutex);
404 
405 	box = rtwdev->h2c.last_box_num;
406 	switch (box) {
407 	case 0:
408 		box_reg = REG_HMEBOX0;
409 		box_ex_reg = REG_HMEBOX0_EX;
410 		break;
411 	case 1:
412 		box_reg = REG_HMEBOX1;
413 		box_ex_reg = REG_HMEBOX1_EX;
414 		break;
415 	case 2:
416 		box_reg = REG_HMEBOX2;
417 		box_ex_reg = REG_HMEBOX2_EX;
418 		break;
419 	case 3:
420 		box_reg = REG_HMEBOX3;
421 		box_ex_reg = REG_HMEBOX3_EX;
422 		break;
423 	default:
424 		WARN(1, "invalid h2c mail box number\n");
425 		return;
426 	}
427 
428 	ret = read_poll_timeout_atomic(rtw_read8, box_state,
429 				       !((box_state >> box) & 0x1), 100, 3000,
430 				       false, rtwdev, REG_HMETFR);
431 
432 	if (ret) {
433 		rtw_err(rtwdev, "failed to send h2c command\n");
434 		rtw_fw_dump_dbg_info(rtwdev);
435 		return;
436 	}
437 
438 	rtw_write32(rtwdev, box_ex_reg, h2c->w1);
439 	rtw_write32(rtwdev, box_reg, h2c->w0);
440 
441 	if (++rtwdev->h2c.last_box_num >= 4)
442 		rtwdev->h2c.last_box_num = 0;
443 }
444 
rtw_fw_send_h2c_command(struct rtw_dev * rtwdev,u8 * h2c)445 static void rtw_fw_send_h2c_command(struct rtw_dev *rtwdev,
446 				    u8 *h2c)
447 {
448 	struct rtw_h2c_cmd *h2c_cmd = (struct rtw_h2c_cmd *)h2c;
449 	u8 box;
450 	u8 box_state;
451 	u32 box_reg, box_ex_reg;
452 	int ret;
453 
454 	rtw_dbg(rtwdev, RTW_DBG_FW,
455 		"send H2C content %02x%02x%02x%02x %02x%02x%02x%02x\n",
456 		h2c[3], h2c[2], h2c[1], h2c[0],
457 		h2c[7], h2c[6], h2c[5], h2c[4]);
458 
459 	lockdep_assert_held(&rtwdev->mutex);
460 
461 	box = rtwdev->h2c.last_box_num;
462 	switch (box) {
463 	case 0:
464 		box_reg = REG_HMEBOX0;
465 		box_ex_reg = REG_HMEBOX0_EX;
466 		break;
467 	case 1:
468 		box_reg = REG_HMEBOX1;
469 		box_ex_reg = REG_HMEBOX1_EX;
470 		break;
471 	case 2:
472 		box_reg = REG_HMEBOX2;
473 		box_ex_reg = REG_HMEBOX2_EX;
474 		break;
475 	case 3:
476 		box_reg = REG_HMEBOX3;
477 		box_ex_reg = REG_HMEBOX3_EX;
478 		break;
479 	default:
480 		WARN(1, "invalid h2c mail box number\n");
481 		return;
482 	}
483 
484 	ret = read_poll_timeout_atomic(rtw_read8, box_state,
485 				       !((box_state >> box) & 0x1), 100, 3000,
486 				       false, rtwdev, REG_HMETFR);
487 
488 	if (ret) {
489 		rtw_err(rtwdev, "failed to send h2c command\n");
490 		return;
491 	}
492 
493 	rtw_write32(rtwdev, box_ex_reg, le32_to_cpu(h2c_cmd->msg_ext));
494 	rtw_write32(rtwdev, box_reg, le32_to_cpu(h2c_cmd->msg));
495 
496 	if (++rtwdev->h2c.last_box_num >= 4)
497 		rtwdev->h2c.last_box_num = 0;
498 }
499 
rtw_fw_h2c_cmd_dbg(struct rtw_dev * rtwdev,u8 * h2c)500 void rtw_fw_h2c_cmd_dbg(struct rtw_dev *rtwdev, u8 *h2c)
501 {
502 	rtw_fw_send_h2c_command(rtwdev, h2c);
503 }
504 
rtw_fw_send_h2c_packet(struct rtw_dev * rtwdev,u8 * h2c_pkt)505 static void rtw_fw_send_h2c_packet(struct rtw_dev *rtwdev, u8 *h2c_pkt)
506 {
507 	int ret;
508 
509 	lockdep_assert_held(&rtwdev->mutex);
510 
511 	FW_OFFLOAD_H2C_SET_SEQ_NUM(h2c_pkt, rtwdev->h2c.seq);
512 	ret = rtw_hci_write_data_h2c(rtwdev, h2c_pkt, H2C_PKT_SIZE);
513 	if (ret)
514 		rtw_err(rtwdev, "failed to send h2c packet\n");
515 	rtwdev->h2c.seq++;
516 }
517 
518 void
rtw_fw_send_general_info(struct rtw_dev * rtwdev)519 rtw_fw_send_general_info(struct rtw_dev *rtwdev)
520 {
521 	struct rtw_fifo_conf *fifo = &rtwdev->fifo;
522 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
523 	u16 total_size = H2C_PKT_HDR_SIZE + 4;
524 
525 	if (rtw_chip_wcpu_11n(rtwdev))
526 		return;
527 
528 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_GENERAL_INFO);
529 
530 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
531 
532 	GENERAL_INFO_SET_FW_TX_BOUNDARY(h2c_pkt,
533 					fifo->rsvd_fw_txbuf_addr -
534 					fifo->rsvd_boundary);
535 
536 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
537 }
538 
539 void
rtw_fw_send_phydm_info(struct rtw_dev * rtwdev)540 rtw_fw_send_phydm_info(struct rtw_dev *rtwdev)
541 {
542 	struct rtw_hal *hal = &rtwdev->hal;
543 	struct rtw_efuse *efuse = &rtwdev->efuse;
544 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
545 	u16 total_size = H2C_PKT_HDR_SIZE + 8;
546 	u8 fw_rf_type = 0;
547 
548 	if (rtw_chip_wcpu_11n(rtwdev))
549 		return;
550 
551 	if (hal->rf_type == RF_1T1R)
552 		fw_rf_type = FW_RF_1T1R;
553 	else if (hal->rf_type == RF_2T2R)
554 		fw_rf_type = FW_RF_2T2R;
555 
556 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_PHYDM_INFO);
557 
558 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
559 	PHYDM_INFO_SET_REF_TYPE(h2c_pkt, efuse->rfe_option);
560 	PHYDM_INFO_SET_RF_TYPE(h2c_pkt, fw_rf_type);
561 	PHYDM_INFO_SET_CUT_VER(h2c_pkt, hal->cut_version);
562 	PHYDM_INFO_SET_RX_ANT_STATUS(h2c_pkt, hal->antenna_tx);
563 	PHYDM_INFO_SET_TX_ANT_STATUS(h2c_pkt, hal->antenna_rx);
564 
565 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
566 }
567 
rtw_fw_do_iqk(struct rtw_dev * rtwdev,struct rtw_iqk_para * para)568 void rtw_fw_do_iqk(struct rtw_dev *rtwdev, struct rtw_iqk_para *para)
569 {
570 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
571 	u16 total_size = H2C_PKT_HDR_SIZE + 1;
572 
573 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_IQK);
574 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
575 	IQK_SET_CLEAR(h2c_pkt, para->clear);
576 	IQK_SET_SEGMENT_IQK(h2c_pkt, para->segment_iqk);
577 
578 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
579 }
580 EXPORT_SYMBOL(rtw_fw_do_iqk);
581 
rtw_fw_inform_rfk_status(struct rtw_dev * rtwdev,bool start)582 void rtw_fw_inform_rfk_status(struct rtw_dev *rtwdev, bool start)
583 {
584 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
585 
586 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WIFI_CALIBRATION);
587 
588 	RFK_SET_INFORM_START(h2c_pkt, start);
589 
590 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
591 }
592 EXPORT_SYMBOL(rtw_fw_inform_rfk_status);
593 
rtw_fw_query_bt_info(struct rtw_dev * rtwdev)594 void rtw_fw_query_bt_info(struct rtw_dev *rtwdev)
595 {
596 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
597 
598 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_INFO);
599 
600 	SET_QUERY_BT_INFO(h2c_pkt, true);
601 
602 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
603 }
604 
rtw_fw_default_port(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif)605 void rtw_fw_default_port(struct rtw_dev *rtwdev, struct rtw_vif *rtwvif)
606 {
607 	struct rtw_h2c_register h2c = {};
608 
609 	if (rtwvif->net_type != RTW_NET_MGD_LINKED)
610 		return;
611 
612 	/* Leave LPS before default port H2C so FW timer is correct */
613 	rtw_leave_lps(rtwdev);
614 
615 	h2c.w0 = u32_encode_bits(H2C_CMD_DEFAULT_PORT, RTW_H2C_W0_CMDID) |
616 		 u32_encode_bits(rtwvif->port, RTW_H2C_DEFAULT_PORT_W0_PORTID) |
617 		 u32_encode_bits(rtwvif->mac_id, RTW_H2C_DEFAULT_PORT_W0_MACID);
618 
619 	rtw_fw_send_h2c_command_register(rtwdev, &h2c);
620 }
621 
rtw_fw_wl_ch_info(struct rtw_dev * rtwdev,u8 link,u8 ch,u8 bw)622 void rtw_fw_wl_ch_info(struct rtw_dev *rtwdev, u8 link, u8 ch, u8 bw)
623 {
624 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
625 
626 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WL_CH_INFO);
627 
628 	SET_WL_CH_INFO_LINK(h2c_pkt, link);
629 	SET_WL_CH_INFO_CHNL(h2c_pkt, ch);
630 	SET_WL_CH_INFO_BW(h2c_pkt, bw);
631 
632 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
633 }
634 
rtw_fw_query_bt_mp_info(struct rtw_dev * rtwdev,struct rtw_coex_info_req * req)635 void rtw_fw_query_bt_mp_info(struct rtw_dev *rtwdev,
636 			     struct rtw_coex_info_req *req)
637 {
638 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
639 
640 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_MP_INFO);
641 
642 	SET_BT_MP_INFO_SEQ(h2c_pkt, req->seq);
643 	SET_BT_MP_INFO_OP_CODE(h2c_pkt, req->op_code);
644 	SET_BT_MP_INFO_PARA1(h2c_pkt, req->para1);
645 	SET_BT_MP_INFO_PARA2(h2c_pkt, req->para2);
646 	SET_BT_MP_INFO_PARA3(h2c_pkt, req->para3);
647 
648 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
649 }
650 
rtw_fw_force_bt_tx_power(struct rtw_dev * rtwdev,u8 bt_pwr_dec_lvl)651 void rtw_fw_force_bt_tx_power(struct rtw_dev *rtwdev, u8 bt_pwr_dec_lvl)
652 {
653 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
654 	u8 index = 0 - bt_pwr_dec_lvl;
655 
656 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_FORCE_BT_TX_POWER);
657 
658 	SET_BT_TX_POWER_INDEX(h2c_pkt, index);
659 
660 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
661 }
662 
rtw_fw_bt_ignore_wlan_action(struct rtw_dev * rtwdev,bool enable)663 void rtw_fw_bt_ignore_wlan_action(struct rtw_dev *rtwdev, bool enable)
664 {
665 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
666 
667 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_IGNORE_WLAN_ACTION);
668 
669 	SET_IGNORE_WLAN_ACTION_EN(h2c_pkt, enable);
670 
671 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
672 }
673 
rtw_fw_coex_tdma_type(struct rtw_dev * rtwdev,u8 para1,u8 para2,u8 para3,u8 para4,u8 para5)674 void rtw_fw_coex_tdma_type(struct rtw_dev *rtwdev,
675 			   u8 para1, u8 para2, u8 para3, u8 para4, u8 para5)
676 {
677 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
678 
679 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_COEX_TDMA_TYPE);
680 
681 	SET_COEX_TDMA_TYPE_PARA1(h2c_pkt, para1);
682 	SET_COEX_TDMA_TYPE_PARA2(h2c_pkt, para2);
683 	SET_COEX_TDMA_TYPE_PARA3(h2c_pkt, para3);
684 	SET_COEX_TDMA_TYPE_PARA4(h2c_pkt, para4);
685 	SET_COEX_TDMA_TYPE_PARA5(h2c_pkt, para5);
686 
687 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
688 }
689 
rtw_fw_coex_query_hid_info(struct rtw_dev * rtwdev,u8 sub_id,u8 data)690 void rtw_fw_coex_query_hid_info(struct rtw_dev *rtwdev, u8 sub_id, u8 data)
691 {
692 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
693 
694 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_QUERY_BT_HID_INFO);
695 
696 	SET_COEX_QUERY_HID_INFO_SUBID(h2c_pkt, sub_id);
697 	SET_COEX_QUERY_HID_INFO_DATA1(h2c_pkt, data);
698 
699 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
700 }
701 
rtw_fw_bt_wifi_control(struct rtw_dev * rtwdev,u8 op_code,u8 * data)702 void rtw_fw_bt_wifi_control(struct rtw_dev *rtwdev, u8 op_code, u8 *data)
703 {
704 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
705 
706 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BT_WIFI_CONTROL);
707 
708 	SET_BT_WIFI_CONTROL_OP_CODE(h2c_pkt, op_code);
709 
710 	SET_BT_WIFI_CONTROL_DATA1(h2c_pkt, *data);
711 	SET_BT_WIFI_CONTROL_DATA2(h2c_pkt, *(data + 1));
712 	SET_BT_WIFI_CONTROL_DATA3(h2c_pkt, *(data + 2));
713 	SET_BT_WIFI_CONTROL_DATA4(h2c_pkt, *(data + 3));
714 	SET_BT_WIFI_CONTROL_DATA5(h2c_pkt, *(data + 4));
715 
716 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
717 }
718 
rtw_fw_send_rssi_info(struct rtw_dev * rtwdev,struct rtw_sta_info * si)719 void rtw_fw_send_rssi_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si)
720 {
721 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
722 	u8 rssi = ewma_rssi_read(&si->avg_rssi);
723 	bool stbc_en = si->stbc_en ? true : false;
724 
725 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSSI_MONITOR);
726 
727 	SET_RSSI_INFO_MACID(h2c_pkt, si->mac_id);
728 	SET_RSSI_INFO_RSSI(h2c_pkt, rssi);
729 	SET_RSSI_INFO_STBC(h2c_pkt, stbc_en);
730 
731 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
732 }
733 
rtw_fw_send_ra_info(struct rtw_dev * rtwdev,struct rtw_sta_info * si,bool reset_ra_mask)734 void rtw_fw_send_ra_info(struct rtw_dev *rtwdev, struct rtw_sta_info *si,
735 			 bool reset_ra_mask)
736 {
737 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
738 	bool disable_pt = true;
739 
740 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RA_INFO);
741 
742 	SET_RA_INFO_MACID(h2c_pkt, si->mac_id);
743 	SET_RA_INFO_RATE_ID(h2c_pkt, si->rate_id);
744 	SET_RA_INFO_INIT_RA_LVL(h2c_pkt, si->init_ra_lv);
745 	SET_RA_INFO_SGI_EN(h2c_pkt, si->sgi_enable);
746 	SET_RA_INFO_BW_MODE(h2c_pkt, si->bw_mode);
747 	SET_RA_INFO_LDPC(h2c_pkt, !!si->ldpc_en);
748 	SET_RA_INFO_NO_UPDATE(h2c_pkt, !reset_ra_mask);
749 	SET_RA_INFO_VHT_EN(h2c_pkt, si->vht_enable);
750 	SET_RA_INFO_DIS_PT(h2c_pkt, disable_pt);
751 	SET_RA_INFO_RA_MASK0(h2c_pkt, (si->ra_mask & 0xff));
752 	SET_RA_INFO_RA_MASK1(h2c_pkt, (si->ra_mask & 0xff00) >> 8);
753 	SET_RA_INFO_RA_MASK2(h2c_pkt, (si->ra_mask & 0xff0000) >> 16);
754 	SET_RA_INFO_RA_MASK3(h2c_pkt, (si->ra_mask & 0xff000000) >> 24);
755 
756 	si->init_ra_lv = 0;
757 
758 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
759 }
760 
rtw_fw_media_status_report(struct rtw_dev * rtwdev,u8 mac_id,bool connect)761 void rtw_fw_media_status_report(struct rtw_dev *rtwdev, u8 mac_id, bool connect)
762 {
763 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
764 
765 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_MEDIA_STATUS_RPT);
766 	MEDIA_STATUS_RPT_SET_OP_MODE(h2c_pkt, connect);
767 	MEDIA_STATUS_RPT_SET_MACID(h2c_pkt, mac_id);
768 
769 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
770 }
771 
rtw_fw_update_wl_phy_info(struct rtw_dev * rtwdev)772 void rtw_fw_update_wl_phy_info(struct rtw_dev *rtwdev)
773 {
774 	struct rtw_traffic_stats *stats = &rtwdev->stats;
775 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
776 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
777 
778 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WL_PHY_INFO);
779 	SET_WL_PHY_INFO_TX_TP(h2c_pkt, stats->tx_throughput);
780 	SET_WL_PHY_INFO_RX_TP(h2c_pkt, stats->rx_throughput);
781 	SET_WL_PHY_INFO_TX_RATE_DESC(h2c_pkt, dm_info->tx_rate);
782 	SET_WL_PHY_INFO_RX_RATE_DESC(h2c_pkt, dm_info->curr_rx_rate);
783 	SET_WL_PHY_INFO_RX_EVM(h2c_pkt, dm_info->rx_evm_dbm[RF_PATH_A]);
784 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
785 }
786 
rtw_fw_beacon_filter_config(struct rtw_dev * rtwdev,bool connect,struct ieee80211_vif * vif)787 void rtw_fw_beacon_filter_config(struct rtw_dev *rtwdev, bool connect,
788 				 struct ieee80211_vif *vif)
789 {
790 	struct ieee80211_bss_conf *bss_conf = &vif->bss_conf;
791 	struct ieee80211_sta *sta = ieee80211_find_sta(vif, bss_conf->bssid);
792 	static const u8 rssi_min = 0, rssi_max = 100, rssi_offset = 100;
793 	struct rtw_sta_info *si =
794 		sta ? (struct rtw_sta_info *)sta->drv_priv : NULL;
795 	s32 thold = RTW_DEFAULT_CQM_THOLD;
796 	u32 hyst = RTW_DEFAULT_CQM_HYST;
797 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
798 
799 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_BCN_FILTER))
800 		return;
801 
802 	if (bss_conf->cqm_rssi_thold)
803 		thold = bss_conf->cqm_rssi_thold;
804 	if (bss_conf->cqm_rssi_hyst)
805 		hyst = bss_conf->cqm_rssi_hyst;
806 
807 	if (!connect) {
808 		SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BCN_FILTER_OFFLOAD_P1);
809 		SET_BCN_FILTER_OFFLOAD_P1_ENABLE(h2c_pkt, connect);
810 		rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
811 
812 		return;
813 	}
814 
815 	if (!si)
816 		return;
817 
818 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BCN_FILTER_OFFLOAD_P0);
819 	ether_addr_copy(&h2c_pkt[1], bss_conf->bssid);
820 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
821 
822 	memset(h2c_pkt, 0, sizeof(h2c_pkt));
823 	thold = clamp_t(s32, thold + rssi_offset, rssi_min, rssi_max);
824 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_BCN_FILTER_OFFLOAD_P1);
825 	SET_BCN_FILTER_OFFLOAD_P1_ENABLE(h2c_pkt, connect);
826 	SET_BCN_FILTER_OFFLOAD_P1_OFFLOAD_MODE(h2c_pkt,
827 					       BCN_FILTER_OFFLOAD_MODE_DEFAULT);
828 	SET_BCN_FILTER_OFFLOAD_P1_THRESHOLD(h2c_pkt, thold);
829 	SET_BCN_FILTER_OFFLOAD_P1_BCN_LOSS_CNT(h2c_pkt, BCN_LOSS_CNT);
830 	SET_BCN_FILTER_OFFLOAD_P1_MACID(h2c_pkt, si->mac_id);
831 	SET_BCN_FILTER_OFFLOAD_P1_HYST(h2c_pkt, hyst);
832 	SET_BCN_FILTER_OFFLOAD_P1_BCN_INTERVAL(h2c_pkt, bss_conf->beacon_int);
833 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
834 }
835 
rtw_fw_set_pwr_mode(struct rtw_dev * rtwdev)836 void rtw_fw_set_pwr_mode(struct rtw_dev *rtwdev)
837 {
838 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
839 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
840 
841 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_SET_PWR_MODE);
842 
843 	SET_PWR_MODE_SET_MODE(h2c_pkt, conf->mode);
844 	SET_PWR_MODE_SET_RLBM(h2c_pkt, conf->rlbm);
845 	SET_PWR_MODE_SET_SMART_PS(h2c_pkt, conf->smart_ps);
846 	SET_PWR_MODE_SET_AWAKE_INTERVAL(h2c_pkt, conf->awake_interval);
847 	SET_PWR_MODE_SET_PORT_ID(h2c_pkt, conf->port_id);
848 	SET_PWR_MODE_SET_PWR_STATE(h2c_pkt, conf->state);
849 
850 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
851 }
852 
rtw_fw_set_keep_alive_cmd(struct rtw_dev * rtwdev,bool enable)853 void rtw_fw_set_keep_alive_cmd(struct rtw_dev *rtwdev, bool enable)
854 {
855 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
856 	struct rtw_fw_wow_keep_alive_para mode = {
857 		.adopt = true,
858 		.pkt_type = KEEP_ALIVE_NULL_PKT,
859 		.period = 5,
860 	};
861 
862 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_KEEP_ALIVE);
863 	SET_KEEP_ALIVE_ENABLE(h2c_pkt, enable);
864 	SET_KEEP_ALIVE_ADOPT(h2c_pkt, mode.adopt);
865 	SET_KEEP_ALIVE_PKT_TYPE(h2c_pkt, mode.pkt_type);
866 	SET_KEEP_ALIVE_CHECK_PERIOD(h2c_pkt, mode.period);
867 
868 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
869 }
870 
rtw_fw_set_disconnect_decision_cmd(struct rtw_dev * rtwdev,bool enable)871 void rtw_fw_set_disconnect_decision_cmd(struct rtw_dev *rtwdev, bool enable)
872 {
873 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
874 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
875 	struct rtw_fw_wow_disconnect_para mode = {
876 		.adopt = true,
877 		.period = 30,
878 		.retry_count = 5,
879 	};
880 
881 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_DISCONNECT_DECISION);
882 
883 	if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags)) {
884 		SET_DISCONNECT_DECISION_ENABLE(h2c_pkt, enable);
885 		SET_DISCONNECT_DECISION_ADOPT(h2c_pkt, mode.adopt);
886 		SET_DISCONNECT_DECISION_CHECK_PERIOD(h2c_pkt, mode.period);
887 		SET_DISCONNECT_DECISION_TRY_PKT_NUM(h2c_pkt, mode.retry_count);
888 	}
889 
890 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
891 }
892 
rtw_fw_set_wowlan_ctrl_cmd(struct rtw_dev * rtwdev,bool enable)893 void rtw_fw_set_wowlan_ctrl_cmd(struct rtw_dev *rtwdev, bool enable)
894 {
895 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
896 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
897 
898 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_WOWLAN);
899 
900 	SET_WOWLAN_FUNC_ENABLE(h2c_pkt, enable);
901 	if (rtw_wow_mgd_linked(rtwdev)) {
902 		if (test_bit(RTW_WOW_FLAG_EN_MAGIC_PKT, rtw_wow->flags))
903 			SET_WOWLAN_MAGIC_PKT_ENABLE(h2c_pkt, enable);
904 		if (test_bit(RTW_WOW_FLAG_EN_DISCONNECT, rtw_wow->flags))
905 			SET_WOWLAN_DEAUTH_WAKEUP_ENABLE(h2c_pkt, enable);
906 		if (test_bit(RTW_WOW_FLAG_EN_REKEY_PKT, rtw_wow->flags))
907 			SET_WOWLAN_REKEY_WAKEUP_ENABLE(h2c_pkt, enable);
908 		if (rtw_wow->pattern_cnt)
909 			SET_WOWLAN_PATTERN_MATCH_ENABLE(h2c_pkt, enable);
910 	}
911 
912 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
913 }
914 
rtw_fw_set_aoac_global_info_cmd(struct rtw_dev * rtwdev,u8 pairwise_key_enc,u8 group_key_enc)915 void rtw_fw_set_aoac_global_info_cmd(struct rtw_dev *rtwdev,
916 				     u8 pairwise_key_enc,
917 				     u8 group_key_enc)
918 {
919 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
920 
921 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_AOAC_GLOBAL_INFO);
922 
923 	SET_AOAC_GLOBAL_INFO_PAIRWISE_ENC_ALG(h2c_pkt, pairwise_key_enc);
924 	SET_AOAC_GLOBAL_INFO_GROUP_ENC_ALG(h2c_pkt, group_key_enc);
925 
926 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
927 }
928 
rtw_fw_set_remote_wake_ctrl_cmd(struct rtw_dev * rtwdev,bool enable)929 void rtw_fw_set_remote_wake_ctrl_cmd(struct rtw_dev *rtwdev, bool enable)
930 {
931 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
932 
933 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_REMOTE_WAKE_CTRL);
934 
935 	SET_REMOTE_WAKECTRL_ENABLE(h2c_pkt, enable);
936 
937 	if (rtw_wow_no_link(rtwdev))
938 		SET_REMOTE_WAKE_CTRL_NLO_OFFLOAD_EN(h2c_pkt, enable);
939 
940 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
941 }
942 
rtw_get_rsvd_page_location(struct rtw_dev * rtwdev,enum rtw_rsvd_packet_type type)943 static u8 rtw_get_rsvd_page_location(struct rtw_dev *rtwdev,
944 				     enum rtw_rsvd_packet_type type)
945 {
946 	struct rtw_rsvd_page *rsvd_pkt;
947 	u8 location = 0;
948 
949 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
950 		if (type == rsvd_pkt->type)
951 			location = rsvd_pkt->page;
952 	}
953 
954 	return location;
955 }
956 
rtw_fw_set_nlo_info(struct rtw_dev * rtwdev,bool enable)957 void rtw_fw_set_nlo_info(struct rtw_dev *rtwdev, bool enable)
958 {
959 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
960 	u8 loc_nlo;
961 
962 	loc_nlo = rtw_get_rsvd_page_location(rtwdev, RSVD_NLO_INFO);
963 
964 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_NLO_INFO);
965 
966 	SET_NLO_FUN_EN(h2c_pkt, enable);
967 	if (enable) {
968 		if (rtw_get_lps_deep_mode(rtwdev) != LPS_DEEP_MODE_NONE)
969 			SET_NLO_PS_32K(h2c_pkt, enable);
970 		SET_NLO_IGNORE_SECURITY(h2c_pkt, enable);
971 		SET_NLO_LOC_NLO_INFO(h2c_pkt, loc_nlo);
972 	}
973 
974 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
975 }
976 
rtw_fw_set_recover_bt_device(struct rtw_dev * rtwdev)977 void rtw_fw_set_recover_bt_device(struct rtw_dev *rtwdev)
978 {
979 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
980 
981 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RECOVER_BT_DEV);
982 	SET_RECOVER_BT_DEV_EN(h2c_pkt, 1);
983 
984 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
985 }
986 
rtw_fw_set_pg_info(struct rtw_dev * rtwdev)987 void rtw_fw_set_pg_info(struct rtw_dev *rtwdev)
988 {
989 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
990 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
991 	u8 loc_pg, loc_dpk;
992 
993 	loc_pg = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_INFO);
994 	loc_dpk = rtw_get_rsvd_page_location(rtwdev, RSVD_LPS_PG_DPK);
995 
996 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_LPS_PG_INFO);
997 
998 	LPS_PG_INFO_LOC(h2c_pkt, loc_pg);
999 	LPS_PG_DPK_LOC(h2c_pkt, loc_dpk);
1000 	LPS_PG_SEC_CAM_EN(h2c_pkt, conf->sec_cam_backup);
1001 	LPS_PG_PATTERN_CAM_EN(h2c_pkt, conf->pattern_cam_backup);
1002 
1003 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
1004 }
1005 
rtw_get_rsvd_page_probe_req_location(struct rtw_dev * rtwdev,struct cfg80211_ssid * ssid)1006 static u8 rtw_get_rsvd_page_probe_req_location(struct rtw_dev *rtwdev,
1007 					       struct cfg80211_ssid *ssid)
1008 {
1009 	struct rtw_rsvd_page *rsvd_pkt;
1010 	u8 location = 0;
1011 
1012 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1013 		if (rsvd_pkt->type != RSVD_PROBE_REQ)
1014 			continue;
1015 		if ((!ssid && !rsvd_pkt->ssid) ||
1016 		    cfg80211_ssid_eq(rsvd_pkt->ssid, ssid))
1017 			location = rsvd_pkt->page;
1018 	}
1019 
1020 	return location;
1021 }
1022 
rtw_get_rsvd_page_probe_req_size(struct rtw_dev * rtwdev,struct cfg80211_ssid * ssid)1023 static u16 rtw_get_rsvd_page_probe_req_size(struct rtw_dev *rtwdev,
1024 					    struct cfg80211_ssid *ssid)
1025 {
1026 	struct rtw_rsvd_page *rsvd_pkt;
1027 	u16 size = 0;
1028 
1029 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1030 		if (rsvd_pkt->type != RSVD_PROBE_REQ)
1031 			continue;
1032 		if ((!ssid && !rsvd_pkt->ssid) ||
1033 		    cfg80211_ssid_eq(rsvd_pkt->ssid, ssid))
1034 			size = rsvd_pkt->probe_req_size;
1035 	}
1036 
1037 	return size;
1038 }
1039 
rtw_send_rsvd_page_h2c(struct rtw_dev * rtwdev)1040 void rtw_send_rsvd_page_h2c(struct rtw_dev *rtwdev)
1041 {
1042 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1043 	u8 location = 0;
1044 
1045 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_RSVD_PAGE);
1046 
1047 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_PROBE_RESP);
1048 	*(h2c_pkt + 1) = location;
1049 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PROBE_RESP loc: %d\n", location);
1050 
1051 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_PS_POLL);
1052 	*(h2c_pkt + 2) = location;
1053 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_PS_POLL loc: %d\n", location);
1054 
1055 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_NULL);
1056 	*(h2c_pkt + 3) = location;
1057 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_NULL loc: %d\n", location);
1058 
1059 	location = rtw_get_rsvd_page_location(rtwdev, RSVD_QOS_NULL);
1060 	*(h2c_pkt + 4) = location;
1061 	rtw_dbg(rtwdev, RTW_DBG_FW, "RSVD_QOS_NULL loc: %d\n", location);
1062 
1063 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
1064 }
1065 
rtw_nlo_info_get(struct ieee80211_hw * hw)1066 static struct sk_buff *rtw_nlo_info_get(struct ieee80211_hw *hw)
1067 {
1068 	struct rtw_dev *rtwdev = hw->priv;
1069 	const struct rtw_chip_info *chip = rtwdev->chip;
1070 	struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req;
1071 	struct rtw_nlo_info_hdr *nlo_hdr;
1072 	struct cfg80211_ssid *ssid;
1073 	struct sk_buff *skb;
1074 	u8 *pos, loc;
1075 	u32 size;
1076 	int i;
1077 
1078 	if (!pno_req->inited || !pno_req->match_set_cnt)
1079 		return NULL;
1080 
1081 	size = sizeof(struct rtw_nlo_info_hdr) + pno_req->match_set_cnt *
1082 		      IEEE80211_MAX_SSID_LEN + chip->tx_pkt_desc_sz;
1083 
1084 	skb = alloc_skb(size, GFP_KERNEL);
1085 	if (!skb)
1086 		return NULL;
1087 
1088 	skb_reserve(skb, chip->tx_pkt_desc_sz);
1089 
1090 	nlo_hdr = skb_put_zero(skb, sizeof(struct rtw_nlo_info_hdr));
1091 
1092 	nlo_hdr->nlo_count = pno_req->match_set_cnt;
1093 	nlo_hdr->hidden_ap_count = pno_req->match_set_cnt;
1094 
1095 	/* pattern check for firmware */
1096 	memset(nlo_hdr->pattern_check, 0xA5, FW_NLO_INFO_CHECK_SIZE);
1097 
1098 	for (i = 0; i < pno_req->match_set_cnt; i++)
1099 		nlo_hdr->ssid_len[i] = pno_req->match_sets[i].ssid.ssid_len;
1100 
1101 	for (i = 0; i < pno_req->match_set_cnt; i++) {
1102 		ssid = &pno_req->match_sets[i].ssid;
1103 		loc  = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid);
1104 		if (!loc) {
1105 			rtw_err(rtwdev, "failed to get probe req rsvd loc\n");
1106 			kfree_skb(skb);
1107 			return NULL;
1108 		}
1109 		nlo_hdr->location[i] = loc;
1110 	}
1111 
1112 	for (i = 0; i < pno_req->match_set_cnt; i++) {
1113 		pos = skb_put_zero(skb, IEEE80211_MAX_SSID_LEN);
1114 		memcpy(pos, pno_req->match_sets[i].ssid.ssid,
1115 		       pno_req->match_sets[i].ssid.ssid_len);
1116 	}
1117 
1118 	return skb;
1119 }
1120 
rtw_cs_channel_info_get(struct ieee80211_hw * hw)1121 static struct sk_buff *rtw_cs_channel_info_get(struct ieee80211_hw *hw)
1122 {
1123 	struct rtw_dev *rtwdev = hw->priv;
1124 	const struct rtw_chip_info *chip = rtwdev->chip;
1125 	struct rtw_pno_request *pno_req = &rtwdev->wow.pno_req;
1126 	struct ieee80211_channel *channels = pno_req->channels;
1127 	struct sk_buff *skb;
1128 	int count =  pno_req->channel_cnt;
1129 	u8 *pos;
1130 	int i = 0;
1131 
1132 	skb = alloc_skb(4 * count + chip->tx_pkt_desc_sz, GFP_KERNEL);
1133 	if (!skb)
1134 		return NULL;
1135 
1136 	skb_reserve(skb, chip->tx_pkt_desc_sz);
1137 
1138 	for (i = 0; i < count; i++) {
1139 		pos = skb_put_zero(skb, 4);
1140 
1141 		CHSW_INFO_SET_CH(pos, channels[i].hw_value);
1142 
1143 		if (channels[i].flags & IEEE80211_CHAN_RADAR)
1144 			CHSW_INFO_SET_ACTION_ID(pos, 0);
1145 		else
1146 			CHSW_INFO_SET_ACTION_ID(pos, 1);
1147 		CHSW_INFO_SET_TIMEOUT(pos, 1);
1148 		CHSW_INFO_SET_PRI_CH_IDX(pos, 1);
1149 		CHSW_INFO_SET_BW(pos, 0);
1150 	}
1151 
1152 	return skb;
1153 }
1154 
rtw_lps_pg_dpk_get(struct ieee80211_hw * hw)1155 static struct sk_buff *rtw_lps_pg_dpk_get(struct ieee80211_hw *hw)
1156 {
1157 	struct rtw_dev *rtwdev = hw->priv;
1158 	const struct rtw_chip_info *chip = rtwdev->chip;
1159 	struct rtw_dpk_info *dpk_info = &rtwdev->dm_info.dpk_info;
1160 	struct rtw_lps_pg_dpk_hdr *dpk_hdr;
1161 	struct sk_buff *skb;
1162 	u32 size;
1163 
1164 	size = chip->tx_pkt_desc_sz + sizeof(*dpk_hdr);
1165 	skb = alloc_skb(size, GFP_KERNEL);
1166 	if (!skb)
1167 		return NULL;
1168 
1169 	skb_reserve(skb, chip->tx_pkt_desc_sz);
1170 	dpk_hdr = skb_put_zero(skb, sizeof(*dpk_hdr));
1171 	dpk_hdr->dpk_ch = dpk_info->dpk_ch;
1172 	dpk_hdr->dpk_path_ok = dpk_info->dpk_path_ok[0];
1173 	memcpy(dpk_hdr->dpk_txagc, dpk_info->dpk_txagc, 2);
1174 	memcpy(dpk_hdr->dpk_gs, dpk_info->dpk_gs, 4);
1175 	memcpy(dpk_hdr->coef, dpk_info->coef, 160);
1176 
1177 	return skb;
1178 }
1179 
rtw_lps_pg_info_get(struct ieee80211_hw * hw)1180 static struct sk_buff *rtw_lps_pg_info_get(struct ieee80211_hw *hw)
1181 {
1182 	struct rtw_dev *rtwdev = hw->priv;
1183 	const struct rtw_chip_info *chip = rtwdev->chip;
1184 	struct rtw_lps_conf *conf = &rtwdev->lps_conf;
1185 	struct rtw_lps_pg_info_hdr *pg_info_hdr;
1186 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
1187 	struct sk_buff *skb;
1188 	u32 size;
1189 
1190 	size = chip->tx_pkt_desc_sz + sizeof(*pg_info_hdr);
1191 	skb = alloc_skb(size, GFP_KERNEL);
1192 	if (!skb)
1193 		return NULL;
1194 
1195 	skb_reserve(skb, chip->tx_pkt_desc_sz);
1196 	pg_info_hdr = skb_put_zero(skb, sizeof(*pg_info_hdr));
1197 	pg_info_hdr->tx_bu_page_count = rtwdev->fifo.rsvd_drv_pg_num;
1198 	pg_info_hdr->macid = find_first_bit(rtwdev->mac_id_map, RTW_MAX_MAC_ID_NUM);
1199 	pg_info_hdr->sec_cam_count =
1200 		rtw_sec_cam_pg_backup(rtwdev, pg_info_hdr->sec_cam);
1201 	pg_info_hdr->pattern_count = rtw_wow->pattern_cnt;
1202 
1203 	conf->sec_cam_backup = pg_info_hdr->sec_cam_count != 0;
1204 	conf->pattern_cam_backup = rtw_wow->pattern_cnt != 0;
1205 
1206 	return skb;
1207 }
1208 
rtw_get_rsvd_page_skb(struct ieee80211_hw * hw,struct rtw_rsvd_page * rsvd_pkt)1209 static struct sk_buff *rtw_get_rsvd_page_skb(struct ieee80211_hw *hw,
1210 					     struct rtw_rsvd_page *rsvd_pkt)
1211 {
1212 	struct ieee80211_vif *vif;
1213 	struct rtw_vif *rtwvif;
1214 	struct sk_buff *skb_new;
1215 	struct cfg80211_ssid *ssid;
1216 	u16 tim_offset = 0;
1217 
1218 	if (rsvd_pkt->type == RSVD_DUMMY) {
1219 		skb_new = alloc_skb(1, GFP_KERNEL);
1220 		if (!skb_new)
1221 			return NULL;
1222 
1223 		skb_put(skb_new, 1);
1224 		return skb_new;
1225 	}
1226 
1227 	rtwvif = rsvd_pkt->rtwvif;
1228 	if (!rtwvif)
1229 		return NULL;
1230 
1231 	vif = rtwvif_to_vif(rtwvif);
1232 
1233 	switch (rsvd_pkt->type) {
1234 	case RSVD_BEACON:
1235 		skb_new = ieee80211_beacon_get_tim(hw, vif, &tim_offset, NULL, 0);
1236 		rsvd_pkt->tim_offset = tim_offset;
1237 		break;
1238 	case RSVD_PS_POLL:
1239 		skb_new = ieee80211_pspoll_get(hw, vif);
1240 		break;
1241 	case RSVD_PROBE_RESP:
1242 		skb_new = ieee80211_proberesp_get(hw, vif);
1243 		break;
1244 	case RSVD_NULL:
1245 		skb_new = ieee80211_nullfunc_get(hw, vif, -1, false);
1246 		break;
1247 	case RSVD_QOS_NULL:
1248 		skb_new = ieee80211_nullfunc_get(hw, vif, -1, true);
1249 		break;
1250 	case RSVD_LPS_PG_DPK:
1251 		skb_new = rtw_lps_pg_dpk_get(hw);
1252 		break;
1253 	case RSVD_LPS_PG_INFO:
1254 		skb_new = rtw_lps_pg_info_get(hw);
1255 		break;
1256 	case RSVD_PROBE_REQ:
1257 		ssid = (struct cfg80211_ssid *)rsvd_pkt->ssid;
1258 		if (ssid)
1259 			skb_new = ieee80211_probereq_get(hw, vif->addr,
1260 							 ssid->ssid,
1261 							 ssid->ssid_len, 0);
1262 		else
1263 			skb_new = ieee80211_probereq_get(hw, vif->addr, NULL, 0, 0);
1264 		if (skb_new)
1265 			rsvd_pkt->probe_req_size = (u16)skb_new->len;
1266 		break;
1267 	case RSVD_NLO_INFO:
1268 		skb_new = rtw_nlo_info_get(hw);
1269 		break;
1270 	case RSVD_CH_INFO:
1271 		skb_new = rtw_cs_channel_info_get(hw);
1272 		break;
1273 	default:
1274 		return NULL;
1275 	}
1276 
1277 	if (!skb_new)
1278 		return NULL;
1279 
1280 	return skb_new;
1281 }
1282 
rtw_fill_rsvd_page_desc(struct rtw_dev * rtwdev,struct sk_buff * skb,enum rtw_rsvd_packet_type type)1283 static void rtw_fill_rsvd_page_desc(struct rtw_dev *rtwdev, struct sk_buff *skb,
1284 				    enum rtw_rsvd_packet_type type)
1285 {
1286 	struct rtw_tx_pkt_info pkt_info = {0};
1287 	const struct rtw_chip_info *chip = rtwdev->chip;
1288 	u8 *pkt_desc;
1289 
1290 	rtw_tx_rsvd_page_pkt_info_update(rtwdev, &pkt_info, skb, type);
1291 	pkt_desc = skb_push(skb, chip->tx_pkt_desc_sz);
1292 	memset(pkt_desc, 0, chip->tx_pkt_desc_sz);
1293 	rtw_tx_fill_tx_desc(rtwdev, &pkt_info, skb);
1294 }
1295 
rtw_len_to_page(unsigned int len,u16 page_size)1296 static inline u8 rtw_len_to_page(unsigned int len, u16 page_size)
1297 {
1298 	return DIV_ROUND_UP(len, page_size);
1299 }
1300 
rtw_rsvd_page_list_to_buf(struct rtw_dev * rtwdev,u16 page_size,u16 page_margin,u32 page,u8 * buf,struct rtw_rsvd_page * rsvd_pkt)1301 static void rtw_rsvd_page_list_to_buf(struct rtw_dev *rtwdev, u16 page_size,
1302 				      u16 page_margin, u32 page, u8 *buf,
1303 				      struct rtw_rsvd_page *rsvd_pkt)
1304 {
1305 	struct sk_buff *skb = rsvd_pkt->skb;
1306 
1307 	if (page >= 1)
1308 		memcpy(buf + page_margin + page_size * (page - 1),
1309 		       skb->data, skb->len);
1310 	else
1311 		memcpy(buf, skb->data, skb->len);
1312 }
1313 
rtw_alloc_rsvd_page(struct rtw_dev * rtwdev,enum rtw_rsvd_packet_type type,bool txdesc)1314 static struct rtw_rsvd_page *rtw_alloc_rsvd_page(struct rtw_dev *rtwdev,
1315 						 enum rtw_rsvd_packet_type type,
1316 						 bool txdesc)
1317 {
1318 	struct rtw_rsvd_page *rsvd_pkt = NULL;
1319 
1320 	rsvd_pkt = kzalloc(sizeof(*rsvd_pkt), GFP_KERNEL);
1321 
1322 	if (!rsvd_pkt)
1323 		return NULL;
1324 
1325 	INIT_LIST_HEAD(&rsvd_pkt->vif_list);
1326 	INIT_LIST_HEAD(&rsvd_pkt->build_list);
1327 	rsvd_pkt->type = type;
1328 	rsvd_pkt->add_txdesc = txdesc;
1329 
1330 	return rsvd_pkt;
1331 }
1332 
rtw_insert_rsvd_page(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,struct rtw_rsvd_page * rsvd_pkt)1333 static void rtw_insert_rsvd_page(struct rtw_dev *rtwdev,
1334 				 struct rtw_vif *rtwvif,
1335 				 struct rtw_rsvd_page *rsvd_pkt)
1336 {
1337 	lockdep_assert_held(&rtwdev->mutex);
1338 
1339 	list_add_tail(&rsvd_pkt->vif_list, &rtwvif->rsvd_page_list);
1340 }
1341 
rtw_add_rsvd_page(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,enum rtw_rsvd_packet_type type,bool txdesc)1342 static void rtw_add_rsvd_page(struct rtw_dev *rtwdev,
1343 			      struct rtw_vif *rtwvif,
1344 			      enum rtw_rsvd_packet_type type,
1345 			      bool txdesc)
1346 {
1347 	struct rtw_rsvd_page *rsvd_pkt;
1348 
1349 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, type, txdesc);
1350 	if (!rsvd_pkt) {
1351 		rtw_err(rtwdev, "failed to alloc rsvd page %d\n", type);
1352 		return;
1353 	}
1354 
1355 	rsvd_pkt->rtwvif = rtwvif;
1356 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1357 }
1358 
rtw_add_rsvd_page_probe_req(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,struct cfg80211_ssid * ssid)1359 static void rtw_add_rsvd_page_probe_req(struct rtw_dev *rtwdev,
1360 					struct rtw_vif *rtwvif,
1361 					struct cfg80211_ssid *ssid)
1362 {
1363 	struct rtw_rsvd_page *rsvd_pkt;
1364 
1365 	rsvd_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_PROBE_REQ, true);
1366 	if (!rsvd_pkt) {
1367 		rtw_err(rtwdev, "failed to alloc probe req rsvd page\n");
1368 		return;
1369 	}
1370 
1371 	rsvd_pkt->rtwvif = rtwvif;
1372 	rsvd_pkt->ssid = ssid;
1373 	rtw_insert_rsvd_page(rtwdev, rtwvif, rsvd_pkt);
1374 }
1375 
rtw_remove_rsvd_page(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif)1376 void rtw_remove_rsvd_page(struct rtw_dev *rtwdev,
1377 			  struct rtw_vif *rtwvif)
1378 {
1379 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1380 
1381 	lockdep_assert_held(&rtwdev->mutex);
1382 
1383 	/* remove all of the rsvd pages for vif */
1384 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwvif->rsvd_page_list,
1385 				 vif_list) {
1386 		list_del(&rsvd_pkt->vif_list);
1387 		if (!list_empty(&rsvd_pkt->build_list))
1388 			list_del(&rsvd_pkt->build_list);
1389 		kfree(rsvd_pkt);
1390 	}
1391 }
1392 
rtw_add_rsvd_page_bcn(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif)1393 void rtw_add_rsvd_page_bcn(struct rtw_dev *rtwdev,
1394 			   struct rtw_vif *rtwvif)
1395 {
1396 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1397 
1398 	if (vif->type != NL80211_IFTYPE_AP &&
1399 	    vif->type != NL80211_IFTYPE_ADHOC &&
1400 	    vif->type != NL80211_IFTYPE_MESH_POINT) {
1401 		rtw_warn(rtwdev, "Cannot add beacon rsvd page for %d\n",
1402 			 vif->type);
1403 		return;
1404 	}
1405 
1406 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_BEACON, false);
1407 }
1408 
rtw_add_rsvd_page_pno(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif)1409 void rtw_add_rsvd_page_pno(struct rtw_dev *rtwdev,
1410 			   struct rtw_vif *rtwvif)
1411 {
1412 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1413 	struct rtw_wow_param *rtw_wow = &rtwdev->wow;
1414 	struct rtw_pno_request *rtw_pno_req = &rtw_wow->pno_req;
1415 	struct cfg80211_ssid *ssid;
1416 	int i;
1417 
1418 	if (vif->type != NL80211_IFTYPE_STATION) {
1419 		rtw_warn(rtwdev, "Cannot add PNO rsvd page for %d\n",
1420 			 vif->type);
1421 		return;
1422 	}
1423 
1424 	for (i = 0 ; i < rtw_pno_req->match_set_cnt; i++) {
1425 		ssid = &rtw_pno_req->match_sets[i].ssid;
1426 		rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, ssid);
1427 	}
1428 
1429 	rtw_add_rsvd_page_probe_req(rtwdev, rtwvif, NULL);
1430 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NLO_INFO, false);
1431 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_CH_INFO, true);
1432 }
1433 
rtw_add_rsvd_page_sta(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif)1434 void rtw_add_rsvd_page_sta(struct rtw_dev *rtwdev,
1435 			   struct rtw_vif *rtwvif)
1436 {
1437 	struct ieee80211_vif *vif = rtwvif_to_vif(rtwvif);
1438 
1439 	if (vif->type != NL80211_IFTYPE_STATION) {
1440 		rtw_warn(rtwdev, "Cannot add sta rsvd page for %d\n",
1441 			 vif->type);
1442 		return;
1443 	}
1444 
1445 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_PS_POLL, true);
1446 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_QOS_NULL, true);
1447 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_NULL, true);
1448 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_DPK, true);
1449 	rtw_add_rsvd_page(rtwdev, rtwvif, RSVD_LPS_PG_INFO, true);
1450 }
1451 
rtw_fw_write_data_rsvd_page(struct rtw_dev * rtwdev,u16 pg_addr,u8 * buf,u32 size)1452 int rtw_fw_write_data_rsvd_page(struct rtw_dev *rtwdev, u16 pg_addr,
1453 				u8 *buf, u32 size)
1454 {
1455 	u8 bckp[2];
1456 	u8 val;
1457 	u16 rsvd_pg_head;
1458 	u32 bcn_valid_addr;
1459 	u32 bcn_valid_mask;
1460 	int ret;
1461 
1462 	lockdep_assert_held(&rtwdev->mutex);
1463 
1464 	if (!size)
1465 		return -EINVAL;
1466 
1467 	if (rtw_chip_wcpu_11n(rtwdev)) {
1468 		rtw_write32_set(rtwdev, REG_DWBCN0_CTRL, BIT_BCN_VALID);
1469 	} else {
1470 		pg_addr &= BIT_MASK_BCN_HEAD_1_V1;
1471 		pg_addr |= BIT_BCN_VALID_V1;
1472 		rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2, pg_addr);
1473 	}
1474 
1475 	val = rtw_read8(rtwdev, REG_CR + 1);
1476 	bckp[0] = val;
1477 	val |= BIT_ENSWBCN >> 8;
1478 	rtw_write8(rtwdev, REG_CR + 1, val);
1479 
1480 	if (rtw_hci_type(rtwdev) == RTW_HCI_TYPE_PCIE) {
1481 		val = rtw_read8(rtwdev, REG_FWHW_TXQ_CTRL + 2);
1482 		bckp[1] = val;
1483 		val &= ~(BIT_EN_BCNQ_DL >> 16);
1484 		rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, val);
1485 	}
1486 
1487 	ret = rtw_hci_write_data_rsvd_page(rtwdev, buf, size);
1488 	if (ret) {
1489 		rtw_err(rtwdev, "failed to write data to rsvd page\n");
1490 		goto restore;
1491 	}
1492 
1493 	if (rtw_chip_wcpu_11n(rtwdev)) {
1494 		bcn_valid_addr = REG_DWBCN0_CTRL;
1495 		bcn_valid_mask = BIT_BCN_VALID;
1496 	} else {
1497 		bcn_valid_addr = REG_FIFOPAGE_CTRL_2;
1498 		bcn_valid_mask = BIT_BCN_VALID_V1;
1499 	}
1500 
1501 	if (!check_hw_ready(rtwdev, bcn_valid_addr, bcn_valid_mask, 1)) {
1502 		rtw_err(rtwdev, "error beacon valid\n");
1503 		ret = -EBUSY;
1504 	}
1505 
1506 restore:
1507 	rsvd_pg_head = rtwdev->fifo.rsvd_boundary;
1508 	rtw_write16(rtwdev, REG_FIFOPAGE_CTRL_2,
1509 		    rsvd_pg_head | BIT_BCN_VALID_V1);
1510 	if (rtw_hci_type(rtwdev) == RTW_HCI_TYPE_PCIE)
1511 		rtw_write8(rtwdev, REG_FWHW_TXQ_CTRL + 2, bckp[1]);
1512 	rtw_write8(rtwdev, REG_CR + 1, bckp[0]);
1513 
1514 	return ret;
1515 }
1516 
rtw_download_drv_rsvd_page(struct rtw_dev * rtwdev,u8 * buf,u32 size)1517 static int rtw_download_drv_rsvd_page(struct rtw_dev *rtwdev, u8 *buf, u32 size)
1518 {
1519 	u32 pg_size;
1520 	u32 pg_num = 0;
1521 	u16 pg_addr = 0;
1522 
1523 	pg_size = rtwdev->chip->page_size;
1524 	pg_num = size / pg_size + ((size & (pg_size - 1)) ? 1 : 0);
1525 	if (pg_num > rtwdev->fifo.rsvd_drv_pg_num)
1526 		return -ENOMEM;
1527 
1528 	pg_addr = rtwdev->fifo.rsvd_drv_addr;
1529 
1530 	return rtw_fw_write_data_rsvd_page(rtwdev, pg_addr, buf, size);
1531 }
1532 
__rtw_build_rsvd_page_reset(struct rtw_dev * rtwdev)1533 static void __rtw_build_rsvd_page_reset(struct rtw_dev *rtwdev)
1534 {
1535 	struct rtw_rsvd_page *rsvd_pkt, *tmp;
1536 
1537 	list_for_each_entry_safe(rsvd_pkt, tmp, &rtwdev->rsvd_page_list,
1538 				 build_list) {
1539 		list_del_init(&rsvd_pkt->build_list);
1540 
1541 		/* Don't free except for the dummy rsvd page,
1542 		 * others will be freed when removing vif
1543 		 */
1544 		if (rsvd_pkt->type == RSVD_DUMMY)
1545 			kfree(rsvd_pkt);
1546 	}
1547 }
1548 
rtw_build_rsvd_page_iter(void * data,u8 * mac,struct ieee80211_vif * vif)1549 static void rtw_build_rsvd_page_iter(void *data, u8 *mac,
1550 				     struct ieee80211_vif *vif)
1551 {
1552 	struct rtw_dev *rtwdev = data;
1553 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
1554 	struct rtw_rsvd_page *rsvd_pkt;
1555 
1556 	/* AP not yet started, don't gather its rsvd pages */
1557 	if (vif->type == NL80211_IFTYPE_AP && !rtwdev->ap_active)
1558 		return;
1559 
1560 	list_for_each_entry(rsvd_pkt, &rtwvif->rsvd_page_list, vif_list) {
1561 		if (rsvd_pkt->type == RSVD_BEACON)
1562 			list_add(&rsvd_pkt->build_list,
1563 				 &rtwdev->rsvd_page_list);
1564 		else
1565 			list_add_tail(&rsvd_pkt->build_list,
1566 				      &rtwdev->rsvd_page_list);
1567 	}
1568 }
1569 
__rtw_build_rsvd_page_from_vifs(struct rtw_dev * rtwdev)1570 static int  __rtw_build_rsvd_page_from_vifs(struct rtw_dev *rtwdev)
1571 {
1572 	struct rtw_rsvd_page *rsvd_pkt;
1573 
1574 	__rtw_build_rsvd_page_reset(rtwdev);
1575 
1576 	/* gather rsvd page from vifs */
1577 	rtw_iterate_vifs_atomic(rtwdev, rtw_build_rsvd_page_iter, rtwdev);
1578 
1579 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
1580 					    struct rtw_rsvd_page, build_list);
1581 	if (!rsvd_pkt) {
1582 		WARN(1, "Should not have an empty reserved page\n");
1583 		return -EINVAL;
1584 	}
1585 
1586 	/* the first rsvd should be beacon, otherwise add a dummy one */
1587 	if (rsvd_pkt->type != RSVD_BEACON) {
1588 		struct rtw_rsvd_page *dummy_pkt;
1589 
1590 		dummy_pkt = rtw_alloc_rsvd_page(rtwdev, RSVD_DUMMY, false);
1591 		if (!dummy_pkt) {
1592 			rtw_err(rtwdev, "failed to alloc dummy rsvd page\n");
1593 			return -ENOMEM;
1594 		}
1595 
1596 		list_add(&dummy_pkt->build_list, &rtwdev->rsvd_page_list);
1597 	}
1598 
1599 	return 0;
1600 }
1601 
rtw_build_rsvd_page(struct rtw_dev * rtwdev,u32 * size)1602 static u8 *rtw_build_rsvd_page(struct rtw_dev *rtwdev, u32 *size)
1603 {
1604 	const struct rtw_chip_info *chip = rtwdev->chip;
1605 	struct ieee80211_hw *hw = rtwdev->hw;
1606 	struct rtw_rsvd_page *rsvd_pkt;
1607 	struct sk_buff *iter;
1608 	u16 page_size, page_margin, tx_desc_sz;
1609 	u8 total_page = 0;
1610 	u32 page = 0;
1611 	u8 *buf;
1612 	int ret;
1613 
1614 	page_size = chip->page_size;
1615 	tx_desc_sz = chip->tx_pkt_desc_sz;
1616 	page_margin = page_size - tx_desc_sz;
1617 
1618 	ret = __rtw_build_rsvd_page_from_vifs(rtwdev);
1619 	if (ret) {
1620 		rtw_err(rtwdev,
1621 			"failed to build rsvd page from vifs, ret %d\n", ret);
1622 		return NULL;
1623 	}
1624 
1625 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1626 		iter = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1627 		if (!iter) {
1628 			rtw_err(rtwdev, "failed to build rsvd packet\n");
1629 			goto release_skb;
1630 		}
1631 
1632 		/* Fill the tx_desc for the rsvd pkt that requires one.
1633 		 * And iter->len will be added with size of tx_desc_sz.
1634 		 */
1635 		if (rsvd_pkt->add_txdesc)
1636 			rtw_fill_rsvd_page_desc(rtwdev, iter, rsvd_pkt->type);
1637 
1638 		rsvd_pkt->skb = iter;
1639 		rsvd_pkt->page = total_page;
1640 
1641 		/* Reserved page is downloaded via TX path, and TX path will
1642 		 * generate a tx_desc at the header to describe length of
1643 		 * the buffer. If we are not counting page numbers with the
1644 		 * size of tx_desc added at the first rsvd_pkt (usually a
1645 		 * beacon, firmware default refer to the first page as the
1646 		 * content of beacon), we could generate a buffer which size
1647 		 * is smaller than the actual size of the whole rsvd_page
1648 		 */
1649 		if (total_page == 0) {
1650 			if (rsvd_pkt->type != RSVD_BEACON &&
1651 			    rsvd_pkt->type != RSVD_DUMMY) {
1652 				rtw_err(rtwdev, "first page should be a beacon\n");
1653 				goto release_skb;
1654 			}
1655 			total_page += rtw_len_to_page(iter->len + tx_desc_sz,
1656 						      page_size);
1657 		} else {
1658 			total_page += rtw_len_to_page(iter->len, page_size);
1659 		}
1660 	}
1661 
1662 	if (total_page > rtwdev->fifo.rsvd_drv_pg_num) {
1663 		rtw_err(rtwdev, "rsvd page over size: %d\n", total_page);
1664 		goto release_skb;
1665 	}
1666 
1667 	*size = (total_page - 1) * page_size + page_margin;
1668 	buf = kzalloc(*size, GFP_KERNEL);
1669 	if (!buf)
1670 		goto release_skb;
1671 
1672 	/* Copy the content of each rsvd_pkt to the buf, and they should
1673 	 * be aligned to the pages.
1674 	 *
1675 	 * Note that the first rsvd_pkt is a beacon no matter what vif->type.
1676 	 * And that rsvd_pkt does not require tx_desc because when it goes
1677 	 * through TX path, the TX path will generate one for it.
1678 	 */
1679 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1680 		rtw_rsvd_page_list_to_buf(rtwdev, page_size, page_margin,
1681 					  page, buf, rsvd_pkt);
1682 		if (page == 0)
1683 			page += rtw_len_to_page(rsvd_pkt->skb->len +
1684 						tx_desc_sz, page_size);
1685 		else
1686 			page += rtw_len_to_page(rsvd_pkt->skb->len, page_size);
1687 
1688 		kfree_skb(rsvd_pkt->skb);
1689 		rsvd_pkt->skb = NULL;
1690 	}
1691 
1692 	return buf;
1693 
1694 release_skb:
1695 	list_for_each_entry(rsvd_pkt, &rtwdev->rsvd_page_list, build_list) {
1696 		kfree_skb(rsvd_pkt->skb);
1697 		rsvd_pkt->skb = NULL;
1698 	}
1699 
1700 	return NULL;
1701 }
1702 
rtw_download_beacon(struct rtw_dev * rtwdev)1703 static int rtw_download_beacon(struct rtw_dev *rtwdev)
1704 {
1705 	struct ieee80211_hw *hw = rtwdev->hw;
1706 	struct rtw_rsvd_page *rsvd_pkt;
1707 	struct sk_buff *skb;
1708 	int ret = 0;
1709 
1710 	rsvd_pkt = list_first_entry_or_null(&rtwdev->rsvd_page_list,
1711 					    struct rtw_rsvd_page, build_list);
1712 	if (!rsvd_pkt) {
1713 		rtw_err(rtwdev, "failed to get rsvd page from build list\n");
1714 		return -ENOENT;
1715 	}
1716 
1717 	if (rsvd_pkt->type != RSVD_BEACON &&
1718 	    rsvd_pkt->type != RSVD_DUMMY) {
1719 		rtw_err(rtwdev, "invalid rsvd page type %d, should be beacon or dummy\n",
1720 			rsvd_pkt->type);
1721 		return -EINVAL;
1722 	}
1723 
1724 	skb = rtw_get_rsvd_page_skb(hw, rsvd_pkt);
1725 	if (!skb) {
1726 		rtw_err(rtwdev, "failed to get beacon skb\n");
1727 		return -ENOMEM;
1728 	}
1729 
1730 	ret = rtw_download_drv_rsvd_page(rtwdev, skb->data, skb->len);
1731 	if (ret)
1732 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1733 
1734 	dev_kfree_skb(skb);
1735 
1736 	return ret;
1737 }
1738 
rtw_fw_download_rsvd_page(struct rtw_dev * rtwdev)1739 int rtw_fw_download_rsvd_page(struct rtw_dev *rtwdev)
1740 {
1741 	u8 *buf;
1742 	u32 size;
1743 	int ret;
1744 
1745 	buf = rtw_build_rsvd_page(rtwdev, &size);
1746 	if (!buf) {
1747 		rtw_err(rtwdev, "failed to build rsvd page pkt\n");
1748 		return -ENOMEM;
1749 	}
1750 
1751 	ret = rtw_download_drv_rsvd_page(rtwdev, buf, size);
1752 	if (ret) {
1753 		rtw_err(rtwdev, "failed to download drv rsvd page\n");
1754 		goto free;
1755 	}
1756 
1757 	/* The last thing is to download the *ONLY* beacon again, because
1758 	 * the previous tx_desc is to describe the total rsvd page. Download
1759 	 * the beacon again to replace the TX desc header, and we will get
1760 	 * a correct tx_desc for the beacon in the rsvd page.
1761 	 */
1762 	ret = rtw_download_beacon(rtwdev);
1763 	if (ret) {
1764 		rtw_err(rtwdev, "failed to download beacon\n");
1765 		goto free;
1766 	}
1767 
1768 free:
1769 	kfree(buf);
1770 
1771 	return ret;
1772 }
1773 
rtw_fw_update_beacon_work(struct work_struct * work)1774 void rtw_fw_update_beacon_work(struct work_struct *work)
1775 {
1776 	struct rtw_dev *rtwdev = container_of(work, struct rtw_dev,
1777 					      update_beacon_work);
1778 
1779 	mutex_lock(&rtwdev->mutex);
1780 	rtw_fw_download_rsvd_page(rtwdev);
1781 	rtw_send_rsvd_page_h2c(rtwdev);
1782 	mutex_unlock(&rtwdev->mutex);
1783 }
1784 
rtw_fw_read_fifo_page(struct rtw_dev * rtwdev,u32 offset,u32 size,u32 * buf,u32 residue,u16 start_pg)1785 static void rtw_fw_read_fifo_page(struct rtw_dev *rtwdev, u32 offset, u32 size,
1786 				  u32 *buf, u32 residue, u16 start_pg)
1787 {
1788 	u32 i;
1789 	u16 idx = 0;
1790 	u16 ctl;
1791 
1792 	ctl = rtw_read16(rtwdev, REG_PKTBUF_DBG_CTRL) & 0xf000;
1793 	/* disable rx clock gate */
1794 	rtw_write32_set(rtwdev, REG_RCR, BIT_DISGCLK);
1795 
1796 	do {
1797 		rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, start_pg | ctl);
1798 
1799 		for (i = FIFO_DUMP_ADDR + residue;
1800 		     i < FIFO_DUMP_ADDR + FIFO_PAGE_SIZE; i += 4) {
1801 			buf[idx++] = rtw_read32(rtwdev, i);
1802 			size -= 4;
1803 			if (size == 0)
1804 				goto out;
1805 		}
1806 
1807 		residue = 0;
1808 		start_pg++;
1809 	} while (size);
1810 
1811 out:
1812 	rtw_write16(rtwdev, REG_PKTBUF_DBG_CTRL, ctl);
1813 	/* restore rx clock gate */
1814 	rtw_write32_clr(rtwdev, REG_RCR, BIT_DISGCLK);
1815 }
1816 
rtw_fw_read_fifo(struct rtw_dev * rtwdev,enum rtw_fw_fifo_sel sel,u32 offset,u32 size,u32 * buf)1817 static void rtw_fw_read_fifo(struct rtw_dev *rtwdev, enum rtw_fw_fifo_sel sel,
1818 			     u32 offset, u32 size, u32 *buf)
1819 {
1820 	const struct rtw_chip_info *chip = rtwdev->chip;
1821 	u32 start_pg, residue;
1822 
1823 	if (sel >= RTW_FW_FIFO_MAX) {
1824 		rtw_dbg(rtwdev, RTW_DBG_FW, "wrong fw fifo sel\n");
1825 		return;
1826 	}
1827 	if (sel == RTW_FW_FIFO_SEL_RSVD_PAGE)
1828 		offset += rtwdev->fifo.rsvd_boundary << TX_PAGE_SIZE_SHIFT;
1829 	residue = offset & (FIFO_PAGE_SIZE - 1);
1830 	start_pg = (offset >> FIFO_PAGE_SIZE_SHIFT) + chip->fw_fifo_addr[sel];
1831 
1832 	rtw_fw_read_fifo_page(rtwdev, offset, size, buf, residue, start_pg);
1833 }
1834 
rtw_fw_dump_check_size(struct rtw_dev * rtwdev,enum rtw_fw_fifo_sel sel,u32 start_addr,u32 size)1835 static bool rtw_fw_dump_check_size(struct rtw_dev *rtwdev,
1836 				   enum rtw_fw_fifo_sel sel,
1837 				   u32 start_addr, u32 size)
1838 {
1839 	switch (sel) {
1840 	case RTW_FW_FIFO_SEL_TX:
1841 	case RTW_FW_FIFO_SEL_RX:
1842 		if ((start_addr + size) > rtwdev->chip->fw_fifo_addr[sel])
1843 			return false;
1844 		fallthrough;
1845 	default:
1846 		return true;
1847 	}
1848 }
1849 
rtw_fw_dump_fifo(struct rtw_dev * rtwdev,u8 fifo_sel,u32 addr,u32 size,u32 * buffer)1850 int rtw_fw_dump_fifo(struct rtw_dev *rtwdev, u8 fifo_sel, u32 addr, u32 size,
1851 		     u32 *buffer)
1852 {
1853 	if (!rtwdev->chip->fw_fifo_addr[0]) {
1854 		rtw_dbg(rtwdev, RTW_DBG_FW, "chip not support dump fw fifo\n");
1855 		return -ENOTSUPP;
1856 	}
1857 
1858 	if (size == 0 || !buffer)
1859 		return -EINVAL;
1860 
1861 	if (size & 0x3) {
1862 		rtw_dbg(rtwdev, RTW_DBG_FW, "not 4byte alignment\n");
1863 		return -EINVAL;
1864 	}
1865 
1866 	if (!rtw_fw_dump_check_size(rtwdev, fifo_sel, addr, size)) {
1867 		rtw_dbg(rtwdev, RTW_DBG_FW, "fw fifo dump size overflow\n");
1868 		return -EINVAL;
1869 	}
1870 
1871 	rtw_fw_read_fifo(rtwdev, fifo_sel, addr, size, buffer);
1872 
1873 	return 0;
1874 }
1875 
__rtw_fw_update_pkt(struct rtw_dev * rtwdev,u8 pkt_id,u16 size,u8 location)1876 static void __rtw_fw_update_pkt(struct rtw_dev *rtwdev, u8 pkt_id, u16 size,
1877 				u8 location)
1878 {
1879 	const struct rtw_chip_info *chip = rtwdev->chip;
1880 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1881 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_UPDATE_PKT_LEN;
1882 
1883 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_UPDATE_PKT);
1884 
1885 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1886 	UPDATE_PKT_SET_PKT_ID(h2c_pkt, pkt_id);
1887 	UPDATE_PKT_SET_LOCATION(h2c_pkt, location);
1888 
1889 	/* include txdesc size */
1890 	size += chip->tx_pkt_desc_sz;
1891 	UPDATE_PKT_SET_SIZE(h2c_pkt, size);
1892 
1893 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1894 }
1895 
rtw_fw_update_pkt_probe_req(struct rtw_dev * rtwdev,struct cfg80211_ssid * ssid)1896 void rtw_fw_update_pkt_probe_req(struct rtw_dev *rtwdev,
1897 				 struct cfg80211_ssid *ssid)
1898 {
1899 	u8 loc;
1900 	u16 size;
1901 
1902 	loc = rtw_get_rsvd_page_probe_req_location(rtwdev, ssid);
1903 	if (!loc) {
1904 		rtw_err(rtwdev, "failed to get probe_req rsvd loc\n");
1905 		return;
1906 	}
1907 
1908 	size = rtw_get_rsvd_page_probe_req_size(rtwdev, ssid);
1909 	if (!size) {
1910 		rtw_err(rtwdev, "failed to get probe_req rsvd size\n");
1911 		return;
1912 	}
1913 
1914 	__rtw_fw_update_pkt(rtwdev, RTW_PACKET_PROBE_REQ, size, loc);
1915 }
1916 
rtw_fw_channel_switch(struct rtw_dev * rtwdev,bool enable)1917 void rtw_fw_channel_switch(struct rtw_dev *rtwdev, bool enable)
1918 {
1919 	struct rtw_pno_request *rtw_pno_req = &rtwdev->wow.pno_req;
1920 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1921 	u16 total_size = H2C_PKT_HDR_SIZE + H2C_PKT_CH_SWITCH_LEN;
1922 	u8 loc_ch_info;
1923 	const struct rtw_ch_switch_option cs_option = {
1924 		.dest_ch_en = 1,
1925 		.dest_ch = 1,
1926 		.periodic_option = 2,
1927 		.normal_period = 5,
1928 		.normal_period_sel = 0,
1929 		.normal_cycle = 10,
1930 		.slow_period = 1,
1931 		.slow_period_sel = 1,
1932 	};
1933 
1934 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_CH_SWITCH);
1935 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, total_size);
1936 
1937 	CH_SWITCH_SET_START(h2c_pkt, enable);
1938 	CH_SWITCH_SET_DEST_CH_EN(h2c_pkt, cs_option.dest_ch_en);
1939 	CH_SWITCH_SET_DEST_CH(h2c_pkt, cs_option.dest_ch);
1940 	CH_SWITCH_SET_NORMAL_PERIOD(h2c_pkt, cs_option.normal_period);
1941 	CH_SWITCH_SET_NORMAL_PERIOD_SEL(h2c_pkt, cs_option.normal_period_sel);
1942 	CH_SWITCH_SET_SLOW_PERIOD(h2c_pkt, cs_option.slow_period);
1943 	CH_SWITCH_SET_SLOW_PERIOD_SEL(h2c_pkt, cs_option.slow_period_sel);
1944 	CH_SWITCH_SET_NORMAL_CYCLE(h2c_pkt, cs_option.normal_cycle);
1945 	CH_SWITCH_SET_PERIODIC_OPT(h2c_pkt, cs_option.periodic_option);
1946 
1947 	CH_SWITCH_SET_CH_NUM(h2c_pkt, rtw_pno_req->channel_cnt);
1948 	CH_SWITCH_SET_INFO_SIZE(h2c_pkt, rtw_pno_req->channel_cnt * 4);
1949 
1950 	loc_ch_info = rtw_get_rsvd_page_location(rtwdev, RSVD_CH_INFO);
1951 	CH_SWITCH_SET_INFO_LOC(h2c_pkt, loc_ch_info);
1952 
1953 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
1954 }
1955 
rtw_fw_adaptivity(struct rtw_dev * rtwdev)1956 void rtw_fw_adaptivity(struct rtw_dev *rtwdev)
1957 {
1958 	struct rtw_dm_info *dm_info = &rtwdev->dm_info;
1959 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1960 
1961 	if (!rtw_edcca_enabled) {
1962 		dm_info->edcca_mode = RTW_EDCCA_NORMAL;
1963 		rtw_dbg(rtwdev, RTW_DBG_ADAPTIVITY,
1964 			"EDCCA disabled by debugfs\n");
1965 	}
1966 
1967 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_ADAPTIVITY);
1968 	SET_ADAPTIVITY_MODE(h2c_pkt, dm_info->edcca_mode);
1969 	SET_ADAPTIVITY_OPTION(h2c_pkt, 1);
1970 	SET_ADAPTIVITY_IGI(h2c_pkt, dm_info->igi_history[0]);
1971 	SET_ADAPTIVITY_L2H(h2c_pkt, dm_info->l2h_th_ini);
1972 	SET_ADAPTIVITY_DENSITY(h2c_pkt, dm_info->scan_density);
1973 
1974 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
1975 }
1976 
rtw_fw_scan_notify(struct rtw_dev * rtwdev,bool start)1977 void rtw_fw_scan_notify(struct rtw_dev *rtwdev, bool start)
1978 {
1979 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
1980 
1981 	SET_H2C_CMD_ID_CLASS(h2c_pkt, H2C_CMD_SCAN);
1982 	SET_SCAN_START(h2c_pkt, start);
1983 
1984 	rtw_fw_send_h2c_command(rtwdev, h2c_pkt);
1985 }
1986 
rtw_append_probe_req_ie(struct rtw_dev * rtwdev,struct sk_buff * skb,struct sk_buff_head * list,u8 * bands,struct rtw_vif * rtwvif)1987 static int rtw_append_probe_req_ie(struct rtw_dev *rtwdev, struct sk_buff *skb,
1988 				   struct sk_buff_head *list, u8 *bands,
1989 				   struct rtw_vif *rtwvif)
1990 {
1991 	const struct rtw_chip_info *chip = rtwdev->chip;
1992 	struct ieee80211_scan_ies *ies = rtwvif->scan_ies;
1993 	struct sk_buff *new;
1994 	u8 idx;
1995 
1996 	for (idx = NL80211_BAND_2GHZ; idx < NUM_NL80211_BANDS; idx++) {
1997 		if (!(BIT(idx) & chip->band))
1998 			continue;
1999 		new = skb_copy(skb, GFP_KERNEL);
2000 		if (!new)
2001 			return -ENOMEM;
2002 		skb_put_data(new, ies->ies[idx], ies->len[idx]);
2003 		skb_put_data(new, ies->common_ies, ies->common_ie_len);
2004 		skb_queue_tail(list, new);
2005 		(*bands)++;
2006 	}
2007 
2008 	return 0;
2009 }
2010 
_rtw_hw_scan_update_probe_req(struct rtw_dev * rtwdev,u8 num_probes,struct sk_buff_head * probe_req_list)2011 static int _rtw_hw_scan_update_probe_req(struct rtw_dev *rtwdev, u8 num_probes,
2012 					 struct sk_buff_head *probe_req_list)
2013 {
2014 	const struct rtw_chip_info *chip = rtwdev->chip;
2015 	struct sk_buff *skb, *tmp;
2016 	u16 pg_addr = rtwdev->fifo.rsvd_h2c_info_addr, loc;
2017 	u8 tx_desc_sz = chip->tx_pkt_desc_sz;
2018 	u16 page_size = chip->page_size;
2019 	u8 page_offset = 1, *buf;
2020 	u16 buf_offset = page_size * page_offset;
2021 	unsigned int pkt_len;
2022 	u8 page_cnt, pages;
2023 	int ret;
2024 
2025 	if (rtw_fw_feature_ext_check(&rtwdev->fw, FW_FEATURE_EXT_OLD_PAGE_NUM))
2026 		page_cnt = RTW_OLD_PROBE_PG_CNT;
2027 	else
2028 		page_cnt = RTW_PROBE_PG_CNT;
2029 
2030 	pages = page_offset + num_probes * page_cnt;
2031 
2032 	buf = kzalloc(page_size * pages, GFP_KERNEL);
2033 	if (!buf)
2034 		return -ENOMEM;
2035 
2036 	buf_offset -= tx_desc_sz;
2037 	skb_queue_walk_safe(probe_req_list, skb, tmp) {
2038 		skb_unlink(skb, probe_req_list);
2039 		rtw_fill_rsvd_page_desc(rtwdev, skb, RSVD_PROBE_REQ);
2040 		if (skb->len > page_size * page_cnt) {
2041 			ret = -EINVAL;
2042 			goto out;
2043 		}
2044 
2045 		memcpy(buf + buf_offset, skb->data, skb->len);
2046 		pkt_len = skb->len - tx_desc_sz;
2047 		loc = pg_addr - rtwdev->fifo.rsvd_boundary + page_offset;
2048 		__rtw_fw_update_pkt(rtwdev, RTW_PACKET_PROBE_REQ, pkt_len, loc);
2049 
2050 		buf_offset += page_cnt * page_size;
2051 		page_offset += page_cnt;
2052 		kfree_skb(skb);
2053 	}
2054 
2055 	ret = rtw_fw_write_data_rsvd_page(rtwdev, pg_addr, buf, buf_offset);
2056 	if (ret) {
2057 		rtw_err(rtwdev, "Download probe request to firmware failed\n");
2058 		goto out;
2059 	}
2060 
2061 	rtwdev->scan_info.probe_pg_size = page_offset;
2062 out:
2063 	kfree(buf);
2064 	skb_queue_walk_safe(probe_req_list, skb, tmp)
2065 		kfree_skb(skb);
2066 
2067 	return ret;
2068 }
2069 
rtw_hw_scan_update_probe_req(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif)2070 static int rtw_hw_scan_update_probe_req(struct rtw_dev *rtwdev,
2071 					struct rtw_vif *rtwvif)
2072 {
2073 	struct cfg80211_scan_request *req = rtwvif->scan_req;
2074 	struct sk_buff_head list;
2075 	struct sk_buff *skb, *tmp;
2076 	u8 num = req->n_ssids, i, bands = 0;
2077 	int ret;
2078 
2079 	skb_queue_head_init(&list);
2080 	for (i = 0; i < num; i++) {
2081 		skb = ieee80211_probereq_get(rtwdev->hw, rtwvif->mac_addr,
2082 					     req->ssids[i].ssid,
2083 					     req->ssids[i].ssid_len,
2084 					     req->ie_len);
2085 		if (!skb) {
2086 			ret = -ENOMEM;
2087 			goto out;
2088 		}
2089 		ret = rtw_append_probe_req_ie(rtwdev, skb, &list, &bands,
2090 					      rtwvif);
2091 		if (ret)
2092 			goto out;
2093 
2094 		kfree_skb(skb);
2095 	}
2096 
2097 	return _rtw_hw_scan_update_probe_req(rtwdev, num * bands, &list);
2098 
2099 out:
2100 	skb_queue_walk_safe(&list, skb, tmp)
2101 		kfree_skb(skb);
2102 
2103 	return ret;
2104 }
2105 
rtw_add_chan_info(struct rtw_dev * rtwdev,struct rtw_chan_info * info,struct rtw_chan_list * list,u8 * buf)2106 static int rtw_add_chan_info(struct rtw_dev *rtwdev, struct rtw_chan_info *info,
2107 			     struct rtw_chan_list *list, u8 *buf)
2108 {
2109 	u8 *chan = &buf[list->size];
2110 	u8 info_size = RTW_CH_INFO_SIZE;
2111 
2112 	if (list->size > list->buf_size)
2113 		return -ENOMEM;
2114 
2115 	CH_INFO_SET_CH(chan, info->channel);
2116 	CH_INFO_SET_PRI_CH_IDX(chan, info->pri_ch_idx);
2117 	CH_INFO_SET_BW(chan, info->bw);
2118 	CH_INFO_SET_TIMEOUT(chan, info->timeout);
2119 	CH_INFO_SET_ACTION_ID(chan, info->action_id);
2120 	CH_INFO_SET_EXTRA_INFO(chan, info->extra_info);
2121 	if (info->extra_info) {
2122 		EXTRA_CH_INFO_SET_ID(chan, RTW_SCAN_EXTRA_ID_DFS);
2123 		EXTRA_CH_INFO_SET_INFO(chan, RTW_SCAN_EXTRA_ACTION_SCAN);
2124 		EXTRA_CH_INFO_SET_SIZE(chan, RTW_EX_CH_INFO_SIZE -
2125 				       RTW_EX_CH_INFO_HDR_SIZE);
2126 		EXTRA_CH_INFO_SET_DFS_EXT_TIME(chan, RTW_DFS_CHAN_TIME);
2127 		info_size += RTW_EX_CH_INFO_SIZE;
2128 	}
2129 	list->size += info_size;
2130 	list->ch_num++;
2131 
2132 	return 0;
2133 }
2134 
rtw_add_chan_list(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,struct rtw_chan_list * list,u8 * buf)2135 static int rtw_add_chan_list(struct rtw_dev *rtwdev, struct rtw_vif *rtwvif,
2136 			     struct rtw_chan_list *list, u8 *buf)
2137 {
2138 	struct cfg80211_scan_request *req = rtwvif->scan_req;
2139 	struct rtw_fifo_conf *fifo = &rtwdev->fifo;
2140 	struct ieee80211_channel *channel;
2141 	int i, ret = 0;
2142 
2143 	for (i = 0; i < req->n_channels; i++) {
2144 		struct rtw_chan_info ch_info = {0};
2145 
2146 		channel = req->channels[i];
2147 		ch_info.channel = channel->hw_value;
2148 		ch_info.bw = RTW_SCAN_WIDTH;
2149 		ch_info.pri_ch_idx = RTW_PRI_CH_IDX;
2150 		ch_info.timeout = req->duration_mandatory ?
2151 				  req->duration : RTW_CHANNEL_TIME;
2152 
2153 		if (channel->flags & (IEEE80211_CHAN_RADAR | IEEE80211_CHAN_NO_IR)) {
2154 			ch_info.action_id = RTW_CHANNEL_RADAR;
2155 			ch_info.extra_info = 1;
2156 			/* Overwrite duration for passive scans if necessary */
2157 			ch_info.timeout = ch_info.timeout > RTW_PASS_CHAN_TIME ?
2158 					  ch_info.timeout : RTW_PASS_CHAN_TIME;
2159 		} else {
2160 			ch_info.action_id = RTW_CHANNEL_ACTIVE;
2161 		}
2162 
2163 		ret = rtw_add_chan_info(rtwdev, &ch_info, list, buf);
2164 		if (ret)
2165 			return ret;
2166 	}
2167 
2168 	if (list->size > fifo->rsvd_pg_num << TX_PAGE_SIZE_SHIFT) {
2169 		rtw_err(rtwdev, "List exceeds rsvd page total size\n");
2170 		return -EINVAL;
2171 	}
2172 
2173 	list->addr = fifo->rsvd_h2c_info_addr + rtwdev->scan_info.probe_pg_size;
2174 	ret = rtw_fw_write_data_rsvd_page(rtwdev, list->addr, buf, list->size);
2175 	if (ret)
2176 		rtw_err(rtwdev, "Download channel list failed\n");
2177 
2178 	return ret;
2179 }
2180 
rtw_fw_set_scan_offload(struct rtw_dev * rtwdev,struct rtw_ch_switch_option * opt,struct rtw_vif * rtwvif,struct rtw_chan_list * list)2181 static void rtw_fw_set_scan_offload(struct rtw_dev *rtwdev,
2182 				    struct rtw_ch_switch_option *opt,
2183 				    struct rtw_vif *rtwvif,
2184 				    struct rtw_chan_list *list)
2185 {
2186 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2187 	struct cfg80211_scan_request *req = rtwvif->scan_req;
2188 	struct rtw_fifo_conf *fifo = &rtwdev->fifo;
2189 	/* reserve one dummy page at the beginning for tx descriptor */
2190 	u8 pkt_loc = fifo->rsvd_h2c_info_addr - fifo->rsvd_boundary + 1;
2191 	bool random_seq = req->flags & NL80211_SCAN_FLAG_RANDOM_SN;
2192 	u8 h2c_pkt[H2C_PKT_SIZE] = {0};
2193 
2194 	rtw_h2c_pkt_set_header(h2c_pkt, H2C_PKT_SCAN_OFFLOAD);
2195 	SET_PKT_H2C_TOTAL_LEN(h2c_pkt, H2C_PKT_CH_SWITCH_LEN);
2196 
2197 	SCAN_OFFLOAD_SET_START(h2c_pkt, opt->switch_en);
2198 	SCAN_OFFLOAD_SET_BACK_OP_EN(h2c_pkt, opt->back_op_en);
2199 	SCAN_OFFLOAD_SET_RANDOM_SEQ_EN(h2c_pkt, random_seq);
2200 	SCAN_OFFLOAD_SET_NO_CCK_EN(h2c_pkt, req->no_cck);
2201 	SCAN_OFFLOAD_SET_CH_NUM(h2c_pkt, list->ch_num);
2202 	SCAN_OFFLOAD_SET_CH_INFO_SIZE(h2c_pkt, list->size);
2203 	SCAN_OFFLOAD_SET_CH_INFO_LOC(h2c_pkt, list->addr - fifo->rsvd_boundary);
2204 	SCAN_OFFLOAD_SET_OP_CH(h2c_pkt, scan_info->op_chan);
2205 	SCAN_OFFLOAD_SET_OP_PRI_CH_IDX(h2c_pkt, scan_info->op_pri_ch_idx);
2206 	SCAN_OFFLOAD_SET_OP_BW(h2c_pkt, scan_info->op_bw);
2207 	SCAN_OFFLOAD_SET_OP_PORT_ID(h2c_pkt, rtwvif->port);
2208 	SCAN_OFFLOAD_SET_OP_DWELL_TIME(h2c_pkt, req->duration_mandatory ?
2209 				       req->duration : RTW_CHANNEL_TIME);
2210 	SCAN_OFFLOAD_SET_OP_GAP_TIME(h2c_pkt, RTW_OFF_CHAN_TIME);
2211 	SCAN_OFFLOAD_SET_SSID_NUM(h2c_pkt, req->n_ssids);
2212 	SCAN_OFFLOAD_SET_PKT_LOC(h2c_pkt, pkt_loc);
2213 
2214 	rtw_fw_send_h2c_packet(rtwdev, h2c_pkt);
2215 }
2216 
rtw_hw_scan_start(struct rtw_dev * rtwdev,struct ieee80211_vif * vif,struct ieee80211_scan_request * scan_req)2217 void rtw_hw_scan_start(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
2218 		       struct ieee80211_scan_request *scan_req)
2219 {
2220 	struct rtw_vif *rtwvif = (struct rtw_vif *)vif->drv_priv;
2221 	struct cfg80211_scan_request *req = &scan_req->req;
2222 	u8 mac_addr[ETH_ALEN];
2223 
2224 	rtwdev->scan_info.scanning_vif = vif;
2225 	rtwvif->scan_ies = &scan_req->ies;
2226 	rtwvif->scan_req = req;
2227 
2228 	ieee80211_stop_queues(rtwdev->hw);
2229 	rtw_leave_lps_deep(rtwdev);
2230 	rtw_hci_flush_all_queues(rtwdev, false);
2231 	rtw_mac_flush_all_queues(rtwdev, false);
2232 	if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
2233 		get_random_mask_addr(mac_addr, req->mac_addr,
2234 				     req->mac_addr_mask);
2235 	else
2236 		ether_addr_copy(mac_addr, vif->addr);
2237 
2238 	rtw_core_scan_start(rtwdev, rtwvif, mac_addr, true);
2239 
2240 	rtwdev->hal.rcr &= ~BIT_CBSSID_BCN;
2241 	rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr);
2242 }
2243 
rtw_hw_scan_complete(struct rtw_dev * rtwdev,struct ieee80211_vif * vif,bool aborted)2244 void rtw_hw_scan_complete(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
2245 			  bool aborted)
2246 {
2247 	struct cfg80211_scan_info info = {
2248 		.aborted = aborted,
2249 	};
2250 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2251 	struct rtw_hal *hal = &rtwdev->hal;
2252 	struct rtw_vif *rtwvif;
2253 	u8 chan = scan_info->op_chan;
2254 
2255 	if (!vif)
2256 		return;
2257 
2258 	rtwdev->hal.rcr |= BIT_CBSSID_BCN;
2259 	rtw_write32(rtwdev, REG_RCR, rtwdev->hal.rcr);
2260 
2261 	rtw_core_scan_complete(rtwdev, vif, true);
2262 
2263 	rtwvif = (struct rtw_vif *)vif->drv_priv;
2264 	if (chan)
2265 		rtw_store_op_chan(rtwdev, false);
2266 	rtw_phy_set_tx_power_level(rtwdev, hal->current_channel);
2267 	ieee80211_wake_queues(rtwdev->hw);
2268 	ieee80211_scan_completed(rtwdev->hw, &info);
2269 
2270 	rtwvif->scan_req = NULL;
2271 	rtwvif->scan_ies = NULL;
2272 	rtwdev->scan_info.scanning_vif = NULL;
2273 }
2274 
rtw_hw_scan_prehandle(struct rtw_dev * rtwdev,struct rtw_vif * rtwvif,struct rtw_chan_list * list)2275 static int rtw_hw_scan_prehandle(struct rtw_dev *rtwdev, struct rtw_vif *rtwvif,
2276 				 struct rtw_chan_list *list)
2277 {
2278 	struct cfg80211_scan_request *req = rtwvif->scan_req;
2279 	int size = req->n_channels * (RTW_CH_INFO_SIZE + RTW_EX_CH_INFO_SIZE);
2280 	u8 *buf;
2281 	int ret;
2282 
2283 	buf = kmalloc(size, GFP_KERNEL);
2284 	if (!buf)
2285 		return -ENOMEM;
2286 
2287 	ret = rtw_hw_scan_update_probe_req(rtwdev, rtwvif);
2288 	if (ret) {
2289 		rtw_err(rtwdev, "Update probe request failed\n");
2290 		goto out;
2291 	}
2292 
2293 	list->buf_size = size;
2294 	list->size = 0;
2295 	list->ch_num = 0;
2296 	ret = rtw_add_chan_list(rtwdev, rtwvif, list, buf);
2297 out:
2298 	kfree(buf);
2299 
2300 	return ret;
2301 }
2302 
rtw_hw_scan_offload(struct rtw_dev * rtwdev,struct ieee80211_vif * vif,bool enable)2303 int rtw_hw_scan_offload(struct rtw_dev *rtwdev, struct ieee80211_vif *vif,
2304 			bool enable)
2305 {
2306 	struct rtw_vif *rtwvif = vif ? (struct rtw_vif *)vif->drv_priv : NULL;
2307 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2308 	struct rtw_ch_switch_option cs_option = {0};
2309 	struct rtw_chan_list chan_list = {0};
2310 	int ret = 0;
2311 
2312 	if (!rtwvif)
2313 		return -EINVAL;
2314 
2315 	cs_option.switch_en = enable;
2316 	cs_option.back_op_en = scan_info->op_chan != 0;
2317 	if (enable) {
2318 		ret = rtw_hw_scan_prehandle(rtwdev, rtwvif, &chan_list);
2319 		if (ret)
2320 			goto out;
2321 	}
2322 	rtw_fw_set_scan_offload(rtwdev, &cs_option, rtwvif, &chan_list);
2323 out:
2324 	if (rtwdev->ap_active) {
2325 		ret = rtw_download_beacon(rtwdev);
2326 		if (ret)
2327 			rtw_err(rtwdev, "HW scan download beacon failed\n");
2328 	}
2329 
2330 	return ret;
2331 }
2332 
rtw_hw_scan_abort(struct rtw_dev * rtwdev)2333 void rtw_hw_scan_abort(struct rtw_dev *rtwdev)
2334 {
2335 	struct ieee80211_vif *vif = rtwdev->scan_info.scanning_vif;
2336 
2337 	if (!rtw_fw_feature_check(&rtwdev->fw, FW_FEATURE_SCAN_OFFLOAD))
2338 		return;
2339 
2340 	rtw_hw_scan_offload(rtwdev, vif, false);
2341 	rtw_hw_scan_complete(rtwdev, vif, true);
2342 }
2343 
rtw_hw_scan_status_report(struct rtw_dev * rtwdev,struct sk_buff * skb)2344 void rtw_hw_scan_status_report(struct rtw_dev *rtwdev, struct sk_buff *skb)
2345 {
2346 	struct ieee80211_vif *vif = rtwdev->scan_info.scanning_vif;
2347 	struct rtw_c2h_cmd *c2h;
2348 	bool aborted;
2349 	u8 rc;
2350 
2351 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
2352 		return;
2353 
2354 	c2h = get_c2h_from_skb(skb);
2355 	rc = GET_SCAN_REPORT_RETURN_CODE(c2h->payload);
2356 	aborted = rc != RTW_SCAN_REPORT_SUCCESS;
2357 	rtw_hw_scan_complete(rtwdev, vif, aborted);
2358 
2359 	if (aborted)
2360 		rtw_dbg(rtwdev, RTW_DBG_HW_SCAN, "HW scan aborted with code: %d\n", rc);
2361 }
2362 
rtw_store_op_chan(struct rtw_dev * rtwdev,bool backup)2363 void rtw_store_op_chan(struct rtw_dev *rtwdev, bool backup)
2364 {
2365 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2366 	struct rtw_hal *hal = &rtwdev->hal;
2367 	u8 band;
2368 
2369 	if (backup) {
2370 		scan_info->op_chan = hal->current_channel;
2371 		scan_info->op_bw = hal->current_band_width;
2372 		scan_info->op_pri_ch_idx = hal->current_primary_channel_index;
2373 		scan_info->op_pri_ch = hal->primary_channel;
2374 	} else {
2375 		band = scan_info->op_chan > 14 ? RTW_BAND_5G : RTW_BAND_2G;
2376 		rtw_update_channel(rtwdev, scan_info->op_chan,
2377 				   scan_info->op_pri_ch,
2378 				   band, scan_info->op_bw);
2379 	}
2380 }
2381 
rtw_clear_op_chan(struct rtw_dev * rtwdev)2382 void rtw_clear_op_chan(struct rtw_dev *rtwdev)
2383 {
2384 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2385 
2386 	scan_info->op_chan = 0;
2387 	scan_info->op_bw = 0;
2388 	scan_info->op_pri_ch_idx = 0;
2389 	scan_info->op_pri_ch = 0;
2390 }
2391 
rtw_is_op_chan(struct rtw_dev * rtwdev,u8 channel)2392 static bool rtw_is_op_chan(struct rtw_dev *rtwdev, u8 channel)
2393 {
2394 	struct rtw_hw_scan_info *scan_info = &rtwdev->scan_info;
2395 
2396 	return channel == scan_info->op_chan;
2397 }
2398 
rtw_hw_scan_chan_switch(struct rtw_dev * rtwdev,struct sk_buff * skb)2399 void rtw_hw_scan_chan_switch(struct rtw_dev *rtwdev, struct sk_buff *skb)
2400 {
2401 	struct rtw_hal *hal = &rtwdev->hal;
2402 	struct rtw_c2h_cmd *c2h;
2403 	enum rtw_scan_notify_id id;
2404 	u8 chan, band, status;
2405 
2406 	if (!test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
2407 		return;
2408 
2409 	c2h = get_c2h_from_skb(skb);
2410 	chan = GET_CHAN_SWITCH_CENTRAL_CH(c2h->payload);
2411 	id = GET_CHAN_SWITCH_ID(c2h->payload);
2412 	status = GET_CHAN_SWITCH_STATUS(c2h->payload);
2413 
2414 	if (id == RTW_SCAN_NOTIFY_ID_POSTSWITCH) {
2415 		band = chan > 14 ? RTW_BAND_5G : RTW_BAND_2G;
2416 		rtw_update_channel(rtwdev, chan, chan, band,
2417 				   RTW_CHANNEL_WIDTH_20);
2418 		if (rtw_is_op_chan(rtwdev, chan)) {
2419 			rtw_store_op_chan(rtwdev, false);
2420 			ieee80211_wake_queues(rtwdev->hw);
2421 			rtw_core_enable_beacon(rtwdev, true);
2422 		}
2423 	} else if (id == RTW_SCAN_NOTIFY_ID_PRESWITCH) {
2424 		if (IS_CH_5G_BAND(chan)) {
2425 			rtw_coex_switchband_notify(rtwdev, COEX_SWITCH_TO_5G);
2426 		} else if (IS_CH_2G_BAND(chan)) {
2427 			u8 chan_type;
2428 
2429 			if (test_bit(RTW_FLAG_SCANNING, rtwdev->flags))
2430 				chan_type = COEX_SWITCH_TO_24G;
2431 			else
2432 				chan_type = COEX_SWITCH_TO_24G_NOFORSCAN;
2433 			rtw_coex_switchband_notify(rtwdev, chan_type);
2434 		}
2435 		/* The channel of C2H RTW_SCAN_NOTIFY_ID_PRESWITCH is next
2436 		 * channel that hardware will switch. We need to stop queue
2437 		 * if next channel is non-op channel.
2438 		 */
2439 		if (!rtw_is_op_chan(rtwdev, chan) &&
2440 		    rtw_is_op_chan(rtwdev, hal->current_channel)) {
2441 			rtw_core_enable_beacon(rtwdev, false);
2442 			ieee80211_stop_queues(rtwdev->hw);
2443 		}
2444 	}
2445 
2446 	rtw_dbg(rtwdev, RTW_DBG_HW_SCAN,
2447 		"Chan switch: %x, id: %x, status: %x\n", chan, id, status);
2448 }
2449