virtio_config.h 13 KB

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