hwbm.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* Support for hardware buffer manager.
  2. *
  3. * Copyright (C) 2016 Marvell
  4. *
  5. * Gregory CLEMENT <gregory.clement@free-electrons.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/printk.h>
  14. #include <linux/skbuff.h>
  15. #include <net/hwbm.h>
  16. void hwbm_buf_free(struct hwbm_pool *bm_pool, void *buf)
  17. {
  18. if (likely(bm_pool->frag_size <= PAGE_SIZE))
  19. skb_free_frag(buf);
  20. else
  21. kfree(buf);
  22. }
  23. EXPORT_SYMBOL_GPL(hwbm_buf_free);
  24. /* Refill processing for HW buffer management */
  25. int hwbm_pool_refill(struct hwbm_pool *bm_pool, gfp_t gfp)
  26. {
  27. int frag_size = bm_pool->frag_size;
  28. void *buf;
  29. if (likely(frag_size <= PAGE_SIZE))
  30. buf = netdev_alloc_frag(frag_size);
  31. else
  32. buf = kmalloc(frag_size, gfp);
  33. if (!buf)
  34. return -ENOMEM;
  35. if (bm_pool->construct)
  36. if (bm_pool->construct(bm_pool, buf)) {
  37. hwbm_buf_free(bm_pool, buf);
  38. return -ENOMEM;
  39. }
  40. return 0;
  41. }
  42. EXPORT_SYMBOL_GPL(hwbm_pool_refill);
  43. int hwbm_pool_add(struct hwbm_pool *bm_pool, unsigned int buf_num, gfp_t gfp)
  44. {
  45. int err, i;
  46. unsigned long flags;
  47. spin_lock_irqsave(&bm_pool->lock, flags);
  48. if (bm_pool->buf_num == bm_pool->size) {
  49. pr_warn("pool already filled\n");
  50. return bm_pool->buf_num;
  51. }
  52. if (buf_num + bm_pool->buf_num > bm_pool->size) {
  53. pr_warn("cannot allocate %d buffers for pool\n",
  54. buf_num);
  55. return 0;
  56. }
  57. if ((buf_num + bm_pool->buf_num) < bm_pool->buf_num) {
  58. pr_warn("Adding %d buffers to the %d current buffers will overflow\n",
  59. buf_num, bm_pool->buf_num);
  60. return 0;
  61. }
  62. for (i = 0; i < buf_num; i++) {
  63. err = hwbm_pool_refill(bm_pool, gfp);
  64. if (err < 0)
  65. break;
  66. }
  67. /* Update BM driver with number of buffers added to pool */
  68. bm_pool->buf_num += i;
  69. pr_debug("hwpm pool: %d of %d buffers added\n", i, buf_num);
  70. spin_unlock_irqrestore(&bm_pool->lock, flags);
  71. return i;
  72. }
  73. EXPORT_SYMBOL_GPL(hwbm_pool_add);