staging: r8188eu: clean up comparsions to NULL in os_dep directory
[linux-2.6-microblaze.git] / drivers / staging / r8188eu / os_dep / xmit_linux.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2007 - 2012 Realtek Corporation. */
3
4 #define _XMIT_OSDEP_C_
5
6 #include <linux/version.h>
7 #include <osdep_service.h>
8 #include <drv_types.h>
9
10 #include <if_ether.h>
11 #include <ip.h>
12 #include <wifi.h>
13 #include <mlme_osdep.h>
14 #include <xmit_osdep.h>
15 #include <osdep_intf.h>
16 #include <usb_osintf.h>
17
18 uint rtw_remainder_len(struct pkt_file *pfile)
19 {
20         return pfile->buf_len - ((size_t)(pfile->cur_addr) -
21                (size_t)(pfile->buf_start));
22 }
23
24 void _rtw_open_pktfile(struct sk_buff *pktptr, struct pkt_file *pfile)
25 {
26
27         if (!pktptr) {
28                 pr_err("8188eu: pktptr is NULL\n");
29                 return;
30         }
31         if (!pfile) {
32                 pr_err("8188eu: pfile is NULL\n");
33                 return;
34         }
35         pfile->pkt = pktptr;
36         pfile->cur_addr = pktptr->data;
37         pfile->buf_start = pktptr->data;
38         pfile->pkt_len = pktptr->len;
39         pfile->buf_len = pktptr->len;
40
41         pfile->cur_buffer = pfile->buf_start;
42
43 }
44
45 uint _rtw_pktfile_read (struct pkt_file *pfile, u8 *rmem, uint rlen)
46 {
47         uint    len = 0;
48
49         len =  rtw_remainder_len(pfile);
50         len = (rlen > len) ? len : rlen;
51
52         if (rmem)
53                 skb_copy_bits(pfile->pkt, pfile->buf_len-pfile->pkt_len, rmem, len);
54
55         pfile->cur_addr += len;
56         pfile->pkt_len -= len;
57
58         return len;
59 }
60
61 int rtw_endofpktfile(struct pkt_file *pfile)
62 {
63
64         if (pfile->pkt_len == 0) {
65
66                 return true;
67         }
68
69         return false;
70 }
71
72 void rtw_set_tx_chksum_offload(struct sk_buff *pkt, struct pkt_attrib *pattrib)
73 {
74 }
75
76 int rtw_os_xmit_resource_alloc(struct adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz)
77 {
78         int i;
79
80         pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
81         if (!pxmitbuf->pallocated_buf)
82                 return _FAIL;
83
84         pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((size_t)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
85         pxmitbuf->dma_transfer_addr = 0;
86
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");
91                         return _FAIL;
92                 }
93         }
94         return _SUCCESS;
95 }
96
97 void rtw_os_xmit_resource_free(struct adapter *padapter,
98                                struct xmit_buf *pxmitbuf, u32 free_sz)
99 {
100         int i;
101
102         for (i = 0; i < 8; i++)
103                 usb_free_urb(pxmitbuf->pxmit_urb[i]);
104
105         kfree(pxmitbuf->pallocated_buf);
106 }
107
108 #define WMM_XMIT_THRESHOLD      (NR_XMITFRAME*2/5)
109
110 void rtw_os_pkt_complete(struct adapter *padapter, struct sk_buff *pkt)
111 {
112 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 35))
113         u16     queue;
114         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
115
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);
121         } else {
122                 if (__netif_subqueue_stopped(padapter->pnetdev, queue))
123                         netif_wake_subqueue(padapter->pnetdev, queue);
124         }
125 #else
126         if (netif_queue_stopped(padapter->pnetdev))
127                 netif_wake_queue(padapter->pnetdev);
128 #endif
129
130         dev_kfree_skb_any(pkt);
131 }
132
133 void rtw_os_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe)
134 {
135         if (pxframe->pkt)
136                 rtw_os_pkt_complete(padapter, pxframe->pkt);
137         pxframe->pkt = NULL;
138 }
139
140 void rtw_os_xmit_schedule(struct adapter *padapter)
141 {
142         struct xmit_priv *pxmitpriv;
143
144         if (!padapter)
145                 return;
146
147         pxmitpriv = &padapter->xmitpriv;
148
149         spin_lock_bh(&pxmitpriv->lock);
150
151         if (rtw_txframes_pending(padapter))
152                 tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
153
154         spin_unlock_bh(&pxmitpriv->lock);
155 }
156
157 static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt)
158 {
159         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
160         u16     queue;
161
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);
167         } else {
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);
171                 }
172         }
173 }
174
175 static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb)
176 {
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;
182         s32     res;
183
184         spin_lock_bh(&pstapriv->asoc_list_lock);
185         phead = &pstapriv->asoc_list;
186         plist = phead->next;
187
188         /* free sta asoc_queue */
189         while (phead != plist) {
190                 psta = container_of(plist, struct sta_info, asoc_list);
191
192                 plist = plist->next;
193
194                 /* avoid   come from STA1 and send back STA1 */
195                 if (!memcmp(psta->hwaddr, &skb->data[6], 6))
196                         continue;
197
198                 newskb = skb_copy(skb, GFP_ATOMIC);
199
200                 if (newskb) {
201                         memcpy(newskb->data, psta->hwaddr, 6);
202                         res = rtw_xmit(padapter, &newskb);
203                         if (res < 0) {
204                                 DBG_88E("%s()-%d: rtw_xmit() return error!\n", __func__, __LINE__);
205                                 pxmitpriv->tx_drop++;
206                                 dev_kfree_skb_any(newskb);
207                         } else {
208                                 pxmitpriv->tx_pkts++;
209                         }
210                 } else {
211                         DBG_88E("%s-%d: skb_copy() failed!\n", __func__, __LINE__);
212                         pxmitpriv->tx_drop++;
213
214                         spin_unlock_bh(&pstapriv->asoc_list_lock);
215                         return false;   /*  Caller shall tx this multicast frame via normal way. */
216                 }
217         }
218
219         spin_unlock_bh(&pstapriv->asoc_list_lock);
220         dev_kfree_skb_any(skb);
221         return true;
222 }
223
224 int rtw_xmit_entry(struct sk_buff *pkt, struct  net_device *pnetdev)
225 {
226         struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev);
227         struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
228         struct mlme_priv        *pmlmepriv = &padapter->mlmepriv;
229         s32 res = 0;
230
231         RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
232
233         if (rtw_if_up(padapter) == false) {
234                 RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
235                 goto drop_packet;
236         }
237
238         rtw_check_xmit_resource(padapter, pkt);
239
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);
245                         if (res)
246                                 goto exit;
247                 }
248         }
249
250         res = rtw_xmit(padapter, &pkt);
251         if (res < 0)
252                 goto drop_packet;
253
254         pxmitpriv->tx_pkts++;
255         RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
256         goto exit;
257
258 drop_packet:
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));
262
263 exit:
264
265         return 0;
266 }