rtl8821ce_recv.c 14 KB

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