GNU Linux-libre 5.10.219-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 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
27         for (i = 0; i < 8; i++) {
28                 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
29                 if (!pxmitbuf->pxmit_urb[i]) {
30                         DBG_88E("pxmitbuf->pxmit_urb[i]==NULL");
31                         return _FAIL;
32                 }
33         }
34         return _SUCCESS;
35 }
36
37 void rtw_os_xmit_resource_free(struct xmit_buf *pxmitbuf)
38 {
39         int i;
40
41         for (i = 0; i < 8; i++)
42                 usb_free_urb(pxmitbuf->pxmit_urb[i]);
43
44         kfree(pxmitbuf->pallocated_buf);
45 }
46
47 #define WMM_XMIT_THRESHOLD      (NR_XMITFRAME * 2 / 5)
48
49 void rtw_os_pkt_complete(struct adapter *padapter, struct sk_buff *pkt)
50 {
51         u16 queue;
52         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
53
54         queue = skb_get_queue_mapping(pkt);
55         if (padapter->registrypriv.wifi_spec) {
56                 if (__netif_subqueue_stopped(padapter->pnetdev, queue) &&
57                     (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD))
58                         netif_wake_subqueue(padapter->pnetdev, queue);
59         } else {
60                 if (__netif_subqueue_stopped(padapter->pnetdev, queue))
61                         netif_wake_subqueue(padapter->pnetdev, queue);
62         }
63
64         dev_kfree_skb_any(pkt);
65 }
66
67 void rtw_os_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe)
68 {
69         if (pxframe->pkt)
70                 rtw_os_pkt_complete(padapter, pxframe->pkt);
71         pxframe->pkt = NULL;
72 }
73
74 void rtw_os_xmit_schedule(struct adapter *padapter)
75 {
76         struct xmit_priv *pxmitpriv;
77
78         if (!padapter)
79                 return;
80
81         pxmitpriv = &padapter->xmitpriv;
82
83         spin_lock_bh(&pxmitpriv->lock);
84
85         if (rtw_txframes_pending(padapter))
86                 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
87
88         spin_unlock_bh(&pxmitpriv->lock);
89 }
90
91 static void rtw_check_xmit_resource(struct adapter *padapter,
92                                     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",
140                                         __func__, __LINE__);
141                                 pxmitpriv->tx_drop++;
142                                 dev_kfree_skb_any(newskb);
143                         } else {
144                                 pxmitpriv->tx_pkts++;
145                         }
146                 } else {
147                         DBG_88E("%s-%d: skb_copy() failed!\n",
148                                 __func__, __LINE__);
149                         pxmitpriv->tx_drop++;
150
151                         spin_unlock_bh(&pstapriv->asoc_list_lock);
152
153                         /* Caller shall tx this multicast frame
154                          * via normal way.
155                          */
156                         return false;
157                 }
158         }
159
160         spin_unlock_bh(&pstapriv->asoc_list_lock);
161         dev_kfree_skb_any(skb);
162         return true;
163 }
164
165 int rtw_xmit_entry(struct sk_buff *pkt, struct net_device *pnetdev)
166 {
167         struct adapter *padapter = rtw_netdev_priv(pnetdev);
168         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
169         struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
170         s32 res = 0;
171
172         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
173
174         if (!rtw_if_up(padapter)) {
175                 RT_TRACE(_module_xmit_osdep_c_, _drv_err_,
176                          ("%s: rtw_if_up fail\n", __func__));
177                 goto drop_packet;
178         }
179
180         rtw_check_xmit_resource(padapter, pkt);
181
182         if (!rtw_mc2u_disable && check_fwstate(pmlmepriv, WIFI_AP_STATE) &&
183             (IP_MCAST_MAC(pkt->data) || ICMPV6_MCAST_MAC(pkt->data)) &&
184             (padapter->registrypriv.wifi_spec == 0)) {
185                 if (pxmitpriv->free_xmitframe_cnt > NR_XMITFRAME / 4) {
186                         res = rtw_mlcst2unicst(padapter, pkt);
187                         if (res)
188                                 goto exit;
189                 }
190         }
191
192         res = rtw_xmit(padapter, &pkt);
193         if (res < 0)
194                 goto drop_packet;
195
196         pxmitpriv->tx_pkts++;
197         RT_TRACE(_module_xmit_osdep_c_, _drv_info_,
198                  ("%s: tx_pkts=%d\n", __func__, (u32)pxmitpriv->tx_pkts));
199         goto exit;
200
201 drop_packet:
202         pxmitpriv->tx_drop++;
203         dev_kfree_skb_any(pkt);
204         RT_TRACE(_module_xmit_osdep_c_, _drv_notice_,
205                  ("%s: drop, tx_drop=%d\n", __func__, (u32)pxmitpriv->tx_drop));
206
207 exit:
208         return 0;
209 }