virtio_config.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. /**
  93. * virtio_config_val - look for a feature and get a virtio config entry.
  94. * @vdev: the virtio device
  95. * @fbit: the feature bit
  96. * @offset: the type to search for.
  97. * @v: a pointer to the value to fill in.
  98. *
  99. * The return value is -ENOENT if the feature doesn't exist. Otherwise
  100. * the config value is copied into whatever is pointed to by v. */
  101. #define virtio_config_val(vdev, fbit, offset, v) \
  102. virtio_config_buf((vdev), (fbit), (offset), (v), sizeof(*v))
  103. #define virtio_config_val_len(vdev, fbit, offset, v, len) \
  104. virtio_config_buf((vdev), (fbit), (offset), (v), (len))
  105. static inline int virtio_config_buf(struct virtio_device *vdev,
  106. unsigned int fbit,
  107. unsigned int offset,
  108. void *buf, unsigned len)
  109. {
  110. if (!virtio_has_feature(vdev, fbit))
  111. return -ENOENT;
  112. vdev->config->get(vdev, offset, buf, len);
  113. return 0;
  114. }
  115. static inline
  116. struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
  117. vq_callback_t *c, const char *n)
  118. {
  119. vq_callback_t *callbacks[] = { c };
  120. const char *names[] = { n };
  121. struct virtqueue *vq;
  122. int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
  123. if (err < 0)
  124. return ERR_PTR(err);
  125. return vq;
  126. }
  127. static inline
  128. const char *virtio_bus_name(struct virtio_device *vdev)
  129. {
  130. if (!vdev->config->bus_name)
  131. return "virtio";
  132. return vdev->config->bus_name(vdev);
  133. }
  134. /**
  135. * virtqueue_set_affinity - setting affinity for a virtqueue
  136. * @vq: the virtqueue
  137. * @cpu: the cpu no.
  138. *
  139. * Pay attention the function are best-effort: the affinity hint may not be set
  140. * due to config support, irq type and sharing.
  141. *
  142. */
  143. static inline
  144. int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
  145. {
  146. struct virtio_device *vdev = vq->vdev;
  147. if (vdev->config->set_vq_affinity)
  148. return vdev->config->set_vq_affinity(vq, cpu);
  149. return 0;
  150. }
  151. /* Config space accessors. */
  152. #define virtio_cread(vdev, structname, member, ptr) \
  153. do { \
  154. /* Must match the member's type, and be integer */ \
  155. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  156. (*ptr) = 1; \
  157. \
  158. switch (sizeof(*ptr)) { \
  159. case 1: \
  160. *(ptr) = virtio_cread8(vdev, \
  161. offsetof(structname, member)); \
  162. break; \
  163. case 2: \
  164. *(ptr) = virtio_cread16(vdev, \
  165. offsetof(structname, member)); \
  166. break; \
  167. case 4: \
  168. *(ptr) = virtio_cread32(vdev, \
  169. offsetof(structname, member)); \
  170. break; \
  171. case 8: \
  172. *(ptr) = virtio_cread64(vdev, \
  173. offsetof(structname, member)); \
  174. break; \
  175. default: \
  176. BUG(); \
  177. } \
  178. } while(0)
  179. /* Config space accessors. */
  180. #define virtio_cwrite(vdev, structname, member, ptr) \
  181. do { \
  182. /* Must match the member's type, and be integer */ \
  183. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  184. BUG_ON((*ptr) == 1); \
  185. \
  186. switch (sizeof(*ptr)) { \
  187. case 1: \
  188. virtio_cwrite8(vdev, \
  189. offsetof(structname, member), \
  190. *(ptr)); \
  191. break; \
  192. case 2: \
  193. virtio_cwrite16(vdev, \
  194. offsetof(structname, member), \
  195. *(ptr)); \
  196. break; \
  197. case 4: \
  198. virtio_cwrite32(vdev, \
  199. offsetof(structname, member), \
  200. *(ptr)); \
  201. break; \
  202. case 8: \
  203. virtio_cwrite64(vdev, \
  204. offsetof(structname, member), \
  205. *(ptr)); \
  206. break; \
  207. default: \
  208. BUG(); \
  209. } \
  210. } while(0)
  211. static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
  212. {
  213. u8 ret;
  214. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  215. return ret;
  216. }
  217. static inline void virtio_cread_bytes(struct virtio_device *vdev,
  218. unsigned int offset,
  219. void *buf, size_t len)
  220. {
  221. vdev->config->get(vdev, offset, buf, len);
  222. }
  223. static inline void virtio_cwrite8(struct virtio_device *vdev,
  224. unsigned int offset, u8 val)
  225. {
  226. vdev->config->set(vdev, offset, &val, sizeof(val));
  227. }
  228. static inline u16 virtio_cread16(struct virtio_device *vdev,
  229. unsigned int offset)
  230. {
  231. u16 ret;
  232. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  233. return ret;
  234. }
  235. static inline void virtio_cwrite16(struct virtio_device *vdev,
  236. unsigned int offset, u16 val)
  237. {
  238. vdev->config->set(vdev, offset, &val, sizeof(val));
  239. }
  240. static inline u32 virtio_cread32(struct virtio_device *vdev,
  241. unsigned int offset)
  242. {
  243. u32 ret;
  244. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  245. return ret;
  246. }
  247. static inline void virtio_cwrite32(struct virtio_device *vdev,
  248. unsigned int offset, u32 val)
  249. {
  250. vdev->config->set(vdev, offset, &val, sizeof(val));
  251. }
  252. static inline u64 virtio_cread64(struct virtio_device *vdev,
  253. unsigned int offset)
  254. {
  255. u64 ret;
  256. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  257. return ret;
  258. }
  259. static inline void virtio_cwrite64(struct virtio_device *vdev,
  260. unsigned int offset, u64 val)
  261. {
  262. vdev->config->set(vdev, offset, &val, sizeof(val));
  263. }
  264. /* Conditional config space accessors. */
  265. #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
  266. ({ \
  267. int _r = 0; \
  268. if (!virtio_has_feature(vdev, fbit)) \
  269. _r = -ENOENT; \
  270. else \
  271. virtio_cread((vdev), structname, member, ptr); \
  272. _r; \
  273. })
  274. #endif /* _LINUX_VIRTIO_CONFIG_H */