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