virtio_config.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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. /**
  105. * virtio_device_ready - enable vq use in probe function
  106. * @vdev: the device
  107. *
  108. * Driver must call this to use vqs in the probe function.
  109. *
  110. * Note: vqs are enabled automatically after probe returns.
  111. */
  112. static inline
  113. void virtio_device_ready(struct virtio_device *dev)
  114. {
  115. unsigned status = dev->config->get_status(dev);
  116. BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
  117. dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
  118. }
  119. static inline
  120. const char *virtio_bus_name(struct virtio_device *vdev)
  121. {
  122. if (!vdev->config->bus_name)
  123. return "virtio";
  124. return vdev->config->bus_name(vdev);
  125. }
  126. /**
  127. * virtqueue_set_affinity - setting affinity for a virtqueue
  128. * @vq: the virtqueue
  129. * @cpu: the cpu no.
  130. *
  131. * Pay attention the function are best-effort: the affinity hint may not be set
  132. * due to config support, irq type and sharing.
  133. *
  134. */
  135. static inline
  136. int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
  137. {
  138. struct virtio_device *vdev = vq->vdev;
  139. if (vdev->config->set_vq_affinity)
  140. return vdev->config->set_vq_affinity(vq, cpu);
  141. return 0;
  142. }
  143. /* Config space accessors. */
  144. #define virtio_cread(vdev, structname, member, ptr) \
  145. do { \
  146. /* Must match the member's type, and be integer */ \
  147. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  148. (*ptr) = 1; \
  149. \
  150. switch (sizeof(*ptr)) { \
  151. case 1: \
  152. *(ptr) = virtio_cread8(vdev, \
  153. offsetof(structname, member)); \
  154. break; \
  155. case 2: \
  156. *(ptr) = virtio_cread16(vdev, \
  157. offsetof(structname, member)); \
  158. break; \
  159. case 4: \
  160. *(ptr) = virtio_cread32(vdev, \
  161. offsetof(structname, member)); \
  162. break; \
  163. case 8: \
  164. *(ptr) = virtio_cread64(vdev, \
  165. offsetof(structname, member)); \
  166. break; \
  167. default: \
  168. BUG(); \
  169. } \
  170. } while(0)
  171. /* Config space accessors. */
  172. #define virtio_cwrite(vdev, structname, member, ptr) \
  173. do { \
  174. /* Must match the member's type, and be integer */ \
  175. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  176. BUG_ON((*ptr) == 1); \
  177. \
  178. switch (sizeof(*ptr)) { \
  179. case 1: \
  180. virtio_cwrite8(vdev, \
  181. offsetof(structname, member), \
  182. *(ptr)); \
  183. break; \
  184. case 2: \
  185. virtio_cwrite16(vdev, \
  186. offsetof(structname, member), \
  187. *(ptr)); \
  188. break; \
  189. case 4: \
  190. virtio_cwrite32(vdev, \
  191. offsetof(structname, member), \
  192. *(ptr)); \
  193. break; \
  194. case 8: \
  195. virtio_cwrite64(vdev, \
  196. offsetof(structname, member), \
  197. *(ptr)); \
  198. break; \
  199. default: \
  200. BUG(); \
  201. } \
  202. } while(0)
  203. static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
  204. {
  205. u8 ret;
  206. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  207. return ret;
  208. }
  209. static inline void virtio_cread_bytes(struct virtio_device *vdev,
  210. unsigned int offset,
  211. void *buf, size_t len)
  212. {
  213. vdev->config->get(vdev, offset, buf, len);
  214. }
  215. static inline void virtio_cwrite8(struct virtio_device *vdev,
  216. unsigned int offset, u8 val)
  217. {
  218. vdev->config->set(vdev, offset, &val, sizeof(val));
  219. }
  220. static inline u16 virtio_cread16(struct virtio_device *vdev,
  221. unsigned int offset)
  222. {
  223. u16 ret;
  224. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  225. return ret;
  226. }
  227. static inline void virtio_cwrite16(struct virtio_device *vdev,
  228. unsigned int offset, u16 val)
  229. {
  230. vdev->config->set(vdev, offset, &val, sizeof(val));
  231. }
  232. static inline u32 virtio_cread32(struct virtio_device *vdev,
  233. unsigned int offset)
  234. {
  235. u32 ret;
  236. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  237. return ret;
  238. }
  239. static inline void virtio_cwrite32(struct virtio_device *vdev,
  240. unsigned int offset, u32 val)
  241. {
  242. vdev->config->set(vdev, offset, &val, sizeof(val));
  243. }
  244. static inline u64 virtio_cread64(struct virtio_device *vdev,
  245. unsigned int offset)
  246. {
  247. u64 ret;
  248. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  249. return ret;
  250. }
  251. static inline void virtio_cwrite64(struct virtio_device *vdev,
  252. unsigned int offset, u64 val)
  253. {
  254. vdev->config->set(vdev, offset, &val, sizeof(val));
  255. }
  256. /* Conditional config space accessors. */
  257. #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
  258. ({ \
  259. int _r = 0; \
  260. if (!virtio_has_feature(vdev, fbit)) \
  261. _r = -ENOENT; \
  262. else \
  263. virtio_cread((vdev), structname, member, ptr); \
  264. _r; \
  265. })
  266. #endif /* _LINUX_VIRTIO_CONFIG_H */