virtio_config.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_test_bit - helper to test feature bits. For use by transports.
  77. * Devices should normally use virtio_has_feature,
  78. * which includes more checks.
  79. * @vdev: the device
  80. * @fbit: the feature bit
  81. */
  82. static inline bool __virtio_test_bit(const struct virtio_device *vdev,
  83. unsigned int fbit)
  84. {
  85. /* Did you forget to fix assumptions on max features? */
  86. if (__builtin_constant_p(fbit))
  87. BUILD_BUG_ON(fbit >= 32);
  88. else
  89. BUG_ON(fbit >= 32);
  90. return vdev->features & BIT(fbit);
  91. }
  92. /**
  93. * __virtio_set_bit - helper to set feature bits. For use by transports.
  94. * @vdev: the device
  95. * @fbit: the feature bit
  96. */
  97. static inline void __virtio_set_bit(struct virtio_device *vdev,
  98. unsigned int fbit)
  99. {
  100. /* Did you forget to fix assumptions on max features? */
  101. if (__builtin_constant_p(fbit))
  102. BUILD_BUG_ON(fbit >= 32);
  103. else
  104. BUG_ON(fbit >= 32);
  105. vdev->features |= BIT(fbit);
  106. }
  107. /**
  108. * __virtio_clear_bit - helper to clear feature bits. For use by transports.
  109. * @vdev: the device
  110. * @fbit: the feature bit
  111. */
  112. static inline void __virtio_clear_bit(struct virtio_device *vdev,
  113. unsigned int fbit)
  114. {
  115. /* Did you forget to fix assumptions on max features? */
  116. if (__builtin_constant_p(fbit))
  117. BUILD_BUG_ON(fbit >= 32);
  118. else
  119. BUG_ON(fbit >= 32);
  120. vdev->features &= ~BIT(fbit);
  121. }
  122. /**
  123. * virtio_has_feature - helper to determine if this device has this feature.
  124. * @vdev: the device
  125. * @fbit: the feature bit
  126. */
  127. static inline bool virtio_has_feature(const struct virtio_device *vdev,
  128. unsigned int fbit)
  129. {
  130. if (fbit < VIRTIO_TRANSPORT_F_START)
  131. virtio_check_driver_offered_feature(vdev, fbit);
  132. return __virtio_test_bit(vdev, fbit);
  133. }
  134. static inline
  135. struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
  136. vq_callback_t *c, const char *n)
  137. {
  138. vq_callback_t *callbacks[] = { c };
  139. const char *names[] = { n };
  140. struct virtqueue *vq;
  141. int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
  142. if (err < 0)
  143. return ERR_PTR(err);
  144. return vq;
  145. }
  146. /**
  147. * virtio_device_ready - enable vq use in probe function
  148. * @vdev: the device
  149. *
  150. * Driver must call this to use vqs in the probe function.
  151. *
  152. * Note: vqs are enabled automatically after probe returns.
  153. */
  154. static inline
  155. void virtio_device_ready(struct virtio_device *dev)
  156. {
  157. unsigned status = dev->config->get_status(dev);
  158. BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
  159. dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
  160. }
  161. static inline
  162. const char *virtio_bus_name(struct virtio_device *vdev)
  163. {
  164. if (!vdev->config->bus_name)
  165. return "virtio";
  166. return vdev->config->bus_name(vdev);
  167. }
  168. /**
  169. * virtqueue_set_affinity - setting affinity for a virtqueue
  170. * @vq: the virtqueue
  171. * @cpu: the cpu no.
  172. *
  173. * Pay attention the function are best-effort: the affinity hint may not be set
  174. * due to config support, irq type and sharing.
  175. *
  176. */
  177. static inline
  178. int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
  179. {
  180. struct virtio_device *vdev = vq->vdev;
  181. if (vdev->config->set_vq_affinity)
  182. return vdev->config->set_vq_affinity(vq, cpu);
  183. return 0;
  184. }
  185. /* Config space accessors. */
  186. #define virtio_cread(vdev, structname, member, ptr) \
  187. do { \
  188. /* Must match the member's type, and be integer */ \
  189. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  190. (*ptr) = 1; \
  191. \
  192. switch (sizeof(*ptr)) { \
  193. case 1: \
  194. *(ptr) = virtio_cread8(vdev, \
  195. offsetof(structname, member)); \
  196. break; \
  197. case 2: \
  198. *(ptr) = virtio_cread16(vdev, \
  199. offsetof(structname, member)); \
  200. break; \
  201. case 4: \
  202. *(ptr) = virtio_cread32(vdev, \
  203. offsetof(structname, member)); \
  204. break; \
  205. case 8: \
  206. *(ptr) = virtio_cread64(vdev, \
  207. offsetof(structname, member)); \
  208. break; \
  209. default: \
  210. BUG(); \
  211. } \
  212. } while(0)
  213. /* Config space accessors. */
  214. #define virtio_cwrite(vdev, structname, member, ptr) \
  215. do { \
  216. /* Must match the member's type, and be integer */ \
  217. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  218. BUG_ON((*ptr) == 1); \
  219. \
  220. switch (sizeof(*ptr)) { \
  221. case 1: \
  222. virtio_cwrite8(vdev, \
  223. offsetof(structname, member), \
  224. *(ptr)); \
  225. break; \
  226. case 2: \
  227. virtio_cwrite16(vdev, \
  228. offsetof(structname, member), \
  229. *(ptr)); \
  230. break; \
  231. case 4: \
  232. virtio_cwrite32(vdev, \
  233. offsetof(structname, member), \
  234. *(ptr)); \
  235. break; \
  236. case 8: \
  237. virtio_cwrite64(vdev, \
  238. offsetof(structname, member), \
  239. *(ptr)); \
  240. break; \
  241. default: \
  242. BUG(); \
  243. } \
  244. } while(0)
  245. static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
  246. {
  247. u8 ret;
  248. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  249. return ret;
  250. }
  251. static inline void virtio_cread_bytes(struct virtio_device *vdev,
  252. unsigned int offset,
  253. void *buf, size_t len)
  254. {
  255. vdev->config->get(vdev, offset, buf, len);
  256. }
  257. static inline void virtio_cwrite8(struct virtio_device *vdev,
  258. unsigned int offset, u8 val)
  259. {
  260. vdev->config->set(vdev, offset, &val, sizeof(val));
  261. }
  262. static inline u16 virtio_cread16(struct virtio_device *vdev,
  263. unsigned int offset)
  264. {
  265. u16 ret;
  266. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  267. return ret;
  268. }
  269. static inline void virtio_cwrite16(struct virtio_device *vdev,
  270. unsigned int offset, u16 val)
  271. {
  272. vdev->config->set(vdev, offset, &val, sizeof(val));
  273. }
  274. static inline u32 virtio_cread32(struct virtio_device *vdev,
  275. unsigned int offset)
  276. {
  277. u32 ret;
  278. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  279. return ret;
  280. }
  281. static inline void virtio_cwrite32(struct virtio_device *vdev,
  282. unsigned int offset, u32 val)
  283. {
  284. vdev->config->set(vdev, offset, &val, sizeof(val));
  285. }
  286. static inline u64 virtio_cread64(struct virtio_device *vdev,
  287. unsigned int offset)
  288. {
  289. u64 ret;
  290. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  291. return ret;
  292. }
  293. static inline void virtio_cwrite64(struct virtio_device *vdev,
  294. unsigned int offset, u64 val)
  295. {
  296. vdev->config->set(vdev, offset, &val, sizeof(val));
  297. }
  298. /* Conditional config space accessors. */
  299. #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
  300. ({ \
  301. int _r = 0; \
  302. if (!virtio_has_feature(vdev, fbit)) \
  303. _r = -ENOENT; \
  304. else \
  305. virtio_cread((vdev), structname, member, ptr); \
  306. _r; \
  307. })
  308. #endif /* _LINUX_VIRTIO_CONFIG_H */