vnic_wq.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright 2014 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. */
  17. #include <linux/errno.h>
  18. #include <linux/types.h>
  19. #include <linux/pci.h>
  20. #include <linux/delay.h>
  21. #include <linux/slab.h>
  22. #include "vnic_dev.h"
  23. #include "vnic_wq.h"
  24. static inline int vnic_wq_get_ctrl(struct vnic_dev *vdev, struct vnic_wq *wq,
  25. unsigned int index, enum vnic_res_type res_type)
  26. {
  27. wq->ctrl = svnic_dev_get_res(vdev, res_type, index);
  28. if (!wq->ctrl)
  29. return -EINVAL;
  30. return 0;
  31. }
  32. static inline int vnic_wq_alloc_ring(struct vnic_dev *vdev, struct vnic_wq *wq,
  33. unsigned int index, unsigned int desc_count, unsigned int desc_size)
  34. {
  35. return svnic_dev_alloc_desc_ring(vdev, &wq->ring, desc_count,
  36. desc_size);
  37. }
  38. static int vnic_wq_alloc_bufs(struct vnic_wq *wq)
  39. {
  40. struct vnic_wq_buf *buf;
  41. unsigned int i, j, count = wq->ring.desc_count;
  42. unsigned int blks = VNIC_WQ_BUF_BLKS_NEEDED(count);
  43. for (i = 0; i < blks; i++) {
  44. wq->bufs[i] = kzalloc(VNIC_WQ_BUF_BLK_SZ, GFP_ATOMIC);
  45. if (!wq->bufs[i]) {
  46. pr_err("Failed to alloc wq_bufs\n");
  47. return -ENOMEM;
  48. }
  49. }
  50. for (i = 0; i < blks; i++) {
  51. buf = wq->bufs[i];
  52. for (j = 0; j < VNIC_WQ_BUF_DFLT_BLK_ENTRIES; j++) {
  53. buf->index = i * VNIC_WQ_BUF_DFLT_BLK_ENTRIES + j;
  54. buf->desc = (u8 *)wq->ring.descs +
  55. wq->ring.desc_size * buf->index;
  56. if (buf->index + 1 == count) {
  57. buf->next = wq->bufs[0];
  58. break;
  59. } else if (j + 1 == VNIC_WQ_BUF_DFLT_BLK_ENTRIES) {
  60. buf->next = wq->bufs[i + 1];
  61. } else {
  62. buf->next = buf + 1;
  63. buf++;
  64. }
  65. }
  66. }
  67. wq->to_use = wq->to_clean = wq->bufs[0];
  68. return 0;
  69. }
  70. void svnic_wq_free(struct vnic_wq *wq)
  71. {
  72. struct vnic_dev *vdev;
  73. unsigned int i;
  74. vdev = wq->vdev;
  75. svnic_dev_free_desc_ring(vdev, &wq->ring);
  76. for (i = 0; i < VNIC_WQ_BUF_BLKS_MAX; i++) {
  77. kfree(wq->bufs[i]);
  78. wq->bufs[i] = NULL;
  79. }
  80. wq->ctrl = NULL;
  81. }
  82. int vnic_wq_devcmd2_alloc(struct vnic_dev *vdev, struct vnic_wq *wq,
  83. unsigned int desc_count, unsigned int desc_size)
  84. {
  85. int err;
  86. wq->index = 0;
  87. wq->vdev = vdev;
  88. err = vnic_wq_get_ctrl(vdev, wq, 0, RES_TYPE_DEVCMD2);
  89. if (err) {
  90. pr_err("Failed to get devcmd2 resource\n");
  91. return err;
  92. }
  93. svnic_wq_disable(wq);
  94. err = vnic_wq_alloc_ring(vdev, wq, 0, desc_count, desc_size);
  95. if (err)
  96. return err;
  97. return 0;
  98. }
  99. int svnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq,
  100. unsigned int index, unsigned int desc_count, unsigned int desc_size)
  101. {
  102. int err;
  103. wq->index = index;
  104. wq->vdev = vdev;
  105. err = vnic_wq_get_ctrl(vdev, wq, index, RES_TYPE_WQ);
  106. if (err) {
  107. pr_err("Failed to hook WQ[%d] resource\n", index);
  108. return err;
  109. }
  110. svnic_wq_disable(wq);
  111. err = vnic_wq_alloc_ring(vdev, wq, index, desc_count, desc_size);
  112. if (err)
  113. return err;
  114. err = vnic_wq_alloc_bufs(wq);
  115. if (err) {
  116. svnic_wq_free(wq);
  117. return err;
  118. }
  119. return 0;
  120. }
  121. void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
  122. unsigned int fetch_index, unsigned int posted_index,
  123. unsigned int error_interrupt_enable,
  124. unsigned int error_interrupt_offset)
  125. {
  126. u64 paddr;
  127. unsigned int count = wq->ring.desc_count;
  128. paddr = (u64)wq->ring.base_addr | VNIC_PADDR_TARGET;
  129. writeq(paddr, &wq->ctrl->ring_base);
  130. iowrite32(count, &wq->ctrl->ring_size);
  131. iowrite32(fetch_index, &wq->ctrl->fetch_index);
  132. iowrite32(posted_index, &wq->ctrl->posted_index);
  133. iowrite32(cq_index, &wq->ctrl->cq_index);
  134. iowrite32(error_interrupt_enable, &wq->ctrl->error_interrupt_enable);
  135. iowrite32(error_interrupt_offset, &wq->ctrl->error_interrupt_offset);
  136. iowrite32(0, &wq->ctrl->error_status);
  137. wq->to_use = wq->to_clean =
  138. &wq->bufs[fetch_index / VNIC_WQ_BUF_BLK_ENTRIES(count)]
  139. [fetch_index % VNIC_WQ_BUF_BLK_ENTRIES(count)];
  140. }
  141. void svnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
  142. unsigned int error_interrupt_enable,
  143. unsigned int error_interrupt_offset)
  144. {
  145. vnic_wq_init_start(wq, cq_index, 0, 0, error_interrupt_enable,
  146. error_interrupt_offset);
  147. }
  148. unsigned int svnic_wq_error_status(struct vnic_wq *wq)
  149. {
  150. return ioread32(&wq->ctrl->error_status);
  151. }
  152. void svnic_wq_enable(struct vnic_wq *wq)
  153. {
  154. iowrite32(1, &wq->ctrl->enable);
  155. }
  156. int svnic_wq_disable(struct vnic_wq *wq)
  157. {
  158. unsigned int wait;
  159. iowrite32(0, &wq->ctrl->enable);
  160. /* Wait for HW to ACK disable request */
  161. for (wait = 0; wait < 100; wait++) {
  162. if (!(ioread32(&wq->ctrl->running)))
  163. return 0;
  164. udelay(1);
  165. }
  166. pr_err("Failed to disable WQ[%d]\n", wq->index);
  167. return -ETIMEDOUT;
  168. }
  169. void svnic_wq_clean(struct vnic_wq *wq,
  170. void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf))
  171. {
  172. struct vnic_wq_buf *buf;
  173. BUG_ON(ioread32(&wq->ctrl->enable));
  174. buf = wq->to_clean;
  175. while (svnic_wq_desc_used(wq) > 0) {
  176. (*buf_clean)(wq, buf);
  177. buf = wq->to_clean = buf->next;
  178. wq->ring.desc_avail++;
  179. }
  180. wq->to_use = wq->to_clean = wq->bufs[0];
  181. iowrite32(0, &wq->ctrl->fetch_index);
  182. iowrite32(0, &wq->ctrl->posted_index);
  183. iowrite32(0, &wq->ctrl->error_status);
  184. svnic_dev_clear_desc_ring(&wq->ring);
  185. }