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