Linux-libre 3.14.34-gnu
[librecmc/linux-libre.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         struct xmit_priv *pxmitpriv;
160
161         if (!padapter)
162                 return;
163
164         pxmitpriv = &padapter->xmitpriv;
165
166         spin_lock_bh(&pxmitpriv->lock);
167
168         if (rtw_txframes_pending(padapter))
169                 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
170
171         spin_unlock_bh(&pxmitpriv->lock);
172 }
173
174 static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt)
175 {
176         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
177         u16     queue;
178
179         queue = skb_get_queue_mapping(pkt);
180         if (padapter->registrypriv.wifi_spec) {
181                 /* No free space for Tx, tx_worker is too slow */
182                 if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD)
183                         netif_stop_subqueue(padapter->pnetdev, queue);
184         } else {
185                 if (pxmitpriv->free_xmitframe_cnt <= 4) {
186                         if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue)))
187                                 netif_stop_subqueue(padapter->pnetdev, queue);
188                 }
189         }
190 }
191
192 static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb)
193 {
194         struct  sta_priv *pstapriv = &padapter->stapriv;
195         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
196         struct list_head *phead, *plist;
197         struct sk_buff *newskb;
198         struct sta_info *psta = NULL;
199         s32     res;
200
201         spin_lock_bh(&pstapriv->asoc_list_lock);
202         phead = &pstapriv->asoc_list;
203         plist = get_next(phead);
204
205         /* free sta asoc_queue */
206         while (!rtw_end_of_queue_search(phead, plist)) {
207                 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
208
209                 plist = get_next(plist);
210
211                 /* avoid   come from STA1 and send back STA1 */
212                 if (!memcmp(psta->hwaddr, &skb->data[6], 6))
213                         continue;
214
215                 newskb = skb_copy(skb, GFP_ATOMIC);
216
217                 if (newskb) {
218                         memcpy(newskb->data, psta->hwaddr, 6);
219                         res = rtw_xmit(padapter, &newskb);
220                         if (res < 0) {
221                                 DBG_88E("%s()-%d: rtw_xmit() return error!\n", __func__, __LINE__);
222                                 pxmitpriv->tx_drop++;
223                                 dev_kfree_skb_any(newskb);
224                         } else {
225                                 pxmitpriv->tx_pkts++;
226                         }
227                 } else {
228                         DBG_88E("%s-%d: skb_copy() failed!\n", __func__, __LINE__);
229                         pxmitpriv->tx_drop++;
230
231                         spin_unlock_bh(&pstapriv->asoc_list_lock);
232                         return false;   /*  Caller shall tx this multicast frame via normal way. */
233                 }
234         }
235
236         spin_unlock_bh(&pstapriv->asoc_list_lock);
237         dev_kfree_skb_any(skb);
238         return true;
239 }
240
241
242 int rtw_xmit_entry(struct sk_buff *pkt, struct  net_device *pnetdev)
243 {
244         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
245         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
246         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
247         s32 res = 0;
248
249 _func_enter_;
250
251         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
252
253         if (rtw_if_up(padapter) == false) {
254                 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
255                 goto drop_packet;
256         }
257
258         rtw_check_xmit_resource(padapter, pkt);
259
260         if (!rtw_mc2u_disable && check_fwstate(pmlmepriv, WIFI_AP_STATE) &&
261             (IP_MCAST_MAC(pkt->data) || ICMPV6_MCAST_MAC(pkt->data)) &&
262             (padapter->registrypriv.wifi_spec == 0)) {
263                 if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4)) {
264                         res = rtw_mlcst2unicst(padapter, pkt);
265                         if (res)
266                                 goto exit;
267                 }
268         }
269
270         res = rtw_xmit(padapter, &pkt);
271         if (res < 0)
272                 goto drop_packet;
273
274         pxmitpriv->tx_pkts++;
275         RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
276         goto exit;
277
278 drop_packet:
279         pxmitpriv->tx_drop++;
280         dev_kfree_skb_any(pkt);
281         RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));
282
283 exit:
284
285 _func_exit_;
286
287         return 0;
288 }