virtio_config.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 <linux/virtio_byteorder.h>
  7. #include <uapi/linux/virtio_config.h>
  8. /**
  9. * virtio_config_ops - operations for configuring a virtio device
  10. * @get: read the value of a configuration field
  11. * vdev: the virtio_device
  12. * offset: the offset of the configuration field
  13. * buf: the buffer to write the field value into.
  14. * len: the length of the buffer
  15. * @set: write the value of a configuration field
  16. * vdev: the virtio_device
  17. * offset: the offset of the configuration field
  18. * buf: the buffer to read the field value from.
  19. * len: the length of the buffer
  20. * @get_status: read the status byte
  21. * vdev: the virtio_device
  22. * Returns the status byte
  23. * @set_status: write the status byte
  24. * vdev: the virtio_device
  25. * status: the new status byte
  26. * @reset: reset the device
  27. * vdev: the virtio device
  28. * After this, status and feature negotiation must be done again
  29. * Device must not be reset from its vq/config callbacks, or in
  30. * parallel with being added/removed.
  31. * @find_vqs: find virtqueues and instantiate them.
  32. * vdev: the virtio_device
  33. * nvqs: the number of virtqueues to find
  34. * vqs: on success, includes new virtqueues
  35. * callbacks: array of callbacks, for each virtqueue
  36. * include a NULL entry for vqs that do not need a callback
  37. * names: array of virtqueue names (mainly for debugging)
  38. * include a NULL entry for vqs unused by driver
  39. * Returns 0 on success or error status
  40. * @del_vqs: free virtqueues found by find_vqs().
  41. * @get_features: get the array of feature bits for this device.
  42. * vdev: the virtio_device
  43. * Returns the first 32 feature bits (all we currently need).
  44. * @finalize_features: confirm what device features we'll be using.
  45. * vdev: the virtio_device
  46. * This gives the final feature bits for the device: it can change
  47. * the dev->feature bits if it wants.
  48. * @bus_name: return the bus name associated with the device
  49. * vdev: the virtio_device
  50. * This returns a pointer to the bus name a la pci_name from which
  51. * the caller can then copy.
  52. * @set_vq_affinity: set the affinity for a virtqueue.
  53. */
  54. typedef void vq_callback_t(struct virtqueue *);
  55. struct virtio_config_ops {
  56. void (*get)(struct virtio_device *vdev, unsigned offset,
  57. void *buf, unsigned len);
  58. void (*set)(struct virtio_device *vdev, unsigned offset,
  59. const void *buf, unsigned len);
  60. u8 (*get_status)(struct virtio_device *vdev);
  61. void (*set_status)(struct virtio_device *vdev, u8 status);
  62. void (*reset)(struct virtio_device *vdev);
  63. int (*find_vqs)(struct virtio_device *, unsigned nvqs,
  64. struct virtqueue *vqs[],
  65. vq_callback_t *callbacks[],
  66. const char *names[]);
  67. void (*del_vqs)(struct virtio_device *);
  68. u64 (*get_features)(struct virtio_device *vdev);
  69. void (*finalize_features)(struct virtio_device *vdev);
  70. const char *(*bus_name)(struct virtio_device *vdev);
  71. int (*set_vq_affinity)(struct virtqueue *vq, int cpu);
  72. };
  73. /* If driver didn't advertise the feature, it will never appear. */
  74. void virtio_check_driver_offered_feature(const struct virtio_device *vdev,
  75. unsigned int fbit);
  76. /**
  77. * __virtio_test_bit - helper to test feature bits. For use by transports.
  78. * Devices should normally use virtio_has_feature,
  79. * which includes more checks.
  80. * @vdev: the device
  81. * @fbit: the feature bit
  82. */
  83. static inline bool __virtio_test_bit(const struct virtio_device *vdev,
  84. unsigned int fbit)
  85. {
  86. /* Did you forget to fix assumptions on max features? */
  87. if (__builtin_constant_p(fbit))
  88. BUILD_BUG_ON(fbit >= 64);
  89. else
  90. BUG_ON(fbit >= 64);
  91. return vdev->features & BIT_ULL(fbit);
  92. }
  93. /**
  94. * __virtio_set_bit - helper to set feature bits. For use by transports.
  95. * @vdev: the device
  96. * @fbit: the feature bit
  97. */
  98. static inline void __virtio_set_bit(struct virtio_device *vdev,
  99. unsigned int fbit)
  100. {
  101. /* Did you forget to fix assumptions on max features? */
  102. if (__builtin_constant_p(fbit))
  103. BUILD_BUG_ON(fbit >= 64);
  104. else
  105. BUG_ON(fbit >= 64);
  106. vdev->features |= BIT_ULL(fbit);
  107. }
  108. /**
  109. * __virtio_clear_bit - helper to clear feature bits. For use by transports.
  110. * @vdev: the device
  111. * @fbit: the feature bit
  112. */
  113. static inline void __virtio_clear_bit(struct virtio_device *vdev,
  114. unsigned int fbit)
  115. {
  116. /* Did you forget to fix assumptions on max features? */
  117. if (__builtin_constant_p(fbit))
  118. BUILD_BUG_ON(fbit >= 64);
  119. else
  120. BUG_ON(fbit >= 64);
  121. vdev->features &= ~BIT_ULL(fbit);
  122. }
  123. /**
  124. * virtio_has_feature - helper to determine if this device has this feature.
  125. * @vdev: the device
  126. * @fbit: the feature bit
  127. */
  128. static inline bool virtio_has_feature(const struct virtio_device *vdev,
  129. unsigned int fbit)
  130. {
  131. if (fbit < VIRTIO_TRANSPORT_F_START)
  132. virtio_check_driver_offered_feature(vdev, fbit);
  133. return __virtio_test_bit(vdev, fbit);
  134. }
  135. static inline
  136. struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
  137. vq_callback_t *c, const char *n)
  138. {
  139. vq_callback_t *callbacks[] = { c };
  140. const char *names[] = { n };
  141. struct virtqueue *vq;
  142. int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
  143. if (err < 0)
  144. return ERR_PTR(err);
  145. return vq;
  146. }
  147. /**
  148. * virtio_device_ready - enable vq use in probe function
  149. * @vdev: the device
  150. *
  151. * Driver must call this to use vqs in the probe function.
  152. *
  153. * Note: vqs are enabled automatically after probe returns.
  154. */
  155. static inline
  156. void virtio_device_ready(struct virtio_device *dev)
  157. {
  158. unsigned status = dev->config->get_status(dev);
  159. BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
  160. dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
  161. }
  162. static inline
  163. const char *virtio_bus_name(struct virtio_device *vdev)
  164. {
  165. if (!vdev->config->bus_name)
  166. return "virtio";
  167. return vdev->config->bus_name(vdev);
  168. }
  169. /**
  170. * virtqueue_set_affinity - setting affinity for a virtqueue
  171. * @vq: the virtqueue
  172. * @cpu: the cpu no.
  173. *
  174. * Pay attention the function are best-effort: the affinity hint may not be set
  175. * due to config support, irq type and sharing.
  176. *
  177. */
  178. static inline
  179. int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
  180. {
  181. struct virtio_device *vdev = vq->vdev;
  182. if (vdev->config->set_vq_affinity)
  183. return vdev->config->set_vq_affinity(vq, cpu);
  184. return 0;
  185. }
  186. /* Memory accessors */
  187. static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
  188. {
  189. return __virtio16_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
  190. }
  191. static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
  192. {
  193. return __cpu_to_virtio16(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
  194. }
  195. static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
  196. {
  197. return __virtio32_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
  198. }
  199. static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
  200. {
  201. return __cpu_to_virtio32(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
  202. }
  203. static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
  204. {
  205. return __virtio64_to_cpu(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
  206. }
  207. static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
  208. {
  209. return __cpu_to_virtio64(virtio_has_feature(vdev, VIRTIO_F_VERSION_1), val);
  210. }
  211. /* Config space accessors. */
  212. #define virtio_cread(vdev, structname, member, ptr) \
  213. do { \
  214. /* Must match the member's type, and be integer */ \
  215. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  216. (*ptr) = 1; \
  217. \
  218. switch (sizeof(*ptr)) { \
  219. case 1: \
  220. *(ptr) = virtio_cread8(vdev, \
  221. offsetof(structname, member)); \
  222. break; \
  223. case 2: \
  224. *(ptr) = virtio_cread16(vdev, \
  225. offsetof(structname, member)); \
  226. break; \
  227. case 4: \
  228. *(ptr) = virtio_cread32(vdev, \
  229. offsetof(structname, member)); \
  230. break; \
  231. case 8: \
  232. *(ptr) = virtio_cread64(vdev, \
  233. offsetof(structname, member)); \
  234. break; \
  235. default: \
  236. BUG(); \
  237. } \
  238. } while(0)
  239. /* Config space accessors. */
  240. #define virtio_cwrite(vdev, structname, member, ptr) \
  241. do { \
  242. /* Must match the member's type, and be integer */ \
  243. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  244. BUG_ON((*ptr) == 1); \
  245. \
  246. switch (sizeof(*ptr)) { \
  247. case 1: \
  248. virtio_cwrite8(vdev, \
  249. offsetof(structname, member), \
  250. *(ptr)); \
  251. break; \
  252. case 2: \
  253. virtio_cwrite16(vdev, \
  254. offsetof(structname, member), \
  255. *(ptr)); \
  256. break; \
  257. case 4: \
  258. virtio_cwrite32(vdev, \
  259. offsetof(structname, member), \
  260. *(ptr)); \
  261. break; \
  262. case 8: \
  263. virtio_cwrite64(vdev, \
  264. offsetof(structname, member), \
  265. *(ptr)); \
  266. break; \
  267. default: \
  268. BUG(); \
  269. } \
  270. } while(0)
  271. static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
  272. {
  273. u8 ret;
  274. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  275. return ret;
  276. }
  277. static inline void virtio_cread_bytes(struct virtio_device *vdev,
  278. unsigned int offset,
  279. void *buf, size_t len)
  280. {
  281. vdev->config->get(vdev, offset, buf, len);
  282. }
  283. static inline void virtio_cwrite8(struct virtio_device *vdev,
  284. unsigned int offset, u8 val)
  285. {
  286. vdev->config->set(vdev, offset, &val, sizeof(val));
  287. }
  288. static inline u16 virtio_cread16(struct virtio_device *vdev,
  289. unsigned int offset)
  290. {
  291. u16 ret;
  292. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  293. return ret;
  294. }
  295. static inline void virtio_cwrite16(struct virtio_device *vdev,
  296. unsigned int offset, u16 val)
  297. {
  298. vdev->config->set(vdev, offset, &val, sizeof(val));
  299. }
  300. static inline u32 virtio_cread32(struct virtio_device *vdev,
  301. unsigned int offset)
  302. {
  303. u32 ret;
  304. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  305. return ret;
  306. }
  307. static inline void virtio_cwrite32(struct virtio_device *vdev,
  308. unsigned int offset, u32 val)
  309. {
  310. vdev->config->set(vdev, offset, &val, sizeof(val));
  311. }
  312. static inline u64 virtio_cread64(struct virtio_device *vdev,
  313. unsigned int offset)
  314. {
  315. u64 ret;
  316. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  317. return ret;
  318. }
  319. static inline void virtio_cwrite64(struct virtio_device *vdev,
  320. unsigned int offset, u64 val)
  321. {
  322. vdev->config->set(vdev, offset, &val, sizeof(val));
  323. }
  324. /* Conditional config space accessors. */
  325. #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
  326. ({ \
  327. int _r = 0; \
  328. if (!virtio_has_feature(vdev, fbit)) \
  329. _r = -ENOENT; \
  330. else \
  331. virtio_cread((vdev), structname, member, ptr); \
  332. _r; \
  333. })
  334. #endif /* _LINUX_VIRTIO_CONFIG_H */