xmit_linux.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. *
  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. #include <drv_types.h>
  22. uint rtw_remainder_len(struct pkt_file *pfile)
  23. {
  24. return (pfile->buf_len - ((SIZE_PTR)(pfile->cur_addr) - (SIZE_PTR)(pfile->buf_start)));
  25. }
  26. void _rtw_open_pktfile (_pkt *pktptr, struct pkt_file *pfile)
  27. {
  28. _func_enter_;
  29. pfile->pkt = pktptr;
  30. pfile->cur_addr = pfile->buf_start = pktptr->data;
  31. pfile->pkt_len = pfile->buf_len = pktptr->len;
  32. pfile->cur_buffer = pfile->buf_start ;
  33. _func_exit_;
  34. }
  35. uint _rtw_pktfile_read (struct pkt_file *pfile, u8 *rmem, uint rlen)
  36. {
  37. uint len = 0;
  38. _func_enter_;
  39. len = rtw_remainder_len(pfile);
  40. len = (rlen > len)? len: rlen;
  41. if(rmem)
  42. skb_copy_bits(pfile->pkt, pfile->buf_len-pfile->pkt_len, rmem, len);
  43. pfile->cur_addr += len;
  44. pfile->pkt_len -= len;
  45. _func_exit_;
  46. return len;
  47. }
  48. sint rtw_endofpktfile(struct pkt_file *pfile)
  49. {
  50. _func_enter_;
  51. if (pfile->pkt_len == 0) {
  52. _func_exit_;
  53. return _TRUE;
  54. }
  55. _func_exit_;
  56. return _FALSE;
  57. }
  58. void rtw_set_tx_chksum_offload(_pkt *pkt, struct pkt_attrib *pattrib)
  59. {
  60. #ifdef CONFIG_TCP_CSUM_OFFLOAD_TX
  61. struct sk_buff *skb = (struct sk_buff *)pkt;
  62. pattrib->hw_tcp_csum = 0;
  63. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  64. if (skb_shinfo(skb)->nr_frags == 0)
  65. {
  66. const struct iphdr *ip = ip_hdr(skb);
  67. if (ip->protocol == IPPROTO_TCP) {
  68. // TCP checksum offload by HW
  69. DBG_871X("CHECKSUM_PARTIAL TCP\n");
  70. pattrib->hw_tcp_csum = 1;
  71. //skb_checksum_help(skb);
  72. } else if (ip->protocol == IPPROTO_UDP) {
  73. //DBG_871X("CHECKSUM_PARTIAL UDP\n");
  74. #if 1
  75. skb_checksum_help(skb);
  76. #else
  77. // Set UDP checksum = 0 to skip checksum check
  78. struct udphdr *udp = skb_transport_header(skb);
  79. udp->check = 0;
  80. #endif
  81. } else {
  82. DBG_871X("%s-%d TCP CSUM offload Error!!\n", __FUNCTION__, __LINE__);
  83. WARN_ON(1); /* we need a WARN() */
  84. }
  85. }
  86. else { // IP fragmentation case
  87. DBG_871X("%s-%d nr_frags != 0, using skb_checksum_help(skb);!!\n", __FUNCTION__, __LINE__);
  88. skb_checksum_help(skb);
  89. }
  90. }
  91. #endif
  92. }
  93. int rtw_os_xmit_resource_alloc(_adapter *padapter, struct xmit_buf *pxmitbuf, u32 alloc_sz, u8 flag)
  94. {
  95. if (alloc_sz > 0) {
  96. #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
  97. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  98. struct usb_device *pusbd = pdvobjpriv->pusbdev;
  99. pxmitbuf->pallocated_buf = rtw_usb_buffer_alloc(pusbd, (size_t)alloc_sz, &pxmitbuf->dma_transfer_addr);
  100. pxmitbuf->pbuf = pxmitbuf->pallocated_buf;
  101. if(pxmitbuf->pallocated_buf == NULL)
  102. return _FAIL;
  103. #else // CONFIG_USE_USB_BUFFER_ALLOC_TX
  104. pxmitbuf->pallocated_buf = rtw_zmalloc(alloc_sz);
  105. if (pxmitbuf->pallocated_buf == NULL)
  106. {
  107. return _FAIL;
  108. }
  109. pxmitbuf->pbuf = (u8 *)N_BYTE_ALIGMENT((SIZE_PTR)(pxmitbuf->pallocated_buf), XMITBUF_ALIGN_SZ);
  110. #endif // CONFIG_USE_USB_BUFFER_ALLOC_TX
  111. }
  112. if (flag) {
  113. #ifdef CONFIG_USB_HCI
  114. int i;
  115. for(i=0; i<8; i++)
  116. {
  117. pxmitbuf->pxmit_urb[i] = usb_alloc_urb(0, GFP_KERNEL);
  118. if(pxmitbuf->pxmit_urb[i] == NULL)
  119. {
  120. DBG_871X("pxmitbuf->pxmit_urb[i]==NULL");
  121. return _FAIL;
  122. }
  123. }
  124. #endif
  125. }
  126. return _SUCCESS;
  127. }
  128. void rtw_os_xmit_resource_free(_adapter *padapter, struct xmit_buf *pxmitbuf,u32 free_sz, u8 flag)
  129. {
  130. if (flag) {
  131. #ifdef CONFIG_USB_HCI
  132. int i;
  133. for(i=0; i<8; i++)
  134. {
  135. if(pxmitbuf->pxmit_urb[i])
  136. {
  137. //usb_kill_urb(pxmitbuf->pxmit_urb[i]);
  138. usb_free_urb(pxmitbuf->pxmit_urb[i]);
  139. }
  140. }
  141. #endif
  142. }
  143. if (free_sz > 0 ) {
  144. #ifdef CONFIG_USE_USB_BUFFER_ALLOC_TX
  145. struct dvobj_priv *pdvobjpriv = adapter_to_dvobj(padapter);
  146. struct usb_device *pusbd = pdvobjpriv->pusbdev;
  147. rtw_usb_buffer_free(pusbd, (size_t)free_sz, pxmitbuf->pallocated_buf, pxmitbuf->dma_transfer_addr);
  148. pxmitbuf->pallocated_buf = NULL;
  149. pxmitbuf->dma_transfer_addr = 0;
  150. #else // CONFIG_USE_USB_BUFFER_ALLOC_TX
  151. if(pxmitbuf->pallocated_buf)
  152. rtw_mfree(pxmitbuf->pallocated_buf, free_sz);
  153. #endif // CONFIG_USE_USB_BUFFER_ALLOC_TX
  154. }
  155. }
  156. #define WMM_XMIT_THRESHOLD (NR_XMITFRAME*2/5)
  157. void rtw_os_pkt_complete(_adapter *padapter, _pkt *pkt)
  158. {
  159. #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,35))
  160. u16 queue;
  161. struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
  162. queue = skb_get_queue_mapping(pkt);
  163. if (padapter->registrypriv.wifi_spec) {
  164. if(__netif_subqueue_stopped(padapter->pnetdev, queue) &&
  165. (pxmitpriv->hwxmits[queue].accnt < WMM_XMIT_THRESHOLD))
  166. {
  167. netif_wake_subqueue(padapter->pnetdev, queue);
  168. }
  169. } else {
  170. if(__netif_subqueue_stopped(padapter->pnetdev, queue))
  171. netif_wake_subqueue(padapter->pnetdev, queue);
  172. }
  173. #else
  174. if (netif_queue_stopped(padapter->pnetdev))
  175. netif_wake_queue(padapter->pnetdev);
  176. #endif
  177. dev_kfree_skb_any(pkt);
  178. }
  179. void rtw_os_xmit_complete(_adapter *padapter, struct xmit_frame *pxframe)
  180. {
  181. if(pxframe->pkt)
  182. {
  183. //RT_TRACE(_module_xmit_osdep_c_,_drv_err_,("linux : rtw_os_xmit_complete, dev_kfree_skb()\n"));
  184. //dev_kfree_skb_any(pxframe->pkt);
  185. rtw_os_pkt_complete(padapter, pxframe->pkt);
  186. }
  187. pxframe->pkt = NULL;
  188. }
  189. void rtw_os_xmit_schedule(_adapter *padapter)
  190. {
  191. _adapter *pri_adapter = padapter;
  192. #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_GSPI_HCI)
  193. if(!padapter)
  194. return;
  195. #ifdef CONFIG_CONCURRENT_MODE
  196. if(padapter->adapter_type > PRIMARY_ADAPTER)
  197. pri_adapter = padapter->pbuddy_adapter;
  198. #endif
  199. if (_rtw_queue_empty(&pri_adapter->xmitpriv.pending_xmitbuf_queue) == _FALSE)
  200. _rtw_up_sema(&pri_adapter->xmitpriv.xmit_sema);
  201. #else
  202. _irqL irqL;
  203. struct xmit_priv *pxmitpriv;
  204. if(!padapter)
  205. return;
  206. pxmitpriv = &padapter->xmitpriv;
  207. _enter_critical_bh(&pxmitpriv->lock, &irqL);
  208. if(rtw_txframes_pending(padapter))
  209. {
  210. tasklet_hi_schedule(&pxmitpriv->xmit_tasklet);
  211. }
  212. _exit_critical_bh(&pxmitpriv->lock, &irqL);
  213. #endif
  214. }
  215. static void rtw_check_xmit_resource(_adapter *padapter, _pkt *pkt)
  216. {
  217. struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
  218. #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
  219. u16 queue;
  220. queue = skb_get_queue_mapping(pkt);
  221. if (padapter->registrypriv.wifi_spec) {
  222. /* No free space for Tx, tx_worker is too slow */
  223. if (pxmitpriv->hwxmits[queue].accnt > WMM_XMIT_THRESHOLD) {
  224. //DBG_871X("%s(): stop netif_subqueue[%d]\n", __FUNCTION__, queue);
  225. netif_stop_subqueue(padapter->pnetdev, queue);
  226. }
  227. } else {
  228. if(pxmitpriv->free_xmitframe_cnt<=4) {
  229. if (!netif_tx_queue_stopped(netdev_get_tx_queue(padapter->pnetdev, queue)))
  230. netif_stop_subqueue(padapter->pnetdev, queue);
  231. }
  232. }
  233. #else
  234. if(pxmitpriv->free_xmitframe_cnt<=4)
  235. {
  236. if (!rtw_netif_queue_stopped(padapter->pnetdev))
  237. rtw_netif_stop_queue(padapter->pnetdev);
  238. }
  239. #endif
  240. }
  241. #ifdef CONFIG_TX_MCAST2UNI
  242. int rtw_mlcst2unicst(_adapter *padapter, struct sk_buff *skb)
  243. {
  244. struct sta_priv *pstapriv = &padapter->stapriv;
  245. struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
  246. _irqL irqL;
  247. _list *phead, *plist;
  248. struct sk_buff *newskb;
  249. struct sta_info *psta = NULL;
  250. u8 chk_alive_num = 0;
  251. char chk_alive_list[NUM_STA];
  252. u8 bc_addr[6]={0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  253. u8 null_addr[6]={0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
  254. int i;
  255. s32 res;
  256. _enter_critical_bh(&pstapriv->asoc_list_lock, &irqL);
  257. phead = &pstapriv->asoc_list;
  258. plist = get_next(phead);
  259. //free sta asoc_queue
  260. while ((rtw_end_of_queue_search(phead, plist)) == _FALSE) {
  261. int stainfo_offset;
  262. psta = LIST_CONTAINOR(plist, struct sta_info, asoc_list);
  263. plist = get_next(plist);
  264. stainfo_offset = rtw_stainfo_offset(pstapriv, psta);
  265. if (stainfo_offset_valid(stainfo_offset)) {
  266. chk_alive_list[chk_alive_num++] = stainfo_offset;
  267. }
  268. }
  269. _exit_critical_bh(&pstapriv->asoc_list_lock, &irqL);
  270. for (i = 0; i < chk_alive_num; i++) {
  271. psta = rtw_get_stainfo_by_offset(pstapriv, chk_alive_list[i]);
  272. if(!(psta->state &_FW_LINKED))
  273. continue;
  274. /* avoid come from STA1 and send back STA1 */
  275. if (_rtw_memcmp(psta->hwaddr, &skb->data[6], 6) == _TRUE
  276. || _rtw_memcmp(psta->hwaddr, null_addr, 6) == _TRUE
  277. || _rtw_memcmp(psta->hwaddr, bc_addr, 6) == _TRUE
  278. )
  279. continue;
  280. newskb = skb_copy(skb, GFP_ATOMIC);
  281. if (newskb) {
  282. _rtw_memcpy(newskb->data, psta->hwaddr, 6);
  283. res = rtw_xmit(padapter, &newskb);
  284. if (res < 0) {
  285. DBG_871X("%s()-%d: rtw_xmit() return error!\n", __FUNCTION__, __LINE__);
  286. pxmitpriv->tx_drop++;
  287. dev_kfree_skb_any(newskb);
  288. } else
  289. pxmitpriv->tx_pkts++;
  290. } else {
  291. DBG_871X("%s-%d: skb_copy() failed!\n", __FUNCTION__, __LINE__);
  292. pxmitpriv->tx_drop++;
  293. //dev_kfree_skb_any(skb);
  294. return _FALSE; // Caller shall tx this multicast frame via normal way.
  295. }
  296. }
  297. dev_kfree_skb_any(skb);
  298. return _TRUE;
  299. }
  300. #endif // CONFIG_TX_MCAST2UNI
  301. int rtw_xmit_entry(_pkt *pkt, _nic_hdl pnetdev)
  302. {
  303. _adapter *padapter = (_adapter *)rtw_netdev_priv(pnetdev);
  304. struct xmit_priv *pxmitpriv = &padapter->xmitpriv;
  305. #ifdef CONFIG_TX_MCAST2UNI
  306. struct mlme_priv *pmlmepriv = &padapter->mlmepriv;
  307. extern int rtw_mc2u_disable;
  308. #endif // CONFIG_TX_MCAST2UNI
  309. s32 res = 0;
  310. #if (LINUX_VERSION_CODE>=KERNEL_VERSION(2,6,35))
  311. u16 queue;
  312. #endif
  313. _func_enter_;
  314. RT_TRACE(_module_rtl871x_mlme_c_, _drv_info_, ("+xmit_enry\n"));
  315. if (rtw_if_up(padapter) == _FALSE) {
  316. RT_TRACE(_module_xmit_osdep_c_, _drv_err_, ("rtw_xmit_entry: rtw_if_up fail\n"));
  317. #ifdef DBG_TX_DROP_FRAME
  318. DBG_871X("DBG_TX_DROP_FRAME %s if_up fail\n", __FUNCTION__);
  319. #endif
  320. goto drop_packet;
  321. }
  322. rtw_check_xmit_resource(padapter, pkt);
  323. #ifdef CONFIG_TX_MCAST2UNI
  324. if ( !rtw_mc2u_disable
  325. && check_fwstate(pmlmepriv, WIFI_AP_STATE) == _TRUE
  326. && ( IP_MCAST_MAC(pkt->data)
  327. || ICMPV6_MCAST_MAC(pkt->data) )
  328. && (padapter->registrypriv.wifi_spec == 0)
  329. )
  330. {
  331. if ( pxmitpriv->free_xmitframe_cnt > (NR_XMITFRAME/4) ) {
  332. res = rtw_mlcst2unicst(padapter, pkt);
  333. if (res == _TRUE) {
  334. goto exit;
  335. }
  336. } else {
  337. //DBG_871X("Stop M2U(%d, %d)! ", pxmitpriv->free_xmitframe_cnt, pxmitpriv->free_xmitbuf_cnt);
  338. //DBG_871X("!m2u );
  339. }
  340. }
  341. #endif // CONFIG_TX_MCAST2UNI
  342. res = rtw_xmit(padapter, &pkt);
  343. if (res < 0) {
  344. #ifdef DBG_TX_DROP_FRAME
  345. DBG_871X("DBG_TX_DROP_FRAME %s rtw_xmit fail\n", __FUNCTION__);
  346. #endif
  347. goto drop_packet;
  348. }
  349. pxmitpriv->tx_pkts++;
  350. RT_TRACE(_module_xmit_osdep_c_, _drv_info_, ("rtw_xmit_entry: tx_pkts=%d\n", (u32)pxmitpriv->tx_pkts));
  351. goto exit;
  352. drop_packet:
  353. pxmitpriv->tx_drop++;
  354. dev_kfree_skb_any(pkt);
  355. RT_TRACE(_module_xmit_osdep_c_, _drv_notice_, ("rtw_xmit_entry: drop, tx_drop=%d\n", (u32)pxmitpriv->tx_drop));
  356. exit:
  357. _func_exit_;
  358. return 0;
  359. }