rtw_mem.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2016 - 2017 Realtek Corporation.
  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. *****************************************************************************/
  15. #include <drv_types.h>
  16. #include <rtw_mem.h>
  17. MODULE_LICENSE("GPL");
  18. MODULE_DESCRIPTION("Realtek Wireless Lan Driver");
  19. MODULE_AUTHOR("Realtek Semiconductor Corp.");
  20. MODULE_VERSION("DRIVERVERSION");
  21. struct sk_buff_head rtk_skb_mem_q;
  22. struct u8 *rtk_buf_mem[NR_RECVBUFF];
  23. struct u8 *rtw_get_buf_premem(int index)
  24. {
  25. printk("%s, rtk_buf_mem index : %d\n", __func__, index);
  26. return rtk_buf_mem[index];
  27. }
  28. u16 rtw_rtkm_get_buff_size(void)
  29. {
  30. return MAX_RTKM_RECVBUF_SZ;
  31. }
  32. EXPORT_SYMBOL(rtw_rtkm_get_buff_size);
  33. u8 rtw_rtkm_get_nr_recv_skb(void)
  34. {
  35. return MAX_RTKM_NR_PREALLOC_RECV_SKB;
  36. }
  37. EXPORT_SYMBOL(rtw_rtkm_get_nr_recv_skb);
  38. struct sk_buff *rtw_alloc_skb_premem(u16 in_size)
  39. {
  40. struct sk_buff *skb = NULL;
  41. if (in_size > MAX_RTKM_RECVBUF_SZ) {
  42. pr_info("warning %s: driver buffer size(%d) > rtkm buffer size(%d)\n", __func__, in_size, MAX_RTKM_RECVBUF_SZ);
  43. WARN_ON(1);
  44. return skb;
  45. }
  46. skb = skb_dequeue(&rtk_skb_mem_q);
  47. printk("%s, rtk_skb_mem_q len : %d\n", __func__, skb_queue_len(&rtk_skb_mem_q));
  48. return skb;
  49. }
  50. EXPORT_SYMBOL(rtw_alloc_skb_premem);
  51. int rtw_free_skb_premem(struct sk_buff *pskb)
  52. {
  53. if (!pskb)
  54. return -1;
  55. if (skb_queue_len(&rtk_skb_mem_q) >= MAX_RTKM_NR_PREALLOC_RECV_SKB)
  56. return -1;
  57. skb_queue_tail(&rtk_skb_mem_q, pskb);
  58. printk("%s, rtk_skb_mem_q len : %d\n", __func__, skb_queue_len(&rtk_skb_mem_q));
  59. return 0;
  60. }
  61. EXPORT_SYMBOL(rtw_free_skb_premem);
  62. static int __init rtw_mem_init(void)
  63. {
  64. int i;
  65. SIZE_PTR tmpaddr = 0;
  66. SIZE_PTR alignment = 0;
  67. struct sk_buff *pskb = NULL;
  68. printk("%s\n", __func__);
  69. pr_info("MAX_RTKM_NR_PREALLOC_RECV_SKB: %d\n", MAX_RTKM_NR_PREALLOC_RECV_SKB);
  70. pr_info("MAX_RTKM_RECVBUF_SZ: %d\n", MAX_RTKM_RECVBUF_SZ);
  71. #ifdef CONFIG_USE_USB_BUFFER_ALLOC_RX
  72. for (i = 0; i < NR_RECVBUFF; i++)
  73. rtk_buf_mem[i] = usb_buffer_alloc(dev, size, (in_interrupt() ? GFP_ATOMIC : GFP_KERNEL), dma);
  74. #endif /* CONFIG_USE_USB_BUFFER_ALLOC_RX */
  75. skb_queue_head_init(&rtk_skb_mem_q);
  76. for (i = 0; i < MAX_RTKM_NR_PREALLOC_RECV_SKB; i++) {
  77. pskb = __dev_alloc_skb(MAX_RTKM_RECVBUF_SZ + RECVBUFF_ALIGN_SZ, in_interrupt() ? GFP_ATOMIC : GFP_KERNEL);
  78. if (pskb) {
  79. tmpaddr = (SIZE_PTR)pskb->data;
  80. alignment = tmpaddr & (RECVBUFF_ALIGN_SZ - 1);
  81. skb_reserve(pskb, (RECVBUFF_ALIGN_SZ - alignment));
  82. skb_queue_tail(&rtk_skb_mem_q, pskb);
  83. } else
  84. printk("%s, alloc skb memory fail!\n", __func__);
  85. pskb = NULL;
  86. }
  87. printk("%s, rtk_skb_mem_q len : %d\n", __func__, skb_queue_len(&rtk_skb_mem_q));
  88. return 0;
  89. }
  90. static void __exit rtw_mem_exit(void)
  91. {
  92. if (skb_queue_len(&rtk_skb_mem_q))
  93. printk("%s, rtk_skb_mem_q len : %d\n", __func__, skb_queue_len(&rtk_skb_mem_q));
  94. skb_queue_purge(&rtk_skb_mem_q);
  95. printk("%s\n", __func__);
  96. }
  97. module_init(rtw_mem_init);
  98. module_exit(rtw_mem_exit);