hc
2024-05-08 f309769f8af08599af39b6de4f675784ce76530d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/******************************************************************************
 *
 * Copyright(c) 2015 - 2017 Realtek Corporation.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 *
 *****************************************************************************/
#define _RTL8822CE_RECV_C_
 
#include <drv_types.h>        /* PADAPTER and etc. */
#include <hal_data.h>        /* HAL_DATA_TYPE */
#include "../rtl8822c.h"
#include "rtl8822ce.h"
 
/* Debug Buffer Descriptor Ring */
 
/*#define BUF_DESC_DEBUG*/
#ifdef BUF_DESC_DEBUG
#define buf_desc_debug(...) do {\
       RTW_INFO("BUF_DESC:" __VA_ARGS__);\
   } while (0)
#else
#define buf_desc_debug(...)  do {} while (0)
#endif
 
/*
 * Wait until rx data is ready
 *    return value: _SUCCESS if Rx packet is ready, _FAIL if not ready
 */
 
static u32 rtl8822ce_wait_rxrdy(_adapter *padapter,
               u8 *rx_bd, u16 rx_q_idx)
{
   struct recv_priv *r_priv = &padapter->recvpriv;
   u8 first_seg = 0, last_seg = 0;
   u16 total_len = 0, read_cnt = 0;
 
   static BOOLEAN start_rx = _FALSE;
   u16 status = _SUCCESS;
   HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter);
 
   if (rx_bd == NULL)
       return _FAIL;
 
   total_len = (u16)GET_RX_BD_TOTALRXPKTSIZE(rx_bd);
   first_seg = (u8)GET_RX_BD_FS(rx_bd);
   last_seg = (u8)GET_RX_BD_LS(rx_bd);
 
   buf_desc_debug("RX:%s enter: rx_bd addr = %p, total_len=%d, first_seg=%d, last_seg=%d, read_cnt %d, index %d, address %p\n",
              __func__,
       (u8 *)&r_priv->rx_ring[rx_q_idx].desc[r_priv->rx_ring[rx_q_idx].idx],
              total_len, first_seg, last_seg, read_cnt,
              r_priv->rx_ring[rx_q_idx].idx, rx_bd);
 
#if defined(USING_RX_TAG)
   /* Rx Tag not ported */
   if (!start_rx) {
       start_rx = _TRUE;
       pHalData->RxTag = 1;
   } else {
       while (total_len != (pHalData->RxTag + 1)) {
 
           read_cnt++;
 
           total_len = (u16)GET_RX_BD_TOTALRXPKTSIZE(rx_bd);
           first_seg = (u8)GET_RX_BD_FS(rx_bd);
           last_seg = (u8)GET_RX_BD_LS(rx_bd);
 
           if (read_cnt > 10000) {
               pHalData->RxTag = total_len;
               break;
           }
 
           if (total_len == 0 && pHalData->RxTag == 0x1fff)
               break;
       }
       pHalData->RxTag = total_len;
   }
#else
   while (total_len == 0) {
       read_cnt++;
 
       total_len = (u16) GET_RX_BD_TOTALRXPKTSIZE(rx_bd);
       first_seg = (u8) GET_RX_BD_FS(rx_bd);
       last_seg = (u8) GET_RX_BD_LS(rx_bd);
 
       if (read_cnt > 20) {
           status = _FAIL;
           break;
       }
   }
#endif
 
   buf_desc_debug("RX:%s exit total_len=%d, rx_tag = %d, first_seg=%d, last_seg=%d, read_cnt %d\n",
              __func__, total_len, pHalData->RxTag,
              first_seg, last_seg, read_cnt);
 
   return status;
}
 
/*
 * Check the number of rxdec to be handled between
 *   "index of RX queue descriptor maintained by host (write pointer)" and
 *   "index of RX queue descriptor maintained by hardware (read pointer)"
 */
static u16 rtl8822ce_check_rxdesc_remain(_adapter *padapter, int rx_queue_idx)
{
   struct recv_priv *r_priv = &padapter->recvpriv;
   u16 desc_idx_hw = 0, desc_idx_host = 0, num_rxdesc_to_handle = 0;
   u32 tmp_4bytes = 0;
   static BOOLEAN    start_rx = FALSE;
 
 
   tmp_4bytes = rtw_read32(padapter, REG_RXQ_RXBD_IDX_8822C);
   desc_idx_hw = (u16)((tmp_4bytes >> 16) & 0x7ff);
   desc_idx_host = (u16)(tmp_4bytes & 0x7ff);
 
   /*
    * make sure driver does not handle packet if hardware pointer
    * keeps in zero in initial state
    */
   buf_desc_debug("RX:%s(%d) reg_value %x\n", __func__, __LINE__,
              tmp_4bytes);
 
   if (desc_idx_hw > 0)
       start_rx = TRUE;
 
   if (!start_rx)
       return 0;
 
   if (desc_idx_hw < desc_idx_host)
       /* hw idx is turn around */
       num_rxdesc_to_handle = RX_BD_NUM_8822CE - desc_idx_host + desc_idx_hw;
   else
       num_rxdesc_to_handle = desc_idx_hw - desc_idx_host;
 
   if (num_rxdesc_to_handle == 0)
       return 0;
 
   r_priv->rx_ring[rx_queue_idx].idx = desc_idx_host;
 
   buf_desc_debug("RX:%s reg_val %x, hw_idx %x, host_idx %x, desc to handle = %d\n",
       __func__, tmp_4bytes, desc_idx_hw, desc_idx_host, num_rxdesc_to_handle);
 
   return num_rxdesc_to_handle;
}
 
static void rtl8822ce_rx_mpdu(_adapter *padapter)
{
   struct recv_priv *r_priv = &padapter->recvpriv;
   struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
   _queue *pfree_recv_queue = &r_priv->free_recv_queue;
   HAL_DATA_TYPE *pHalData = GET_HAL_DATA(padapter);
   union recv_frame *precvframe = NULL;
   struct rx_pkt_attrib *pattrib = NULL;
   int rx_q_idx = RX_MPDU_QUEUE;
   u32 count = r_priv->rxringcount;
   u16 remaing_rxdesc = 0;
   u8 *rx_bd;
   struct sk_buff *skb;
   u32 desc_size;
 
 
   desc_size = rtl8822c_get_rx_desc_size(padapter);
 
   /* RX NORMAL PKT */
 
   remaing_rxdesc = rtl8822ce_check_rxdesc_remain(padapter, rx_q_idx);
   while (remaing_rxdesc) {
 
       /* rx descriptor */
       rx_bd = (u8 *)&r_priv->rx_ring[rx_q_idx].buf_desc[r_priv->rx_ring[rx_q_idx].idx];
 
       /* rx packet */
       skb = r_priv->rx_ring[rx_q_idx].rx_buf[r_priv->rx_ring[rx_q_idx].idx];
 
       buf_desc_debug("RX:%s(%d), rx_bd addr = %x, total_len = %d, ring idx = %d\n",
                  __func__, __LINE__, (u32)rx_bd,
                  GET_RX_BD_TOTALRXPKTSIZE(rx_bd),
                  r_priv->rx_ring[rx_q_idx].idx);
 
       buf_desc_debug("RX:%s(%d), skb(rx_buf)=%x, buf addr(virtual = %x, phisycal = %x)\n",
                  __func__, __LINE__, (u32)skb,
                  (u32)(skb_tail_pointer(skb)),
                  GET_RX_BD_PHYSICAL_ADDR_LOW(rx_bd));
 
       /* wait until packet is ready. this operation is similar to
        * check own bit and should be called before dma_unmap_single
        * which release memory mapping
        */
 
       if (rtl8822ce_wait_rxrdy(padapter, rx_bd, rx_q_idx) !=
           _SUCCESS)
           buf_desc_debug("RX:%s(%d) packet not ready\n",
                      __func__, __LINE__);
 
       {
           precvframe = rtw_alloc_recvframe(pfree_recv_queue);
 
           if (precvframe == NULL) {
               goto done;
           }
 
           _rtw_init_listhead(&precvframe->u.hdr.list);
           precvframe->u.hdr.len = 0;
 
#ifndef CONFIG_PCIE_DMA_COHERENT
           dma_unmap_single(&pdvobjpriv->ppcidev->dev,
                    *((dma_addr_t *)skb->cb),
                    r_priv->rxbuffersize,
                    DMA_FROM_DEVICE);
#endif
 
           rtl8822c_query_rx_desc(precvframe, skb->data);
           pattrib = &precvframe->u.hdr.attrib;
 
#ifdef CONFIG_RX_PACKET_APPEND_FCS
           {
               struct mlme_priv *mlmepriv =
                       &padapter->mlmepriv;
 
               if (check_fwstate(mlmepriv,
                         WIFI_MONITOR_STATE) == _FALSE)
                   if (pattrib->pkt_rpt_type == NORMAL_RX)
                       pattrib->pkt_len -=
                           IEEE80211_FCS_LEN;
           }
#endif
 
           buf_desc_debug("RX:%s(%d), pkt_len = %d, pattrib->drvinfo_sz = %d, pattrib->qos = %d, pattrib->shift_sz = %d\n",
                      __func__, __LINE__, pattrib->pkt_len,
                      pattrib->drvinfo_sz, pattrib->qos,
                      pattrib->shift_sz);
 
           if (rtw_os_alloc_recvframe(padapter, precvframe,
                  (skb->data + desc_size +
                   pattrib->drvinfo_sz + pattrib->shift_sz),
                          skb) == _FAIL) {
 
               rtw_free_recvframe(precvframe,
                          &r_priv->free_recv_queue);
 
               RTW_INFO("rtl8822ce_rx_mpdu:can't allocate memory for skb copy\n");
#ifndef CONFIG_PCIE_DMA_COHERENT
               *((dma_addr_t *) skb->cb) =
                   dma_map_single(&pdvobjpriv->ppcidev->dev,
                              skb_tail_pointer(skb),
                              r_priv->rxbuffersize,
                              DMA_FROM_DEVICE);
#endif
               goto done;
           }
 
           recvframe_put(precvframe, pattrib->pkt_len);
 
           if (pattrib->pkt_rpt_type == NORMAL_RX) {
               /* Normal rx packet */
               pre_recv_entry(precvframe, pattrib->physt ? ((u8 *)(skb->data) + desc_size) : NULL);
           } else {
               if (pattrib->pkt_rpt_type == C2H_PACKET)
                   rtl8822c_c2h_handler_no_io(padapter,
                                skb->data,
                                desc_size +
                                pattrib->pkt_len);
 
               rtw_free_recvframe(precvframe,
                          pfree_recv_queue);
           }
#ifndef CONFIG_PCIE_DMA_COHERENT
           *((dma_addr_t *) skb->cb) =
               dma_map_single(&pdvobjpriv->ppcidev->dev,
                          skb_tail_pointer(skb),
                          r_priv->rxbuffersize,
                          DMA_FROM_DEVICE);
#endif
       }
done:
 
 
       SET_RX_BD_PHYSICAL_ADDR_LOW(rx_bd, *((dma_addr_t *)skb->cb));
   #ifdef CONFIG_64BIT_DMA
       SET_RX_BD_PHYSICAL_ADDR_HIGH(rx_bd, (*((dma_addr_t *)skb->cb)>>32));
   #endif
       /*Max. MPDU size is 11454 bytes, fix Rx buffer size to 12K is safe.*/
       /*Even most Rx frame size is smaller than 4K, 12K Rx buffer size will not affect Rx efficiency.*/
       SET_RX_BD_RXBUFFSIZE(rx_bd, 12*1024);
 
       r_priv->rx_ring[rx_q_idx].idx =
           (r_priv->rx_ring[rx_q_idx].idx + 1) %
           r_priv->rxringcount;
 
       rtw_write16(padapter, REG_RXQ_RXBD_IDX,
               r_priv->rx_ring[rx_q_idx].idx);
 
       buf_desc_debug("RX:%s(%d) reg_value %x\n", __func__, __LINE__,
                  rtw_read32(padapter, REG_RXQ_RXBD_IDX));
 
       remaing_rxdesc--;
   }
}
 
static void rtl8822ce_recv_tasklet(unsigned long priv)
{
   _irqL    irqL;
   _adapter    *padapter = (_adapter *)priv;
   HAL_DATA_TYPE    *pHalData = GET_HAL_DATA(padapter);
   struct dvobj_priv    *pdvobjpriv = adapter_to_dvobj(padapter);
 
   rtl8822ce_rx_mpdu(padapter);
   _enter_critical(&pdvobjpriv->irq_th_lock, &irqL);
   pHalData->IntrMask[0] |= (BIT_RXOK_MSK_8822C | BIT_RDU_MSK_8822C);
   pHalData->IntrMask[1] |= BIT_FOVW_MSK_8822C;
   rtw_write32(padapter, REG_HIMR0_8822C, pHalData->IntrMask[0]);
   rtw_write32(padapter, REG_HIMR1_8822C, pHalData->IntrMask[1]);
   _exit_critical(&pdvobjpriv->irq_th_lock, &irqL);
}
 
static void rtl8822ce_xmit_beacon(PADAPTER Adapter)
{
#if defined(CONFIG_AP_MODE) && defined(CONFIG_NATIVEAP_MLME)
   struct mlme_priv *pmlmepriv = &Adapter->mlmepriv;
 
   if (MLME_IS_AP(Adapter) || MLME_IS_MESH(Adapter)) {
       /* send_beacon(Adapter); */
       if (pmlmepriv->update_bcn == _TRUE)
           tx_beacon_hdl(Adapter, NULL);
   }
#endif
}
 
static void rtl8822ce_prepare_bcn_tasklet(unsigned long priv)
{
   _adapter *padapter = (_adapter *)priv;
 
   rtl8822ce_xmit_beacon(padapter);
}
 
s32 rtl8822ce_init_recv_priv(_adapter *padapter)
{
   struct recv_priv    *precvpriv = &padapter->recvpriv;
   s32    ret = _SUCCESS;
 
 
#ifdef PLATFORM_LINUX
   tasklet_init(&precvpriv->recv_tasklet,
            rtl8822ce_recv_tasklet,
            (unsigned long)padapter);
 
   tasklet_init(&precvpriv->irq_prepare_beacon_tasklet,
            rtl8822ce_prepare_bcn_tasklet,
            (unsigned long)padapter);
#endif
 
 
   return ret;
}
 
void rtl8822ce_free_recv_priv(_adapter *padapter)
{
 
}
 
int rtl8822ce_init_rxbd_ring(_adapter *padapter)
{
   struct recv_priv    *r_priv = &padapter->recvpriv;
   struct dvobj_priv    *pdvobjpriv = adapter_to_dvobj(padapter);
   struct pci_dev    *pdev = pdvobjpriv->ppcidev;
   struct net_device    *dev = padapter->pnetdev;
   dma_addr_t *mapping = NULL;
   struct sk_buff *skb = NULL;
   u8    *rx_desc = NULL;
   int    i, rx_queue_idx;
 
 
   /* rx_queue_idx 0:RX_MPDU_QUEUE */
   /* rx_queue_idx 1:RX_CMD_QUEUE */
   for (rx_queue_idx = 0; rx_queue_idx < 1; rx_queue_idx++) {
       r_priv->rx_ring[rx_queue_idx].buf_desc =
           dma_alloc_coherent(&pdev->dev,
                      sizeof(*r_priv->rx_ring[rx_queue_idx].buf_desc) *
                      r_priv->rxringcount,
                      &r_priv->rx_ring[rx_queue_idx].dma, GFP_ATOMIC);
 
       if (!r_priv->rx_ring[rx_queue_idx].buf_desc ||
           (unsigned long)r_priv->rx_ring[rx_queue_idx].buf_desc &
           0xFF) {
           RTW_INFO("Cannot allocate RX ring\n");
           return _FAIL;
       }
 
       _rtw_memset(r_priv->rx_ring[rx_queue_idx].buf_desc, 0,
               sizeof(*r_priv->rx_ring[rx_queue_idx].buf_desc) *
               r_priv->rxringcount);
       r_priv->rx_ring[rx_queue_idx].idx = 0;
 
       for (i = 0; i < r_priv->rxringcount; i++) {
#ifdef CONFIG_PCIE_DMA_COHERENT
           skb = dev_alloc_skb_coherent(pdev, r_priv->rxbuffersize);
#else
           skb = dev_alloc_skb(r_priv->rxbuffersize);
#endif
           if (!skb) {
               RTW_INFO("Cannot allocate skb for RX ring\n");
               return _FAIL;
           }
 
           rx_desc = (u8 *)(&r_priv->rx_ring[rx_queue_idx].buf_desc[i]);
           r_priv->rx_ring[rx_queue_idx].rx_buf[i] = skb;
           mapping = (dma_addr_t *)skb->cb;
 
           /* just set skb->cb to mapping addr
            * for dma_unmap_single use */
#ifndef CONFIG_PCIE_DMA_COHERENT
           *mapping = dma_map_single(&pdev->dev, skb_tail_pointer(skb),
                         r_priv->rxbuffersize,
                         DMA_FROM_DEVICE);
#endif
           /* Reset FS, LS, Total len */
           SET_RX_BD_LS(rx_desc, 0);
           SET_RX_BD_FS(rx_desc, 0);
           SET_RX_BD_TOTALRXPKTSIZE(rx_desc, 0);
           /*Max. MPDU size is 11454 bytes, fix Rx buffer size to 12K is safe.*/
           /*Even most Rx frame size is smaller than 4K, 12K Rx buffer size will not affect Rx efficiency.*/
           SET_RX_BD_RXBUFFSIZE(rx_desc, 12*1024);
           SET_RX_BD_PHYSICAL_ADDR_LOW(rx_desc, *mapping);
       #ifdef CONFIG_64BIT_DMA
           SET_RX_BD_PHYSICAL_ADDR_HIGH(rx_desc, (u32)(*mapping >> 32));
       #endif
           buf_desc_debug("RX:rx buffer desc addr[%d] = %x, skb(rx_buf) = %x, buffer addr (virtual = %x, physical = %x)\n",
               i, (u32)&r_priv->rx_ring[rx_queue_idx].buf_desc[i],
               (u32)r_priv->rx_ring[rx_queue_idx].rx_buf[i],
               (u32)(skb_tail_pointer(skb)), (u32)(*mapping));
       }
   }
 
 
   return _SUCCESS;
}
 
#ifdef CONFIG_PCIE_DMA_COHERENT
void rtl8822ce_free_rxbd_ring(_adapter *padapter)
{
   struct recv_priv *r_priv = &padapter->recvpriv;
   struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
   struct pci_dev *pdev = pdvobjpriv->ppcidev;
   int i, rx_queue_idx;
 
 
   /* rx_queue_idx 0:RX_MPDU_QUEUE */
   /* rx_queue_idx 1:RX_CMD_QUEUE */
   for (rx_queue_idx = 0; rx_queue_idx < 1; rx_queue_idx++) {
       for (i = 0; i < r_priv->rxringcount; i++) {
           struct sk_buff *skb;
 
           skb = r_priv->rx_ring[rx_queue_idx].rx_buf[i];
 
           if (!skb)
               continue;
 
           /* skb buf */
           dma_free_coherent(&pdev->dev, r_priv->rxringcount,
               (dma_addr_t *)skb->data, *(dma_addr_t *)skb->cb);
 
           /* skb */
           _rtw_mfree(skb, sizeof(struct sk_buff));
       }
       dma_free_coherent(&pdev->dev,
                 sizeof(*r_priv->rx_ring[rx_queue_idx].buf_desc) *
                 r_priv->rxringcount,
                 r_priv->rx_ring[rx_queue_idx].buf_desc,
                 r_priv->rx_ring[rx_queue_idx].dma);
       r_priv->rx_ring[rx_queue_idx].buf_desc = NULL;
   } /* for */
}
#else
void rtl8822ce_free_rxbd_ring(_adapter *padapter)
{
   struct recv_priv *r_priv = &padapter->recvpriv;
   struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
   struct pci_dev *pdev = pdvobjpriv->ppcidev;
   int i, rx_queue_idx;
 
 
   /* rx_queue_idx 0:RX_MPDU_QUEUE */
   /* rx_queue_idx 1:RX_CMD_QUEUE */
   for (rx_queue_idx = 0; rx_queue_idx < 1; rx_queue_idx++) {
       for (i = 0; i < r_priv->rxringcount; i++) {
           struct sk_buff *skb;
 
           skb = r_priv->rx_ring[rx_queue_idx].rx_buf[i];
 
           if (!skb)
               continue;
 
           dma_unmap_single(&pdev->dev,
                    *((dma_addr_t *) skb->cb),
                    r_priv->rxbuffersize,
                    DMA_FROM_DEVICE);
           kfree_skb(skb);
       }
 
       dma_free_coherent(&pdev->dev,
               sizeof(*r_priv->rx_ring[rx_queue_idx].buf_desc) *
                   r_priv->rxringcount,
                   r_priv->rx_ring[rx_queue_idx].buf_desc,
                   r_priv->rx_ring[rx_queue_idx].dma);
 
       r_priv->rx_ring[rx_queue_idx].buf_desc = NULL;
   } /* for */
}
#endif /* CONFIG_PCIE_DMA_COHERENT */