OSDN Git Service

rtl8723au: Remove special endian headers and use kernel facilities instead
[android-x86/external-modules-rtl8723au.git] / 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 <drv_conf.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 <circ_buf.h>
33
34 uint rtw_remainder_len(struct pkt_file *pfile)
35 {
36         return (pfile->buf_len - ((SIZE_PTR)(pfile->cur_addr) - (SIZE_PTR)(pfile->buf_start)));
37 }
38
39 void _rtw_open_pktfile (_pkt *pktptr, struct pkt_file *pfile)
40 {
41 _func_enter_;
42
43         pfile->pkt = pktptr;
44         pfile->cur_addr = pfile->buf_start = pktptr->data;
45         pfile->pkt_len = pfile->buf_len = pktptr->len;
46
47         pfile->cur_buffer = pfile->buf_start ;
48         
49 _func_exit_;
50 }
51
52 uint _rtw_pktfile_read (struct pkt_file *pfile, u8 *rmem, uint rlen)
53 {       
54         uint    len = 0;
55         
56 _func_enter_;
57
58        len =  rtw_remainder_len(pfile);
59         len = (rlen > len)? len: rlen;
60
61        if(rmem)
62           skb_copy_bits(pfile->pkt, pfile->buf_len-pfile->pkt_len, rmem, len);
63
64        pfile->cur_addr += len;
65        pfile->pkt_len -= len;
66            
67 _func_exit_;                    
68
69         return len;     
70 }
71
72 sint rtw_endofpktfile(struct pkt_file *pfile)
73 {
74 _func_enter_;
75
76         if (pfile->pkt_len == 0) {
77 _func_exit_;
78                 return _TRUE;
79         }
80
81 _func_exit_;
82
83         return _FALSE;
84 }
85
86 void rtw_set_tx_chksum_offload(_pkt *pkt, struct pkt_attrib *pattrib)
87 {
88
89 #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
90         struct sk_buff *skb = (struct sk_buff *)pkt;
91         pattrib->hw_tcp_csum = 0;
92         
93         if (skb->ip_summed == CHECKSUM_PARTIAL) {
94                 if (skb_shinfo(skb)->nr_frags == 0)
95                 {       
96                         const struct iphdr *ip = ip_hdr(skb);
97                         if (ip->protocol == IPPROTO_TCP) {
98                                 // TCP checksum offload by HW
99                                 DBG_8723A("CHECKSUM_PARTIAL TCP\n");
100                                 pattrib->hw_tcp_csum = 1;
101                                 //skb_checksum_help(skb);
102                         } else if (ip->protocol == IPPROTO_UDP) {
103                                 //DBG_8723A("CHECKSUM_PARTIAL UDP\n");
104 #if 1                       
105                                 skb_checksum_help(skb);
106 #else
107                                 // Set UDP checksum = 0 to skip checksum check
108                                 struct udphdr *udp = skb_transport_header(skb);
109                                 udp->check = 0;
110 #endif
111                         } else {
112                                 DBG_8723A("%s-%d TCP CSUM offload Error!!\n", __FUNCTION__, __LINE__);
113                                 WARN_ON(1);     /* we need a WARN() */
114                             }
115                 }
116                 else { // IP fragmentation case
117                         DBG_8723A("%s-%d nr_frags != 0, using skb_checksum_help(skb);!!\n", __FUNCTION__, __LINE__);
118                         skb_checksum_help(skb);
119                 }               
120         }
121 #endif  
122         
123 }
124
125 int rtw_os_xmit_resource_alloc(_adapter *padapter, struct xmit_buf *pxmitbuf,u32 alloc_sz)
126 {
127 #ifdef CONFIG_USB_HCI
128         int i;
129         struct dvobj_priv       *pdvobjpriv = adapter_to_dvobj(padapter);
130         struct usb_device       *pusbd = pdvobjpriv->pusbdev;
131
132 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
133         pxmitbuf->pallocated_buf = rtw_usb_buffer_alloc(pusbd, (size_t)alloc_sz, &pxmitbuf->dma_transfer_addr);
134         pxmitbuf->pbuf = pxmitbuf->pallocated_buf;
135         if(pxmitbuf->pallocated_buf == NULL)
136                 return _FAIL;
137 #else // CONFIG_USE_USB_BUFFER_ALLOC_TX
138         
139         pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
140         if (pxmitbuf->pallocated_buf == NULL)
141         {
142                 return _FAIL;
143         }
144
145         pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
146         pxmitbuf->dma_transfer_addr = 0;
147
148 #endif // CONFIG_USE_USB_BUFFER_ALLOC_TX
149
150         for(i=0; i<8; i++)
151         {
152                 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
153                 if(pxmitbuf->pxmit_urb[i] == NULL) 
154                 {
155                         DBG_8723A("pxmitbuf->pxmit_urb[i]==NULL");
156                         return _FAIL;    
157                 }                       
158         
159         }
160 #endif
161 #if defined(CONFIG_PCI_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
162         pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
163         if (pxmitbuf->pallocated_buf == NULL)
164         {
165                 return _FAIL;
166         }
167
168         pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
169 #endif
170
171         return _SUCCESS;        
172 }
173
174 void rtw_os_xmit_resource_free(_adapter *padapter, struct xmit_buf *pxmitbuf,u32 free_sz)
175 {
176 #ifdef CONFIG_USB_HCI
177         int i;
178         struct dvobj_priv       *pdvobjpriv = adapter_to_dvobj(padapter);
179         struct usb_device       *pusbd = pdvobjpriv->pusbdev;
180
181
182         for(i=0; i<8; i++)
183         {
184                 if(pxmitbuf->pxmit_urb[i])
185                 {
186                         //usb_kill_urb(pxmitbuf->pxmit_urb[i]);
187                         usb_free_urb(pxmitbuf->pxmit_urb[i]);
188                 }
189         }
190
191 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
192         rtw_usb_buffer_free(pusbd, (size_t)free_sz, pxmitbuf->pallocated_buf, pxmitbuf->dma_transfer_addr);
193         pxmitbuf->pallocated_buf =  NULL;
194         pxmitbuf->dma_transfer_addr = 0;
195 #else   // CONFIG_USE_USB_BUFFER_ALLOC_TX
196         if(pxmitbuf->pallocated_buf)
197                 rtw_mfree(pxmitbuf->pallocated_buf, free_sz);
198 #endif  // CONFIG_USE_USB_BUFFER_ALLOC_TX
199
200 #endif
201 #if defined(CONFIG_PCI_HCI) || defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
202         if(pxmitbuf->pallocated_buf)
203                 rtw_mfree(pxmitbuf->pallocated_buf, free_sz);
204 #endif
205 }
206
207 #define WMM_XMIT_THRESHOLD      (NR_XMITFRAME*2/5)
208
209 void rtw_os_pkt_complete(_adapter *padapter, _pkt *pkt)
210 {
211 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
212         u16     queue;
213         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
214
215         queue = skb_get_queue_mapping(pkt);
216         if (padapter->registrypriv.wifi_spec) {
217                 if(__netif_subqueue_stopped(padapter->pnetdev, queue) &&
218                         (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD))
219                 {
220                         netif_wake_subqueue(padapter->pnetdev, queue);
221                 }
222         } else {
223                 if(__netif_subqueue_stopped(padapter->pnetdev, queue))
224                         netif_wake_subqueue(padapter->pnetdev, queue);
225         }
226 #else
227         if (netif_queue_stopped(padapter->pnetdev))
228                 netif_wake_queue(padapter->pnetdev);
229 #endif
230
231         dev_kfree_skb_any(pkt);
232 }
233
234 void rtw_os_xmit_complete(_adapter *padapter, struct xmit_frame *pxframe)
235 {
236         if(pxframe->pkt)
237         {
238                 //RT_TRACE(_module_xmit_osdep_c_,_drv_err_,("linux : rtw_os_xmit_complete, dev_kfree_skb()\n"));        
239
240                 //dev_kfree_skb_any(pxframe->pkt);      
241                 rtw_os_pkt_complete(padapter, pxframe->pkt);
242                 
243         }       
244
245         pxframe->pkt = NULL;
246 }
247
248 void rtw_os_xmit_schedule(_adapter *padapter)
249 {
250         _adapter *pri_adapter = padapter;
251
252 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
253         if(!padapter)
254                 return;
255
256 #ifdef CONFIG_CONCURRENT_MODE
257         if(padapter->adapter_type > PRIMARY_ADAPTER)
258                 pri_adapter = padapter->pbuddy_adapter;
259 #endif
260
261         if (_rtw_queue_empty(&pri_adapter->xmitpriv.pending_xmitbuf_queue) == _FALSE)
262                 _rtw_up_sema(&pri_adapter->xmitpriv.xmit_sema);
263
264
265 #else
266         _irqL  irqL;
267         struct xmit_priv *pxmitpriv;
268
269         if(!padapter)
270                 return;
271
272         pxmitpriv = &padapter->xmitpriv;
273
274         _enter_critical_bh(&pxmitpriv->lock, &irqL);
275
276         if(rtw_txframes_pending(padapter))      
277         {
278                 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
279         }
280
281         _exit_critical_bh(&pxmitpriv->lock, &irqL);
282 #endif
283 }
284
285 static void rtw_check_xmit_resource(_adapter *padapter, _pkt *pkt)
286 {
287         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
288 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
289         u16     queue;
290
291         queue = skb_get_queue_mapping(pkt);
292         if (padapter->registrypriv.wifi_spec) {
293                 /* No free space for Tx, tx_worker is too slow */
294                 if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD) {
295                         //DBG_8723A("%s(): stop netif_subqueue[%d]\n", __FUNCTION__, queue);
296                         netif_stop_subqueue(padapter->pnetdev, queue);
297                 }
298         } else {
299                 if(pxmitpriv->free_xmitframe_cnt<=4) {
300                         if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue)))
301                                 netif_stop_subqueue(padapter->pnetdev, queue);
302                 }
303         }
304 #else
305         if(pxmitpriv->free_xmitframe_cnt<=4)
306         {
307                 if (!rtw_netif_queue_stopped(padapter->pnetdev))
308                         rtw_netif_stop_queue(padapter->pnetdev);
309         }
310 #endif
311 }
312
313 #ifdef CONFIG_TX_MCAST2UNI
314 int rtw_mlcst2unicst(_adapter *padapter, struct sk_buff *skb)
315 {
316         struct  sta_priv *pstapriv = &padapter->stapriv;
317         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
318         _irqL   irqL;
319         _list   *phead, *plist;
320         struct sk_buff *newskb;
321         struct sta_info *psta = NULL;
322         u8 chk_alive_num = 0;
323         char chk_alive_list[NUM_STA];
324         u8 bc_addr[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
325         u8 null_addr[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
326
327         int i;
328         s32     res;
329
330         _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
331         phead = &pstapriv->asoc_list;
332         plist = get_next(phead);
333         
334         //free sta asoc_queue
335         while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
336                 int stainfo_offset;
337                 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
338                 plist = get_next(plist);
339
340                 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
341                 if (stainfo_offset_valid(stainfo_offset)) {
342                         chk_alive_list[chk_alive_num++] = stainfo_offset;
343                 }
344         }
345         _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
346
347         for (i = 0; i < chk_alive_num; i++) {
348                 psta = rtw_get_stainfo_by_offset(pstapriv, chk_alive_list[i]);
349                 if(!(psta->state &_FW_LINKED))
350                         continue;               
351                 
352                 /* avoid come from STA1 and send back STA1 */ 
353                 if (_rtw_memcmp(psta->hwaddr, &skb->data[6], 6) == _TRUE
354                         || _rtw_memcmp(psta->hwaddr, null_addr, 6) == _TRUE
355                         || _rtw_memcmp(psta->hwaddr, bc_addr, 6) == _TRUE
356                 )
357                         continue;
358
359                 newskb = skb_copy(skb, GFP_ATOMIC);
360
361                 if (newskb) {
362                         _rtw_memcpy(newskb->data, psta->hwaddr, 6);
363                         res = rtw_xmit(padapter, &newskb);
364                         if (res < 0) {
365                                 DBG_8723A("%s()-%d: rtw_xmit() return error!\n", __FUNCTION__, __LINE__);
366                                 pxmitpriv->tx_drop++;
367                                 dev_kfree_skb_any(newskb);                      
368                         } else
369                                 pxmitpriv->tx_pkts++;
370                 } else {
371                         DBG_8723A("%s-%d: skb_copy() failed!\n", __FUNCTION__, __LINE__);
372                         pxmitpriv->tx_drop++;
373                         //dev_kfree_skb_any(skb);
374                         return _FALSE;  // Caller shall tx this multicast frame via normal way.
375                 }
376         }
377
378         dev_kfree_skb_any(skb);
379         return _TRUE;
380 }
381 #endif  // CONFIG_TX_MCAST2UNI
382
383
384 int rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
385 {
386         _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev);
387         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
388 #ifdef CONFIG_TX_MCAST2UNI
389         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
390         extern int rtw_mc2u_disable;
391 #endif  // CONFIG_TX_MCAST2UNI  
392         s32 res = 0;
393 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
394         u16 queue;
395 #endif
396
397 _func_enter_;
398
399         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
400
401         if (rtw_if_up(padapter) == _FALSE) {
402                 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
403                 #ifdef DBG_TX_DROP_FRAME
404                 DBG_8723A("DBG_TX_DROP_FRAME %s if_up fail\n", __FUNCTION__);
405                 #endif
406                 goto drop_packet;
407         }
408
409         rtw_check_xmit_resource(padapter, pkt);
410
411 #ifdef CONFIG_TX_MCAST2UNI
412         if ( !rtw_mc2u_disable
413                 && check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE
414                 && ( IP_MCAST_MAC(pkt->data)
415                         || ICMPV6_MCAST_MAC(pkt->data) )
416                 && (padapter->registrypriv.wifi_spec == 0)
417                 )
418         {
419                 if ( pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4) ) {
420                         res = rtw_mlcst2unicst(padapter, pkt);
421                         if (res == _TRUE) {
422                                 goto exit;
423                         }
424                 } else {
425                         //DBG_8723A("Stop M2U(%d, %d)! ", pxmitpriv->free_xmitframe_cnt, pxmitpriv->free_xmitbuf_cnt);
426                         //DBG_8723A("!m2u );
427                 }
428         }       
429 #endif  // CONFIG_TX_MCAST2UNI  
430
431         res = rtw_xmit(padapter, &pkt);
432         if (res < 0) {
433                 #ifdef DBG_TX_DROP_FRAME
434                 DBG_8723A("DBG_TX_DROP_FRAME %s rtw_xmit fail\n", __FUNCTION__);
435                 #endif
436                 goto drop_packet;
437         }
438
439         pxmitpriv->tx_pkts++;
440         RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
441         goto exit;
442
443 drop_packet:
444         pxmitpriv->tx_drop++;
445         dev_kfree_skb_any(pkt);
446         RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));
447
448 exit:
449
450 _func_exit_;
451
452         return 0;
453 }
454