virtio_config.h 11 KB

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