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