virtio_config.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #ifndef _LINUX_VIRTIO_CONFIG_H
  2. #define _LINUX_VIRTIO_CONFIG_H
  3. #include <linux/err.h>
  4. #include <linux/bug.h>
  5. #include <linux/virtio.h>
  6. #include <uapi/linux/virtio_config.h>
  7. /**
  8. * virtio_config_ops - operations for configuring a virtio device
  9. * @get: read the value of a configuration field
  10. * vdev: the virtio_device
  11. * offset: the offset of the configuration field
  12. * buf: the buffer to write the field value into.
  13. * len: the length of the buffer
  14. * @set: write the value of a configuration field
  15. * vdev: the virtio_device
  16. * offset: the offset of the configuration field
  17. * buf: the buffer to read the field value from.
  18. * len: the length of the buffer
  19. * @get_status: read the status byte
  20. * vdev: the virtio_device
  21. * Returns the status byte
  22. * @set_status: write the status byte
  23. * vdev: the virtio_device
  24. * status: the new status byte
  25. * @reset: reset the device
  26. * vdev: the virtio device
  27. * After this, status and feature negotiation must be done again
  28. * Device must not be reset from its vq/config callbacks, or in
  29. * parallel with being added/removed.
  30. * @find_vqs: find virtqueues and instantiate them.
  31. * vdev: the virtio_device
  32. * nvqs: the number of virtqueues to find
  33. * vqs: on success, includes new virtqueues
  34. * callbacks: array of callbacks, for each virtqueue
  35. * include a NULL entry for vqs that do not need a callback
  36. * names: array of virtqueue names (mainly for debugging)
  37. * include a NULL entry for vqs unused by driver
  38. * Returns 0 on success or error status
  39. * @del_vqs: free virtqueues found by find_vqs().
  40. * @get_features: get the array of feature bits for this device.
  41. * vdev: the virtio_device
  42. * Returns the first 32 feature bits (all we currently need).
  43. * @finalize_features: confirm what device features we'll be using.
  44. * vdev: the virtio_device
  45. * This gives the final feature bits for the device: it can change
  46. * the dev->feature bits if it wants.
  47. * @bus_name: return the bus name associated with the device
  48. * vdev: the virtio_device
  49. * This returns a pointer to the bus name a la pci_name from which
  50. * the caller can then copy.
  51. * @set_vq_affinity: set the affinity for a virtqueue.
  52. */
  53. typedef void vq_callback_t(struct virtqueue *);
  54. struct virtio_config_ops {
  55. void (*get)(struct virtio_device *vdev, unsigned offset,
  56. void *buf, unsigned len);
  57. void (*set)(struct virtio_device *vdev, unsigned offset,
  58. const void *buf, unsigned len);
  59. u8 (*get_status)(struct virtio_device *vdev);
  60. void (*set_status)(struct virtio_device *vdev, u8 status);
  61. void (*reset)(struct virtio_device *vdev);
  62. int (*find_vqs)(struct virtio_device *, unsigned nvqs,
  63. struct virtqueue *vqs[],
  64. vq_callback_t *callbacks[],
  65. const char *names[]);
  66. void (*del_vqs)(struct virtio_device *);
  67. u32 (*get_features)(struct virtio_device *vdev);
  68. void (*finalize_features)(struct virtio_device *vdev);
  69. const char *(*bus_name)(struct virtio_device *vdev);
  70. int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
  71. };
  72. /* If driver didn't advertise the feature, it will never appear. */
  73. void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  74. unsigned int fbit);
  75. /**
  76. * virtio_has_feature - helper to determine if this device has this feature.
  77. * @vdev: the device
  78. * @fbit: the feature bit
  79. */
  80. static inline bool virtio_has_feature(const struct virtio_device *vdev,
  81. unsigned int fbit)
  82. {
  83. /* Did you forget to fix assumptions on max features? */
  84. if (__builtin_constant_p(fbit))
  85. BUILD_BUG_ON(fbit >= 32);
  86. else
  87. BUG_ON(fbit >= 32);
  88. if (fbit < VIRTIO_TRANSPORT_F_START)
  89. virtio_check_driver_offered_feature(vdev, fbit);
  90. return test_bit(fbit, vdev->features);
  91. }
  92. static inline
  93. struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
  94. vq_callback_t *c, const char *n)
  95. {
  96. vq_callback_t *callbacks[] = { c };
  97. const char *names[] = { n };
  98. struct virtqueue *vq;
  99. int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
  100. if (err < 0)
  101. return ERR_PTR(err);
  102. return vq;
  103. }
  104. static inline
  105. const char *virtio_bus_name(struct virtio_device *vdev)
  106. {
  107. if (!vdev->config->bus_name)
  108. return "virtio";
  109. return vdev->config->bus_name(vdev);
  110. }
  111. /**
  112. * virtqueue_set_affinity - setting affinity for a virtqueue
  113. * @vq: the virtqueue
  114. * @cpu: the cpu no.
  115. *
  116. * Pay attention the function are best-effort: the affinity hint may not be set
  117. * due to config support, irq type and sharing.
  118. *
  119. */
  120. static inline
  121. int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
  122. {
  123. struct virtio_device *vdev = vq->vdev;
  124. if (vdev->config->set_vq_affinity)
  125. return vdev->config->set_vq_affinity(vq, cpu);
  126. return 0;
  127. }
  128. /* Config space accessors. */
  129. #define virtio_cread(vdev, structname, member, ptr) \
  130. do { \
  131. /* Must match the member's type, and be integer */ \
  132. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  133. (*ptr) = 1; \
  134. \
  135. switch (sizeof(*ptr)) { \
  136. case 1: \
  137. *(ptr) = virtio_cread8(vdev, \
  138. offsetof(structname, member)); \
  139. break; \
  140. case 2: \
  141. *(ptr) = virtio_cread16(vdev, \
  142. offsetof(structname, member)); \
  143. break; \
  144. case 4: \
  145. *(ptr) = virtio_cread32(vdev, \
  146. offsetof(structname, member)); \
  147. break; \
  148. case 8: \
  149. *(ptr) = virtio_cread64(vdev, \
  150. offsetof(structname, member)); \
  151. break; \
  152. default: \
  153. BUG(); \
  154. } \
  155. } while(0)
  156. /* Config space accessors. */
  157. #define virtio_cwrite(vdev, structname, member, ptr) \
  158. do { \
  159. /* Must match the member's type, and be integer */ \
  160. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  161. BUG_ON((*ptr) == 1); \
  162. \
  163. switch (sizeof(*ptr)) { \
  164. case 1: \
  165. virtio_cwrite8(vdev, \
  166. offsetof(structname, member), \
  167. *(ptr)); \
  168. break; \
  169. case 2: \
  170. virtio_cwrite16(vdev, \
  171. offsetof(structname, member), \
  172. *(ptr)); \
  173. break; \
  174. case 4: \
  175. virtio_cwrite32(vdev, \
  176. offsetof(structname, member), \
  177. *(ptr)); \
  178. break; \
  179. case 8: \
  180. virtio_cwrite64(vdev, \
  181. offsetof(structname, member), \
  182. *(ptr)); \
  183. break; \
  184. default: \
  185. BUG(); \
  186. } \
  187. } while(0)
  188. static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
  189. {
  190. u8 ret;
  191. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  192. return ret;
  193. }
  194. static inline void virtio_cread_bytes(struct virtio_device *vdev,
  195. unsigned int offset,
  196. void *buf, size_t len)
  197. {
  198. vdev->config->get(vdev, offset, buf, len);
  199. }
  200. static inline void virtio_cwrite8(struct virtio_device *vdev,
  201. unsigned int offset, u8 val)
  202. {
  203. vdev->config->set(vdev, offset, &val, sizeof(val));
  204. }
  205. static inline u16 virtio_cread16(struct virtio_device *vdev,
  206. unsigned int offset)
  207. {
  208. u16 ret;
  209. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  210. return ret;
  211. }
  212. static inline void virtio_cwrite16(struct virtio_device *vdev,
  213. unsigned int offset, u16 val)
  214. {
  215. vdev->config->set(vdev, offset, &val, sizeof(val));
  216. }
  217. static inline u32 virtio_cread32(struct virtio_device *vdev,
  218. unsigned int offset)
  219. {
  220. u32 ret;
  221. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  222. return ret;
  223. }
  224. static inline void virtio_cwrite32(struct virtio_device *vdev,
  225. unsigned int offset, u32 val)
  226. {
  227. vdev->config->set(vdev, offset, &val, sizeof(val));
  228. }
  229. static inline u64 virtio_cread64(struct virtio_device *vdev,
  230. unsigned int offset)
  231. {
  232. u64 ret;
  233. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  234. return ret;
  235. }
  236. static inline void virtio_cwrite64(struct virtio_device *vdev,
  237. unsigned int offset, u64 val)
  238. {
  239. vdev->config->set(vdev, offset, &val, sizeof(val));
  240. }
  241. /* Conditional config space accessors. */
  242. #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
  243. ({ \
  244. int _r = 0; \
  245. if (!virtio_has_feature(vdev, fbit)) \
  246. _r = -ENOENT; \
  247. else \
  248. virtio_cread((vdev), structname, member, ptr); \
  249. _r; \
  250. })
  251. #endif /* _LINUX_VIRTIO_CONFIG_H */