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 <osdep_service.h>
23 #include <drv_types.h>
26 #include <mlme_osdep.h>
27 #include <xmit_osdep.h>
28 #include <osdep_intf.h>
30 uint rtw_remainder_len(struct pkt_file *pfile)
32 return pfile->buf_len - ((size_t)(pfile->cur_addr) -
33 (size_t)(pfile->buf_start));
36 void _rtw_open_pktfile(struct sk_buff *pktptr, struct pkt_file *pfile)
40 pfile->cur_addr = pktptr->data;
41 pfile->buf_start = pktptr->data;
42 pfile->pkt_len = pktptr->len;
43 pfile->buf_len = pktptr->len;
45 pfile->cur_buffer = pfile->buf_start;
49 uint _rtw_pktfile_read (struct pkt_file *pfile, u8 *rmem, uint rlen)
54 len = rtw_remainder_len(pfile);
55 len = (rlen > len) ? len : rlen;
58 skb_copy_bits(pfile->pkt, pfile->buf_len-pfile->pkt_len, rmem, len);
60 pfile->cur_addr += len;
61 pfile->pkt_len -= len;
67 int rtw_endofpktfile(struct pkt_file *pfile)
70 if (pfile->pkt_len == 0) {
78 int rtw_os_xmit_resource_alloc(struct adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz)
82 pxmitbuf->pallocated_buf = kzalloc(alloc_sz, GFP_KERNEL);
83 if (pxmitbuf->pallocated_buf == NULL)
86 pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
87 pxmitbuf->dma_transfer_addr = 0;
89 for (i = 0; i < 8; i++) {
90 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
91 if (pxmitbuf->pxmit_urb[i] == NULL) {
92 DBG_88E("pxmitbuf->pxmit_urb[i]==NULL");
99 void rtw_os_xmit_resource_free(struct adapter *padapter,
100 struct xmit_buf *pxmitbuf, u32 free_sz)
104 for (i = 0; i < 8; i++)
105 usb_free_urb(pxmitbuf->pxmit_urb[i]);
107 kfree(pxmitbuf->pallocated_buf);
110 #define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5)
112 void rtw_os_pkt_complete(struct adapter *padapter, struct sk_buff *pkt)
115 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
117 queue = skb_get_queue_mapping(pkt);
118 if (padapter->registrypriv.wifi_spec) {
119 if (__netif_subqueue_stopped(padapter->pnetdev, queue) &&
120 (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD))
121 netif_wake_subqueue(padapter->pnetdev, queue);
123 if (__netif_subqueue_stopped(padapter->pnetdev, queue))
124 netif_wake_subqueue(padapter->pnetdev, queue);
127 dev_kfree_skb_any(pkt);
130 void rtw_os_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe)
133 rtw_os_pkt_complete(padapter, pxframe->pkt);
137 void rtw_os_xmit_schedule(struct adapter *padapter)
139 struct xmit_priv *pxmitpriv;
144 pxmitpriv = &padapter->xmitpriv;
146 spin_lock_bh(&pxmitpriv->lock);
148 if (rtw_txframes_pending(padapter))
149 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
151 spin_unlock_bh(&pxmitpriv->lock);
154 static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt)
156 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
159 queue = skb_get_queue_mapping(pkt);
160 if (padapter->registrypriv.wifi_spec) {
161 /* No free space for Tx, tx_worker is too slow */
162 if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD)
163 netif_stop_subqueue(padapter->pnetdev, queue);
165 if (pxmitpriv->free_xmitframe_cnt <= 4) {
166 if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue)))
167 netif_stop_subqueue(padapter->pnetdev, queue);
172 static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb)
174 struct sta_priv *pstapriv = &padapter->stapriv;
175 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
176 struct list_head *phead, *plist;
177 struct sk_buff *newskb;
178 struct sta_info *psta = NULL;
181 spin_lock_bh(&pstapriv->asoc_list_lock);
182 phead = &pstapriv->asoc_list;
185 /* free sta asoc_queue */
186 while (phead != plist) {
187 psta = container_of(plist, struct sta_info, asoc_list);
191 /* avoid come from STA1 and send back STA1 */
192 if (!memcmp(psta->hwaddr, &skb->data[6], 6))
195 newskb = skb_copy(skb, GFP_ATOMIC);
198 memcpy(newskb->data, psta->hwaddr, 6);
199 res = rtw_xmit(padapter, &newskb);
201 DBG_88E("%s()-%d: rtw_xmit() return error!\n", __func__, __LINE__);
202 pxmitpriv->tx_drop++;
203 dev_kfree_skb_any(newskb);
205 pxmitpriv->tx_pkts++;
208 DBG_88E("%s-%d: skb_copy() failed!\n", __func__, __LINE__);
209 pxmitpriv->tx_drop++;
211 spin_unlock_bh(&pstapriv->asoc_list_lock);
212 return false; /* Caller shall tx this multicast frame via normal way. */
216 spin_unlock_bh(&pstapriv->asoc_list_lock);
217 dev_kfree_skb_any(skb);
222 int rtw_xmit_entry(struct sk_buff *pkt, struct net_device *pnetdev)
224 struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
225 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
226 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
230 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
232 if (rtw_if_up(padapter) == false) {
233 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
237 rtw_check_xmit_resource(padapter, pkt);
239 if (!rtw_mc2u_disable && check_fwstate(pmlmepriv, WIFI_AP_STATE) &&
240 (IP_MCAST_MAC(pkt->data) || ICMPV6_MCAST_MAC(pkt->data)) &&
241 (padapter->registrypriv.wifi_spec == 0)) {
242 if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4)) {
243 res = rtw_mlcst2unicst(padapter, pkt);
249 res = rtw_xmit(padapter, &pkt);
253 pxmitpriv->tx_pkts++;
254 RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
258 pxmitpriv->tx_drop++;
259 dev_kfree_skb_any(pkt);
260 RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));