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