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