1 /******************************************************************************
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
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.
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
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
19 ******************************************************************************/
20 #define _XMIT_OSDEP_C_
22 #include <drv_types.h>
24 #define DBG_DUMP_OS_QUEUE_CTL 0
26 uint rtw_remainder_len(struct pkt_file *pfile)
28 return (pfile->buf_len - ((SIZE_PTR)(pfile->cur_addr) - (SIZE_PTR)(pfile->buf_start)));
31 void _rtw_open_pktfile (_pkt *pktptr, struct pkt_file *pfile)
36 pfile->cur_addr = pfile->buf_start = pktptr->data;
37 pfile->pkt_len = pfile->buf_len = pktptr->len;
39 pfile->cur_buffer = pfile->buf_start ;
44 uint _rtw_pktfile_read (struct pkt_file *pfile, u8 *rmem, uint rlen)
50 len = rtw_remainder_len(pfile);
51 len = (rlen > len)? len: rlen;
54 skb_copy_bits(pfile->pkt, pfile->buf_len-pfile->pkt_len, rmem, len);
56 pfile->cur_addr += len;
57 pfile->pkt_len -= len;
64 sint rtw_endofpktfile(struct pkt_file *pfile)
68 if (pfile->pkt_len == 0) {
78 void rtw_set_tx_chksum_offload(_pkt *pkt, struct pkt_attrib *pattrib)
81 #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
82 struct sk_buff *skb = (struct sk_buff *)pkt;
83 pattrib->hw_tcp_csum = 0;
85 if (skb->ip_summed == CHECKSUM_PARTIAL) {
86 if (skb_shinfo(skb)->nr_frags == 0)
88 const struct iphdr *ip = ip_hdr(skb);
89 if (ip->protocol == IPPROTO_TCP) {
90 // TCP checksum offload by HW
91 DBG_871X("CHECKSUM_PARTIAL TCP\n");
92 pattrib->hw_tcp_csum = 1;
93 //skb_checksum_help(skb);
94 } else if (ip->protocol == IPPROTO_UDP) {
95 //DBG_871X("CHECKSUM_PARTIAL UDP\n");
97 skb_checksum_help(skb);
99 // Set UDP checksum = 0 to skip checksum check
100 struct udphdr *udp = skb_transport_header(skb);
104 DBG_871X("%s-%d TCP CSUM offload Error!!\n", __FUNCTION__, __LINE__);
105 WARN_ON(1); /* we need a WARN() */
108 else { // IP fragmentation case
109 DBG_871X("%s-%d nr_frags != 0, using skb_checksum_help(skb);!!\n", __FUNCTION__, __LINE__);
110 skb_checksum_help(skb);
117 int rtw_os_xmit_resource_alloc(_adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz, u8 flag)
120 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
121 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
122 struct usb_device *pusbd = pdvobjpriv->pusbdev;
124 pxmitbuf->pallocated_buf = rtw_usb_buffer_alloc(pusbd, (size_t)alloc_sz, &pxmitbuf->dma_transfer_addr);
125 pxmitbuf->pbuf = pxmitbuf->pallocated_buf;
126 if(pxmitbuf->pallocated_buf == NULL)
128 #else // CONFIG_USE_USB_BUFFER_ALLOC_TX
130 pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
131 if (pxmitbuf->pallocated_buf == NULL)
136 pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
138 #endif // CONFIG_USE_USB_BUFFER_ALLOC_TX
142 #ifdef CONFIG_USB_HCI
146 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
147 if(pxmitbuf->pxmit_urb[i] == NULL)
149 DBG_871X("pxmitbuf->pxmit_urb[i]==NULL");
159 void rtw_os_xmit_resource_free(_adapter *padapter, struct xmit_buf *pxmitbuf,u32 free_sz, u8 flag)
162 #ifdef CONFIG_USB_HCI
167 if(pxmitbuf->pxmit_urb[i])
169 //usb_kill_urb(pxmitbuf->pxmit_urb[i]);
170 usb_free_urb(pxmitbuf->pxmit_urb[i]);
177 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
178 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
179 struct usb_device *pusbd = pdvobjpriv->pusbdev;
181 rtw_usb_buffer_free(pusbd, (size_t)free_sz, pxmitbuf->pallocated_buf, pxmitbuf->dma_transfer_addr);
182 pxmitbuf->pallocated_buf = NULL;
183 pxmitbuf->dma_transfer_addr = 0;
184 #else // CONFIG_USE_USB_BUFFER_ALLOC_TX
185 if(pxmitbuf->pallocated_buf)
186 rtw_mfree(pxmitbuf->pallocated_buf, free_sz);
187 #endif // CONFIG_USE_USB_BUFFER_ALLOC_TX
191 void dump_os_queue(void *sel, _adapter *padapter)
193 struct net_device *ndev = padapter->pnetdev;
195 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
199 DBG_871X_SEL_NL(sel, "os_queue[%d]:%s\n"
200 , i, __netif_subqueue_stopped(ndev, i)?"stopped":"waked");
203 DBG_871X_SEL_NL(sel, "os_queue:%s\n"
204 , netif_queue_stopped(ndev)?"stopped":"waked");
208 #define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5)
210 inline static bool rtw_os_need_wake_queue(_adapter *padapter, u16 qidx)
212 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
213 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
215 if (padapter->registrypriv.wifi_spec) {
216 if (pxmitpriv->hwxmits[qidx].accnt < WMM_XMIT_THRESHOLD)
227 inline static bool rtw_os_need_stop_queue(_adapter *padapter, u16 qidx)
229 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
230 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
231 if (padapter->registrypriv.wifi_spec) {
232 /* No free space for Tx, tx_worker is too slow */
233 if (pxmitpriv->hwxmits[qidx].accnt > WMM_XMIT_THRESHOLD)
236 if(pxmitpriv->free_xmitframe_cnt<=4)
240 if(pxmitpriv->free_xmitframe_cnt<=4)
246 void rtw_os_pkt_complete(_adapter *padapter, _pkt *pkt)
248 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
251 qidx = skb_get_queue_mapping(pkt);
252 if (rtw_os_need_wake_queue(padapter, qidx)) {
253 if (DBG_DUMP_OS_QUEUE_CTL)
254 DBG_871X(FUNC_ADPT_FMT": netif_wake_subqueue[%d]\n", FUNC_ADPT_ARG(padapter), qidx);
255 netif_wake_subqueue(padapter->pnetdev, qidx);
258 if (rtw_os_need_wake_queue(padapter, 0)) {
259 if (DBG_DUMP_OS_QUEUE_CTL)
260 DBG_871X(FUNC_ADPT_FMT": netif_wake_queue\n", FUNC_ADPT_ARG(padapter));
261 netif_wake_queue(padapter->pnetdev);
268 void rtw_os_xmit_complete(_adapter *padapter, struct xmit_frame *pxframe)
271 rtw_os_pkt_complete(padapter, pxframe->pkt);
276 void rtw_os_xmit_schedule(_adapter *padapter)
278 _adapter *pri_adapter = padapter;
280 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
284 #ifdef CONFIG_CONCURRENT_MODE
285 if(padapter->adapter_type > PRIMARY_ADAPTER)
286 pri_adapter = padapter->pbuddy_adapter;
289 if (_rtw_queue_empty(&padapter->xmitpriv.pending_xmitbuf_queue) == _FALSE)
290 _rtw_up_sema(&pri_adapter->xmitpriv.xmit_sema);
295 struct xmit_priv *pxmitpriv;
300 pxmitpriv = &padapter->xmitpriv;
302 _enter_critical_bh(&pxmitpriv->lock, &irqL);
304 if(rtw_txframes_pending(padapter))
306 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
309 _exit_critical_bh(&pxmitpriv->lock, &irqL);
313 static bool rtw_check_xmit_resource(_adapter *padapter, _pkt *pkt)
316 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
317 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
320 qidx = skb_get_queue_mapping(pkt);
321 if (rtw_os_need_stop_queue(padapter, qidx)) {
322 if (DBG_DUMP_OS_QUEUE_CTL)
323 DBG_871X(FUNC_ADPT_FMT": netif_stop_subqueue[%d]\n", FUNC_ADPT_ARG(padapter), qidx);
324 netif_stop_subqueue(padapter->pnetdev, qidx);
328 if (rtw_os_need_stop_queue(padapter, 0)) {
329 if (DBG_DUMP_OS_QUEUE_CTL)
330 DBG_871X(FUNC_ADPT_FMT": netif_stop_queue\n", FUNC_ADPT_ARG(padapter));
331 rtw_netif_stop_queue(padapter->pnetdev);
338 void rtw_os_wake_queue_at_free_stainfo(_adapter *padapter, int *qcnt_freed)
340 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
344 if (qcnt_freed[i] == 0)
347 if(rtw_os_need_wake_queue(padapter, i)) {
348 if (DBG_DUMP_OS_QUEUE_CTL)
349 DBG_871X(FUNC_ADPT_FMT": netif_wake_subqueue[%d]\n", FUNC_ADPT_ARG(padapter), i);
350 netif_wake_subqueue(padapter->pnetdev, i);
354 if (qcnt_freed[0] || qcnt_freed[1] || qcnt_freed[2] || qcnt_freed[3]) {
355 if(rtw_os_need_wake_queue(padapter, 0)) {
356 if (DBG_DUMP_OS_QUEUE_CTL)
357 DBG_871X(FUNC_ADPT_FMT": netif_wake_queue\n", FUNC_ADPT_ARG(padapter));
358 netif_wake_queue(padapter->pnetdev);
364 #ifdef CONFIG_TX_MCAST2UNI
365 int rtw_mlcst2unicst(_adapter *padapter, struct sk_buff *skb)
367 struct sta_priv *pstapriv = &padapter->stapriv;
368 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
370 _list *phead, *plist;
371 struct sk_buff *newskb;
372 struct sta_info *psta = NULL;
373 u8 chk_alive_num = 0;
374 char chk_alive_list[NUM_STA];
375 u8 bc_addr[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
376 u8 null_addr[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
381 DBG_COUNTER(padapter->tx_logs.os_tx_m2u);
383 _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
384 phead = &pstapriv->asoc_list;
385 plist = get_next(phead);
387 //free sta asoc_queue
388 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
390 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
391 plist = get_next(plist);
393 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
394 if (stainfo_offset_valid(stainfo_offset)) {
395 chk_alive_list[chk_alive_num++] = stainfo_offset;
398 _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
400 for (i = 0; i < chk_alive_num; i++) {
401 psta = rtw_get_stainfo_by_offset(pstapriv, chk_alive_list[i]);
402 if(!(psta->state &_FW_LINKED))
404 DBG_COUNTER(padapter->tx_logs.os_tx_m2u_ignore_fw_linked);
408 /* avoid come from STA1 and send back STA1 */
409 if (_rtw_memcmp(psta->hwaddr, &skb->data[6], 6) == _TRUE
410 || _rtw_memcmp(psta->hwaddr, null_addr, 6) == _TRUE
411 || _rtw_memcmp(psta->hwaddr, bc_addr, 6) == _TRUE
414 DBG_COUNTER(padapter->tx_logs.os_tx_m2u_ignore_self);
418 DBG_COUNTER(padapter->tx_logs.os_tx_m2u_entry);
420 newskb = rtw_skb_copy(skb);
423 _rtw_memcpy(newskb->data, psta->hwaddr, 6);
424 res = rtw_xmit(padapter, &newskb);
426 DBG_COUNTER(padapter->tx_logs.os_tx_m2u_entry_err_xmit);
427 DBG_871X("%s()-%d: rtw_xmit() return error!\n", __FUNCTION__, __LINE__);
428 pxmitpriv->tx_drop++;
429 rtw_skb_free(newskb);
432 DBG_COUNTER(padapter->tx_logs.os_tx_m2u_entry_err_skb);
433 DBG_871X("%s-%d: rtw_skb_copy() failed!\n", __FUNCTION__, __LINE__);
434 pxmitpriv->tx_drop++;
436 return _FALSE; // Caller shall tx this multicast frame via normal way.
443 #endif // CONFIG_TX_MCAST2UNI
446 int _rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
448 _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev);
449 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
450 #ifdef CONFIG_TX_MCAST2UNI
451 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
452 extern int rtw_mc2u_disable;
453 #endif // CONFIG_TX_MCAST2UNI
455 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
461 if(padapter->registrypriv.mp_mode)
463 DBG_871X("MP_TX_DROP_OS_FRAME\n");
466 DBG_COUNTER(padapter->tx_logs.os_tx);
467 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
469 if (rtw_if_up(padapter) == _FALSE) {
470 DBG_COUNTER(padapter->tx_logs.os_tx_err_up);
471 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
472 #ifdef DBG_TX_DROP_FRAME
473 DBG_871X("DBG_TX_DROP_FRAME %s if_up fail\n", __FUNCTION__);
478 rtw_check_xmit_resource(padapter, pkt);
480 #ifdef CONFIG_TX_MCAST2UNI
481 if ( !rtw_mc2u_disable
482 && check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE
483 && ( IP_MCAST_MAC(pkt->data)
484 || ICMPV6_MCAST_MAC(pkt->data)
485 #ifdef CONFIG_TX_BCAST2UNI
486 || is_broadcast_mac_addr(pkt->data)
489 && (padapter->registrypriv.wifi_spec == 0)
492 if ( pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4) ) {
493 res = rtw_mlcst2unicst(padapter, pkt);
498 //DBG_871X("Stop M2U(%d, %d)! ", pxmitpriv->free_xmitframe_cnt, pxmitpriv->free_xmitbuf_cnt);
500 DBG_COUNTER(padapter->tx_logs.os_tx_m2u_stop);
503 #endif // CONFIG_TX_MCAST2UNI
505 res = rtw_xmit(padapter, &pkt);
507 #ifdef DBG_TX_DROP_FRAME
508 DBG_871X("DBG_TX_DROP_FRAME %s rtw_xmit fail\n", __FUNCTION__);
513 RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
517 pxmitpriv->tx_drop++;
518 rtw_os_pkt_complete(padapter, pkt);
519 RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));
528 int rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
533 rtw_mstat_update(MSTAT_TYPE_SKB, MSTAT_ALLOC_SUCCESS, pkt->truesize);
534 ret = _rtw_xmit_entry(pkt, pnetdev);