GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / staging / rtl8188eu / os_dep / xmit_linux.c
1 // SPDX-License-Identifier: GPL-2.0
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
5  *
6  ******************************************************************************/
7 #define _XMIT_OSDEP_C_
8
9 #include <osdep_service.h>
10 #include <drv_types.h>
11
12 #include <wifi.h>
13 #include <mlme_osdep.h>
14 #include <xmit_osdep.h>
15 #include <osdep_intf.h>
16
17 int rtw_os_xmit_resource_alloc(struct adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz)
18 {
19         int i;
20
21         pxmitbuf->pallocated_buf = kzalloc(alloc_sz, GFP_KERNEL);
22         if (!pxmitbuf->pallocated_buf)
23                 return _FAIL;
24
25         pxmitbuf->pbuf = PTR_ALIGN(pxmitbuf->pallocated_buf, XMITBUF_ALIGN_SZ);
26         pxmitbuf->dma_transfer_addr = 0;
27
28         for (i = 0; i < 8; i++) {
29                 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
30                 if (!pxmitbuf->pxmit_urb[i]) {
31                         DBG_88E("pxmitbuf->pxmit_urb[i]==NULL");
32                         return _FAIL;
33                 }
34         }
35         return _SUCCESS;
36 }
37
38 void rtw_os_xmit_resource_free(struct xmit_buf *pxmitbuf)
39 {
40         int i;
41
42         for (i = 0; i < 8; i++)
43                 usb_free_urb(pxmitbuf->pxmit_urb[i]);
44
45         kfree(pxmitbuf->pallocated_buf);
46 }
47
48 #define WMM_XMIT_THRESHOLD      (NR_XMITFRAME*2/5)
49
50 void rtw_os_pkt_complete(struct adapter *padapter, struct sk_buff *pkt)
51 {
52         u16     queue;
53         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
54
55         queue = skb_get_queue_mapping(pkt);
56         if (padapter->registrypriv.wifi_spec) {
57                 if (__netif_subqueue_stopped(padapter->pnetdev, queue) &&
58                     (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD))
59                         netif_wake_subqueue(padapter->pnetdev, queue);
60         } else {
61                 if (__netif_subqueue_stopped(padapter->pnetdev, queue))
62                         netif_wake_subqueue(padapter->pnetdev, queue);
63         }
64
65         dev_kfree_skb_any(pkt);
66 }
67
68 void rtw_os_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe)
69 {
70         if (pxframe->pkt)
71                 rtw_os_pkt_complete(padapter, pxframe->pkt);
72         pxframe->pkt = NULL;
73 }
74
75 void rtw_os_xmit_schedule(struct adapter *padapter)
76 {
77         struct xmit_priv *pxmitpriv;
78
79         if (!padapter)
80                 return;
81
82         pxmitpriv = &padapter->xmitpriv;
83
84         spin_lock_bh(&pxmitpriv->lock);
85
86         if (rtw_txframes_pending(padapter))
87                 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
88
89         spin_unlock_bh(&pxmitpriv->lock);
90 }
91
92 static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt)
93 {
94         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
95         u16     queue;
96
97         queue = skb_get_queue_mapping(pkt);
98         if (padapter->registrypriv.wifi_spec) {
99                 /* No free space for Tx, tx_worker is too slow */
100                 if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD)
101                         netif_stop_subqueue(padapter->pnetdev, queue);
102         } else {
103                 if (pxmitpriv->free_xmitframe_cnt <= 4) {
104                         if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue)))
105                                 netif_stop_subqueue(padapter->pnetdev, queue);
106                 }
107         }
108 }
109
110 static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb)
111 {
112         struct  sta_priv *pstapriv = &padapter->stapriv;
113         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
114         struct list_head *phead, *plist;
115         struct sk_buff *newskb;
116         struct sta_info *psta = NULL;
117         s32     res;
118
119         spin_lock_bh(&pstapriv->asoc_list_lock);
120         phead = &pstapriv->asoc_list;
121         plist = phead->next;
122
123         /* free sta asoc_queue */
124         while (phead != plist) {
125                 psta = container_of(plist, struct sta_info, asoc_list);
126
127                 plist = plist->next;
128
129                 /* avoid   come from STA1 and send back STA1 */
130                 if (!memcmp(psta->hwaddr, &skb->data[6], 6))
131                         continue;
132
133                 newskb = skb_copy(skb, GFP_ATOMIC);
134
135                 if (newskb) {
136                         memcpy(newskb->data, psta->hwaddr, 6);
137                         res = rtw_xmit(padapter, &newskb);
138                         if (res < 0) {
139                                 DBG_88E("%s()-%d: rtw_xmit() return error!\n", __func__, __LINE__);
140                                 pxmitpriv->tx_drop++;
141                                 dev_kfree_skb_any(newskb);
142                         } else {
143                                 pxmitpriv->tx_pkts++;
144                         }
145                 } else {
146                         DBG_88E("%s-%d: skb_copy() failed!\n", __func__, __LINE__);
147                         pxmitpriv->tx_drop++;
148
149                         spin_unlock_bh(&pstapriv->asoc_list_lock);
150                         return false;   /*  Caller shall tx this multicast frame via normal way. */
151                 }
152         }
153
154         spin_unlock_bh(&pstapriv->asoc_list_lock);
155         dev_kfree_skb_any(skb);
156         return true;
157 }
158
159 int rtw_xmit_entry(struct sk_buff *pkt, struct  net_device *pnetdev)
160 {
161         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
162         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
163         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
164         s32 res = 0;
165
166         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
167
168         if (rtw_if_up(padapter) == false) {
169                 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
170                 goto drop_packet;
171         }
172
173         rtw_check_xmit_resource(padapter, pkt);
174
175         if (!rtw_mc2u_disable && check_fwstate(pmlmepriv, WIFI_AP_STATE) &&
176             (IP_MCAST_MAC(pkt->data) || ICMPV6_MCAST_MAC(pkt->data)) &&
177             (padapter->registrypriv.wifi_spec == 0)) {
178                 if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4)) {
179                         res = rtw_mlcst2unicst(padapter, pkt);
180                         if (res)
181                                 goto exit;
182                 }
183         }
184
185         res = rtw_xmit(padapter, &pkt);
186         if (res < 0)
187                 goto drop_packet;
188
189         pxmitpriv->tx_pkts++;
190         RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
191         goto exit;
192
193 drop_packet:
194         pxmitpriv->tx_drop++;
195         dev_kfree_skb_any(pkt);
196         RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));
197
198 exit:
199         return 0;
200 }