Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[firefly-linux-kernel-4.4.55.git] / drivers / staging / rtl8188eu / os_dep / xmit_linux.c
1 /******************************************************************************
2  *
3  * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
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  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
17  *
18  *
19  ******************************************************************************/
20 #define _XMIT_OSDEP_C_
21
22 #include <linux/version.h>
23 #include <osdep_service.h>
24 #include <drv_types.h>
25
26 #include <if_ether.h>
27 #include <ip.h>
28 #include <wifi.h>
29 #include <mlme_osdep.h>
30 #include <xmit_osdep.h>
31 #include <osdep_intf.h>
32 #include <usb_osintf.h>
33
34 uint rtw_remainder_len(struct pkt_file *pfile)
35 {
36         return pfile->buf_len - ((size_t)(pfile->cur_addr) -
37                (size_t)(pfile->buf_start));
38 }
39
40 void _rtw_open_pktfile(struct sk_buff *pktptr, struct pkt_file *pfile)
41 {
42 _func_enter_;
43
44         pfile->pkt = pktptr;
45         pfile->cur_addr = pktptr->data;
46         pfile->buf_start = pktptr->data;
47         pfile->pkt_len = pktptr->len;
48         pfile->buf_len = pktptr->len;
49
50         pfile->cur_buffer = pfile->buf_start;
51
52 _func_exit_;
53 }
54
55 uint _rtw_pktfile_read (struct pkt_file *pfile, u8 *rmem, uint rlen)
56 {
57         uint    len = 0;
58
59 _func_enter_;
60
61         len =  rtw_remainder_len(pfile);
62         len = (rlen > len) ? len : rlen;
63
64         if (rmem)
65                 skb_copy_bits(pfile->pkt, pfile->buf_len-pfile->pkt_len, rmem, len);
66
67         pfile->cur_addr += len;
68         pfile->pkt_len -= len;
69
70 _func_exit_;
71
72         return len;
73 }
74
75 int rtw_endofpktfile(struct pkt_file *pfile)
76 {
77 _func_enter_;
78
79         if (pfile->pkt_len == 0) {
80         _func_exit_;
81                 return true;
82         }
83
84 _func_exit_;
85
86         return false;
87 }
88
89 void rtw_set_tx_chksum_offload(struct sk_buff *pkt, struct pkt_attrib *pattrib)
90 {
91 }
92
93 int rtw_os_xmit_resource_alloc(struct adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz)
94 {
95         int i;
96
97         pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
98         if (pxmitbuf->pallocated_buf == NULL)
99                 return _FAIL;
100
101         pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
102         pxmitbuf->dma_transfer_addr = 0;
103
104         for (i = 0; i < 8; i++) {
105                 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
106                 if (pxmitbuf->pxmit_urb[i] == NULL) {
107                         DBG_88E("pxmitbuf->pxmit_urb[i]==NULL");
108                         return _FAIL;
109                 }
110         }
111         return _SUCCESS;
112 }
113
114 void rtw_os_xmit_resource_free(struct adapter *padapter,
115                                struct xmit_buf *pxmitbuf, u32 free_sz)
116 {
117         int i;
118
119         for (i = 0; i < 8; i++)
120                 usb_free_urb(pxmitbuf->pxmit_urb[i]);
121
122         kfree(pxmitbuf->pallocated_buf);
123 }
124
125 #define WMM_XMIT_THRESHOLD      (NR_XMITFRAME*2/5)
126
127 void rtw_os_pkt_complete(struct adapter *padapter, struct sk_buff *pkt)
128 {
129 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
130         u16     queue;
131         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
132
133         queue = skb_get_queue_mapping(pkt);
134         if (padapter->registrypriv.wifi_spec) {
135                 if (__netif_subqueue_stopped(padapter->pnetdev, queue) &&
136                     (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD))
137                         netif_wake_subqueue(padapter->pnetdev, queue);
138         } else {
139                 if (__netif_subqueue_stopped(padapter->pnetdev, queue))
140                         netif_wake_subqueue(padapter->pnetdev, queue);
141         }
142 #else
143         if (netif_queue_stopped(padapter->pnetdev))
144                 netif_wake_queue(padapter->pnetdev);
145 #endif
146
147         dev_kfree_skb_any(pkt);
148 }
149
150 void rtw_os_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe)
151 {
152         if (pxframe->pkt)
153                 rtw_os_pkt_complete(padapter, pxframe->pkt);
154         pxframe->pkt = NULL;
155 }
156
157 void rtw_os_xmit_schedule(struct adapter *padapter)
158 {
159         unsigned long  irql;
160         struct xmit_priv *pxmitpriv;
161
162         if (!padapter)
163                 return;
164
165         pxmitpriv = &padapter->xmitpriv;
166
167         _enter_critical_bh(&pxmitpriv->lock, &irql);
168
169         if (rtw_txframes_pending(padapter))
170                 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
171
172         _exit_critical_bh(&pxmitpriv->lock, &irql);
173 }
174
175 static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt)
176 {
177         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
178         u16     queue;
179
180         queue = skb_get_queue_mapping(pkt);
181         if (padapter->registrypriv.wifi_spec) {
182                 /* No free space for Tx, tx_worker is too slow */
183                 if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD)
184                         netif_stop_subqueue(padapter->pnetdev, queue);
185         } else {
186                 if (pxmitpriv->free_xmitframe_cnt <= 4) {
187                         if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue)))
188                                 netif_stop_subqueue(padapter->pnetdev, queue);
189                 }
190         }
191 }
192
193 static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb)
194 {
195         struct  sta_priv *pstapriv = &padapter->stapriv;
196         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
197         unsigned long   irql;
198         struct list_head *phead, *plist;
199         struct sk_buff *newskb;
200         struct sta_info *psta = NULL;
201         s32     res;
202
203         _enter_critical_bh(&pstapriv->asoc_list_lock, &irql);
204         phead = &pstapriv->asoc_list;
205         plist = get_next(phead);
206
207         /* free sta asoc_queue */
208         while (!rtw_end_of_queue_search(phead, plist)) {
209                 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
210
211                 plist = get_next(plist);
212
213                 /* avoid   come from STA1 and send back STA1 */
214                 if (!memcmp(psta->hwaddr, &skb->data[6], 6))
215                         continue;
216
217                 newskb = skb_copy(skb, GFP_ATOMIC);
218
219                 if (newskb) {
220                         memcpy(newskb->data, psta->hwaddr, 6);
221                         res = rtw_xmit(padapter, &newskb);
222                         if (res < 0) {
223                                 DBG_88E("%s()-%d: rtw_xmit() return error!\n", __func__, __LINE__);
224                                 pxmitpriv->tx_drop++;
225                                 dev_kfree_skb_any(newskb);
226                         } else {
227                                 pxmitpriv->tx_pkts++;
228                         }
229                 } else {
230                         DBG_88E("%s-%d: skb_copy() failed!\n", __func__, __LINE__);
231                         pxmitpriv->tx_drop++;
232
233                         _exit_critical_bh(&pstapriv->asoc_list_lock, &irql);
234                         return false;   /*  Caller shall tx this multicast frame via normal way. */
235                 }
236         }
237
238         _exit_critical_bh(&pstapriv->asoc_list_lock, &irql);
239         dev_kfree_skb_any(skb);
240         return true;
241 }
242
243
244 int rtw_xmit_entry(struct sk_buff *pkt, struct  net_device *pnetdev)
245 {
246         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
247         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
248         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
249         s32 res = 0;
250
251 _func_enter_;
252
253         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
254
255         if (rtw_if_up(padapter) == false) {
256                 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
257                 goto drop_packet;
258         }
259
260         rtw_check_xmit_resource(padapter, pkt);
261
262         if (!rtw_mc2u_disable && check_fwstate(pmlmepriv, WIFI_AP_STATE) &&
263             (IP_MCAST_MAC(pkt->data) || ICMPV6_MCAST_MAC(pkt->data)) &&
264             (padapter->registrypriv.wifi_spec == 0)) {
265                 if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4)) {
266                         res = rtw_mlcst2unicst(padapter, pkt);
267                         if (res)
268                                 goto exit;
269                 }
270         }
271
272         res = rtw_xmit(padapter, &pkt);
273         if (res < 0)
274                 goto drop_packet;
275
276         pxmitpriv->tx_pkts++;
277         RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
278         goto exit;
279
280 drop_packet:
281         pxmitpriv->tx_drop++;
282         dev_kfree_skb_any(pkt);
283         RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));
284
285 exit:
286
287 _func_exit_;
288
289         return 0;
290 }