virtio_config.h 12 KB

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