vnic_wq.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /* Copyright 2014 Cisco Systems, Inc. All rights reserved. */
  3. #ifndef _VNIC_WQ_H_
  4. #define _VNIC_WQ_H_
  5. #include <linux/pci.h>
  6. #include "vnic_dev.h"
  7. #include "vnic_cq.h"
  8. /* Work queue control */
  9. struct vnic_wq_ctrl {
  10. u64 ring_base; /* 0x00 */
  11. u32 ring_size; /* 0x08 */
  12. u32 pad0;
  13. u32 posted_index; /* 0x10 */
  14. u32 pad1;
  15. u32 cq_index; /* 0x18 */
  16. u32 pad2;
  17. u32 enable; /* 0x20 */
  18. u32 pad3;
  19. u32 running; /* 0x28 */
  20. u32 pad4;
  21. u32 fetch_index; /* 0x30 */
  22. u32 pad5;
  23. u32 dca_value; /* 0x38 */
  24. u32 pad6;
  25. u32 error_interrupt_enable; /* 0x40 */
  26. u32 pad7;
  27. u32 error_interrupt_offset; /* 0x48 */
  28. u32 pad8;
  29. u32 error_status; /* 0x50 */
  30. u32 pad9;
  31. };
  32. struct vnic_wq_buf {
  33. struct vnic_wq_buf *next;
  34. dma_addr_t dma_addr;
  35. void *os_buf;
  36. unsigned int len;
  37. unsigned int index;
  38. int sop;
  39. void *desc;
  40. };
  41. /* Break the vnic_wq_buf allocations into blocks of 64 entries */
  42. #define VNIC_WQ_BUF_MIN_BLK_ENTRIES 32
  43. #define VNIC_WQ_BUF_DFLT_BLK_ENTRIES 64
  44. #define VNIC_WQ_BUF_BLK_ENTRIES(entries) \
  45. ((unsigned int)(entries < VNIC_WQ_BUF_DFLT_BLK_ENTRIES) ? \
  46. VNIC_WQ_BUF_MIN_BLK_ENTRIES : VNIC_WQ_BUF_DFLT_BLK_ENTRIES)
  47. #define VNIC_WQ_BUF_BLK_SZ \
  48. (VNIC_WQ_BUF_DFLT_BLK_ENTRIES * sizeof(struct vnic_wq_buf))
  49. #define VNIC_WQ_BUF_BLKS_NEEDED(entries) \
  50. DIV_ROUND_UP(entries, VNIC_WQ_BUF_DFLT_BLK_ENTRIES)
  51. #define VNIC_WQ_BUF_BLKS_NEEDED(entries) \
  52. DIV_ROUND_UP(entries, VNIC_WQ_BUF_DFLT_BLK_ENTRIES)
  53. #define VNIC_WQ_BUF_BLKS_MAX VNIC_WQ_BUF_BLKS_NEEDED(4096)
  54. struct vnic_wq {
  55. unsigned int index;
  56. struct vnic_dev *vdev;
  57. struct vnic_wq_ctrl __iomem *ctrl; /* memory-mapped */
  58. struct vnic_dev_ring ring;
  59. struct vnic_wq_buf *bufs[VNIC_WQ_BUF_BLKS_MAX];
  60. struct vnic_wq_buf *to_use;
  61. struct vnic_wq_buf *to_clean;
  62. unsigned int pkts_outstanding;
  63. };
  64. static inline unsigned int svnic_wq_desc_avail(struct vnic_wq *wq)
  65. {
  66. /* how many does SW own? */
  67. return wq->ring.desc_avail;
  68. }
  69. static inline unsigned int svnic_wq_desc_used(struct vnic_wq *wq)
  70. {
  71. /* how many does HW own? */
  72. return wq->ring.desc_count - wq->ring.desc_avail - 1;
  73. }
  74. static inline void *svnic_wq_next_desc(struct vnic_wq *wq)
  75. {
  76. return wq->to_use->desc;
  77. }
  78. static inline void svnic_wq_post(struct vnic_wq *wq,
  79. void *os_buf, dma_addr_t dma_addr,
  80. unsigned int len, int sop, int eop)
  81. {
  82. struct vnic_wq_buf *buf = wq->to_use;
  83. buf->sop = sop;
  84. buf->os_buf = eop ? os_buf : NULL;
  85. buf->dma_addr = dma_addr;
  86. buf->len = len;
  87. buf = buf->next;
  88. if (eop) {
  89. /* Adding write memory barrier prevents compiler and/or CPU
  90. * reordering, thus avoiding descriptor posting before
  91. * descriptor is initialized. Otherwise, hardware can read
  92. * stale descriptor fields.
  93. */
  94. wmb();
  95. iowrite32(buf->index, &wq->ctrl->posted_index);
  96. }
  97. wq->to_use = buf;
  98. wq->ring.desc_avail--;
  99. }
  100. static inline void svnic_wq_service(struct vnic_wq *wq,
  101. struct cq_desc *cq_desc, u16 completed_index,
  102. void (*buf_service)(struct vnic_wq *wq,
  103. struct cq_desc *cq_desc, struct vnic_wq_buf *buf, void *opaque),
  104. void *opaque)
  105. {
  106. struct vnic_wq_buf *buf;
  107. buf = wq->to_clean;
  108. while (1) {
  109. (*buf_service)(wq, cq_desc, buf, opaque);
  110. wq->ring.desc_avail++;
  111. wq->to_clean = buf->next;
  112. if (buf->index == completed_index)
  113. break;
  114. buf = wq->to_clean;
  115. }
  116. }
  117. void svnic_wq_free(struct vnic_wq *wq);
  118. int svnic_wq_alloc(struct vnic_dev *vdev, struct vnic_wq *wq,
  119. unsigned int index, unsigned int desc_count, unsigned int desc_size);
  120. int vnic_wq_devcmd2_alloc(struct vnic_dev *vdev, struct vnic_wq *wq,
  121. unsigned int desc_count, unsigned int desc_size);
  122. void vnic_wq_init_start(struct vnic_wq *wq, unsigned int cq_index,
  123. unsigned int fetch_index, unsigned int post_index,
  124. unsigned int error_interrupt_enable,
  125. unsigned int error_interrupt_offset);
  126. void svnic_wq_init(struct vnic_wq *wq, unsigned int cq_index,
  127. unsigned int error_interrupt_enable,
  128. unsigned int error_interrupt_offset);
  129. unsigned int svnic_wq_error_status(struct vnic_wq *wq);
  130. void svnic_wq_enable(struct vnic_wq *wq);
  131. int svnic_wq_disable(struct vnic_wq *wq);
  132. void svnic_wq_clean(struct vnic_wq *wq,
  133. void (*buf_clean)(struct vnic_wq *wq, struct vnic_wq_buf *buf));
  134. #endif /* _VNIC_WQ_H_ */