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