virtio_config.h 12 KB

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