vnic_rq.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright 2008 Cisco Systems, Inc. All rights reserved.
  4. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  5. */
  6. #ifndef _VNIC_RQ_H_
  7. #define _VNIC_RQ_H_
  8. #include <linux/pci.h>
  9. #include "vnic_dev.h"
  10. #include "vnic_cq.h"
  11. /*
  12. * These defines avoid symbol clash between fnic and enic (Cisco 10G Eth
  13. * Driver) when both are built with CONFIG options =y
  14. */
  15. #define vnic_rq_desc_avail fnic_rq_desc_avail
  16. #define vnic_rq_desc_used fnic_rq_desc_used
  17. #define vnic_rq_next_desc fnic_rq_next_desc
  18. #define vnic_rq_next_index fnic_rq_next_index
  19. #define vnic_rq_next_buf_index fnic_rq_next_buf_index
  20. #define vnic_rq_post fnic_rq_post
  21. #define vnic_rq_posting_soon fnic_rq_posting_soon
  22. #define vnic_rq_return_descs fnic_rq_return_descs
  23. #define vnic_rq_service fnic_rq_service
  24. #define vnic_rq_fill fnic_rq_fill
  25. #define vnic_rq_free fnic_rq_free
  26. #define vnic_rq_alloc fnic_rq_alloc
  27. #define vnic_rq_init fnic_rq_init
  28. #define vnic_rq_error_status fnic_rq_error_status
  29. #define vnic_rq_enable fnic_rq_enable
  30. #define vnic_rq_disable fnic_rq_disable
  31. #define vnic_rq_clean fnic_rq_clean
  32. /* Receive queue control */
  33. struct vnic_rq_ctrl {
  34. u64 ring_base; /* 0x00 */
  35. u32 ring_size; /* 0x08 */
  36. u32 pad0;
  37. u32 posted_index; /* 0x10 */
  38. u32 pad1;
  39. u32 cq_index; /* 0x18 */
  40. u32 pad2;
  41. u32 enable; /* 0x20 */
  42. u32 pad3;
  43. u32 running; /* 0x28 */
  44. u32 pad4;
  45. u32 fetch_index; /* 0x30 */
  46. u32 pad5;
  47. u32 error_interrupt_enable; /* 0x38 */
  48. u32 pad6;
  49. u32 error_interrupt_offset; /* 0x40 */
  50. u32 pad7;
  51. u32 error_status; /* 0x48 */
  52. u32 pad8;
  53. u32 dropped_packet_count; /* 0x50 */
  54. u32 pad9;
  55. u32 dropped_packet_count_rc; /* 0x58 */
  56. u32 pad10;
  57. };
  58. /* Break the vnic_rq_buf allocations into blocks of 64 entries */
  59. #define VNIC_RQ_BUF_BLK_ENTRIES 64
  60. #define VNIC_RQ_BUF_BLK_SZ \
  61. (VNIC_RQ_BUF_BLK_ENTRIES * sizeof(struct vnic_rq_buf))
  62. #define VNIC_RQ_BUF_BLKS_NEEDED(entries) \
  63. DIV_ROUND_UP(entries, VNIC_RQ_BUF_BLK_ENTRIES)
  64. #define VNIC_RQ_BUF_BLKS_MAX VNIC_RQ_BUF_BLKS_NEEDED(4096)
  65. struct vnic_rq_buf {
  66. struct vnic_rq_buf *next;
  67. dma_addr_t dma_addr;
  68. void *os_buf;
  69. unsigned int os_buf_index;
  70. unsigned int len;
  71. unsigned int index;
  72. void *desc;
  73. };
  74. struct vnic_rq {
  75. unsigned int index;
  76. struct vnic_dev *vdev;
  77. struct vnic_rq_ctrl __iomem *ctrl; /* memory-mapped */
  78. struct vnic_dev_ring ring;
  79. struct vnic_rq_buf *bufs[VNIC_RQ_BUF_BLKS_MAX];
  80. struct vnic_rq_buf *to_use;
  81. struct vnic_rq_buf *to_clean;
  82. void *os_buf_head;
  83. unsigned int buf_index;
  84. unsigned int pkts_outstanding;
  85. };
  86. static inline unsigned int vnic_rq_desc_avail(struct vnic_rq *rq)
  87. {
  88. /* how many does SW own? */
  89. return rq->ring.desc_avail;
  90. }
  91. static inline unsigned int vnic_rq_desc_used(struct vnic_rq *rq)
  92. {
  93. /* how many does HW own? */
  94. return rq->ring.desc_count - rq->ring.desc_avail - 1;
  95. }
  96. static inline void *vnic_rq_next_desc(struct vnic_rq *rq)
  97. {
  98. return rq->to_use->desc;
  99. }
  100. static inline unsigned int vnic_rq_next_index(struct vnic_rq *rq)
  101. {
  102. return rq->to_use->index;
  103. }
  104. static inline unsigned int vnic_rq_next_buf_index(struct vnic_rq *rq)
  105. {
  106. return rq->buf_index++;
  107. }
  108. static inline void vnic_rq_post(struct vnic_rq *rq,
  109. void *os_buf, unsigned int os_buf_index,
  110. dma_addr_t dma_addr, unsigned int len)
  111. {
  112. struct vnic_rq_buf *buf = rq->to_use;
  113. buf->os_buf = os_buf;
  114. buf->os_buf_index = os_buf_index;
  115. buf->dma_addr = dma_addr;
  116. buf->len = len;
  117. buf = buf->next;
  118. rq->to_use = buf;
  119. rq->ring.desc_avail--;
  120. /* Move the posted_index every nth descriptor
  121. */
  122. #ifndef VNIC_RQ_RETURN_RATE
  123. #define VNIC_RQ_RETURN_RATE 0xf /* keep 2^n - 1 */
  124. #endif
  125. if ((buf->index & VNIC_RQ_RETURN_RATE) == 0) {
  126. /* Adding write memory barrier prevents compiler and/or CPU
  127. * reordering, thus avoiding descriptor posting before
  128. * descriptor is initialized. Otherwise, hardware can read
  129. * stale descriptor fields.
  130. */
  131. wmb();
  132. iowrite32(buf->index, &rq->ctrl->posted_index);
  133. }
  134. }
  135. static inline int vnic_rq_posting_soon(struct vnic_rq *rq)
  136. {
  137. return (rq->to_use->index & VNIC_RQ_RETURN_RATE) == 0;
  138. }
  139. static inline void vnic_rq_return_descs(struct vnic_rq *rq, unsigned int count)
  140. {
  141. rq->ring.desc_avail += count;
  142. }
  143. enum desc_return_options {
  144. VNIC_RQ_RETURN_DESC,
  145. VNIC_RQ_DEFER_RETURN_DESC,
  146. };
  147. static inline void vnic_rq_service(struct vnic_rq *rq,
  148. struct cq_desc *cq_desc, u16 completed_index,
  149. int desc_return, void (*buf_service)(struct vnic_rq *rq,
  150. struct cq_desc *cq_desc, struct vnic_rq_buf *buf,
  151. int skipped, void *opaque), void *opaque)
  152. {
  153. struct vnic_rq_buf *buf;
  154. int skipped;
  155. buf = rq->to_clean;
  156. while (1) {
  157. skipped = (buf->index != completed_index);
  158. (*buf_service)(rq, cq_desc, buf, skipped, opaque);
  159. if (desc_return == VNIC_RQ_RETURN_DESC)
  160. rq->ring.desc_avail++;
  161. rq->to_clean = buf->next;
  162. if (!skipped)
  163. break;
  164. buf = rq->to_clean;
  165. }
  166. }
  167. static inline int vnic_rq_fill(struct vnic_rq *rq,
  168. int (*buf_fill)(struct vnic_rq *rq))
  169. {
  170. int err;
  171. while (vnic_rq_desc_avail(rq) > 1) {
  172. err = (*buf_fill)(rq);
  173. if (err)
  174. return err;
  175. }
  176. return 0;
  177. }
  178. void vnic_rq_free(struct vnic_rq *rq);
  179. int vnic_rq_alloc(struct vnic_dev *vdev, struct vnic_rq *rq, unsigned int index,
  180. unsigned int desc_count, unsigned int desc_size);
  181. void vnic_rq_init(struct vnic_rq *rq, unsigned int cq_index,
  182. unsigned int error_interrupt_enable,
  183. unsigned int error_interrupt_offset);
  184. unsigned int vnic_rq_error_status(struct vnic_rq *rq);
  185. void vnic_rq_enable(struct vnic_rq *rq);
  186. int vnic_rq_disable(struct vnic_rq *rq);
  187. void vnic_rq_clean(struct vnic_rq *rq,
  188. void (*buf_clean)(struct vnic_rq *rq, struct vnic_rq_buf *buf));
  189. #endif /* _VNIC_RQ_H_ */