snet_vdpa.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * SolidRun DPU driver for control plane
  4. *
  5. * Copyright (C) 2022-2023 SolidRun
  6. *
  7. * Author: Alvaro Karsz <alvaro.karsz@solid-run.com>
  8. *
  9. */
  10. #ifndef _SNET_VDPA_H_
  11. #define _SNET_VDPA_H_
  12. #include <linux/vdpa.h>
  13. #include <linux/pci.h>
  14. #define SNET_NAME_SIZE 256
  15. #define SNET_ERR(pdev, fmt, ...) dev_err(&(pdev)->dev, "%s"fmt, "snet_vdpa: ", ##__VA_ARGS__)
  16. #define SNET_WARN(pdev, fmt, ...) dev_warn(&(pdev)->dev, "%s"fmt, "snet_vdpa: ", ##__VA_ARGS__)
  17. #define SNET_INFO(pdev, fmt, ...) dev_info(&(pdev)->dev, "%s"fmt, "snet_vdpa: ", ##__VA_ARGS__)
  18. #define SNET_DBG(pdev, fmt, ...) dev_dbg(&(pdev)->dev, "%s"fmt, "snet_vdpa: ", ##__VA_ARGS__)
  19. #define SNET_HAS_FEATURE(s, f) ((s)->negotiated_features & BIT_ULL(f))
  20. /* Check if negotiated config version is at least @ver */
  21. #define SNET_CFG_VER(snet, ver) ((snet)->psnet->negotiated_cfg_ver >= (ver))
  22. /* VQ struct */
  23. struct snet_vq {
  24. /* VQ callback */
  25. struct vdpa_callback cb;
  26. /* VQ state received from bus */
  27. struct vdpa_vq_state vq_state;
  28. /* desc base address */
  29. u64 desc_area;
  30. /* device base address */
  31. u64 device_area;
  32. /* driver base address */
  33. u64 driver_area;
  34. /* Queue size */
  35. u32 num;
  36. /* Serial ID for VQ */
  37. u32 sid;
  38. /* is ready flag */
  39. bool ready;
  40. /* IRQ number */
  41. u32 irq;
  42. /* IRQ index, DPU uses this to parse data from MSI-X table */
  43. u32 irq_idx;
  44. /* IRQ name */
  45. char irq_name[SNET_NAME_SIZE];
  46. /* pointer to mapped PCI BAR register used by this VQ to kick */
  47. void __iomem *kick_ptr;
  48. };
  49. struct snet {
  50. /* vdpa device */
  51. struct vdpa_device vdpa;
  52. /* Config callback */
  53. struct vdpa_callback cb;
  54. /* To lock the control mechanism */
  55. struct mutex ctrl_lock;
  56. /* Spinlock to protect critical parts in the control mechanism */
  57. spinlock_t ctrl_spinlock;
  58. /* array of virqueues */
  59. struct snet_vq **vqs;
  60. /* Used features */
  61. u64 negotiated_features;
  62. /* Device serial ID */
  63. u32 sid;
  64. /* device status */
  65. u8 status;
  66. /* boolean indicating if snet config was passed to the device */
  67. bool dpu_ready;
  68. /* IRQ number */
  69. u32 cfg_irq;
  70. /* IRQ index, DPU uses this to parse data from MSI-X table */
  71. u32 cfg_irq_idx;
  72. /* IRQ name */
  73. char cfg_irq_name[SNET_NAME_SIZE];
  74. /* BAR to access the VF */
  75. void __iomem *bar;
  76. /* PCI device */
  77. struct pci_dev *pdev;
  78. /* Pointer to snet pdev parent device */
  79. struct psnet *psnet;
  80. /* Pointer to snet config device */
  81. struct snet_dev_cfg *cfg;
  82. };
  83. struct snet_dev_cfg {
  84. /* Device ID following VirtIO spec. */
  85. u32 virtio_id;
  86. /* Number of VQs for this device */
  87. u32 vq_num;
  88. /* Size of every VQ */
  89. u32 vq_size;
  90. /* Virtual Function id */
  91. u32 vfid;
  92. /* Device features, following VirtIO spec */
  93. u64 features;
  94. /* Reserved for future usage */
  95. u32 rsvd[6];
  96. /* VirtIO device specific config size */
  97. u32 cfg_size;
  98. /* VirtIO device specific config address */
  99. void __iomem *virtio_cfg;
  100. } __packed;
  101. struct snet_cfg {
  102. /* Magic key */
  103. u32 key;
  104. /* Size of total config in bytes */
  105. u32 cfg_size;
  106. /* Config version */
  107. u32 cfg_ver;
  108. /* Number of Virtual Functions to create */
  109. u32 vf_num;
  110. /* BAR to use for the VFs */
  111. u32 vf_bar;
  112. /* Where should we write the SNET's config */
  113. u32 host_cfg_off;
  114. /* Max. allowed size for a SNET's config */
  115. u32 max_size_host_cfg;
  116. /* VirtIO config offset in BAR */
  117. u32 virtio_cfg_off;
  118. /* Offset in PCI BAR for VQ kicks */
  119. u32 kick_off;
  120. /* Offset in PCI BAR for HW monitoring */
  121. u32 hwmon_off;
  122. /* Offset in PCI BAR for Control mechanism */
  123. u32 ctrl_off;
  124. /* Config general flags - enum snet_cfg_flags */
  125. u32 flags;
  126. /* Reserved for future usage */
  127. u32 rsvd[6];
  128. /* Number of snet devices */
  129. u32 devices_num;
  130. /* The actual devices */
  131. struct snet_dev_cfg **devs;
  132. } __packed;
  133. /* SolidNET PCIe device, one device per PCIe physical function */
  134. struct psnet {
  135. /* PCI BARs */
  136. void __iomem *bars[PCI_STD_NUM_BARS];
  137. /* Negotiated config version */
  138. u32 negotiated_cfg_ver;
  139. /* Next IRQ index to use in case when the IRQs are allocated from this device */
  140. u32 next_irq;
  141. /* BAR number used to communicate with the device */
  142. u8 barno;
  143. /* spinlock to protect data that can be changed by SNET devices */
  144. spinlock_t lock;
  145. /* Pointer to the device's config read from BAR */
  146. struct snet_cfg cfg;
  147. /* Name of monitor device */
  148. char hwmon_name[SNET_NAME_SIZE];
  149. };
  150. enum snet_cfg_flags {
  151. /* Create a HWMON device */
  152. SNET_CFG_FLAG_HWMON = BIT(0),
  153. /* USE IRQs from the physical function */
  154. SNET_CFG_FLAG_IRQ_PF = BIT(1),
  155. };
  156. #define PSNET_FLAG_ON(p, f) ((p)->cfg.flags & (f))
  157. static inline u32 psnet_read32(struct psnet *psnet, u32 off)
  158. {
  159. return ioread32(psnet->bars[psnet->barno] + off);
  160. }
  161. static inline u32 snet_read32(struct snet *snet, u32 off)
  162. {
  163. return ioread32(snet->bar + off);
  164. }
  165. static inline void snet_write32(struct snet *snet, u32 off, u32 val)
  166. {
  167. iowrite32(val, snet->bar + off);
  168. }
  169. static inline u64 psnet_read64(struct psnet *psnet, u32 off)
  170. {
  171. u64 val;
  172. /* 64bits are written in 2 halves, low part first */
  173. val = (u64)psnet_read32(psnet, off);
  174. val |= ((u64)psnet_read32(psnet, off + 4) << 32);
  175. return val;
  176. }
  177. static inline void snet_write64(struct snet *snet, u32 off, u64 val)
  178. {
  179. /* The DPU expects a 64bit integer in 2 halves, the low part first */
  180. snet_write32(snet, off, (u32)val);
  181. snet_write32(snet, off + 4, (u32)(val >> 32));
  182. }
  183. #if IS_ENABLED(CONFIG_HWMON)
  184. void psnet_create_hwmon(struct pci_dev *pdev);
  185. #endif
  186. void snet_ctrl_clear(struct snet *snet);
  187. int snet_destroy_dev(struct snet *snet);
  188. int snet_read_vq_state(struct snet *snet, u16 idx, struct vdpa_vq_state *state);
  189. int snet_suspend_dev(struct snet *snet);
  190. int snet_resume_dev(struct snet *snet);
  191. #endif //_SNET_VDPA_H_