1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
6 #include <linux/version.h>
7 #include <osdep_service.h>
13 #include <mlme_osdep.h>
14 #include <xmit_osdep.h>
15 #include <osdep_intf.h>
16 #include <usb_osintf.h>
18 uint rtw_remainder_len(struct pkt_file *pfile)
20 return pfile->buf_len - ((size_t)(pfile->cur_addr) -
21 (size_t)(pfile->buf_start));
24 void _rtw_open_pktfile(struct sk_buff *pktptr, struct pkt_file *pfile)
28 pr_err("8188eu: pktptr is NULL\n");
32 pr_err("8188eu: pfile is NULL\n");
36 pfile->cur_addr = pktptr->data;
37 pfile->buf_start = pktptr->data;
38 pfile->pkt_len = pktptr->len;
39 pfile->buf_len = pktptr->len;
41 pfile->cur_buffer = pfile->buf_start;
45 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;
61 int rtw_endofpktfile(struct pkt_file *pfile)
64 if (pfile->pkt_len == 0) {
72 void rtw_set_tx_chksum_offload(struct sk_buff *pkt, struct pkt_attrib *pattrib)
76 int rtw_os_xmit_resource_alloc(struct adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz)
80 pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
81 if (!pxmitbuf->pallocated_buf)
84 pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
85 pxmitbuf->dma_transfer_addr = 0;
87 for (i = 0; i < 8; i++) {
88 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
89 if (!pxmitbuf->pxmit_urb[i]) {
90 DBG_88E("pxmitbuf->pxmit_urb[i]==NULL");
97 void rtw_os_xmit_resource_free(struct adapter *padapter,
98 struct xmit_buf *pxmitbuf, u32 free_sz)
102 for (i = 0; i < 8; i++)
103 usb_free_urb(pxmitbuf->pxmit_urb[i]);
105 kfree(pxmitbuf->pallocated_buf);
108 #define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5)
110 void rtw_os_pkt_complete(struct adapter *padapter, struct sk_buff *pkt)
112 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
114 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
116 queue = skb_get_queue_mapping(pkt);
117 if (padapter->registrypriv.wifi_spec) {
118 if (__netif_subqueue_stopped(padapter->pnetdev, queue) &&
119 (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD))
120 netif_wake_subqueue(padapter->pnetdev, queue);
122 if (__netif_subqueue_stopped(padapter->pnetdev, queue))
123 netif_wake_subqueue(padapter->pnetdev, queue);
126 if (netif_queue_stopped(padapter->pnetdev))
127 netif_wake_queue(padapter->pnetdev);
130 dev_kfree_skb_any(pkt);
133 void rtw_os_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe)
136 rtw_os_pkt_complete(padapter, pxframe->pkt);
140 void rtw_os_xmit_schedule(struct adapter *padapter)
142 struct xmit_priv *pxmitpriv;
147 pxmitpriv = &padapter->xmitpriv;
149 spin_lock_bh(&pxmitpriv->lock);
151 if (rtw_txframes_pending(padapter))
152 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
154 spin_unlock_bh(&pxmitpriv->lock);
157 static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt)
159 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
162 queue = skb_get_queue_mapping(pkt);
163 if (padapter->registrypriv.wifi_spec) {
164 /* No free space for Tx, tx_worker is too slow */
165 if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD)
166 netif_stop_subqueue(padapter->pnetdev, queue);
168 if (pxmitpriv->free_xmitframe_cnt <= 4) {
169 if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue)))
170 netif_stop_subqueue(padapter->pnetdev, queue);
175 static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb)
177 struct sta_priv *pstapriv = &padapter->stapriv;
178 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
179 struct list_head *phead, *plist;
180 struct sk_buff *newskb;
181 struct sta_info *psta = NULL;
184 spin_lock_bh(&pstapriv->asoc_list_lock);
185 phead = &pstapriv->asoc_list;
188 /* free sta asoc_queue */
189 while (phead != plist) {
190 psta = container_of(plist, struct sta_info, asoc_list);
194 /* avoid come from STA1 and send back STA1 */
195 if (!memcmp(psta->hwaddr, &skb->data[6], 6))
198 newskb = skb_copy(skb, GFP_ATOMIC);
201 memcpy(newskb->data, psta->hwaddr, 6);
202 res = rtw_xmit(padapter, &newskb);
204 DBG_88E("%s()-%d: rtw_xmit() return error!\n", __func__, __LINE__);
205 pxmitpriv->tx_drop++;
206 dev_kfree_skb_any(newskb);
208 pxmitpriv->tx_pkts++;
211 DBG_88E("%s-%d: skb_copy() failed!\n", __func__, __LINE__);
212 pxmitpriv->tx_drop++;
214 spin_unlock_bh(&pstapriv->asoc_list_lock);
215 return false; /* Caller shall tx this multicast frame via normal way. */
219 spin_unlock_bh(&pstapriv->asoc_list_lock);
220 dev_kfree_skb_any(skb);
224 int rtw_xmit_entry(struct sk_buff *pkt, struct net_device *pnetdev)
226 struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
227 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
228 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
231 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
233 if (rtw_if_up(padapter) == false) {
234 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
238 rtw_check_xmit_resource(padapter, pkt);
240 if (!rtw_mc2u_disable && check_fwstate(pmlmepriv, WIFI_AP_STATE) &&
241 (IP_MCAST_MAC(pkt->data) || ICMPV6_MCAST_MAC(pkt->data)) &&
242 (padapter->registrypriv.wifi_spec == 0)) {
243 if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4)) {
244 res = rtw_mlcst2unicst(padapter, pkt);
250 res = rtw_xmit(padapter, &pkt);
254 pxmitpriv->tx_pkts++;
255 RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
259 pxmitpriv->tx_drop++;
260 dev_kfree_skb_any(pkt);
261 RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));