]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/ntb/hw/intel/ntb_hw_intel.c
NTB: Add flow control to the ntb_netdev
[mirror_ubuntu-bionic-kernel.git] / drivers / ntb / hw / intel / ntb_hw_intel.c
CommitLineData
fce8a7bb
JM
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.
e26a5843 8 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
fce8a7bb
JM
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
13 *
14 * BSD LICENSE
15 *
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
e26a5843 17 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
fce8a7bb
JM
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 *
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
28 * distribution.
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
32 *
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 *
45 * Intel PCIe NTB Linux driver
46 *
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
49 */
e26a5843 50
fce8a7bb 51#include <linux/debugfs.h>
113bf1c9 52#include <linux/delay.h>
fce8a7bb
JM
53#include <linux/init.h>
54#include <linux/interrupt.h>
55#include <linux/module.h>
56#include <linux/pci.h>
113bf1c9 57#include <linux/random.h>
fce8a7bb 58#include <linux/slab.h>
e26a5843
AH
59#include <linux/ntb.h>
60
ec110bc7 61#include "ntb_hw_intel.h"
fce8a7bb 62
e26a5843
AH
63#define NTB_NAME "ntb_hw_intel"
64#define NTB_DESC "Intel(R) PCI-E Non-Transparent Bridge Driver"
65#define NTB_VER "2.0"
fce8a7bb 66
e26a5843 67MODULE_DESCRIPTION(NTB_DESC);
fce8a7bb
JM
68MODULE_VERSION(NTB_VER);
69MODULE_LICENSE("Dual BSD/GPL");
70MODULE_AUTHOR("Intel Corporation");
71
e26a5843
AH
72#define bar0_off(base, bar) ((base) + ((bar) << 2))
73#define bar2_off(base, bar) bar0_off(base, (bar) - 2)
74
2f887b9a
DJ
75static const struct intel_ntb_reg atom_reg;
76static const struct intel_ntb_alt_reg atom_pri_reg;
77static const struct intel_ntb_alt_reg atom_sec_reg;
78static const struct intel_ntb_alt_reg atom_b2b_reg;
79static const struct intel_ntb_xlat_reg atom_pri_xlat;
80static const struct intel_ntb_xlat_reg atom_sec_xlat;
81static const struct intel_ntb_reg xeon_reg;
82static const struct intel_ntb_alt_reg xeon_pri_reg;
83static const struct intel_ntb_alt_reg xeon_sec_reg;
84static const struct intel_ntb_alt_reg xeon_b2b_reg;
85static const struct intel_ntb_xlat_reg xeon_pri_xlat;
86static const struct intel_ntb_xlat_reg xeon_sec_xlat;
87static struct intel_b2b_addr xeon_b2b_usd_addr;
88static struct intel_b2b_addr xeon_b2b_dsd_addr;
e26a5843
AH
89static const struct ntb_dev_ops intel_ntb_ops;
90
91static const struct file_operations intel_ntb_debugfs_info;
1517a3f2
JM
92static struct dentry *debugfs_dir;
93
42fefc86
AH
94static int b2b_mw_idx = -1;
95module_param(b2b_mw_idx, int, 0644);
96MODULE_PARM_DESC(b2b_mw_idx, "Use this mw idx to access the peer ntb. A "
97 "value of zero or positive starts from first mw idx, and a "
98 "negative value starts from last mw idx. Both sides MUST "
99 "set the same value here!");
100
101static unsigned int b2b_mw_share;
102module_param(b2b_mw_share, uint, 0644);
103MODULE_PARM_DESC(b2b_mw_share, "If the b2b mw is large enough, configure the "
104 "ntb so that the peer ntb only occupies the first half of "
105 "the mw, so the second half can still be used as a mw. Both "
106 "sides MUST set the same value here!");
107
2f887b9a
DJ
108module_param_named(xeon_b2b_usd_bar2_addr64,
109 xeon_b2b_usd_addr.bar2_addr64, ullong, 0644);
110MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
111 "XEON B2B USD BAR 2 64-bit address");
112
113module_param_named(xeon_b2b_usd_bar4_addr64,
114 xeon_b2b_usd_addr.bar4_addr64, ullong, 0644);
115MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
116 "XEON B2B USD BAR 4 64-bit address");
117
118module_param_named(xeon_b2b_usd_bar4_addr32,
119 xeon_b2b_usd_addr.bar4_addr32, ullong, 0644);
120MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
121 "XEON B2B USD split-BAR 4 32-bit address");
122
123module_param_named(xeon_b2b_usd_bar5_addr32,
124 xeon_b2b_usd_addr.bar5_addr32, ullong, 0644);
125MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
126 "XEON B2B USD split-BAR 5 32-bit address");
127
128module_param_named(xeon_b2b_dsd_bar2_addr64,
129 xeon_b2b_dsd_addr.bar2_addr64, ullong, 0644);
130MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
131 "XEON B2B DSD BAR 2 64-bit address");
132
133module_param_named(xeon_b2b_dsd_bar4_addr64,
134 xeon_b2b_dsd_addr.bar4_addr64, ullong, 0644);
135MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
136 "XEON B2B DSD BAR 4 64-bit address");
137
138module_param_named(xeon_b2b_dsd_bar4_addr32,
139 xeon_b2b_dsd_addr.bar4_addr32, ullong, 0644);
140MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
141 "XEON B2B DSD split-BAR 4 32-bit address");
142
143module_param_named(xeon_b2b_dsd_bar5_addr32,
144 xeon_b2b_dsd_addr.bar5_addr32, ullong, 0644);
145MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
146 "XEON B2B DSD split-BAR 5 32-bit address");
42fefc86 147
e26a5843
AH
148#ifndef ioread64
149#ifdef readq
150#define ioread64 readq
151#else
152#define ioread64 _ioread64
153static inline u64 _ioread64(void __iomem *mmio)
b775e85b 154{
e26a5843 155 u64 low, high;
b775e85b 156
e26a5843
AH
157 low = ioread32(mmio);
158 high = ioread32(mmio + sizeof(u32));
159 return low | (high << 32);
160}
161#endif
162#endif
163
164#ifndef iowrite64
165#ifdef writeq
166#define iowrite64 writeq
167#else
168#define iowrite64 _iowrite64
169static inline void _iowrite64(u64 val, void __iomem *mmio)
170{
171 iowrite32(val, mmio);
172 iowrite32(val >> 32, mmio + sizeof(u32));
b775e85b 173}
e26a5843
AH
174#endif
175#endif
b775e85b 176
2f887b9a 177static inline int pdev_is_atom(struct pci_dev *pdev)
b775e85b 178{
e26a5843 179 switch (pdev->device) {
b775e85b
DJ
180 case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
181 return 1;
b775e85b 182 }
b775e85b
DJ
183 return 0;
184}
185
2f887b9a 186static inline int pdev_is_xeon(struct pci_dev *pdev)
069684e8 187{
e26a5843 188 switch (pdev->device) {
069684e8
DJ
189 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
190 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
191 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
192 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
193 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
194 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
195 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
196 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
197 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
198 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
199 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
200 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
e26a5843 201 return 1;
069684e8 202 }
e26a5843 203 return 0;
069684e8
DJ
204}
205
e26a5843 206static inline void ndev_reset_unsafe_flags(struct intel_ntb_dev *ndev)
fce8a7bb 207{
e26a5843
AH
208 ndev->unsafe_flags = 0;
209 ndev->unsafe_flags_ignore = 0;
210
211 /* Only B2B has a workaround to avoid SDOORBELL */
212 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP)
213 if (!ntb_topo_is_b2b(ndev->ntb.topo))
214 ndev->unsafe_flags |= NTB_UNSAFE_DB;
215
216 /* No low level workaround to avoid SB01BASE */
217 if (ndev->hwerr_flags & NTB_HWERR_SB01BASE_LOCKUP) {
218 ndev->unsafe_flags |= NTB_UNSAFE_DB;
219 ndev->unsafe_flags |= NTB_UNSAFE_SPAD;
220 }
fce8a7bb
JM
221}
222
e26a5843
AH
223static inline int ndev_is_unsafe(struct intel_ntb_dev *ndev,
224 unsigned long flag)
fce8a7bb 225{
e26a5843 226 return !!(flag & ndev->unsafe_flags & ~ndev->unsafe_flags_ignore);
fce8a7bb
JM
227}
228
e26a5843
AH
229static inline int ndev_ignore_unsafe(struct intel_ntb_dev *ndev,
230 unsigned long flag)
e8aeb60c 231{
e26a5843
AH
232 flag &= ndev->unsafe_flags;
233 ndev->unsafe_flags_ignore |= flag;
e8aeb60c 234
e26a5843 235 return !!flag;
e8aeb60c
JM
236}
237
e26a5843 238static int ndev_mw_to_bar(struct intel_ntb_dev *ndev, int idx)
fce8a7bb 239{
e26a5843 240 if (idx < 0 || idx > ndev->mw_count)
fce8a7bb 241 return -EINVAL;
e26a5843
AH
242 return ndev->reg->mw_bar[idx];
243}
fce8a7bb 244
e26a5843
AH
245static inline int ndev_db_addr(struct intel_ntb_dev *ndev,
246 phys_addr_t *db_addr, resource_size_t *db_size,
247 phys_addr_t reg_addr, unsigned long reg)
248{
fd839bf8
DJ
249 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
250 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
e8aeb60c 251
e26a5843
AH
252 if (db_addr) {
253 *db_addr = reg_addr + reg;
254 dev_dbg(ndev_dev(ndev), "Peer db addr %llx\n", *db_addr);
255 }
fce8a7bb 256
e26a5843
AH
257 if (db_size) {
258 *db_size = ndev->reg->db_size;
259 dev_dbg(ndev_dev(ndev), "Peer db size %llx\n", *db_size);
260 }
fce8a7bb
JM
261
262 return 0;
263}
264
e26a5843
AH
265static inline u64 ndev_db_read(struct intel_ntb_dev *ndev,
266 void __iomem *mmio)
fce8a7bb 267{
fd839bf8
DJ
268 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
269 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
fce8a7bb 270
e26a5843
AH
271 return ndev->reg->db_ioread(mmio);
272}
273
274static inline int ndev_db_write(struct intel_ntb_dev *ndev, u64 db_bits,
275 void __iomem *mmio)
276{
fd839bf8
DJ
277 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
278 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
fce8a7bb 279
e26a5843
AH
280 if (db_bits & ~ndev->db_valid_mask)
281 return -EINVAL;
fce8a7bb 282
e26a5843 283 ndev->reg->db_iowrite(db_bits, mmio);
e8aeb60c 284
e26a5843 285 return 0;
fce8a7bb
JM
286}
287
e26a5843
AH
288static inline int ndev_db_set_mask(struct intel_ntb_dev *ndev, u64 db_bits,
289 void __iomem *mmio)
fce8a7bb 290{
e26a5843 291 unsigned long irqflags;
fce8a7bb 292
fd839bf8
DJ
293 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
294 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
e26a5843
AH
295
296 if (db_bits & ~ndev->db_valid_mask)
297 return -EINVAL;
fce8a7bb 298
e26a5843
AH
299 spin_lock_irqsave(&ndev->db_mask_lock, irqflags);
300 {
301 ndev->db_mask |= db_bits;
302 ndev->reg->db_iowrite(ndev->db_mask, mmio);
303 }
304 spin_unlock_irqrestore(&ndev->db_mask_lock, irqflags);
fce8a7bb 305
e26a5843 306 return 0;
fce8a7bb
JM
307}
308
e26a5843
AH
309static inline int ndev_db_clear_mask(struct intel_ntb_dev *ndev, u64 db_bits,
310 void __iomem *mmio)
fce8a7bb 311{
e26a5843 312 unsigned long irqflags;
fce8a7bb 313
fd839bf8
DJ
314 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
315 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
e26a5843
AH
316
317 if (db_bits & ~ndev->db_valid_mask)
318 return -EINVAL;
fce8a7bb 319
e26a5843
AH
320 spin_lock_irqsave(&ndev->db_mask_lock, irqflags);
321 {
322 ndev->db_mask &= ~db_bits;
323 ndev->reg->db_iowrite(ndev->db_mask, mmio);
324 }
325 spin_unlock_irqrestore(&ndev->db_mask_lock, irqflags);
fce8a7bb 326
e26a5843 327 return 0;
fce8a7bb
JM
328}
329
e26a5843 330static inline int ndev_vec_mask(struct intel_ntb_dev *ndev, int db_vector)
fce8a7bb 331{
e26a5843 332 u64 shift, mask;
fce8a7bb 333
e26a5843
AH
334 shift = ndev->db_vec_shift;
335 mask = BIT_ULL(shift) - 1;
fce8a7bb 336
e26a5843 337 return mask << (shift * db_vector);
fce8a7bb
JM
338}
339
e26a5843
AH
340static inline int ndev_spad_addr(struct intel_ntb_dev *ndev, int idx,
341 phys_addr_t *spad_addr, phys_addr_t reg_addr,
342 unsigned long reg)
fce8a7bb 343{
fd839bf8
DJ
344 if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
345 pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
e26a5843
AH
346
347 if (idx < 0 || idx >= ndev->spad_count)
fce8a7bb
JM
348 return -EINVAL;
349
e26a5843
AH
350 if (spad_addr) {
351 *spad_addr = reg_addr + reg + (idx << 2);
352 dev_dbg(ndev_dev(ndev), "Peer spad addr %llx\n", *spad_addr);
353 }
fce8a7bb
JM
354
355 return 0;
356}
357
e26a5843
AH
358static inline u32 ndev_spad_read(struct intel_ntb_dev *ndev, int idx,
359 void __iomem *mmio)
fce8a7bb 360{
fd839bf8
DJ
361 if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
362 pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
fce8a7bb 363
e26a5843
AH
364 if (idx < 0 || idx >= ndev->spad_count)
365 return 0;
fce8a7bb 366
e26a5843 367 return ioread32(mmio + (idx << 2));
fce8a7bb
JM
368}
369
e26a5843
AH
370static inline int ndev_spad_write(struct intel_ntb_dev *ndev, int idx, u32 val,
371 void __iomem *mmio)
fce8a7bb 372{
fd839bf8
DJ
373 if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
374 pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
e26a5843
AH
375
376 if (idx < 0 || idx >= ndev->spad_count)
fce8a7bb
JM
377 return -EINVAL;
378
e26a5843 379 iowrite32(val, mmio + (idx << 2));
fce8a7bb
JM
380
381 return 0;
382}
383
e26a5843 384static irqreturn_t ndev_interrupt(struct intel_ntb_dev *ndev, int vec)
282a2fee 385{
e26a5843
AH
386 u64 vec_mask;
387
388 vec_mask = ndev_vec_mask(ndev, vec);
389
390 dev_dbg(ndev_dev(ndev), "vec %d vec_mask %llx\n", vec, vec_mask);
282a2fee 391
e26a5843
AH
392 ndev->last_ts = jiffies;
393
394 if (vec_mask & ndev->db_link_mask) {
395 if (ndev->reg->poll_link(ndev))
396 ntb_link_event(&ndev->ntb);
397 }
398
399 if (vec_mask & ndev->db_valid_mask)
400 ntb_db_event(&ndev->ntb, vec);
401
402 return IRQ_HANDLED;
282a2fee
JM
403}
404
e26a5843 405static irqreturn_t ndev_vec_isr(int irq, void *dev)
fce8a7bb 406{
e26a5843 407 struct intel_ntb_vec *nvec = dev;
fce8a7bb 408
e26a5843 409 return ndev_interrupt(nvec->ndev, nvec->num);
fce8a7bb
JM
410}
411
e26a5843 412static irqreturn_t ndev_irq_isr(int irq, void *dev)
fce8a7bb 413{
e26a5843 414 struct intel_ntb_dev *ndev = dev;
fce8a7bb 415
e26a5843 416 return ndev_interrupt(ndev, irq - ndev_pdev(ndev)->irq);
fce8a7bb
JM
417}
418
e26a5843
AH
419static int ndev_init_isr(struct intel_ntb_dev *ndev,
420 int msix_min, int msix_max,
421 int msix_shift, int total_shift)
fce8a7bb 422{
e26a5843 423 struct pci_dev *pdev;
0e041fb5 424 int rc, i, msix_count, node;
fce8a7bb 425
e26a5843 426 pdev = ndev_pdev(ndev);
fce8a7bb 427
0e041fb5
AH
428 node = dev_to_node(&pdev->dev);
429
e26a5843
AH
430 /* Mask all doorbell interrupts */
431 ndev->db_mask = ndev->db_valid_mask;
432 ndev->reg->db_iowrite(ndev->db_mask,
433 ndev->self_mmio +
434 ndev->self_reg->db_mask);
fce8a7bb 435
e26a5843
AH
436 /* Try to set up msix irq */
437
0e041fb5
AH
438 ndev->vec = kzalloc_node(msix_max * sizeof(*ndev->vec),
439 GFP_KERNEL, node);
e26a5843
AH
440 if (!ndev->vec)
441 goto err_msix_vec_alloc;
442
0e041fb5
AH
443 ndev->msix = kzalloc_node(msix_max * sizeof(*ndev->msix),
444 GFP_KERNEL, node);
e26a5843
AH
445 if (!ndev->msix)
446 goto err_msix_alloc;
447
448 for (i = 0; i < msix_max; ++i)
449 ndev->msix[i].entry = i;
450
451 msix_count = pci_enable_msix_range(pdev, ndev->msix,
452 msix_min, msix_max);
453 if (msix_count < 0)
454 goto err_msix_enable;
455
456 for (i = 0; i < msix_count; ++i) {
457 ndev->vec[i].ndev = ndev;
458 ndev->vec[i].num = i;
459 rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
460 "ndev_vec_isr", &ndev->vec[i]);
461 if (rc)
462 goto err_msix_request;
fce8a7bb 463 }
fce8a7bb 464
e26a5843
AH
465 dev_dbg(ndev_dev(ndev), "Using msix interrupts\n");
466 ndev->db_vec_count = msix_count;
467 ndev->db_vec_shift = msix_shift;
468 return 0;
fce8a7bb 469
e26a5843
AH
470err_msix_request:
471 while (i-- > 0)
472 free_irq(ndev->msix[i].vector, ndev);
473 pci_disable_msix(pdev);
474err_msix_enable:
475 kfree(ndev->msix);
476err_msix_alloc:
477 kfree(ndev->vec);
478err_msix_vec_alloc:
479 ndev->msix = NULL;
480 ndev->vec = NULL;
fce8a7bb 481
e26a5843 482 /* Try to set up msi irq */
113bf1c9 483
e26a5843
AH
484 rc = pci_enable_msi(pdev);
485 if (rc)
486 goto err_msi_enable;
113bf1c9 487
e26a5843
AH
488 rc = request_irq(pdev->irq, ndev_irq_isr, 0,
489 "ndev_irq_isr", ndev);
490 if (rc)
491 goto err_msi_request;
113bf1c9 492
e26a5843
AH
493 dev_dbg(ndev_dev(ndev), "Using msi interrupts\n");
494 ndev->db_vec_count = 1;
495 ndev->db_vec_shift = total_shift;
496 return 0;
113bf1c9 497
e26a5843
AH
498err_msi_request:
499 pci_disable_msi(pdev);
500err_msi_enable:
113bf1c9 501
e26a5843 502 /* Try to set up intx irq */
113bf1c9 503
e26a5843 504 pci_intx(pdev, 1);
113bf1c9 505
e26a5843
AH
506 rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
507 "ndev_irq_isr", ndev);
508 if (rc)
509 goto err_intx_request;
510
511 dev_dbg(ndev_dev(ndev), "Using intx interrupts\n");
512 ndev->db_vec_count = 1;
513 ndev->db_vec_shift = total_shift;
514 return 0;
515
516err_intx_request:
517 return rc;
113bf1c9
JM
518}
519
e26a5843 520static void ndev_deinit_isr(struct intel_ntb_dev *ndev)
fce8a7bb 521{
e26a5843
AH
522 struct pci_dev *pdev;
523 int i;
fce8a7bb 524
e26a5843 525 pdev = ndev_pdev(ndev);
fce8a7bb 526
e26a5843
AH
527 /* Mask all doorbell interrupts */
528 ndev->db_mask = ndev->db_valid_mask;
529 ndev->reg->db_iowrite(ndev->db_mask,
530 ndev->self_mmio +
531 ndev->self_reg->db_mask);
113bf1c9 532
e26a5843
AH
533 if (ndev->msix) {
534 i = ndev->db_vec_count;
535 while (i--)
536 free_irq(ndev->msix[i].vector, &ndev->vec[i]);
537 pci_disable_msix(pdev);
538 kfree(ndev->msix);
539 kfree(ndev->vec);
fce8a7bb 540 } else {
e26a5843
AH
541 free_irq(pdev->irq, ndev);
542 if (pci_dev_msi_enabled(pdev))
543 pci_disable_msi(pdev);
fce8a7bb 544 }
fce8a7bb
JM
545}
546
e26a5843
AH
547static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
548 size_t count, loff_t *offp)
fce8a7bb 549{
e26a5843
AH
550 struct intel_ntb_dev *ndev;
551 void __iomem *mmio;
552 char *buf;
553 size_t buf_size;
554 ssize_t ret, off;
555 union { u64 v64; u32 v32; u16 v16; } u;
fce8a7bb 556
e26a5843
AH
557 ndev = filp->private_data;
558 mmio = ndev->self_mmio;
fce8a7bb 559
e26a5843 560 buf_size = min(count, 0x800ul);
fce8a7bb 561
e26a5843
AH
562 buf = kmalloc(buf_size, GFP_KERNEL);
563 if (!buf)
564 return -ENOMEM;
fce8a7bb 565
e26a5843 566 off = 0;
fce8a7bb 567
e26a5843
AH
568 off += scnprintf(buf + off, buf_size - off,
569 "NTB Device Information:\n");
fce8a7bb 570
e26a5843
AH
571 off += scnprintf(buf + off, buf_size - off,
572 "Connection Topology -\t%s\n",
573 ntb_topo_string(ndev->ntb.topo));
fce8a7bb 574
e26a5843
AH
575 off += scnprintf(buf + off, buf_size - off,
576 "B2B Offset -\t\t%#lx\n", ndev->b2b_off);
577 off += scnprintf(buf + off, buf_size - off,
578 "B2B MW Idx -\t\t%d\n", ndev->b2b_idx);
579 off += scnprintf(buf + off, buf_size - off,
580 "BAR4 Split -\t\t%s\n",
581 ndev->bar4_split ? "yes" : "no");
113bf1c9 582
e26a5843
AH
583 off += scnprintf(buf + off, buf_size - off,
584 "NTB CTL -\t\t%#06x\n", ndev->ntb_ctl);
585 off += scnprintf(buf + off, buf_size - off,
586 "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
587
588 if (!ndev->reg->link_is_up(ndev)) {
589 off += scnprintf(buf + off, buf_size - off,
590 "Link Status -\t\tDown\n");
591 } else {
592 off += scnprintf(buf + off, buf_size - off,
593 "Link Status -\t\tUp\n");
594 off += scnprintf(buf + off, buf_size - off,
595 "Link Speed -\t\tPCI-E Gen %u\n",
596 NTB_LNK_STA_SPEED(ndev->lnk_sta));
597 off += scnprintf(buf + off, buf_size - off,
598 "Link Width -\t\tx%u\n",
599 NTB_LNK_STA_WIDTH(ndev->lnk_sta));
113bf1c9
JM
600 }
601
e26a5843
AH
602 off += scnprintf(buf + off, buf_size - off,
603 "Memory Window Count -\t%u\n", ndev->mw_count);
604 off += scnprintf(buf + off, buf_size - off,
605 "Scratchpad Count -\t%u\n", ndev->spad_count);
606 off += scnprintf(buf + off, buf_size - off,
607 "Doorbell Count -\t%u\n", ndev->db_count);
608 off += scnprintf(buf + off, buf_size - off,
609 "Doorbell Vector Count -\t%u\n", ndev->db_vec_count);
610 off += scnprintf(buf + off, buf_size - off,
611 "Doorbell Vector Shift -\t%u\n", ndev->db_vec_shift);
612
613 off += scnprintf(buf + off, buf_size - off,
614 "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
615 off += scnprintf(buf + off, buf_size - off,
616 "Doorbell Link Mask -\t%#llx\n", ndev->db_link_mask);
617 off += scnprintf(buf + off, buf_size - off,
618 "Doorbell Mask Cached -\t%#llx\n", ndev->db_mask);
619
620 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_mask);
621 off += scnprintf(buf + off, buf_size - off,
622 "Doorbell Mask -\t\t%#llx\n", u.v64);
623
624 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_bell);
625 off += scnprintf(buf + off, buf_size - off,
626 "Doorbell Bell -\t\t%#llx\n", u.v64);
627
628 off += scnprintf(buf + off, buf_size - off,
629 "\nNTB Incoming XLAT:\n");
630
631 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 2));
632 off += scnprintf(buf + off, buf_size - off,
633 "XLAT23 -\t\t%#018llx\n", u.v64);
634
bf44fe46
DJ
635 if (ndev->bar4_split) {
636 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
637 off += scnprintf(buf + off, buf_size - off,
638 "XLAT4 -\t\t\t%#06x\n", u.v32);
639
640 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 5));
641 off += scnprintf(buf + off, buf_size - off,
642 "XLAT5 -\t\t\t%#06x\n", u.v32);
643 } else {
644 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
645 off += scnprintf(buf + off, buf_size - off,
646 "XLAT45 -\t\t%#018llx\n", u.v64);
647 }
e26a5843
AH
648
649 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 2));
650 off += scnprintf(buf + off, buf_size - off,
651 "LMT23 -\t\t\t%#018llx\n", u.v64);
652
bf44fe46
DJ
653 if (ndev->bar4_split) {
654 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
655 off += scnprintf(buf + off, buf_size - off,
656 "LMT4 -\t\t\t%#06x\n", u.v32);
657 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 5));
658 off += scnprintf(buf + off, buf_size - off,
659 "LMT5 -\t\t\t%#06x\n", u.v32);
660 } else {
661 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
662 off += scnprintf(buf + off, buf_size - off,
663 "LMT45 -\t\t\t%#018llx\n", u.v64);
664 }
e26a5843 665
2f887b9a 666 if (pdev_is_xeon(ndev->ntb.pdev)) {
e26a5843
AH
667 if (ntb_topo_is_b2b(ndev->ntb.topo)) {
668 off += scnprintf(buf + off, buf_size - off,
669 "\nNTB Outgoing B2B XLAT:\n");
670
2f887b9a 671 u.v64 = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
e26a5843
AH
672 off += scnprintf(buf + off, buf_size - off,
673 "B2B XLAT23 -\t\t%#018llx\n", u.v64);
674
bf44fe46
DJ
675 if (ndev->bar4_split) {
676 u.v32 = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
677 off += scnprintf(buf + off, buf_size - off,
678 "B2B XLAT4 -\t\t%#06x\n",
679 u.v32);
680 u.v32 = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
681 off += scnprintf(buf + off, buf_size - off,
682 "B2B XLAT5 -\t\t%#06x\n",
683 u.v32);
684 } else {
685 u.v64 = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
686 off += scnprintf(buf + off, buf_size - off,
687 "B2B XLAT45 -\t\t%#018llx\n",
688 u.v64);
689 }
e26a5843 690
2f887b9a 691 u.v64 = ioread64(mmio + XEON_PBAR23LMT_OFFSET);
e26a5843
AH
692 off += scnprintf(buf + off, buf_size - off,
693 "B2B LMT23 -\t\t%#018llx\n", u.v64);
694
bf44fe46
DJ
695 if (ndev->bar4_split) {
696 u.v32 = ioread32(mmio + XEON_PBAR4LMT_OFFSET);
697 off += scnprintf(buf + off, buf_size - off,
698 "B2B LMT4 -\t\t%#06x\n",
699 u.v32);
700 u.v32 = ioread32(mmio + XEON_PBAR5LMT_OFFSET);
701 off += scnprintf(buf + off, buf_size - off,
702 "B2B LMT5 -\t\t%#06x\n",
703 u.v32);
704 } else {
705 u.v64 = ioread64(mmio + XEON_PBAR45LMT_OFFSET);
706 off += scnprintf(buf + off, buf_size - off,
707 "B2B LMT45 -\t\t%#018llx\n",
708 u.v64);
709 }
e26a5843
AH
710
711 off += scnprintf(buf + off, buf_size - off,
712 "\nNTB Secondary BAR:\n");
713
2f887b9a 714 u.v64 = ioread64(mmio + XEON_SBAR0BASE_OFFSET);
e26a5843
AH
715 off += scnprintf(buf + off, buf_size - off,
716 "SBAR01 -\t\t%#018llx\n", u.v64);
717
2f887b9a 718 u.v64 = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
e26a5843
AH
719 off += scnprintf(buf + off, buf_size - off,
720 "SBAR23 -\t\t%#018llx\n", u.v64);
721
bf44fe46
DJ
722 if (ndev->bar4_split) {
723 u.v32 = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
724 off += scnprintf(buf + off, buf_size - off,
725 "SBAR4 -\t\t\t%#06x\n", u.v32);
726 u.v32 = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
727 off += scnprintf(buf + off, buf_size - off,
728 "SBAR5 -\t\t\t%#06x\n", u.v32);
729 } else {
730 u.v64 = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
731 off += scnprintf(buf + off, buf_size - off,
732 "SBAR45 -\t\t%#018llx\n",
733 u.v64);
734 }
e26a5843
AH
735 }
736
737 off += scnprintf(buf + off, buf_size - off,
2f887b9a 738 "\nXEON NTB Statistics:\n");
e26a5843 739
2f887b9a 740 u.v16 = ioread16(mmio + XEON_USMEMMISS_OFFSET);
e26a5843
AH
741 off += scnprintf(buf + off, buf_size - off,
742 "Upstream Memory Miss -\t%u\n", u.v16);
743
744 off += scnprintf(buf + off, buf_size - off,
2f887b9a 745 "\nXEON NTB Hardware Errors:\n");
e26a5843
AH
746
747 if (!pci_read_config_word(ndev->ntb.pdev,
2f887b9a 748 XEON_DEVSTS_OFFSET, &u.v16))
e26a5843
AH
749 off += scnprintf(buf + off, buf_size - off,
750 "DEVSTS -\t\t%#06x\n", u.v16);
751
752 if (!pci_read_config_word(ndev->ntb.pdev,
2f887b9a 753 XEON_LINK_STATUS_OFFSET, &u.v16))
e26a5843
AH
754 off += scnprintf(buf + off, buf_size - off,
755 "LNKSTS -\t\t%#06x\n", u.v16);
113bf1c9 756
e26a5843 757 if (!pci_read_config_dword(ndev->ntb.pdev,
2f887b9a 758 XEON_UNCERRSTS_OFFSET, &u.v32))
e26a5843
AH
759 off += scnprintf(buf + off, buf_size - off,
760 "UNCERRSTS -\t\t%#06x\n", u.v32);
761
762 if (!pci_read_config_dword(ndev->ntb.pdev,
2f887b9a 763 XEON_CORERRSTS_OFFSET, &u.v32))
e26a5843
AH
764 off += scnprintf(buf + off, buf_size - off,
765 "CORERRSTS -\t\t%#06x\n", u.v32);
766 }
767
768 ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
769 kfree(buf);
770 return ret;
113bf1c9
JM
771}
772
e26a5843 773static void ndev_init_debugfs(struct intel_ntb_dev *ndev)
fce8a7bb 774{
e26a5843
AH
775 if (!debugfs_dir) {
776 ndev->debugfs_dir = NULL;
777 ndev->debugfs_info = NULL;
778 } else {
779 ndev->debugfs_dir =
780 debugfs_create_dir(ndev_name(ndev), debugfs_dir);
781 if (!ndev->debugfs_dir)
782 ndev->debugfs_info = NULL;
783 else
784 ndev->debugfs_info =
785 debugfs_create_file("info", S_IRUSR,
786 ndev->debugfs_dir, ndev,
787 &intel_ntb_debugfs_info);
fce8a7bb 788 }
e26a5843 789}
fce8a7bb 790
e26a5843
AH
791static void ndev_deinit_debugfs(struct intel_ntb_dev *ndev)
792{
793 debugfs_remove_recursive(ndev->debugfs_dir);
fce8a7bb
JM
794}
795
e26a5843 796static int intel_ntb_mw_count(struct ntb_dev *ntb)
fce8a7bb 797{
e26a5843
AH
798 return ntb_ndev(ntb)->mw_count;
799}
ed6c24ed 800
e26a5843
AH
801static int intel_ntb_mw_get_range(struct ntb_dev *ntb, int idx,
802 phys_addr_t *base,
803 resource_size_t *size,
804 resource_size_t *align,
805 resource_size_t *align_size)
806{
807 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
808 int bar;
948d3a65 809
e26a5843
AH
810 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
811 idx += 1;
ab760a0c 812
e26a5843
AH
813 bar = ndev_mw_to_bar(ndev, idx);
814 if (bar < 0)
815 return bar;
ed6c24ed 816
e26a5843
AH
817 if (base)
818 *base = pci_resource_start(ndev->ntb.pdev, bar) +
819 (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
069684e8 820
e26a5843
AH
821 if (size)
822 *size = pci_resource_len(ndev->ntb.pdev, bar) -
823 (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
fce8a7bb 824
e26a5843
AH
825 if (align)
826 *align = pci_resource_len(ndev->ntb.pdev, bar);
ed6c24ed 827
e26a5843
AH
828 if (align_size)
829 *align_size = 1;
fce8a7bb
JM
830
831 return 0;
832}
833
e26a5843
AH
834static int intel_ntb_mw_set_trans(struct ntb_dev *ntb, int idx,
835 dma_addr_t addr, resource_size_t size)
fce8a7bb 836{
e26a5843
AH
837 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
838 unsigned long base_reg, xlat_reg, limit_reg;
839 resource_size_t bar_size, mw_size;
840 void __iomem *mmio;
841 u64 base, limit, reg_val;
842 int bar;
fce8a7bb 843
e26a5843
AH
844 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
845 idx += 1;
fce8a7bb 846
e26a5843
AH
847 bar = ndev_mw_to_bar(ndev, idx);
848 if (bar < 0)
849 return bar;
fce8a7bb 850
e26a5843
AH
851 bar_size = pci_resource_len(ndev->ntb.pdev, bar);
852
853 if (idx == ndev->b2b_idx)
854 mw_size = bar_size - ndev->b2b_off;
855 else
856 mw_size = bar_size;
857
858 /* hardware requires that addr is aligned to bar size */
859 if (addr & (bar_size - 1))
fce8a7bb 860 return -EINVAL;
e26a5843
AH
861
862 /* make sure the range fits in the usable mw size */
863 if (size > mw_size)
864 return -EINVAL;
865
866 mmio = ndev->self_mmio;
867 base_reg = bar0_off(ndev->xlat_reg->bar0_base, bar);
868 xlat_reg = bar2_off(ndev->xlat_reg->bar2_xlat, bar);
869 limit_reg = bar2_off(ndev->xlat_reg->bar2_limit, bar);
870
871 if (bar < 4 || !ndev->bar4_split) {
872 base = ioread64(mmio + base_reg);
873
874 /* Set the limit if supported, if size is not mw_size */
875 if (limit_reg && size != mw_size)
876 limit = base + size;
877 else
878 limit = 0;
879
880 /* set and verify setting the translation address */
881 iowrite64(addr, mmio + xlat_reg);
882 reg_val = ioread64(mmio + xlat_reg);
883 if (reg_val != addr) {
884 iowrite64(0, mmio + xlat_reg);
885 return -EIO;
886 }
887
888 /* set and verify setting the limit */
889 iowrite64(limit, mmio + limit_reg);
890 reg_val = ioread64(mmio + limit_reg);
891 if (reg_val != limit) {
892 iowrite64(base, mmio + limit_reg);
893 iowrite64(0, mmio + xlat_reg);
894 return -EIO;
895 }
896 } else {
897 /* split bar addr range must all be 32 bit */
898 if (addr & (~0ull << 32))
899 return -EINVAL;
900 if ((addr + size) & (~0ull << 32))
901 return -EINVAL;
902
903 base = ioread32(mmio + base_reg);
904
905 /* Set the limit if supported, if size is not mw_size */
906 if (limit_reg && size != mw_size)
907 limit = base + size;
908 else
909 limit = 0;
910
911 /* set and verify setting the translation address */
912 iowrite32(addr, mmio + xlat_reg);
913 reg_val = ioread32(mmio + xlat_reg);
914 if (reg_val != addr) {
915 iowrite32(0, mmio + xlat_reg);
916 return -EIO;
917 }
918
919 /* set and verify setting the limit */
920 iowrite32(limit, mmio + limit_reg);
921 reg_val = ioread32(mmio + limit_reg);
922 if (reg_val != limit) {
923 iowrite32(base, mmio + limit_reg);
924 iowrite32(0, mmio + xlat_reg);
925 return -EIO;
926 }
fce8a7bb
JM
927 }
928
e26a5843
AH
929 return 0;
930}
fce8a7bb 931
e26a5843
AH
932static int intel_ntb_link_is_up(struct ntb_dev *ntb,
933 enum ntb_speed *speed,
934 enum ntb_width *width)
935{
936 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
fce8a7bb 937
e26a5843
AH
938 if (ndev->reg->link_is_up(ndev)) {
939 if (speed)
940 *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
941 if (width)
942 *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
943 return 1;
944 } else {
945 /* TODO MAYBE: is it possible to observe the link speed and
946 * width while link is training? */
947 if (speed)
948 *speed = NTB_SPEED_NONE;
949 if (width)
950 *width = NTB_WIDTH_NONE;
951 return 0;
952 }
953}
954
955static int intel_ntb_link_enable(struct ntb_dev *ntb,
956 enum ntb_speed max_speed,
957 enum ntb_width max_width)
958{
959 struct intel_ntb_dev *ndev;
960 u32 ntb_ctl;
961
962 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
963
964 if (ndev->ntb.topo == NTB_TOPO_SEC)
965 return -EINVAL;
966
967 dev_dbg(ndev_dev(ndev),
968 "Enabling link with max_speed %d max_width %d\n",
969 max_speed, max_width);
970 if (max_speed != NTB_SPEED_AUTO)
971 dev_dbg(ndev_dev(ndev), "ignoring max_speed %d\n", max_speed);
972 if (max_width != NTB_WIDTH_AUTO)
973 dev_dbg(ndev_dev(ndev), "ignoring max_width %d\n", max_width);
974
975 ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
976 ntb_ctl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
977 ntb_ctl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
978 ntb_ctl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
979 if (ndev->bar4_split)
980 ntb_ctl |= NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP;
981 iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
fce8a7bb
JM
982
983 return 0;
984}
985
e26a5843 986static int intel_ntb_link_disable(struct ntb_dev *ntb)
fce8a7bb 987{
e26a5843
AH
988 struct intel_ntb_dev *ndev;
989 u32 ntb_cntl;
fce8a7bb 990
e26a5843 991 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
fce8a7bb 992
e26a5843
AH
993 if (ndev->ntb.topo == NTB_TOPO_SEC)
994 return -EINVAL;
3b12a0d1 995
e26a5843
AH
996 dev_dbg(ndev_dev(ndev), "Disabling link\n");
997
998 /* Bring NTB link down */
999 ntb_cntl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
1000 ntb_cntl &= ~(NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP);
1001 ntb_cntl &= ~(NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP);
1002 if (ndev->bar4_split)
1003 ntb_cntl &= ~(NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP);
1004 ntb_cntl |= NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK;
1005 iowrite32(ntb_cntl, ndev->self_mmio + ndev->reg->ntb_ctl);
fce8a7bb 1006
3b12a0d1 1007 return 0;
fce8a7bb
JM
1008}
1009
e26a5843 1010static int intel_ntb_db_is_unsafe(struct ntb_dev *ntb)
fce8a7bb 1011{
e26a5843 1012 return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_DB);
fce8a7bb
JM
1013}
1014
e26a5843 1015static u64 intel_ntb_db_valid_mask(struct ntb_dev *ntb)
fce8a7bb 1016{
e26a5843
AH
1017 return ntb_ndev(ntb)->db_valid_mask;
1018}
fce8a7bb 1019
e26a5843
AH
1020static int intel_ntb_db_vector_count(struct ntb_dev *ntb)
1021{
1022 struct intel_ntb_dev *ndev;
fce8a7bb 1023
e26a5843 1024 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
e8aeb60c 1025
e26a5843
AH
1026 return ndev->db_vec_count;
1027}
fce8a7bb 1028
e26a5843
AH
1029static u64 intel_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
1030{
1031 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
fce8a7bb 1032
e26a5843
AH
1033 if (db_vector < 0 || db_vector > ndev->db_vec_count)
1034 return 0;
fce8a7bb 1035
e26a5843 1036 return ndev->db_valid_mask & ndev_vec_mask(ndev, db_vector);
fce8a7bb
JM
1037}
1038
e26a5843 1039static u64 intel_ntb_db_read(struct ntb_dev *ntb)
fce8a7bb 1040{
e26a5843 1041 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
fce8a7bb 1042
e26a5843
AH
1043 return ndev_db_read(ndev,
1044 ndev->self_mmio +
1045 ndev->self_reg->db_bell);
1046}
fce8a7bb 1047
e26a5843
AH
1048static int intel_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
1049{
1050 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
e8aeb60c 1051
e26a5843
AH
1052 return ndev_db_write(ndev, db_bits,
1053 ndev->self_mmio +
1054 ndev->self_reg->db_bell);
1055}
fce8a7bb 1056
e26a5843
AH
1057static int intel_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1058{
1059 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
fce8a7bb 1060
e26a5843
AH
1061 return ndev_db_set_mask(ndev, db_bits,
1062 ndev->self_mmio +
1063 ndev->self_reg->db_mask);
fce8a7bb
JM
1064}
1065
e26a5843 1066static int intel_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
fce8a7bb 1067{
e26a5843 1068 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
fce8a7bb 1069
e26a5843
AH
1070 return ndev_db_clear_mask(ndev, db_bits,
1071 ndev->self_mmio +
1072 ndev->self_reg->db_mask);
1073}
fce8a7bb 1074
e26a5843
AH
1075static int intel_ntb_peer_db_addr(struct ntb_dev *ntb,
1076 phys_addr_t *db_addr,
1077 resource_size_t *db_size)
1078{
1079 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
fce8a7bb 1080
e26a5843
AH
1081 return ndev_db_addr(ndev, db_addr, db_size, ndev->peer_addr,
1082 ndev->peer_reg->db_bell);
fce8a7bb
JM
1083}
1084
e26a5843 1085static int intel_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
fce8a7bb 1086{
e26a5843 1087 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
fce8a7bb 1088
e26a5843
AH
1089 return ndev_db_write(ndev, db_bits,
1090 ndev->peer_mmio +
1091 ndev->peer_reg->db_bell);
1092}
fce8a7bb 1093
e26a5843
AH
1094static int intel_ntb_spad_is_unsafe(struct ntb_dev *ntb)
1095{
1096 return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_SPAD);
1097}
fce8a7bb 1098
e26a5843
AH
1099static int intel_ntb_spad_count(struct ntb_dev *ntb)
1100{
1101 struct intel_ntb_dev *ndev;
fce8a7bb 1102
e26a5843 1103 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
fce8a7bb 1104
e26a5843
AH
1105 return ndev->spad_count;
1106}
fce8a7bb 1107
e26a5843
AH
1108static u32 intel_ntb_spad_read(struct ntb_dev *ntb, int idx)
1109{
1110 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
fce8a7bb 1111
e26a5843
AH
1112 return ndev_spad_read(ndev, idx,
1113 ndev->self_mmio +
1114 ndev->self_reg->spad);
fce8a7bb
JM
1115}
1116
e26a5843
AH
1117static int intel_ntb_spad_write(struct ntb_dev *ntb,
1118 int idx, u32 val)
53a788a7 1119{
e26a5843 1120 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
53a788a7 1121
e26a5843
AH
1122 return ndev_spad_write(ndev, idx, val,
1123 ndev->self_mmio +
1124 ndev->self_reg->spad);
1125}
53a788a7 1126
e26a5843
AH
1127static int intel_ntb_peer_spad_addr(struct ntb_dev *ntb, int idx,
1128 phys_addr_t *spad_addr)
1129{
1130 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
53a788a7 1131
e26a5843
AH
1132 return ndev_spad_addr(ndev, idx, spad_addr, ndev->peer_addr,
1133 ndev->peer_reg->spad);
1134}
53a788a7 1135
e26a5843
AH
1136static u32 intel_ntb_peer_spad_read(struct ntb_dev *ntb, int idx)
1137{
1138 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
53a788a7 1139
e26a5843
AH
1140 return ndev_spad_read(ndev, idx,
1141 ndev->peer_mmio +
1142 ndev->peer_reg->spad);
1143}
53a788a7 1144
e26a5843
AH
1145static int intel_ntb_peer_spad_write(struct ntb_dev *ntb,
1146 int idx, u32 val)
1147{
1148 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
53a788a7 1149
e26a5843
AH
1150 return ndev_spad_write(ndev, idx, val,
1151 ndev->peer_mmio +
1152 ndev->peer_reg->spad);
1153}
53a788a7 1154
2f887b9a 1155/* ATOM */
53a788a7 1156
2f887b9a 1157static u64 atom_db_ioread(void __iomem *mmio)
e26a5843
AH
1158{
1159 return ioread64(mmio);
1160}
1161
2f887b9a 1162static void atom_db_iowrite(u64 bits, void __iomem *mmio)
e26a5843
AH
1163{
1164 iowrite64(bits, mmio);
53a788a7
AG
1165}
1166
2f887b9a 1167static int atom_poll_link(struct intel_ntb_dev *ndev)
fce8a7bb 1168{
e26a5843 1169 u32 ntb_ctl;
53a788a7 1170
2f887b9a 1171 ntb_ctl = ioread32(ndev->self_mmio + ATOM_NTBCNTL_OFFSET);
53a788a7 1172
e26a5843
AH
1173 if (ntb_ctl == ndev->ntb_ctl)
1174 return 0;
53a788a7 1175
e26a5843 1176 ndev->ntb_ctl = ntb_ctl;
53a788a7 1177
2f887b9a 1178 ndev->lnk_sta = ioread32(ndev->self_mmio + ATOM_LINK_STATUS_OFFSET);
53a788a7 1179
e26a5843
AH
1180 return 1;
1181}
53a788a7 1182
2f887b9a 1183static int atom_link_is_up(struct intel_ntb_dev *ndev)
e26a5843 1184{
2f887b9a 1185 return ATOM_NTB_CTL_ACTIVE(ndev->ntb_ctl);
e26a5843 1186}
53a788a7 1187
2f887b9a 1188static int atom_link_is_err(struct intel_ntb_dev *ndev)
e26a5843 1189{
2f887b9a
DJ
1190 if (ioread32(ndev->self_mmio + ATOM_LTSSMSTATEJMP_OFFSET)
1191 & ATOM_LTSSMSTATEJMP_FORCEDETECT)
e26a5843 1192 return 1;
53a788a7 1193
2f887b9a
DJ
1194 if (ioread32(ndev->self_mmio + ATOM_IBSTERRRCRVSTS0_OFFSET)
1195 & ATOM_IBIST_ERR_OFLOW)
e26a5843
AH
1196 return 1;
1197
1198 return 0;
53a788a7
AG
1199}
1200
2f887b9a 1201static inline enum ntb_topo atom_ppd_topo(struct intel_ntb_dev *ndev, u32 ppd)
53a788a7 1202{
2f887b9a
DJ
1203 switch (ppd & ATOM_PPD_TOPO_MASK) {
1204 case ATOM_PPD_TOPO_B2B_USD:
e26a5843
AH
1205 dev_dbg(ndev_dev(ndev), "PPD %d B2B USD\n", ppd);
1206 return NTB_TOPO_B2B_USD;
1207
2f887b9a 1208 case ATOM_PPD_TOPO_B2B_DSD:
e26a5843
AH
1209 dev_dbg(ndev_dev(ndev), "PPD %d B2B DSD\n", ppd);
1210 return NTB_TOPO_B2B_DSD;
1211
2f887b9a
DJ
1212 case ATOM_PPD_TOPO_PRI_USD:
1213 case ATOM_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
1214 case ATOM_PPD_TOPO_SEC_USD:
1215 case ATOM_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
e26a5843
AH
1216 dev_dbg(ndev_dev(ndev), "PPD %d non B2B disabled\n", ppd);
1217 return NTB_TOPO_NONE;
1218 }
fce8a7bb 1219
e26a5843
AH
1220 dev_dbg(ndev_dev(ndev), "PPD %d invalid\n", ppd);
1221 return NTB_TOPO_NONE;
1222}
1223
2f887b9a 1224static void atom_link_hb(struct work_struct *work)
e26a5843
AH
1225{
1226 struct intel_ntb_dev *ndev = hb_ndev(work);
1227 unsigned long poll_ts;
1228 void __iomem *mmio;
1229 u32 status32;
1230
2f887b9a 1231 poll_ts = ndev->last_ts + ATOM_LINK_HB_TIMEOUT;
e26a5843
AH
1232
1233 /* Delay polling the link status if an interrupt was received,
1234 * unless the cached link status says the link is down.
1235 */
2f887b9a 1236 if (time_after(poll_ts, jiffies) && atom_link_is_up(ndev)) {
e26a5843
AH
1237 schedule_delayed_work(&ndev->hb_timer, poll_ts - jiffies);
1238 return;
fce8a7bb
JM
1239 }
1240
2f887b9a 1241 if (atom_poll_link(ndev))
e26a5843
AH
1242 ntb_link_event(&ndev->ntb);
1243
2f887b9a
DJ
1244 if (atom_link_is_up(ndev) || !atom_link_is_err(ndev)) {
1245 schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
e26a5843 1246 return;
fce8a7bb
JM
1247 }
1248
e26a5843 1249 /* Link is down with error: recover the link! */
fce8a7bb 1250
e26a5843 1251 mmio = ndev->self_mmio;
fce8a7bb 1252
e26a5843 1253 /* Driver resets the NTB ModPhy lanes - magic! */
2f887b9a
DJ
1254 iowrite8(0xe0, mmio + ATOM_MODPHY_PCSREG6);
1255 iowrite8(0x40, mmio + ATOM_MODPHY_PCSREG4);
1256 iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG4);
1257 iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG6);
fce8a7bb 1258
e26a5843
AH
1259 /* Driver waits 100ms to allow the NTB ModPhy to settle */
1260 msleep(100);
1261
1262 /* Clear AER Errors, write to clear */
2f887b9a 1263 status32 = ioread32(mmio + ATOM_ERRCORSTS_OFFSET);
e26a5843
AH
1264 dev_dbg(ndev_dev(ndev), "ERRCORSTS = %x\n", status32);
1265 status32 &= PCI_ERR_COR_REP_ROLL;
2f887b9a 1266 iowrite32(status32, mmio + ATOM_ERRCORSTS_OFFSET);
e26a5843
AH
1267
1268 /* Clear unexpected electrical idle event in LTSSM, write to clear */
2f887b9a 1269 status32 = ioread32(mmio + ATOM_LTSSMERRSTS0_OFFSET);
e26a5843 1270 dev_dbg(ndev_dev(ndev), "LTSSMERRSTS0 = %x\n", status32);
2f887b9a
DJ
1271 status32 |= ATOM_LTSSMERRSTS0_UNEXPECTEDEI;
1272 iowrite32(status32, mmio + ATOM_LTSSMERRSTS0_OFFSET);
e26a5843
AH
1273
1274 /* Clear DeSkew Buffer error, write to clear */
2f887b9a 1275 status32 = ioread32(mmio + ATOM_DESKEWSTS_OFFSET);
e26a5843 1276 dev_dbg(ndev_dev(ndev), "DESKEWSTS = %x\n", status32);
2f887b9a
DJ
1277 status32 |= ATOM_DESKEWSTS_DBERR;
1278 iowrite32(status32, mmio + ATOM_DESKEWSTS_OFFSET);
e26a5843 1279
2f887b9a 1280 status32 = ioread32(mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
e26a5843 1281 dev_dbg(ndev_dev(ndev), "IBSTERRRCRVSTS0 = %x\n", status32);
2f887b9a
DJ
1282 status32 &= ATOM_IBIST_ERR_OFLOW;
1283 iowrite32(status32, mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
e26a5843
AH
1284
1285 /* Releases the NTB state machine to allow the link to retrain */
2f887b9a 1286 status32 = ioread32(mmio + ATOM_LTSSMSTATEJMP_OFFSET);
e26a5843 1287 dev_dbg(ndev_dev(ndev), "LTSSMSTATEJMP = %x\n", status32);
2f887b9a
DJ
1288 status32 &= ~ATOM_LTSSMSTATEJMP_FORCEDETECT;
1289 iowrite32(status32, mmio + ATOM_LTSSMSTATEJMP_OFFSET);
e26a5843
AH
1290
1291 /* There is a potential race between the 2 NTB devices recovering at the
1292 * same time. If the times are the same, the link will not recover and
1293 * the driver will be stuck in this loop forever. Add a random interval
1294 * to the recovery time to prevent this race.
1295 */
2f887b9a
DJ
1296 schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_RECOVERY_TIME
1297 + prandom_u32() % ATOM_LINK_RECOVERY_TIME);
fce8a7bb
JM
1298}
1299
2f887b9a 1300static int atom_init_isr(struct intel_ntb_dev *ndev)
fce8a7bb 1301{
fce8a7bb
JM
1302 int rc;
1303
2f887b9a
DJ
1304 rc = ndev_init_isr(ndev, 1, ATOM_DB_MSIX_VECTOR_COUNT,
1305 ATOM_DB_MSIX_VECTOR_SHIFT, ATOM_DB_TOTAL_SHIFT);
fce8a7bb
JM
1306 if (rc)
1307 return rc;
1308
2f887b9a 1309 /* ATOM doesn't have link status interrupt, poll on that platform */
e26a5843 1310 ndev->last_ts = jiffies;
2f887b9a
DJ
1311 INIT_DELAYED_WORK(&ndev->hb_timer, atom_link_hb);
1312 schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
fce8a7bb
JM
1313
1314 return 0;
1315}
1316
2f887b9a 1317static void atom_deinit_isr(struct intel_ntb_dev *ndev)
fce8a7bb 1318{
e26a5843
AH
1319 cancel_delayed_work_sync(&ndev->hb_timer);
1320 ndev_deinit_isr(ndev);
1321}
fce8a7bb 1322
2f887b9a 1323static int atom_init_ntb(struct intel_ntb_dev *ndev)
e26a5843 1324{
2f887b9a
DJ
1325 ndev->mw_count = ATOM_MW_COUNT;
1326 ndev->spad_count = ATOM_SPAD_COUNT;
1327 ndev->db_count = ATOM_DB_COUNT;
fce8a7bb 1328
e26a5843
AH
1329 switch (ndev->ntb.topo) {
1330 case NTB_TOPO_B2B_USD:
1331 case NTB_TOPO_B2B_DSD:
2f887b9a
DJ
1332 ndev->self_reg = &atom_pri_reg;
1333 ndev->peer_reg = &atom_b2b_reg;
1334 ndev->xlat_reg = &atom_sec_xlat;
e26a5843
AH
1335
1336 /* Enable Bus Master and Memory Space on the secondary side */
1337 iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
2f887b9a 1338 ndev->self_mmio + ATOM_SPCICMD_OFFSET);
e26a5843
AH
1339
1340 break;
1341
1342 default:
1343 return -EINVAL;
1344 }
1345
1346 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
fce8a7bb
JM
1347
1348 return 0;
1349}
1350
2f887b9a 1351static int atom_init_dev(struct intel_ntb_dev *ndev)
fce8a7bb 1352{
e26a5843 1353 u32 ppd;
fce8a7bb
JM
1354 int rc;
1355
2f887b9a 1356 rc = pci_read_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET, &ppd);
e26a5843
AH
1357 if (rc)
1358 return -EIO;
fce8a7bb 1359
2f887b9a 1360 ndev->ntb.topo = atom_ppd_topo(ndev, ppd);
e26a5843
AH
1361 if (ndev->ntb.topo == NTB_TOPO_NONE)
1362 return -EINVAL;
fce8a7bb 1363
2f887b9a 1364 rc = atom_init_ntb(ndev);
e26a5843
AH
1365 if (rc)
1366 return rc;
fce8a7bb 1367
2f887b9a 1368 rc = atom_init_isr(ndev);
e26a5843 1369 if (rc)
fce8a7bb 1370 return rc;
e26a5843
AH
1371
1372 if (ndev->ntb.topo != NTB_TOPO_SEC) {
1373 /* Initiate PCI-E link training */
2f887b9a
DJ
1374 rc = pci_write_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET,
1375 ppd | ATOM_PPD_INIT_LINK);
e26a5843
AH
1376 if (rc)
1377 return rc;
fce8a7bb
JM
1378 }
1379
fce8a7bb
JM
1380 return 0;
1381}
1382
2f887b9a 1383static void atom_deinit_dev(struct intel_ntb_dev *ndev)
fce8a7bb 1384{
2f887b9a 1385 atom_deinit_isr(ndev);
e26a5843 1386}
fce8a7bb 1387
2f887b9a 1388/* XEON */
fce8a7bb 1389
2f887b9a 1390static u64 xeon_db_ioread(void __iomem *mmio)
e26a5843
AH
1391{
1392 return (u64)ioread16(mmio);
1393}
fce8a7bb 1394
2f887b9a 1395static void xeon_db_iowrite(u64 bits, void __iomem *mmio)
e26a5843
AH
1396{
1397 iowrite16((u16)bits, mmio);
1398}
fce8a7bb 1399
2f887b9a 1400static int xeon_poll_link(struct intel_ntb_dev *ndev)
e26a5843
AH
1401{
1402 u16 reg_val;
1403 int rc;
1404
1405 ndev->reg->db_iowrite(ndev->db_link_mask,
1406 ndev->self_mmio +
1407 ndev->self_reg->db_bell);
1408
1409 rc = pci_read_config_word(ndev->ntb.pdev,
2f887b9a 1410 XEON_LINK_STATUS_OFFSET, &reg_val);
e26a5843
AH
1411 if (rc)
1412 return 0;
1413
1414 if (reg_val == ndev->lnk_sta)
1415 return 0;
1416
1417 ndev->lnk_sta = reg_val;
1418
1419 return 1;
fce8a7bb
JM
1420}
1421
2f887b9a 1422static int xeon_link_is_up(struct intel_ntb_dev *ndev)
fce8a7bb 1423{
5ae0beb6
DJ
1424 if (ndev->ntb.topo == NTB_TOPO_SEC)
1425 return 1;
1426
e26a5843
AH
1427 return NTB_LNK_STA_ACTIVE(ndev->lnk_sta);
1428}
fce8a7bb 1429
2f887b9a 1430static inline enum ntb_topo xeon_ppd_topo(struct intel_ntb_dev *ndev, u8 ppd)
e26a5843 1431{
2f887b9a
DJ
1432 switch (ppd & XEON_PPD_TOPO_MASK) {
1433 case XEON_PPD_TOPO_B2B_USD:
e26a5843
AH
1434 return NTB_TOPO_B2B_USD;
1435
2f887b9a 1436 case XEON_PPD_TOPO_B2B_DSD:
e26a5843
AH
1437 return NTB_TOPO_B2B_DSD;
1438
2f887b9a
DJ
1439 case XEON_PPD_TOPO_PRI_USD:
1440 case XEON_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
e26a5843 1441 return NTB_TOPO_PRI;
fce8a7bb 1442
2f887b9a
DJ
1443 case XEON_PPD_TOPO_SEC_USD:
1444 case XEON_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
e26a5843 1445 return NTB_TOPO_SEC;
fce8a7bb
JM
1446 }
1447
e26a5843 1448 return NTB_TOPO_NONE;
fce8a7bb
JM
1449}
1450
2f887b9a 1451static inline int xeon_ppd_bar4_split(struct intel_ntb_dev *ndev, u8 ppd)
fce8a7bb 1452{
2f887b9a 1453 if (ppd & XEON_PPD_SPLIT_BAR_MASK) {
e26a5843
AH
1454 dev_dbg(ndev_dev(ndev), "PPD %d split bar\n", ppd);
1455 return 1;
1456 }
1457 return 0;
1458}
fce8a7bb 1459
2f887b9a 1460static int xeon_init_isr(struct intel_ntb_dev *ndev)
e26a5843 1461{
2f887b9a
DJ
1462 return ndev_init_isr(ndev, XEON_DB_MSIX_VECTOR_COUNT,
1463 XEON_DB_MSIX_VECTOR_COUNT,
1464 XEON_DB_MSIX_VECTOR_SHIFT,
1465 XEON_DB_TOTAL_SHIFT);
e26a5843 1466}
fce8a7bb 1467
2f887b9a 1468static void xeon_deinit_isr(struct intel_ntb_dev *ndev)
e26a5843
AH
1469{
1470 ndev_deinit_isr(ndev);
fce8a7bb
JM
1471}
1472
2f887b9a
DJ
1473static int xeon_setup_b2b_mw(struct intel_ntb_dev *ndev,
1474 const struct intel_b2b_addr *addr,
1475 const struct intel_b2b_addr *peer_addr)
6465d02e 1476{
e26a5843
AH
1477 struct pci_dev *pdev;
1478 void __iomem *mmio;
1479 resource_size_t bar_size;
1480 phys_addr_t bar_addr;
1481 int b2b_bar;
1482 u8 bar_sz;
1483
1484 pdev = ndev_pdev(ndev);
1485 mmio = ndev->self_mmio;
1486
1487 if (ndev->b2b_idx >= ndev->mw_count) {
1488 dev_dbg(ndev_dev(ndev), "not using b2b mw\n");
1489 b2b_bar = 0;
1490 ndev->b2b_off = 0;
1491 } else {
1492 b2b_bar = ndev_mw_to_bar(ndev, ndev->b2b_idx);
1493 if (b2b_bar < 0)
1494 return -EIO;
6465d02e 1495
e26a5843 1496 dev_dbg(ndev_dev(ndev), "using b2b mw bar %d\n", b2b_bar);
6465d02e 1497
e26a5843 1498 bar_size = pci_resource_len(ndev->ntb.pdev, b2b_bar);
6465d02e 1499
e26a5843 1500 dev_dbg(ndev_dev(ndev), "b2b bar size %#llx\n", bar_size);
6465d02e 1501
2f887b9a 1502 if (b2b_mw_share && XEON_B2B_MIN_SIZE <= bar_size >> 1) {
e26a5843
AH
1503 dev_dbg(ndev_dev(ndev),
1504 "b2b using first half of bar\n");
1505 ndev->b2b_off = bar_size >> 1;
2f887b9a 1506 } else if (XEON_B2B_MIN_SIZE <= bar_size) {
e26a5843
AH
1507 dev_dbg(ndev_dev(ndev),
1508 "b2b using whole bar\n");
1509 ndev->b2b_off = 0;
1510 --ndev->mw_count;
1511 } else {
1512 dev_dbg(ndev_dev(ndev),
1513 "b2b bar size is too small\n");
1514 return -EIO;
1515 }
6465d02e
JM
1516 }
1517
e26a5843
AH
1518 /* Reset the secondary bar sizes to match the primary bar sizes,
1519 * except disable or halve the size of the b2b secondary bar.
1520 *
1521 * Note: code for each specific bar size register, because the register
1522 * offsets are not in a consistent order (bar5sz comes after ppd, odd).
1523 */
2f887b9a 1524 pci_read_config_byte(pdev, XEON_PBAR23SZ_OFFSET, &bar_sz);
e26a5843
AH
1525 dev_dbg(ndev_dev(ndev), "PBAR23SZ %#x\n", bar_sz);
1526 if (b2b_bar == 2) {
1527 if (ndev->b2b_off)
1528 bar_sz -= 1;
1529 else
1530 bar_sz = 0;
1531 }
2f887b9a
DJ
1532 pci_write_config_byte(pdev, XEON_SBAR23SZ_OFFSET, bar_sz);
1533 pci_read_config_byte(pdev, XEON_SBAR23SZ_OFFSET, &bar_sz);
e26a5843
AH
1534 dev_dbg(ndev_dev(ndev), "SBAR23SZ %#x\n", bar_sz);
1535
1536 if (!ndev->bar4_split) {
2f887b9a 1537 pci_read_config_byte(pdev, XEON_PBAR45SZ_OFFSET, &bar_sz);
e26a5843
AH
1538 dev_dbg(ndev_dev(ndev), "PBAR45SZ %#x\n", bar_sz);
1539 if (b2b_bar == 4) {
1540 if (ndev->b2b_off)
1541 bar_sz -= 1;
1542 else
1543 bar_sz = 0;
1544 }
2f887b9a
DJ
1545 pci_write_config_byte(pdev, XEON_SBAR45SZ_OFFSET, bar_sz);
1546 pci_read_config_byte(pdev, XEON_SBAR45SZ_OFFSET, &bar_sz);
e26a5843
AH
1547 dev_dbg(ndev_dev(ndev), "SBAR45SZ %#x\n", bar_sz);
1548 } else {
2f887b9a 1549 pci_read_config_byte(pdev, XEON_PBAR4SZ_OFFSET, &bar_sz);
e26a5843
AH
1550 dev_dbg(ndev_dev(ndev), "PBAR4SZ %#x\n", bar_sz);
1551 if (b2b_bar == 4) {
1552 if (ndev->b2b_off)
1553 bar_sz -= 1;
1554 else
1555 bar_sz = 0;
1556 }
2f887b9a
DJ
1557 pci_write_config_byte(pdev, XEON_SBAR4SZ_OFFSET, bar_sz);
1558 pci_read_config_byte(pdev, XEON_SBAR4SZ_OFFSET, &bar_sz);
e26a5843
AH
1559 dev_dbg(ndev_dev(ndev), "SBAR4SZ %#x\n", bar_sz);
1560
2f887b9a 1561 pci_read_config_byte(pdev, XEON_PBAR5SZ_OFFSET, &bar_sz);
e26a5843
AH
1562 dev_dbg(ndev_dev(ndev), "PBAR5SZ %#x\n", bar_sz);
1563 if (b2b_bar == 5) {
1564 if (ndev->b2b_off)
1565 bar_sz -= 1;
1566 else
1567 bar_sz = 0;
1568 }
2f887b9a
DJ
1569 pci_write_config_byte(pdev, XEON_SBAR5SZ_OFFSET, bar_sz);
1570 pci_read_config_byte(pdev, XEON_SBAR5SZ_OFFSET, &bar_sz);
e26a5843
AH
1571 dev_dbg(ndev_dev(ndev), "SBAR5SZ %#x\n", bar_sz);
1572 }
6465d02e 1573
e26a5843
AH
1574 /* SBAR01 hit by first part of the b2b bar */
1575 if (b2b_bar == 0)
1576 bar_addr = addr->bar0_addr;
1577 else if (b2b_bar == 2)
1578 bar_addr = addr->bar2_addr64;
1579 else if (b2b_bar == 4 && !ndev->bar4_split)
1580 bar_addr = addr->bar4_addr64;
1581 else if (b2b_bar == 4)
1582 bar_addr = addr->bar4_addr32;
1583 else if (b2b_bar == 5)
1584 bar_addr = addr->bar5_addr32;
1585 else
1586 return -EIO;
6465d02e 1587
e26a5843 1588 dev_dbg(ndev_dev(ndev), "SBAR01 %#018llx\n", bar_addr);
2f887b9a 1589 iowrite64(bar_addr, mmio + XEON_SBAR0BASE_OFFSET);
6465d02e 1590
e26a5843
AH
1591 /* Other SBAR are normally hit by the PBAR xlat, except for b2b bar.
1592 * The b2b bar is either disabled above, or configured half-size, and
1593 * it starts at the PBAR xlat + offset.
1594 */
1517a3f2 1595
e26a5843 1596 bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
2f887b9a
DJ
1597 iowrite64(bar_addr, mmio + XEON_SBAR23BASE_OFFSET);
1598 bar_addr = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
e26a5843
AH
1599 dev_dbg(ndev_dev(ndev), "SBAR23 %#018llx\n", bar_addr);
1600
1601 if (!ndev->bar4_split) {
1602 bar_addr = addr->bar4_addr64 +
1603 (b2b_bar == 4 ? ndev->b2b_off : 0);
2f887b9a
DJ
1604 iowrite64(bar_addr, mmio + XEON_SBAR45BASE_OFFSET);
1605 bar_addr = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
e26a5843
AH
1606 dev_dbg(ndev_dev(ndev), "SBAR45 %#018llx\n", bar_addr);
1607 } else {
1608 bar_addr = addr->bar4_addr32 +
1609 (b2b_bar == 4 ? ndev->b2b_off : 0);
2f887b9a
DJ
1610 iowrite32(bar_addr, mmio + XEON_SBAR4BASE_OFFSET);
1611 bar_addr = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
e26a5843
AH
1612 dev_dbg(ndev_dev(ndev), "SBAR4 %#010llx\n", bar_addr);
1613
1614 bar_addr = addr->bar5_addr32 +
1615 (b2b_bar == 5 ? ndev->b2b_off : 0);
2f887b9a
DJ
1616 iowrite32(bar_addr, mmio + XEON_SBAR5BASE_OFFSET);
1617 bar_addr = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
e26a5843
AH
1618 dev_dbg(ndev_dev(ndev), "SBAR5 %#010llx\n", bar_addr);
1619 }
1517a3f2 1620
e26a5843 1621 /* setup incoming bar limits == base addrs (zero length windows) */
1517a3f2 1622
e26a5843 1623 bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
2f887b9a
DJ
1624 iowrite64(bar_addr, mmio + XEON_SBAR23LMT_OFFSET);
1625 bar_addr = ioread64(mmio + XEON_SBAR23LMT_OFFSET);
e26a5843 1626 dev_dbg(ndev_dev(ndev), "SBAR23LMT %#018llx\n", bar_addr);
1517a3f2 1627
e26a5843
AH
1628 if (!ndev->bar4_split) {
1629 bar_addr = addr->bar4_addr64 +
1630 (b2b_bar == 4 ? ndev->b2b_off : 0);
2f887b9a
DJ
1631 iowrite64(bar_addr, mmio + XEON_SBAR45LMT_OFFSET);
1632 bar_addr = ioread64(mmio + XEON_SBAR45LMT_OFFSET);
e26a5843
AH
1633 dev_dbg(ndev_dev(ndev), "SBAR45LMT %#018llx\n", bar_addr);
1634 } else {
1635 bar_addr = addr->bar4_addr32 +
1636 (b2b_bar == 4 ? ndev->b2b_off : 0);
2f887b9a
DJ
1637 iowrite32(bar_addr, mmio + XEON_SBAR4LMT_OFFSET);
1638 bar_addr = ioread32(mmio + XEON_SBAR4LMT_OFFSET);
e26a5843
AH
1639 dev_dbg(ndev_dev(ndev), "SBAR4LMT %#010llx\n", bar_addr);
1640
1641 bar_addr = addr->bar5_addr32 +
1642 (b2b_bar == 5 ? ndev->b2b_off : 0);
2f887b9a
DJ
1643 iowrite32(bar_addr, mmio + XEON_SBAR5LMT_OFFSET);
1644 bar_addr = ioread32(mmio + XEON_SBAR5LMT_OFFSET);
e26a5843 1645 dev_dbg(ndev_dev(ndev), "SBAR5LMT %#05llx\n", bar_addr);
1517a3f2 1646 }
1517a3f2 1647
e26a5843 1648 /* zero incoming translation addrs */
2f887b9a 1649 iowrite64(0, mmio + XEON_SBAR23XLAT_OFFSET);
78958433 1650
e26a5843 1651 if (!ndev->bar4_split) {
2f887b9a 1652 iowrite64(0, mmio + XEON_SBAR45XLAT_OFFSET);
e26a5843 1653 } else {
2f887b9a
DJ
1654 iowrite32(0, mmio + XEON_SBAR4XLAT_OFFSET);
1655 iowrite32(0, mmio + XEON_SBAR5XLAT_OFFSET);
e26a5843 1656 }
ab760a0c 1657
e26a5843 1658 /* zero outgoing translation limits (whole bar size windows) */
2f887b9a 1659 iowrite64(0, mmio + XEON_PBAR23LMT_OFFSET);
e26a5843 1660 if (!ndev->bar4_split) {
2f887b9a 1661 iowrite64(0, mmio + XEON_PBAR45LMT_OFFSET);
e26a5843 1662 } else {
2f887b9a
DJ
1663 iowrite32(0, mmio + XEON_PBAR4LMT_OFFSET);
1664 iowrite32(0, mmio + XEON_PBAR5LMT_OFFSET);
78958433 1665 }
9fec60c4 1666
e26a5843
AH
1667 /* set outgoing translation offsets */
1668 bar_addr = peer_addr->bar2_addr64;
2f887b9a
DJ
1669 iowrite64(bar_addr, mmio + XEON_PBAR23XLAT_OFFSET);
1670 bar_addr = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
e26a5843
AH
1671 dev_dbg(ndev_dev(ndev), "PBAR23XLAT %#018llx\n", bar_addr);
1672
1673 if (!ndev->bar4_split) {
1674 bar_addr = peer_addr->bar4_addr64;
2f887b9a
DJ
1675 iowrite64(bar_addr, mmio + XEON_PBAR45XLAT_OFFSET);
1676 bar_addr = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
e26a5843
AH
1677 dev_dbg(ndev_dev(ndev), "PBAR45XLAT %#018llx\n", bar_addr);
1678 } else {
1679 bar_addr = peer_addr->bar4_addr32;
2f887b9a
DJ
1680 iowrite32(bar_addr, mmio + XEON_PBAR4XLAT_OFFSET);
1681 bar_addr = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
e26a5843
AH
1682 dev_dbg(ndev_dev(ndev), "PBAR4XLAT %#010llx\n", bar_addr);
1683
1684 bar_addr = peer_addr->bar5_addr32;
2f887b9a
DJ
1685 iowrite32(bar_addr, mmio + XEON_PBAR5XLAT_OFFSET);
1686 bar_addr = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
e26a5843
AH
1687 dev_dbg(ndev_dev(ndev), "PBAR5XLAT %#010llx\n", bar_addr);
1688 }
9fec60c4 1689
e26a5843
AH
1690 /* set the translation offset for b2b registers */
1691 if (b2b_bar == 0)
1692 bar_addr = peer_addr->bar0_addr;
1693 else if (b2b_bar == 2)
1694 bar_addr = peer_addr->bar2_addr64;
1695 else if (b2b_bar == 4 && !ndev->bar4_split)
1696 bar_addr = peer_addr->bar4_addr64;
1697 else if (b2b_bar == 4)
1698 bar_addr = peer_addr->bar4_addr32;
1699 else if (b2b_bar == 5)
1700 bar_addr = peer_addr->bar5_addr32;
1701 else
1702 return -EIO;
1703
1704 /* B2B_XLAT_OFFSET is 64bit, but can only take 32bit writes */
1705 dev_dbg(ndev_dev(ndev), "B2BXLAT %#018llx\n", bar_addr);
2f887b9a
DJ
1706 iowrite32(bar_addr, mmio + XEON_B2B_XLAT_OFFSETL);
1707 iowrite32(bar_addr >> 32, mmio + XEON_B2B_XLAT_OFFSETU);
e26a5843
AH
1708
1709 if (b2b_bar) {
1710 /* map peer ntb mmio config space registers */
1711 ndev->peer_mmio = pci_iomap(pdev, b2b_bar,
2f887b9a 1712 XEON_B2B_MIN_SIZE);
e26a5843
AH
1713 if (!ndev->peer_mmio)
1714 return -EIO;
9fec60c4
JM
1715 }
1716
e26a5843 1717 return 0;
9fec60c4
JM
1718}
1719
2f887b9a 1720static int xeon_init_ntb(struct intel_ntb_dev *ndev)
ab760a0c 1721{
e26a5843 1722 int rc;
5ae0beb6 1723 u32 ntb_ctl;
e26a5843
AH
1724
1725 if (ndev->bar4_split)
1726 ndev->mw_count = HSX_SPLIT_BAR_MW_COUNT;
ab760a0c 1727 else
2f887b9a 1728 ndev->mw_count = XEON_MW_COUNT;
ab760a0c 1729
2f887b9a
DJ
1730 ndev->spad_count = XEON_SPAD_COUNT;
1731 ndev->db_count = XEON_DB_COUNT;
1732 ndev->db_link_mask = XEON_DB_LINK_BIT;
1db97f25 1733
e26a5843
AH
1734 switch (ndev->ntb.topo) {
1735 case NTB_TOPO_PRI:
1736 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
1737 dev_err(ndev_dev(ndev), "NTB Primary config disabled\n");
1738 return -EINVAL;
1739 }
5ae0beb6
DJ
1740
1741 /* enable link to allow secondary side device to appear */
1742 ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
1743 ntb_ctl &= ~NTB_CTL_DISABLE;
1744 iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
1745
e26a5843
AH
1746 /* use half the spads for the peer */
1747 ndev->spad_count >>= 1;
2f887b9a
DJ
1748 ndev->self_reg = &xeon_pri_reg;
1749 ndev->peer_reg = &xeon_sec_reg;
1750 ndev->xlat_reg = &xeon_sec_xlat;
e26a5843 1751 break;
1db97f25 1752
e26a5843
AH
1753 case NTB_TOPO_SEC:
1754 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
1755 dev_err(ndev_dev(ndev), "NTB Secondary config disabled\n");
1756 return -EINVAL;
1757 }
1758 /* use half the spads for the peer */
1759 ndev->spad_count >>= 1;
2f887b9a
DJ
1760 ndev->self_reg = &xeon_sec_reg;
1761 ndev->peer_reg = &xeon_pri_reg;
1762 ndev->xlat_reg = &xeon_pri_xlat;
e26a5843 1763 break;
1db97f25 1764
e26a5843
AH
1765 case NTB_TOPO_B2B_USD:
1766 case NTB_TOPO_B2B_DSD:
2f887b9a
DJ
1767 ndev->self_reg = &xeon_pri_reg;
1768 ndev->peer_reg = &xeon_b2b_reg;
1769 ndev->xlat_reg = &xeon_sec_xlat;
1db97f25 1770
e26a5843 1771 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
2f887b9a 1772 ndev->peer_reg = &xeon_pri_reg;
ab760a0c 1773
e26a5843
AH
1774 if (b2b_mw_idx < 0)
1775 ndev->b2b_idx = b2b_mw_idx + ndev->mw_count;
1776 else
1777 ndev->b2b_idx = b2b_mw_idx;
ab760a0c 1778
e26a5843
AH
1779 dev_dbg(ndev_dev(ndev),
1780 "setting up b2b mw idx %d means %d\n",
1781 b2b_mw_idx, ndev->b2b_idx);
1782
1783 } else if (ndev->hwerr_flags & NTB_HWERR_B2BDOORBELL_BIT14) {
1784 dev_warn(ndev_dev(ndev), "Reduce doorbell count by 1\n");
1785 ndev->db_count -= 1;
1786 }
1787
1788 if (ndev->ntb.topo == NTB_TOPO_B2B_USD) {
2f887b9a
DJ
1789 rc = xeon_setup_b2b_mw(ndev,
1790 &xeon_b2b_dsd_addr,
1791 &xeon_b2b_usd_addr);
e26a5843 1792 } else {
2f887b9a
DJ
1793 rc = xeon_setup_b2b_mw(ndev,
1794 &xeon_b2b_usd_addr,
1795 &xeon_b2b_dsd_addr);
e26a5843
AH
1796 }
1797 if (rc)
1798 return rc;
1799
1800 /* Enable Bus Master and Memory Space on the secondary side */
1801 iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
2f887b9a 1802 ndev->self_mmio + XEON_SPCICMD_OFFSET);
ab760a0c 1803
1db97f25 1804 break;
e26a5843 1805
1db97f25 1806 default:
e26a5843 1807 return -EINVAL;
1db97f25
DJ
1808 }
1809
e26a5843
AH
1810 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
1811
1812 ndev->reg->db_iowrite(ndev->db_valid_mask,
1813 ndev->self_mmio +
1814 ndev->self_reg->db_mask);
ab760a0c 1815
1db97f25
DJ
1816 return 0;
1817}
1818
2f887b9a 1819static int xeon_init_dev(struct intel_ntb_dev *ndev)
1db97f25 1820{
e26a5843
AH
1821 struct pci_dev *pdev;
1822 u8 ppd;
1823 int rc, mem;
1824
dd5d4d8e
DJ
1825 pdev = ndev_pdev(ndev);
1826
1827 switch (pdev->device) {
e26a5843
AH
1828 /* There is a Xeon hardware errata related to writes to SDOORBELL or
1829 * B2BDOORBELL in conjunction with inbound access to NTB MMIO Space,
1830 * which may hang the system. To workaround this use the second memory
1831 * window to access the interrupt and scratch pad registers on the
1832 * remote system.
1833 */
dd5d4d8e
DJ
1834 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
1835 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
1836 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
1837 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
1838 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
1839 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
1840 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
1841 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
1842 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
1843 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
1844 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
1845 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
1846 ndev->hwerr_flags |= NTB_HWERR_SDOORBELL_LOCKUP;
1847 break;
1848 }
1db97f25 1849
dd5d4d8e 1850 switch (pdev->device) {
e26a5843
AH
1851 /* There is a hardware errata related to accessing any register in
1852 * SB01BASE in the presence of bidirectional traffic crossing the NTB.
1853 */
dd5d4d8e
DJ
1854 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
1855 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
1856 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
1857 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
1858 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
1859 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
1860 ndev->hwerr_flags |= NTB_HWERR_SB01BASE_LOCKUP;
1861 break;
1862 }
e26a5843 1863
dd5d4d8e 1864 switch (pdev->device) {
e26a5843
AH
1865 /* HW Errata on bit 14 of b2bdoorbell register. Writes will not be
1866 * mirrored to the remote system. Shrink the number of bits by one,
1867 * since bit 14 is the last bit.
1868 */
dd5d4d8e
DJ
1869 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
1870 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
1871 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
1872 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
1873 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
1874 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
1875 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
1876 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
1877 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
1878 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
1879 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
1880 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
1881 ndev->hwerr_flags |= NTB_HWERR_B2BDOORBELL_BIT14;
1882 break;
1883 }
1db97f25 1884
2f887b9a 1885 ndev->reg = &xeon_reg;
e26a5843 1886
2f887b9a 1887 rc = pci_read_config_byte(pdev, XEON_PPD_OFFSET, &ppd);
1db97f25 1888 if (rc)
e26a5843 1889 return -EIO;
1db97f25 1890
2f887b9a 1891 ndev->ntb.topo = xeon_ppd_topo(ndev, ppd);
e26a5843
AH
1892 dev_dbg(ndev_dev(ndev), "ppd %#x topo %s\n", ppd,
1893 ntb_topo_string(ndev->ntb.topo));
1894 if (ndev->ntb.topo == NTB_TOPO_NONE)
1db97f25 1895 return -EINVAL;
e26a5843
AH
1896
1897 if (ndev->ntb.topo != NTB_TOPO_SEC) {
2f887b9a 1898 ndev->bar4_split = xeon_ppd_bar4_split(ndev, ppd);
e26a5843
AH
1899 dev_dbg(ndev_dev(ndev), "ppd %#x bar4_split %d\n",
1900 ppd, ndev->bar4_split);
1901 } else {
1902 /* This is a way for transparent BAR to figure out if we are
1903 * doing split BAR or not. There is no way for the hw on the
1904 * transparent side to know and set the PPD.
1905 */
1906 mem = pci_select_bars(pdev, IORESOURCE_MEM);
1907 ndev->bar4_split = hweight32(mem) ==
1908 HSX_SPLIT_BAR_MW_COUNT + 1;
1909 dev_dbg(ndev_dev(ndev), "mem %#x bar4_split %d\n",
1910 mem, ndev->bar4_split);
1db97f25
DJ
1911 }
1912
2f887b9a 1913 rc = xeon_init_ntb(ndev);
e26a5843
AH
1914 if (rc)
1915 return rc;
1db97f25 1916
2f887b9a 1917 return xeon_init_isr(ndev);
e26a5843
AH
1918}
1919
2f887b9a 1920static void xeon_deinit_dev(struct intel_ntb_dev *ndev)
e26a5843 1921{
2f887b9a 1922 xeon_deinit_isr(ndev);
1db97f25
DJ
1923}
1924
e26a5843 1925static int intel_ntb_init_pci(struct intel_ntb_dev *ndev, struct pci_dev *pdev)
1db97f25
DJ
1926{
1927 int rc;
1928
e26a5843
AH
1929 pci_set_drvdata(pdev, ndev);
1930
1931 rc = pci_enable_device(pdev);
1932 if (rc)
1933 goto err_pci_enable;
1934
1935 rc = pci_request_regions(pdev, NTB_NAME);
1936 if (rc)
1937 goto err_pci_regions;
1938
1939 pci_set_master(pdev);
1940
1941 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1942 if (rc) {
1943 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1944 if (rc)
1945 goto err_dma_mask;
1946 dev_warn(ndev_dev(ndev), "Cannot DMA highmem\n");
1947 }
1db97f25 1948
e26a5843
AH
1949 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1950 if (rc) {
1951 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1952 if (rc)
1953 goto err_dma_mask;
1954 dev_warn(ndev_dev(ndev), "Cannot DMA consistent highmem\n");
1955 }
1956
1957 ndev->self_mmio = pci_iomap(pdev, 0, 0);
1958 if (!ndev->self_mmio) {
1959 rc = -EIO;
1960 goto err_mmio;
1961 }
1962 ndev->peer_mmio = ndev->self_mmio;
1db97f25
DJ
1963
1964 return 0;
e26a5843
AH
1965
1966err_mmio:
1967err_dma_mask:
1968 pci_clear_master(pdev);
1969 pci_release_regions(pdev);
1970err_pci_regions:
1971 pci_disable_device(pdev);
1972err_pci_enable:
1973 pci_set_drvdata(pdev, NULL);
1974 return rc;
1db97f25
DJ
1975}
1976
e26a5843 1977static void intel_ntb_deinit_pci(struct intel_ntb_dev *ndev)
fce8a7bb 1978{
e26a5843 1979 struct pci_dev *pdev = ndev_pdev(ndev);
fce8a7bb 1980
e26a5843
AH
1981 if (ndev->peer_mmio && ndev->peer_mmio != ndev->self_mmio)
1982 pci_iounmap(pdev, ndev->peer_mmio);
1983 pci_iounmap(pdev, ndev->self_mmio);
fce8a7bb 1984
e26a5843
AH
1985 pci_clear_master(pdev);
1986 pci_release_regions(pdev);
1987 pci_disable_device(pdev);
1988 pci_set_drvdata(pdev, NULL);
1989}
069684e8 1990
e26a5843
AH
1991static inline void ndev_init_struct(struct intel_ntb_dev *ndev,
1992 struct pci_dev *pdev)
1993{
1994 ndev->ntb.pdev = pdev;
1995 ndev->ntb.topo = NTB_TOPO_NONE;
1996 ndev->ntb.ops = &intel_ntb_ops;
069684e8 1997
e26a5843
AH
1998 ndev->b2b_off = 0;
1999 ndev->b2b_idx = INT_MAX;
fce8a7bb 2000
e26a5843 2001 ndev->bar4_split = 0;
fce8a7bb 2002
e26a5843
AH
2003 ndev->mw_count = 0;
2004 ndev->spad_count = 0;
2005 ndev->db_count = 0;
2006 ndev->db_vec_count = 0;
2007 ndev->db_vec_shift = 0;
fce8a7bb 2008
e26a5843
AH
2009 ndev->ntb_ctl = 0;
2010 ndev->lnk_sta = 0;
1db97f25 2011
e26a5843
AH
2012 ndev->db_valid_mask = 0;
2013 ndev->db_link_mask = 0;
2014 ndev->db_mask = 0;
ab760a0c 2015
e26a5843
AH
2016 spin_lock_init(&ndev->db_mask_lock);
2017}
ab760a0c 2018
e26a5843
AH
2019static int intel_ntb_pci_probe(struct pci_dev *pdev,
2020 const struct pci_device_id *id)
2021{
2022 struct intel_ntb_dev *ndev;
0e041fb5
AH
2023 int rc, node;
2024
2025 node = dev_to_node(&pdev->dev);
fce8a7bb 2026
2f887b9a 2027 if (pdev_is_atom(pdev)) {
0e041fb5 2028 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
e26a5843
AH
2029 if (!ndev) {
2030 rc = -ENOMEM;
2031 goto err_ndev;
2032 }
fce8a7bb 2033
e26a5843 2034 ndev_init_struct(ndev, pdev);
ab760a0c 2035
e26a5843
AH
2036 rc = intel_ntb_init_pci(ndev, pdev);
2037 if (rc)
2038 goto err_init_pci;
2039
2f887b9a 2040 rc = atom_init_dev(ndev);
e26a5843
AH
2041 if (rc)
2042 goto err_init_dev;
ab760a0c 2043
2f887b9a 2044 } else if (pdev_is_xeon(pdev)) {
0e041fb5 2045 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
e26a5843
AH
2046 if (!ndev) {
2047 rc = -ENOMEM;
2048 goto err_ndev;
fce8a7bb 2049 }
fce8a7bb 2050
e26a5843 2051 ndev_init_struct(ndev, pdev);
fce8a7bb 2052
e26a5843
AH
2053 rc = intel_ntb_init_pci(ndev, pdev);
2054 if (rc)
2055 goto err_init_pci;
fce8a7bb 2056
2f887b9a 2057 rc = xeon_init_dev(ndev);
fce8a7bb 2058 if (rc)
e26a5843 2059 goto err_init_dev;
fce8a7bb 2060
e26a5843
AH
2061 } else {
2062 rc = -EINVAL;
2063 goto err_ndev;
fce8a7bb
JM
2064 }
2065
e26a5843 2066 ndev_reset_unsafe_flags(ndev);
fce8a7bb 2067
e26a5843 2068 ndev->reg->poll_link(ndev);
fce8a7bb 2069
e26a5843 2070 ndev_init_debugfs(ndev);
fce8a7bb 2071
e26a5843 2072 rc = ntb_register_device(&ndev->ntb);
fce8a7bb 2073 if (rc)
e26a5843 2074 goto err_register;
fce8a7bb 2075
7eb38781
DJ
2076 dev_info(&pdev->dev, "NTB device registered.\n");
2077
fce8a7bb
JM
2078 return 0;
2079
e26a5843
AH
2080err_register:
2081 ndev_deinit_debugfs(ndev);
2f887b9a
DJ
2082 if (pdev_is_atom(pdev))
2083 atom_deinit_dev(ndev);
2084 else if (pdev_is_xeon(pdev))
2085 xeon_deinit_dev(ndev);
e26a5843
AH
2086err_init_dev:
2087 intel_ntb_deinit_pci(ndev);
2088err_init_pci:
fce8a7bb 2089 kfree(ndev);
e26a5843 2090err_ndev:
fce8a7bb
JM
2091 return rc;
2092}
2093
e26a5843 2094static void intel_ntb_pci_remove(struct pci_dev *pdev)
fce8a7bb 2095{
e26a5843
AH
2096 struct intel_ntb_dev *ndev = pci_get_drvdata(pdev);
2097
2098 ntb_unregister_device(&ndev->ntb);
2099 ndev_deinit_debugfs(ndev);
2f887b9a
DJ
2100 if (pdev_is_atom(pdev))
2101 atom_deinit_dev(ndev);
2102 else if (pdev_is_xeon(pdev))
2103 xeon_deinit_dev(ndev);
e26a5843
AH
2104 intel_ntb_deinit_pci(ndev);
2105 kfree(ndev);
2106}
fce8a7bb 2107
2f887b9a
DJ
2108static const struct intel_ntb_reg atom_reg = {
2109 .poll_link = atom_poll_link,
2110 .link_is_up = atom_link_is_up,
2111 .db_ioread = atom_db_ioread,
2112 .db_iowrite = atom_db_iowrite,
e26a5843 2113 .db_size = sizeof(u64),
2f887b9a 2114 .ntb_ctl = ATOM_NTBCNTL_OFFSET,
e26a5843
AH
2115 .mw_bar = {2, 4},
2116};
fce8a7bb 2117
2f887b9a
DJ
2118static const struct intel_ntb_alt_reg atom_pri_reg = {
2119 .db_bell = ATOM_PDOORBELL_OFFSET,
2120 .db_mask = ATOM_PDBMSK_OFFSET,
2121 .spad = ATOM_SPAD_OFFSET,
e26a5843 2122};
fce8a7bb 2123
2f887b9a
DJ
2124static const struct intel_ntb_alt_reg atom_b2b_reg = {
2125 .db_bell = ATOM_B2B_DOORBELL_OFFSET,
2126 .spad = ATOM_B2B_SPAD_OFFSET,
e26a5843 2127};
fce8a7bb 2128
2f887b9a
DJ
2129static const struct intel_ntb_xlat_reg atom_sec_xlat = {
2130 /* FIXME : .bar0_base = ATOM_SBAR0BASE_OFFSET, */
2131 /* FIXME : .bar2_limit = ATOM_SBAR2LMT_OFFSET, */
2132 .bar2_xlat = ATOM_SBAR2XLAT_OFFSET,
e26a5843 2133};
ab760a0c 2134
2f887b9a
DJ
2135static const struct intel_ntb_reg xeon_reg = {
2136 .poll_link = xeon_poll_link,
2137 .link_is_up = xeon_link_is_up,
2138 .db_ioread = xeon_db_ioread,
2139 .db_iowrite = xeon_db_iowrite,
e26a5843 2140 .db_size = sizeof(u32),
2f887b9a 2141 .ntb_ctl = XEON_NTBCNTL_OFFSET,
e26a5843
AH
2142 .mw_bar = {2, 4, 5},
2143};
fce8a7bb 2144
2f887b9a
DJ
2145static const struct intel_ntb_alt_reg xeon_pri_reg = {
2146 .db_bell = XEON_PDOORBELL_OFFSET,
2147 .db_mask = XEON_PDBMSK_OFFSET,
2148 .spad = XEON_SPAD_OFFSET,
e26a5843
AH
2149};
2150
2f887b9a
DJ
2151static const struct intel_ntb_alt_reg xeon_sec_reg = {
2152 .db_bell = XEON_SDOORBELL_OFFSET,
2153 .db_mask = XEON_SDBMSK_OFFSET,
e26a5843 2154 /* second half of the scratchpads */
2f887b9a 2155 .spad = XEON_SPAD_OFFSET + (XEON_SPAD_COUNT << 1),
e26a5843 2156};
fce8a7bb 2157
2f887b9a
DJ
2158static const struct intel_ntb_alt_reg xeon_b2b_reg = {
2159 .db_bell = XEON_B2B_DOORBELL_OFFSET,
2160 .spad = XEON_B2B_SPAD_OFFSET,
e26a5843
AH
2161};
2162
2f887b9a 2163static const struct intel_ntb_xlat_reg xeon_pri_xlat = {
e26a5843
AH
2164 /* Note: no primary .bar0_base visible to the secondary side.
2165 *
2166 * The secondary side cannot get the base address stored in primary
2167 * bars. The base address is necessary to set the limit register to
2168 * any value other than zero, or unlimited.
2169 *
2170 * WITHOUT THE BASE ADDRESS, THE SECONDARY SIDE CANNOT DISABLE the
2171 * window by setting the limit equal to base, nor can it limit the size
2172 * of the memory window by setting the limit to base + size.
2173 */
2f887b9a
DJ
2174 .bar2_limit = XEON_PBAR23LMT_OFFSET,
2175 .bar2_xlat = XEON_PBAR23XLAT_OFFSET,
e26a5843
AH
2176};
2177
2f887b9a
DJ
2178static const struct intel_ntb_xlat_reg xeon_sec_xlat = {
2179 .bar0_base = XEON_SBAR0BASE_OFFSET,
2180 .bar2_limit = XEON_SBAR23LMT_OFFSET,
2181 .bar2_xlat = XEON_SBAR23XLAT_OFFSET,
e26a5843
AH
2182};
2183
2f887b9a
DJ
2184static struct intel_b2b_addr xeon_b2b_usd_addr = {
2185 .bar2_addr64 = XEON_B2B_BAR2_USD_ADDR64,
2186 .bar4_addr64 = XEON_B2B_BAR4_USD_ADDR64,
2187 .bar4_addr32 = XEON_B2B_BAR4_USD_ADDR32,
2188 .bar5_addr32 = XEON_B2B_BAR5_USD_ADDR32,
e26a5843
AH
2189};
2190
2f887b9a
DJ
2191static struct intel_b2b_addr xeon_b2b_dsd_addr = {
2192 .bar2_addr64 = XEON_B2B_BAR2_DSD_ADDR64,
2193 .bar4_addr64 = XEON_B2B_BAR4_DSD_ADDR64,
2194 .bar4_addr32 = XEON_B2B_BAR4_DSD_ADDR32,
2195 .bar5_addr32 = XEON_B2B_BAR5_DSD_ADDR32,
e26a5843
AH
2196};
2197
2198/* operations for primary side of local ntb */
2199static const struct ntb_dev_ops intel_ntb_ops = {
2200 .mw_count = intel_ntb_mw_count,
2201 .mw_get_range = intel_ntb_mw_get_range,
2202 .mw_set_trans = intel_ntb_mw_set_trans,
2203 .link_is_up = intel_ntb_link_is_up,
2204 .link_enable = intel_ntb_link_enable,
2205 .link_disable = intel_ntb_link_disable,
2206 .db_is_unsafe = intel_ntb_db_is_unsafe,
2207 .db_valid_mask = intel_ntb_db_valid_mask,
2208 .db_vector_count = intel_ntb_db_vector_count,
2209 .db_vector_mask = intel_ntb_db_vector_mask,
2210 .db_read = intel_ntb_db_read,
2211 .db_clear = intel_ntb_db_clear,
2212 .db_set_mask = intel_ntb_db_set_mask,
2213 .db_clear_mask = intel_ntb_db_clear_mask,
2214 .peer_db_addr = intel_ntb_peer_db_addr,
2215 .peer_db_set = intel_ntb_peer_db_set,
2216 .spad_is_unsafe = intel_ntb_spad_is_unsafe,
2217 .spad_count = intel_ntb_spad_count,
2218 .spad_read = intel_ntb_spad_read,
2219 .spad_write = intel_ntb_spad_write,
2220 .peer_spad_addr = intel_ntb_peer_spad_addr,
2221 .peer_spad_read = intel_ntb_peer_spad_read,
2222 .peer_spad_write = intel_ntb_peer_spad_write,
2223};
2224
2225static const struct file_operations intel_ntb_debugfs_info = {
2226 .owner = THIS_MODULE,
2227 .open = simple_open,
2228 .read = ndev_debugfs_read,
2229};
2230
2231static const struct pci_device_id intel_ntb_pci_tbl[] = {
2232 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
2233 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
2234 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
2235 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
2236 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
2237 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
2238 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
2239 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
2240 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
2241 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
2242 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
2243 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
2244 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
2245 {0}
2246};
2247MODULE_DEVICE_TABLE(pci, intel_ntb_pci_tbl);
2248
2249static struct pci_driver intel_ntb_pci_driver = {
fce8a7bb 2250 .name = KBUILD_MODNAME,
e26a5843
AH
2251 .id_table = intel_ntb_pci_tbl,
2252 .probe = intel_ntb_pci_probe,
2253 .remove = intel_ntb_pci_remove,
fce8a7bb 2254};
6465d02e 2255
e26a5843
AH
2256static int __init intel_ntb_pci_driver_init(void)
2257{
7eb38781
DJ
2258 pr_info("%s %s\n", NTB_DESC, NTB_VER);
2259
e26a5843
AH
2260 if (debugfs_initialized())
2261 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2262
2263 return pci_register_driver(&intel_ntb_pci_driver);
2264}
2265module_init(intel_ntb_pci_driver_init);
2266
2267static void __exit intel_ntb_pci_driver_exit(void)
2268{
2269 pci_unregister_driver(&intel_ntb_pci_driver);
2270
2271 debugfs_remove_recursive(debugfs_dir);
2272}
2273module_exit(intel_ntb_pci_driver_exit);
2274