virtio_config.h 11 KB

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