Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 1 | /****************************************************************************** |
| 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 | * |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 14 | ******************************************************************************/ |
| 15 | #define _XMIT_OSDEP_C_ |
| 16 | |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 17 | #include <osdep_service.h> |
| 18 | #include <drv_types.h> |
| 19 | |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 20 | #include <wifi.h> |
| 21 | #include <mlme_osdep.h> |
| 22 | #include <xmit_osdep.h> |
| 23 | #include <osdep_intf.h> |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 24 | |
| 25 | uint rtw_remainder_len(struct pkt_file *pfile) |
| 26 | { |
| 27 | return pfile->buf_len - ((size_t)(pfile->cur_addr) - |
| 28 | (size_t)(pfile->buf_start)); |
| 29 | } |
| 30 | |
| 31 | void _rtw_open_pktfile(struct sk_buff *pktptr, struct pkt_file *pfile) |
| 32 | { |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 33 | |
| 34 | pfile->pkt = pktptr; |
| 35 | pfile->cur_addr = pktptr->data; |
| 36 | pfile->buf_start = pktptr->data; |
| 37 | pfile->pkt_len = pktptr->len; |
| 38 | pfile->buf_len = pktptr->len; |
| 39 | |
| 40 | pfile->cur_buffer = pfile->buf_start; |
| 41 | |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Jia He | 7be921a2 | 2014-11-04 09:39:58 +0800 | [diff] [blame] | 44 | uint _rtw_pktfile_read(struct pkt_file *pfile, u8 *rmem, uint rlen) |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 45 | { |
| 46 | uint len = 0; |
| 47 | |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 48 | |
| 49 | len = rtw_remainder_len(pfile); |
Ivan Safonov | 20e7653 | 2015-10-27 22:16:25 +0700 | [diff] [blame] | 50 | len = min(rlen, len); |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 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 | |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 58 | |
| 59 | return len; |
| 60 | } |
| 61 | |
| 62 | int rtw_endofpktfile(struct pkt_file *pfile) |
| 63 | { |
Krzysztof Konopko | 31d4cf1 | 2014-11-10 18:54:22 +0000 | [diff] [blame] | 64 | return pfile->pkt_len == 0; |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 65 | } |
| 66 | |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 67 | int rtw_os_xmit_resource_alloc(struct adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz) |
| 68 | { |
| 69 | int i; |
| 70 | |
navin patidar | fadbe0c | 2014-06-22 14:06:23 +0530 | [diff] [blame] | 71 | pxmitbuf->pallocated_buf = kzalloc(alloc_sz, GFP_KERNEL); |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 72 | if (pxmitbuf->pallocated_buf == NULL) |
| 73 | return _FAIL; |
| 74 | |
Ivan Safonov | fb11340 | 2016-09-19 00:25:40 +0700 | [diff] [blame] | 75 | pxmitbuf->pbuf = PTR_ALIGN(pxmitbuf->pallocated_buf, XMITBUF_ALIGN_SZ); |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 76 | pxmitbuf->dma_transfer_addr = 0; |
| 77 | |
| 78 | for (i = 0; i < 8; i++) { |
| 79 | pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL); |
| 80 | if (pxmitbuf->pxmit_urb[i] == NULL) { |
| 81 | DBG_88E("pxmitbuf->pxmit_urb[i]==NULL"); |
| 82 | return _FAIL; |
| 83 | } |
| 84 | } |
| 85 | return _SUCCESS; |
| 86 | } |
| 87 | |
| 88 | void rtw_os_xmit_resource_free(struct adapter *padapter, |
| 89 | struct xmit_buf *pxmitbuf, u32 free_sz) |
| 90 | { |
| 91 | int i; |
| 92 | |
| 93 | for (i = 0; i < 8; i++) |
| 94 | usb_free_urb(pxmitbuf->pxmit_urb[i]); |
| 95 | |
| 96 | kfree(pxmitbuf->pallocated_buf); |
| 97 | } |
| 98 | |
| 99 | #define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5) |
| 100 | |
| 101 | void rtw_os_pkt_complete(struct adapter *padapter, struct sk_buff *pkt) |
| 102 | { |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 103 | u16 queue; |
| 104 | struct xmit_priv *pxmitpriv = &padapter->xmitpriv; |
| 105 | |
| 106 | queue = skb_get_queue_mapping(pkt); |
| 107 | if (padapter->registrypriv.wifi_spec) { |
| 108 | if (__netif_subqueue_stopped(padapter->pnetdev, queue) && |
| 109 | (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD)) |
| 110 | netif_wake_subqueue(padapter->pnetdev, queue); |
| 111 | } else { |
| 112 | if (__netif_subqueue_stopped(padapter->pnetdev, queue)) |
| 113 | netif_wake_subqueue(padapter->pnetdev, queue); |
| 114 | } |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 115 | |
| 116 | dev_kfree_skb_any(pkt); |
| 117 | } |
| 118 | |
| 119 | void rtw_os_xmit_complete(struct adapter *padapter, struct xmit_frame *pxframe) |
| 120 | { |
| 121 | if (pxframe->pkt) |
| 122 | rtw_os_pkt_complete(padapter, pxframe->pkt); |
| 123 | pxframe->pkt = NULL; |
| 124 | } |
| 125 | |
| 126 | void rtw_os_xmit_schedule(struct adapter *padapter) |
| 127 | { |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 128 | struct xmit_priv *pxmitpriv; |
| 129 | |
| 130 | if (!padapter) |
| 131 | return; |
| 132 | |
| 133 | pxmitpriv = &padapter->xmitpriv; |
| 134 | |
Larry Finger | 7057dcb | 2013-12-19 22:38:34 -0600 | [diff] [blame] | 135 | spin_lock_bh(&pxmitpriv->lock); |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 136 | |
| 137 | if (rtw_txframes_pending(padapter)) |
| 138 | tasklet_hi_schedule(&pxmitpriv->xmit_tasklet); |
| 139 | |
Larry Finger | e02bcf6 | 2013-12-19 22:38:35 -0600 | [diff] [blame] | 140 | spin_unlock_bh(&pxmitpriv->lock); |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void rtw_check_xmit_resource(struct adapter *padapter, struct sk_buff *pkt) |
| 144 | { |
| 145 | struct xmit_priv *pxmitpriv = &padapter->xmitpriv; |
| 146 | u16 queue; |
| 147 | |
| 148 | queue = skb_get_queue_mapping(pkt); |
| 149 | if (padapter->registrypriv.wifi_spec) { |
| 150 | /* No free space for Tx, tx_worker is too slow */ |
| 151 | if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD) |
| 152 | netif_stop_subqueue(padapter->pnetdev, queue); |
| 153 | } else { |
| 154 | if (pxmitpriv->free_xmitframe_cnt <= 4) { |
| 155 | if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue))) |
| 156 | netif_stop_subqueue(padapter->pnetdev, queue); |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static int rtw_mlcst2unicst(struct adapter *padapter, struct sk_buff *skb) |
| 162 | { |
| 163 | struct sta_priv *pstapriv = &padapter->stapriv; |
| 164 | struct xmit_priv *pxmitpriv = &padapter->xmitpriv; |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 165 | struct list_head *phead, *plist; |
| 166 | struct sk_buff *newskb; |
| 167 | struct sta_info *psta = NULL; |
| 168 | s32 res; |
| 169 | |
Larry Finger | 7057dcb | 2013-12-19 22:38:34 -0600 | [diff] [blame] | 170 | spin_lock_bh(&pstapriv->asoc_list_lock); |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 171 | phead = &pstapriv->asoc_list; |
Larry Finger | c44e5e3 | 2014-02-09 15:15:58 -0600 | [diff] [blame] | 172 | plist = phead->next; |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 173 | |
| 174 | /* free sta asoc_queue */ |
navin patidar | 8466070 | 2014-06-22 13:49:32 +0530 | [diff] [blame] | 175 | while (phead != plist) { |
Larry Finger | bea8810 | 2014-02-09 15:15:57 -0600 | [diff] [blame] | 176 | psta = container_of(plist, struct sta_info, asoc_list); |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 177 | |
Larry Finger | c44e5e3 | 2014-02-09 15:15:58 -0600 | [diff] [blame] | 178 | plist = plist->next; |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 179 | |
| 180 | /* avoid come from STA1 and send back STA1 */ |
| 181 | if (!memcmp(psta->hwaddr, &skb->data[6], 6)) |
| 182 | continue; |
| 183 | |
| 184 | newskb = skb_copy(skb, GFP_ATOMIC); |
| 185 | |
| 186 | if (newskb) { |
| 187 | memcpy(newskb->data, psta->hwaddr, 6); |
| 188 | res = rtw_xmit(padapter, &newskb); |
| 189 | if (res < 0) { |
| 190 | DBG_88E("%s()-%d: rtw_xmit() return error!\n", __func__, __LINE__); |
| 191 | pxmitpriv->tx_drop++; |
| 192 | dev_kfree_skb_any(newskb); |
| 193 | } else { |
| 194 | pxmitpriv->tx_pkts++; |
| 195 | } |
| 196 | } else { |
| 197 | DBG_88E("%s-%d: skb_copy() failed!\n", __func__, __LINE__); |
| 198 | pxmitpriv->tx_drop++; |
| 199 | |
Larry Finger | e02bcf6 | 2013-12-19 22:38:35 -0600 | [diff] [blame] | 200 | spin_unlock_bh(&pstapriv->asoc_list_lock); |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 201 | return false; /* Caller shall tx this multicast frame via normal way. */ |
| 202 | } |
| 203 | } |
| 204 | |
Larry Finger | e02bcf6 | 2013-12-19 22:38:35 -0600 | [diff] [blame] | 205 | spin_unlock_bh(&pstapriv->asoc_list_lock); |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 206 | dev_kfree_skb_any(skb); |
| 207 | return true; |
| 208 | } |
| 209 | |
| 210 | |
| 211 | int rtw_xmit_entry(struct sk_buff *pkt, struct net_device *pnetdev) |
| 212 | { |
| 213 | struct adapter *padapter = (struct adapter *)rtw_netdev_priv(pnetdev); |
| 214 | struct xmit_priv *pxmitpriv = &padapter->xmitpriv; |
| 215 | struct mlme_priv *pmlmepriv = &padapter->mlmepriv; |
| 216 | s32 res = 0; |
| 217 | |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 218 | |
| 219 | RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n")); |
| 220 | |
| 221 | if (rtw_if_up(padapter) == false) { |
| 222 | RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n")); |
| 223 | goto drop_packet; |
| 224 | } |
| 225 | |
| 226 | rtw_check_xmit_resource(padapter, pkt); |
| 227 | |
| 228 | if (!rtw_mc2u_disable && check_fwstate(pmlmepriv, WIFI_AP_STATE) && |
| 229 | (IP_MCAST_MAC(pkt->data) || ICMPV6_MCAST_MAC(pkt->data)) && |
| 230 | (padapter->registrypriv.wifi_spec == 0)) { |
| 231 | if (pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4)) { |
| 232 | res = rtw_mlcst2unicst(padapter, pkt); |
| 233 | if (res) |
| 234 | goto exit; |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | res = rtw_xmit(padapter, &pkt); |
| 239 | if (res < 0) |
| 240 | goto drop_packet; |
| 241 | |
| 242 | pxmitpriv->tx_pkts++; |
| 243 | RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts)); |
| 244 | goto exit; |
| 245 | |
| 246 | drop_packet: |
| 247 | pxmitpriv->tx_drop++; |
| 248 | dev_kfree_skb_any(pkt); |
| 249 | RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop)); |
| 250 | |
| 251 | exit: |
| 252 | |
Larry Finger | c2478d3 | 2013-08-21 22:34:02 -0500 | [diff] [blame] | 253 | |
| 254 | return 0; |
| 255 | } |