blob: 1593e280e06038a1e3776810f2689857f6844267 [file] [log] [blame]
Larry Fingerc2478d32013-08-21 22:34:02 -05001/******************************************************************************
2 *
3 * Copyright(c) 2007 - 2012 Realtek Corporation. All rights reserved.
4 *
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.
8 *
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
12 * more details.
13 *
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
17 *
18 *
19 ******************************************************************************/
20#define _XMIT_OSDEP_C_
21
Larry Fingerc2478d32013-08-21 22:34:02 -050022#include <osdep_service.h>
23#include <drv_types.h>
24
Larry Fingerc2478d32013-08-21 22:34:02 -050025#include <wifi.h>
26#include <mlme_osdep.h>
27#include <xmit_osdep.h>
28#include <osdep_intf.h>
Larry Fingerc2478d32013-08-21 22:34:02 -050029
30uint rtw_remainder_len(struct pkt_file *pfile)
31{
32 return pfile->buf_len - ((size_t)(pfile->cur_addr) -
33 (size_t)(pfile->buf_start));
34}
35
36void _rtw_open_pktfile(struct sk_buff *pktptr, struct pkt_file *pfile)
37{
Larry Fingerc2478d32013-08-21 22:34:02 -050038
39 pfile->pkt = pktptr;
40 pfile->cur_addr = pktptr->data;
41 pfile->buf_start = pktptr->data;
42 pfile->pkt_len = pktptr->len;
43 pfile->buf_len = pktptr->len;
44
45 pfile->cur_buffer = pfile->buf_start;
46
Larry Fingerc2478d32013-08-21 22:34:02 -050047}
48
Jia He7be921a22014-11-04 09:39:58 +080049uint _rtw_pktfile_read(struct pkt_file *pfile, u8 *rmem, uint rlen)
Larry Fingerc2478d32013-08-21 22:34:02 -050050{
51 uint len = 0;
52
Larry Fingerc2478d32013-08-21 22:34:02 -050053
54 len = rtw_remainder_len(pfile);
Ivan Safonov20e76532015-10-27 22:16:25 +070055 len = min(rlen, len);
Larry Fingerc2478d32013-08-21 22:34:02 -050056
57 if (rmem)
58 skb_copy_bits(pfile->pkt, pfile->buf_len-pfile->pkt_len, rmem, len);
59
60 pfile->cur_addr += len;
61 pfile->pkt_len -= len;
62
Larry Fingerc2478d32013-08-21 22:34:02 -050063
64 return len;
65}
66
67int rtw_endofpktfile(struct pkt_file *pfile)
68{
Krzysztof Konopko31d4cf12014-11-10 18:54:22 +000069 return pfile->pkt_len == 0;
Larry Fingerc2478d32013-08-21 22:34:02 -050070}
71
Larry Fingerc2478d32013-08-21 22:34:02 -050072int rtw_os_xmit_resource_alloc(struct adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz)
73{
74 int i;
75
navin patidarfadbe0c2014-06-22 14:06:23 +053076 pxmitbuf->pallocated_buf = kzalloc(alloc_sz, GFP_KERNEL);
Larry Fingerc2478d32013-08-21 22:34:02 -050077 if (pxmitbuf->pallocated_buf == NULL)
78 return _FAIL;
79
80 pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
81 pxmitbuf->dma_transfer_addr = 0;
82
83 for (i = 0; i < 8; i++) {
84 pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
85 if (pxmitbuf->pxmit_urb[i] == NULL) {
86 DBG_88E("pxmitbuf->pxmit_urb[i]==NULL");
87 return _FAIL;
88 }
89 }
90 return _SUCCESS;
91}
92
93void rtw_os_xmit_resource_free(struct adapter *padapter,
94 struct xmit_buf *pxmitbuf, u32 free_sz)
95{
96 int i;
97
98 for (i = 0; i < 8; i++)
99 usb_free_urb(pxmitbuf->pxmit_urb[i]);
100
101 kfree(pxmitbuf->pallocated_buf);
102}
103
104#define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5)
105
106void rtw_os_pkt_complete(struct adapter *padapter, struct sk_buff *pkt)
107{
Larry Fingerc2478d32013-08-21 22:34:02 -0500108 u16 queue;
109 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
110
111 queue = skb_get_queue_mapping(pkt);
112 if (padapter->registrypriv.wifi_spec) {
113 if (__netif_subqueue_stopped(padapter->pnetdev, queue) &&
114 (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD))
115 netif_wake_subqueue(padapter->pnetdev, queue);
116 } else {
117 if (__netif_subqueue_stopped(padapter->pnetdev, queue))
118 netif_wake_subqueue(padapter->pnetdev, queue);
119 }
Larry Fingerc2478d32013-08-21 22:34:02 -0500120
121 dev_kfree_skb_any(pkt);
122}
123
124void rtw_os_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe)
125{
126 if (pxframe->pkt)
127 rtw_os_pkt_complete(padapter, pxframe->pkt);
128 pxframe->pkt = NULL;
129}
130
131void rtw_os_xmit_schedule(struct adapter *padapter)
132{
Larry Fingerc2478d32013-08-21 22:34:02 -0500133 struct xmit_priv *pxmitpriv;
134
135 if (!padapter)
136 return;
137
138 pxmitpriv = &padapter->xmitpriv;
139
Larry Finger7057dcb2013-12-19 22:38:34 -0600140 spin_lock_bh(&pxmitpriv->lock);
Larry Fingerc2478d32013-08-21 22:34:02 -0500141
142 if (rtw_txframes_pending(padapter))
143 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
144
Larry Fingere02bcf62013-12-19 22:38:35 -0600145 spin_unlock_bh(&pxmitpriv->lock);
Larry Fingerc2478d32013-08-21 22:34:02 -0500146}
147
148static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt)
149{
150 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
151 u16 queue;
152
153 queue = skb_get_queue_mapping(pkt);
154 if (padapter->registrypriv.wifi_spec) {
155 /* No free space for Tx, tx_worker is too slow */
156 if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD)
157 netif_stop_subqueue(padapter->pnetdev, queue);
158 } else {
159 if (pxmitpriv->free_xmitframe_cnt <= 4) {
160 if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue)))
161 netif_stop_subqueue(padapter->pnetdev, queue);
162 }
163 }
164}
165
166static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb)
167{
168 struct sta_priv *pstapriv = &padapter->stapriv;
169 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
Larry Fingerc2478d32013-08-21 22:34:02 -0500170 struct list_head *phead, *plist;
171 struct sk_buff *newskb;
172 struct sta_info *psta = NULL;
173 s32 res;
174
Larry Finger7057dcb2013-12-19 22:38:34 -0600175 spin_lock_bh(&pstapriv->asoc_list_lock);
Larry Fingerc2478d32013-08-21 22:34:02 -0500176 phead = &pstapriv->asoc_list;
Larry Fingerc44e5e32014-02-09 15:15:58 -0600177 plist = phead->next;
Larry Fingerc2478d32013-08-21 22:34:02 -0500178
179 /* free sta asoc_queue */
navin patidar84660702014-06-22 13:49:32 +0530180 while (phead != plist) {
Larry Fingerbea88102014-02-09 15:15:57 -0600181 psta = container_of(plist, struct sta_info, asoc_list);
Larry Fingerc2478d32013-08-21 22:34:02 -0500182
Larry Fingerc44e5e32014-02-09 15:15:58 -0600183 plist = plist->next;
Larry Fingerc2478d32013-08-21 22:34:02 -0500184
185 /* avoid come from STA1 and send back STA1 */
186 if (!memcmp(psta->hwaddr, &skb->data[6], 6))
187 continue;
188
189 newskb = skb_copy(skb, GFP_ATOMIC);
190
191 if (newskb) {
192 memcpy(newskb->data, psta->hwaddr, 6);
193 res = rtw_xmit(padapter, &newskb);
194 if (res < 0) {
195 DBG_88E("%s()-%d: rtw_xmit() return error!\n", __func__, __LINE__);
196 pxmitpriv->tx_drop++;
197 dev_kfree_skb_any(newskb);
198 } else {
199 pxmitpriv->tx_pkts++;
200 }
201 } else {
202 DBG_88E("%s-%d: skb_copy() failed!\n", __func__, __LINE__);
203 pxmitpriv->tx_drop++;
204
Larry Fingere02bcf62013-12-19 22:38:35 -0600205 spin_unlock_bh(&pstapriv->asoc_list_lock);
Larry Fingerc2478d32013-08-21 22:34:02 -0500206 return false; /* Caller shall tx this multicast frame via normal way. */
207 }
208 }
209
Larry Fingere02bcf62013-12-19 22:38:35 -0600210 spin_unlock_bh(&pstapriv->asoc_list_lock);
Larry Fingerc2478d32013-08-21 22:34:02 -0500211 dev_kfree_skb_any(skb);
212 return true;
213}
214
215
216int rtw_xmit_entry(struct sk_buff *pkt, struct net_device *pnetdev)
217{
218 struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
219 struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
220 struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
221 s32 res = 0;
222
Larry Fingerc2478d32013-08-21 22:34:02 -0500223
224 RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
225
226 if (rtw_if_up(padapter) == false) {
227 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
228 goto drop_packet;
229 }
230
231 rtw_check_xmit_resource(padapter, pkt);
232
233 if (!rtw_mc2u_disable && check_fwstate(pmlmepriv, WIFI_AP_STATE) &&
234 (IP_MCAST_MAC(pkt->data) || ICMPV6_MCAST_MAC(pkt->data)) &&
235 (padapter->registrypriv.wifi_spec == 0)) {
236 if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4)) {
237 res = rtw_mlcst2unicst(padapter, pkt);
238 if (res)
239 goto exit;
240 }
241 }
242
243 res = rtw_xmit(padapter, &pkt);
244 if (res < 0)
245 goto drop_packet;
246
247 pxmitpriv->tx_pkts++;
248 RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
249 goto exit;
250
251drop_packet:
252 pxmitpriv->tx_drop++;
253 dev_kfree_skb_any(pkt);
254 RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));
255
256exit:
257
Larry Fingerc2478d32013-08-21 22:34:02 -0500258
259 return 0;
260}