ntb_hw.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * This file is provided under a dual BSD/GPLv2 license. When using or
  3. * redistributing this file, you may do so under either license.
  4. *
  5. * GPL LICENSE SUMMARY
  6. *
  7. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * BSD LICENSE
  14. *
  15. * Copyright(c) 2012 Intel Corporation. All rights reserved.
  16. *
  17. * Redistribution and use in source and binary forms, with or without
  18. * modification, are permitted provided that the following conditions
  19. * are met:
  20. *
  21. * * Redistributions of source code must retain the above copyright
  22. * notice, this list of conditions and the following disclaimer.
  23. * * Redistributions in binary form must reproduce the above copy
  24. * notice, this list of conditions and the following disclaimer in
  25. * the documentation and/or other materials provided with the
  26. * distribution.
  27. * * Neither the name of Intel Corporation nor the names of its
  28. * contributors may be used to endorse or promote products derived
  29. * from this software without specific prior written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  32. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  33. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  34. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  35. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  37. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  38. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  39. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  40. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  41. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * Intel PCIe NTB Linux driver
  44. *
  45. * Contact Information:
  46. * Jon Mason <jon.mason@intel.com>
  47. */
  48. #include <linux/ntb.h>
  49. #define PCI_DEVICE_ID_INTEL_NTB_B2B_JSF 0x3725
  50. #define PCI_DEVICE_ID_INTEL_NTB_PS_JSF 0x3726
  51. #define PCI_DEVICE_ID_INTEL_NTB_SS_JSF 0x3727
  52. #define PCI_DEVICE_ID_INTEL_NTB_B2B_SNB 0x3C0D
  53. #define PCI_DEVICE_ID_INTEL_NTB_PS_SNB 0x3C0E
  54. #define PCI_DEVICE_ID_INTEL_NTB_SS_SNB 0x3C0F
  55. #define PCI_DEVICE_ID_INTEL_NTB_B2B_IVT 0x0E0D
  56. #define PCI_DEVICE_ID_INTEL_NTB_PS_IVT 0x0E0E
  57. #define PCI_DEVICE_ID_INTEL_NTB_SS_IVT 0x0E0F
  58. #define PCI_DEVICE_ID_INTEL_NTB_B2B_HSX 0x2F0D
  59. #define PCI_DEVICE_ID_INTEL_NTB_PS_HSX 0x2F0E
  60. #define PCI_DEVICE_ID_INTEL_NTB_SS_HSX 0x2F0F
  61. #define PCI_DEVICE_ID_INTEL_NTB_B2B_BWD 0x0C4E
  62. #ifndef readq
  63. static inline u64 readq(void __iomem *addr)
  64. {
  65. return readl(addr) | (((u64) readl(addr + 4)) << 32LL);
  66. }
  67. #endif
  68. #ifndef writeq
  69. static inline void writeq(u64 val, void __iomem *addr)
  70. {
  71. writel(val & 0xffffffff, addr);
  72. writel(val >> 32, addr + 4);
  73. }
  74. #endif
  75. #define NTB_BAR_MMIO 0
  76. #define NTB_BAR_23 2
  77. #define NTB_BAR_4 4
  78. #define NTB_BAR_5 5
  79. #define NTB_BAR_MASK ((1 << NTB_BAR_MMIO) | (1 << NTB_BAR_23) |\
  80. (1 << NTB_BAR_4))
  81. #define NTB_SPLITBAR_MASK ((1 << NTB_BAR_MMIO) | (1 << NTB_BAR_23) |\
  82. (1 << NTB_BAR_4) | (1 << NTB_BAR_5))
  83. #define NTB_HB_TIMEOUT msecs_to_jiffies(1000)
  84. enum ntb_hw_event {
  85. NTB_EVENT_SW_EVENT0 = 0,
  86. NTB_EVENT_SW_EVENT1,
  87. NTB_EVENT_SW_EVENT2,
  88. NTB_EVENT_HW_ERROR,
  89. NTB_EVENT_HW_LINK_UP,
  90. NTB_EVENT_HW_LINK_DOWN,
  91. };
  92. struct ntb_mw {
  93. dma_addr_t phys_addr;
  94. void __iomem *vbase;
  95. resource_size_t bar_sz;
  96. };
  97. struct ntb_db_cb {
  98. int (*callback)(void *data, int db_num);
  99. unsigned int db_num;
  100. void *data;
  101. struct ntb_device *ndev;
  102. struct tasklet_struct irq_work;
  103. };
  104. #define WA_SNB_ERR 0x00000001
  105. struct ntb_device {
  106. struct pci_dev *pdev;
  107. struct msix_entry *msix_entries;
  108. void __iomem *reg_base;
  109. struct ntb_mw *mw;
  110. struct {
  111. unsigned char max_mw;
  112. unsigned char max_spads;
  113. unsigned char max_db_bits;
  114. unsigned char msix_cnt;
  115. } limits;
  116. struct {
  117. void __iomem *ldb;
  118. void __iomem *ldb_mask;
  119. void __iomem *rdb;
  120. void __iomem *bar2_xlat;
  121. void __iomem *bar4_xlat;
  122. void __iomem *bar5_xlat;
  123. void __iomem *spad_write;
  124. void __iomem *spad_read;
  125. void __iomem *lnk_cntl;
  126. void __iomem *lnk_stat;
  127. void __iomem *spci_cmd;
  128. } reg_ofs;
  129. struct ntb_transport *ntb_transport;
  130. void (*event_cb)(void *handle, enum ntb_hw_event event);
  131. struct ntb_db_cb *db_cb;
  132. unsigned char hw_type;
  133. unsigned char conn_type;
  134. unsigned char dev_type;
  135. unsigned char num_msix;
  136. unsigned char bits_per_vector;
  137. unsigned char max_cbs;
  138. unsigned char link_width;
  139. unsigned char link_speed;
  140. unsigned char link_status;
  141. unsigned char split_bar;
  142. struct delayed_work hb_timer;
  143. unsigned long last_ts;
  144. struct delayed_work lr_timer;
  145. struct dentry *debugfs_dir;
  146. struct dentry *debugfs_info;
  147. unsigned int wa_flags;
  148. };
  149. /**
  150. * ntb_max_cbs() - return the max callbacks
  151. * @ndev: pointer to ntb_device instance
  152. *
  153. * Given the ntb pointer, return the maximum number of callbacks
  154. *
  155. * RETURNS: the maximum number of callbacks
  156. */
  157. static inline unsigned char ntb_max_cbs(struct ntb_device *ndev)
  158. {
  159. return ndev->max_cbs;
  160. }
  161. /**
  162. * ntb_max_mw() - return the max number of memory windows
  163. * @ndev: pointer to ntb_device instance
  164. *
  165. * Given the ntb pointer, return the maximum number of memory windows
  166. *
  167. * RETURNS: the maximum number of memory windows
  168. */
  169. static inline unsigned char ntb_max_mw(struct ntb_device *ndev)
  170. {
  171. return ndev->limits.max_mw;
  172. }
  173. /**
  174. * ntb_hw_link_status() - return the hardware link status
  175. * @ndev: pointer to ntb_device instance
  176. *
  177. * Returns true if the hardware is connected to the remote system
  178. *
  179. * RETURNS: true or false based on the hardware link state
  180. */
  181. static inline bool ntb_hw_link_status(struct ntb_device *ndev)
  182. {
  183. return ndev->link_status == NTB_LINK_UP;
  184. }
  185. /**
  186. * ntb_query_pdev() - return the pci_dev pointer
  187. * @ndev: pointer to ntb_device instance
  188. *
  189. * Given the ntb pointer, return the pci_dev pointer for the NTB hardware device
  190. *
  191. * RETURNS: a pointer to the ntb pci_dev
  192. */
  193. static inline struct pci_dev *ntb_query_pdev(struct ntb_device *ndev)
  194. {
  195. return ndev->pdev;
  196. }
  197. /**
  198. * ntb_query_debugfs() - return the debugfs pointer
  199. * @ndev: pointer to ntb_device instance
  200. *
  201. * Given the ntb pointer, return the debugfs directory pointer for the NTB
  202. * hardware device
  203. *
  204. * RETURNS: a pointer to the debugfs directory
  205. */
  206. static inline struct dentry *ntb_query_debugfs(struct ntb_device *ndev)
  207. {
  208. return ndev->debugfs_dir;
  209. }
  210. struct ntb_device *ntb_register_transport(struct pci_dev *pdev,
  211. void *transport);
  212. void ntb_unregister_transport(struct ntb_device *ndev);
  213. void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr);
  214. int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
  215. void *data, int (*db_cb_func)(void *data,
  216. int db_num));
  217. void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx);
  218. int ntb_register_event_callback(struct ntb_device *ndev,
  219. void (*event_cb_func)(void *handle,
  220. enum ntb_hw_event event));
  221. void ntb_unregister_event_callback(struct ntb_device *ndev);
  222. int ntb_get_max_spads(struct ntb_device *ndev);
  223. int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val);
  224. int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val);
  225. int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val);
  226. int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val);
  227. resource_size_t ntb_get_mw_base(struct ntb_device *ndev, unsigned int mw);
  228. void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw);
  229. u64 ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw);
  230. void ntb_ring_doorbell(struct ntb_device *ndev, unsigned int idx);
  231. void *ntb_find_transport(struct pci_dev *pdev);
  232. int ntb_transport_init(struct pci_dev *pdev);
  233. void ntb_transport_free(void *transport);