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>
25 uint rtw_remainder_len(struct pkt_file *pfile)
27 return (pfile->buf_len - ((SIZE_PTR)(pfile->cur_addr) - (SIZE_PTR)(pfile->buf_start)));
30 void _rtw_open_pktfile (_pkt *pktptr, struct pkt_file *pfile)
35 pfile->cur_addr = pfile->buf_start = pktptr->data;
36 pfile->pkt_len = pfile->buf_len = pktptr->len;
38 pfile->cur_buffer = pfile->buf_start ;
43 uint _rtw_pktfile_read (struct pkt_file *pfile, u8 *rmem, uint rlen)
49 len = rtw_remainder_len(pfile);
50 len = (rlen > len)? len: rlen;
53 skb_copy_bits(pfile->pkt, pfile->buf_len-pfile->pkt_len, rmem, len);
55 pfile->cur_addr += len;
56 pfile->pkt_len -= len;
63 sint rtw_endofpktfile(struct pkt_file *pfile)
67 if (pfile->pkt_len == 0) {
77 void rtw_set_tx_chksum_offload(_pkt *pkt, struct pkt_attrib *pattrib)
80 #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
81 struct sk_buff *skb = (struct sk_buff *)pkt;
82 pattrib->hw_tcp_csum = 0;
84 if (skb->ip_summed == CHECKSUM_PARTIAL) {
85 if (skb_shinfo(skb)->nr_frags == 0)
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");
96 skb_checksum_help(skb);
98 // Set UDP checksum = 0 to skip checksum check
99 struct udphdr *udp = skb_transport_header(skb);
103 DBG_871X("%s-%d TCP CSUM offload Error!!\n", __FUNCTION__, __LINE__);
104 WARN_ON(1); /* we need a WARN() */
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);
116 int rtw_os_xmit_resource_alloc(_adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz, u8 flag)
119 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
120 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
121 struct usb_device *pusbd = pdvobjpriv->pusbdev;
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)
127 #else // CONFIG_USE_USB_BUFFER_ALLOC_TX
129 pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
130 if (pxmitbuf->pallocated_buf == NULL)
135 pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
137 #endif // CONFIG_USE_USB_BUFFER_ALLOC_TX
141 #ifdef CONFIG_USB_HCI
145 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
146 if(pxmitbuf->pxmit_urb[i] == NULL)
148 DBG_871X("pxmitbuf->pxmit_urb[i]==NULL");
158 void rtw_os_xmit_resource_free(_adapter *padapter, struct xmit_buf *pxmitbuf,u32 free_sz, u8 flag)
161 #ifdef CONFIG_USB_HCI
166 if(pxmitbuf->pxmit_urb[i])
168 //usb_kill_urb(pxmitbuf->pxmit_urb[i]);
169 usb_free_urb(pxmitbuf->pxmit_urb[i]);
176 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
177 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
178 struct usb_device *pusbd = pdvobjpriv->pusbdev;
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
190 #define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5)
192 void rtw_os_pkt_complete(_adapter *padapter, _pkt *pkt)
194 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
196 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
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))
203 netif_wake_subqueue(padapter->pnetdev, queue);
206 if(__netif_subqueue_stopped(padapter->pnetdev, queue))
207 netif_wake_subqueue(padapter->pnetdev, queue);
210 if (netif_queue_stopped(padapter->pnetdev))
211 netif_wake_queue(padapter->pnetdev);
217 void rtw_os_xmit_complete(_adapter *padapter, struct xmit_frame *pxframe)
220 rtw_os_pkt_complete(padapter, pxframe->pkt);
225 void rtw_os_xmit_schedule(_adapter *padapter)
227 _adapter *pri_adapter = padapter;
229 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
233 #ifdef CONFIG_CONCURRENT_MODE
234 if(padapter->adapter_type > PRIMARY_ADAPTER)
235 pri_adapter = padapter->pbuddy_adapter;
238 if (_rtw_queue_empty(&padapter->xmitpriv.pending_xmitbuf_queue) == _FALSE)
239 _rtw_up_sema(&pri_adapter->xmitpriv.xmit_sema);
244 struct xmit_priv *pxmitpriv;
249 pxmitpriv = &padapter->xmitpriv;
251 _enter_critical_bh(&pxmitpriv->lock, &irqL);
253 if(rtw_txframes_pending(padapter))
255 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
258 _exit_critical_bh(&pxmitpriv->lock, &irqL);
262 static void rtw_check_xmit_resource(_adapter *padapter, _pkt *pkt)
264 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
265 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
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);
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);
282 if(pxmitpriv->free_xmitframe_cnt<=4)
284 if (!rtw_netif_queue_stopped(padapter->pnetdev))
285 rtw_netif_stop_queue(padapter->pnetdev);
290 #ifdef CONFIG_TX_MCAST2UNI
291 int rtw_mlcst2unicst(_adapter *padapter, struct sk_buff *skb)
293 struct sta_priv *pstapriv = &padapter->stapriv;
294 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
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};
307 _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
308 phead = &pstapriv->asoc_list;
309 plist = get_next(phead);
311 //free sta asoc_queue
312 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
314 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
315 plist = get_next(plist);
317 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
318 if (stainfo_offset_valid(stainfo_offset)) {
319 chk_alive_list[chk_alive_num++] = stainfo_offset;
322 _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
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))
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
336 newskb = rtw_skb_copy(skb);
339 _rtw_memcpy(newskb->data, psta->hwaddr, 6);
340 res = rtw_xmit(padapter, &newskb);
342 DBG_871X("%s()-%d: rtw_xmit() return error!\n", __FUNCTION__, __LINE__);
343 pxmitpriv->tx_drop++;
344 rtw_skb_free(newskb);
347 DBG_871X("%s-%d: rtw_skb_copy() failed!\n", __FUNCTION__, __LINE__);
348 pxmitpriv->tx_drop++;
350 return _FALSE; // Caller shall tx this multicast frame via normal way.
357 #endif // CONFIG_TX_MCAST2UNI
360 int _rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
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
369 #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
375 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
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__);
385 rtw_check_xmit_resource(padapter, pkt);
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)
395 if ( pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4) ) {
396 res = rtw_mlcst2unicst(padapter, pkt);
401 //DBG_871X("Stop M2U(%d, %d)! ", pxmitpriv->free_xmitframe_cnt, pxmitpriv->free_xmitbuf_cnt);
405 #endif // CONFIG_TX_MCAST2UNI
407 res = rtw_xmit(padapter, &pkt);
409 #ifdef DBG_TX_DROP_FRAME
410 DBG_871X("DBG_TX_DROP_FRAME %s rtw_xmit fail\n", __FUNCTION__);
415 RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
419 pxmitpriv->tx_drop++;
421 RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));
430 int rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
435 rtw_mstat_update(MSTAT_TYPE_SKB, MSTAT_ALLOC_SUCCESS, pkt->truesize);
436 ret = _rtw_xmit_entry(pkt, pnetdev);