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