mic_common.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2013 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel MIC driver.
  19. *
  20. */
  21. #ifndef __MIC_COMMON_H_
  22. #define __MIC_COMMON_H_
  23. #include <linux/virtio_ring.h>
  24. #ifndef __KERNEL__
  25. #define ALIGN(a, x) (((a) + (x) - 1) & ~((x) - 1))
  26. #define __aligned(x) __attribute__ ((aligned(x)))
  27. #endif
  28. #define mic_aligned_size(x) ALIGN(sizeof(x), 8)
  29. /**
  30. * struct mic_device_desc: Virtio device information shared between the
  31. * virtio driver and userspace backend
  32. *
  33. * @type: Device type: console/network/disk etc. Type 0/-1 terminates.
  34. * @num_vq: Number of virtqueues.
  35. * @feature_len: Number of bytes of feature bits. Multiply by 2: one for
  36. host features and one for guest acknowledgements.
  37. * @config_len: Number of bytes of the config array after virtqueues.
  38. * @status: A status byte, written by the Guest.
  39. * @config: Start of the following variable length config.
  40. */
  41. struct mic_device_desc {
  42. __s8 type;
  43. __u8 num_vq;
  44. __u8 feature_len;
  45. __u8 config_len;
  46. __u8 status;
  47. __u64 config[0];
  48. } __aligned(8);
  49. /**
  50. * struct mic_device_ctrl: Per virtio device information in the device page
  51. * used internally by the host and card side drivers.
  52. *
  53. * @vdev: Used for storing MIC vdev information by the guest.
  54. * @config_change: Set to 1 by host when a config change is requested.
  55. * @vdev_reset: Set to 1 by guest to indicate virtio device has been reset.
  56. * @guest_ack: Set to 1 by guest to ack a command.
  57. * @host_ack: Set to 1 by host to ack a command.
  58. * @used_address_updated: Set to 1 by guest when the used address should be
  59. * updated.
  60. * @c2h_vdev_db: The doorbell number to be used by guest. Set by host.
  61. * @h2c_vdev_db: The doorbell number to be used by host. Set by guest.
  62. */
  63. struct mic_device_ctrl {
  64. __u64 vdev;
  65. __u8 config_change;
  66. __u8 vdev_reset;
  67. __u8 guest_ack;
  68. __u8 host_ack;
  69. __u8 used_address_updated;
  70. __s8 c2h_vdev_db;
  71. __s8 h2c_vdev_db;
  72. } __aligned(8);
  73. /**
  74. * struct mic_bootparam: Virtio device independent information in device page
  75. *
  76. * @magic: A magic value used by the card to ensure it can see the host
  77. * @c2h_shutdown_db: Card to Host shutdown doorbell set by host
  78. * @h2c_shutdown_db: Host to Card shutdown doorbell set by card
  79. * @h2c_config_db: Host to Card Virtio config doorbell set by card
  80. * @shutdown_status: Card shutdown status set by card
  81. * @shutdown_card: Set to 1 by the host when a card shutdown is initiated
  82. */
  83. struct mic_bootparam {
  84. __u32 magic;
  85. __s8 c2h_shutdown_db;
  86. __s8 h2c_shutdown_db;
  87. __s8 h2c_config_db;
  88. __u8 shutdown_status;
  89. __u8 shutdown_card;
  90. } __aligned(8);
  91. /**
  92. * struct mic_device_page: High level representation of the device page
  93. *
  94. * @bootparam: The bootparam structure is used for sharing information and
  95. * status updates between MIC host and card drivers.
  96. * @desc: Array of MIC virtio device descriptors.
  97. */
  98. struct mic_device_page {
  99. struct mic_bootparam bootparam;
  100. struct mic_device_desc desc[0];
  101. };
  102. /**
  103. * struct mic_vqconfig: This is how we expect the device configuration field
  104. * for a virtqueue to be laid out in config space.
  105. *
  106. * @address: Guest/MIC physical address of the virtio ring
  107. * (avail and desc rings)
  108. * @used_address: Guest/MIC physical address of the used ring
  109. * @num: The number of entries in the virtio_ring
  110. */
  111. struct mic_vqconfig {
  112. __u64 address;
  113. __u64 used_address;
  114. __u16 num;
  115. } __aligned(8);
  116. /*
  117. * The alignment to use between consumer and producer parts of vring.
  118. * This is pagesize for historical reasons.
  119. */
  120. #define MIC_VIRTIO_RING_ALIGN 4096
  121. #define MIC_MAX_VRINGS 4
  122. #define MIC_VRING_ENTRIES 128
  123. /*
  124. * Max vring entries (power of 2) to ensure desc and avail rings
  125. * fit in a single page
  126. */
  127. #define MIC_MAX_VRING_ENTRIES 128
  128. /**
  129. * Max size of the desc block in bytes: includes:
  130. * - struct mic_device_desc
  131. * - struct mic_vqconfig (num_vq of these)
  132. * - host and guest features
  133. * - virtio device config space
  134. */
  135. #define MIC_MAX_DESC_BLK_SIZE 256
  136. /**
  137. * struct _mic_vring_info - Host vring info exposed to userspace backend
  138. * for the avail index and magic for the card.
  139. *
  140. * @avail_idx: host avail idx
  141. * @magic: A magic debug cookie.
  142. */
  143. struct _mic_vring_info {
  144. __u16 avail_idx;
  145. int magic;
  146. };
  147. /**
  148. * struct mic_vring - Vring information.
  149. *
  150. * @vr: The virtio ring.
  151. * @info: Host vring information exposed to the userspace backend for the
  152. * avail index and magic for the card.
  153. * @va: The va for the buffer allocated for vr and info.
  154. * @len: The length of the buffer required for allocating vr and info.
  155. */
  156. struct mic_vring {
  157. struct vring vr;
  158. struct _mic_vring_info *info;
  159. void *va;
  160. int len;
  161. };
  162. #define mic_aligned_desc_size(d) ALIGN(mic_desc_size(d), 8)
  163. #ifndef INTEL_MIC_CARD
  164. static inline unsigned mic_desc_size(const struct mic_device_desc *desc)
  165. {
  166. return mic_aligned_size(*desc)
  167. + desc->num_vq * mic_aligned_size(struct mic_vqconfig)
  168. + desc->feature_len * 2
  169. + desc->config_len;
  170. }
  171. static inline struct mic_vqconfig *
  172. mic_vq_config(const struct mic_device_desc *desc)
  173. {
  174. return (struct mic_vqconfig *)(desc + 1);
  175. }
  176. static inline __u8 *mic_vq_features(const struct mic_device_desc *desc)
  177. {
  178. return (__u8 *)(mic_vq_config(desc) + desc->num_vq);
  179. }
  180. static inline __u8 *mic_vq_configspace(const struct mic_device_desc *desc)
  181. {
  182. return mic_vq_features(desc) + desc->feature_len * 2;
  183. }
  184. static inline unsigned mic_total_desc_size(struct mic_device_desc *desc)
  185. {
  186. return mic_aligned_desc_size(desc) +
  187. mic_aligned_size(struct mic_device_ctrl);
  188. }
  189. #endif
  190. /* Device page size */
  191. #define MIC_DP_SIZE 4096
  192. #define MIC_MAGIC 0xc0ffee00
  193. /**
  194. * enum mic_states - MIC states.
  195. */
  196. enum mic_states {
  197. MIC_OFFLINE = 0,
  198. MIC_ONLINE,
  199. MIC_SHUTTING_DOWN,
  200. MIC_RESET_FAILED,
  201. MIC_SUSPENDING,
  202. MIC_SUSPENDED,
  203. MIC_LAST
  204. };
  205. /**
  206. * enum mic_status - MIC status reported by card after
  207. * a host or card initiated shutdown or a card crash.
  208. */
  209. enum mic_status {
  210. MIC_NOP = 0,
  211. MIC_CRASHED,
  212. MIC_HALTED,
  213. MIC_POWER_OFF,
  214. MIC_RESTART,
  215. MIC_STATUS_LAST
  216. };
  217. #endif