virtio_config.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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 * const 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. /**
  141. * virtio_has_iommu_quirk - determine whether this device has the iommu quirk
  142. * @vdev: the device
  143. */
  144. static inline bool virtio_has_iommu_quirk(const struct virtio_device *vdev)
  145. {
  146. /*
  147. * Note the reverse polarity of the quirk feature (compared to most
  148. * other features), this is for compatibility with legacy systems.
  149. */
  150. return !virtio_has_feature(vdev, VIRTIO_F_IOMMU_PLATFORM);
  151. }
  152. static inline
  153. struct virtqueue *virtio_find_single_vq(struct virtio_device *vdev,
  154. vq_callback_t *c, const char *n)
  155. {
  156. vq_callback_t *callbacks[] = { c };
  157. const char *names[] = { n };
  158. struct virtqueue *vq;
  159. int err = vdev->config->find_vqs(vdev, 1, &vq, callbacks, names);
  160. if (err < 0)
  161. return ERR_PTR(err);
  162. return vq;
  163. }
  164. /**
  165. * virtio_device_ready - enable vq use in probe function
  166. * @vdev: the device
  167. *
  168. * Driver must call this to use vqs in the probe function.
  169. *
  170. * Note: vqs are enabled automatically after probe returns.
  171. */
  172. static inline
  173. void virtio_device_ready(struct virtio_device *dev)
  174. {
  175. unsigned status = dev->config->get_status(dev);
  176. BUG_ON(status & VIRTIO_CONFIG_S_DRIVER_OK);
  177. dev->config->set_status(dev, status | VIRTIO_CONFIG_S_DRIVER_OK);
  178. }
  179. static inline
  180. const char *virtio_bus_name(struct virtio_device *vdev)
  181. {
  182. if (!vdev->config->bus_name)
  183. return "virtio";
  184. return vdev->config->bus_name(vdev);
  185. }
  186. /**
  187. * virtqueue_set_affinity - setting affinity for a virtqueue
  188. * @vq: the virtqueue
  189. * @cpu: the cpu no.
  190. *
  191. * Pay attention the function are best-effort: the affinity hint may not be set
  192. * due to config support, irq type and sharing.
  193. *
  194. */
  195. static inline
  196. int virtqueue_set_affinity(struct virtqueue *vq, int cpu)
  197. {
  198. struct virtio_device *vdev = vq->vdev;
  199. if (vdev->config->set_vq_affinity)
  200. return vdev->config->set_vq_affinity(vq, cpu);
  201. return 0;
  202. }
  203. static inline bool virtio_is_little_endian(struct virtio_device *vdev)
  204. {
  205. return virtio_has_feature(vdev, VIRTIO_F_VERSION_1) ||
  206. virtio_legacy_is_little_endian();
  207. }
  208. /* Memory accessors */
  209. static inline u16 virtio16_to_cpu(struct virtio_device *vdev, __virtio16 val)
  210. {
  211. return __virtio16_to_cpu(virtio_is_little_endian(vdev), val);
  212. }
  213. static inline __virtio16 cpu_to_virtio16(struct virtio_device *vdev, u16 val)
  214. {
  215. return __cpu_to_virtio16(virtio_is_little_endian(vdev), val);
  216. }
  217. static inline u32 virtio32_to_cpu(struct virtio_device *vdev, __virtio32 val)
  218. {
  219. return __virtio32_to_cpu(virtio_is_little_endian(vdev), val);
  220. }
  221. static inline __virtio32 cpu_to_virtio32(struct virtio_device *vdev, u32 val)
  222. {
  223. return __cpu_to_virtio32(virtio_is_little_endian(vdev), val);
  224. }
  225. static inline u64 virtio64_to_cpu(struct virtio_device *vdev, __virtio64 val)
  226. {
  227. return __virtio64_to_cpu(virtio_is_little_endian(vdev), val);
  228. }
  229. static inline __virtio64 cpu_to_virtio64(struct virtio_device *vdev, u64 val)
  230. {
  231. return __cpu_to_virtio64(virtio_is_little_endian(vdev), val);
  232. }
  233. /* Config space accessors. */
  234. #define virtio_cread(vdev, structname, member, ptr) \
  235. do { \
  236. /* Must match the member's type, and be integer */ \
  237. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  238. (*ptr) = 1; \
  239. \
  240. switch (sizeof(*ptr)) { \
  241. case 1: \
  242. *(ptr) = virtio_cread8(vdev, \
  243. offsetof(structname, member)); \
  244. break; \
  245. case 2: \
  246. *(ptr) = virtio_cread16(vdev, \
  247. offsetof(structname, member)); \
  248. break; \
  249. case 4: \
  250. *(ptr) = virtio_cread32(vdev, \
  251. offsetof(structname, member)); \
  252. break; \
  253. case 8: \
  254. *(ptr) = virtio_cread64(vdev, \
  255. offsetof(structname, member)); \
  256. break; \
  257. default: \
  258. BUG(); \
  259. } \
  260. } while(0)
  261. /* Config space accessors. */
  262. #define virtio_cwrite(vdev, structname, member, ptr) \
  263. do { \
  264. /* Must match the member's type, and be integer */ \
  265. if (!typecheck(typeof((((structname*)0)->member)), *(ptr))) \
  266. BUG_ON((*ptr) == 1); \
  267. \
  268. switch (sizeof(*ptr)) { \
  269. case 1: \
  270. virtio_cwrite8(vdev, \
  271. offsetof(structname, member), \
  272. *(ptr)); \
  273. break; \
  274. case 2: \
  275. virtio_cwrite16(vdev, \
  276. offsetof(structname, member), \
  277. *(ptr)); \
  278. break; \
  279. case 4: \
  280. virtio_cwrite32(vdev, \
  281. offsetof(structname, member), \
  282. *(ptr)); \
  283. break; \
  284. case 8: \
  285. virtio_cwrite64(vdev, \
  286. offsetof(structname, member), \
  287. *(ptr)); \
  288. break; \
  289. default: \
  290. BUG(); \
  291. } \
  292. } while(0)
  293. /* Read @count fields, @bytes each. */
  294. static inline void __virtio_cread_many(struct virtio_device *vdev,
  295. unsigned int offset,
  296. void *buf, size_t count, size_t bytes)
  297. {
  298. u32 old, gen = vdev->config->generation ?
  299. vdev->config->generation(vdev) : 0;
  300. int i;
  301. do {
  302. old = gen;
  303. for (i = 0; i < count; i++)
  304. vdev->config->get(vdev, offset + bytes * i,
  305. buf + i * bytes, bytes);
  306. gen = vdev->config->generation ?
  307. vdev->config->generation(vdev) : 0;
  308. } while (gen != old);
  309. }
  310. static inline void virtio_cread_bytes(struct virtio_device *vdev,
  311. unsigned int offset,
  312. void *buf, size_t len)
  313. {
  314. __virtio_cread_many(vdev, offset, buf, len, 1);
  315. }
  316. static inline u8 virtio_cread8(struct virtio_device *vdev, unsigned int offset)
  317. {
  318. u8 ret;
  319. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  320. return ret;
  321. }
  322. static inline void virtio_cwrite8(struct virtio_device *vdev,
  323. unsigned int offset, u8 val)
  324. {
  325. vdev->config->set(vdev, offset, &val, sizeof(val));
  326. }
  327. static inline u16 virtio_cread16(struct virtio_device *vdev,
  328. unsigned int offset)
  329. {
  330. u16 ret;
  331. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  332. return virtio16_to_cpu(vdev, (__force __virtio16)ret);
  333. }
  334. static inline void virtio_cwrite16(struct virtio_device *vdev,
  335. unsigned int offset, u16 val)
  336. {
  337. val = (__force u16)cpu_to_virtio16(vdev, val);
  338. vdev->config->set(vdev, offset, &val, sizeof(val));
  339. }
  340. static inline u32 virtio_cread32(struct virtio_device *vdev,
  341. unsigned int offset)
  342. {
  343. u32 ret;
  344. vdev->config->get(vdev, offset, &ret, sizeof(ret));
  345. return virtio32_to_cpu(vdev, (__force __virtio32)ret);
  346. }
  347. static inline void virtio_cwrite32(struct virtio_device *vdev,
  348. unsigned int offset, u32 val)
  349. {
  350. val = (__force u32)cpu_to_virtio32(vdev, val);
  351. vdev->config->set(vdev, offset, &val, sizeof(val));
  352. }
  353. static inline u64 virtio_cread64(struct virtio_device *vdev,
  354. unsigned int offset)
  355. {
  356. u64 ret;
  357. __virtio_cread_many(vdev, offset, &ret, 1, sizeof(ret));
  358. return virtio64_to_cpu(vdev, (__force __virtio64)ret);
  359. }
  360. static inline void virtio_cwrite64(struct virtio_device *vdev,
  361. unsigned int offset, u64 val)
  362. {
  363. val = (__force u64)cpu_to_virtio64(vdev, val);
  364. vdev->config->set(vdev, offset, &val, sizeof(val));
  365. }
  366. /* Conditional config space accessors. */
  367. #define virtio_cread_feature(vdev, fbit, structname, member, ptr) \
  368. ({ \
  369. int _r = 0; \
  370. if (!virtio_has_feature(vdev, fbit)) \
  371. _r = -ENOENT; \
  372. else \
  373. virtio_cread((vdev), structname, member, ptr); \
  374. _r; \
  375. })
  376. #endif /* _LINUX_VIRTIO_CONFIG_H */