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)
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 ;
42 uint _rtw_pktfile_read(struct pkt_file *pfile, u8 *rmem, uint rlen)
47 len = rtw_remainder_len(pfile);
48 len = (rlen > len) ? len : rlen;
51 skb_copy_bits(pfile->pkt, pfile->buf_len - pfile->pkt_len, rmem, len);
53 pfile->cur_addr += len;
54 pfile->pkt_len -= len;
60 sint rtw_endofpktfile(struct pkt_file *pfile)
63 if (pfile->pkt_len == 0) {
71 void rtw_set_tx_chksum_offload(_pkt *pkt, struct pkt_attrib *pattrib)
74 #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
75 struct sk_buff *skb = (struct sk_buff *)pkt;
76 pattrib->hw_tcp_csum = 0;
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"); */
89 skb_checksum_help(skb);
91 /* Set UDP checksum = 0 to skip checksum check */
92 struct udphdr *udp = skb_transport_header(skb);
96 RTW_INFO("%s-%d TCP CSUM offload Error!!\n", __FUNCTION__, __LINE__);
97 WARN_ON(1); /* we need a WARN() */
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);
108 int rtw_os_xmit_resource_alloc(_adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz, u8 flag)
111 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
112 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
113 struct usb_device *pusbd = pdvobjpriv->pusbdev;
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)
119 #else /* CONFIG_USE_USB_BUFFER_ALLOC_TX */
121 pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
122 if (pxmitbuf->pallocated_buf == NULL)
125 pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
127 #endif /* CONFIG_USE_USB_BUFFER_ALLOC_TX */
131 #ifdef CONFIG_USB_HCI
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");
146 void rtw_os_xmit_resource_free(_adapter *padapter, struct xmit_buf *pxmitbuf, u32 free_sz, u8 flag)
149 #ifdef CONFIG_USB_HCI
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]);
162 #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
163 struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
164 struct usb_device *pusbd = pdvobjpriv->pusbdev;
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 */
176 void dump_os_queue(void *sel, _adapter *padapter)
178 struct net_device *ndev = padapter->pnetdev;
180 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
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");
188 RTW_PRINT_SEL(sel, "os_queue:%s\n"
189 , netif_queue_stopped(ndev) ? "stopped" : "waked");
193 #define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5)
195 static inline bool rtw_os_need_wake_queue(_adapter *padapter, u16 qidx)
197 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
198 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
200 if (padapter->registrypriv.wifi_spec) {
201 if (pxmitpriv->hwxmits[qidx].accnt < WMM_XMIT_THRESHOLD)
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))
210 #endif /* CONFIG_MCC_MODE */
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))
221 #endif /* CONFIG_MCC_MODE */
226 static inline bool rtw_os_need_stop_queue(_adapter *padapter, u16 qidx)
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)
235 if (pxmitpriv->free_xmitframe_cnt <= 4)
239 if (pxmitpriv->free_xmitframe_cnt <= 4)
245 void rtw_os_pkt_complete(_adapter *padapter, _pkt *pkt)
247 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
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);
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);
267 void rtw_os_xmit_complete(_adapter *padapter, struct xmit_frame *pxframe)
270 rtw_os_pkt_complete(padapter, pxframe->pkt);
275 void rtw_os_xmit_schedule(_adapter *padapter)
277 #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
278 _adapter *pri_adapter = GET_PRIMARY_ADAPTER(padapter);
283 if (_rtw_queue_empty(&padapter->xmitpriv.pending_xmitbuf_queue) == _FALSE)
284 _rtw_up_sema(&pri_adapter->xmitpriv.xmit_sema);
289 struct xmit_priv *pxmitpriv;
294 pxmitpriv = &padapter->xmitpriv;
296 _enter_critical_bh(&pxmitpriv->lock, &irqL);
298 if (rtw_txframes_pending(padapter))
299 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
301 _exit_critical_bh(&pxmitpriv->lock, &irqL);
305 static bool rtw_check_xmit_resource(_adapter *padapter, _pkt *pkt)
308 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
309 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
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);
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);
330 void rtw_os_wake_queue_at_free_stainfo(_adapter *padapter, int *qcnt_freed)
332 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
335 for (i = 0; i < 4; i++) {
336 if (qcnt_freed[i] == 0)
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);
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);
356 #ifdef CONFIG_TX_MCAST2UNI
357 int rtw_mlcst2unicst(_adapter *padapter, struct sk_buff *skb)
359 struct sta_priv *pstapriv = &padapter->stapriv;
360 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
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};
373 DBG_COUNTER(padapter->tx_logs.os_tx_m2u);
375 _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
376 phead = &pstapriv->asoc_list;
377 plist = get_next(phead);
379 /* free sta asoc_queue */
380 while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
382 psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
383 plist = get_next(plist);
385 stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
386 if (stainfo_offset_valid(stainfo_offset))
387 chk_alive_list[chk_alive_num++] = stainfo_offset;
389 _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
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);
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
403 DBG_COUNTER(padapter->tx_logs.os_tx_m2u_ignore_self);
407 DBG_COUNTER(padapter->tx_logs.os_tx_m2u_entry);
409 newskb = rtw_skb_copy(skb);
412 _rtw_memcpy(newskb->data, psta->hwaddr, 6);
413 res = rtw_xmit(padapter, &newskb);
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);
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. */
432 #endif /* CONFIG_TX_MCAST2UNI */
435 int _rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
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 */
444 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
449 if (padapter->registrypriv.mp_mode) {
450 RTW_INFO("MP_TX_DROP_OS_FRAME\n");
453 DBG_COUNTER(padapter->tx_logs.os_tx);
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__);
463 rtw_check_xmit_resource(padapter, pkt);
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)
474 && (padapter->registrypriv.wifi_spec == 0)
476 if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME / 4)) {
477 res = rtw_mlcst2unicst(padapter, pkt);
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);
486 #endif /* CONFIG_TX_MCAST2UNI */
488 res = rtw_xmit(padapter, &pkt);
490 #ifdef DBG_TX_DROP_FRAME
491 RTW_INFO("DBG_TX_DROP_FRAME %s rtw_xmit fail\n", __FUNCTION__);
499 pxmitpriv->tx_drop++;
500 rtw_os_pkt_complete(padapter, pkt);
508 int rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
510 _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev);
511 struct mlme_priv *pmlmepriv = &(padapter->mlmepriv);
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);
521 rtw_mstat_update(MSTAT_TYPE_SKB, MSTAT_ALLOC_SUCCESS, pkt->truesize);
522 ret = _rtw_xmit_entry(pkt, pnetdev);