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_
23 #include <osdep_service.h>
24 #include <drv_types.h>
28 #include <rtw_byteorder.h>
30 #include <mlme_osdep.h>
31 #include <xmit_osdep.h>
32 #include <osdep_intf.h>
35 uint rtw_remainder_len(struct pkt_file *pfile)
37 return (pfile->buf_len - ((SIZE_PTR)(pfile->cur_addr) - (SIZE_PTR)(pfile->buf_start)));
40 void _rtw_open_pktfile (_pkt *pktptr, struct pkt_file *pfile)
45 pfile->cur_addr = pfile->buf_start = pktptr->data;
46 pfile->pkt_len = pfile->buf_len = pktptr->len;
48 pfile->cur_buffer = pfile->buf_start ;
53 uint _rtw_pktfile_read (struct pkt_file *pfile, u8 *rmem, uint rlen)
59 len = rtw_remainder_len(pfile);
60 len = (rlen > len)? len: rlen;
63 skb_copy_bits(pfile->pkt, pfile->buf_len-pfile->pkt_len, rmem, len);
65 pfile->cur_addr += len;
66 pfile->pkt_len -= len;
73 sint rtw_endofpktfile(struct pkt_file *pfile)
77 if (pfile->pkt_len == 0) {
87 void rtw_set_tx_chksum_offload(_pkt *pkt, struct pkt_attrib *pattrib)
90 #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
91 struct sk_buff *skb = (struct sk_buff *)pkt;
92 pattrib->hw_tcp_csum = 0;
94 if (skb->ip_summed == CHECKSUM_PARTIAL) {
95 if (skb_shinfo(skb)->nr_frags == 0)
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");
106 skb_checksum_help(skb);
108 // Set UDP checksum = 0 to skip checksum check
109 struct udphdr *udp = skb_transport_header(skb);
113 DBG_871X("%s-%d TCP CSUM offload Error!!\n", __FUNCTION__, __LINE__);
114 WARN_ON(1); /* we need a WARN() */
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);
126 int rtw_os_xmit_resource_alloc(_adapter *padapter, struct xmit_buf *pxmitbuf,u32 alloc_sz)
128 #ifdef CONFIG_USB_HCI
130 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
131 struct usb_device *pusbd = pdvobjpriv->pusbdev;
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)
138 #else // CONFIG_USE_USB_BUFFER_ALLOC_TX
140 pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
141 if (pxmitbuf->pallocated_buf == NULL)
146 pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
147 pxmitbuf->dma_transfer_addr = 0;
149 #endif // CONFIG_USE_USB_BUFFER_ALLOC_TX
153 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
154 if(pxmitbuf->pxmit_urb[i] == NULL)
156 DBG_871X("pxmitbuf->pxmit_urb[i]==NULL");
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)
169 pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
175 void rtw_os_xmit_resource_free(_adapter *padapter, struct xmit_buf *pxmitbuf,u32 free_sz)
177 #ifdef CONFIG_USB_HCI
179 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
180 struct usb_device *pusbd = pdvobjpriv->pusbdev;
185 if(pxmitbuf->pxmit_urb[i])
187 //usb_kill_urb(pxmitbuf->pxmit_urb[i]);
188 usb_free_urb(pxmitbuf->pxmit_urb[i]);
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
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);
208 #define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5)
210 void rtw_os_pkt_complete(_adapter *padapter, _pkt *pkt)
212 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
214 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
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))
221 netif_wake_subqueue(padapter->pnetdev, queue);
224 if(__netif_subqueue_stopped(padapter->pnetdev, queue))
225 netif_wake_subqueue(padapter->pnetdev, queue);
228 if (netif_queue_stopped(padapter->pnetdev))
229 netif_wake_queue(padapter->pnetdev);
235 void rtw_os_xmit_complete(_adapter *padapter, struct xmit_frame *pxframe)
238 rtw_os_pkt_complete(padapter, pxframe->pkt);
243 void rtw_os_xmit_schedule(_adapter *padapter)
245 _adapter *pri_adapter = padapter;
247 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
251 #ifdef CONFIG_CONCURRENT_MODE
252 if(padapter->adapter_type > PRIMARY_ADAPTER)
253 pri_adapter = padapter->pbuddy_adapter;
256 if (_rtw_queue_empty(&pri_adapter->xmitpriv.pending_xmitbuf_queue) == _FALSE)
257 _rtw_up_sema(&pri_adapter->xmitpriv.xmit_sema);
262 struct xmit_priv *pxmitpriv;
267 pxmitpriv = &padapter->xmitpriv;
269 _enter_critical_bh(&pxmitpriv->lock, &irqL);
271 if(rtw_txframes_pending(padapter))
273 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
276 _exit_critical_bh(&pxmitpriv->lock, &irqL);
280 static void rtw_check_xmit_resource(_adapter *padapter, _pkt *pkt)
282 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
283 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
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);
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);
300 if(pxmitpriv->free_xmitframe_cnt<=4)
302 if (!rtw_netif_queue_stopped(padapter->pnetdev))
303 rtw_netif_stop_queue(padapter->pnetdev);
308 #ifdef CONFIG_TX_MCAST2UNI
309 int rtw_mlcst2unicst(_adapter *padapter, struct sk_buff *skb)
311 struct sta_priv *pstapriv = &padapter->stapriv;
312 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
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};
325 _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
326 phead = &pstapriv->asoc_list;
327 plist = get_next(phead);
329 //free sta asoc_queue
330 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
332 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
333 plist = get_next(plist);
335 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
336 if (stainfo_offset_valid(stainfo_offset)) {
337 chk_alive_list[chk_alive_num++] = stainfo_offset;
340 _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
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))
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
354 newskb = rtw_skb_copy(skb);
357 _rtw_memcpy(newskb->data, psta->hwaddr, 6);
358 res = rtw_xmit(padapter, &newskb);
360 DBG_871X("%s()-%d: rtw_xmit() return error!\n", __FUNCTION__, __LINE__);
361 pxmitpriv->tx_drop++;
362 rtw_skb_free(newskb);
364 pxmitpriv->tx_pkts++;
366 DBG_871X("%s-%d: rtw_skb_copy() failed!\n", __FUNCTION__, __LINE__);
367 pxmitpriv->tx_drop++;
369 return _FALSE; // Caller shall tx this multicast frame via normal way.
376 #endif // CONFIG_TX_MCAST2UNI
379 int _rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
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
388 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
394 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
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__);
404 rtw_check_xmit_resource(padapter, pkt);
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)
414 if ( pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4) ) {
415 res = rtw_mlcst2unicst(padapter, pkt);
420 //DBG_871X("Stop M2U(%d, %d)! ", pxmitpriv->free_xmitframe_cnt, pxmitpriv->free_xmitbuf_cnt);
424 #endif // CONFIG_TX_MCAST2UNI
426 res = rtw_xmit(padapter, &pkt);
428 #ifdef DBG_TX_DROP_FRAME
429 DBG_871X("DBG_TX_DROP_FRAME %s rtw_xmit fail\n", __FUNCTION__);
434 pxmitpriv->tx_pkts++;
435 RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
439 pxmitpriv->tx_drop++;
441 RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));
450 int rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
453 rtw_mstat_update(MSTAT_TYPE_SKB, MSTAT_ALLOC_SUCCESS, pkt->truesize);
454 return _rtw_xmit_entry(pkt, pnetdev);