fdma_api.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // SPDX-License-Identifier: GPL-2.0+
  2. #include "fdma_api.h"
  3. #include <linux/bits.h>
  4. #include <linux/etherdevice.h>
  5. #include <linux/types.h>
  6. /* Add a DB to a DCB, providing a callback for getting the DB dataptr. */
  7. static int __fdma_db_add(struct fdma *fdma, int dcb_idx, int db_idx, u64 status,
  8. int (*cb)(struct fdma *fdma, int dcb_idx,
  9. int db_idx, u64 *dataptr))
  10. {
  11. struct fdma_db *db = fdma_db_get(fdma, dcb_idx, db_idx);
  12. db->status = status;
  13. return cb(fdma, dcb_idx, db_idx, &db->dataptr);
  14. }
  15. /* Add a DB to a DCB, using the callback set in the fdma_ops struct. */
  16. int fdma_db_add(struct fdma *fdma, int dcb_idx, int db_idx, u64 status)
  17. {
  18. return __fdma_db_add(fdma,
  19. dcb_idx,
  20. db_idx,
  21. status,
  22. fdma->ops.dataptr_cb);
  23. }
  24. /* Add a DCB with callbacks for getting the DB dataptr and the DCB nextptr. */
  25. int __fdma_dcb_add(struct fdma *fdma, int dcb_idx, u64 info, u64 status,
  26. int (*dcb_cb)(struct fdma *fdma, int dcb_idx, u64 *nextptr),
  27. int (*db_cb)(struct fdma *fdma, int dcb_idx, int db_idx,
  28. u64 *dataptr))
  29. {
  30. struct fdma_dcb *dcb = fdma_dcb_get(fdma, dcb_idx);
  31. int i, err;
  32. for (i = 0; i < fdma->n_dbs; i++) {
  33. err = __fdma_db_add(fdma, dcb_idx, i, status, db_cb);
  34. if (unlikely(err))
  35. return err;
  36. }
  37. err = dcb_cb(fdma, dcb_idx, &fdma->last_dcb->nextptr);
  38. if (unlikely(err))
  39. return err;
  40. fdma->last_dcb = dcb;
  41. dcb->nextptr = FDMA_DCB_INVALID_DATA;
  42. dcb->info = info;
  43. return 0;
  44. }
  45. EXPORT_SYMBOL_GPL(__fdma_dcb_add);
  46. /* Add a DCB, using the preset callbacks in the fdma_ops struct. */
  47. int fdma_dcb_add(struct fdma *fdma, int dcb_idx, u64 info, u64 status)
  48. {
  49. return __fdma_dcb_add(fdma,
  50. dcb_idx,
  51. info, status,
  52. fdma->ops.nextptr_cb,
  53. fdma->ops.dataptr_cb);
  54. }
  55. EXPORT_SYMBOL_GPL(fdma_dcb_add);
  56. /* Initialize the DCB's and DB's. */
  57. int fdma_dcbs_init(struct fdma *fdma, u64 info, u64 status)
  58. {
  59. int i, err;
  60. fdma->last_dcb = fdma->dcbs;
  61. fdma->db_index = 0;
  62. fdma->dcb_index = 0;
  63. for (i = 0; i < fdma->n_dcbs; i++) {
  64. err = fdma_dcb_add(fdma, i, info, status);
  65. if (err)
  66. return err;
  67. }
  68. return 0;
  69. }
  70. EXPORT_SYMBOL_GPL(fdma_dcbs_init);
  71. /* Allocate coherent DMA memory for FDMA. */
  72. int fdma_alloc_coherent(struct device *dev, struct fdma *fdma)
  73. {
  74. fdma->dcbs = dma_alloc_coherent(dev,
  75. fdma->size,
  76. &fdma->dma,
  77. GFP_KERNEL);
  78. if (!fdma->dcbs)
  79. return -ENOMEM;
  80. return 0;
  81. }
  82. EXPORT_SYMBOL_GPL(fdma_alloc_coherent);
  83. /* Allocate physical memory for FDMA. */
  84. int fdma_alloc_phys(struct fdma *fdma)
  85. {
  86. fdma->dcbs = kzalloc(fdma->size, GFP_KERNEL);
  87. if (!fdma->dcbs)
  88. return -ENOMEM;
  89. fdma->dma = virt_to_phys(fdma->dcbs);
  90. return 0;
  91. }
  92. EXPORT_SYMBOL_GPL(fdma_alloc_phys);
  93. /* Free coherent DMA memory. */
  94. void fdma_free_coherent(struct device *dev, struct fdma *fdma)
  95. {
  96. dma_free_coherent(dev, fdma->size, fdma->dcbs, fdma->dma);
  97. }
  98. EXPORT_SYMBOL_GPL(fdma_free_coherent);
  99. /* Free virtual memory. */
  100. void fdma_free_phys(struct fdma *fdma)
  101. {
  102. kfree(fdma->dcbs);
  103. }
  104. EXPORT_SYMBOL_GPL(fdma_free_phys);
  105. /* Get the size of the FDMA memory */
  106. u32 fdma_get_size(struct fdma *fdma)
  107. {
  108. return ALIGN(sizeof(struct fdma_dcb) * fdma->n_dcbs, PAGE_SIZE);
  109. }
  110. EXPORT_SYMBOL_GPL(fdma_get_size);
  111. /* Get the size of the FDMA memory. This function is only applicable if the
  112. * dataptr addresses and DCB's are in contiguous memory.
  113. */
  114. u32 fdma_get_size_contiguous(struct fdma *fdma)
  115. {
  116. return ALIGN(fdma->n_dcbs * sizeof(struct fdma_dcb) +
  117. fdma->n_dcbs * fdma->n_dbs * fdma->db_size,
  118. PAGE_SIZE);
  119. }
  120. EXPORT_SYMBOL_GPL(fdma_get_size_contiguous);