1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
4 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
6 ******************************************************************************/
7 #define _RTL8723BS_XMIT_C_
10 #include <rtw_debug.h>
11 #include <rtl8723b_hal.h>
13 static u8 rtw_sdio_wait_enough_TxOQT_space(struct adapter *padapter, u8 agg_num)
16 struct hal_com_data *pHalData = GET_HAL_DATA(padapter);
18 while (pHalData->SdioTxOQTFreeSpace < agg_num) {
20 (padapter->bSurpriseRemoved) ||
21 (padapter->bDriverStopped)
23 DBG_871X("%s: bSurpriseRemoved or bDriverStopped (wait TxOQT)\n", __func__);
27 HalQueryTxOQTBufferStatus8723BSdio(padapter);
29 if ((++n % 60) == 0) {
31 DBG_871X("%s(%d): QOT free space(%d), agg_num: %d\n",
32 __func__, n, pHalData->SdioTxOQTFreeSpace, agg_num);
39 pHalData->SdioTxOQTFreeSpace -= agg_num;
42 /* ++priv->pshare->nr_out_of_txoqt_space; */
47 static s32 rtl8723_dequeue_writeport(struct adapter *padapter)
49 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
50 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
51 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
52 struct xmit_buf *pxmitbuf;
53 struct adapter *pri_padapter = padapter;
57 u8 bUpdatePageNum = false;
59 ret = ret || check_fwstate(pmlmepriv, _FW_UNDER_SURVEY);
62 pxmitbuf = dequeue_pending_xmitbuf_under_survey(pxmitpriv);
64 pxmitbuf = dequeue_pending_xmitbuf(pxmitpriv);
69 deviceId = ffaddr2deviceId(pdvobjpriv, pxmitbuf->ff_hwaddr);
71 /* translate fifo addr to queue index */
73 case WLAN_TX_HIQ_DEVICE_ID:
74 PageIdx = HI_QUEUE_IDX;
77 case WLAN_TX_MIQ_DEVICE_ID:
78 PageIdx = MID_QUEUE_IDX;
81 case WLAN_TX_LOQ_DEVICE_ID:
82 PageIdx = LOW_QUEUE_IDX;
87 /* check if hardware tx fifo page is enough */
88 if (!rtw_hal_sdio_query_tx_freepage(pri_padapter, PageIdx, pxmitbuf->pg_num)) {
89 if (!bUpdatePageNum) {
90 /* Total number of page is NOT available, so update current FIFO status */
91 HalQueryTxBufferStatus8723BSdio(padapter);
92 bUpdatePageNum = true;
95 bUpdatePageNum = false;
96 enqueue_pending_xmitbuf_to_head(pxmitpriv, pxmitbuf);
102 (padapter->bSurpriseRemoved) ||
103 (padapter->bDriverStopped)
108 ("%s: bSurpriseRemoved(write port)\n", __func__)
113 if (rtw_sdio_wait_enough_TxOQT_space(padapter, pxmitbuf->agg_num) == false)
116 traffic_check_for_leave_lps(padapter, true, pxmitbuf->agg_num);
118 rtw_write_port(padapter, deviceId, pxmitbuf->len, (u8 *)pxmitbuf);
120 rtw_hal_sdio_update_tx_freepage(pri_padapter, PageIdx, pxmitbuf->pg_num);
123 /* rtw_free_xmitframe(pxmitpriv, pframe); */
124 /* pxmitbuf->priv_data = NULL; */
125 rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
127 #ifdef CONFIG_SDIO_TX_TASKLET
128 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
136 *Transmit xmitbuf to hardware tx fifo
140 *_FAIL something error
142 s32 rtl8723bs_xmit_buf_handler(struct adapter *padapter)
144 struct xmit_priv *pxmitpriv;
145 u8 queue_empty, queue_pending;
149 pxmitpriv = &padapter->xmitpriv;
151 if (wait_for_completion_interruptible(&pxmitpriv->xmit_comp)) {
152 DBG_871X_LEVEL(_drv_emerg_, "%s: down SdioXmitBufSema fail!\n", __func__);
156 ret = (padapter->bDriverStopped) || (padapter->bSurpriseRemoved);
162 "%s: bDriverStopped(%d) bSurpriseRemoved(%d)!\n",
164 padapter->bDriverStopped,
165 padapter->bSurpriseRemoved
171 queue_pending = check_pending_xmitbuf(pxmitpriv);
176 ret = rtw_register_tx_alive(padapter);
177 if (ret != _SUCCESS) {
182 queue_empty = rtl8723_dequeue_writeport(padapter);
183 /* dump secondary adapter xmitbuf */
184 } while (!queue_empty);
186 rtw_unregister_tx_alive(padapter);
193 *Aggregation packets and send to hardware
197 *-1 Hardware resource(TX FIFO) not ready
198 *-2 Software resource(xmitbuf) not ready
200 static s32 xmit_xmitframes(struct adapter *padapter, struct xmit_priv *pxmitpriv)
204 struct hw_xmit *hwxmits, *phwxmit;
206 struct tx_servq *ptxservq;
207 struct list_head *sta_plist, *sta_phead, *frame_plist, *frame_phead;
208 struct xmit_frame *pxmitframe;
209 struct __queue *pframe_queue;
210 struct xmit_buf *pxmitbuf;
211 u32 txlen, max_xmit_len;
212 u8 txdesc_size = TXDESC_SIZE;
216 hwxmits = pxmitpriv->hwxmits;
217 hwentry = pxmitpriv->hwxmit_entry;
223 if (padapter->registrypriv.wifi_spec == 1) {
224 for (idx = 0; idx < 4; idx++)
225 inx[idx] = pxmitpriv->wmm_para_seq[idx];
233 /* 0(VO), 1(VI), 2(BE), 3(BK) */
234 for (idx = 0; idx < hwentry; idx++) {
235 phwxmit = hwxmits + inx[idx];
238 (check_pending_xmitbuf(pxmitpriv)) &&
239 (padapter->mlmepriv.LinkDetectInfo.bHigherBusyTxTraffic)
241 if ((phwxmit->accnt > 0) && (phwxmit->accnt < 5)) {
247 max_xmit_len = rtw_hal_get_sdio_tx_max_length(padapter, inx[idx]);
249 spin_lock_bh(&pxmitpriv->lock);
251 sta_phead = get_list_head(phwxmit->sta_queue);
252 sta_plist = get_next(sta_phead);
253 /* because stop_sta_xmit may delete sta_plist at any time */
254 /* so we should add lock here, or while loop can not exit */
255 while (sta_phead != sta_plist) {
256 ptxservq = LIST_CONTAINOR(sta_plist, struct tx_servq, tx_pending);
257 sta_plist = get_next(sta_plist);
261 "%s idx:%d hwxmit_pkt_num:%d ptxservq_pkt_num:%d\n",
268 "%s free_xmit_extbuf_cnt =%d free_xmitbuf_cnt =%d free_xmitframe_cnt =%d\n",
270 pxmitpriv->free_xmit_extbuf_cnt,
271 pxmitpriv->free_xmitbuf_cnt,
272 pxmitpriv->free_xmitframe_cnt
275 pframe_queue = &ptxservq->sta_pending;
277 frame_phead = get_list_head(pframe_queue);
279 while (list_empty(frame_phead) == false) {
280 frame_plist = get_next(frame_phead);
281 pxmitframe = LIST_CONTAINOR(frame_plist, struct xmit_frame, list);
283 /* check xmit_buf size enough or not */
284 txlen = txdesc_size + rtw_wlan_pkt_size(pxmitframe);
286 ((_RND(pxmitbuf->len, 8) + txlen) > max_xmit_len) ||
287 (k >= (rtw_hal_sdio_max_txoqt_free_space(padapter) - 1))
290 /* pxmitbuf->priv_data will be NULL, and will crash here */
291 if (pxmitbuf->len > 0 &&
292 pxmitbuf->priv_data) {
293 struct xmit_frame *pframe;
294 pframe = (struct xmit_frame *)pxmitbuf->priv_data;
296 pxmitbuf->agg_num = k;
297 rtl8723b_update_txdesc(pframe, pframe->buf_addr);
298 rtw_free_xmitframe(pxmitpriv, pframe);
299 pxmitbuf->priv_data = NULL;
300 enqueue_pending_xmitbuf(pxmitpriv, pxmitbuf);
301 /* can not yield under lock */
304 rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
307 pxmitbuf = rtw_alloc_xmitbuf(pxmitpriv);
310 DBG_871X_LEVEL(_drv_err_, "%s: xmit_buf is not enough!\n", __func__);
313 complete(&(pxmitpriv->xmit_comp));
319 /* ok to send, remove frame from queue */
320 if (check_fwstate(&padapter->mlmepriv, WIFI_AP_STATE) == true) {
322 (pxmitframe->attrib.psta->state & WIFI_SLEEP_STATE) &&
323 (pxmitframe->attrib.triggered == 0)
326 "%s: one not triggered pkt in queue when this STA sleep,"
327 " break and goto next sta\n",
334 list_del_init(&pxmitframe->list);
339 pxmitbuf->ff_hwaddr = rtw_get_ff_hwaddr(pxmitframe);
340 pxmitbuf->priv_data = (u8 *)pxmitframe;
343 /* coalesce the xmitframe to xmitbuf */
344 pxmitframe->pxmitbuf = pxmitbuf;
345 pxmitframe->buf_addr = pxmitbuf->ptail;
347 ret = rtw_xmitframe_coalesce(padapter, pxmitframe->pkt, pxmitframe);
349 DBG_871X_LEVEL(_drv_err_, "%s: coalesce FAIL!", __func__);
350 /* Todo: error handler */
354 rtl8723b_update_txdesc(pxmitframe, pxmitframe->buf_addr);
355 rtw_count_tx_stats(padapter, pxmitframe, pxmitframe->attrib.last_txcmdsz);
357 txlen = txdesc_size + pxmitframe->attrib.last_txcmdsz;
358 pxmitframe->pg_num = (txlen + 127) / 128;
359 pxmitbuf->pg_num += (txlen + 127) / 128;
361 /* ((struct xmit_frame*)pxmitbuf->priv_data)->pg_num += pxmitframe->pg_num; */
362 pxmitbuf->ptail += _RND(txlen, 8); /* round to 8 bytes alignment */
363 pxmitbuf->len = _RND(pxmitbuf->len, 8) + txlen;
367 rtw_free_xmitframe(pxmitpriv, pxmitframe);
371 if (list_empty(&pframe_queue->queue))
372 list_del_init(&ptxservq->tx_pending);
377 spin_unlock_bh(&pxmitpriv->lock);
379 /* dump xmit_buf to hw tx fifo */
381 RT_TRACE(_module_hal_xmit_c_, _drv_info_, ("pxmitbuf->len =%d enqueue\n", pxmitbuf->len));
383 if (pxmitbuf->len > 0) {
384 struct xmit_frame *pframe;
385 pframe = (struct xmit_frame *)pxmitbuf->priv_data;
387 pxmitbuf->agg_num = k;
388 rtl8723b_update_txdesc(pframe, pframe->buf_addr);
389 rtw_free_xmitframe(pxmitpriv, pframe);
390 pxmitbuf->priv_data = NULL;
391 enqueue_pending_xmitbuf(pxmitpriv, pxmitbuf);
394 rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
407 *Transmit xmitframe from queue
411 *_FAIL something error
413 static s32 rtl8723bs_xmit_handler(struct adapter *padapter)
415 struct xmit_priv *pxmitpriv;
419 pxmitpriv = &padapter->xmitpriv;
421 if (wait_for_completion_interruptible(&pxmitpriv->SdioXmitStart)) {
422 DBG_871X_LEVEL(_drv_emerg_, "%s: SdioXmitStart fail!\n", __func__);
428 (padapter->bDriverStopped) ||
429 (padapter->bSurpriseRemoved)
435 "%s: bDriverStopped(%d) bSurpriseRemoved(%d)\n",
437 padapter->bDriverStopped,
438 padapter->bSurpriseRemoved
444 spin_lock_bh(&pxmitpriv->lock);
445 ret = rtw_txframes_pending(padapter);
446 spin_unlock_bh(&pxmitpriv->lock);
451 /* dequeue frame and write to hardware */
453 ret = xmit_xmitframes(padapter, pxmitpriv);
455 /* here sleep 1ms will cause big TP loss of TX */
456 /* from 50+ to 40+ */
457 if (padapter->registrypriv.wifi_spec)
464 spin_lock_bh(&pxmitpriv->lock);
465 ret = rtw_txframes_pending(padapter);
466 spin_unlock_bh(&pxmitpriv->lock);
474 int rtl8723bs_xmit_thread(void *context)
477 struct adapter *padapter;
478 struct xmit_priv *pxmitpriv;
483 pxmitpriv = &padapter->xmitpriv;
485 rtw_sprintf(thread_name, 20, "RTWHALXT-" ADPT_FMT, ADPT_ARG(padapter));
486 thread_enter(thread_name);
488 DBG_871X("start "FUNC_ADPT_FMT"\n", FUNC_ADPT_ARG(padapter));
491 ret = rtl8723bs_xmit_handler(padapter);
492 if (signal_pending(current)) {
493 flush_signals(current);
495 } while (_SUCCESS == ret);
497 complete(&pxmitpriv->SdioXmitTerminate);
499 RT_TRACE(_module_hal_xmit_c_, _drv_notice_, ("-%s\n", __func__));
504 s32 rtl8723bs_mgnt_xmit(
505 struct adapter *padapter, struct xmit_frame *pmgntframe
509 struct pkt_attrib *pattrib;
510 struct xmit_buf *pxmitbuf;
511 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
512 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
513 u8 *pframe = (u8 *)(pmgntframe->buf_addr) + TXDESC_OFFSET;
514 u8 txdesc_size = TXDESC_SIZE;
516 RT_TRACE(_module_hal_xmit_c_, _drv_info_, ("+%s\n", __func__));
518 pattrib = &pmgntframe->attrib;
519 pxmitbuf = pmgntframe->pxmitbuf;
521 rtl8723b_update_txdesc(pmgntframe, pmgntframe->buf_addr);
523 pxmitbuf->len = txdesc_size + pattrib->last_txcmdsz;
524 pxmitbuf->pg_num = (pxmitbuf->len + 127) / 128; /* 128 is tx page size */
525 pxmitbuf->ptail = pmgntframe->buf_addr + pxmitbuf->len;
526 pxmitbuf->ff_hwaddr = rtw_get_ff_hwaddr(pmgntframe);
528 rtw_count_tx_stats(padapter, pmgntframe, pattrib->last_txcmdsz);
530 rtw_free_xmitframe(pxmitpriv, pmgntframe);
532 pxmitbuf->priv_data = NULL;
534 if (GetFrameSubType(pframe) == WIFI_BEACON) { /* dump beacon directly */
535 ret = rtw_write_port(padapter, pdvobjpriv->Queue2Pipe[pxmitbuf->ff_hwaddr], pxmitbuf->len, (u8 *)pxmitbuf);
537 rtw_sctx_done_err(&pxmitbuf->sctx, RTW_SCTX_DONE_WRITE_PORT_ERR);
539 rtw_free_xmitbuf(pxmitpriv, pxmitbuf);
541 enqueue_pending_xmitbuf(pxmitpriv, pxmitbuf);
548 *Handle xmitframe(packet) come from rtw_xmit()
551 *true dump packet directly ok
552 *false enqueue, temporary can't transmit packets to hardware
554 s32 rtl8723bs_hal_xmit(
555 struct adapter *padapter, struct xmit_frame *pxmitframe
558 struct xmit_priv *pxmitpriv;
562 pxmitframe->attrib.qsel = pxmitframe->attrib.priority;
563 pxmitpriv = &padapter->xmitpriv;
566 (pxmitframe->frame_tag == DATA_FRAMETAG) &&
567 (pxmitframe->attrib.ether_type != 0x0806) &&
568 (pxmitframe->attrib.ether_type != 0x888e) &&
569 (pxmitframe->attrib.dhcp_pkt != 1)
571 if (padapter->mlmepriv.LinkDetectInfo.bBusyTraffic)
572 rtw_issue_addbareq_cmd(padapter, pxmitframe);
575 spin_lock_bh(&pxmitpriv->lock);
576 err = rtw_xmitframe_enqueue(padapter, pxmitframe);
577 spin_unlock_bh(&pxmitpriv->lock);
578 if (err != _SUCCESS) {
579 RT_TRACE(_module_hal_xmit_c_, _drv_err_, ("rtl8723bs_hal_xmit: enqueue xmitframe fail\n"));
580 rtw_free_xmitframe(pxmitpriv, pxmitframe);
582 pxmitpriv->tx_drop++;
586 complete(&pxmitpriv->SdioXmitStart);
591 s32 rtl8723bs_hal_xmitframe_enqueue(
592 struct adapter *padapter, struct xmit_frame *pxmitframe
595 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
598 err = rtw_xmitframe_enqueue(padapter, pxmitframe);
599 if (err != _SUCCESS) {
600 rtw_free_xmitframe(pxmitpriv, pxmitframe);
602 pxmitpriv->tx_drop++;
604 #ifdef CONFIG_SDIO_TX_TASKLET
605 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
607 complete(&pxmitpriv->SdioXmitStart);
617 *_SUCCESS start thread ok
618 *_FAIL start thread fail
621 s32 rtl8723bs_init_xmit_priv(struct adapter *padapter)
623 struct xmit_priv *xmitpriv = &padapter->xmitpriv;
624 struct hal_com_data *phal;
627 phal = GET_HAL_DATA(padapter);
629 spin_lock_init(&phal->SdioTxFIFOFreePageLock);
630 init_completion(&xmitpriv->SdioXmitStart);
631 init_completion(&xmitpriv->SdioXmitTerminate);
636 void rtl8723bs_free_xmit_priv(struct adapter *padapter)
638 struct xmit_priv *pxmitpriv;
639 struct xmit_buf *pxmitbuf;
640 struct __queue *pqueue;
641 struct list_head *plist, *phead;
642 struct list_head tmplist;
645 pxmitpriv = &padapter->xmitpriv;
646 pqueue = &pxmitpriv->pending_xmitbuf_queue;
647 phead = get_list_head(pqueue);
648 INIT_LIST_HEAD(&tmplist);
650 spin_lock_bh(&pqueue->lock);
651 if (!list_empty(&pqueue->queue)) {
652 /* Insert tmplist to end of queue, and delete phead */
653 /* then tmplist become head of queue. */
654 list_add_tail(&tmplist, phead);
655 list_del_init(phead);
657 spin_unlock_bh(&pqueue->lock);
660 while (list_empty(phead) == false) {
661 plist = get_next(phead);
662 list_del_init(plist);
664 pxmitbuf = LIST_CONTAINOR(plist, struct xmit_buf, list);
665 rtw_free_xmitframe(pxmitpriv, (struct xmit_frame *)pxmitbuf->priv_data);
666 pxmitbuf->priv_data = NULL;
667 rtw_free_xmitbuf(pxmitpriv, pxmitbuf);