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.
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
8 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
9 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of version 2 of the GNU General Public License as
13 * published by the Free Software Foundation.
17 * Copyright(c) 2012 Intel Corporation. All rights reserved.
18 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
19 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
25 * * Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * * Redistributions in binary form must reproduce the above copy
28 * notice, this list of conditions and the following disclaimer in
29 * the documentation and/or other materials provided with the
31 * * Neither the name of Intel Corporation nor the names of its
32 * contributors may be used to endorse or promote products derived
33 * from this software without specific prior written permission.
35 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 * Intel PCIe NTB Linux driver
49 * Contact Information:
50 * Jon Mason <jon.mason@intel.com>
53 #include <linux/debugfs.h>
54 #include <linux/delay.h>
55 #include <linux/init.h>
56 #include <linux/interrupt.h>
57 #include <linux/module.h>
58 #include <linux/pci.h>
59 #include <linux/random.h>
60 #include <linux/slab.h>
61 #include <linux/ntb.h>
63 #include "ntb_hw_intel.h"
65 #define NTB_NAME "ntb_hw_intel"
66 #define NTB_DESC "Intel(R) PCI-E Non-Transparent Bridge Driver"
69 MODULE_DESCRIPTION(NTB_DESC);
70 MODULE_VERSION(NTB_VER);
71 MODULE_LICENSE("Dual BSD/GPL");
72 MODULE_AUTHOR("Intel Corporation");
74 #define bar0_off(base, bar) ((base) + ((bar) << 2))
75 #define bar2_off(base, bar) bar0_off(base, (bar) - 2)
77 static const struct intel_ntb_reg atom_reg;
78 static const struct intel_ntb_alt_reg atom_pri_reg;
79 static const struct intel_ntb_alt_reg atom_sec_reg;
80 static const struct intel_ntb_alt_reg atom_b2b_reg;
81 static const struct intel_ntb_xlat_reg atom_pri_xlat;
82 static const struct intel_ntb_xlat_reg atom_sec_xlat;
83 static const struct intel_ntb_reg xeon_reg;
84 static const struct intel_ntb_alt_reg xeon_pri_reg;
85 static const struct intel_ntb_alt_reg xeon_sec_reg;
86 static const struct intel_ntb_alt_reg xeon_b2b_reg;
87 static const struct intel_ntb_xlat_reg xeon_pri_xlat;
88 static const struct intel_ntb_xlat_reg xeon_sec_xlat;
89 static struct intel_b2b_addr xeon_b2b_usd_addr;
90 static struct intel_b2b_addr xeon_b2b_dsd_addr;
91 static const struct intel_ntb_reg skx_reg;
92 static const struct intel_ntb_alt_reg skx_pri_reg;
93 static const struct intel_ntb_alt_reg skx_b2b_reg;
94 static const struct intel_ntb_xlat_reg skx_sec_xlat;
95 static const struct ntb_dev_ops intel_ntb_ops;
96 static const struct ntb_dev_ops intel_ntb3_ops;
98 static const struct file_operations intel_ntb_debugfs_info;
99 static struct dentry *debugfs_dir;
101 static int b2b_mw_idx = -1;
102 module_param(b2b_mw_idx, int, 0644);
103 MODULE_PARM_DESC(b2b_mw_idx, "Use this mw idx to access the peer ntb. A "
104 "value of zero or positive starts from first mw idx, and a "
105 "negative value starts from last mw idx. Both sides MUST "
106 "set the same value here!");
108 static unsigned int b2b_mw_share;
109 module_param(b2b_mw_share, uint, 0644);
110 MODULE_PARM_DESC(b2b_mw_share, "If the b2b mw is large enough, configure the "
111 "ntb so that the peer ntb only occupies the first half of "
112 "the mw, so the second half can still be used as a mw. Both "
113 "sides MUST set the same value here!");
115 module_param_named(xeon_b2b_usd_bar2_addr64,
116 xeon_b2b_usd_addr.bar2_addr64, ullong, 0644);
117 MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
118 "XEON B2B USD BAR 2 64-bit address");
120 module_param_named(xeon_b2b_usd_bar4_addr64,
121 xeon_b2b_usd_addr.bar4_addr64, ullong, 0644);
122 MODULE_PARM_DESC(xeon_b2b_usd_bar4_addr64,
123 "XEON B2B USD BAR 4 64-bit address");
125 module_param_named(xeon_b2b_usd_bar4_addr32,
126 xeon_b2b_usd_addr.bar4_addr32, ullong, 0644);
127 MODULE_PARM_DESC(xeon_b2b_usd_bar4_addr32,
128 "XEON B2B USD split-BAR 4 32-bit address");
130 module_param_named(xeon_b2b_usd_bar5_addr32,
131 xeon_b2b_usd_addr.bar5_addr32, ullong, 0644);
132 MODULE_PARM_DESC(xeon_b2b_usd_bar5_addr32,
133 "XEON B2B USD split-BAR 5 32-bit address");
135 module_param_named(xeon_b2b_dsd_bar2_addr64,
136 xeon_b2b_dsd_addr.bar2_addr64, ullong, 0644);
137 MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
138 "XEON B2B DSD BAR 2 64-bit address");
140 module_param_named(xeon_b2b_dsd_bar4_addr64,
141 xeon_b2b_dsd_addr.bar4_addr64, ullong, 0644);
142 MODULE_PARM_DESC(xeon_b2b_dsd_bar4_addr64,
143 "XEON B2B DSD BAR 4 64-bit address");
145 module_param_named(xeon_b2b_dsd_bar4_addr32,
146 xeon_b2b_dsd_addr.bar4_addr32, ullong, 0644);
147 MODULE_PARM_DESC(xeon_b2b_dsd_bar4_addr32,
148 "XEON B2B DSD split-BAR 4 32-bit address");
150 module_param_named(xeon_b2b_dsd_bar5_addr32,
151 xeon_b2b_dsd_addr.bar5_addr32, ullong, 0644);
152 MODULE_PARM_DESC(xeon_b2b_dsd_bar5_addr32,
153 "XEON B2B DSD split-BAR 5 32-bit address");
155 static inline enum ntb_topo xeon_ppd_topo(struct intel_ntb_dev *ndev, u8 ppd);
156 static int xeon_init_isr(struct intel_ntb_dev *ndev);
160 #define ioread64 readq
162 #define ioread64 _ioread64
163 static inline u64 _ioread64(void __iomem *mmio)
167 low = ioread32(mmio);
168 high = ioread32(mmio + sizeof(u32));
169 return low | (high << 32);
176 #define iowrite64 writeq
178 #define iowrite64 _iowrite64
179 static inline void _iowrite64(u64 val, void __iomem *mmio)
181 iowrite32(val, mmio);
182 iowrite32(val >> 32, mmio + sizeof(u32));
187 static inline int pdev_is_atom(struct pci_dev *pdev)
189 switch (pdev->device) {
190 case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
196 static inline int pdev_is_xeon(struct pci_dev *pdev)
198 switch (pdev->device) {
199 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
200 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
201 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
202 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
203 case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
204 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
205 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
206 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
207 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
208 case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
209 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
210 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
211 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
212 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
213 case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
219 static inline int pdev_is_skx_xeon(struct pci_dev *pdev)
221 if (pdev->device == PCI_DEVICE_ID_INTEL_NTB_B2B_SKX)
227 static inline void ndev_reset_unsafe_flags(struct intel_ntb_dev *ndev)
229 ndev->unsafe_flags = 0;
230 ndev->unsafe_flags_ignore = 0;
232 /* Only B2B has a workaround to avoid SDOORBELL */
233 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP)
234 if (!ntb_topo_is_b2b(ndev->ntb.topo))
235 ndev->unsafe_flags |= NTB_UNSAFE_DB;
237 /* No low level workaround to avoid SB01BASE */
238 if (ndev->hwerr_flags & NTB_HWERR_SB01BASE_LOCKUP) {
239 ndev->unsafe_flags |= NTB_UNSAFE_DB;
240 ndev->unsafe_flags |= NTB_UNSAFE_SPAD;
244 static inline int ndev_is_unsafe(struct intel_ntb_dev *ndev,
247 return !!(flag & ndev->unsafe_flags & ~ndev->unsafe_flags_ignore);
250 static inline int ndev_ignore_unsafe(struct intel_ntb_dev *ndev,
253 flag &= ndev->unsafe_flags;
254 ndev->unsafe_flags_ignore |= flag;
259 static int ndev_mw_to_bar(struct intel_ntb_dev *ndev, int idx)
261 if (idx < 0 || idx >= ndev->mw_count)
263 return ndev->reg->mw_bar[idx];
266 static inline int ndev_db_addr(struct intel_ntb_dev *ndev,
267 phys_addr_t *db_addr, resource_size_t *db_size,
268 phys_addr_t reg_addr, unsigned long reg)
270 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
271 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
274 *db_addr = reg_addr + reg;
275 dev_dbg(&ndev->ntb.pdev->dev, "Peer db addr %llx\n", *db_addr);
279 *db_size = ndev->reg->db_size;
280 dev_dbg(&ndev->ntb.pdev->dev, "Peer db size %llx\n", *db_size);
286 static inline u64 ndev_db_read(struct intel_ntb_dev *ndev,
289 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
290 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
292 return ndev->reg->db_ioread(mmio);
295 static inline int ndev_db_write(struct intel_ntb_dev *ndev, u64 db_bits,
298 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
299 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
301 if (db_bits & ~ndev->db_valid_mask)
304 ndev->reg->db_iowrite(db_bits, mmio);
309 static inline int ndev_db_set_mask(struct intel_ntb_dev *ndev, u64 db_bits,
312 unsigned long irqflags;
314 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
315 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
317 if (db_bits & ~ndev->db_valid_mask)
320 spin_lock_irqsave(&ndev->db_mask_lock, irqflags);
322 ndev->db_mask |= db_bits;
323 ndev->reg->db_iowrite(ndev->db_mask, mmio);
325 spin_unlock_irqrestore(&ndev->db_mask_lock, irqflags);
330 static inline int ndev_db_clear_mask(struct intel_ntb_dev *ndev, u64 db_bits,
333 unsigned long irqflags;
335 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
336 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
338 if (db_bits & ~ndev->db_valid_mask)
341 spin_lock_irqsave(&ndev->db_mask_lock, irqflags);
343 ndev->db_mask &= ~db_bits;
344 ndev->reg->db_iowrite(ndev->db_mask, mmio);
346 spin_unlock_irqrestore(&ndev->db_mask_lock, irqflags);
351 static inline u64 ndev_vec_mask(struct intel_ntb_dev *ndev, int db_vector)
355 shift = ndev->db_vec_shift;
356 mask = BIT_ULL(shift) - 1;
358 return mask << (shift * db_vector);
361 static inline int ndev_spad_addr(struct intel_ntb_dev *ndev, int idx,
362 phys_addr_t *spad_addr, phys_addr_t reg_addr,
365 if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
366 pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
368 if (idx < 0 || idx >= ndev->spad_count)
372 *spad_addr = reg_addr + reg + (idx << 2);
373 dev_dbg(&ndev->ntb.pdev->dev, "Peer spad addr %llx\n",
380 static inline u32 ndev_spad_read(struct intel_ntb_dev *ndev, int idx,
383 if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
384 pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
386 if (idx < 0 || idx >= ndev->spad_count)
389 return ioread32(mmio + (idx << 2));
392 static inline int ndev_spad_write(struct intel_ntb_dev *ndev, int idx, u32 val,
395 if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
396 pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
398 if (idx < 0 || idx >= ndev->spad_count)
401 iowrite32(val, mmio + (idx << 2));
406 static irqreturn_t ndev_interrupt(struct intel_ntb_dev *ndev, int vec)
410 vec_mask = ndev_vec_mask(ndev, vec);
412 if ((ndev->hwerr_flags & NTB_HWERR_MSIX_VECTOR32_BAD) && (vec == 31))
413 vec_mask |= ndev->db_link_mask;
415 dev_dbg(&ndev->ntb.pdev->dev, "vec %d vec_mask %llx\n", vec, vec_mask);
417 ndev->last_ts = jiffies;
419 if (vec_mask & ndev->db_link_mask) {
420 if (ndev->reg->poll_link(ndev))
421 ntb_link_event(&ndev->ntb);
424 if (vec_mask & ndev->db_valid_mask)
425 ntb_db_event(&ndev->ntb, vec);
430 static irqreturn_t ndev_vec_isr(int irq, void *dev)
432 struct intel_ntb_vec *nvec = dev;
434 dev_dbg(&nvec->ndev->ntb.pdev->dev, "irq: %d nvec->num: %d\n",
437 return ndev_interrupt(nvec->ndev, nvec->num);
440 static irqreturn_t ndev_irq_isr(int irq, void *dev)
442 struct intel_ntb_dev *ndev = dev;
444 return ndev_interrupt(ndev, irq - ndev->ntb.pdev->irq);
447 static int ndev_init_isr(struct intel_ntb_dev *ndev,
448 int msix_min, int msix_max,
449 int msix_shift, int total_shift)
451 struct pci_dev *pdev;
452 int rc, i, msix_count, node;
454 pdev = ndev->ntb.pdev;
456 node = dev_to_node(&pdev->dev);
458 /* Mask all doorbell interrupts */
459 ndev->db_mask = ndev->db_valid_mask;
460 ndev->reg->db_iowrite(ndev->db_mask,
462 ndev->self_reg->db_mask);
464 /* Try to set up msix irq */
466 ndev->vec = kzalloc_node(msix_max * sizeof(*ndev->vec),
469 goto err_msix_vec_alloc;
471 ndev->msix = kzalloc_node(msix_max * sizeof(*ndev->msix),
476 for (i = 0; i < msix_max; ++i)
477 ndev->msix[i].entry = i;
479 msix_count = pci_enable_msix_range(pdev, ndev->msix,
482 goto err_msix_enable;
484 for (i = 0; i < msix_count; ++i) {
485 ndev->vec[i].ndev = ndev;
486 ndev->vec[i].num = i;
487 rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
488 "ndev_vec_isr", &ndev->vec[i]);
490 goto err_msix_request;
493 dev_dbg(&pdev->dev, "Using %d msix interrupts\n", msix_count);
494 ndev->db_vec_count = msix_count;
495 ndev->db_vec_shift = msix_shift;
500 free_irq(ndev->msix[i].vector, &ndev->vec[i]);
501 pci_disable_msix(pdev);
510 /* Try to set up msi irq */
512 rc = pci_enable_msi(pdev);
516 rc = request_irq(pdev->irq, ndev_irq_isr, 0,
517 "ndev_irq_isr", ndev);
519 goto err_msi_request;
521 dev_dbg(&pdev->dev, "Using msi interrupts\n");
522 ndev->db_vec_count = 1;
523 ndev->db_vec_shift = total_shift;
527 pci_disable_msi(pdev);
530 /* Try to set up intx irq */
534 rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
535 "ndev_irq_isr", ndev);
537 goto err_intx_request;
539 dev_dbg(&pdev->dev, "Using intx interrupts\n");
540 ndev->db_vec_count = 1;
541 ndev->db_vec_shift = total_shift;
548 static void ndev_deinit_isr(struct intel_ntb_dev *ndev)
550 struct pci_dev *pdev;
553 pdev = ndev->ntb.pdev;
555 /* Mask all doorbell interrupts */
556 ndev->db_mask = ndev->db_valid_mask;
557 ndev->reg->db_iowrite(ndev->db_mask,
559 ndev->self_reg->db_mask);
562 i = ndev->db_vec_count;
564 free_irq(ndev->msix[i].vector, &ndev->vec[i]);
565 pci_disable_msix(pdev);
569 free_irq(pdev->irq, ndev);
570 if (pci_dev_msi_enabled(pdev))
571 pci_disable_msi(pdev);
575 static ssize_t ndev_ntb3_debugfs_read(struct file *filp, char __user *ubuf,
576 size_t count, loff_t *offp)
578 struct intel_ntb_dev *ndev;
583 union { u64 v64; u32 v32; u16 v16; } u;
585 ndev = filp->private_data;
586 mmio = ndev->self_mmio;
588 buf_size = min(count, 0x800ul);
590 buf = kmalloc(buf_size, GFP_KERNEL);
596 off += scnprintf(buf + off, buf_size - off,
597 "NTB Device Information:\n");
599 off += scnprintf(buf + off, buf_size - off,
600 "Connection Topology -\t%s\n",
601 ntb_topo_string(ndev->ntb.topo));
603 off += scnprintf(buf + off, buf_size - off,
604 "NTB CTL -\t\t%#06x\n", ndev->ntb_ctl);
605 off += scnprintf(buf + off, buf_size - off,
606 "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
608 if (!ndev->reg->link_is_up(ndev))
609 off += scnprintf(buf + off, buf_size - off,
610 "Link Status -\t\tDown\n");
612 off += scnprintf(buf + off, buf_size - off,
613 "Link Status -\t\tUp\n");
614 off += scnprintf(buf + off, buf_size - off,
615 "Link Speed -\t\tPCI-E Gen %u\n",
616 NTB_LNK_STA_SPEED(ndev->lnk_sta));
617 off += scnprintf(buf + off, buf_size - off,
618 "Link Width -\t\tx%u\n",
619 NTB_LNK_STA_WIDTH(ndev->lnk_sta));
622 off += scnprintf(buf + off, buf_size - off,
623 "Memory Window Count -\t%u\n", ndev->mw_count);
624 off += scnprintf(buf + off, buf_size - off,
625 "Scratchpad Count -\t%u\n", ndev->spad_count);
626 off += scnprintf(buf + off, buf_size - off,
627 "Doorbell Count -\t%u\n", ndev->db_count);
628 off += scnprintf(buf + off, buf_size - off,
629 "Doorbell Vector Count -\t%u\n", ndev->db_vec_count);
630 off += scnprintf(buf + off, buf_size - off,
631 "Doorbell Vector Shift -\t%u\n", ndev->db_vec_shift);
633 off += scnprintf(buf + off, buf_size - off,
634 "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
635 off += scnprintf(buf + off, buf_size - off,
636 "Doorbell Link Mask -\t%#llx\n", ndev->db_link_mask);
637 off += scnprintf(buf + off, buf_size - off,
638 "Doorbell Mask Cached -\t%#llx\n", ndev->db_mask);
640 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_mask);
641 off += scnprintf(buf + off, buf_size - off,
642 "Doorbell Mask -\t\t%#llx\n", u.v64);
644 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_bell);
645 off += scnprintf(buf + off, buf_size - off,
646 "Doorbell Bell -\t\t%#llx\n", u.v64);
648 off += scnprintf(buf + off, buf_size - off,
649 "\nNTB Incoming XLAT:\n");
651 u.v64 = ioread64(mmio + SKX_IMBAR1XBASE_OFFSET);
652 off += scnprintf(buf + off, buf_size - off,
653 "IMBAR1XBASE -\t\t%#018llx\n", u.v64);
655 u.v64 = ioread64(mmio + SKX_IMBAR2XBASE_OFFSET);
656 off += scnprintf(buf + off, buf_size - off,
657 "IMBAR2XBASE -\t\t%#018llx\n", u.v64);
659 u.v64 = ioread64(mmio + SKX_IMBAR1XLMT_OFFSET);
660 off += scnprintf(buf + off, buf_size - off,
661 "IMBAR1XLMT -\t\t\t%#018llx\n", u.v64);
663 u.v64 = ioread64(mmio + SKX_IMBAR2XLMT_OFFSET);
664 off += scnprintf(buf + off, buf_size - off,
665 "IMBAR2XLMT -\t\t\t%#018llx\n", u.v64);
667 if (ntb_topo_is_b2b(ndev->ntb.topo)) {
668 off += scnprintf(buf + off, buf_size - off,
669 "\nNTB Outgoing B2B XLAT:\n");
671 u.v64 = ioread64(mmio + SKX_EMBAR1XBASE_OFFSET);
672 off += scnprintf(buf + off, buf_size - off,
673 "EMBAR1XBASE -\t\t%#018llx\n", u.v64);
675 u.v64 = ioread64(mmio + SKX_EMBAR2XBASE_OFFSET);
676 off += scnprintf(buf + off, buf_size - off,
677 "EMBAR2XBASE -\t\t%#018llx\n", u.v64);
679 u.v64 = ioread64(mmio + SKX_EMBAR1XLMT_OFFSET);
680 off += scnprintf(buf + off, buf_size - off,
681 "EMBAR1XLMT -\t\t%#018llx\n", u.v64);
683 u.v64 = ioread64(mmio + SKX_EMBAR2XLMT_OFFSET);
684 off += scnprintf(buf + off, buf_size - off,
685 "EMBAR2XLMT -\t\t%#018llx\n", u.v64);
687 off += scnprintf(buf + off, buf_size - off,
688 "\nNTB Secondary BAR:\n");
690 u.v64 = ioread64(mmio + SKX_EMBAR0_OFFSET);
691 off += scnprintf(buf + off, buf_size - off,
692 "EMBAR0 -\t\t%#018llx\n", u.v64);
694 u.v64 = ioread64(mmio + SKX_EMBAR1_OFFSET);
695 off += scnprintf(buf + off, buf_size - off,
696 "EMBAR1 -\t\t%#018llx\n", u.v64);
698 u.v64 = ioread64(mmio + SKX_EMBAR2_OFFSET);
699 off += scnprintf(buf + off, buf_size - off,
700 "EMBAR2 -\t\t%#018llx\n", u.v64);
703 off += scnprintf(buf + off, buf_size - off,
704 "\nNTB Statistics:\n");
706 u.v16 = ioread16(mmio + SKX_USMEMMISS_OFFSET);
707 off += scnprintf(buf + off, buf_size - off,
708 "Upstream Memory Miss -\t%u\n", u.v16);
710 off += scnprintf(buf + off, buf_size - off,
711 "\nNTB Hardware Errors:\n");
713 if (!pci_read_config_word(ndev->ntb.pdev,
714 SKX_DEVSTS_OFFSET, &u.v16))
715 off += scnprintf(buf + off, buf_size - off,
716 "DEVSTS -\t\t%#06x\n", u.v16);
718 if (!pci_read_config_word(ndev->ntb.pdev,
719 SKX_LINK_STATUS_OFFSET, &u.v16))
720 off += scnprintf(buf + off, buf_size - off,
721 "LNKSTS -\t\t%#06x\n", u.v16);
723 if (!pci_read_config_dword(ndev->ntb.pdev,
724 SKX_UNCERRSTS_OFFSET, &u.v32))
725 off += scnprintf(buf + off, buf_size - off,
726 "UNCERRSTS -\t\t%#06x\n", u.v32);
728 if (!pci_read_config_dword(ndev->ntb.pdev,
729 SKX_CORERRSTS_OFFSET, &u.v32))
730 off += scnprintf(buf + off, buf_size - off,
731 "CORERRSTS -\t\t%#06x\n", u.v32);
733 ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
738 static ssize_t ndev_ntb_debugfs_read(struct file *filp, char __user *ubuf,
739 size_t count, loff_t *offp)
741 struct intel_ntb_dev *ndev;
742 struct pci_dev *pdev;
747 union { u64 v64; u32 v32; u16 v16; u8 v8; } u;
749 ndev = filp->private_data;
750 pdev = ndev->ntb.pdev;
751 mmio = ndev->self_mmio;
753 buf_size = min(count, 0x800ul);
755 buf = kmalloc(buf_size, GFP_KERNEL);
761 off += scnprintf(buf + off, buf_size - off,
762 "NTB Device Information:\n");
764 off += scnprintf(buf + off, buf_size - off,
765 "Connection Topology -\t%s\n",
766 ntb_topo_string(ndev->ntb.topo));
768 if (ndev->b2b_idx != UINT_MAX) {
769 off += scnprintf(buf + off, buf_size - off,
770 "B2B MW Idx -\t\t%u\n", ndev->b2b_idx);
771 off += scnprintf(buf + off, buf_size - off,
772 "B2B Offset -\t\t%#lx\n", ndev->b2b_off);
775 off += scnprintf(buf + off, buf_size - off,
776 "BAR4 Split -\t\t%s\n",
777 ndev->bar4_split ? "yes" : "no");
779 off += scnprintf(buf + off, buf_size - off,
780 "NTB CTL -\t\t%#06x\n", ndev->ntb_ctl);
781 off += scnprintf(buf + off, buf_size - off,
782 "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
784 if (!ndev->reg->link_is_up(ndev)) {
785 off += scnprintf(buf + off, buf_size - off,
786 "Link Status -\t\tDown\n");
788 off += scnprintf(buf + off, buf_size - off,
789 "Link Status -\t\tUp\n");
790 off += scnprintf(buf + off, buf_size - off,
791 "Link Speed -\t\tPCI-E Gen %u\n",
792 NTB_LNK_STA_SPEED(ndev->lnk_sta));
793 off += scnprintf(buf + off, buf_size - off,
794 "Link Width -\t\tx%u\n",
795 NTB_LNK_STA_WIDTH(ndev->lnk_sta));
798 off += scnprintf(buf + off, buf_size - off,
799 "Memory Window Count -\t%u\n", ndev->mw_count);
800 off += scnprintf(buf + off, buf_size - off,
801 "Scratchpad Count -\t%u\n", ndev->spad_count);
802 off += scnprintf(buf + off, buf_size - off,
803 "Doorbell Count -\t%u\n", ndev->db_count);
804 off += scnprintf(buf + off, buf_size - off,
805 "Doorbell Vector Count -\t%u\n", ndev->db_vec_count);
806 off += scnprintf(buf + off, buf_size - off,
807 "Doorbell Vector Shift -\t%u\n", ndev->db_vec_shift);
809 off += scnprintf(buf + off, buf_size - off,
810 "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
811 off += scnprintf(buf + off, buf_size - off,
812 "Doorbell Link Mask -\t%#llx\n", ndev->db_link_mask);
813 off += scnprintf(buf + off, buf_size - off,
814 "Doorbell Mask Cached -\t%#llx\n", ndev->db_mask);
816 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_mask);
817 off += scnprintf(buf + off, buf_size - off,
818 "Doorbell Mask -\t\t%#llx\n", u.v64);
820 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_bell);
821 off += scnprintf(buf + off, buf_size - off,
822 "Doorbell Bell -\t\t%#llx\n", u.v64);
824 off += scnprintf(buf + off, buf_size - off,
825 "\nNTB Window Size:\n");
827 pci_read_config_byte(pdev, XEON_PBAR23SZ_OFFSET, &u.v8);
828 off += scnprintf(buf + off, buf_size - off,
829 "PBAR23SZ %hhu\n", u.v8);
830 if (!ndev->bar4_split) {
831 pci_read_config_byte(pdev, XEON_PBAR45SZ_OFFSET, &u.v8);
832 off += scnprintf(buf + off, buf_size - off,
833 "PBAR45SZ %hhu\n", u.v8);
835 pci_read_config_byte(pdev, XEON_PBAR4SZ_OFFSET, &u.v8);
836 off += scnprintf(buf + off, buf_size - off,
837 "PBAR4SZ %hhu\n", u.v8);
838 pci_read_config_byte(pdev, XEON_PBAR5SZ_OFFSET, &u.v8);
839 off += scnprintf(buf + off, buf_size - off,
840 "PBAR5SZ %hhu\n", u.v8);
843 pci_read_config_byte(pdev, XEON_SBAR23SZ_OFFSET, &u.v8);
844 off += scnprintf(buf + off, buf_size - off,
845 "SBAR23SZ %hhu\n", u.v8);
846 if (!ndev->bar4_split) {
847 pci_read_config_byte(pdev, XEON_SBAR45SZ_OFFSET, &u.v8);
848 off += scnprintf(buf + off, buf_size - off,
849 "SBAR45SZ %hhu\n", u.v8);
851 pci_read_config_byte(pdev, XEON_SBAR4SZ_OFFSET, &u.v8);
852 off += scnprintf(buf + off, buf_size - off,
853 "SBAR4SZ %hhu\n", u.v8);
854 pci_read_config_byte(pdev, XEON_SBAR5SZ_OFFSET, &u.v8);
855 off += scnprintf(buf + off, buf_size - off,
856 "SBAR5SZ %hhu\n", u.v8);
859 off += scnprintf(buf + off, buf_size - off,
860 "\nNTB Incoming XLAT:\n");
862 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 2));
863 off += scnprintf(buf + off, buf_size - off,
864 "XLAT23 -\t\t%#018llx\n", u.v64);
866 if (ndev->bar4_split) {
867 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
868 off += scnprintf(buf + off, buf_size - off,
869 "XLAT4 -\t\t\t%#06x\n", u.v32);
871 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 5));
872 off += scnprintf(buf + off, buf_size - off,
873 "XLAT5 -\t\t\t%#06x\n", u.v32);
875 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
876 off += scnprintf(buf + off, buf_size - off,
877 "XLAT45 -\t\t%#018llx\n", u.v64);
880 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 2));
881 off += scnprintf(buf + off, buf_size - off,
882 "LMT23 -\t\t\t%#018llx\n", u.v64);
884 if (ndev->bar4_split) {
885 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
886 off += scnprintf(buf + off, buf_size - off,
887 "LMT4 -\t\t\t%#06x\n", u.v32);
888 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 5));
889 off += scnprintf(buf + off, buf_size - off,
890 "LMT5 -\t\t\t%#06x\n", u.v32);
892 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
893 off += scnprintf(buf + off, buf_size - off,
894 "LMT45 -\t\t\t%#018llx\n", u.v64);
897 if (pdev_is_xeon(pdev)) {
898 if (ntb_topo_is_b2b(ndev->ntb.topo)) {
899 off += scnprintf(buf + off, buf_size - off,
900 "\nNTB Outgoing B2B XLAT:\n");
902 u.v64 = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
903 off += scnprintf(buf + off, buf_size - off,
904 "B2B XLAT23 -\t\t%#018llx\n", u.v64);
906 if (ndev->bar4_split) {
907 u.v32 = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
908 off += scnprintf(buf + off, buf_size - off,
909 "B2B XLAT4 -\t\t%#06x\n",
911 u.v32 = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
912 off += scnprintf(buf + off, buf_size - off,
913 "B2B XLAT5 -\t\t%#06x\n",
916 u.v64 = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
917 off += scnprintf(buf + off, buf_size - off,
918 "B2B XLAT45 -\t\t%#018llx\n",
922 u.v64 = ioread64(mmio + XEON_PBAR23LMT_OFFSET);
923 off += scnprintf(buf + off, buf_size - off,
924 "B2B LMT23 -\t\t%#018llx\n", u.v64);
926 if (ndev->bar4_split) {
927 u.v32 = ioread32(mmio + XEON_PBAR4LMT_OFFSET);
928 off += scnprintf(buf + off, buf_size - off,
929 "B2B LMT4 -\t\t%#06x\n",
931 u.v32 = ioread32(mmio + XEON_PBAR5LMT_OFFSET);
932 off += scnprintf(buf + off, buf_size - off,
933 "B2B LMT5 -\t\t%#06x\n",
936 u.v64 = ioread64(mmio + XEON_PBAR45LMT_OFFSET);
937 off += scnprintf(buf + off, buf_size - off,
938 "B2B LMT45 -\t\t%#018llx\n",
942 off += scnprintf(buf + off, buf_size - off,
943 "\nNTB Secondary BAR:\n");
945 u.v64 = ioread64(mmio + XEON_SBAR0BASE_OFFSET);
946 off += scnprintf(buf + off, buf_size - off,
947 "SBAR01 -\t\t%#018llx\n", u.v64);
949 u.v64 = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
950 off += scnprintf(buf + off, buf_size - off,
951 "SBAR23 -\t\t%#018llx\n", u.v64);
953 if (ndev->bar4_split) {
954 u.v32 = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
955 off += scnprintf(buf + off, buf_size - off,
956 "SBAR4 -\t\t\t%#06x\n", u.v32);
957 u.v32 = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
958 off += scnprintf(buf + off, buf_size - off,
959 "SBAR5 -\t\t\t%#06x\n", u.v32);
961 u.v64 = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
962 off += scnprintf(buf + off, buf_size - off,
963 "SBAR45 -\t\t%#018llx\n",
968 off += scnprintf(buf + off, buf_size - off,
969 "\nXEON NTB Statistics:\n");
971 u.v16 = ioread16(mmio + XEON_USMEMMISS_OFFSET);
972 off += scnprintf(buf + off, buf_size - off,
973 "Upstream Memory Miss -\t%u\n", u.v16);
975 off += scnprintf(buf + off, buf_size - off,
976 "\nXEON NTB Hardware Errors:\n");
978 if (!pci_read_config_word(pdev,
979 XEON_DEVSTS_OFFSET, &u.v16))
980 off += scnprintf(buf + off, buf_size - off,
981 "DEVSTS -\t\t%#06x\n", u.v16);
983 if (!pci_read_config_word(pdev,
984 XEON_LINK_STATUS_OFFSET, &u.v16))
985 off += scnprintf(buf + off, buf_size - off,
986 "LNKSTS -\t\t%#06x\n", u.v16);
988 if (!pci_read_config_dword(pdev,
989 XEON_UNCERRSTS_OFFSET, &u.v32))
990 off += scnprintf(buf + off, buf_size - off,
991 "UNCERRSTS -\t\t%#06x\n", u.v32);
993 if (!pci_read_config_dword(pdev,
994 XEON_CORERRSTS_OFFSET, &u.v32))
995 off += scnprintf(buf + off, buf_size - off,
996 "CORERRSTS -\t\t%#06x\n", u.v32);
999 ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
1004 static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
1005 size_t count, loff_t *offp)
1007 struct intel_ntb_dev *ndev = filp->private_data;
1009 if (pdev_is_xeon(ndev->ntb.pdev) ||
1010 pdev_is_atom(ndev->ntb.pdev))
1011 return ndev_ntb_debugfs_read(filp, ubuf, count, offp);
1012 else if (pdev_is_skx_xeon(ndev->ntb.pdev))
1013 return ndev_ntb3_debugfs_read(filp, ubuf, count, offp);
1018 static void ndev_init_debugfs(struct intel_ntb_dev *ndev)
1021 ndev->debugfs_dir = NULL;
1022 ndev->debugfs_info = NULL;
1025 debugfs_create_dir(pci_name(ndev->ntb.pdev),
1027 if (!ndev->debugfs_dir)
1028 ndev->debugfs_info = NULL;
1030 ndev->debugfs_info =
1031 debugfs_create_file("info", S_IRUSR,
1032 ndev->debugfs_dir, ndev,
1033 &intel_ntb_debugfs_info);
1037 static void ndev_deinit_debugfs(struct intel_ntb_dev *ndev)
1039 debugfs_remove_recursive(ndev->debugfs_dir);
1042 static int intel_ntb_mw_count(struct ntb_dev *ntb, int pidx)
1044 if (pidx != NTB_DEF_PEER_IDX)
1047 return ntb_ndev(ntb)->mw_count;
1050 static int intel_ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int idx,
1051 resource_size_t *addr_align,
1052 resource_size_t *size_align,
1053 resource_size_t *size_max)
1055 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1056 resource_size_t bar_size, mw_size;
1059 if (pidx != NTB_DEF_PEER_IDX)
1062 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
1065 bar = ndev_mw_to_bar(ndev, idx);
1069 bar_size = pci_resource_len(ndev->ntb.pdev, bar);
1071 if (idx == ndev->b2b_idx)
1072 mw_size = bar_size - ndev->b2b_off;
1077 *addr_align = pci_resource_len(ndev->ntb.pdev, bar);
1083 *size_max = mw_size;
1088 static int intel_ntb_mw_set_trans(struct ntb_dev *ntb, int pidx, int idx,
1089 dma_addr_t addr, resource_size_t size)
1091 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1092 unsigned long base_reg, xlat_reg, limit_reg;
1093 resource_size_t bar_size, mw_size;
1095 u64 base, limit, reg_val;
1098 if (pidx != NTB_DEF_PEER_IDX)
1101 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
1104 bar = ndev_mw_to_bar(ndev, idx);
1108 bar_size = pci_resource_len(ndev->ntb.pdev, bar);
1110 if (idx == ndev->b2b_idx)
1111 mw_size = bar_size - ndev->b2b_off;
1115 /* hardware requires that addr is aligned to bar size */
1116 if (addr & (bar_size - 1))
1119 /* make sure the range fits in the usable mw size */
1123 mmio = ndev->self_mmio;
1124 base_reg = bar0_off(ndev->xlat_reg->bar0_base, bar);
1125 xlat_reg = bar2_off(ndev->xlat_reg->bar2_xlat, bar);
1126 limit_reg = bar2_off(ndev->xlat_reg->bar2_limit, bar);
1128 if (bar < 4 || !ndev->bar4_split) {
1129 base = ioread64(mmio + base_reg) & NTB_BAR_MASK_64;
1131 /* Set the limit if supported, if size is not mw_size */
1132 if (limit_reg && size != mw_size)
1133 limit = base + size;
1137 /* set and verify setting the translation address */
1138 iowrite64(addr, mmio + xlat_reg);
1139 reg_val = ioread64(mmio + xlat_reg);
1140 if (reg_val != addr) {
1141 iowrite64(0, mmio + xlat_reg);
1145 /* set and verify setting the limit */
1146 iowrite64(limit, mmio + limit_reg);
1147 reg_val = ioread64(mmio + limit_reg);
1148 if (reg_val != limit) {
1149 iowrite64(base, mmio + limit_reg);
1150 iowrite64(0, mmio + xlat_reg);
1154 /* split bar addr range must all be 32 bit */
1155 if (addr & (~0ull << 32))
1157 if ((addr + size) & (~0ull << 32))
1160 base = ioread32(mmio + base_reg) & NTB_BAR_MASK_32;
1162 /* Set the limit if supported, if size is not mw_size */
1163 if (limit_reg && size != mw_size)
1164 limit = base + size;
1168 /* set and verify setting the translation address */
1169 iowrite32(addr, mmio + xlat_reg);
1170 reg_val = ioread32(mmio + xlat_reg);
1171 if (reg_val != addr) {
1172 iowrite32(0, mmio + xlat_reg);
1176 /* set and verify setting the limit */
1177 iowrite32(limit, mmio + limit_reg);
1178 reg_val = ioread32(mmio + limit_reg);
1179 if (reg_val != limit) {
1180 iowrite32(base, mmio + limit_reg);
1181 iowrite32(0, mmio + xlat_reg);
1189 static u64 intel_ntb_link_is_up(struct ntb_dev *ntb,
1190 enum ntb_speed *speed,
1191 enum ntb_width *width)
1193 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1195 if (ndev->reg->link_is_up(ndev)) {
1197 *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
1199 *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
1202 /* TODO MAYBE: is it possible to observe the link speed and
1203 * width while link is training? */
1205 *speed = NTB_SPEED_NONE;
1207 *width = NTB_WIDTH_NONE;
1212 static int intel_ntb_link_enable(struct ntb_dev *ntb,
1213 enum ntb_speed max_speed,
1214 enum ntb_width max_width)
1216 struct intel_ntb_dev *ndev;
1219 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1221 if (ndev->ntb.topo == NTB_TOPO_SEC)
1224 dev_dbg(&ntb->pdev->dev,
1225 "Enabling link with max_speed %d max_width %d\n",
1226 max_speed, max_width);
1227 if (max_speed != NTB_SPEED_AUTO)
1228 dev_dbg(&ntb->pdev->dev, "ignoring max_speed %d\n", max_speed);
1229 if (max_width != NTB_WIDTH_AUTO)
1230 dev_dbg(&ntb->pdev->dev, "ignoring max_width %d\n", max_width);
1232 ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
1233 ntb_ctl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
1234 ntb_ctl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
1235 ntb_ctl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
1236 if (ndev->bar4_split)
1237 ntb_ctl |= NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP;
1238 iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
1243 static int intel_ntb_link_disable(struct ntb_dev *ntb)
1245 struct intel_ntb_dev *ndev;
1248 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1250 if (ndev->ntb.topo == NTB_TOPO_SEC)
1253 dev_dbg(&ntb->pdev->dev, "Disabling link\n");
1255 /* Bring NTB link down */
1256 ntb_cntl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
1257 ntb_cntl &= ~(NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP);
1258 ntb_cntl &= ~(NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP);
1259 if (ndev->bar4_split)
1260 ntb_cntl &= ~(NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP);
1261 ntb_cntl |= NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK;
1262 iowrite32(ntb_cntl, ndev->self_mmio + ndev->reg->ntb_ctl);
1267 static int intel_ntb_peer_mw_count(struct ntb_dev *ntb)
1269 /* Numbers of inbound and outbound memory windows match */
1270 return ntb_ndev(ntb)->mw_count;
1273 static int intel_ntb_peer_mw_get_addr(struct ntb_dev *ntb, int idx,
1274 phys_addr_t *base, resource_size_t *size)
1276 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1279 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
1282 bar = ndev_mw_to_bar(ndev, idx);
1287 *base = pci_resource_start(ndev->ntb.pdev, bar) +
1288 (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
1291 *size = pci_resource_len(ndev->ntb.pdev, bar) -
1292 (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
1297 static int intel_ntb_db_is_unsafe(struct ntb_dev *ntb)
1299 return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_DB);
1302 static u64 intel_ntb_db_valid_mask(struct ntb_dev *ntb)
1304 return ntb_ndev(ntb)->db_valid_mask;
1307 static int intel_ntb_db_vector_count(struct ntb_dev *ntb)
1309 struct intel_ntb_dev *ndev;
1311 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1313 return ndev->db_vec_count;
1316 static u64 intel_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
1318 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1320 if (db_vector < 0 || db_vector > ndev->db_vec_count)
1323 return ndev->db_valid_mask & ndev_vec_mask(ndev, db_vector);
1326 static u64 intel_ntb_db_read(struct ntb_dev *ntb)
1328 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1330 return ndev_db_read(ndev,
1332 ndev->self_reg->db_bell);
1335 static int intel_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
1337 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1339 return ndev_db_write(ndev, db_bits,
1341 ndev->self_reg->db_bell);
1344 static int intel_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1346 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1348 return ndev_db_set_mask(ndev, db_bits,
1350 ndev->self_reg->db_mask);
1353 static int intel_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
1355 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1357 return ndev_db_clear_mask(ndev, db_bits,
1359 ndev->self_reg->db_mask);
1362 static int intel_ntb_peer_db_addr(struct ntb_dev *ntb,
1363 phys_addr_t *db_addr,
1364 resource_size_t *db_size)
1366 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1368 return ndev_db_addr(ndev, db_addr, db_size, ndev->peer_addr,
1369 ndev->peer_reg->db_bell);
1372 static int intel_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
1374 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1376 return ndev_db_write(ndev, db_bits,
1378 ndev->peer_reg->db_bell);
1381 static int intel_ntb_spad_is_unsafe(struct ntb_dev *ntb)
1383 return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_SPAD);
1386 static int intel_ntb_spad_count(struct ntb_dev *ntb)
1388 struct intel_ntb_dev *ndev;
1390 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1392 return ndev->spad_count;
1395 static u32 intel_ntb_spad_read(struct ntb_dev *ntb, int idx)
1397 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1399 return ndev_spad_read(ndev, idx,
1401 ndev->self_reg->spad);
1404 static int intel_ntb_spad_write(struct ntb_dev *ntb,
1407 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1409 return ndev_spad_write(ndev, idx, val,
1411 ndev->self_reg->spad);
1414 static int intel_ntb_peer_spad_addr(struct ntb_dev *ntb, int pidx, int sidx,
1415 phys_addr_t *spad_addr)
1417 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1419 return ndev_spad_addr(ndev, sidx, spad_addr, ndev->peer_addr,
1420 ndev->peer_reg->spad);
1423 static u32 intel_ntb_peer_spad_read(struct ntb_dev *ntb, int pidx, int sidx)
1425 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1427 return ndev_spad_read(ndev, sidx,
1429 ndev->peer_reg->spad);
1432 static int intel_ntb_peer_spad_write(struct ntb_dev *ntb, int pidx,
1435 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1437 return ndev_spad_write(ndev, sidx, val,
1439 ndev->peer_reg->spad);
1444 static u64 atom_db_ioread(void __iomem *mmio)
1446 return ioread64(mmio);
1449 static void atom_db_iowrite(u64 bits, void __iomem *mmio)
1451 iowrite64(bits, mmio);
1454 static int atom_poll_link(struct intel_ntb_dev *ndev)
1458 ntb_ctl = ioread32(ndev->self_mmio + ATOM_NTBCNTL_OFFSET);
1460 if (ntb_ctl == ndev->ntb_ctl)
1463 ndev->ntb_ctl = ntb_ctl;
1465 ndev->lnk_sta = ioread32(ndev->self_mmio + ATOM_LINK_STATUS_OFFSET);
1470 static int atom_link_is_up(struct intel_ntb_dev *ndev)
1472 return ATOM_NTB_CTL_ACTIVE(ndev->ntb_ctl);
1475 static int atom_link_is_err(struct intel_ntb_dev *ndev)
1477 if (ioread32(ndev->self_mmio + ATOM_LTSSMSTATEJMP_OFFSET)
1478 & ATOM_LTSSMSTATEJMP_FORCEDETECT)
1481 if (ioread32(ndev->self_mmio + ATOM_IBSTERRRCRVSTS0_OFFSET)
1482 & ATOM_IBIST_ERR_OFLOW)
1488 static inline enum ntb_topo atom_ppd_topo(struct intel_ntb_dev *ndev, u32 ppd)
1490 struct device *dev = &ndev->ntb.pdev->dev;
1492 switch (ppd & ATOM_PPD_TOPO_MASK) {
1493 case ATOM_PPD_TOPO_B2B_USD:
1494 dev_dbg(dev, "PPD %d B2B USD\n", ppd);
1495 return NTB_TOPO_B2B_USD;
1497 case ATOM_PPD_TOPO_B2B_DSD:
1498 dev_dbg(dev, "PPD %d B2B DSD\n", ppd);
1499 return NTB_TOPO_B2B_DSD;
1501 case ATOM_PPD_TOPO_PRI_USD:
1502 case ATOM_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
1503 case ATOM_PPD_TOPO_SEC_USD:
1504 case ATOM_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
1505 dev_dbg(dev, "PPD %d non B2B disabled\n", ppd);
1506 return NTB_TOPO_NONE;
1509 dev_dbg(dev, "PPD %d invalid\n", ppd);
1510 return NTB_TOPO_NONE;
1513 static void atom_link_hb(struct work_struct *work)
1515 struct intel_ntb_dev *ndev = hb_ndev(work);
1516 struct device *dev = &ndev->ntb.pdev->dev;
1517 unsigned long poll_ts;
1521 poll_ts = ndev->last_ts + ATOM_LINK_HB_TIMEOUT;
1523 /* Delay polling the link status if an interrupt was received,
1524 * unless the cached link status says the link is down.
1526 if (time_after(poll_ts, jiffies) && atom_link_is_up(ndev)) {
1527 schedule_delayed_work(&ndev->hb_timer, poll_ts - jiffies);
1531 if (atom_poll_link(ndev))
1532 ntb_link_event(&ndev->ntb);
1534 if (atom_link_is_up(ndev) || !atom_link_is_err(ndev)) {
1535 schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
1539 /* Link is down with error: recover the link! */
1541 mmio = ndev->self_mmio;
1543 /* Driver resets the NTB ModPhy lanes - magic! */
1544 iowrite8(0xe0, mmio + ATOM_MODPHY_PCSREG6);
1545 iowrite8(0x40, mmio + ATOM_MODPHY_PCSREG4);
1546 iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG4);
1547 iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG6);
1549 /* Driver waits 100ms to allow the NTB ModPhy to settle */
1552 /* Clear AER Errors, write to clear */
1553 status32 = ioread32(mmio + ATOM_ERRCORSTS_OFFSET);
1554 dev_dbg(dev, "ERRCORSTS = %x\n", status32);
1555 status32 &= PCI_ERR_COR_REP_ROLL;
1556 iowrite32(status32, mmio + ATOM_ERRCORSTS_OFFSET);
1558 /* Clear unexpected electrical idle event in LTSSM, write to clear */
1559 status32 = ioread32(mmio + ATOM_LTSSMERRSTS0_OFFSET);
1560 dev_dbg(dev, "LTSSMERRSTS0 = %x\n", status32);
1561 status32 |= ATOM_LTSSMERRSTS0_UNEXPECTEDEI;
1562 iowrite32(status32, mmio + ATOM_LTSSMERRSTS0_OFFSET);
1564 /* Clear DeSkew Buffer error, write to clear */
1565 status32 = ioread32(mmio + ATOM_DESKEWSTS_OFFSET);
1566 dev_dbg(dev, "DESKEWSTS = %x\n", status32);
1567 status32 |= ATOM_DESKEWSTS_DBERR;
1568 iowrite32(status32, mmio + ATOM_DESKEWSTS_OFFSET);
1570 status32 = ioread32(mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
1571 dev_dbg(dev, "IBSTERRRCRVSTS0 = %x\n", status32);
1572 status32 &= ATOM_IBIST_ERR_OFLOW;
1573 iowrite32(status32, mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
1575 /* Releases the NTB state machine to allow the link to retrain */
1576 status32 = ioread32(mmio + ATOM_LTSSMSTATEJMP_OFFSET);
1577 dev_dbg(dev, "LTSSMSTATEJMP = %x\n", status32);
1578 status32 &= ~ATOM_LTSSMSTATEJMP_FORCEDETECT;
1579 iowrite32(status32, mmio + ATOM_LTSSMSTATEJMP_OFFSET);
1581 /* There is a potential race between the 2 NTB devices recovering at the
1582 * same time. If the times are the same, the link will not recover and
1583 * the driver will be stuck in this loop forever. Add a random interval
1584 * to the recovery time to prevent this race.
1586 schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_RECOVERY_TIME
1587 + prandom_u32() % ATOM_LINK_RECOVERY_TIME);
1590 static int atom_init_isr(struct intel_ntb_dev *ndev)
1594 rc = ndev_init_isr(ndev, 1, ATOM_DB_MSIX_VECTOR_COUNT,
1595 ATOM_DB_MSIX_VECTOR_SHIFT, ATOM_DB_TOTAL_SHIFT);
1599 /* ATOM doesn't have link status interrupt, poll on that platform */
1600 ndev->last_ts = jiffies;
1601 INIT_DELAYED_WORK(&ndev->hb_timer, atom_link_hb);
1602 schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
1607 static void atom_deinit_isr(struct intel_ntb_dev *ndev)
1609 cancel_delayed_work_sync(&ndev->hb_timer);
1610 ndev_deinit_isr(ndev);
1613 static int atom_init_ntb(struct intel_ntb_dev *ndev)
1615 ndev->mw_count = ATOM_MW_COUNT;
1616 ndev->spad_count = ATOM_SPAD_COUNT;
1617 ndev->db_count = ATOM_DB_COUNT;
1619 switch (ndev->ntb.topo) {
1620 case NTB_TOPO_B2B_USD:
1621 case NTB_TOPO_B2B_DSD:
1622 ndev->self_reg = &atom_pri_reg;
1623 ndev->peer_reg = &atom_b2b_reg;
1624 ndev->xlat_reg = &atom_sec_xlat;
1626 /* Enable Bus Master and Memory Space on the secondary side */
1627 iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
1628 ndev->self_mmio + ATOM_SPCICMD_OFFSET);
1636 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
1641 static int atom_init_dev(struct intel_ntb_dev *ndev)
1646 rc = pci_read_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET, &ppd);
1650 ndev->ntb.topo = atom_ppd_topo(ndev, ppd);
1651 if (ndev->ntb.topo == NTB_TOPO_NONE)
1654 rc = atom_init_ntb(ndev);
1658 rc = atom_init_isr(ndev);
1662 if (ndev->ntb.topo != NTB_TOPO_SEC) {
1663 /* Initiate PCI-E link training */
1664 rc = pci_write_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET,
1665 ppd | ATOM_PPD_INIT_LINK);
1673 static void atom_deinit_dev(struct intel_ntb_dev *ndev)
1675 atom_deinit_isr(ndev);
1678 /* Skylake Xeon NTB */
1680 static int skx_poll_link(struct intel_ntb_dev *ndev)
1685 ndev->reg->db_iowrite(ndev->db_link_mask,
1687 ndev->self_reg->db_clear);
1689 rc = pci_read_config_word(ndev->ntb.pdev,
1690 SKX_LINK_STATUS_OFFSET, ®_val);
1694 if (reg_val == ndev->lnk_sta)
1697 ndev->lnk_sta = reg_val;
1702 static u64 skx_db_ioread(void __iomem *mmio)
1704 return ioread64(mmio);
1707 static void skx_db_iowrite(u64 bits, void __iomem *mmio)
1709 iowrite64(bits, mmio);
1712 static int skx_init_isr(struct intel_ntb_dev *ndev)
1717 * The MSIX vectors and the interrupt status bits are not lined up
1718 * on Skylake. By default the link status bit is bit 32, however it
1719 * is by default MSIX vector0. We need to fixup to line them up.
1720 * The vectors at reset is 1-32,0. We need to reprogram to 0-32.
1723 for (i = 0; i < SKX_DB_MSIX_VECTOR_COUNT; i++)
1724 iowrite8(i, ndev->self_mmio + SKX_INTVEC_OFFSET + i);
1726 /* move link status down one as workaround */
1727 if (ndev->hwerr_flags & NTB_HWERR_MSIX_VECTOR32_BAD) {
1728 iowrite8(SKX_DB_MSIX_VECTOR_COUNT - 2,
1729 ndev->self_mmio + SKX_INTVEC_OFFSET +
1730 (SKX_DB_MSIX_VECTOR_COUNT - 1));
1733 return ndev_init_isr(ndev, SKX_DB_MSIX_VECTOR_COUNT,
1734 SKX_DB_MSIX_VECTOR_COUNT,
1735 SKX_DB_MSIX_VECTOR_SHIFT,
1736 SKX_DB_TOTAL_SHIFT);
1739 static int skx_setup_b2b_mw(struct intel_ntb_dev *ndev,
1740 const struct intel_b2b_addr *addr,
1741 const struct intel_b2b_addr *peer_addr)
1743 struct pci_dev *pdev;
1745 resource_size_t bar_size;
1746 phys_addr_t bar_addr;
1750 pdev = ndev->ntb.pdev;
1751 mmio = ndev->self_mmio;
1753 if (ndev->b2b_idx == UINT_MAX) {
1754 dev_dbg(&pdev->dev, "not using b2b mw\n");
1758 b2b_bar = ndev_mw_to_bar(ndev, ndev->b2b_idx);
1762 dev_dbg(&pdev->dev, "using b2b mw bar %d\n", b2b_bar);
1764 bar_size = pci_resource_len(ndev->ntb.pdev, b2b_bar);
1766 dev_dbg(&pdev->dev, "b2b bar size %#llx\n", bar_size);
1768 if (b2b_mw_share && ((bar_size >> 1) >= XEON_B2B_MIN_SIZE)) {
1769 dev_dbg(&pdev->dev, "b2b using first half of bar\n");
1770 ndev->b2b_off = bar_size >> 1;
1771 } else if (bar_size >= XEON_B2B_MIN_SIZE) {
1772 dev_dbg(&pdev->dev, "b2b using whole bar\n");
1776 dev_dbg(&pdev->dev, "b2b bar size is too small\n");
1782 * Reset the secondary bar sizes to match the primary bar sizes,
1783 * except disable or halve the size of the b2b secondary bar.
1785 pci_read_config_byte(pdev, SKX_IMBAR1SZ_OFFSET, &bar_sz);
1786 dev_dbg(&pdev->dev, "IMBAR1SZ %#x\n", bar_sz);
1794 pci_write_config_byte(pdev, SKX_EMBAR1SZ_OFFSET, bar_sz);
1795 pci_read_config_byte(pdev, SKX_EMBAR1SZ_OFFSET, &bar_sz);
1796 dev_dbg(&pdev->dev, "EMBAR1SZ %#x\n", bar_sz);
1798 pci_read_config_byte(pdev, SKX_IMBAR2SZ_OFFSET, &bar_sz);
1799 dev_dbg(&pdev->dev, "IMBAR2SZ %#x\n", bar_sz);
1807 pci_write_config_byte(pdev, SKX_EMBAR2SZ_OFFSET, bar_sz);
1808 pci_read_config_byte(pdev, SKX_EMBAR2SZ_OFFSET, &bar_sz);
1809 dev_dbg(&pdev->dev, "EMBAR2SZ %#x\n", bar_sz);
1811 /* SBAR01 hit by first part of the b2b bar */
1813 bar_addr = addr->bar0_addr;
1814 else if (b2b_bar == 1)
1815 bar_addr = addr->bar2_addr64;
1816 else if (b2b_bar == 2)
1817 bar_addr = addr->bar4_addr64;
1821 /* setup incoming bar limits == base addrs (zero length windows) */
1822 bar_addr = addr->bar2_addr64 + (b2b_bar == 1 ? ndev->b2b_off : 0);
1823 iowrite64(bar_addr, mmio + SKX_IMBAR1XLMT_OFFSET);
1824 bar_addr = ioread64(mmio + SKX_IMBAR1XLMT_OFFSET);
1825 dev_dbg(&pdev->dev, "IMBAR1XLMT %#018llx\n", bar_addr);
1827 bar_addr = addr->bar4_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
1828 iowrite64(bar_addr, mmio + SKX_IMBAR2XLMT_OFFSET);
1829 bar_addr = ioread64(mmio + SKX_IMBAR2XLMT_OFFSET);
1830 dev_dbg(&pdev->dev, "IMBAR2XLMT %#018llx\n", bar_addr);
1832 /* zero incoming translation addrs */
1833 iowrite64(0, mmio + SKX_IMBAR1XBASE_OFFSET);
1834 iowrite64(0, mmio + SKX_IMBAR2XBASE_OFFSET);
1836 ndev->peer_mmio = ndev->self_mmio;
1841 static int skx_init_ntb(struct intel_ntb_dev *ndev)
1846 ndev->mw_count = XEON_MW_COUNT;
1847 ndev->spad_count = SKX_SPAD_COUNT;
1848 ndev->db_count = SKX_DB_COUNT;
1849 ndev->db_link_mask = SKX_DB_LINK_BIT;
1851 /* DB fixup for using 31 right now */
1852 if (ndev->hwerr_flags & NTB_HWERR_MSIX_VECTOR32_BAD)
1853 ndev->db_link_mask |= BIT_ULL(31);
1855 switch (ndev->ntb.topo) {
1856 case NTB_TOPO_B2B_USD:
1857 case NTB_TOPO_B2B_DSD:
1858 ndev->self_reg = &skx_pri_reg;
1859 ndev->peer_reg = &skx_b2b_reg;
1860 ndev->xlat_reg = &skx_sec_xlat;
1862 if (ndev->ntb.topo == NTB_TOPO_B2B_USD) {
1863 rc = skx_setup_b2b_mw(ndev,
1865 &xeon_b2b_usd_addr);
1867 rc = skx_setup_b2b_mw(ndev,
1869 &xeon_b2b_dsd_addr);
1875 /* Enable Bus Master and Memory Space on the secondary side */
1876 iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
1877 ndev->self_mmio + SKX_SPCICMD_OFFSET);
1885 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
1887 ndev->reg->db_iowrite(ndev->db_valid_mask,
1889 ndev->self_reg->db_mask);
1894 static int skx_init_dev(struct intel_ntb_dev *ndev)
1896 struct pci_dev *pdev;
1900 pdev = ndev->ntb.pdev;
1902 ndev->reg = &skx_reg;
1904 rc = pci_read_config_byte(pdev, XEON_PPD_OFFSET, &ppd);
1908 ndev->ntb.topo = xeon_ppd_topo(ndev, ppd);
1909 dev_dbg(&pdev->dev, "ppd %#x topo %s\n", ppd,
1910 ntb_topo_string(ndev->ntb.topo));
1911 if (ndev->ntb.topo == NTB_TOPO_NONE)
1914 if (pdev_is_skx_xeon(pdev))
1915 ndev->hwerr_flags |= NTB_HWERR_MSIX_VECTOR32_BAD;
1917 rc = skx_init_ntb(ndev);
1921 return skx_init_isr(ndev);
1924 static int intel_ntb3_link_enable(struct ntb_dev *ntb,
1925 enum ntb_speed max_speed,
1926 enum ntb_width max_width)
1928 struct intel_ntb_dev *ndev;
1931 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1933 dev_dbg(&ntb->pdev->dev,
1934 "Enabling link with max_speed %d max_width %d\n",
1935 max_speed, max_width);
1937 if (max_speed != NTB_SPEED_AUTO)
1938 dev_dbg(&ntb->pdev->dev, "ignoring max_speed %d\n", max_speed);
1939 if (max_width != NTB_WIDTH_AUTO)
1940 dev_dbg(&ntb->pdev->dev, "ignoring max_width %d\n", max_width);
1942 ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
1943 ntb_ctl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
1944 ntb_ctl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
1945 ntb_ctl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
1946 iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
1950 static int intel_ntb3_mw_set_trans(struct ntb_dev *ntb, int pidx, int idx,
1951 dma_addr_t addr, resource_size_t size)
1953 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1954 unsigned long xlat_reg, limit_reg;
1955 resource_size_t bar_size, mw_size;
1957 u64 base, limit, reg_val;
1960 if (pidx != NTB_DEF_PEER_IDX)
1963 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
1966 bar = ndev_mw_to_bar(ndev, idx);
1970 bar_size = pci_resource_len(ndev->ntb.pdev, bar);
1972 if (idx == ndev->b2b_idx)
1973 mw_size = bar_size - ndev->b2b_off;
1977 /* hardware requires that addr is aligned to bar size */
1978 if (addr & (bar_size - 1))
1981 /* make sure the range fits in the usable mw size */
1985 mmio = ndev->self_mmio;
1986 xlat_reg = ndev->xlat_reg->bar2_xlat + (idx * 0x10);
1987 limit_reg = ndev->xlat_reg->bar2_limit + (idx * 0x10);
1988 base = pci_resource_start(ndev->ntb.pdev, bar);
1990 /* Set the limit if supported, if size is not mw_size */
1991 if (limit_reg && size != mw_size)
1992 limit = base + size;
1994 limit = base + mw_size;
1996 /* set and verify setting the translation address */
1997 iowrite64(addr, mmio + xlat_reg);
1998 reg_val = ioread64(mmio + xlat_reg);
1999 if (reg_val != addr) {
2000 iowrite64(0, mmio + xlat_reg);
2004 dev_dbg(&ntb->pdev->dev, "BAR %d IMBARXBASE: %#Lx\n", bar, reg_val);
2006 /* set and verify setting the limit */
2007 iowrite64(limit, mmio + limit_reg);
2008 reg_val = ioread64(mmio + limit_reg);
2009 if (reg_val != limit) {
2010 iowrite64(base, mmio + limit_reg);
2011 iowrite64(0, mmio + xlat_reg);
2015 dev_dbg(&ntb->pdev->dev, "BAR %d IMBARXLMT: %#Lx\n", bar, reg_val);
2018 limit_reg = ndev->xlat_reg->bar2_limit + (idx * 0x10) + 0x4000;
2019 base = ioread64(mmio + SKX_EMBAR1_OFFSET + (8 * idx));
2022 if (limit_reg && size != mw_size)
2023 limit = base + size;
2025 limit = base + mw_size;
2027 /* set and verify setting the limit */
2028 iowrite64(limit, mmio + limit_reg);
2029 reg_val = ioread64(mmio + limit_reg);
2030 if (reg_val != limit) {
2031 iowrite64(base, mmio + limit_reg);
2032 iowrite64(0, mmio + xlat_reg);
2036 dev_dbg(&ntb->pdev->dev, "BAR %d EMBARXLMT: %#Lx\n", bar, reg_val);
2041 static int intel_ntb3_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
2043 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
2046 if (db_bits & ~ndev->db_valid_mask)
2050 bit = __ffs(db_bits);
2051 iowrite32(1, ndev->peer_mmio +
2052 ndev->peer_reg->db_bell + (bit * 4));
2053 db_bits &= db_bits - 1;
2059 static u64 intel_ntb3_db_read(struct ntb_dev *ntb)
2061 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
2063 return ndev_db_read(ndev,
2065 ndev->self_reg->db_clear);
2068 static int intel_ntb3_db_clear(struct ntb_dev *ntb, u64 db_bits)
2070 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
2072 return ndev_db_write(ndev, db_bits,
2074 ndev->self_reg->db_clear);
2079 static u64 xeon_db_ioread(void __iomem *mmio)
2081 return (u64)ioread16(mmio);
2084 static void xeon_db_iowrite(u64 bits, void __iomem *mmio)
2086 iowrite16((u16)bits, mmio);
2089 static int xeon_poll_link(struct intel_ntb_dev *ndev)
2094 ndev->reg->db_iowrite(ndev->db_link_mask,
2096 ndev->self_reg->db_bell);
2098 rc = pci_read_config_word(ndev->ntb.pdev,
2099 XEON_LINK_STATUS_OFFSET, ®_val);
2103 if (reg_val == ndev->lnk_sta)
2106 ndev->lnk_sta = reg_val;
2111 static int xeon_link_is_up(struct intel_ntb_dev *ndev)
2113 if (ndev->ntb.topo == NTB_TOPO_SEC)
2116 return NTB_LNK_STA_ACTIVE(ndev->lnk_sta);
2119 static inline enum ntb_topo xeon_ppd_topo(struct intel_ntb_dev *ndev, u8 ppd)
2121 switch (ppd & XEON_PPD_TOPO_MASK) {
2122 case XEON_PPD_TOPO_B2B_USD:
2123 return NTB_TOPO_B2B_USD;
2125 case XEON_PPD_TOPO_B2B_DSD:
2126 return NTB_TOPO_B2B_DSD;
2128 case XEON_PPD_TOPO_PRI_USD:
2129 case XEON_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
2130 return NTB_TOPO_PRI;
2132 case XEON_PPD_TOPO_SEC_USD:
2133 case XEON_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
2134 return NTB_TOPO_SEC;
2137 return NTB_TOPO_NONE;
2140 static inline int xeon_ppd_bar4_split(struct intel_ntb_dev *ndev, u8 ppd)
2142 if (ppd & XEON_PPD_SPLIT_BAR_MASK) {
2143 dev_dbg(&ndev->ntb.pdev->dev, "PPD %d split bar\n", ppd);
2149 static int xeon_init_isr(struct intel_ntb_dev *ndev)
2151 return ndev_init_isr(ndev, XEON_DB_MSIX_VECTOR_COUNT,
2152 XEON_DB_MSIX_VECTOR_COUNT,
2153 XEON_DB_MSIX_VECTOR_SHIFT,
2154 XEON_DB_TOTAL_SHIFT);
2157 static void xeon_deinit_isr(struct intel_ntb_dev *ndev)
2159 ndev_deinit_isr(ndev);
2162 static int xeon_setup_b2b_mw(struct intel_ntb_dev *ndev,
2163 const struct intel_b2b_addr *addr,
2164 const struct intel_b2b_addr *peer_addr)
2166 struct pci_dev *pdev;
2168 resource_size_t bar_size;
2169 phys_addr_t bar_addr;
2173 pdev = ndev->ntb.pdev;
2174 mmio = ndev->self_mmio;
2176 if (ndev->b2b_idx == UINT_MAX) {
2177 dev_dbg(&pdev->dev, "not using b2b mw\n");
2181 b2b_bar = ndev_mw_to_bar(ndev, ndev->b2b_idx);
2185 dev_dbg(&pdev->dev, "using b2b mw bar %d\n", b2b_bar);
2187 bar_size = pci_resource_len(ndev->ntb.pdev, b2b_bar);
2189 dev_dbg(&pdev->dev, "b2b bar size %#llx\n", bar_size);
2191 if (b2b_mw_share && XEON_B2B_MIN_SIZE <= bar_size >> 1) {
2192 dev_dbg(&pdev->dev, "b2b using first half of bar\n");
2193 ndev->b2b_off = bar_size >> 1;
2194 } else if (XEON_B2B_MIN_SIZE <= bar_size) {
2195 dev_dbg(&pdev->dev, "b2b using whole bar\n");
2199 dev_dbg(&pdev->dev, "b2b bar size is too small\n");
2204 /* Reset the secondary bar sizes to match the primary bar sizes,
2205 * except disable or halve the size of the b2b secondary bar.
2207 * Note: code for each specific bar size register, because the register
2208 * offsets are not in a consistent order (bar5sz comes after ppd, odd).
2210 pci_read_config_byte(pdev, XEON_PBAR23SZ_OFFSET, &bar_sz);
2211 dev_dbg(&pdev->dev, "PBAR23SZ %#x\n", bar_sz);
2218 pci_write_config_byte(pdev, XEON_SBAR23SZ_OFFSET, bar_sz);
2219 pci_read_config_byte(pdev, XEON_SBAR23SZ_OFFSET, &bar_sz);
2220 dev_dbg(&pdev->dev, "SBAR23SZ %#x\n", bar_sz);
2222 if (!ndev->bar4_split) {
2223 pci_read_config_byte(pdev, XEON_PBAR45SZ_OFFSET, &bar_sz);
2224 dev_dbg(&pdev->dev, "PBAR45SZ %#x\n", bar_sz);
2231 pci_write_config_byte(pdev, XEON_SBAR45SZ_OFFSET, bar_sz);
2232 pci_read_config_byte(pdev, XEON_SBAR45SZ_OFFSET, &bar_sz);
2233 dev_dbg(&pdev->dev, "SBAR45SZ %#x\n", bar_sz);
2235 pci_read_config_byte(pdev, XEON_PBAR4SZ_OFFSET, &bar_sz);
2236 dev_dbg(&pdev->dev, "PBAR4SZ %#x\n", bar_sz);
2243 pci_write_config_byte(pdev, XEON_SBAR4SZ_OFFSET, bar_sz);
2244 pci_read_config_byte(pdev, XEON_SBAR4SZ_OFFSET, &bar_sz);
2245 dev_dbg(&pdev->dev, "SBAR4SZ %#x\n", bar_sz);
2247 pci_read_config_byte(pdev, XEON_PBAR5SZ_OFFSET, &bar_sz);
2248 dev_dbg(&pdev->dev, "PBAR5SZ %#x\n", bar_sz);
2255 pci_write_config_byte(pdev, XEON_SBAR5SZ_OFFSET, bar_sz);
2256 pci_read_config_byte(pdev, XEON_SBAR5SZ_OFFSET, &bar_sz);
2257 dev_dbg(&pdev->dev, "SBAR5SZ %#x\n", bar_sz);
2260 /* SBAR01 hit by first part of the b2b bar */
2262 bar_addr = addr->bar0_addr;
2263 else if (b2b_bar == 2)
2264 bar_addr = addr->bar2_addr64;
2265 else if (b2b_bar == 4 && !ndev->bar4_split)
2266 bar_addr = addr->bar4_addr64;
2267 else if (b2b_bar == 4)
2268 bar_addr = addr->bar4_addr32;
2269 else if (b2b_bar == 5)
2270 bar_addr = addr->bar5_addr32;
2274 dev_dbg(&pdev->dev, "SBAR01 %#018llx\n", bar_addr);
2275 iowrite64(bar_addr, mmio + XEON_SBAR0BASE_OFFSET);
2277 /* Other SBAR are normally hit by the PBAR xlat, except for b2b bar.
2278 * The b2b bar is either disabled above, or configured half-size, and
2279 * it starts at the PBAR xlat + offset.
2282 bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
2283 iowrite64(bar_addr, mmio + XEON_SBAR23BASE_OFFSET);
2284 bar_addr = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
2285 dev_dbg(&pdev->dev, "SBAR23 %#018llx\n", bar_addr);
2287 if (!ndev->bar4_split) {
2288 bar_addr = addr->bar4_addr64 +
2289 (b2b_bar == 4 ? ndev->b2b_off : 0);
2290 iowrite64(bar_addr, mmio + XEON_SBAR45BASE_OFFSET);
2291 bar_addr = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
2292 dev_dbg(&pdev->dev, "SBAR45 %#018llx\n", bar_addr);
2294 bar_addr = addr->bar4_addr32 +
2295 (b2b_bar == 4 ? ndev->b2b_off : 0);
2296 iowrite32(bar_addr, mmio + XEON_SBAR4BASE_OFFSET);
2297 bar_addr = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
2298 dev_dbg(&pdev->dev, "SBAR4 %#010llx\n", bar_addr);
2300 bar_addr = addr->bar5_addr32 +
2301 (b2b_bar == 5 ? ndev->b2b_off : 0);
2302 iowrite32(bar_addr, mmio + XEON_SBAR5BASE_OFFSET);
2303 bar_addr = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
2304 dev_dbg(&pdev->dev, "SBAR5 %#010llx\n", bar_addr);
2307 /* setup incoming bar limits == base addrs (zero length windows) */
2309 bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
2310 iowrite64(bar_addr, mmio + XEON_SBAR23LMT_OFFSET);
2311 bar_addr = ioread64(mmio + XEON_SBAR23LMT_OFFSET);
2312 dev_dbg(&pdev->dev, "SBAR23LMT %#018llx\n", bar_addr);
2314 if (!ndev->bar4_split) {
2315 bar_addr = addr->bar4_addr64 +
2316 (b2b_bar == 4 ? ndev->b2b_off : 0);
2317 iowrite64(bar_addr, mmio + XEON_SBAR45LMT_OFFSET);
2318 bar_addr = ioread64(mmio + XEON_SBAR45LMT_OFFSET);
2319 dev_dbg(&pdev->dev, "SBAR45LMT %#018llx\n", bar_addr);
2321 bar_addr = addr->bar4_addr32 +
2322 (b2b_bar == 4 ? ndev->b2b_off : 0);
2323 iowrite32(bar_addr, mmio + XEON_SBAR4LMT_OFFSET);
2324 bar_addr = ioread32(mmio + XEON_SBAR4LMT_OFFSET);
2325 dev_dbg(&pdev->dev, "SBAR4LMT %#010llx\n", bar_addr);
2327 bar_addr = addr->bar5_addr32 +
2328 (b2b_bar == 5 ? ndev->b2b_off : 0);
2329 iowrite32(bar_addr, mmio + XEON_SBAR5LMT_OFFSET);
2330 bar_addr = ioread32(mmio + XEON_SBAR5LMT_OFFSET);
2331 dev_dbg(&pdev->dev, "SBAR5LMT %#05llx\n", bar_addr);
2334 /* zero incoming translation addrs */
2335 iowrite64(0, mmio + XEON_SBAR23XLAT_OFFSET);
2337 if (!ndev->bar4_split) {
2338 iowrite64(0, mmio + XEON_SBAR45XLAT_OFFSET);
2340 iowrite32(0, mmio + XEON_SBAR4XLAT_OFFSET);
2341 iowrite32(0, mmio + XEON_SBAR5XLAT_OFFSET);
2344 /* zero outgoing translation limits (whole bar size windows) */
2345 iowrite64(0, mmio + XEON_PBAR23LMT_OFFSET);
2346 if (!ndev->bar4_split) {
2347 iowrite64(0, mmio + XEON_PBAR45LMT_OFFSET);
2349 iowrite32(0, mmio + XEON_PBAR4LMT_OFFSET);
2350 iowrite32(0, mmio + XEON_PBAR5LMT_OFFSET);
2353 /* set outgoing translation offsets */
2354 bar_addr = peer_addr->bar2_addr64;
2355 iowrite64(bar_addr, mmio + XEON_PBAR23XLAT_OFFSET);
2356 bar_addr = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
2357 dev_dbg(&pdev->dev, "PBAR23XLAT %#018llx\n", bar_addr);
2359 if (!ndev->bar4_split) {
2360 bar_addr = peer_addr->bar4_addr64;
2361 iowrite64(bar_addr, mmio + XEON_PBAR45XLAT_OFFSET);
2362 bar_addr = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
2363 dev_dbg(&pdev->dev, "PBAR45XLAT %#018llx\n", bar_addr);
2365 bar_addr = peer_addr->bar4_addr32;
2366 iowrite32(bar_addr, mmio + XEON_PBAR4XLAT_OFFSET);
2367 bar_addr = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
2368 dev_dbg(&pdev->dev, "PBAR4XLAT %#010llx\n", bar_addr);
2370 bar_addr = peer_addr->bar5_addr32;
2371 iowrite32(bar_addr, mmio + XEON_PBAR5XLAT_OFFSET);
2372 bar_addr = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
2373 dev_dbg(&pdev->dev, "PBAR5XLAT %#010llx\n", bar_addr);
2376 /* set the translation offset for b2b registers */
2378 bar_addr = peer_addr->bar0_addr;
2379 else if (b2b_bar == 2)
2380 bar_addr = peer_addr->bar2_addr64;
2381 else if (b2b_bar == 4 && !ndev->bar4_split)
2382 bar_addr = peer_addr->bar4_addr64;
2383 else if (b2b_bar == 4)
2384 bar_addr = peer_addr->bar4_addr32;
2385 else if (b2b_bar == 5)
2386 bar_addr = peer_addr->bar5_addr32;
2390 /* B2B_XLAT_OFFSET is 64bit, but can only take 32bit writes */
2391 dev_dbg(&pdev->dev, "B2BXLAT %#018llx\n", bar_addr);
2392 iowrite32(bar_addr, mmio + XEON_B2B_XLAT_OFFSETL);
2393 iowrite32(bar_addr >> 32, mmio + XEON_B2B_XLAT_OFFSETU);
2396 /* map peer ntb mmio config space registers */
2397 ndev->peer_mmio = pci_iomap(pdev, b2b_bar,
2399 if (!ndev->peer_mmio)
2402 ndev->peer_addr = pci_resource_start(pdev, b2b_bar);
2408 static int xeon_init_ntb(struct intel_ntb_dev *ndev)
2410 struct device *dev = &ndev->ntb.pdev->dev;
2414 if (ndev->bar4_split)
2415 ndev->mw_count = HSX_SPLIT_BAR_MW_COUNT;
2417 ndev->mw_count = XEON_MW_COUNT;
2419 ndev->spad_count = XEON_SPAD_COUNT;
2420 ndev->db_count = XEON_DB_COUNT;
2421 ndev->db_link_mask = XEON_DB_LINK_BIT;
2423 switch (ndev->ntb.topo) {
2425 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
2426 dev_err(dev, "NTB Primary config disabled\n");
2430 /* enable link to allow secondary side device to appear */
2431 ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
2432 ntb_ctl &= ~NTB_CTL_DISABLE;
2433 iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
2435 /* use half the spads for the peer */
2436 ndev->spad_count >>= 1;
2437 ndev->self_reg = &xeon_pri_reg;
2438 ndev->peer_reg = &xeon_sec_reg;
2439 ndev->xlat_reg = &xeon_sec_xlat;
2443 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
2444 dev_err(dev, "NTB Secondary config disabled\n");
2447 /* use half the spads for the peer */
2448 ndev->spad_count >>= 1;
2449 ndev->self_reg = &xeon_sec_reg;
2450 ndev->peer_reg = &xeon_pri_reg;
2451 ndev->xlat_reg = &xeon_pri_xlat;
2454 case NTB_TOPO_B2B_USD:
2455 case NTB_TOPO_B2B_DSD:
2456 ndev->self_reg = &xeon_pri_reg;
2457 ndev->peer_reg = &xeon_b2b_reg;
2458 ndev->xlat_reg = &xeon_sec_xlat;
2460 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
2461 ndev->peer_reg = &xeon_pri_reg;
2464 ndev->b2b_idx = b2b_mw_idx + ndev->mw_count;
2466 ndev->b2b_idx = b2b_mw_idx;
2468 if (ndev->b2b_idx >= ndev->mw_count) {
2470 "b2b_mw_idx %d invalid for mw_count %u\n",
2471 b2b_mw_idx, ndev->mw_count);
2475 dev_dbg(dev, "setting up b2b mw idx %d means %d\n",
2476 b2b_mw_idx, ndev->b2b_idx);
2478 } else if (ndev->hwerr_flags & NTB_HWERR_B2BDOORBELL_BIT14) {
2479 dev_warn(dev, "Reduce doorbell count by 1\n");
2480 ndev->db_count -= 1;
2483 if (ndev->ntb.topo == NTB_TOPO_B2B_USD) {
2484 rc = xeon_setup_b2b_mw(ndev,
2486 &xeon_b2b_usd_addr);
2488 rc = xeon_setup_b2b_mw(ndev,
2490 &xeon_b2b_dsd_addr);
2495 /* Enable Bus Master and Memory Space on the secondary side */
2496 iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
2497 ndev->self_mmio + XEON_SPCICMD_OFFSET);
2505 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
2507 ndev->reg->db_iowrite(ndev->db_valid_mask,
2509 ndev->self_reg->db_mask);
2514 static int xeon_init_dev(struct intel_ntb_dev *ndev)
2516 struct pci_dev *pdev;
2520 pdev = ndev->ntb.pdev;
2522 switch (pdev->device) {
2523 /* There is a Xeon hardware errata related to writes to SDOORBELL or
2524 * B2BDOORBELL in conjunction with inbound access to NTB MMIO Space,
2525 * which may hang the system. To workaround this use the second memory
2526 * window to access the interrupt and scratch pad registers on the
2529 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
2530 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
2531 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
2532 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
2533 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
2534 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
2535 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
2536 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
2537 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
2538 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
2539 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
2540 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
2541 case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
2542 case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
2543 case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
2544 ndev->hwerr_flags |= NTB_HWERR_SDOORBELL_LOCKUP;
2548 switch (pdev->device) {
2549 /* There is a hardware errata related to accessing any register in
2550 * SB01BASE in the presence of bidirectional traffic crossing the NTB.
2552 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
2553 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
2554 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
2555 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
2556 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
2557 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
2558 case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
2559 case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
2560 case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
2561 ndev->hwerr_flags |= NTB_HWERR_SB01BASE_LOCKUP;
2565 switch (pdev->device) {
2566 /* HW Errata on bit 14 of b2bdoorbell register. Writes will not be
2567 * mirrored to the remote system. Shrink the number of bits by one,
2568 * since bit 14 is the last bit.
2570 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
2571 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
2572 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
2573 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
2574 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
2575 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
2576 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
2577 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
2578 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
2579 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
2580 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
2581 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
2582 case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
2583 case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
2584 case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
2585 ndev->hwerr_flags |= NTB_HWERR_B2BDOORBELL_BIT14;
2589 ndev->reg = &xeon_reg;
2591 rc = pci_read_config_byte(pdev, XEON_PPD_OFFSET, &ppd);
2595 ndev->ntb.topo = xeon_ppd_topo(ndev, ppd);
2596 dev_dbg(&pdev->dev, "ppd %#x topo %s\n", ppd,
2597 ntb_topo_string(ndev->ntb.topo));
2598 if (ndev->ntb.topo == NTB_TOPO_NONE)
2601 if (ndev->ntb.topo != NTB_TOPO_SEC) {
2602 ndev->bar4_split = xeon_ppd_bar4_split(ndev, ppd);
2603 dev_dbg(&pdev->dev, "ppd %#x bar4_split %d\n",
2604 ppd, ndev->bar4_split);
2606 /* This is a way for transparent BAR to figure out if we are
2607 * doing split BAR or not. There is no way for the hw on the
2608 * transparent side to know and set the PPD.
2610 mem = pci_select_bars(pdev, IORESOURCE_MEM);
2611 ndev->bar4_split = hweight32(mem) ==
2612 HSX_SPLIT_BAR_MW_COUNT + 1;
2613 dev_dbg(&pdev->dev, "mem %#x bar4_split %d\n",
2614 mem, ndev->bar4_split);
2617 rc = xeon_init_ntb(ndev);
2621 return xeon_init_isr(ndev);
2624 static void xeon_deinit_dev(struct intel_ntb_dev *ndev)
2626 xeon_deinit_isr(ndev);
2629 static int intel_ntb_init_pci(struct intel_ntb_dev *ndev, struct pci_dev *pdev)
2633 pci_set_drvdata(pdev, ndev);
2635 rc = pci_enable_device(pdev);
2637 goto err_pci_enable;
2639 rc = pci_request_regions(pdev, NTB_NAME);
2641 goto err_pci_regions;
2643 pci_set_master(pdev);
2645 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2647 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2650 dev_warn(&pdev->dev, "Cannot DMA highmem\n");
2653 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2655 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2658 dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
2661 ndev->self_mmio = pci_iomap(pdev, 0, 0);
2662 if (!ndev->self_mmio) {
2666 ndev->peer_mmio = ndev->self_mmio;
2667 ndev->peer_addr = pci_resource_start(pdev, 0);
2673 pci_clear_master(pdev);
2674 pci_release_regions(pdev);
2676 pci_disable_device(pdev);
2678 pci_set_drvdata(pdev, NULL);
2682 static void intel_ntb_deinit_pci(struct intel_ntb_dev *ndev)
2684 struct pci_dev *pdev = ndev->ntb.pdev;
2686 if (ndev->peer_mmio && ndev->peer_mmio != ndev->self_mmio)
2687 pci_iounmap(pdev, ndev->peer_mmio);
2688 pci_iounmap(pdev, ndev->self_mmio);
2690 pci_clear_master(pdev);
2691 pci_release_regions(pdev);
2692 pci_disable_device(pdev);
2693 pci_set_drvdata(pdev, NULL);
2696 static inline void ndev_init_struct(struct intel_ntb_dev *ndev,
2697 struct pci_dev *pdev)
2699 ndev->ntb.pdev = pdev;
2700 ndev->ntb.topo = NTB_TOPO_NONE;
2701 ndev->ntb.ops = &intel_ntb_ops;
2704 ndev->b2b_idx = UINT_MAX;
2706 ndev->bar4_split = 0;
2709 ndev->spad_count = 0;
2711 ndev->db_vec_count = 0;
2712 ndev->db_vec_shift = 0;
2717 ndev->db_valid_mask = 0;
2718 ndev->db_link_mask = 0;
2721 spin_lock_init(&ndev->db_mask_lock);
2724 static int intel_ntb_pci_probe(struct pci_dev *pdev,
2725 const struct pci_device_id *id)
2727 struct intel_ntb_dev *ndev;
2730 node = dev_to_node(&pdev->dev);
2732 if (pdev_is_atom(pdev)) {
2733 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
2739 ndev_init_struct(ndev, pdev);
2741 rc = intel_ntb_init_pci(ndev, pdev);
2745 rc = atom_init_dev(ndev);
2749 } else if (pdev_is_xeon(pdev)) {
2750 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
2756 ndev_init_struct(ndev, pdev);
2758 rc = intel_ntb_init_pci(ndev, pdev);
2762 rc = xeon_init_dev(ndev);
2766 } else if (pdev_is_skx_xeon(pdev)) {
2767 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
2773 ndev_init_struct(ndev, pdev);
2774 ndev->ntb.ops = &intel_ntb3_ops;
2776 rc = intel_ntb_init_pci(ndev, pdev);
2780 rc = skx_init_dev(ndev);
2789 ndev_reset_unsafe_flags(ndev);
2791 ndev->reg->poll_link(ndev);
2793 ndev_init_debugfs(ndev);
2795 rc = ntb_register_device(&ndev->ntb);
2799 dev_info(&pdev->dev, "NTB device registered.\n");
2804 ndev_deinit_debugfs(ndev);
2805 if (pdev_is_atom(pdev))
2806 atom_deinit_dev(ndev);
2807 else if (pdev_is_xeon(pdev) || pdev_is_skx_xeon(pdev))
2808 xeon_deinit_dev(ndev);
2810 intel_ntb_deinit_pci(ndev);
2817 static void intel_ntb_pci_remove(struct pci_dev *pdev)
2819 struct intel_ntb_dev *ndev = pci_get_drvdata(pdev);
2821 ntb_unregister_device(&ndev->ntb);
2822 ndev_deinit_debugfs(ndev);
2823 if (pdev_is_atom(pdev))
2824 atom_deinit_dev(ndev);
2825 else if (pdev_is_xeon(pdev) || pdev_is_skx_xeon(pdev))
2826 xeon_deinit_dev(ndev);
2827 intel_ntb_deinit_pci(ndev);
2831 static const struct intel_ntb_reg atom_reg = {
2832 .poll_link = atom_poll_link,
2833 .link_is_up = atom_link_is_up,
2834 .db_ioread = atom_db_ioread,
2835 .db_iowrite = atom_db_iowrite,
2836 .db_size = sizeof(u64),
2837 .ntb_ctl = ATOM_NTBCNTL_OFFSET,
2841 static const struct intel_ntb_alt_reg atom_pri_reg = {
2842 .db_bell = ATOM_PDOORBELL_OFFSET,
2843 .db_mask = ATOM_PDBMSK_OFFSET,
2844 .spad = ATOM_SPAD_OFFSET,
2847 static const struct intel_ntb_alt_reg atom_b2b_reg = {
2848 .db_bell = ATOM_B2B_DOORBELL_OFFSET,
2849 .spad = ATOM_B2B_SPAD_OFFSET,
2852 static const struct intel_ntb_xlat_reg atom_sec_xlat = {
2853 /* FIXME : .bar0_base = ATOM_SBAR0BASE_OFFSET, */
2854 /* FIXME : .bar2_limit = ATOM_SBAR2LMT_OFFSET, */
2855 .bar2_xlat = ATOM_SBAR2XLAT_OFFSET,
2858 static const struct intel_ntb_reg xeon_reg = {
2859 .poll_link = xeon_poll_link,
2860 .link_is_up = xeon_link_is_up,
2861 .db_ioread = xeon_db_ioread,
2862 .db_iowrite = xeon_db_iowrite,
2863 .db_size = sizeof(u32),
2864 .ntb_ctl = XEON_NTBCNTL_OFFSET,
2865 .mw_bar = {2, 4, 5},
2868 static const struct intel_ntb_alt_reg xeon_pri_reg = {
2869 .db_bell = XEON_PDOORBELL_OFFSET,
2870 .db_mask = XEON_PDBMSK_OFFSET,
2871 .spad = XEON_SPAD_OFFSET,
2874 static const struct intel_ntb_alt_reg xeon_sec_reg = {
2875 .db_bell = XEON_SDOORBELL_OFFSET,
2876 .db_mask = XEON_SDBMSK_OFFSET,
2877 /* second half of the scratchpads */
2878 .spad = XEON_SPAD_OFFSET + (XEON_SPAD_COUNT << 1),
2881 static const struct intel_ntb_alt_reg xeon_b2b_reg = {
2882 .db_bell = XEON_B2B_DOORBELL_OFFSET,
2883 .spad = XEON_B2B_SPAD_OFFSET,
2886 static const struct intel_ntb_xlat_reg xeon_pri_xlat = {
2887 /* Note: no primary .bar0_base visible to the secondary side.
2889 * The secondary side cannot get the base address stored in primary
2890 * bars. The base address is necessary to set the limit register to
2891 * any value other than zero, or unlimited.
2893 * WITHOUT THE BASE ADDRESS, THE SECONDARY SIDE CANNOT DISABLE the
2894 * window by setting the limit equal to base, nor can it limit the size
2895 * of the memory window by setting the limit to base + size.
2897 .bar2_limit = XEON_PBAR23LMT_OFFSET,
2898 .bar2_xlat = XEON_PBAR23XLAT_OFFSET,
2901 static const struct intel_ntb_xlat_reg xeon_sec_xlat = {
2902 .bar0_base = XEON_SBAR0BASE_OFFSET,
2903 .bar2_limit = XEON_SBAR23LMT_OFFSET,
2904 .bar2_xlat = XEON_SBAR23XLAT_OFFSET,
2907 static struct intel_b2b_addr xeon_b2b_usd_addr = {
2908 .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
2909 .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
2910 .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
2911 .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
2914 static struct intel_b2b_addr xeon_b2b_dsd_addr = {
2915 .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
2916 .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
2917 .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
2918 .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
2921 static const struct intel_ntb_reg skx_reg = {
2922 .poll_link = skx_poll_link,
2923 .link_is_up = xeon_link_is_up,
2924 .db_ioread = skx_db_ioread,
2925 .db_iowrite = skx_db_iowrite,
2926 .db_size = sizeof(u32),
2927 .ntb_ctl = SKX_NTBCNTL_OFFSET,
2931 static const struct intel_ntb_alt_reg skx_pri_reg = {
2932 .db_bell = SKX_EM_DOORBELL_OFFSET,
2933 .db_clear = SKX_IM_INT_STATUS_OFFSET,
2934 .db_mask = SKX_IM_INT_DISABLE_OFFSET,
2935 .spad = SKX_IM_SPAD_OFFSET,
2938 static const struct intel_ntb_alt_reg skx_b2b_reg = {
2939 .db_bell = SKX_IM_DOORBELL_OFFSET,
2940 .db_clear = SKX_EM_INT_STATUS_OFFSET,
2941 .db_mask = SKX_EM_INT_DISABLE_OFFSET,
2942 .spad = SKX_B2B_SPAD_OFFSET,
2945 static const struct intel_ntb_xlat_reg skx_sec_xlat = {
2946 /* .bar0_base = SKX_EMBAR0_OFFSET, */
2947 .bar2_limit = SKX_IMBAR1XLMT_OFFSET,
2948 .bar2_xlat = SKX_IMBAR1XBASE_OFFSET,
2951 /* operations for primary side of local ntb */
2952 static const struct ntb_dev_ops intel_ntb_ops = {
2953 .mw_count = intel_ntb_mw_count,
2954 .mw_get_align = intel_ntb_mw_get_align,
2955 .mw_set_trans = intel_ntb_mw_set_trans,
2956 .peer_mw_count = intel_ntb_peer_mw_count,
2957 .peer_mw_get_addr = intel_ntb_peer_mw_get_addr,
2958 .link_is_up = intel_ntb_link_is_up,
2959 .link_enable = intel_ntb_link_enable,
2960 .link_disable = intel_ntb_link_disable,
2961 .db_is_unsafe = intel_ntb_db_is_unsafe,
2962 .db_valid_mask = intel_ntb_db_valid_mask,
2963 .db_vector_count = intel_ntb_db_vector_count,
2964 .db_vector_mask = intel_ntb_db_vector_mask,
2965 .db_read = intel_ntb_db_read,
2966 .db_clear = intel_ntb_db_clear,
2967 .db_set_mask = intel_ntb_db_set_mask,
2968 .db_clear_mask = intel_ntb_db_clear_mask,
2969 .peer_db_addr = intel_ntb_peer_db_addr,
2970 .peer_db_set = intel_ntb_peer_db_set,
2971 .spad_is_unsafe = intel_ntb_spad_is_unsafe,
2972 .spad_count = intel_ntb_spad_count,
2973 .spad_read = intel_ntb_spad_read,
2974 .spad_write = intel_ntb_spad_write,
2975 .peer_spad_addr = intel_ntb_peer_spad_addr,
2976 .peer_spad_read = intel_ntb_peer_spad_read,
2977 .peer_spad_write = intel_ntb_peer_spad_write,
2980 static const struct ntb_dev_ops intel_ntb3_ops = {
2981 .mw_count = intel_ntb_mw_count,
2982 .mw_get_align = intel_ntb_mw_get_align,
2983 .mw_set_trans = intel_ntb3_mw_set_trans,
2984 .peer_mw_count = intel_ntb_peer_mw_count,
2985 .peer_mw_get_addr = intel_ntb_peer_mw_get_addr,
2986 .link_is_up = intel_ntb_link_is_up,
2987 .link_enable = intel_ntb3_link_enable,
2988 .link_disable = intel_ntb_link_disable,
2989 .db_valid_mask = intel_ntb_db_valid_mask,
2990 .db_vector_count = intel_ntb_db_vector_count,
2991 .db_vector_mask = intel_ntb_db_vector_mask,
2992 .db_read = intel_ntb3_db_read,
2993 .db_clear = intel_ntb3_db_clear,
2994 .db_set_mask = intel_ntb_db_set_mask,
2995 .db_clear_mask = intel_ntb_db_clear_mask,
2996 .peer_db_addr = intel_ntb_peer_db_addr,
2997 .peer_db_set = intel_ntb3_peer_db_set,
2998 .spad_is_unsafe = intel_ntb_spad_is_unsafe,
2999 .spad_count = intel_ntb_spad_count,
3000 .spad_read = intel_ntb_spad_read,
3001 .spad_write = intel_ntb_spad_write,
3002 .peer_spad_addr = intel_ntb_peer_spad_addr,
3003 .peer_spad_read = intel_ntb_peer_spad_read,
3004 .peer_spad_write = intel_ntb_peer_spad_write,
3007 static const struct file_operations intel_ntb_debugfs_info = {
3008 .owner = THIS_MODULE,
3009 .open = simple_open,
3010 .read = ndev_debugfs_read,
3013 static const struct pci_device_id intel_ntb_pci_tbl[] = {
3014 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
3015 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
3016 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
3017 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
3018 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
3019 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BDX)},
3020 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
3021 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
3022 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
3023 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
3024 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_BDX)},
3025 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
3026 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
3027 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
3028 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
3029 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_BDX)},
3030 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SKX)},
3033 MODULE_DEVICE_TABLE(pci, intel_ntb_pci_tbl);
3035 static struct pci_driver intel_ntb_pci_driver = {
3036 .name = KBUILD_MODNAME,
3037 .id_table = intel_ntb_pci_tbl,
3038 .probe = intel_ntb_pci_probe,
3039 .remove = intel_ntb_pci_remove,
3042 static int __init intel_ntb_pci_driver_init(void)
3044 pr_info("%s %s\n", NTB_DESC, NTB_VER);
3046 if (debugfs_initialized())
3047 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
3049 return pci_register_driver(&intel_ntb_pci_driver);
3051 module_init(intel_ntb_pci_driver_init);
3053 static void __exit intel_ntb_pci_driver_exit(void)
3055 pci_unregister_driver(&intel_ntb_pci_driver);
3057 debugfs_remove_recursive(debugfs_dir);
3059 module_exit(intel_ntb_pci_driver_exit);