hwbm.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. spin_unlock_irqrestore(&bm_pool->lock, flags);
  51. return bm_pool->buf_num;
  52. }
  53. if (buf_num + bm_pool->buf_num > bm_pool->size) {
  54. pr_warn("cannot allocate %d buffers for pool\n",
  55. buf_num);
  56. spin_unlock_irqrestore(&bm_pool->lock, flags);
  57. return 0;
  58. }
  59. if ((buf_num + bm_pool->buf_num) < bm_pool->buf_num) {
  60. pr_warn("Adding %d buffers to the %d current buffers will overflow\n",
  61. buf_num, bm_pool->buf_num);
  62. spin_unlock_irqrestore(&bm_pool->lock, flags);
  63. return 0;
  64. }
  65. for (i = 0; i < buf_num; i++) {
  66. err = hwbm_pool_refill(bm_pool, gfp);
  67. if (err < 0)
  68. break;
  69. }
  70. /* Update BM driver with number of buffers added to pool */
  71. bm_pool->buf_num += i;
  72. pr_debug("hwpm pool: %d of %d buffers added\n", i, buf_num);
  73. spin_unlock_irqrestore(&bm_pool->lock, flags);
  74. return i;
  75. }
  76. EXPORT_SYMBOL_GPL(hwbm_pool_add);