vnic_wq.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. // Copyright 2014 Cisco Systems, Inc. All rights reserved.
  3. #include <linux/errno.h>
  4. #include <linux/types.h>
  5. #include <linux/pci.h>
  6. #include <linux/delay.h>
  7. #include <linux/slab.h>
  8. #include "vnic_dev.h"
  9. #include "vnic_wq.h"
  10. static inline int vnic_wq_get_ctrl(struct vnic_dev *vdev, struct vnic_wq *wq,
  11. unsigned int index, enum vnic_res_type res_type)
  12. {
  13. wq->ctrl = svnic_dev_get_res(vdev, res_type, index);
  14. if (!wq->ctrl)
  15. return -EINVAL;
  16. return 0;
  17. }
  18. static inline int vnic_wq_alloc_ring(struct vnic_dev *vdev, struct vnic_wq *wq,
  19. unsigned int index, unsigned int desc_count, unsigned int desc_size)
  20. {
  21. return svnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count,
  22. desc_size);
  23. }
  24. static int vnic_wq_alloc_bufs(struct vnic_wq *wq)
  25. {
  26. struct vnic_wq_buf *buf;
  27. unsigned int i, j, count = wq->ring.desc_count;
  28. unsigned int blks = VNIC_WQ_BUF_BLKS_NEEDED(count);
  29. for (i = 0; i < blks; i++) {
  30. wq->bufs[i] = kzalloc(VNIC_WQ_BUF_BLK_SZ, GFP_ATOMIC);
  31. if (!wq->bufs[i]) {
  32. pr_err("Failed to alloc wq_bufs\n");
  33. return -ENOMEM;
  34. }
  35. }
  36. for (i = 0; i < blks; i++) {
  37. buf = wq->bufs[i];
  38. for (j = 0; j < VNIC_WQ_BUF_DFLT_BLK_ENTRIES; j++) {
  39. buf->index = i * VNIC_WQ_BUF_DFLT_BLK_ENTRIES + j;
  40. buf->desc = (u8 *)wq->ring.descs +
  41. wq->ring.desc_size * buf->index;
  42. if (buf->index + 1 == count) {
  43. buf->next = wq->bufs[0];
  44. break;
  45. } else if (j + 1 == VNIC_WQ_BUF_DFLT_BLK_ENTRIES) {
  46. buf->next = wq->bufs[i + 1];
  47. } else {
  48. buf->next = buf + 1;
  49. buf++;
  50. }
  51. }
  52. }
  53. wq->to_use = wq->to_clean = wq->bufs[0];
  54. return 0;
  55. }
  56. void svnic_wq_free(struct vnic_wq *wq)
  57. {
  58. struct vnic_dev *vdev;
  59. unsigned int i;
  60. vdev = wq->vdev;
  61. svnic_dev_free_desc_ring(vdev, &wq->ring);
  62. for (i = 0; i < VNIC_WQ_BUF_BLKS_MAX; i++) {
  63. kfree(wq->bufs[i]);
  64. wq->bufs[i] = NULL;
  65. }
  66. wq->ctrl = NULL;
  67. }
  68. int vnic_wq_devcmd2_alloc(struct vnic_dev *vdev, struct vnic_wq *wq,
  69. unsigned int desc_count, unsigned int desc_size)
  70. {
  71. int err;
  72. wq->index = 0;
  73. wq->vdev = vdev;
  74. err = vnic_wq_get_ctrl(vdev, wq, 0, RES_TYPE_DEVCMD2);
  75. if (err) {
  76. pr_err("Failed to get devcmd2 resource\n");
  77. return err;
  78. }
  79. svnic_wq_disable(wq);
  80. err = vnic_wq_alloc_ring(vdev, wq, 0, desc_count, desc_size);
  81. if (err)
  82. return err;
  83. return 0;
  84. }
  85. int svnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq,
  86. unsigned int index, unsigned int desc_count, unsigned int desc_size)
  87. {
  88. int err;
  89. wq->index = index;
  90. wq->vdev = vdev;
  91. err = vnic_wq_get_ctrl(vdev, wq, index, RES_TYPE_WQ);
  92. if (err) {
  93. pr_err("Failed to hook WQ[%d] resource\n", index);
  94. return err;
  95. }
  96. svnic_wq_disable(wq);
  97. err = vnic_wq_alloc_ring(vdev, wq, index, desc_count, desc_size);
  98. if (err)
  99. return err;
  100. err = vnic_wq_alloc_bufs(wq);
  101. if (err) {
  102. svnic_wq_free(wq);
  103. return err;
  104. }
  105. return 0;
  106. }
  107. void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
  108. unsigned int fetch_index, unsigned int posted_index,
  109. unsigned int error_interrupt_enable,
  110. unsigned int error_interrupt_offset)
  111. {
  112. u64 paddr;
  113. unsigned int count = wq->ring.desc_count;
  114. paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET;
  115. writeq(paddr, &wq->ctrl->ring_base);
  116. iowrite32(count, &wq->ctrl->ring_size);
  117. iowrite32(fetch_index, &wq->ctrl->fetch_index);
  118. iowrite32(posted_index, &wq->ctrl->posted_index);
  119. iowrite32(cq_index, &wq->ctrl->cq_index);
  120. iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable);
  121. iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset);
  122. iowrite32(0, &wq->ctrl->error_status);
  123. wq->to_use = wq->to_clean =
  124. &wq->bufs[fetch_index / VNIC_WQ_BUF_BLK_ENTRIES(count)]
  125. [fetch_index % VNIC_WQ_BUF_BLK_ENTRIES(count)];
  126. }
  127. void svnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
  128. unsigned int error_interrupt_enable,
  129. unsigned int error_interrupt_offset)
  130. {
  131. vnic_wq_init_start(wq, cq_index, 0, 0, error_interrupt_enable,
  132. error_interrupt_offset);
  133. }
  134. unsigned int svnic_wq_error_status(struct vnic_wq *wq)
  135. {
  136. return ioread32(&wq->ctrl->error_status);
  137. }
  138. void svnic_wq_enable(struct vnic_wq *wq)
  139. {
  140. iowrite32(1, &wq->ctrl->enable);
  141. }
  142. int svnic_wq_disable(struct vnic_wq *wq)
  143. {
  144. unsigned int wait;
  145. iowrite32(0, &wq->ctrl->enable);
  146. /* Wait for HW to ACK disable request */
  147. for (wait = 0; wait < 100; wait++) {
  148. if (!(ioread32(&wq->ctrl->running)))
  149. return 0;
  150. udelay(1);
  151. }
  152. pr_err("Failed to disable WQ[%d]\n", wq->index);
  153. return -ETIMEDOUT;
  154. }
  155. void svnic_wq_clean(struct vnic_wq *wq,
  156. void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf))
  157. {
  158. struct vnic_wq_buf *buf;
  159. BUG_ON(ioread32(&wq->ctrl->enable));
  160. buf = wq->to_clean;
  161. while (svnic_wq_desc_used(wq) > 0) {
  162. (*buf_clean)(wq, buf);
  163. buf = wq->to_clean = buf->next;
  164. wq->ring.desc_avail++;
  165. }
  166. wq->to_use = wq->to_clean = wq->bufs[0];
  167. iowrite32(0, &wq->ctrl->fetch_index);
  168. iowrite32(0, &wq->ctrl->posted_index);
  169. iowrite32(0, &wq->ctrl->error_status);
  170. svnic_dev_clear_desc_ring(&wq->ring);
  171. }