You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

netfront.h 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #ifndef _NETFRONT_H
  2. #define _NETFRONT_H
  3. /** @file
  4. *
  5. * Xen netfront driver
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <ipxe/xen.h>
  10. #include <xen/io/netif.h>
  11. /** Number of transmit ring entries */
  12. #define NETFRONT_NUM_TX_DESC 16
  13. /** Number of receive ring entries */
  14. #define NETFRONT_NUM_RX_DESC 8
  15. /** Grant reference indices */
  16. enum netfront_ref_index {
  17. /** Transmit ring grant reference index */
  18. NETFRONT_REF_TX_RING = 0,
  19. /** Transmit descriptor grant reference base index */
  20. NETFRONT_REF_TX_BASE,
  21. /** Receive ring grant reference index */
  22. NETFRONT_REF_RX_RING = ( NETFRONT_REF_TX_BASE + NETFRONT_NUM_TX_DESC ),
  23. /** Receive descriptor grant reference base index */
  24. NETFRONT_REF_RX_BASE,
  25. /** Total number of grant references required */
  26. NETFRONT_REF_COUNT = ( NETFRONT_REF_RX_BASE + NETFRONT_NUM_RX_DESC )
  27. };
  28. /** A netfront descriptor ring */
  29. struct netfront_ring {
  30. /** Shared ring */
  31. union {
  32. /** Transmit shared ring */
  33. netif_tx_sring_t *tx;
  34. /** Receive shared ring */
  35. netif_rx_sring_t *rx;
  36. /** Raw pointer */
  37. void *raw;
  38. } sring;
  39. /** Shared ring grant reference key */
  40. const char *ref_key;
  41. /** Shared ring grant reference */
  42. grant_ref_t ref;
  43. /** Maximum number of used descriptors */
  44. size_t count;
  45. /** I/O buffers, indexed by buffer ID */
  46. struct io_buffer **iobufs;
  47. /** I/O buffer grant references, indexed by buffer ID */
  48. grant_ref_t *refs;
  49. /** Buffer ID ring */
  50. uint8_t *ids;
  51. /** Buffer ID ring producer counter */
  52. unsigned int id_prod;
  53. /** Buffer ID ring consumer counter */
  54. unsigned int id_cons;
  55. };
  56. /**
  57. * Initialise descriptor ring
  58. *
  59. * @v ring Descriptor ring
  60. * @v ref_key Shared ring grant reference key
  61. * @v ref Shared ring grant reference
  62. * @v count Maxium number of used descriptors
  63. * @v iobufs I/O buffers
  64. * @v refs I/O buffer grant references
  65. * @v ids Buffer IDs
  66. */
  67. static inline __attribute__ (( always_inline )) void
  68. netfront_init_ring ( struct netfront_ring *ring, const char *ref_key,
  69. grant_ref_t ref, unsigned int count,
  70. struct io_buffer **iobufs, grant_ref_t *refs,
  71. uint8_t *ids ) {
  72. ring->ref_key = ref_key;
  73. ring->ref = ref;
  74. ring->count = count;
  75. ring->iobufs = iobufs;
  76. ring->refs = refs;
  77. ring->ids = ids;
  78. }
  79. /**
  80. * Check whether or not descriptor ring is full
  81. *
  82. * @v ring Descriptor ring
  83. * @v is_full Ring is full
  84. */
  85. static inline __attribute__ (( always_inline )) int
  86. netfront_ring_is_full ( struct netfront_ring *ring ) {
  87. unsigned int fill_level;
  88. fill_level = ( ring->id_prod - ring->id_cons );
  89. assert ( fill_level <= ring->count );
  90. return ( fill_level >= ring->count );
  91. }
  92. /**
  93. * Check whether or not descriptor ring is empty
  94. *
  95. * @v ring Descriptor ring
  96. * @v is_empty Ring is empty
  97. */
  98. static inline __attribute__ (( always_inline )) int
  99. netfront_ring_is_empty ( struct netfront_ring *ring ) {
  100. return ( ring->id_prod == ring->id_cons );
  101. }
  102. /** A netfront NIC */
  103. struct netfront_nic {
  104. /** Xen device */
  105. struct xen_device *xendev;
  106. /** Grant references */
  107. grant_ref_t refs[NETFRONT_REF_COUNT];
  108. /** Transmit ring */
  109. struct netfront_ring tx;
  110. /** Transmit front ring */
  111. netif_tx_front_ring_t tx_fring;
  112. /** Transmit I/O buffers */
  113. struct io_buffer *tx_iobufs[NETFRONT_NUM_TX_DESC];
  114. /** Transmit I/O buffer IDs */
  115. uint8_t tx_ids[NETFRONT_NUM_TX_DESC];
  116. /** Receive ring */
  117. struct netfront_ring rx;
  118. /** Receive front ring */
  119. netif_rx_front_ring_t rx_fring;
  120. /** Receive I/O buffers */
  121. struct io_buffer *rx_iobufs[NETFRONT_NUM_RX_DESC];
  122. /** Receive I/O buffer IDs */
  123. uint8_t rx_ids[NETFRONT_NUM_RX_DESC];
  124. /** Event channel */
  125. struct evtchn_send event;
  126. };
  127. /** Transmit shared ring field */
  128. #define tx_sring tx.sring.tx
  129. /** Receive shared ring field */
  130. #define rx_sring rx.sring.rx
  131. #endif /* _NETFRONT_H */