vdpa.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_VDPA_H
  3. #define _LINUX_VDPA_H
  4. #include <linux/kernel.h>
  5. #include <linux/device.h>
  6. #include <linux/interrupt.h>
  7. #include <linux/vhost_iotlb.h>
  8. #include <linux/virtio_net.h>
  9. #include <linux/if_ether.h>
  10. /**
  11. * struct vdpa_calllback - vDPA callback definition.
  12. * @callback: interrupt callback function
  13. * @private: the data passed to the callback function
  14. */
  15. struct vdpa_callback {
  16. irqreturn_t (*callback)(void *data);
  17. void *private;
  18. };
  19. /**
  20. * struct vdpa_notification_area - vDPA notification area
  21. * @addr: base address of the notification area
  22. * @size: size of the notification area
  23. */
  24. struct vdpa_notification_area {
  25. resource_size_t addr;
  26. resource_size_t size;
  27. };
  28. /**
  29. * struct vdpa_vq_state_split - vDPA split virtqueue state
  30. * @avail_index: available index
  31. */
  32. struct vdpa_vq_state_split {
  33. u16 avail_index;
  34. };
  35. /**
  36. * struct vdpa_vq_state_packed - vDPA packed virtqueue state
  37. * @last_avail_counter: last driver ring wrap counter observed by device
  38. * @last_avail_idx: device available index
  39. * @last_used_counter: device ring wrap counter
  40. * @last_used_idx: used index
  41. */
  42. struct vdpa_vq_state_packed {
  43. u16 last_avail_counter:1;
  44. u16 last_avail_idx:15;
  45. u16 last_used_counter:1;
  46. u16 last_used_idx:15;
  47. };
  48. struct vdpa_vq_state {
  49. union {
  50. struct vdpa_vq_state_split split;
  51. struct vdpa_vq_state_packed packed;
  52. };
  53. };
  54. struct vdpa_mgmt_dev;
  55. /**
  56. * struct vdpa_device - representation of a vDPA device
  57. * @dev: underlying device
  58. * @dma_dev: the actual device that is performing DMA
  59. * @driver_override: driver name to force a match; do not set directly,
  60. * because core frees it; use driver_set_override() to
  61. * set or clear it.
  62. * @config: the configuration ops for this device.
  63. * @cf_lock: Protects get and set access to configuration layout.
  64. * @index: device index
  65. * @features_valid: were features initialized? for legacy guests
  66. * @ngroups: the number of virtqueue groups
  67. * @nas: the number of address spaces
  68. * @use_va: indicate whether virtual address must be used by this device
  69. * @nvqs: maximum number of supported virtqueues
  70. * @mdev: management device pointer; caller must setup when registering device as part
  71. * of dev_add() mgmtdev ops callback before invoking _vdpa_register_device().
  72. */
  73. struct vdpa_device {
  74. struct device dev;
  75. struct device *dma_dev;
  76. const char *driver_override;
  77. const struct vdpa_config_ops *config;
  78. struct rw_semaphore cf_lock; /* Protects get/set config */
  79. unsigned int index;
  80. bool features_valid;
  81. bool use_va;
  82. u32 nvqs;
  83. struct vdpa_mgmt_dev *mdev;
  84. unsigned int ngroups;
  85. unsigned int nas;
  86. };
  87. /**
  88. * struct vdpa_iova_range - the IOVA range support by the device
  89. * @first: start of the IOVA range
  90. * @last: end of the IOVA range
  91. */
  92. struct vdpa_iova_range {
  93. u64 first;
  94. u64 last;
  95. };
  96. struct vdpa_dev_set_config {
  97. u64 device_features;
  98. struct {
  99. u8 mac[ETH_ALEN];
  100. u16 mtu;
  101. u16 max_vq_pairs;
  102. } net;
  103. u64 mask;
  104. };
  105. /**
  106. * Corresponding file area for device memory mapping
  107. * @file: vma->vm_file for the mapping
  108. * @offset: mapping offset in the vm_file
  109. */
  110. struct vdpa_map_file {
  111. struct file *file;
  112. u64 offset;
  113. };
  114. /**
  115. * struct vdpa_config_ops - operations for configuring a vDPA device.
  116. * Note: vDPA device drivers are required to implement all of the
  117. * operations unless it is mentioned to be optional in the following
  118. * list.
  119. *
  120. * @set_vq_address: Set the address of virtqueue
  121. * @vdev: vdpa device
  122. * @idx: virtqueue index
  123. * @desc_area: address of desc area
  124. * @driver_area: address of driver area
  125. * @device_area: address of device area
  126. * Returns integer: success (0) or error (< 0)
  127. * @set_vq_num: Set the size of virtqueue
  128. * @vdev: vdpa device
  129. * @idx: virtqueue index
  130. * @num: the size of virtqueue
  131. * @kick_vq: Kick the virtqueue
  132. * @vdev: vdpa device
  133. * @idx: virtqueue index
  134. * @set_vq_cb: Set the interrupt callback function for
  135. * a virtqueue
  136. * @vdev: vdpa device
  137. * @idx: virtqueue index
  138. * @cb: virtio-vdev interrupt callback structure
  139. * @set_vq_ready: Set ready status for a virtqueue
  140. * @vdev: vdpa device
  141. * @idx: virtqueue index
  142. * @ready: ready (true) not ready(false)
  143. * @get_vq_ready: Get ready status for a virtqueue
  144. * @vdev: vdpa device
  145. * @idx: virtqueue index
  146. * Returns boolean: ready (true) or not (false)
  147. * @set_vq_state: Set the state for a virtqueue
  148. * @vdev: vdpa device
  149. * @idx: virtqueue index
  150. * @state: pointer to set virtqueue state (last_avail_idx)
  151. * Returns integer: success (0) or error (< 0)
  152. * @get_vq_state: Get the state for a virtqueue
  153. * @vdev: vdpa device
  154. * @idx: virtqueue index
  155. * @state: pointer to returned state (last_avail_idx)
  156. * @get_vq_notification: Get the notification area for a virtqueue (optional)
  157. * @vdev: vdpa device
  158. * @idx: virtqueue index
  159. * Returns the notifcation area
  160. * @get_vq_irq: Get the irq number of a virtqueue (optional,
  161. * but must implemented if require vq irq offloading)
  162. * @vdev: vdpa device
  163. * @idx: virtqueue index
  164. * Returns int: irq number of a virtqueue,
  165. * negative number if no irq assigned.
  166. * @get_vq_align: Get the virtqueue align requirement
  167. * for the device
  168. * @vdev: vdpa device
  169. * Returns virtqueue algin requirement
  170. * @get_vq_group: Get the group id for a specific
  171. * virtqueue (optional)
  172. * @vdev: vdpa device
  173. * @idx: virtqueue index
  174. * Returns u32: group id for this virtqueue
  175. * @get_device_features: Get virtio features supported by the device
  176. * @vdev: vdpa device
  177. * Returns the virtio features support by the
  178. * device
  179. * @set_driver_features: Set virtio features supported by the driver
  180. * @vdev: vdpa device
  181. * @features: feature support by the driver
  182. * Returns integer: success (0) or error (< 0)
  183. * @get_driver_features: Get the virtio driver features in action
  184. * @vdev: vdpa device
  185. * Returns the virtio features accepted
  186. * @set_config_cb: Set the config interrupt callback
  187. * @vdev: vdpa device
  188. * @cb: virtio-vdev interrupt callback structure
  189. * @get_vq_num_max: Get the max size of virtqueue
  190. * @vdev: vdpa device
  191. * Returns u16: max size of virtqueue
  192. * @get_vq_num_min: Get the min size of virtqueue (optional)
  193. * @vdev: vdpa device
  194. * Returns u16: min size of virtqueue
  195. * @get_device_id: Get virtio device id
  196. * @vdev: vdpa device
  197. * Returns u32: virtio device id
  198. * @get_vendor_id: Get id for the vendor that provides this device
  199. * @vdev: vdpa device
  200. * Returns u32: virtio vendor id
  201. * @get_status: Get the device status
  202. * @vdev: vdpa device
  203. * Returns u8: virtio device status
  204. * @set_status: Set the device status
  205. * @vdev: vdpa device
  206. * @status: virtio device status
  207. * @reset: Reset device
  208. * @vdev: vdpa device
  209. * Returns integer: success (0) or error (< 0)
  210. * @suspend: Suspend or resume the device (optional)
  211. * @vdev: vdpa device
  212. * Returns integer: success (0) or error (< 0)
  213. * @get_config_size: Get the size of the configuration space includes
  214. * fields that are conditional on feature bits.
  215. * @vdev: vdpa device
  216. * Returns size_t: configuration size
  217. * @get_config: Read from device specific configuration space
  218. * @vdev: vdpa device
  219. * @offset: offset from the beginning of
  220. * configuration space
  221. * @buf: buffer used to read to
  222. * @len: the length to read from
  223. * configuration space
  224. * @set_config: Write to device specific configuration space
  225. * @vdev: vdpa device
  226. * @offset: offset from the beginning of
  227. * configuration space
  228. * @buf: buffer used to write from
  229. * @len: the length to write to
  230. * configuration space
  231. * @get_generation: Get device config generation (optional)
  232. * @vdev: vdpa device
  233. * Returns u32: device generation
  234. * @get_iova_range: Get supported iova range (optional)
  235. * @vdev: vdpa device
  236. * Returns the iova range supported by
  237. * the device.
  238. * @set_group_asid: Set address space identifier for a
  239. * virtqueue group (optional)
  240. * @vdev: vdpa device
  241. * @group: virtqueue group
  242. * @asid: address space id for this group
  243. * Returns integer: success (0) or error (< 0)
  244. * @set_map: Set device memory mapping (optional)
  245. * Needed for device that using device
  246. * specific DMA translation (on-chip IOMMU)
  247. * @vdev: vdpa device
  248. * @asid: address space identifier
  249. * @iotlb: vhost memory mapping to be
  250. * used by the vDPA
  251. * Returns integer: success (0) or error (< 0)
  252. * @dma_map: Map an area of PA to IOVA (optional)
  253. * Needed for device that using device
  254. * specific DMA translation (on-chip IOMMU)
  255. * and preferring incremental map.
  256. * @vdev: vdpa device
  257. * @asid: address space identifier
  258. * @iova: iova to be mapped
  259. * @size: size of the area
  260. * @pa: physical address for the map
  261. * @perm: device access permission (VHOST_MAP_XX)
  262. * Returns integer: success (0) or error (< 0)
  263. * @dma_unmap: Unmap an area of IOVA (optional but
  264. * must be implemented with dma_map)
  265. * Needed for device that using device
  266. * specific DMA translation (on-chip IOMMU)
  267. * and preferring incremental unmap.
  268. * @vdev: vdpa device
  269. * @asid: address space identifier
  270. * @iova: iova to be unmapped
  271. * @size: size of the area
  272. * Returns integer: success (0) or error (< 0)
  273. * @free: Free resources that belongs to vDPA (optional)
  274. * @vdev: vdpa device
  275. */
  276. struct vdpa_config_ops {
  277. /* Virtqueue ops */
  278. int (*set_vq_address)(struct vdpa_device *vdev,
  279. u16 idx, u64 desc_area, u64 driver_area,
  280. u64 device_area);
  281. void (*set_vq_num)(struct vdpa_device *vdev, u16 idx, u32 num);
  282. void (*kick_vq)(struct vdpa_device *vdev, u16 idx);
  283. void (*set_vq_cb)(struct vdpa_device *vdev, u16 idx,
  284. struct vdpa_callback *cb);
  285. void (*set_vq_ready)(struct vdpa_device *vdev, u16 idx, bool ready);
  286. bool (*get_vq_ready)(struct vdpa_device *vdev, u16 idx);
  287. int (*set_vq_state)(struct vdpa_device *vdev, u16 idx,
  288. const struct vdpa_vq_state *state);
  289. int (*get_vq_state)(struct vdpa_device *vdev, u16 idx,
  290. struct vdpa_vq_state *state);
  291. int (*get_vendor_vq_stats)(struct vdpa_device *vdev, u16 idx,
  292. struct sk_buff *msg,
  293. struct netlink_ext_ack *extack);
  294. struct vdpa_notification_area
  295. (*get_vq_notification)(struct vdpa_device *vdev, u16 idx);
  296. /* vq irq is not expected to be changed once DRIVER_OK is set */
  297. int (*get_vq_irq)(struct vdpa_device *vdev, u16 idx);
  298. /* Device ops */
  299. u32 (*get_vq_align)(struct vdpa_device *vdev);
  300. u32 (*get_vq_group)(struct vdpa_device *vdev, u16 idx);
  301. u64 (*get_device_features)(struct vdpa_device *vdev);
  302. int (*set_driver_features)(struct vdpa_device *vdev, u64 features);
  303. u64 (*get_driver_features)(struct vdpa_device *vdev);
  304. void (*set_config_cb)(struct vdpa_device *vdev,
  305. struct vdpa_callback *cb);
  306. u16 (*get_vq_num_max)(struct vdpa_device *vdev);
  307. u16 (*get_vq_num_min)(struct vdpa_device *vdev);
  308. u32 (*get_device_id)(struct vdpa_device *vdev);
  309. u32 (*get_vendor_id)(struct vdpa_device *vdev);
  310. u8 (*get_status)(struct vdpa_device *vdev);
  311. void (*set_status)(struct vdpa_device *vdev, u8 status);
  312. int (*reset)(struct vdpa_device *vdev);
  313. int (*suspend)(struct vdpa_device *vdev);
  314. size_t (*get_config_size)(struct vdpa_device *vdev);
  315. void (*get_config)(struct vdpa_device *vdev, unsigned int offset,
  316. void *buf, unsigned int len);
  317. void (*set_config)(struct vdpa_device *vdev, unsigned int offset,
  318. const void *buf, unsigned int len);
  319. u32 (*get_generation)(struct vdpa_device *vdev);
  320. struct vdpa_iova_range (*get_iova_range)(struct vdpa_device *vdev);
  321. /* DMA ops */
  322. int (*set_map)(struct vdpa_device *vdev, unsigned int asid,
  323. struct vhost_iotlb *iotlb);
  324. int (*dma_map)(struct vdpa_device *vdev, unsigned int asid,
  325. u64 iova, u64 size, u64 pa, u32 perm, void *opaque);
  326. int (*dma_unmap)(struct vdpa_device *vdev, unsigned int asid,
  327. u64 iova, u64 size);
  328. int (*set_group_asid)(struct vdpa_device *vdev, unsigned int group,
  329. unsigned int asid);
  330. /* Free device resources */
  331. void (*free)(struct vdpa_device *vdev);
  332. };
  333. struct vdpa_device *__vdpa_alloc_device(struct device *parent,
  334. const struct vdpa_config_ops *config,
  335. unsigned int ngroups, unsigned int nas,
  336. size_t size, const char *name,
  337. bool use_va);
  338. /**
  339. * vdpa_alloc_device - allocate and initilaize a vDPA device
  340. *
  341. * @dev_struct: the type of the parent structure
  342. * @member: the name of struct vdpa_device within the @dev_struct
  343. * @parent: the parent device
  344. * @config: the bus operations that is supported by this device
  345. * @ngroups: the number of virtqueue groups supported by this device
  346. * @nas: the number of address spaces
  347. * @name: name of the vdpa device
  348. * @use_va: indicate whether virtual address must be used by this device
  349. *
  350. * Return allocated data structure or ERR_PTR upon error
  351. */
  352. #define vdpa_alloc_device(dev_struct, member, parent, config, ngroups, nas, \
  353. name, use_va) \
  354. container_of((__vdpa_alloc_device( \
  355. parent, config, ngroups, nas, \
  356. (sizeof(dev_struct) + \
  357. BUILD_BUG_ON_ZERO(offsetof( \
  358. dev_struct, member))), name, use_va)), \
  359. dev_struct, member)
  360. int vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
  361. void vdpa_unregister_device(struct vdpa_device *vdev);
  362. int _vdpa_register_device(struct vdpa_device *vdev, u32 nvqs);
  363. void _vdpa_unregister_device(struct vdpa_device *vdev);
  364. /**
  365. * struct vdpa_driver - operations for a vDPA driver
  366. * @driver: underlying device driver
  367. * @probe: the function to call when a device is found. Returns 0 or -errno.
  368. * @remove: the function to call when a device is removed.
  369. */
  370. struct vdpa_driver {
  371. struct device_driver driver;
  372. int (*probe)(struct vdpa_device *vdev);
  373. void (*remove)(struct vdpa_device *vdev);
  374. };
  375. #define vdpa_register_driver(drv) \
  376. __vdpa_register_driver(drv, THIS_MODULE)
  377. int __vdpa_register_driver(struct vdpa_driver *drv, struct module *owner);
  378. void vdpa_unregister_driver(struct vdpa_driver *drv);
  379. #define module_vdpa_driver(__vdpa_driver) \
  380. module_driver(__vdpa_driver, vdpa_register_driver, \
  381. vdpa_unregister_driver)
  382. static inline struct vdpa_driver *drv_to_vdpa(struct device_driver *driver)
  383. {
  384. return container_of(driver, struct vdpa_driver, driver);
  385. }
  386. static inline struct vdpa_device *dev_to_vdpa(struct device *_dev)
  387. {
  388. return container_of(_dev, struct vdpa_device, dev);
  389. }
  390. static inline void *vdpa_get_drvdata(const struct vdpa_device *vdev)
  391. {
  392. return dev_get_drvdata(&vdev->dev);
  393. }
  394. static inline void vdpa_set_drvdata(struct vdpa_device *vdev, void *data)
  395. {
  396. dev_set_drvdata(&vdev->dev, data);
  397. }
  398. static inline struct device *vdpa_get_dma_dev(struct vdpa_device *vdev)
  399. {
  400. return vdev->dma_dev;
  401. }
  402. static inline int vdpa_reset(struct vdpa_device *vdev)
  403. {
  404. const struct vdpa_config_ops *ops = vdev->config;
  405. int ret;
  406. down_write(&vdev->cf_lock);
  407. vdev->features_valid = false;
  408. ret = ops->reset(vdev);
  409. up_write(&vdev->cf_lock);
  410. return ret;
  411. }
  412. static inline int vdpa_set_features_unlocked(struct vdpa_device *vdev, u64 features)
  413. {
  414. const struct vdpa_config_ops *ops = vdev->config;
  415. int ret;
  416. vdev->features_valid = true;
  417. ret = ops->set_driver_features(vdev, features);
  418. return ret;
  419. }
  420. static inline int vdpa_set_features(struct vdpa_device *vdev, u64 features)
  421. {
  422. int ret;
  423. down_write(&vdev->cf_lock);
  424. ret = vdpa_set_features_unlocked(vdev, features);
  425. up_write(&vdev->cf_lock);
  426. return ret;
  427. }
  428. void vdpa_get_config(struct vdpa_device *vdev, unsigned int offset,
  429. void *buf, unsigned int len);
  430. void vdpa_set_config(struct vdpa_device *dev, unsigned int offset,
  431. const void *buf, unsigned int length);
  432. void vdpa_set_status(struct vdpa_device *vdev, u8 status);
  433. /**
  434. * struct vdpa_mgmtdev_ops - vdpa device ops
  435. * @dev_add: Add a vdpa device using alloc and register
  436. * @mdev: parent device to use for device addition
  437. * @name: name of the new vdpa device
  438. * @config: config attributes to apply to the device under creation
  439. * Driver need to add a new device using _vdpa_register_device()
  440. * after fully initializing the vdpa device. Driver must return 0
  441. * on success or appropriate error code.
  442. * @dev_del: Remove a vdpa device using unregister
  443. * @mdev: parent device to use for device removal
  444. * @dev: vdpa device to remove
  445. * Driver need to remove the specified device by calling
  446. * _vdpa_unregister_device().
  447. */
  448. struct vdpa_mgmtdev_ops {
  449. int (*dev_add)(struct vdpa_mgmt_dev *mdev, const char *name,
  450. const struct vdpa_dev_set_config *config);
  451. void (*dev_del)(struct vdpa_mgmt_dev *mdev, struct vdpa_device *dev);
  452. };
  453. /**
  454. * struct vdpa_mgmt_dev - vdpa management device
  455. * @device: Management parent device
  456. * @ops: operations supported by management device
  457. * @id_table: Pointer to device id table of supported ids
  458. * @config_attr_mask: bit mask of attributes of type enum vdpa_attr that
  459. * management device support during dev_add callback
  460. * @list: list entry
  461. */
  462. struct vdpa_mgmt_dev {
  463. struct device *device;
  464. const struct vdpa_mgmtdev_ops *ops;
  465. struct virtio_device_id *id_table;
  466. u64 config_attr_mask;
  467. struct list_head list;
  468. u64 supported_features;
  469. u32 max_supported_vqs;
  470. };
  471. int vdpa_mgmtdev_register(struct vdpa_mgmt_dev *mdev);
  472. void vdpa_mgmtdev_unregister(struct vdpa_mgmt_dev *mdev);
  473. #endif /* _LINUX_VDPA_H */