1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3 *
4 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5 *
6 ******************************************************************************/
7 #define _RTL8723BS_RECV_C_
8
9 #include <drv_types.h>
10 #include <rtw_debug.h>
11 #include <rtl8723b_hal.h>
12
initrecvbuf(struct recv_buf * precvbuf,struct adapter * padapter)13 static void initrecvbuf(struct recv_buf *precvbuf, struct adapter *padapter)
14 {
15 INIT_LIST_HEAD(&precvbuf->list);
16 spin_lock_init(&precvbuf->recvbuf_lock);
17
18 precvbuf->adapter = padapter;
19 }
20
update_recvframe_attrib(struct adapter * padapter,union recv_frame * precvframe,struct recv_stat * prxstat)21 static void update_recvframe_attrib(struct adapter *padapter,
22 union recv_frame *precvframe,
23 struct recv_stat *prxstat)
24 {
25 struct rx_pkt_attrib *pattrib;
26 struct recv_stat report;
27 PRXREPORT prxreport = (PRXREPORT)&report;
28
29 report.rxdw0 = prxstat->rxdw0;
30 report.rxdw1 = prxstat->rxdw1;
31 report.rxdw2 = prxstat->rxdw2;
32 report.rxdw3 = prxstat->rxdw3;
33 report.rxdw4 = prxstat->rxdw4;
34 report.rxdw5 = prxstat->rxdw5;
35
36 pattrib = &precvframe->u.hdr.attrib;
37 memset(pattrib, 0, sizeof(struct rx_pkt_attrib));
38
39 /* update rx report to recv_frame attribute */
40 pattrib->pkt_rpt_type = prxreport->c2h_ind ? C2H_PACKET : NORMAL_RX;
41 /* DBG_871X("%s: pkt_rpt_type =%d\n", __func__, pattrib->pkt_rpt_type); */
42
43 if (pattrib->pkt_rpt_type == NORMAL_RX) {
44 /* Normal rx packet */
45 /* update rx report to recv_frame attribute */
46 pattrib->pkt_len = (u16)prxreport->pktlen;
47 pattrib->drvinfo_sz = (u8)(prxreport->drvinfosize << 3);
48 pattrib->physt = (u8)prxreport->physt;
49
50 pattrib->crc_err = (u8)prxreport->crc32;
51 pattrib->icv_err = (u8)prxreport->icverr;
52
53 pattrib->bdecrypted = (u8)(prxreport->swdec ? 0 : 1);
54 pattrib->encrypt = (u8)prxreport->security;
55
56 pattrib->qos = (u8)prxreport->qos;
57 pattrib->priority = (u8)prxreport->tid;
58
59 pattrib->amsdu = (u8)prxreport->amsdu;
60
61 pattrib->seq_num = (u16)prxreport->seq;
62 pattrib->frag_num = (u8)prxreport->frag;
63 pattrib->mfrag = (u8)prxreport->mf;
64 pattrib->mdata = (u8)prxreport->md;
65
66 pattrib->data_rate = (u8)prxreport->rx_rate;
67 } else {
68 pattrib->pkt_len = (u16)prxreport->pktlen;
69 }
70 }
71
72 /*
73 * Notice:
74 *Before calling this function,
75 *precvframe->u.hdr.rx_data should be ready!
76 */
update_recvframe_phyinfo(union recv_frame * precvframe,struct phy_stat * pphy_status)77 static void update_recvframe_phyinfo(union recv_frame *precvframe,
78 struct phy_stat *pphy_status)
79 {
80 struct adapter *padapter = precvframe->u.hdr.adapter;
81 struct rx_pkt_attrib *pattrib = &precvframe->u.hdr.attrib;
82 struct hal_com_data *p_hal_data = GET_HAL_DATA(padapter);
83 struct odm_phy_info *p_phy_info =
84 (struct odm_phy_info *)(&pattrib->phy_info);
85
86 u8 *wlanhdr;
87 u8 *my_bssid;
88 u8 *rx_bssid;
89 u8 *rx_ra;
90 u8 *my_hwaddr;
91 u8 *sa = NULL;
92
93 struct odm_packet_info pkt_info = {
94 .data_rate = 0x00,
95 .station_id = 0x00,
96 .bssid_match = false,
97 .to_self = false,
98 .is_beacon = false,
99 };
100
101 /* _irqL irqL; */
102 struct sta_priv *pstapriv;
103 struct sta_info *psta;
104
105 wlanhdr = get_recvframe_data(precvframe);
106 my_bssid = get_bssid(&padapter->mlmepriv);
107 rx_bssid = get_hdr_bssid(wlanhdr);
108 pkt_info.bssid_match = ((!IsFrameTypeCtrl(wlanhdr)) &&
109 !pattrib->icv_err && !pattrib->crc_err &&
110 ether_addr_equal(rx_bssid, my_bssid));
111
112 rx_ra = get_ra(wlanhdr);
113 my_hwaddr = myid(&padapter->eeprompriv);
114 pkt_info.to_self = pkt_info.bssid_match &&
115 ether_addr_equal(rx_ra, my_hwaddr);
116
117
118 pkt_info.is_beacon = pkt_info.bssid_match &&
119 (GetFrameSubType(wlanhdr) == WIFI_BEACON);
120
121 sa = get_ta(wlanhdr);
122
123 pkt_info.station_id = 0xFF;
124
125 pstapriv = &padapter->stapriv;
126 psta = rtw_get_stainfo(pstapriv, sa);
127 if (psta) {
128 pkt_info.station_id = psta->mac_id;
129 /* DBG_8192C("%s ==> StationID(%d)\n",
130 * __func__, pkt_info.station_id); */
131 }
132 pkt_info.data_rate = pattrib->data_rate;
133
134 /* rtl8723b_query_rx_phy_status(precvframe, pphy_status); */
135 /* spin_lock_bh(&p_hal_data->odm_stainfo_lock); */
136 ODM_PhyStatusQuery(&p_hal_data->odmpriv, p_phy_info,
137 (u8 *)pphy_status, &(pkt_info));
138 if (psta)
139 psta->rssi = pattrib->phy_info.RecvSignalPower;
140 /* spin_unlock_bh(&p_hal_data->odm_stainfo_lock); */
141 precvframe->u.hdr.psta = NULL;
142 if (
143 pkt_info.bssid_match &&
144 (check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE) == true)
145 ) {
146 if (psta) {
147 precvframe->u.hdr.psta = psta;
148 rtl8723b_process_phy_info(padapter, precvframe);
149 }
150 } else if (pkt_info.to_self || pkt_info.is_beacon) {
151 u32 adhoc_state = WIFI_ADHOC_STATE | WIFI_ADHOC_MASTER_STATE;
152 if (check_fwstate(&padapter->mlmepriv, adhoc_state))
153 if (psta)
154 precvframe->u.hdr.psta = psta;
155 rtl8723b_process_phy_info(padapter, precvframe);
156 }
157 }
158
rtl8723bs_c2h_packet_handler(struct adapter * padapter,u8 * pbuf,u16 length)159 static void rtl8723bs_c2h_packet_handler(struct adapter *padapter,
160 u8 *pbuf, u16 length)
161 {
162 u8 *tmp = NULL;
163 u8 res = false;
164
165 if (length == 0)
166 return;
167
168 /* DBG_871X("+%s() length =%d\n", __func__, length); */
169
170 tmp = rtw_zmalloc(length);
171 if (!tmp)
172 return;
173
174 memcpy(tmp, pbuf, length);
175
176 res = rtw_c2h_packet_wk_cmd(padapter, tmp, length);
177
178 if (!res)
179 kfree(tmp);
180
181 /* DBG_871X("-%s res(%d)\n", __func__, res); */
182 }
183
try_alloc_recvframe(struct recv_priv * precvpriv,struct recv_buf * precvbuf)184 static inline union recv_frame *try_alloc_recvframe(struct recv_priv *precvpriv,
185 struct recv_buf *precvbuf)
186 {
187 union recv_frame *precvframe;
188
189 precvframe = rtw_alloc_recvframe(&precvpriv->free_recv_queue);
190 if (!precvframe) {
191 DBG_8192C("%s: no enough recv frame!\n", __func__);
192 rtw_enqueue_recvbuf_to_head(precvbuf,
193 &precvpriv->recv_buf_pending_queue);
194
195 /* The case of can't allocate recvframe should be temporary, */
196 /* schedule again and hope recvframe is available next time. */
197 tasklet_schedule(&precvpriv->recv_tasklet);
198 }
199
200 return precvframe;
201 }
202
rx_crc_err(struct recv_priv * precvpriv,struct hal_com_data * p_hal_data,struct rx_pkt_attrib * pattrib,union recv_frame * precvframe)203 static inline bool rx_crc_err(struct recv_priv *precvpriv,
204 struct hal_com_data *p_hal_data,
205 struct rx_pkt_attrib *pattrib,
206 union recv_frame *precvframe)
207 {
208 /* fix Hardware RX data error, drop whole recv_buffer */
209 if ((!(p_hal_data->ReceiveConfig & RCR_ACRC32)) && pattrib->crc_err) {
210 DBG_8192C("%s()-%d: RX Warning! rx CRC ERROR !!\n",
211 __func__, __LINE__);
212 rtw_free_recvframe(precvframe, &precvpriv->free_recv_queue);
213 return true;
214 }
215
216 return false;
217 }
218
pkt_exceeds_tail(struct recv_priv * precvpriv,u8 * end,u8 * tail,union recv_frame * precvframe)219 static inline bool pkt_exceeds_tail(struct recv_priv *precvpriv,
220 u8 *end, u8 *tail,
221 union recv_frame *precvframe)
222 {
223 if (end > tail) {
224 DBG_8192C("%s()-%d: : next pkt len(%p,%d) exceed ptail(%p)!\n",
225 __func__, __LINE__, ptr, pkt_offset, precvbuf->ptail);
226 rtw_free_recvframe(precvframe, &precvpriv->free_recv_queue);
227 return true;
228 }
229
230 return false;
231 }
232
rtl8723bs_recv_tasklet(struct tasklet_struct * t)233 static void rtl8723bs_recv_tasklet(struct tasklet_struct *t)
234 {
235 struct adapter *padapter = from_tasklet(padapter, t,
236 recvpriv.recv_tasklet);
237 struct hal_com_data *p_hal_data;
238 struct recv_priv *precvpriv;
239 struct recv_buf *precvbuf;
240 union recv_frame *precvframe;
241 struct rx_pkt_attrib *pattrib;
242 struct __queue *recv_buf_queue;
243 u8 *ptr;
244 u32 pkt_offset, skb_len, alloc_sz;
245 _pkt *pkt_copy = NULL;
246 u8 shift_sz = 0, rx_report_sz = 0;
247
248 p_hal_data = GET_HAL_DATA(padapter);
249 precvpriv = &padapter->recvpriv;
250 recv_buf_queue = &precvpriv->recv_buf_pending_queue;
251
252 do {
253 precvbuf = rtw_dequeue_recvbuf(recv_buf_queue);
254 if (!precvbuf)
255 break;
256
257 ptr = precvbuf->pdata;
258
259 while (ptr < precvbuf->ptail) {
260 precvframe = try_alloc_recvframe(precvpriv, precvbuf);
261 if (!precvframe)
262 return;
263
264 /* rx desc parsing */
265 update_recvframe_attrib(padapter, precvframe,
266 (struct recv_stat *)ptr);
267
268 pattrib = &precvframe->u.hdr.attrib;
269
270 if (rx_crc_err(precvpriv, p_hal_data,
271 pattrib, precvframe))
272 break;
273
274 rx_report_sz = RXDESC_SIZE + pattrib->drvinfo_sz;
275 pkt_offset = rx_report_sz +
276 pattrib->shift_sz +
277 pattrib->pkt_len;
278
279 if (pkt_exceeds_tail(precvpriv, ptr + pkt_offset,
280 precvbuf->ptail, precvframe))
281 break;
282
283 if ((pattrib->crc_err) || (pattrib->icv_err)) {
284 DBG_8192C("%s: crc_err =%d icv_err =%d, skip!\n",
285 __func__, pattrib->crc_err,
286 pattrib->icv_err);
287 rtw_free_recvframe(precvframe,
288 &precvpriv->free_recv_queue);
289 } else {
290 /* Modified by Albert 20101213 */
291 /* For 8 bytes IP header alignment. */
292 if (pattrib->qos) /* Qos data, wireless lan header length is 26 */
293 shift_sz = 6;
294 else
295 shift_sz = 0;
296
297 skb_len = pattrib->pkt_len;
298
299 /* for first fragment packet, driver need allocate 1536+drvinfo_sz+RXDESC_SIZE to defrag packet. */
300 /* modify alloc_sz for recvive crc error packet by thomas 2011-06-02 */
301 if ((pattrib->mfrag == 1) && (pattrib->frag_num == 0)) {
302 if (skb_len <= 1650)
303 alloc_sz = 1664;
304 else
305 alloc_sz = skb_len + 14;
306 } else {
307 alloc_sz = skb_len;
308 /* 6 is for IP header 8 bytes alignment in QoS packet case. */
309 /* 8 is for skb->data 4 bytes alignment. */
310 alloc_sz += 14;
311 }
312
313 pkt_copy = rtw_skb_alloc(alloc_sz);
314 if (!pkt_copy) {
315 DBG_8192C("%s: alloc_skb fail, drop frame\n", __func__);
316 rtw_free_recvframe(precvframe, &precvpriv->free_recv_queue);
317 break;
318 }
319
320 pkt_copy->dev = padapter->pnetdev;
321 precvframe->u.hdr.pkt = pkt_copy;
322 skb_reserve(pkt_copy, 8 - ((SIZE_PTR)(pkt_copy->data) & 7));/* force pkt_copy->data at 8-byte alignment address */
323 skb_reserve(pkt_copy, shift_sz);/* force ip_hdr at 8-byte alignment address according to shift_sz. */
324 memcpy(pkt_copy->data, (ptr + rx_report_sz + pattrib->shift_sz), skb_len);
325 precvframe->u.hdr.rx_head = pkt_copy->head;
326 precvframe->u.hdr.rx_data = precvframe->u.hdr.rx_tail = pkt_copy->data;
327 precvframe->u.hdr.rx_end = skb_end_pointer(pkt_copy);
328
329 recvframe_put(precvframe, skb_len);
330 /* recvframe_pull(precvframe, drvinfo_sz + RXDESC_SIZE); */
331
332 if (p_hal_data->ReceiveConfig & RCR_APPFCS)
333 recvframe_pull_tail(precvframe, IEEE80211_FCS_LEN);
334
335 /* move to drv info position */
336 ptr += RXDESC_SIZE;
337
338 /* update drv info */
339 if (p_hal_data->ReceiveConfig & RCR_APP_BA_SSN) {
340 /* rtl8723s_update_bassn(padapter, pdrvinfo); */
341 ptr += 4;
342 }
343
344 if (pattrib->pkt_rpt_type == NORMAL_RX) { /* Normal rx packet */
345 if (pattrib->physt)
346 update_recvframe_phyinfo(precvframe, (struct phy_stat *)ptr);
347
348 if (rtw_recv_entry(precvframe) != _SUCCESS) {
349 RT_TRACE(_module_rtl871x_recv_c_, _drv_dump_, ("%s: rtw_recv_entry(precvframe) != _SUCCESS\n", __func__));
350 }
351 } else if (pattrib->pkt_rpt_type == C2H_PACKET) {
352 C2H_EVT_HDR C2hEvent;
353
354 u16 len_c2h = pattrib->pkt_len;
355 u8 *pbuf_c2h = precvframe->u.hdr.rx_data;
356 u8 *pdata_c2h;
357
358 C2hEvent.CmdID = pbuf_c2h[0];
359 C2hEvent.CmdSeq = pbuf_c2h[1];
360 C2hEvent.CmdLen = (len_c2h-2);
361 pdata_c2h = pbuf_c2h+2;
362
363 if (C2hEvent.CmdID == C2H_CCX_TX_RPT)
364 CCX_FwC2HTxRpt_8723b(padapter, pdata_c2h, C2hEvent.CmdLen);
365 else
366 rtl8723bs_c2h_packet_handler(padapter, precvframe->u.hdr.rx_data, pattrib->pkt_len);
367
368 rtw_free_recvframe(precvframe, &precvpriv->free_recv_queue);
369 }
370 }
371
372 pkt_offset = round_up(pkt_offset, 8);
373 precvbuf->pdata += pkt_offset;
374 ptr = precvbuf->pdata;
375 precvframe = NULL;
376 pkt_copy = NULL;
377 }
378
379 rtw_enqueue_recvbuf(precvbuf, &precvpriv->free_recv_buf_queue);
380 } while (1);
381 }
382
383 /*
384 * Initialize recv private variable for hardware dependent
385 * 1. recv buf
386 * 2. recv tasklet
387 *
388 */
rtl8723bs_init_recv_priv(struct adapter * padapter)389 s32 rtl8723bs_init_recv_priv(struct adapter *padapter)
390 {
391 s32 res;
392 u32 i, n;
393 struct recv_priv *precvpriv;
394 struct recv_buf *precvbuf;
395
396 res = _SUCCESS;
397 precvpriv = &padapter->recvpriv;
398
399 /* 3 1. init recv buffer */
400 _rtw_init_queue(&precvpriv->free_recv_buf_queue);
401 _rtw_init_queue(&precvpriv->recv_buf_pending_queue);
402
403 n = NR_RECVBUFF * sizeof(struct recv_buf) + 4;
404 precvpriv->pallocated_recv_buf = rtw_zmalloc(n);
405 if (!precvpriv->pallocated_recv_buf) {
406 res = _FAIL;
407 RT_TRACE(_module_rtl871x_recv_c_, _drv_err_, ("alloc recv_buf fail!\n"));
408 goto exit;
409 }
410
411 precvpriv->precv_buf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(precvpriv->pallocated_recv_buf), 4);
412
413 /* init each recv buffer */
414 precvbuf = (struct recv_buf *)precvpriv->precv_buf;
415 for (i = 0; i < NR_RECVBUFF; i++) {
416 initrecvbuf(precvbuf, padapter);
417
418 if (!precvbuf->pskb) {
419 SIZE_PTR tmpaddr = 0;
420 SIZE_PTR alignment = 0;
421
422 precvbuf->pskb = rtw_skb_alloc(MAX_RECVBUF_SZ + RECVBUFF_ALIGN_SZ);
423
424 if (precvbuf->pskb) {
425 precvbuf->pskb->dev = padapter->pnetdev;
426
427 tmpaddr = (SIZE_PTR)precvbuf->pskb->data;
428 alignment = tmpaddr & (RECVBUFF_ALIGN_SZ-1);
429 skb_reserve(precvbuf->pskb, (RECVBUFF_ALIGN_SZ - alignment));
430 }
431
432 if (!precvbuf->pskb) {
433 DBG_871X("%s: alloc_skb fail!\n", __func__);
434 }
435 }
436
437 list_add_tail(&precvbuf->list, &precvpriv->free_recv_buf_queue.queue);
438
439 precvbuf++;
440 }
441 precvpriv->free_recv_buf_queue_cnt = i;
442
443 if (res == _FAIL)
444 goto initbuferror;
445
446 /* 3 2. init tasklet */
447 tasklet_setup(&precvpriv->recv_tasklet, rtl8723bs_recv_tasklet);
448
449 goto exit;
450
451 initbuferror:
452 precvbuf = (struct recv_buf *)precvpriv->precv_buf;
453 if (precvbuf) {
454 n = precvpriv->free_recv_buf_queue_cnt;
455 precvpriv->free_recv_buf_queue_cnt = 0;
456 for (i = 0; i < n ; i++) {
457 list_del_init(&precvbuf->list);
458 rtw_os_recvbuf_resource_free(padapter, precvbuf);
459 precvbuf++;
460 }
461 precvpriv->precv_buf = NULL;
462 }
463
464 kfree(precvpriv->pallocated_recv_buf);
465 precvpriv->pallocated_recv_buf = NULL;
466
467 exit:
468 return res;
469 }
470
471 /*
472 * Free recv private variable of hardware dependent
473 * 1. recv buf
474 * 2. recv tasklet
475 *
476 */
rtl8723bs_free_recv_priv(struct adapter * padapter)477 void rtl8723bs_free_recv_priv(struct adapter *padapter)
478 {
479 u32 i;
480 struct recv_priv *precvpriv;
481 struct recv_buf *precvbuf;
482
483 precvpriv = &padapter->recvpriv;
484
485 /* 3 1. kill tasklet */
486 tasklet_kill(&precvpriv->recv_tasklet);
487
488 /* 3 2. free all recv buffers */
489 precvbuf = (struct recv_buf *)precvpriv->precv_buf;
490 if (precvbuf) {
491 precvpriv->free_recv_buf_queue_cnt = 0;
492 for (i = 0; i < NR_RECVBUFF; i++) {
493 list_del_init(&precvbuf->list);
494 rtw_os_recvbuf_resource_free(padapter, precvbuf);
495 precvbuf++;
496 }
497 precvpriv->precv_buf = NULL;
498 }
499
500 kfree(precvpriv->pallocated_recv_buf);
501 precvpriv->pallocated_recv_buf = NULL;
502 }
503