Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 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 | */ |
Jon Mason | 403c63c | 2013-07-29 16:31:18 -0700 | [diff] [blame] | 48 | #include <linux/ntb.h> |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 49 | |
| 50 | #define PCI_DEVICE_ID_INTEL_NTB_B2B_JSF 0x3725 |
Jon Mason | be4dac0 | 2012-09-28 11:38:48 -0700 | [diff] [blame] | 51 | #define PCI_DEVICE_ID_INTEL_NTB_PS_JSF 0x3726 |
| 52 | #define PCI_DEVICE_ID_INTEL_NTB_SS_JSF 0x3727 |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 53 | #define PCI_DEVICE_ID_INTEL_NTB_B2B_SNB 0x3C0D |
Jon Mason | be4dac0 | 2012-09-28 11:38:48 -0700 | [diff] [blame] | 54 | #define PCI_DEVICE_ID_INTEL_NTB_PS_SNB 0x3C0E |
| 55 | #define PCI_DEVICE_ID_INTEL_NTB_SS_SNB 0x3C0F |
| 56 | #define PCI_DEVICE_ID_INTEL_NTB_B2B_IVT 0x0E0D |
| 57 | #define PCI_DEVICE_ID_INTEL_NTB_PS_IVT 0x0E0E |
| 58 | #define PCI_DEVICE_ID_INTEL_NTB_SS_IVT 0x0E0F |
| 59 | #define PCI_DEVICE_ID_INTEL_NTB_B2B_HSX 0x2F0D |
| 60 | #define PCI_DEVICE_ID_INTEL_NTB_PS_HSX 0x2F0E |
| 61 | #define PCI_DEVICE_ID_INTEL_NTB_SS_HSX 0x2F0F |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 62 | #define PCI_DEVICE_ID_INTEL_NTB_B2B_BWD 0x0C4E |
| 63 | |
Jon Mason | ac477af | 2013-01-21 16:40:39 -0700 | [diff] [blame] | 64 | #ifndef readq |
| 65 | static inline u64 readq(void __iomem *addr) |
| 66 | { |
| 67 | return readl(addr) | (((u64) readl(addr + 4)) << 32LL); |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | #ifndef writeq |
| 72 | static inline void writeq(u64 val, void __iomem *addr) |
| 73 | { |
| 74 | writel(val & 0xffffffff, addr); |
| 75 | writel(val >> 32, addr + 4); |
| 76 | } |
| 77 | #endif |
| 78 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 79 | #define NTB_BAR_MMIO 0 |
| 80 | #define NTB_BAR_23 2 |
| 81 | #define NTB_BAR_45 4 |
| 82 | #define NTB_BAR_MASK ((1 << NTB_BAR_MMIO) | (1 << NTB_BAR_23) |\ |
| 83 | (1 << NTB_BAR_45)) |
| 84 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 85 | #define NTB_HB_TIMEOUT msecs_to_jiffies(1000) |
| 86 | |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 87 | #define NTB_MAX_NUM_MW 2 |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 88 | |
| 89 | enum ntb_hw_event { |
| 90 | NTB_EVENT_SW_EVENT0 = 0, |
| 91 | NTB_EVENT_SW_EVENT1, |
| 92 | NTB_EVENT_SW_EVENT2, |
| 93 | NTB_EVENT_HW_ERROR, |
| 94 | NTB_EVENT_HW_LINK_UP, |
| 95 | NTB_EVENT_HW_LINK_DOWN, |
| 96 | }; |
| 97 | |
| 98 | struct ntb_mw { |
| 99 | dma_addr_t phys_addr; |
| 100 | void __iomem *vbase; |
| 101 | resource_size_t bar_sz; |
| 102 | }; |
| 103 | |
| 104 | struct ntb_db_cb { |
Jon Mason | e8aeb60 | 2013-04-18 17:59:44 -0700 | [diff] [blame] | 105 | int (*callback)(void *data, int db_num); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 106 | unsigned int db_num; |
| 107 | void *data; |
| 108 | struct ntb_device *ndev; |
Jon Mason | e8aeb60 | 2013-04-18 17:59:44 -0700 | [diff] [blame] | 109 | struct tasklet_struct irq_work; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 110 | }; |
| 111 | |
| 112 | struct ntb_device { |
| 113 | struct pci_dev *pdev; |
| 114 | struct msix_entry *msix_entries; |
| 115 | void __iomem *reg_base; |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 116 | struct ntb_mw mw[NTB_MAX_NUM_MW]; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 117 | struct { |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 118 | unsigned char max_mw; |
| 119 | unsigned char max_spads; |
| 120 | unsigned char max_db_bits; |
| 121 | unsigned char msix_cnt; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 122 | } limits; |
| 123 | struct { |
Jon Mason | 4979388 | 2013-07-15 15:53:54 -0700 | [diff] [blame] | 124 | void __iomem *ldb; |
| 125 | void __iomem *ldb_mask; |
| 126 | void __iomem *rdb; |
| 127 | void __iomem *bar2_xlat; |
| 128 | void __iomem *bar4_xlat; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 129 | void __iomem *spad_write; |
| 130 | void __iomem *spad_read; |
| 131 | void __iomem *lnk_cntl; |
| 132 | void __iomem *lnk_stat; |
| 133 | void __iomem *spci_cmd; |
| 134 | } reg_ofs; |
| 135 | struct ntb_transport *ntb_transport; |
| 136 | void (*event_cb)(void *handle, enum ntb_hw_event event); |
| 137 | |
| 138 | struct ntb_db_cb *db_cb; |
| 139 | unsigned char hw_type; |
| 140 | unsigned char conn_type; |
| 141 | unsigned char dev_type; |
| 142 | unsigned char num_msix; |
| 143 | unsigned char bits_per_vector; |
| 144 | unsigned char max_cbs; |
Jon Mason | 113bf1c | 2012-11-16 18:52:57 -0700 | [diff] [blame] | 145 | unsigned char link_width; |
| 146 | unsigned char link_speed; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 147 | unsigned char link_status; |
Jon Mason | 113bf1c | 2012-11-16 18:52:57 -0700 | [diff] [blame] | 148 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 149 | struct delayed_work hb_timer; |
| 150 | unsigned long last_ts; |
Jon Mason | 1517a3f | 2013-07-30 15:58:49 -0700 | [diff] [blame] | 151 | |
Jon Mason | 113bf1c | 2012-11-16 18:52:57 -0700 | [diff] [blame] | 152 | struct delayed_work lr_timer; |
| 153 | |
Jon Mason | 1517a3f | 2013-07-30 15:58:49 -0700 | [diff] [blame] | 154 | struct dentry *debugfs_dir; |
Jon Mason | 6465d02 | 2014-04-07 10:55:47 -0700 | [diff] [blame^] | 155 | struct dentry *debugfs_info; |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /** |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 159 | * ntb_max_cbs() - return the max callbacks |
| 160 | * @ndev: pointer to ntb_device instance |
| 161 | * |
| 162 | * Given the ntb pointer, return the maximum number of callbacks |
| 163 | * |
| 164 | * RETURNS: the maximum number of callbacks |
| 165 | */ |
| 166 | static inline unsigned char ntb_max_cbs(struct ntb_device *ndev) |
| 167 | { |
| 168 | return ndev->max_cbs; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * ntb_max_mw() - return the max number of memory windows |
| 173 | * @ndev: pointer to ntb_device instance |
| 174 | * |
| 175 | * Given the ntb pointer, return the maximum number of memory windows |
| 176 | * |
| 177 | * RETURNS: the maximum number of memory windows |
| 178 | */ |
| 179 | static inline unsigned char ntb_max_mw(struct ntb_device *ndev) |
| 180 | { |
| 181 | return ndev->limits.max_mw; |
| 182 | } |
| 183 | |
| 184 | /** |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 185 | * ntb_hw_link_status() - return the hardware link status |
| 186 | * @ndev: pointer to ntb_device instance |
| 187 | * |
| 188 | * Returns true if the hardware is connected to the remote system |
| 189 | * |
| 190 | * RETURNS: true or false based on the hardware link state |
| 191 | */ |
| 192 | static inline bool ntb_hw_link_status(struct ntb_device *ndev) |
| 193 | { |
| 194 | return ndev->link_status == NTB_LINK_UP; |
| 195 | } |
| 196 | |
| 197 | /** |
| 198 | * ntb_query_pdev() - return the pci_dev pointer |
| 199 | * @ndev: pointer to ntb_device instance |
| 200 | * |
Jon Mason | 948d3a6 | 2013-04-18 17:07:36 -0700 | [diff] [blame] | 201 | * Given the ntb pointer, return the pci_dev pointer for the NTB hardware device |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 202 | * |
| 203 | * RETURNS: a pointer to the ntb pci_dev |
| 204 | */ |
| 205 | static inline struct pci_dev *ntb_query_pdev(struct ntb_device *ndev) |
| 206 | { |
| 207 | return ndev->pdev; |
| 208 | } |
| 209 | |
Jon Mason | 1517a3f | 2013-07-30 15:58:49 -0700 | [diff] [blame] | 210 | /** |
| 211 | * ntb_query_debugfs() - return the debugfs pointer |
| 212 | * @ndev: pointer to ntb_device instance |
| 213 | * |
| 214 | * Given the ntb pointer, return the debugfs directory pointer for the NTB |
| 215 | * hardware device |
| 216 | * |
| 217 | * RETURNS: a pointer to the debugfs directory |
| 218 | */ |
| 219 | static inline struct dentry *ntb_query_debugfs(struct ntb_device *ndev) |
| 220 | { |
| 221 | return ndev->debugfs_dir; |
| 222 | } |
| 223 | |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 224 | struct ntb_device *ntb_register_transport(struct pci_dev *pdev, |
| 225 | void *transport); |
| 226 | void ntb_unregister_transport(struct ntb_device *ndev); |
| 227 | void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr); |
| 228 | int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx, |
Jon Mason | e8aeb60 | 2013-04-18 17:59:44 -0700 | [diff] [blame] | 229 | void *data, int (*db_cb_func)(void *data, |
| 230 | int db_num)); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 231 | void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx); |
| 232 | int ntb_register_event_callback(struct ntb_device *ndev, |
Jon Mason | 53ca4fe | 2013-11-26 11:21:50 -0700 | [diff] [blame] | 233 | void (*event_cb_func)(void *handle, |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 234 | enum ntb_hw_event event)); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 235 | void ntb_unregister_event_callback(struct ntb_device *ndev); |
| 236 | int ntb_get_max_spads(struct ntb_device *ndev); |
| 237 | int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val); |
| 238 | int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val); |
| 239 | int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val); |
| 240 | int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val); |
Jon Mason | 282a2fe | 2013-02-12 09:52:50 -0700 | [diff] [blame] | 241 | resource_size_t ntb_get_mw_base(struct ntb_device *ndev, unsigned int mw); |
Jon Mason | 7446564 | 2013-01-21 15:28:52 -0700 | [diff] [blame] | 242 | void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw); |
Jon Mason | ac477af | 2013-01-21 16:40:39 -0700 | [diff] [blame] | 243 | u64 ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw); |
Jon Mason | 4979388 | 2013-07-15 15:53:54 -0700 | [diff] [blame] | 244 | void ntb_ring_doorbell(struct ntb_device *ndev, unsigned int idx); |
Jon Mason | fce8a7b | 2012-11-16 19:27:12 -0700 | [diff] [blame] | 245 | void *ntb_find_transport(struct pci_dev *pdev); |
| 246 | |
| 247 | int ntb_transport_init(struct pci_dev *pdev); |
| 248 | void ntb_transport_free(void *transport); |