blob: e00aa39a454443114da6f20579a8a3fbbb44a688 [file] [log] [blame]
Jon Masonfce8a7b2012-11-16 19:27:12 -07001/*
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.
Allen Hubbee26a5842015-04-09 10:33:20 -04008 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Serge Semin443b9a12017-01-11 03:11:33 +03009 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -070010 *
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.
14 *
15 * BSD LICENSE
16 *
17 * Copyright(c) 2012 Intel Corporation. All rights reserved.
Allen Hubbee26a5842015-04-09 10:33:20 -040018 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
Serge Semin443b9a12017-01-11 03:11:33 +030019 * Copyright (C) 2016 T-Platforms. All Rights Reserved.
Jon Masonfce8a7b2012-11-16 19:27:12 -070020 *
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
23 * are met:
24 *
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
30 * distribution.
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.
34 *
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.
46 *
47 * Intel PCIe NTB Linux driver
48 *
49 * Contact Information:
50 * Jon Mason <jon.mason@intel.com>
51 */
Allen Hubbee26a5842015-04-09 10:33:20 -040052
Jon Masonfce8a7b2012-11-16 19:27:12 -070053#include <linux/debugfs.h>
Jon Mason113bf1c2012-11-16 18:52:57 -070054#include <linux/delay.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070055#include <linux/init.h>
56#include <linux/interrupt.h>
57#include <linux/module.h>
58#include <linux/pci.h>
Jon Mason113bf1c2012-11-16 18:52:57 -070059#include <linux/random.h>
Jon Masonfce8a7b2012-11-16 19:27:12 -070060#include <linux/slab.h>
Allen Hubbee26a5842015-04-09 10:33:20 -040061#include <linux/ntb.h>
62
Allen Hubbeec110bc2015-05-07 06:45:21 -040063#include "ntb_hw_intel.h"
Jon Masonfce8a7b2012-11-16 19:27:12 -070064
Allen Hubbee26a5842015-04-09 10:33:20 -040065#define NTB_NAME "ntb_hw_intel"
66#define NTB_DESC "Intel(R) PCI-E Non-Transparent Bridge Driver"
67#define NTB_VER "2.0"
Jon Masonfce8a7b2012-11-16 19:27:12 -070068
Allen Hubbee26a5842015-04-09 10:33:20 -040069MODULE_DESCRIPTION(NTB_DESC);
Jon Masonfce8a7b2012-11-16 19:27:12 -070070MODULE_VERSION(NTB_VER);
71MODULE_LICENSE("Dual BSD/GPL");
72MODULE_AUTHOR("Intel Corporation");
73
Allen Hubbee26a5842015-04-09 10:33:20 -040074#define bar0_off(base, bar) ((base) + ((bar) << 2))
75#define bar2_off(base, bar) bar0_off(base, (bar) - 2)
Jon Masonfce8a7b2012-11-16 19:27:12 -070076
Dave Jiang2f887b92015-05-20 12:55:47 -040077static const struct intel_ntb_reg atom_reg;
78static const struct intel_ntb_alt_reg atom_pri_reg;
79static const struct intel_ntb_alt_reg atom_sec_reg;
80static const struct intel_ntb_alt_reg atom_b2b_reg;
81static const struct intel_ntb_xlat_reg atom_pri_xlat;
82static const struct intel_ntb_xlat_reg atom_sec_xlat;
83static const struct intel_ntb_reg xeon_reg;
84static const struct intel_ntb_alt_reg xeon_pri_reg;
85static const struct intel_ntb_alt_reg xeon_sec_reg;
86static const struct intel_ntb_alt_reg xeon_b2b_reg;
87static const struct intel_ntb_xlat_reg xeon_pri_xlat;
88static const struct intel_ntb_xlat_reg xeon_sec_xlat;
89static struct intel_b2b_addr xeon_b2b_usd_addr;
90static struct intel_b2b_addr xeon_b2b_dsd_addr;
Dave Jiang783dfa62016-11-16 14:03:38 -070091static const struct intel_ntb_reg skx_reg;
92static const struct intel_ntb_alt_reg skx_pri_reg;
93static const struct intel_ntb_alt_reg skx_b2b_reg;
94static const struct intel_ntb_xlat_reg skx_sec_xlat;
Allen Hubbe42fefc82015-05-11 05:45:30 -040095static const struct ntb_dev_ops intel_ntb_ops;
Dave Jiang783dfa62016-11-16 14:03:38 -070096static const struct ntb_dev_ops intel_ntb3_ops;
Allen Hubbe42fefc82015-05-11 05:45:30 -040097
98static const struct file_operations intel_ntb_debugfs_info;
99static struct dentry *debugfs_dir;
100
Allen Hubbee26a5842015-04-09 10:33:20 -0400101static int b2b_mw_idx = -1;
102module_param(b2b_mw_idx, int, 0644);
103MODULE_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!");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700107
Allen Hubbee26a5842015-04-09 10:33:20 -0400108static unsigned int b2b_mw_share;
109module_param(b2b_mw_share, uint, 0644);
110MODULE_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!");
Jon Masonfce8a7b2012-11-16 19:27:12 -0700114
Dave Jiang2f887b92015-05-20 12:55:47 -0400115module_param_named(xeon_b2b_usd_bar2_addr64,
116 xeon_b2b_usd_addr.bar2_addr64, ullong, 0644);
117MODULE_PARM_DESC(xeon_b2b_usd_bar2_addr64,
118 "XEON B2B USD BAR 2 64-bit address");
Allen Hubbee26a5842015-04-09 10:33:20 -0400119
Dave Jiang2f887b92015-05-20 12:55:47 -0400120module_param_named(xeon_b2b_usd_bar4_addr64,
121 xeon_b2b_usd_addr.bar4_addr64, ullong, 0644);
Wei Yongjun49b89de2016-08-08 09:48:42 +0000122MODULE_PARM_DESC(xeon_b2b_usd_bar4_addr64,
Dave Jiang2f887b92015-05-20 12:55:47 -0400123 "XEON B2B USD BAR 4 64-bit address");
Allen Hubbee26a5842015-04-09 10:33:20 -0400124
Dave Jiang2f887b92015-05-20 12:55:47 -0400125module_param_named(xeon_b2b_usd_bar4_addr32,
126 xeon_b2b_usd_addr.bar4_addr32, ullong, 0644);
Wei Yongjun49b89de2016-08-08 09:48:42 +0000127MODULE_PARM_DESC(xeon_b2b_usd_bar4_addr32,
Dave Jiang2f887b92015-05-20 12:55:47 -0400128 "XEON B2B USD split-BAR 4 32-bit address");
Allen Hubbe42fefc82015-05-11 05:45:30 -0400129
Dave Jiang2f887b92015-05-20 12:55:47 -0400130module_param_named(xeon_b2b_usd_bar5_addr32,
131 xeon_b2b_usd_addr.bar5_addr32, ullong, 0644);
Wei Yongjun49b89de2016-08-08 09:48:42 +0000132MODULE_PARM_DESC(xeon_b2b_usd_bar5_addr32,
Dave Jiang2f887b92015-05-20 12:55:47 -0400133 "XEON B2B USD split-BAR 5 32-bit address");
Allen Hubbe42fefc82015-05-11 05:45:30 -0400134
Dave Jiang2f887b92015-05-20 12:55:47 -0400135module_param_named(xeon_b2b_dsd_bar2_addr64,
136 xeon_b2b_dsd_addr.bar2_addr64, ullong, 0644);
137MODULE_PARM_DESC(xeon_b2b_dsd_bar2_addr64,
138 "XEON B2B DSD BAR 2 64-bit address");
Allen Hubbe42fefc82015-05-11 05:45:30 -0400139
Dave Jiang2f887b92015-05-20 12:55:47 -0400140module_param_named(xeon_b2b_dsd_bar4_addr64,
141 xeon_b2b_dsd_addr.bar4_addr64, ullong, 0644);
Wei Yongjun49b89de2016-08-08 09:48:42 +0000142MODULE_PARM_DESC(xeon_b2b_dsd_bar4_addr64,
Dave Jiang2f887b92015-05-20 12:55:47 -0400143 "XEON B2B DSD BAR 4 64-bit address");
Allen Hubbe42fefc82015-05-11 05:45:30 -0400144
Dave Jiang2f887b92015-05-20 12:55:47 -0400145module_param_named(xeon_b2b_dsd_bar4_addr32,
146 xeon_b2b_dsd_addr.bar4_addr32, ullong, 0644);
Wei Yongjun49b89de2016-08-08 09:48:42 +0000147MODULE_PARM_DESC(xeon_b2b_dsd_bar4_addr32,
Dave Jiang2f887b92015-05-20 12:55:47 -0400148 "XEON B2B DSD split-BAR 4 32-bit address");
Allen Hubbe42fefc82015-05-11 05:45:30 -0400149
Dave Jiang2f887b92015-05-20 12:55:47 -0400150module_param_named(xeon_b2b_dsd_bar5_addr32,
151 xeon_b2b_dsd_addr.bar5_addr32, ullong, 0644);
Wei Yongjun49b89de2016-08-08 09:48:42 +0000152MODULE_PARM_DESC(xeon_b2b_dsd_bar5_addr32,
Dave Jiang2f887b92015-05-20 12:55:47 -0400153 "XEON B2B DSD split-BAR 5 32-bit address");
Jon Mason1517a3f2013-07-30 15:58:49 -0700154
Dave Jiang783dfa62016-11-16 14:03:38 -0700155static inline enum ntb_topo xeon_ppd_topo(struct intel_ntb_dev *ndev, u8 ppd);
156static int xeon_init_isr(struct intel_ntb_dev *ndev);
157
Allen Hubbee26a5842015-04-09 10:33:20 -0400158#ifndef ioread64
159#ifdef readq
160#define ioread64 readq
161#else
162#define ioread64 _ioread64
163static inline u64 _ioread64(void __iomem *mmio)
164{
165 u64 low, high;
Jon Mason113bf1c2012-11-16 18:52:57 -0700166
Allen Hubbee26a5842015-04-09 10:33:20 -0400167 low = ioread32(mmio);
168 high = ioread32(mmio + sizeof(u32));
169 return low | (high << 32);
170}
171#endif
172#endif
Jon Masonfce8a7b2012-11-16 19:27:12 -0700173
Allen Hubbee26a5842015-04-09 10:33:20 -0400174#ifndef iowrite64
175#ifdef writeq
176#define iowrite64 writeq
177#else
178#define iowrite64 _iowrite64
179static inline void _iowrite64(u64 val, void __iomem *mmio)
180{
181 iowrite32(val, mmio);
182 iowrite32(val >> 32, mmio + sizeof(u32));
183}
184#endif
185#endif
186
Dave Jiang2f887b92015-05-20 12:55:47 -0400187static inline int pdev_is_atom(struct pci_dev *pdev)
Allen Hubbee26a5842015-04-09 10:33:20 -0400188{
189 switch (pdev->device) {
190 case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
191 return 1;
192 }
193 return 0;
194}
195
Dave Jiang2f887b92015-05-20 12:55:47 -0400196static inline int pdev_is_xeon(struct pci_dev *pdev)
Allen Hubbee26a5842015-04-09 10:33:20 -0400197{
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:
Dave Jiang0a5d19d2015-07-13 08:07:18 -0400203 case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
Allen Hubbee26a5842015-04-09 10:33:20 -0400204 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:
Dave Jiang0a5d19d2015-07-13 08:07:18 -0400208 case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
Allen Hubbee26a5842015-04-09 10:33:20 -0400209 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:
Dave Jiang0a5d19d2015-07-13 08:07:18 -0400213 case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
Allen Hubbee26a5842015-04-09 10:33:20 -0400214 return 1;
215 }
216 return 0;
217}
218
Dave Jiang783dfa62016-11-16 14:03:38 -0700219static inline int pdev_is_skx_xeon(struct pci_dev *pdev)
220{
221 if (pdev->device == PCI_DEVICE_ID_INTEL_NTB_B2B_SKX)
222 return 1;
223
224 return 0;
225}
226
Allen Hubbee26a5842015-04-09 10:33:20 -0400227static inline void ndev_reset_unsafe_flags(struct intel_ntb_dev *ndev)
228{
229 ndev->unsafe_flags = 0;
230 ndev->unsafe_flags_ignore = 0;
231
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;
236
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;
241 }
242}
243
244static inline int ndev_is_unsafe(struct intel_ntb_dev *ndev,
245 unsigned long flag)
246{
247 return !!(flag & ndev->unsafe_flags & ~ndev->unsafe_flags_ignore);
248}
249
250static inline int ndev_ignore_unsafe(struct intel_ntb_dev *ndev,
251 unsigned long flag)
252{
253 flag &= ndev->unsafe_flags;
254 ndev->unsafe_flags_ignore |= flag;
255
256 return !!flag;
257}
258
259static int ndev_mw_to_bar(struct intel_ntb_dev *ndev, int idx)
260{
Allen Hubbe9a078262015-08-31 09:31:00 -0400261 if (idx < 0 || idx >= ndev->mw_count)
Allen Hubbee26a5842015-04-09 10:33:20 -0400262 return -EINVAL;
263 return ndev->reg->mw_bar[idx];
264}
265
266static 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)
269{
Dave Jiangfd839bf2015-06-15 08:22:30 -0400270 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
271 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
Allen Hubbee26a5842015-04-09 10:33:20 -0400272
273 if (db_addr) {
274 *db_addr = reg_addr + reg;
275 dev_dbg(ndev_dev(ndev), "Peer db addr %llx\n", *db_addr);
276 }
277
278 if (db_size) {
279 *db_size = ndev->reg->db_size;
280 dev_dbg(ndev_dev(ndev), "Peer db size %llx\n", *db_size);
281 }
282
283 return 0;
284}
285
286static inline u64 ndev_db_read(struct intel_ntb_dev *ndev,
287 void __iomem *mmio)
288{
Dave Jiangfd839bf2015-06-15 08:22:30 -0400289 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
290 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
Allen Hubbee26a5842015-04-09 10:33:20 -0400291
292 return ndev->reg->db_ioread(mmio);
293}
294
295static inline int ndev_db_write(struct intel_ntb_dev *ndev, u64 db_bits,
296 void __iomem *mmio)
297{
Dave Jiangfd839bf2015-06-15 08:22:30 -0400298 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
299 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
Allen Hubbee26a5842015-04-09 10:33:20 -0400300
301 if (db_bits & ~ndev->db_valid_mask)
302 return -EINVAL;
303
304 ndev->reg->db_iowrite(db_bits, mmio);
305
306 return 0;
307}
308
309static inline int ndev_db_set_mask(struct intel_ntb_dev *ndev, u64 db_bits,
310 void __iomem *mmio)
311{
312 unsigned long irqflags;
313
Dave Jiangfd839bf2015-06-15 08:22:30 -0400314 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
315 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
Allen Hubbee26a5842015-04-09 10:33:20 -0400316
317 if (db_bits & ~ndev->db_valid_mask)
318 return -EINVAL;
319
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);
326
327 return 0;
328}
329
330static inline int ndev_db_clear_mask(struct intel_ntb_dev *ndev, u64 db_bits,
331 void __iomem *mmio)
332{
333 unsigned long irqflags;
334
Dave Jiangfd839bf2015-06-15 08:22:30 -0400335 if (ndev_is_unsafe(ndev, NTB_UNSAFE_DB))
336 pr_warn_once("%s: NTB unsafe doorbell access", __func__);
Allen Hubbee26a5842015-04-09 10:33:20 -0400337
338 if (db_bits & ~ndev->db_valid_mask)
339 return -EINVAL;
340
341 spin_lock_irqsave(&ndev->db_mask_lock, irqflags);
342 {
343 ndev->db_mask &= ~db_bits;
344 ndev->reg->db_iowrite(ndev->db_mask, mmio);
345 }
346 spin_unlock_irqrestore(&ndev->db_mask_lock, irqflags);
347
348 return 0;
349}
350
351static inline int ndev_vec_mask(struct intel_ntb_dev *ndev, int db_vector)
352{
353 u64 shift, mask;
354
355 shift = ndev->db_vec_shift;
356 mask = BIT_ULL(shift) - 1;
357
358 return mask << (shift * db_vector);
359}
360
361static inline int ndev_spad_addr(struct intel_ntb_dev *ndev, int idx,
362 phys_addr_t *spad_addr, phys_addr_t reg_addr,
363 unsigned long reg)
364{
Dave Jiangfd839bf2015-06-15 08:22:30 -0400365 if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
366 pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
Allen Hubbee26a5842015-04-09 10:33:20 -0400367
368 if (idx < 0 || idx >= ndev->spad_count)
369 return -EINVAL;
370
371 if (spad_addr) {
372 *spad_addr = reg_addr + reg + (idx << 2);
373 dev_dbg(ndev_dev(ndev), "Peer spad addr %llx\n", *spad_addr);
374 }
375
376 return 0;
377}
378
379static inline u32 ndev_spad_read(struct intel_ntb_dev *ndev, int idx,
380 void __iomem *mmio)
381{
Dave Jiangfd839bf2015-06-15 08:22:30 -0400382 if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
383 pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
Allen Hubbee26a5842015-04-09 10:33:20 -0400384
385 if (idx < 0 || idx >= ndev->spad_count)
386 return 0;
387
388 return ioread32(mmio + (idx << 2));
389}
390
391static inline int ndev_spad_write(struct intel_ntb_dev *ndev, int idx, u32 val,
392 void __iomem *mmio)
393{
Dave Jiangfd839bf2015-06-15 08:22:30 -0400394 if (ndev_is_unsafe(ndev, NTB_UNSAFE_SPAD))
395 pr_warn_once("%s: NTB unsafe scratchpad access", __func__);
Allen Hubbee26a5842015-04-09 10:33:20 -0400396
397 if (idx < 0 || idx >= ndev->spad_count)
398 return -EINVAL;
399
400 iowrite32(val, mmio + (idx << 2));
401
402 return 0;
403}
404
405static irqreturn_t ndev_interrupt(struct intel_ntb_dev *ndev, int vec)
406{
407 u64 vec_mask;
408
409 vec_mask = ndev_vec_mask(ndev, vec);
410
Dave Jiang783dfa62016-11-16 14:03:38 -0700411 if ((ndev->hwerr_flags & NTB_HWERR_MSIX_VECTOR32_BAD) && (vec == 31))
412 vec_mask |= ndev->db_link_mask;
413
Allen Hubbee26a5842015-04-09 10:33:20 -0400414 dev_dbg(ndev_dev(ndev), "vec %d vec_mask %llx\n", vec, vec_mask);
415
416 ndev->last_ts = jiffies;
417
418 if (vec_mask & ndev->db_link_mask) {
419 if (ndev->reg->poll_link(ndev))
420 ntb_link_event(&ndev->ntb);
421 }
422
423 if (vec_mask & ndev->db_valid_mask)
424 ntb_db_event(&ndev->ntb, vec);
425
426 return IRQ_HANDLED;
427}
428
429static irqreturn_t ndev_vec_isr(int irq, void *dev)
430{
431 struct intel_ntb_vec *nvec = dev;
432
Dave Jiang783dfa62016-11-16 14:03:38 -0700433 dev_dbg(ndev_dev(nvec->ndev), "irq: %d nvec->num: %d\n",
434 irq, nvec->num);
435
Allen Hubbee26a5842015-04-09 10:33:20 -0400436 return ndev_interrupt(nvec->ndev, nvec->num);
437}
438
439static irqreturn_t ndev_irq_isr(int irq, void *dev)
440{
441 struct intel_ntb_dev *ndev = dev;
442
443 return ndev_interrupt(ndev, irq - ndev_pdev(ndev)->irq);
444}
445
446static int ndev_init_isr(struct intel_ntb_dev *ndev,
447 int msix_min, int msix_max,
448 int msix_shift, int total_shift)
449{
450 struct pci_dev *pdev;
Allen Hubbe0e041fb2015-05-19 12:04:52 -0400451 int rc, i, msix_count, node;
Allen Hubbee26a5842015-04-09 10:33:20 -0400452
453 pdev = ndev_pdev(ndev);
454
Allen Hubbe0e041fb2015-05-19 12:04:52 -0400455 node = dev_to_node(&pdev->dev);
456
Allen Hubbee26a5842015-04-09 10:33:20 -0400457 /* Mask all doorbell interrupts */
458 ndev->db_mask = ndev->db_valid_mask;
459 ndev->reg->db_iowrite(ndev->db_mask,
460 ndev->self_mmio +
461 ndev->self_reg->db_mask);
462
463 /* Try to set up msix irq */
464
Allen Hubbe0e041fb2015-05-19 12:04:52 -0400465 ndev->vec = kzalloc_node(msix_max * sizeof(*ndev->vec),
466 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -0400467 if (!ndev->vec)
468 goto err_msix_vec_alloc;
469
Allen Hubbe0e041fb2015-05-19 12:04:52 -0400470 ndev->msix = kzalloc_node(msix_max * sizeof(*ndev->msix),
471 GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -0400472 if (!ndev->msix)
473 goto err_msix_alloc;
474
475 for (i = 0; i < msix_max; ++i)
476 ndev->msix[i].entry = i;
477
478 msix_count = pci_enable_msix_range(pdev, ndev->msix,
479 msix_min, msix_max);
480 if (msix_count < 0)
481 goto err_msix_enable;
482
483 for (i = 0; i < msix_count; ++i) {
484 ndev->vec[i].ndev = ndev;
485 ndev->vec[i].num = i;
486 rc = request_irq(ndev->msix[i].vector, ndev_vec_isr, 0,
487 "ndev_vec_isr", &ndev->vec[i]);
488 if (rc)
489 goto err_msix_request;
490 }
491
Dave Jiang783dfa62016-11-16 14:03:38 -0700492 dev_dbg(ndev_dev(ndev), "Using %d msix interrupts\n", msix_count);
Allen Hubbee26a5842015-04-09 10:33:20 -0400493 ndev->db_vec_count = msix_count;
494 ndev->db_vec_shift = msix_shift;
495 return 0;
496
497err_msix_request:
498 while (i-- > 0)
Christophe JAILLET28734e82016-12-19 06:52:55 +0100499 free_irq(ndev->msix[i].vector, &ndev->vec[i]);
Allen Hubbee26a5842015-04-09 10:33:20 -0400500 pci_disable_msix(pdev);
501err_msix_enable:
502 kfree(ndev->msix);
503err_msix_alloc:
504 kfree(ndev->vec);
505err_msix_vec_alloc:
506 ndev->msix = NULL;
507 ndev->vec = NULL;
508
509 /* Try to set up msi irq */
510
511 rc = pci_enable_msi(pdev);
512 if (rc)
513 goto err_msi_enable;
514
515 rc = request_irq(pdev->irq, ndev_irq_isr, 0,
516 "ndev_irq_isr", ndev);
517 if (rc)
518 goto err_msi_request;
519
520 dev_dbg(ndev_dev(ndev), "Using msi interrupts\n");
521 ndev->db_vec_count = 1;
522 ndev->db_vec_shift = total_shift;
523 return 0;
524
525err_msi_request:
526 pci_disable_msi(pdev);
527err_msi_enable:
528
529 /* Try to set up intx irq */
530
531 pci_intx(pdev, 1);
532
533 rc = request_irq(pdev->irq, ndev_irq_isr, IRQF_SHARED,
534 "ndev_irq_isr", ndev);
535 if (rc)
536 goto err_intx_request;
537
538 dev_dbg(ndev_dev(ndev), "Using intx interrupts\n");
539 ndev->db_vec_count = 1;
540 ndev->db_vec_shift = total_shift;
541 return 0;
542
543err_intx_request:
544 return rc;
545}
546
547static void ndev_deinit_isr(struct intel_ntb_dev *ndev)
548{
549 struct pci_dev *pdev;
550 int i;
551
552 pdev = ndev_pdev(ndev);
553
554 /* Mask all doorbell interrupts */
555 ndev->db_mask = ndev->db_valid_mask;
556 ndev->reg->db_iowrite(ndev->db_mask,
557 ndev->self_mmio +
558 ndev->self_reg->db_mask);
559
560 if (ndev->msix) {
561 i = ndev->db_vec_count;
562 while (i--)
563 free_irq(ndev->msix[i].vector, &ndev->vec[i]);
564 pci_disable_msix(pdev);
565 kfree(ndev->msix);
566 kfree(ndev->vec);
567 } else {
568 free_irq(pdev->irq, ndev);
569 if (pci_dev_msi_enabled(pdev))
570 pci_disable_msi(pdev);
571 }
572}
573
Dave Jiang783dfa62016-11-16 14:03:38 -0700574static ssize_t ndev_ntb3_debugfs_read(struct file *filp, char __user *ubuf,
575 size_t count, loff_t *offp)
576{
577 struct intel_ntb_dev *ndev;
578 void __iomem *mmio;
579 char *buf;
580 size_t buf_size;
581 ssize_t ret, off;
582 union { u64 v64; u32 v32; u16 v16; } u;
583
584 ndev = filp->private_data;
585 mmio = ndev->self_mmio;
586
587 buf_size = min(count, 0x800ul);
588
589 buf = kmalloc(buf_size, GFP_KERNEL);
590 if (!buf)
591 return -ENOMEM;
592
593 off = 0;
594
595 off += scnprintf(buf + off, buf_size - off,
596 "NTB Device Information:\n");
597
598 off += scnprintf(buf + off, buf_size - off,
599 "Connection Topology -\t%s\n",
600 ntb_topo_string(ndev->ntb.topo));
601
602 off += scnprintf(buf + off, buf_size - off,
603 "NTB CTL -\t\t%#06x\n", ndev->ntb_ctl);
604 off += scnprintf(buf + off, buf_size - off,
605 "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
606
607 if (!ndev->reg->link_is_up(ndev))
608 off += scnprintf(buf + off, buf_size - off,
609 "Link Status -\t\tDown\n");
610 else {
611 off += scnprintf(buf + off, buf_size - off,
612 "Link Status -\t\tUp\n");
613 off += scnprintf(buf + off, buf_size - off,
614 "Link Speed -\t\tPCI-E Gen %u\n",
615 NTB_LNK_STA_SPEED(ndev->lnk_sta));
616 off += scnprintf(buf + off, buf_size - off,
617 "Link Width -\t\tx%u\n",
618 NTB_LNK_STA_WIDTH(ndev->lnk_sta));
619 }
620
621 off += scnprintf(buf + off, buf_size - off,
622 "Memory Window Count -\t%u\n", ndev->mw_count);
623 off += scnprintf(buf + off, buf_size - off,
624 "Scratchpad Count -\t%u\n", ndev->spad_count);
625 off += scnprintf(buf + off, buf_size - off,
626 "Doorbell Count -\t%u\n", ndev->db_count);
627 off += scnprintf(buf + off, buf_size - off,
628 "Doorbell Vector Count -\t%u\n", ndev->db_vec_count);
629 off += scnprintf(buf + off, buf_size - off,
630 "Doorbell Vector Shift -\t%u\n", ndev->db_vec_shift);
631
632 off += scnprintf(buf + off, buf_size - off,
633 "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
634 off += scnprintf(buf + off, buf_size - off,
635 "Doorbell Link Mask -\t%#llx\n", ndev->db_link_mask);
636 off += scnprintf(buf + off, buf_size - off,
637 "Doorbell Mask Cached -\t%#llx\n", ndev->db_mask);
638
639 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_mask);
640 off += scnprintf(buf + off, buf_size - off,
641 "Doorbell Mask -\t\t%#llx\n", u.v64);
642
643 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_bell);
644 off += scnprintf(buf + off, buf_size - off,
645 "Doorbell Bell -\t\t%#llx\n", u.v64);
646
647 off += scnprintf(buf + off, buf_size - off,
648 "\nNTB Incoming XLAT:\n");
649
650 u.v64 = ioread64(mmio + SKX_IMBAR1XBASE_OFFSET);
651 off += scnprintf(buf + off, buf_size - off,
652 "IMBAR1XBASE -\t\t%#018llx\n", u.v64);
653
654 u.v64 = ioread64(mmio + SKX_IMBAR2XBASE_OFFSET);
655 off += scnprintf(buf + off, buf_size - off,
656 "IMBAR2XBASE -\t\t%#018llx\n", u.v64);
657
658 u.v64 = ioread64(mmio + SKX_IMBAR1XLMT_OFFSET);
659 off += scnprintf(buf + off, buf_size - off,
660 "IMBAR1XLMT -\t\t\t%#018llx\n", u.v64);
661
662 u.v64 = ioread64(mmio + SKX_IMBAR2XLMT_OFFSET);
663 off += scnprintf(buf + off, buf_size - off,
664 "IMBAR2XLMT -\t\t\t%#018llx\n", u.v64);
665
666 if (ntb_topo_is_b2b(ndev->ntb.topo)) {
667 off += scnprintf(buf + off, buf_size - off,
668 "\nNTB Outgoing B2B XLAT:\n");
669
670 u.v64 = ioread64(mmio + SKX_EMBAR1XBASE_OFFSET);
671 off += scnprintf(buf + off, buf_size - off,
672 "EMBAR1XBASE -\t\t%#018llx\n", u.v64);
673
674 u.v64 = ioread64(mmio + SKX_EMBAR2XBASE_OFFSET);
675 off += scnprintf(buf + off, buf_size - off,
676 "EMBAR2XBASE -\t\t%#018llx\n", u.v64);
677
678 u.v64 = ioread64(mmio + SKX_EMBAR1XLMT_OFFSET);
679 off += scnprintf(buf + off, buf_size - off,
680 "EMBAR1XLMT -\t\t%#018llx\n", u.v64);
681
682 u.v64 = ioread64(mmio + SKX_EMBAR2XLMT_OFFSET);
683 off += scnprintf(buf + off, buf_size - off,
684 "EMBAR2XLMT -\t\t%#018llx\n", u.v64);
685
686 off += scnprintf(buf + off, buf_size - off,
687 "\nNTB Secondary BAR:\n");
688
689 u.v64 = ioread64(mmio + SKX_EMBAR0_OFFSET);
690 off += scnprintf(buf + off, buf_size - off,
691 "EMBAR0 -\t\t%#018llx\n", u.v64);
692
693 u.v64 = ioread64(mmio + SKX_EMBAR1_OFFSET);
694 off += scnprintf(buf + off, buf_size - off,
695 "EMBAR1 -\t\t%#018llx\n", u.v64);
696
697 u.v64 = ioread64(mmio + SKX_EMBAR2_OFFSET);
698 off += scnprintf(buf + off, buf_size - off,
699 "EMBAR2 -\t\t%#018llx\n", u.v64);
700 }
701
702 off += scnprintf(buf + off, buf_size - off,
703 "\nNTB Statistics:\n");
704
705 u.v16 = ioread16(mmio + SKX_USMEMMISS_OFFSET);
706 off += scnprintf(buf + off, buf_size - off,
707 "Upstream Memory Miss -\t%u\n", u.v16);
708
709 off += scnprintf(buf + off, buf_size - off,
710 "\nNTB Hardware Errors:\n");
711
712 if (!pci_read_config_word(ndev->ntb.pdev,
713 SKX_DEVSTS_OFFSET, &u.v16))
714 off += scnprintf(buf + off, buf_size - off,
715 "DEVSTS -\t\t%#06x\n", u.v16);
716
717 if (!pci_read_config_word(ndev->ntb.pdev,
718 SKX_LINK_STATUS_OFFSET, &u.v16))
719 off += scnprintf(buf + off, buf_size - off,
720 "LNKSTS -\t\t%#06x\n", u.v16);
721
722 if (!pci_read_config_dword(ndev->ntb.pdev,
723 SKX_UNCERRSTS_OFFSET, &u.v32))
724 off += scnprintf(buf + off, buf_size - off,
725 "UNCERRSTS -\t\t%#06x\n", u.v32);
726
727 if (!pci_read_config_dword(ndev->ntb.pdev,
728 SKX_CORERRSTS_OFFSET, &u.v32))
729 off += scnprintf(buf + off, buf_size - off,
730 "CORERRSTS -\t\t%#06x\n", u.v32);
731
732 ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
733 kfree(buf);
734 return ret;
735}
736
737static ssize_t ndev_ntb_debugfs_read(struct file *filp, char __user *ubuf,
738 size_t count, loff_t *offp)
Allen Hubbee26a5842015-04-09 10:33:20 -0400739{
740 struct intel_ntb_dev *ndev;
Allen Hubbe40895272016-07-22 09:38:22 -0400741 struct pci_dev *pdev;
Allen Hubbee26a5842015-04-09 10:33:20 -0400742 void __iomem *mmio;
743 char *buf;
744 size_t buf_size;
745 ssize_t ret, off;
Allen Hubbe40895272016-07-22 09:38:22 -0400746 union { u64 v64; u32 v32; u16 v16; u8 v8; } u;
Allen Hubbee26a5842015-04-09 10:33:20 -0400747
748 ndev = filp->private_data;
Allen Hubbe40895272016-07-22 09:38:22 -0400749 pdev = ndev_pdev(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -0400750 mmio = ndev->self_mmio;
751
752 buf_size = min(count, 0x800ul);
753
754 buf = kmalloc(buf_size, GFP_KERNEL);
755 if (!buf)
756 return -ENOMEM;
757
758 off = 0;
759
760 off += scnprintf(buf + off, buf_size - off,
761 "NTB Device Information:\n");
762
763 off += scnprintf(buf + off, buf_size - off,
764 "Connection Topology -\t%s\n",
765 ntb_topo_string(ndev->ntb.topo));
766
Allen Hubbe2aa2a77a2015-08-31 09:30:59 -0400767 if (ndev->b2b_idx != UINT_MAX) {
768 off += scnprintf(buf + off, buf_size - off,
769 "B2B MW Idx -\t\t%u\n", ndev->b2b_idx);
770 off += scnprintf(buf + off, buf_size - off,
771 "B2B Offset -\t\t%#lx\n", ndev->b2b_off);
772 }
773
Allen Hubbee26a5842015-04-09 10:33:20 -0400774 off += scnprintf(buf + off, buf_size - off,
775 "BAR4 Split -\t\t%s\n",
776 ndev->bar4_split ? "yes" : "no");
777
778 off += scnprintf(buf + off, buf_size - off,
779 "NTB CTL -\t\t%#06x\n", ndev->ntb_ctl);
780 off += scnprintf(buf + off, buf_size - off,
781 "LNK STA -\t\t%#06x\n", ndev->lnk_sta);
782
783 if (!ndev->reg->link_is_up(ndev)) {
784 off += scnprintf(buf + off, buf_size - off,
785 "Link Status -\t\tDown\n");
786 } else {
787 off += scnprintf(buf + off, buf_size - off,
788 "Link Status -\t\tUp\n");
789 off += scnprintf(buf + off, buf_size - off,
790 "Link Speed -\t\tPCI-E Gen %u\n",
791 NTB_LNK_STA_SPEED(ndev->lnk_sta));
792 off += scnprintf(buf + off, buf_size - off,
793 "Link Width -\t\tx%u\n",
794 NTB_LNK_STA_WIDTH(ndev->lnk_sta));
795 }
796
797 off += scnprintf(buf + off, buf_size - off,
798 "Memory Window Count -\t%u\n", ndev->mw_count);
799 off += scnprintf(buf + off, buf_size - off,
800 "Scratchpad Count -\t%u\n", ndev->spad_count);
801 off += scnprintf(buf + off, buf_size - off,
802 "Doorbell Count -\t%u\n", ndev->db_count);
803 off += scnprintf(buf + off, buf_size - off,
804 "Doorbell Vector Count -\t%u\n", ndev->db_vec_count);
805 off += scnprintf(buf + off, buf_size - off,
806 "Doorbell Vector Shift -\t%u\n", ndev->db_vec_shift);
807
808 off += scnprintf(buf + off, buf_size - off,
809 "Doorbell Valid Mask -\t%#llx\n", ndev->db_valid_mask);
810 off += scnprintf(buf + off, buf_size - off,
811 "Doorbell Link Mask -\t%#llx\n", ndev->db_link_mask);
812 off += scnprintf(buf + off, buf_size - off,
813 "Doorbell Mask Cached -\t%#llx\n", ndev->db_mask);
814
815 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_mask);
816 off += scnprintf(buf + off, buf_size - off,
817 "Doorbell Mask -\t\t%#llx\n", u.v64);
818
819 u.v64 = ndev_db_read(ndev, mmio + ndev->self_reg->db_bell);
820 off += scnprintf(buf + off, buf_size - off,
821 "Doorbell Bell -\t\t%#llx\n", u.v64);
822
823 off += scnprintf(buf + off, buf_size - off,
Allen Hubbe40895272016-07-22 09:38:22 -0400824 "\nNTB Window Size:\n");
825
826 pci_read_config_byte(pdev, XEON_PBAR23SZ_OFFSET, &u.v8);
827 off += scnprintf(buf + off, buf_size - off,
828 "PBAR23SZ %hhu\n", u.v8);
829 if (!ndev->bar4_split) {
830 pci_read_config_byte(pdev, XEON_PBAR45SZ_OFFSET, &u.v8);
831 off += scnprintf(buf + off, buf_size - off,
832 "PBAR45SZ %hhu\n", u.v8);
833 } else {
834 pci_read_config_byte(pdev, XEON_PBAR4SZ_OFFSET, &u.v8);
835 off += scnprintf(buf + off, buf_size - off,
836 "PBAR4SZ %hhu\n", u.v8);
837 pci_read_config_byte(pdev, XEON_PBAR5SZ_OFFSET, &u.v8);
838 off += scnprintf(buf + off, buf_size - off,
839 "PBAR5SZ %hhu\n", u.v8);
840 }
841
842 pci_read_config_byte(pdev, XEON_SBAR23SZ_OFFSET, &u.v8);
843 off += scnprintf(buf + off, buf_size - off,
844 "SBAR23SZ %hhu\n", u.v8);
845 if (!ndev->bar4_split) {
846 pci_read_config_byte(pdev, XEON_SBAR45SZ_OFFSET, &u.v8);
847 off += scnprintf(buf + off, buf_size - off,
848 "SBAR45SZ %hhu\n", u.v8);
849 } else {
850 pci_read_config_byte(pdev, XEON_SBAR4SZ_OFFSET, &u.v8);
851 off += scnprintf(buf + off, buf_size - off,
852 "SBAR4SZ %hhu\n", u.v8);
853 pci_read_config_byte(pdev, XEON_SBAR5SZ_OFFSET, &u.v8);
854 off += scnprintf(buf + off, buf_size - off,
855 "SBAR5SZ %hhu\n", u.v8);
856 }
857
858 off += scnprintf(buf + off, buf_size - off,
Allen Hubbee26a5842015-04-09 10:33:20 -0400859 "\nNTB Incoming XLAT:\n");
860
861 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 2));
862 off += scnprintf(buf + off, buf_size - off,
863 "XLAT23 -\t\t%#018llx\n", u.v64);
864
Dave Jiangbf44fe42015-06-18 05:17:30 -0400865 if (ndev->bar4_split) {
866 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
867 off += scnprintf(buf + off, buf_size - off,
868 "XLAT4 -\t\t\t%#06x\n", u.v32);
869
870 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 5));
871 off += scnprintf(buf + off, buf_size - off,
872 "XLAT5 -\t\t\t%#06x\n", u.v32);
873 } else {
874 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_xlat, 4));
875 off += scnprintf(buf + off, buf_size - off,
876 "XLAT45 -\t\t%#018llx\n", u.v64);
877 }
Allen Hubbee26a5842015-04-09 10:33:20 -0400878
879 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 2));
880 off += scnprintf(buf + off, buf_size - off,
881 "LMT23 -\t\t\t%#018llx\n", u.v64);
882
Dave Jiangbf44fe42015-06-18 05:17:30 -0400883 if (ndev->bar4_split) {
884 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
885 off += scnprintf(buf + off, buf_size - off,
886 "LMT4 -\t\t\t%#06x\n", u.v32);
887 u.v32 = ioread32(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 5));
888 off += scnprintf(buf + off, buf_size - off,
889 "LMT5 -\t\t\t%#06x\n", u.v32);
890 } else {
891 u.v64 = ioread64(mmio + bar2_off(ndev->xlat_reg->bar2_limit, 4));
892 off += scnprintf(buf + off, buf_size - off,
893 "LMT45 -\t\t\t%#018llx\n", u.v64);
894 }
Allen Hubbee26a5842015-04-09 10:33:20 -0400895
Allen Hubbe95f14642016-07-22 09:38:23 -0400896 if (pdev_is_xeon(pdev)) {
Allen Hubbee26a5842015-04-09 10:33:20 -0400897 if (ntb_topo_is_b2b(ndev->ntb.topo)) {
898 off += scnprintf(buf + off, buf_size - off,
899 "\nNTB Outgoing B2B XLAT:\n");
900
Dave Jiang2f887b92015-05-20 12:55:47 -0400901 u.v64 = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -0400902 off += scnprintf(buf + off, buf_size - off,
903 "B2B XLAT23 -\t\t%#018llx\n", u.v64);
904
Dave Jiangbf44fe42015-06-18 05:17:30 -0400905 if (ndev->bar4_split) {
906 u.v32 = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
907 off += scnprintf(buf + off, buf_size - off,
908 "B2B XLAT4 -\t\t%#06x\n",
909 u.v32);
910 u.v32 = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
911 off += scnprintf(buf + off, buf_size - off,
912 "B2B XLAT5 -\t\t%#06x\n",
913 u.v32);
914 } else {
915 u.v64 = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
916 off += scnprintf(buf + off, buf_size - off,
917 "B2B XLAT45 -\t\t%#018llx\n",
918 u.v64);
919 }
Allen Hubbee26a5842015-04-09 10:33:20 -0400920
Dave Jiang2f887b92015-05-20 12:55:47 -0400921 u.v64 = ioread64(mmio + XEON_PBAR23LMT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -0400922 off += scnprintf(buf + off, buf_size - off,
923 "B2B LMT23 -\t\t%#018llx\n", u.v64);
924
Dave Jiangbf44fe42015-06-18 05:17:30 -0400925 if (ndev->bar4_split) {
926 u.v32 = ioread32(mmio + XEON_PBAR4LMT_OFFSET);
927 off += scnprintf(buf + off, buf_size - off,
928 "B2B LMT4 -\t\t%#06x\n",
929 u.v32);
930 u.v32 = ioread32(mmio + XEON_PBAR5LMT_OFFSET);
931 off += scnprintf(buf + off, buf_size - off,
932 "B2B LMT5 -\t\t%#06x\n",
933 u.v32);
934 } else {
935 u.v64 = ioread64(mmio + XEON_PBAR45LMT_OFFSET);
936 off += scnprintf(buf + off, buf_size - off,
937 "B2B LMT45 -\t\t%#018llx\n",
938 u.v64);
939 }
Allen Hubbee26a5842015-04-09 10:33:20 -0400940
941 off += scnprintf(buf + off, buf_size - off,
942 "\nNTB Secondary BAR:\n");
943
Dave Jiang2f887b92015-05-20 12:55:47 -0400944 u.v64 = ioread64(mmio + XEON_SBAR0BASE_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -0400945 off += scnprintf(buf + off, buf_size - off,
946 "SBAR01 -\t\t%#018llx\n", u.v64);
947
Dave Jiang2f887b92015-05-20 12:55:47 -0400948 u.v64 = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -0400949 off += scnprintf(buf + off, buf_size - off,
950 "SBAR23 -\t\t%#018llx\n", u.v64);
951
Dave Jiangbf44fe42015-06-18 05:17:30 -0400952 if (ndev->bar4_split) {
953 u.v32 = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
954 off += scnprintf(buf + off, buf_size - off,
955 "SBAR4 -\t\t\t%#06x\n", u.v32);
956 u.v32 = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
957 off += scnprintf(buf + off, buf_size - off,
958 "SBAR5 -\t\t\t%#06x\n", u.v32);
959 } else {
960 u.v64 = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
961 off += scnprintf(buf + off, buf_size - off,
962 "SBAR45 -\t\t%#018llx\n",
963 u.v64);
964 }
Allen Hubbee26a5842015-04-09 10:33:20 -0400965 }
966
967 off += scnprintf(buf + off, buf_size - off,
Dave Jiang2f887b92015-05-20 12:55:47 -0400968 "\nXEON NTB Statistics:\n");
Allen Hubbee26a5842015-04-09 10:33:20 -0400969
Dave Jiang2f887b92015-05-20 12:55:47 -0400970 u.v16 = ioread16(mmio + XEON_USMEMMISS_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -0400971 off += scnprintf(buf + off, buf_size - off,
972 "Upstream Memory Miss -\t%u\n", u.v16);
973
974 off += scnprintf(buf + off, buf_size - off,
Dave Jiang2f887b92015-05-20 12:55:47 -0400975 "\nXEON NTB Hardware Errors:\n");
Allen Hubbee26a5842015-04-09 10:33:20 -0400976
Allen Hubbe95f14642016-07-22 09:38:23 -0400977 if (!pci_read_config_word(pdev,
Dave Jiang2f887b92015-05-20 12:55:47 -0400978 XEON_DEVSTS_OFFSET, &u.v16))
Allen Hubbee26a5842015-04-09 10:33:20 -0400979 off += scnprintf(buf + off, buf_size - off,
980 "DEVSTS -\t\t%#06x\n", u.v16);
981
Allen Hubbe95f14642016-07-22 09:38:23 -0400982 if (!pci_read_config_word(pdev,
Dave Jiang2f887b92015-05-20 12:55:47 -0400983 XEON_LINK_STATUS_OFFSET, &u.v16))
Allen Hubbee26a5842015-04-09 10:33:20 -0400984 off += scnprintf(buf + off, buf_size - off,
985 "LNKSTS -\t\t%#06x\n", u.v16);
986
Allen Hubbe95f14642016-07-22 09:38:23 -0400987 if (!pci_read_config_dword(pdev,
Dave Jiang2f887b92015-05-20 12:55:47 -0400988 XEON_UNCERRSTS_OFFSET, &u.v32))
Allen Hubbee26a5842015-04-09 10:33:20 -0400989 off += scnprintf(buf + off, buf_size - off,
990 "UNCERRSTS -\t\t%#06x\n", u.v32);
991
Allen Hubbe95f14642016-07-22 09:38:23 -0400992 if (!pci_read_config_dword(pdev,
Dave Jiang2f887b92015-05-20 12:55:47 -0400993 XEON_CORERRSTS_OFFSET, &u.v32))
Allen Hubbee26a5842015-04-09 10:33:20 -0400994 off += scnprintf(buf + off, buf_size - off,
995 "CORERRSTS -\t\t%#06x\n", u.v32);
996 }
997
998 ret = simple_read_from_buffer(ubuf, count, offp, buf, off);
999 kfree(buf);
1000 return ret;
1001}
1002
Dave Jiang783dfa62016-11-16 14:03:38 -07001003static ssize_t ndev_debugfs_read(struct file *filp, char __user *ubuf,
1004 size_t count, loff_t *offp)
1005{
1006 struct intel_ntb_dev *ndev = filp->private_data;
1007
1008 if (pdev_is_xeon(ndev->ntb.pdev) ||
1009 pdev_is_atom(ndev->ntb.pdev))
1010 return ndev_ntb_debugfs_read(filp, ubuf, count, offp);
1011 else if (pdev_is_skx_xeon(ndev->ntb.pdev))
1012 return ndev_ntb3_debugfs_read(filp, ubuf, count, offp);
1013
1014 return -ENXIO;
1015}
1016
Allen Hubbee26a5842015-04-09 10:33:20 -04001017static void ndev_init_debugfs(struct intel_ntb_dev *ndev)
1018{
1019 if (!debugfs_dir) {
1020 ndev->debugfs_dir = NULL;
1021 ndev->debugfs_info = NULL;
1022 } else {
1023 ndev->debugfs_dir =
1024 debugfs_create_dir(ndev_name(ndev), debugfs_dir);
1025 if (!ndev->debugfs_dir)
1026 ndev->debugfs_info = NULL;
1027 else
1028 ndev->debugfs_info =
1029 debugfs_create_file("info", S_IRUSR,
1030 ndev->debugfs_dir, ndev,
1031 &intel_ntb_debugfs_info);
1032 }
1033}
1034
1035static void ndev_deinit_debugfs(struct intel_ntb_dev *ndev)
1036{
1037 debugfs_remove_recursive(ndev->debugfs_dir);
1038}
1039
Serge Semin443b9a12017-01-11 03:11:33 +03001040static int intel_ntb_mw_count(struct ntb_dev *ntb, int pidx)
Allen Hubbee26a5842015-04-09 10:33:20 -04001041{
Serge Semin443b9a12017-01-11 03:11:33 +03001042 if (pidx != NTB_DEF_PEER_IDX)
1043 return -EINVAL;
1044
Allen Hubbee26a5842015-04-09 10:33:20 -04001045 return ntb_ndev(ntb)->mw_count;
1046}
1047
Serge Semin443b9a12017-01-11 03:11:33 +03001048static int intel_ntb_mw_get_align(struct ntb_dev *ntb, int pidx, int idx,
1049 resource_size_t *addr_align,
1050 resource_size_t *size_align,
1051 resource_size_t *size_max)
Allen Hubbee26a5842015-04-09 10:33:20 -04001052{
1053 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
Serge Semin443b9a12017-01-11 03:11:33 +03001054 resource_size_t bar_size, mw_size;
Allen Hubbee26a5842015-04-09 10:33:20 -04001055 int bar;
1056
Serge Semin443b9a12017-01-11 03:11:33 +03001057 if (pidx != NTB_DEF_PEER_IDX)
1058 return -EINVAL;
1059
Allen Hubbee26a5842015-04-09 10:33:20 -04001060 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
1061 idx += 1;
1062
1063 bar = ndev_mw_to_bar(ndev, idx);
1064 if (bar < 0)
1065 return bar;
1066
Serge Semin443b9a12017-01-11 03:11:33 +03001067 bar_size = pci_resource_len(ndev->ntb.pdev, bar);
Allen Hubbee26a5842015-04-09 10:33:20 -04001068
Serge Semin443b9a12017-01-11 03:11:33 +03001069 if (idx == ndev->b2b_idx)
1070 mw_size = bar_size - ndev->b2b_off;
1071 else
1072 mw_size = bar_size;
Allen Hubbee26a5842015-04-09 10:33:20 -04001073
Serge Semin443b9a12017-01-11 03:11:33 +03001074 if (addr_align)
1075 *addr_align = pci_resource_len(ndev->ntb.pdev, bar);
Allen Hubbee26a5842015-04-09 10:33:20 -04001076
Serge Semin443b9a12017-01-11 03:11:33 +03001077 if (size_align)
1078 *size_align = 1;
1079
1080 if (size_max)
1081 *size_max = mw_size;
Allen Hubbee26a5842015-04-09 10:33:20 -04001082
1083 return 0;
1084}
1085
Serge Semin443b9a12017-01-11 03:11:33 +03001086static int intel_ntb_mw_set_trans(struct ntb_dev *ntb, int pidx, int idx,
Allen Hubbee26a5842015-04-09 10:33:20 -04001087 dma_addr_t addr, resource_size_t size)
1088{
1089 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1090 unsigned long base_reg, xlat_reg, limit_reg;
1091 resource_size_t bar_size, mw_size;
1092 void __iomem *mmio;
1093 u64 base, limit, reg_val;
1094 int bar;
1095
Serge Semin443b9a12017-01-11 03:11:33 +03001096 if (pidx != NTB_DEF_PEER_IDX)
1097 return -EINVAL;
1098
Allen Hubbee26a5842015-04-09 10:33:20 -04001099 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
1100 idx += 1;
1101
1102 bar = ndev_mw_to_bar(ndev, idx);
1103 if (bar < 0)
1104 return bar;
1105
1106 bar_size = pci_resource_len(ndev->ntb.pdev, bar);
1107
1108 if (idx == ndev->b2b_idx)
1109 mw_size = bar_size - ndev->b2b_off;
1110 else
1111 mw_size = bar_size;
1112
1113 /* hardware requires that addr is aligned to bar size */
1114 if (addr & (bar_size - 1))
1115 return -EINVAL;
1116
1117 /* make sure the range fits in the usable mw size */
1118 if (size > mw_size)
1119 return -EINVAL;
1120
1121 mmio = ndev->self_mmio;
1122 base_reg = bar0_off(ndev->xlat_reg->bar0_base, bar);
1123 xlat_reg = bar2_off(ndev->xlat_reg->bar2_xlat, bar);
1124 limit_reg = bar2_off(ndev->xlat_reg->bar2_limit, bar);
1125
1126 if (bar < 4 || !ndev->bar4_split) {
Dave Jiang703872c2015-11-19 14:00:54 -07001127 base = ioread64(mmio + base_reg) & NTB_BAR_MASK_64;
Allen Hubbee26a5842015-04-09 10:33:20 -04001128
1129 /* Set the limit if supported, if size is not mw_size */
1130 if (limit_reg && size != mw_size)
1131 limit = base + size;
1132 else
1133 limit = 0;
1134
1135 /* set and verify setting the translation address */
1136 iowrite64(addr, mmio + xlat_reg);
1137 reg_val = ioread64(mmio + xlat_reg);
1138 if (reg_val != addr) {
1139 iowrite64(0, mmio + xlat_reg);
1140 return -EIO;
1141 }
1142
1143 /* set and verify setting the limit */
1144 iowrite64(limit, mmio + limit_reg);
1145 reg_val = ioread64(mmio + limit_reg);
1146 if (reg_val != limit) {
1147 iowrite64(base, mmio + limit_reg);
1148 iowrite64(0, mmio + xlat_reg);
1149 return -EIO;
1150 }
1151 } else {
1152 /* split bar addr range must all be 32 bit */
1153 if (addr & (~0ull << 32))
1154 return -EINVAL;
1155 if ((addr + size) & (~0ull << 32))
1156 return -EINVAL;
1157
Dave Jiang703872c2015-11-19 14:00:54 -07001158 base = ioread32(mmio + base_reg) & NTB_BAR_MASK_32;
Allen Hubbee26a5842015-04-09 10:33:20 -04001159
1160 /* Set the limit if supported, if size is not mw_size */
1161 if (limit_reg && size != mw_size)
1162 limit = base + size;
1163 else
1164 limit = 0;
1165
1166 /* set and verify setting the translation address */
1167 iowrite32(addr, mmio + xlat_reg);
1168 reg_val = ioread32(mmio + xlat_reg);
1169 if (reg_val != addr) {
1170 iowrite32(0, mmio + xlat_reg);
1171 return -EIO;
1172 }
1173
1174 /* set and verify setting the limit */
1175 iowrite32(limit, mmio + limit_reg);
1176 reg_val = ioread32(mmio + limit_reg);
1177 if (reg_val != limit) {
1178 iowrite32(base, mmio + limit_reg);
1179 iowrite32(0, mmio + xlat_reg);
1180 return -EIO;
1181 }
1182 }
1183
1184 return 0;
1185}
1186
Serge Semin4e8c11b2016-12-14 02:49:15 +03001187static u64 intel_ntb_link_is_up(struct ntb_dev *ntb,
Allen Hubbee26a5842015-04-09 10:33:20 -04001188 enum ntb_speed *speed,
1189 enum ntb_width *width)
1190{
1191 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1192
1193 if (ndev->reg->link_is_up(ndev)) {
1194 if (speed)
1195 *speed = NTB_LNK_STA_SPEED(ndev->lnk_sta);
1196 if (width)
1197 *width = NTB_LNK_STA_WIDTH(ndev->lnk_sta);
1198 return 1;
1199 } else {
1200 /* TODO MAYBE: is it possible to observe the link speed and
1201 * width while link is training? */
1202 if (speed)
1203 *speed = NTB_SPEED_NONE;
1204 if (width)
1205 *width = NTB_WIDTH_NONE;
1206 return 0;
1207 }
1208}
1209
1210static int intel_ntb_link_enable(struct ntb_dev *ntb,
1211 enum ntb_speed max_speed,
1212 enum ntb_width max_width)
1213{
1214 struct intel_ntb_dev *ndev;
1215 u32 ntb_ctl;
1216
1217 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1218
1219 if (ndev->ntb.topo == NTB_TOPO_SEC)
1220 return -EINVAL;
1221
1222 dev_dbg(ndev_dev(ndev),
1223 "Enabling link with max_speed %d max_width %d\n",
1224 max_speed, max_width);
1225 if (max_speed != NTB_SPEED_AUTO)
1226 dev_dbg(ndev_dev(ndev), "ignoring max_speed %d\n", max_speed);
1227 if (max_width != NTB_WIDTH_AUTO)
1228 dev_dbg(ndev_dev(ndev), "ignoring max_width %d\n", max_width);
1229
1230 ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
1231 ntb_ctl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
1232 ntb_ctl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
1233 ntb_ctl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
1234 if (ndev->bar4_split)
1235 ntb_ctl |= NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP;
1236 iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
1237
1238 return 0;
1239}
1240
1241static int intel_ntb_link_disable(struct ntb_dev *ntb)
1242{
1243 struct intel_ntb_dev *ndev;
1244 u32 ntb_cntl;
1245
1246 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1247
1248 if (ndev->ntb.topo == NTB_TOPO_SEC)
1249 return -EINVAL;
1250
1251 dev_dbg(ndev_dev(ndev), "Disabling link\n");
1252
1253 /* Bring NTB link down */
1254 ntb_cntl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
1255 ntb_cntl &= ~(NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP);
1256 ntb_cntl &= ~(NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP);
1257 if (ndev->bar4_split)
1258 ntb_cntl &= ~(NTB_CTL_P2S_BAR5_SNOOP | NTB_CTL_S2P_BAR5_SNOOP);
1259 ntb_cntl |= NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK;
1260 iowrite32(ntb_cntl, ndev->self_mmio + ndev->reg->ntb_ctl);
1261
1262 return 0;
1263}
1264
Serge Semin443b9a12017-01-11 03:11:33 +03001265static int intel_ntb_peer_mw_count(struct ntb_dev *ntb)
1266{
1267 /* Numbers of inbound and outbound memory windows match */
1268 return ntb_ndev(ntb)->mw_count;
1269}
1270
1271static int intel_ntb_peer_mw_get_addr(struct ntb_dev *ntb, int idx,
1272 phys_addr_t *base, resource_size_t *size)
1273{
1274 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1275 int bar;
1276
1277 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
1278 idx += 1;
1279
1280 bar = ndev_mw_to_bar(ndev, idx);
1281 if (bar < 0)
1282 return bar;
1283
1284 if (base)
1285 *base = pci_resource_start(ndev->ntb.pdev, bar) +
1286 (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
1287
1288 if (size)
1289 *size = pci_resource_len(ndev->ntb.pdev, bar) -
1290 (idx == ndev->b2b_idx ? ndev->b2b_off : 0);
1291
1292 return 0;
1293}
1294
Allen Hubbee26a5842015-04-09 10:33:20 -04001295static int intel_ntb_db_is_unsafe(struct ntb_dev *ntb)
1296{
1297 return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_DB);
1298}
1299
1300static u64 intel_ntb_db_valid_mask(struct ntb_dev *ntb)
1301{
1302 return ntb_ndev(ntb)->db_valid_mask;
1303}
1304
1305static int intel_ntb_db_vector_count(struct ntb_dev *ntb)
1306{
1307 struct intel_ntb_dev *ndev;
1308
1309 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1310
1311 return ndev->db_vec_count;
1312}
1313
1314static u64 intel_ntb_db_vector_mask(struct ntb_dev *ntb, int db_vector)
1315{
1316 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1317
1318 if (db_vector < 0 || db_vector > ndev->db_vec_count)
1319 return 0;
1320
1321 return ndev->db_valid_mask & ndev_vec_mask(ndev, db_vector);
1322}
1323
1324static u64 intel_ntb_db_read(struct ntb_dev *ntb)
1325{
1326 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1327
1328 return ndev_db_read(ndev,
1329 ndev->self_mmio +
1330 ndev->self_reg->db_bell);
1331}
1332
1333static int intel_ntb_db_clear(struct ntb_dev *ntb, u64 db_bits)
1334{
1335 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1336
1337 return ndev_db_write(ndev, db_bits,
1338 ndev->self_mmio +
1339 ndev->self_reg->db_bell);
1340}
1341
1342static int intel_ntb_db_set_mask(struct ntb_dev *ntb, u64 db_bits)
1343{
1344 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1345
1346 return ndev_db_set_mask(ndev, db_bits,
1347 ndev->self_mmio +
1348 ndev->self_reg->db_mask);
1349}
1350
1351static int intel_ntb_db_clear_mask(struct ntb_dev *ntb, u64 db_bits)
1352{
1353 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1354
1355 return ndev_db_clear_mask(ndev, db_bits,
1356 ndev->self_mmio +
1357 ndev->self_reg->db_mask);
1358}
1359
1360static int intel_ntb_peer_db_addr(struct ntb_dev *ntb,
1361 phys_addr_t *db_addr,
1362 resource_size_t *db_size)
1363{
1364 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1365
1366 return ndev_db_addr(ndev, db_addr, db_size, ndev->peer_addr,
1367 ndev->peer_reg->db_bell);
1368}
1369
1370static int intel_ntb_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
1371{
1372 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1373
1374 return ndev_db_write(ndev, db_bits,
1375 ndev->peer_mmio +
1376 ndev->peer_reg->db_bell);
1377}
1378
1379static int intel_ntb_spad_is_unsafe(struct ntb_dev *ntb)
1380{
1381 return ndev_ignore_unsafe(ntb_ndev(ntb), NTB_UNSAFE_SPAD);
1382}
1383
1384static int intel_ntb_spad_count(struct ntb_dev *ntb)
1385{
1386 struct intel_ntb_dev *ndev;
1387
1388 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1389
1390 return ndev->spad_count;
1391}
1392
1393static u32 intel_ntb_spad_read(struct ntb_dev *ntb, int idx)
1394{
1395 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1396
1397 return ndev_spad_read(ndev, idx,
1398 ndev->self_mmio +
1399 ndev->self_reg->spad);
1400}
1401
1402static int intel_ntb_spad_write(struct ntb_dev *ntb,
1403 int idx, u32 val)
1404{
1405 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1406
1407 return ndev_spad_write(ndev, idx, val,
1408 ndev->self_mmio +
1409 ndev->self_reg->spad);
1410}
1411
Serge Semind67288a2017-01-11 03:13:20 +03001412static int intel_ntb_peer_spad_addr(struct ntb_dev *ntb, int pidx, int sidx,
Allen Hubbee26a5842015-04-09 10:33:20 -04001413 phys_addr_t *spad_addr)
1414{
1415 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1416
Serge Semind67288a2017-01-11 03:13:20 +03001417 return ndev_spad_addr(ndev, sidx, spad_addr, ndev->peer_addr,
Allen Hubbee26a5842015-04-09 10:33:20 -04001418 ndev->peer_reg->spad);
1419}
1420
Serge Semind67288a2017-01-11 03:13:20 +03001421static u32 intel_ntb_peer_spad_read(struct ntb_dev *ntb, int pidx, int sidx)
Allen Hubbee26a5842015-04-09 10:33:20 -04001422{
1423 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1424
Serge Semind67288a2017-01-11 03:13:20 +03001425 return ndev_spad_read(ndev, sidx,
Allen Hubbee26a5842015-04-09 10:33:20 -04001426 ndev->peer_mmio +
1427 ndev->peer_reg->spad);
1428}
1429
Serge Semind67288a2017-01-11 03:13:20 +03001430static int intel_ntb_peer_spad_write(struct ntb_dev *ntb, int pidx,
1431 int sidx, u32 val)
Allen Hubbee26a5842015-04-09 10:33:20 -04001432{
1433 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1434
Serge Semind67288a2017-01-11 03:13:20 +03001435 return ndev_spad_write(ndev, sidx, val,
Allen Hubbee26a5842015-04-09 10:33:20 -04001436 ndev->peer_mmio +
1437 ndev->peer_reg->spad);
1438}
1439
Dave Jiang2f887b92015-05-20 12:55:47 -04001440/* ATOM */
Allen Hubbee26a5842015-04-09 10:33:20 -04001441
Dave Jiang2f887b92015-05-20 12:55:47 -04001442static u64 atom_db_ioread(void __iomem *mmio)
Allen Hubbee26a5842015-04-09 10:33:20 -04001443{
1444 return ioread64(mmio);
1445}
1446
Dave Jiang2f887b92015-05-20 12:55:47 -04001447static void atom_db_iowrite(u64 bits, void __iomem *mmio)
Allen Hubbee26a5842015-04-09 10:33:20 -04001448{
1449 iowrite64(bits, mmio);
1450}
1451
Dave Jiang2f887b92015-05-20 12:55:47 -04001452static int atom_poll_link(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04001453{
1454 u32 ntb_ctl;
1455
Dave Jiang2f887b92015-05-20 12:55:47 -04001456 ntb_ctl = ioread32(ndev->self_mmio + ATOM_NTBCNTL_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001457
1458 if (ntb_ctl == ndev->ntb_ctl)
1459 return 0;
1460
1461 ndev->ntb_ctl = ntb_ctl;
1462
Dave Jiang2f887b92015-05-20 12:55:47 -04001463 ndev->lnk_sta = ioread32(ndev->self_mmio + ATOM_LINK_STATUS_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001464
1465 return 1;
1466}
1467
Dave Jiang2f887b92015-05-20 12:55:47 -04001468static int atom_link_is_up(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04001469{
Dave Jiang2f887b92015-05-20 12:55:47 -04001470 return ATOM_NTB_CTL_ACTIVE(ndev->ntb_ctl);
Allen Hubbee26a5842015-04-09 10:33:20 -04001471}
1472
Dave Jiang2f887b92015-05-20 12:55:47 -04001473static int atom_link_is_err(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04001474{
Dave Jiang2f887b92015-05-20 12:55:47 -04001475 if (ioread32(ndev->self_mmio + ATOM_LTSSMSTATEJMP_OFFSET)
1476 & ATOM_LTSSMSTATEJMP_FORCEDETECT)
Allen Hubbee26a5842015-04-09 10:33:20 -04001477 return 1;
1478
Dave Jiang2f887b92015-05-20 12:55:47 -04001479 if (ioread32(ndev->self_mmio + ATOM_IBSTERRRCRVSTS0_OFFSET)
1480 & ATOM_IBIST_ERR_OFLOW)
Allen Hubbee26a5842015-04-09 10:33:20 -04001481 return 1;
1482
1483 return 0;
1484}
1485
Dave Jiang2f887b92015-05-20 12:55:47 -04001486static inline enum ntb_topo atom_ppd_topo(struct intel_ntb_dev *ndev, u32 ppd)
Allen Hubbee26a5842015-04-09 10:33:20 -04001487{
Dave Jiang2f887b92015-05-20 12:55:47 -04001488 switch (ppd & ATOM_PPD_TOPO_MASK) {
1489 case ATOM_PPD_TOPO_B2B_USD:
Allen Hubbee26a5842015-04-09 10:33:20 -04001490 dev_dbg(ndev_dev(ndev), "PPD %d B2B USD\n", ppd);
1491 return NTB_TOPO_B2B_USD;
1492
Dave Jiang2f887b92015-05-20 12:55:47 -04001493 case ATOM_PPD_TOPO_B2B_DSD:
Allen Hubbee26a5842015-04-09 10:33:20 -04001494 dev_dbg(ndev_dev(ndev), "PPD %d B2B DSD\n", ppd);
1495 return NTB_TOPO_B2B_DSD;
1496
Dave Jiang2f887b92015-05-20 12:55:47 -04001497 case ATOM_PPD_TOPO_PRI_USD:
1498 case ATOM_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
1499 case ATOM_PPD_TOPO_SEC_USD:
1500 case ATOM_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
Allen Hubbee26a5842015-04-09 10:33:20 -04001501 dev_dbg(ndev_dev(ndev), "PPD %d non B2B disabled\n", ppd);
1502 return NTB_TOPO_NONE;
1503 }
1504
1505 dev_dbg(ndev_dev(ndev), "PPD %d invalid\n", ppd);
1506 return NTB_TOPO_NONE;
1507}
1508
Dave Jiang2f887b92015-05-20 12:55:47 -04001509static void atom_link_hb(struct work_struct *work)
Allen Hubbee26a5842015-04-09 10:33:20 -04001510{
1511 struct intel_ntb_dev *ndev = hb_ndev(work);
1512 unsigned long poll_ts;
1513 void __iomem *mmio;
1514 u32 status32;
1515
Dave Jiang2f887b92015-05-20 12:55:47 -04001516 poll_ts = ndev->last_ts + ATOM_LINK_HB_TIMEOUT;
Allen Hubbee26a5842015-04-09 10:33:20 -04001517
1518 /* Delay polling the link status if an interrupt was received,
1519 * unless the cached link status says the link is down.
1520 */
Dave Jiang2f887b92015-05-20 12:55:47 -04001521 if (time_after(poll_ts, jiffies) && atom_link_is_up(ndev)) {
Allen Hubbee26a5842015-04-09 10:33:20 -04001522 schedule_delayed_work(&ndev->hb_timer, poll_ts - jiffies);
1523 return;
1524 }
1525
Dave Jiang2f887b92015-05-20 12:55:47 -04001526 if (atom_poll_link(ndev))
Allen Hubbee26a5842015-04-09 10:33:20 -04001527 ntb_link_event(&ndev->ntb);
1528
Dave Jiang2f887b92015-05-20 12:55:47 -04001529 if (atom_link_is_up(ndev) || !atom_link_is_err(ndev)) {
1530 schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
Allen Hubbee26a5842015-04-09 10:33:20 -04001531 return;
1532 }
1533
1534 /* Link is down with error: recover the link! */
1535
1536 mmio = ndev->self_mmio;
1537
1538 /* Driver resets the NTB ModPhy lanes - magic! */
Dave Jiang2f887b92015-05-20 12:55:47 -04001539 iowrite8(0xe0, mmio + ATOM_MODPHY_PCSREG6);
1540 iowrite8(0x40, mmio + ATOM_MODPHY_PCSREG4);
1541 iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG4);
1542 iowrite8(0x60, mmio + ATOM_MODPHY_PCSREG6);
Allen Hubbee26a5842015-04-09 10:33:20 -04001543
1544 /* Driver waits 100ms to allow the NTB ModPhy to settle */
1545 msleep(100);
1546
1547 /* Clear AER Errors, write to clear */
Dave Jiang2f887b92015-05-20 12:55:47 -04001548 status32 = ioread32(mmio + ATOM_ERRCORSTS_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001549 dev_dbg(ndev_dev(ndev), "ERRCORSTS = %x\n", status32);
1550 status32 &= PCI_ERR_COR_REP_ROLL;
Dave Jiang2f887b92015-05-20 12:55:47 -04001551 iowrite32(status32, mmio + ATOM_ERRCORSTS_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001552
1553 /* Clear unexpected electrical idle event in LTSSM, write to clear */
Dave Jiang2f887b92015-05-20 12:55:47 -04001554 status32 = ioread32(mmio + ATOM_LTSSMERRSTS0_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001555 dev_dbg(ndev_dev(ndev), "LTSSMERRSTS0 = %x\n", status32);
Dave Jiang2f887b92015-05-20 12:55:47 -04001556 status32 |= ATOM_LTSSMERRSTS0_UNEXPECTEDEI;
1557 iowrite32(status32, mmio + ATOM_LTSSMERRSTS0_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001558
1559 /* Clear DeSkew Buffer error, write to clear */
Dave Jiang2f887b92015-05-20 12:55:47 -04001560 status32 = ioread32(mmio + ATOM_DESKEWSTS_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001561 dev_dbg(ndev_dev(ndev), "DESKEWSTS = %x\n", status32);
Dave Jiang2f887b92015-05-20 12:55:47 -04001562 status32 |= ATOM_DESKEWSTS_DBERR;
1563 iowrite32(status32, mmio + ATOM_DESKEWSTS_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001564
Dave Jiang2f887b92015-05-20 12:55:47 -04001565 status32 = ioread32(mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001566 dev_dbg(ndev_dev(ndev), "IBSTERRRCRVSTS0 = %x\n", status32);
Dave Jiang2f887b92015-05-20 12:55:47 -04001567 status32 &= ATOM_IBIST_ERR_OFLOW;
1568 iowrite32(status32, mmio + ATOM_IBSTERRRCRVSTS0_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001569
1570 /* Releases the NTB state machine to allow the link to retrain */
Dave Jiang2f887b92015-05-20 12:55:47 -04001571 status32 = ioread32(mmio + ATOM_LTSSMSTATEJMP_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001572 dev_dbg(ndev_dev(ndev), "LTSSMSTATEJMP = %x\n", status32);
Dave Jiang2f887b92015-05-20 12:55:47 -04001573 status32 &= ~ATOM_LTSSMSTATEJMP_FORCEDETECT;
1574 iowrite32(status32, mmio + ATOM_LTSSMSTATEJMP_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001575
1576 /* There is a potential race between the 2 NTB devices recovering at the
1577 * same time. If the times are the same, the link will not recover and
1578 * the driver will be stuck in this loop forever. Add a random interval
1579 * to the recovery time to prevent this race.
1580 */
Dave Jiang2f887b92015-05-20 12:55:47 -04001581 schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_RECOVERY_TIME
1582 + prandom_u32() % ATOM_LINK_RECOVERY_TIME);
Allen Hubbee26a5842015-04-09 10:33:20 -04001583}
1584
Dave Jiang2f887b92015-05-20 12:55:47 -04001585static int atom_init_isr(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04001586{
1587 int rc;
1588
Dave Jiang2f887b92015-05-20 12:55:47 -04001589 rc = ndev_init_isr(ndev, 1, ATOM_DB_MSIX_VECTOR_COUNT,
1590 ATOM_DB_MSIX_VECTOR_SHIFT, ATOM_DB_TOTAL_SHIFT);
Allen Hubbee26a5842015-04-09 10:33:20 -04001591 if (rc)
1592 return rc;
1593
Dave Jiang2f887b92015-05-20 12:55:47 -04001594 /* ATOM doesn't have link status interrupt, poll on that platform */
Allen Hubbee26a5842015-04-09 10:33:20 -04001595 ndev->last_ts = jiffies;
Dave Jiang2f887b92015-05-20 12:55:47 -04001596 INIT_DELAYED_WORK(&ndev->hb_timer, atom_link_hb);
1597 schedule_delayed_work(&ndev->hb_timer, ATOM_LINK_HB_TIMEOUT);
Allen Hubbee26a5842015-04-09 10:33:20 -04001598
1599 return 0;
1600}
1601
Dave Jiang2f887b92015-05-20 12:55:47 -04001602static void atom_deinit_isr(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04001603{
1604 cancel_delayed_work_sync(&ndev->hb_timer);
1605 ndev_deinit_isr(ndev);
1606}
1607
Dave Jiang2f887b92015-05-20 12:55:47 -04001608static int atom_init_ntb(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04001609{
Dave Jiang2f887b92015-05-20 12:55:47 -04001610 ndev->mw_count = ATOM_MW_COUNT;
1611 ndev->spad_count = ATOM_SPAD_COUNT;
1612 ndev->db_count = ATOM_DB_COUNT;
Allen Hubbee26a5842015-04-09 10:33:20 -04001613
1614 switch (ndev->ntb.topo) {
1615 case NTB_TOPO_B2B_USD:
1616 case NTB_TOPO_B2B_DSD:
Dave Jiang2f887b92015-05-20 12:55:47 -04001617 ndev->self_reg = &atom_pri_reg;
1618 ndev->peer_reg = &atom_b2b_reg;
1619 ndev->xlat_reg = &atom_sec_xlat;
Allen Hubbee26a5842015-04-09 10:33:20 -04001620
1621 /* Enable Bus Master and Memory Space on the secondary side */
1622 iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
Dave Jiang2f887b92015-05-20 12:55:47 -04001623 ndev->self_mmio + ATOM_SPCICMD_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04001624
1625 break;
1626
1627 default:
1628 return -EINVAL;
1629 }
1630
1631 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
1632
1633 return 0;
1634}
1635
Dave Jiang2f887b92015-05-20 12:55:47 -04001636static int atom_init_dev(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04001637{
1638 u32 ppd;
1639 int rc;
1640
Dave Jiang2f887b92015-05-20 12:55:47 -04001641 rc = pci_read_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET, &ppd);
Allen Hubbee26a5842015-04-09 10:33:20 -04001642 if (rc)
1643 return -EIO;
1644
Dave Jiang2f887b92015-05-20 12:55:47 -04001645 ndev->ntb.topo = atom_ppd_topo(ndev, ppd);
Allen Hubbee26a5842015-04-09 10:33:20 -04001646 if (ndev->ntb.topo == NTB_TOPO_NONE)
1647 return -EINVAL;
1648
Dave Jiang2f887b92015-05-20 12:55:47 -04001649 rc = atom_init_ntb(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04001650 if (rc)
1651 return rc;
1652
Dave Jiang2f887b92015-05-20 12:55:47 -04001653 rc = atom_init_isr(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04001654 if (rc)
1655 return rc;
1656
1657 if (ndev->ntb.topo != NTB_TOPO_SEC) {
1658 /* Initiate PCI-E link training */
Dave Jiang2f887b92015-05-20 12:55:47 -04001659 rc = pci_write_config_dword(ndev->ntb.pdev, ATOM_PPD_OFFSET,
1660 ppd | ATOM_PPD_INIT_LINK);
Allen Hubbee26a5842015-04-09 10:33:20 -04001661 if (rc)
1662 return rc;
1663 }
1664
1665 return 0;
1666}
1667
Dave Jiang2f887b92015-05-20 12:55:47 -04001668static void atom_deinit_dev(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04001669{
Dave Jiang2f887b92015-05-20 12:55:47 -04001670 atom_deinit_isr(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04001671}
1672
Dave Jiang783dfa62016-11-16 14:03:38 -07001673/* Skylake Xeon NTB */
1674
Dave Jiang939ada52017-02-16 16:22:36 -07001675static int skx_poll_link(struct intel_ntb_dev *ndev)
1676{
1677 u16 reg_val;
1678 int rc;
1679
1680 ndev->reg->db_iowrite(ndev->db_link_mask,
1681 ndev->self_mmio +
1682 ndev->self_reg->db_clear);
1683
1684 rc = pci_read_config_word(ndev->ntb.pdev,
1685 SKX_LINK_STATUS_OFFSET, &reg_val);
1686 if (rc)
1687 return 0;
1688
1689 if (reg_val == ndev->lnk_sta)
1690 return 0;
1691
1692 ndev->lnk_sta = reg_val;
1693
1694 return 1;
1695}
1696
Dave Jiang783dfa62016-11-16 14:03:38 -07001697static u64 skx_db_ioread(void __iomem *mmio)
1698{
1699 return ioread64(mmio);
1700}
1701
1702static void skx_db_iowrite(u64 bits, void __iomem *mmio)
1703{
1704 iowrite64(bits, mmio);
1705}
1706
1707static int skx_init_isr(struct intel_ntb_dev *ndev)
1708{
1709 int i;
1710
1711 /*
1712 * The MSIX vectors and the interrupt status bits are not lined up
1713 * on Skylake. By default the link status bit is bit 32, however it
1714 * is by default MSIX vector0. We need to fixup to line them up.
1715 * The vectors at reset is 1-32,0. We need to reprogram to 0-32.
1716 */
1717
1718 for (i = 0; i < SKX_DB_MSIX_VECTOR_COUNT; i++)
1719 iowrite8(i, ndev->self_mmio + SKX_INTVEC_OFFSET + i);
1720
1721 /* move link status down one as workaround */
1722 if (ndev->hwerr_flags & NTB_HWERR_MSIX_VECTOR32_BAD) {
1723 iowrite8(SKX_DB_MSIX_VECTOR_COUNT - 2,
1724 ndev->self_mmio + SKX_INTVEC_OFFSET +
1725 (SKX_DB_MSIX_VECTOR_COUNT - 1));
1726 }
1727
1728 return ndev_init_isr(ndev, SKX_DB_MSIX_VECTOR_COUNT,
1729 SKX_DB_MSIX_VECTOR_COUNT,
1730 SKX_DB_MSIX_VECTOR_SHIFT,
1731 SKX_DB_TOTAL_SHIFT);
1732}
1733
1734static int skx_setup_b2b_mw(struct intel_ntb_dev *ndev,
1735 const struct intel_b2b_addr *addr,
1736 const struct intel_b2b_addr *peer_addr)
1737{
1738 struct pci_dev *pdev;
1739 void __iomem *mmio;
1740 resource_size_t bar_size;
1741 phys_addr_t bar_addr;
1742 int b2b_bar;
1743 u8 bar_sz;
1744
1745 pdev = ndev_pdev(ndev);
1746 mmio = ndev->self_mmio;
1747
1748 if (ndev->b2b_idx == UINT_MAX) {
1749 dev_dbg(ndev_dev(ndev), "not using b2b mw\n");
1750 b2b_bar = 0;
1751 ndev->b2b_off = 0;
1752 } else {
1753 b2b_bar = ndev_mw_to_bar(ndev, ndev->b2b_idx);
1754 if (b2b_bar < 0)
1755 return -EIO;
1756
1757 dev_dbg(ndev_dev(ndev), "using b2b mw bar %d\n", b2b_bar);
1758
1759 bar_size = pci_resource_len(ndev->ntb.pdev, b2b_bar);
1760
1761 dev_dbg(ndev_dev(ndev), "b2b bar size %#llx\n", bar_size);
1762
1763 if (b2b_mw_share && ((bar_size >> 1) >= XEON_B2B_MIN_SIZE)) {
1764 dev_dbg(ndev_dev(ndev),
1765 "b2b using first half of bar\n");
1766 ndev->b2b_off = bar_size >> 1;
1767 } else if (bar_size >= XEON_B2B_MIN_SIZE) {
1768 dev_dbg(ndev_dev(ndev),
1769 "b2b using whole bar\n");
1770 ndev->b2b_off = 0;
1771 --ndev->mw_count;
1772 } else {
1773 dev_dbg(ndev_dev(ndev),
1774 "b2b bar size is too small\n");
1775 return -EIO;
1776 }
1777 }
1778
1779 /*
1780 * Reset the secondary bar sizes to match the primary bar sizes,
1781 * except disable or halve the size of the b2b secondary bar.
1782 */
1783 pci_read_config_byte(pdev, SKX_IMBAR1SZ_OFFSET, &bar_sz);
1784 dev_dbg(ndev_dev(ndev), "IMBAR1SZ %#x\n", bar_sz);
1785 if (b2b_bar == 1) {
1786 if (ndev->b2b_off)
1787 bar_sz -= 1;
1788 else
1789 bar_sz = 0;
1790 }
1791
1792 pci_write_config_byte(pdev, SKX_EMBAR1SZ_OFFSET, bar_sz);
1793 pci_read_config_byte(pdev, SKX_EMBAR1SZ_OFFSET, &bar_sz);
1794 dev_dbg(ndev_dev(ndev), "EMBAR1SZ %#x\n", bar_sz);
1795
1796 pci_read_config_byte(pdev, SKX_IMBAR2SZ_OFFSET, &bar_sz);
1797 dev_dbg(ndev_dev(ndev), "IMBAR2SZ %#x\n", bar_sz);
1798 if (b2b_bar == 2) {
1799 if (ndev->b2b_off)
1800 bar_sz -= 1;
1801 else
1802 bar_sz = 0;
1803 }
1804
1805 pci_write_config_byte(pdev, SKX_EMBAR2SZ_OFFSET, bar_sz);
1806 pci_read_config_byte(pdev, SKX_EMBAR2SZ_OFFSET, &bar_sz);
1807 dev_dbg(ndev_dev(ndev), "EMBAR2SZ %#x\n", bar_sz);
1808
1809 /* SBAR01 hit by first part of the b2b bar */
1810 if (b2b_bar == 0)
1811 bar_addr = addr->bar0_addr;
1812 else if (b2b_bar == 1)
1813 bar_addr = addr->bar2_addr64;
1814 else if (b2b_bar == 2)
1815 bar_addr = addr->bar4_addr64;
1816 else
1817 return -EIO;
1818
1819 /* setup incoming bar limits == base addrs (zero length windows) */
1820 bar_addr = addr->bar2_addr64 + (b2b_bar == 1 ? ndev->b2b_off : 0);
1821 iowrite64(bar_addr, mmio + SKX_IMBAR1XLMT_OFFSET);
1822 bar_addr = ioread64(mmio + SKX_IMBAR1XLMT_OFFSET);
1823 dev_dbg(ndev_dev(ndev), "IMBAR1XLMT %#018llx\n", bar_addr);
1824
1825 bar_addr = addr->bar4_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
1826 iowrite64(bar_addr, mmio + SKX_IMBAR2XLMT_OFFSET);
1827 bar_addr = ioread64(mmio + SKX_IMBAR2XLMT_OFFSET);
1828 dev_dbg(ndev_dev(ndev), "IMBAR2XLMT %#018llx\n", bar_addr);
1829
1830 /* zero incoming translation addrs */
1831 iowrite64(0, mmio + SKX_IMBAR1XBASE_OFFSET);
1832 iowrite64(0, mmio + SKX_IMBAR2XBASE_OFFSET);
1833
1834 ndev->peer_mmio = ndev->self_mmio;
1835
1836 return 0;
1837}
1838
1839static int skx_init_ntb(struct intel_ntb_dev *ndev)
1840{
1841 int rc;
1842
1843
1844 ndev->mw_count = XEON_MW_COUNT;
1845 ndev->spad_count = SKX_SPAD_COUNT;
1846 ndev->db_count = SKX_DB_COUNT;
1847 ndev->db_link_mask = SKX_DB_LINK_BIT;
1848
1849 /* DB fixup for using 31 right now */
1850 if (ndev->hwerr_flags & NTB_HWERR_MSIX_VECTOR32_BAD)
1851 ndev->db_link_mask |= BIT_ULL(31);
1852
1853 switch (ndev->ntb.topo) {
1854 case NTB_TOPO_B2B_USD:
1855 case NTB_TOPO_B2B_DSD:
1856 ndev->self_reg = &skx_pri_reg;
1857 ndev->peer_reg = &skx_b2b_reg;
1858 ndev->xlat_reg = &skx_sec_xlat;
1859
1860 if (ndev->ntb.topo == NTB_TOPO_B2B_USD) {
1861 rc = skx_setup_b2b_mw(ndev,
1862 &xeon_b2b_dsd_addr,
1863 &xeon_b2b_usd_addr);
1864 } else {
1865 rc = skx_setup_b2b_mw(ndev,
1866 &xeon_b2b_usd_addr,
1867 &xeon_b2b_dsd_addr);
1868 }
1869
1870 if (rc)
1871 return rc;
1872
1873 /* Enable Bus Master and Memory Space on the secondary side */
1874 iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
1875 ndev->self_mmio + SKX_SPCICMD_OFFSET);
1876
1877 break;
1878
1879 default:
1880 return -EINVAL;
1881 }
1882
1883 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
1884
1885 ndev->reg->db_iowrite(ndev->db_valid_mask,
1886 ndev->self_mmio +
1887 ndev->self_reg->db_mask);
1888
1889 return 0;
1890}
1891
1892static int skx_init_dev(struct intel_ntb_dev *ndev)
1893{
1894 struct pci_dev *pdev;
1895 u8 ppd;
1896 int rc;
1897
1898 pdev = ndev_pdev(ndev);
1899
1900 ndev->reg = &skx_reg;
1901
1902 rc = pci_read_config_byte(pdev, XEON_PPD_OFFSET, &ppd);
1903 if (rc)
1904 return -EIO;
1905
1906 ndev->ntb.topo = xeon_ppd_topo(ndev, ppd);
1907 dev_dbg(ndev_dev(ndev), "ppd %#x topo %s\n", ppd,
1908 ntb_topo_string(ndev->ntb.topo));
1909 if (ndev->ntb.topo == NTB_TOPO_NONE)
1910 return -EINVAL;
1911
1912 if (pdev_is_skx_xeon(pdev))
1913 ndev->hwerr_flags |= NTB_HWERR_MSIX_VECTOR32_BAD;
1914
1915 rc = skx_init_ntb(ndev);
1916 if (rc)
1917 return rc;
1918
1919 return skx_init_isr(ndev);
1920}
1921
1922static int intel_ntb3_link_enable(struct ntb_dev *ntb,
1923 enum ntb_speed max_speed,
1924 enum ntb_width max_width)
1925{
1926 struct intel_ntb_dev *ndev;
1927 u32 ntb_ctl;
1928
1929 ndev = container_of(ntb, struct intel_ntb_dev, ntb);
1930
1931 dev_dbg(ndev_dev(ndev),
1932 "Enabling link with max_speed %d max_width %d\n",
1933 max_speed, max_width);
1934
1935 if (max_speed != NTB_SPEED_AUTO)
1936 dev_dbg(ndev_dev(ndev), "ignoring max_speed %d\n", max_speed);
1937 if (max_width != NTB_WIDTH_AUTO)
1938 dev_dbg(ndev_dev(ndev), "ignoring max_width %d\n", max_width);
1939
1940 ntb_ctl = ioread32(ndev->self_mmio + ndev->reg->ntb_ctl);
1941 ntb_ctl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
1942 ntb_ctl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
1943 ntb_ctl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
1944 iowrite32(ntb_ctl, ndev->self_mmio + ndev->reg->ntb_ctl);
1945
1946 return 0;
1947}
Serge Semin443b9a12017-01-11 03:11:33 +03001948static int intel_ntb3_mw_set_trans(struct ntb_dev *ntb, int pidx, int idx,
Dave Jiang783dfa62016-11-16 14:03:38 -07001949 dma_addr_t addr, resource_size_t size)
1950{
1951 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
1952 unsigned long xlat_reg, limit_reg;
1953 resource_size_t bar_size, mw_size;
1954 void __iomem *mmio;
1955 u64 base, limit, reg_val;
1956 int bar;
1957
Serge Semin443b9a12017-01-11 03:11:33 +03001958 if (pidx != NTB_DEF_PEER_IDX)
1959 return -EINVAL;
1960
Dave Jiang783dfa62016-11-16 14:03:38 -07001961 if (idx >= ndev->b2b_idx && !ndev->b2b_off)
1962 idx += 1;
1963
1964 bar = ndev_mw_to_bar(ndev, idx);
1965 if (bar < 0)
1966 return bar;
1967
1968 bar_size = pci_resource_len(ndev->ntb.pdev, bar);
1969
1970 if (idx == ndev->b2b_idx)
1971 mw_size = bar_size - ndev->b2b_off;
1972 else
1973 mw_size = bar_size;
1974
1975 /* hardware requires that addr is aligned to bar size */
1976 if (addr & (bar_size - 1))
1977 return -EINVAL;
1978
1979 /* make sure the range fits in the usable mw size */
1980 if (size > mw_size)
1981 return -EINVAL;
1982
1983 mmio = ndev->self_mmio;
1984 xlat_reg = ndev->xlat_reg->bar2_xlat + (idx * 0x10);
1985 limit_reg = ndev->xlat_reg->bar2_limit + (idx * 0x10);
1986 base = pci_resource_start(ndev->ntb.pdev, bar);
1987
1988 /* Set the limit if supported, if size is not mw_size */
1989 if (limit_reg && size != mw_size)
1990 limit = base + size;
1991 else
1992 limit = base + mw_size;
1993
1994 /* set and verify setting the translation address */
1995 iowrite64(addr, mmio + xlat_reg);
1996 reg_val = ioread64(mmio + xlat_reg);
1997 if (reg_val != addr) {
1998 iowrite64(0, mmio + xlat_reg);
1999 return -EIO;
2000 }
2001
2002 dev_dbg(ndev_dev(ndev), "BAR %d IMBARXBASE: %#Lx\n", bar, reg_val);
2003
2004 /* set and verify setting the limit */
2005 iowrite64(limit, mmio + limit_reg);
2006 reg_val = ioread64(mmio + limit_reg);
2007 if (reg_val != limit) {
2008 iowrite64(base, mmio + limit_reg);
2009 iowrite64(0, mmio + xlat_reg);
2010 return -EIO;
2011 }
2012
2013 dev_dbg(ndev_dev(ndev), "BAR %d IMBARXLMT: %#Lx\n", bar, reg_val);
2014
2015 /* setup the EP */
2016 limit_reg = ndev->xlat_reg->bar2_limit + (idx * 0x10) + 0x4000;
2017 base = ioread64(mmio + SKX_EMBAR1_OFFSET + (8 * idx));
2018 base &= ~0xf;
2019
2020 if (limit_reg && size != mw_size)
2021 limit = base + size;
2022 else
2023 limit = base + mw_size;
2024
2025 /* set and verify setting the limit */
2026 iowrite64(limit, mmio + limit_reg);
2027 reg_val = ioread64(mmio + limit_reg);
2028 if (reg_val != limit) {
2029 iowrite64(base, mmio + limit_reg);
2030 iowrite64(0, mmio + xlat_reg);
2031 return -EIO;
2032 }
2033
2034 dev_dbg(ndev_dev(ndev), "BAR %d EMBARXLMT: %#Lx\n", bar, reg_val);
2035
2036 return 0;
2037}
2038
2039static int intel_ntb3_peer_db_set(struct ntb_dev *ntb, u64 db_bits)
2040{
2041 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
2042 int bit;
2043
2044 if (db_bits & ~ndev->db_valid_mask)
2045 return -EINVAL;
2046
2047 while (db_bits) {
2048 bit = __ffs(db_bits);
2049 iowrite32(1, ndev->peer_mmio +
2050 ndev->peer_reg->db_bell + (bit * 4));
2051 db_bits &= db_bits - 1;
2052 }
2053
2054 return 0;
2055}
2056
2057static u64 intel_ntb3_db_read(struct ntb_dev *ntb)
2058{
2059 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
2060
2061 return ndev_db_read(ndev,
2062 ndev->self_mmio +
2063 ndev->self_reg->db_clear);
2064}
2065
2066static int intel_ntb3_db_clear(struct ntb_dev *ntb, u64 db_bits)
2067{
2068 struct intel_ntb_dev *ndev = ntb_ndev(ntb);
2069
2070 return ndev_db_write(ndev, db_bits,
2071 ndev->self_mmio +
2072 ndev->self_reg->db_clear);
2073}
2074
Dave Jiang2f887b92015-05-20 12:55:47 -04002075/* XEON */
Allen Hubbee26a5842015-04-09 10:33:20 -04002076
Dave Jiang2f887b92015-05-20 12:55:47 -04002077static u64 xeon_db_ioread(void __iomem *mmio)
Allen Hubbee26a5842015-04-09 10:33:20 -04002078{
2079 return (u64)ioread16(mmio);
2080}
2081
Dave Jiang2f887b92015-05-20 12:55:47 -04002082static void xeon_db_iowrite(u64 bits, void __iomem *mmio)
Allen Hubbee26a5842015-04-09 10:33:20 -04002083{
2084 iowrite16((u16)bits, mmio);
2085}
2086
Dave Jiang2f887b92015-05-20 12:55:47 -04002087static int xeon_poll_link(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04002088{
2089 u16 reg_val;
2090 int rc;
2091
2092 ndev->reg->db_iowrite(ndev->db_link_mask,
2093 ndev->self_mmio +
2094 ndev->self_reg->db_bell);
2095
2096 rc = pci_read_config_word(ndev->ntb.pdev,
Dave Jiang2f887b92015-05-20 12:55:47 -04002097 XEON_LINK_STATUS_OFFSET, &reg_val);
Allen Hubbee26a5842015-04-09 10:33:20 -04002098 if (rc)
2099 return 0;
2100
2101 if (reg_val == ndev->lnk_sta)
2102 return 0;
2103
2104 ndev->lnk_sta = reg_val;
2105
2106 return 1;
2107}
2108
Dave Jiang2f887b92015-05-20 12:55:47 -04002109static int xeon_link_is_up(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04002110{
Dave Jiang5ae0beb2015-05-19 16:59:34 -04002111 if (ndev->ntb.topo == NTB_TOPO_SEC)
2112 return 1;
2113
Allen Hubbee26a5842015-04-09 10:33:20 -04002114 return NTB_LNK_STA_ACTIVE(ndev->lnk_sta);
2115}
2116
Dave Jiang2f887b92015-05-20 12:55:47 -04002117static inline enum ntb_topo xeon_ppd_topo(struct intel_ntb_dev *ndev, u8 ppd)
Allen Hubbee26a5842015-04-09 10:33:20 -04002118{
Dave Jiang2f887b92015-05-20 12:55:47 -04002119 switch (ppd & XEON_PPD_TOPO_MASK) {
2120 case XEON_PPD_TOPO_B2B_USD:
Allen Hubbee26a5842015-04-09 10:33:20 -04002121 return NTB_TOPO_B2B_USD;
2122
Dave Jiang2f887b92015-05-20 12:55:47 -04002123 case XEON_PPD_TOPO_B2B_DSD:
Allen Hubbee26a5842015-04-09 10:33:20 -04002124 return NTB_TOPO_B2B_DSD;
2125
Dave Jiang2f887b92015-05-20 12:55:47 -04002126 case XEON_PPD_TOPO_PRI_USD:
2127 case XEON_PPD_TOPO_PRI_DSD: /* accept bogus PRI_DSD */
Allen Hubbee26a5842015-04-09 10:33:20 -04002128 return NTB_TOPO_PRI;
2129
Dave Jiang2f887b92015-05-20 12:55:47 -04002130 case XEON_PPD_TOPO_SEC_USD:
2131 case XEON_PPD_TOPO_SEC_DSD: /* accept bogus SEC_DSD */
Allen Hubbee26a5842015-04-09 10:33:20 -04002132 return NTB_TOPO_SEC;
2133 }
2134
2135 return NTB_TOPO_NONE;
2136}
2137
Dave Jiang2f887b92015-05-20 12:55:47 -04002138static inline int xeon_ppd_bar4_split(struct intel_ntb_dev *ndev, u8 ppd)
Allen Hubbee26a5842015-04-09 10:33:20 -04002139{
Dave Jiang2f887b92015-05-20 12:55:47 -04002140 if (ppd & XEON_PPD_SPLIT_BAR_MASK) {
Allen Hubbee26a5842015-04-09 10:33:20 -04002141 dev_dbg(ndev_dev(ndev), "PPD %d split bar\n", ppd);
2142 return 1;
2143 }
2144 return 0;
2145}
2146
Dave Jiang2f887b92015-05-20 12:55:47 -04002147static int xeon_init_isr(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04002148{
Dave Jiang2f887b92015-05-20 12:55:47 -04002149 return ndev_init_isr(ndev, XEON_DB_MSIX_VECTOR_COUNT,
2150 XEON_DB_MSIX_VECTOR_COUNT,
2151 XEON_DB_MSIX_VECTOR_SHIFT,
2152 XEON_DB_TOTAL_SHIFT);
Allen Hubbee26a5842015-04-09 10:33:20 -04002153}
2154
Dave Jiang2f887b92015-05-20 12:55:47 -04002155static void xeon_deinit_isr(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04002156{
2157 ndev_deinit_isr(ndev);
2158}
2159
Dave Jiang2f887b92015-05-20 12:55:47 -04002160static int xeon_setup_b2b_mw(struct intel_ntb_dev *ndev,
2161 const struct intel_b2b_addr *addr,
2162 const struct intel_b2b_addr *peer_addr)
Allen Hubbee26a5842015-04-09 10:33:20 -04002163{
2164 struct pci_dev *pdev;
2165 void __iomem *mmio;
2166 resource_size_t bar_size;
2167 phys_addr_t bar_addr;
2168 int b2b_bar;
2169 u8 bar_sz;
2170
2171 pdev = ndev_pdev(ndev);
2172 mmio = ndev->self_mmio;
2173
Allen Hubbe2aa2a77a2015-08-31 09:30:59 -04002174 if (ndev->b2b_idx == UINT_MAX) {
Allen Hubbee26a5842015-04-09 10:33:20 -04002175 dev_dbg(ndev_dev(ndev), "not using b2b mw\n");
2176 b2b_bar = 0;
2177 ndev->b2b_off = 0;
2178 } else {
2179 b2b_bar = ndev_mw_to_bar(ndev, ndev->b2b_idx);
2180 if (b2b_bar < 0)
2181 return -EIO;
2182
2183 dev_dbg(ndev_dev(ndev), "using b2b mw bar %d\n", b2b_bar);
2184
2185 bar_size = pci_resource_len(ndev->ntb.pdev, b2b_bar);
2186
2187 dev_dbg(ndev_dev(ndev), "b2b bar size %#llx\n", bar_size);
2188
Dave Jiang2f887b92015-05-20 12:55:47 -04002189 if (b2b_mw_share && XEON_B2B_MIN_SIZE <= bar_size >> 1) {
Allen Hubbee26a5842015-04-09 10:33:20 -04002190 dev_dbg(ndev_dev(ndev),
2191 "b2b using first half of bar\n");
2192 ndev->b2b_off = bar_size >> 1;
Dave Jiang2f887b92015-05-20 12:55:47 -04002193 } else if (XEON_B2B_MIN_SIZE <= bar_size) {
Allen Hubbee26a5842015-04-09 10:33:20 -04002194 dev_dbg(ndev_dev(ndev),
2195 "b2b using whole bar\n");
2196 ndev->b2b_off = 0;
2197 --ndev->mw_count;
2198 } else {
2199 dev_dbg(ndev_dev(ndev),
2200 "b2b bar size is too small\n");
2201 return -EIO;
2202 }
2203 }
2204
2205 /* Reset the secondary bar sizes to match the primary bar sizes,
2206 * except disable or halve the size of the b2b secondary bar.
2207 *
2208 * Note: code for each specific bar size register, because the register
2209 * offsets are not in a consistent order (bar5sz comes after ppd, odd).
2210 */
Dave Jiang2f887b92015-05-20 12:55:47 -04002211 pci_read_config_byte(pdev, XEON_PBAR23SZ_OFFSET, &bar_sz);
Allen Hubbee26a5842015-04-09 10:33:20 -04002212 dev_dbg(ndev_dev(ndev), "PBAR23SZ %#x\n", bar_sz);
2213 if (b2b_bar == 2) {
2214 if (ndev->b2b_off)
2215 bar_sz -= 1;
2216 else
2217 bar_sz = 0;
2218 }
Dave Jiang2f887b92015-05-20 12:55:47 -04002219 pci_write_config_byte(pdev, XEON_SBAR23SZ_OFFSET, bar_sz);
2220 pci_read_config_byte(pdev, XEON_SBAR23SZ_OFFSET, &bar_sz);
Allen Hubbee26a5842015-04-09 10:33:20 -04002221 dev_dbg(ndev_dev(ndev), "SBAR23SZ %#x\n", bar_sz);
2222
2223 if (!ndev->bar4_split) {
Dave Jiang2f887b92015-05-20 12:55:47 -04002224 pci_read_config_byte(pdev, XEON_PBAR45SZ_OFFSET, &bar_sz);
Allen Hubbee26a5842015-04-09 10:33:20 -04002225 dev_dbg(ndev_dev(ndev), "PBAR45SZ %#x\n", bar_sz);
2226 if (b2b_bar == 4) {
2227 if (ndev->b2b_off)
2228 bar_sz -= 1;
2229 else
2230 bar_sz = 0;
2231 }
Dave Jiang2f887b92015-05-20 12:55:47 -04002232 pci_write_config_byte(pdev, XEON_SBAR45SZ_OFFSET, bar_sz);
2233 pci_read_config_byte(pdev, XEON_SBAR45SZ_OFFSET, &bar_sz);
Allen Hubbee26a5842015-04-09 10:33:20 -04002234 dev_dbg(ndev_dev(ndev), "SBAR45SZ %#x\n", bar_sz);
2235 } else {
Dave Jiang2f887b92015-05-20 12:55:47 -04002236 pci_read_config_byte(pdev, XEON_PBAR4SZ_OFFSET, &bar_sz);
Allen Hubbee26a5842015-04-09 10:33:20 -04002237 dev_dbg(ndev_dev(ndev), "PBAR4SZ %#x\n", bar_sz);
2238 if (b2b_bar == 4) {
2239 if (ndev->b2b_off)
2240 bar_sz -= 1;
2241 else
2242 bar_sz = 0;
2243 }
Dave Jiang2f887b92015-05-20 12:55:47 -04002244 pci_write_config_byte(pdev, XEON_SBAR4SZ_OFFSET, bar_sz);
2245 pci_read_config_byte(pdev, XEON_SBAR4SZ_OFFSET, &bar_sz);
Allen Hubbee26a5842015-04-09 10:33:20 -04002246 dev_dbg(ndev_dev(ndev), "SBAR4SZ %#x\n", bar_sz);
2247
Dave Jiang2f887b92015-05-20 12:55:47 -04002248 pci_read_config_byte(pdev, XEON_PBAR5SZ_OFFSET, &bar_sz);
Allen Hubbee26a5842015-04-09 10:33:20 -04002249 dev_dbg(ndev_dev(ndev), "PBAR5SZ %#x\n", bar_sz);
2250 if (b2b_bar == 5) {
2251 if (ndev->b2b_off)
2252 bar_sz -= 1;
2253 else
2254 bar_sz = 0;
2255 }
Dave Jiang2f887b92015-05-20 12:55:47 -04002256 pci_write_config_byte(pdev, XEON_SBAR5SZ_OFFSET, bar_sz);
2257 pci_read_config_byte(pdev, XEON_SBAR5SZ_OFFSET, &bar_sz);
Allen Hubbee26a5842015-04-09 10:33:20 -04002258 dev_dbg(ndev_dev(ndev), "SBAR5SZ %#x\n", bar_sz);
2259 }
2260
2261 /* SBAR01 hit by first part of the b2b bar */
2262 if (b2b_bar == 0)
2263 bar_addr = addr->bar0_addr;
2264 else if (b2b_bar == 2)
2265 bar_addr = addr->bar2_addr64;
2266 else if (b2b_bar == 4 && !ndev->bar4_split)
2267 bar_addr = addr->bar4_addr64;
2268 else if (b2b_bar == 4)
2269 bar_addr = addr->bar4_addr32;
2270 else if (b2b_bar == 5)
2271 bar_addr = addr->bar5_addr32;
2272 else
2273 return -EIO;
2274
2275 dev_dbg(ndev_dev(ndev), "SBAR01 %#018llx\n", bar_addr);
Dave Jiang2f887b92015-05-20 12:55:47 -04002276 iowrite64(bar_addr, mmio + XEON_SBAR0BASE_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002277
2278 /* Other SBAR are normally hit by the PBAR xlat, except for b2b bar.
2279 * The b2b bar is either disabled above, or configured half-size, and
2280 * it starts at the PBAR xlat + offset.
2281 */
2282
2283 bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
Dave Jiang2f887b92015-05-20 12:55:47 -04002284 iowrite64(bar_addr, mmio + XEON_SBAR23BASE_OFFSET);
2285 bar_addr = ioread64(mmio + XEON_SBAR23BASE_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002286 dev_dbg(ndev_dev(ndev), "SBAR23 %#018llx\n", bar_addr);
2287
2288 if (!ndev->bar4_split) {
2289 bar_addr = addr->bar4_addr64 +
2290 (b2b_bar == 4 ? ndev->b2b_off : 0);
Dave Jiang2f887b92015-05-20 12:55:47 -04002291 iowrite64(bar_addr, mmio + XEON_SBAR45BASE_OFFSET);
2292 bar_addr = ioread64(mmio + XEON_SBAR45BASE_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002293 dev_dbg(ndev_dev(ndev), "SBAR45 %#018llx\n", bar_addr);
2294 } else {
2295 bar_addr = addr->bar4_addr32 +
2296 (b2b_bar == 4 ? ndev->b2b_off : 0);
Dave Jiang2f887b92015-05-20 12:55:47 -04002297 iowrite32(bar_addr, mmio + XEON_SBAR4BASE_OFFSET);
2298 bar_addr = ioread32(mmio + XEON_SBAR4BASE_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002299 dev_dbg(ndev_dev(ndev), "SBAR4 %#010llx\n", bar_addr);
2300
2301 bar_addr = addr->bar5_addr32 +
2302 (b2b_bar == 5 ? ndev->b2b_off : 0);
Dave Jiang2f887b92015-05-20 12:55:47 -04002303 iowrite32(bar_addr, mmio + XEON_SBAR5BASE_OFFSET);
2304 bar_addr = ioread32(mmio + XEON_SBAR5BASE_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002305 dev_dbg(ndev_dev(ndev), "SBAR5 %#010llx\n", bar_addr);
2306 }
2307
2308 /* setup incoming bar limits == base addrs (zero length windows) */
2309
2310 bar_addr = addr->bar2_addr64 + (b2b_bar == 2 ? ndev->b2b_off : 0);
Dave Jiang2f887b92015-05-20 12:55:47 -04002311 iowrite64(bar_addr, mmio + XEON_SBAR23LMT_OFFSET);
2312 bar_addr = ioread64(mmio + XEON_SBAR23LMT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002313 dev_dbg(ndev_dev(ndev), "SBAR23LMT %#018llx\n", bar_addr);
2314
2315 if (!ndev->bar4_split) {
2316 bar_addr = addr->bar4_addr64 +
2317 (b2b_bar == 4 ? ndev->b2b_off : 0);
Dave Jiang2f887b92015-05-20 12:55:47 -04002318 iowrite64(bar_addr, mmio + XEON_SBAR45LMT_OFFSET);
2319 bar_addr = ioread64(mmio + XEON_SBAR45LMT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002320 dev_dbg(ndev_dev(ndev), "SBAR45LMT %#018llx\n", bar_addr);
2321 } else {
2322 bar_addr = addr->bar4_addr32 +
2323 (b2b_bar == 4 ? ndev->b2b_off : 0);
Dave Jiang2f887b92015-05-20 12:55:47 -04002324 iowrite32(bar_addr, mmio + XEON_SBAR4LMT_OFFSET);
2325 bar_addr = ioread32(mmio + XEON_SBAR4LMT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002326 dev_dbg(ndev_dev(ndev), "SBAR4LMT %#010llx\n", bar_addr);
2327
2328 bar_addr = addr->bar5_addr32 +
2329 (b2b_bar == 5 ? ndev->b2b_off : 0);
Dave Jiang2f887b92015-05-20 12:55:47 -04002330 iowrite32(bar_addr, mmio + XEON_SBAR5LMT_OFFSET);
2331 bar_addr = ioread32(mmio + XEON_SBAR5LMT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002332 dev_dbg(ndev_dev(ndev), "SBAR5LMT %#05llx\n", bar_addr);
2333 }
2334
2335 /* zero incoming translation addrs */
Dave Jiang2f887b92015-05-20 12:55:47 -04002336 iowrite64(0, mmio + XEON_SBAR23XLAT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002337
2338 if (!ndev->bar4_split) {
Dave Jiang2f887b92015-05-20 12:55:47 -04002339 iowrite64(0, mmio + XEON_SBAR45XLAT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002340 } else {
Dave Jiang2f887b92015-05-20 12:55:47 -04002341 iowrite32(0, mmio + XEON_SBAR4XLAT_OFFSET);
2342 iowrite32(0, mmio + XEON_SBAR5XLAT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002343 }
2344
2345 /* zero outgoing translation limits (whole bar size windows) */
Dave Jiang2f887b92015-05-20 12:55:47 -04002346 iowrite64(0, mmio + XEON_PBAR23LMT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002347 if (!ndev->bar4_split) {
Dave Jiang2f887b92015-05-20 12:55:47 -04002348 iowrite64(0, mmio + XEON_PBAR45LMT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002349 } else {
Dave Jiang2f887b92015-05-20 12:55:47 -04002350 iowrite32(0, mmio + XEON_PBAR4LMT_OFFSET);
2351 iowrite32(0, mmio + XEON_PBAR5LMT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002352 }
2353
2354 /* set outgoing translation offsets */
2355 bar_addr = peer_addr->bar2_addr64;
Dave Jiang2f887b92015-05-20 12:55:47 -04002356 iowrite64(bar_addr, mmio + XEON_PBAR23XLAT_OFFSET);
2357 bar_addr = ioread64(mmio + XEON_PBAR23XLAT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002358 dev_dbg(ndev_dev(ndev), "PBAR23XLAT %#018llx\n", bar_addr);
2359
2360 if (!ndev->bar4_split) {
2361 bar_addr = peer_addr->bar4_addr64;
Dave Jiang2f887b92015-05-20 12:55:47 -04002362 iowrite64(bar_addr, mmio + XEON_PBAR45XLAT_OFFSET);
2363 bar_addr = ioread64(mmio + XEON_PBAR45XLAT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002364 dev_dbg(ndev_dev(ndev), "PBAR45XLAT %#018llx\n", bar_addr);
2365 } else {
2366 bar_addr = peer_addr->bar4_addr32;
Dave Jiang2f887b92015-05-20 12:55:47 -04002367 iowrite32(bar_addr, mmio + XEON_PBAR4XLAT_OFFSET);
2368 bar_addr = ioread32(mmio + XEON_PBAR4XLAT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002369 dev_dbg(ndev_dev(ndev), "PBAR4XLAT %#010llx\n", bar_addr);
2370
2371 bar_addr = peer_addr->bar5_addr32;
Dave Jiang2f887b92015-05-20 12:55:47 -04002372 iowrite32(bar_addr, mmio + XEON_PBAR5XLAT_OFFSET);
2373 bar_addr = ioread32(mmio + XEON_PBAR5XLAT_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002374 dev_dbg(ndev_dev(ndev), "PBAR5XLAT %#010llx\n", bar_addr);
2375 }
2376
2377 /* set the translation offset for b2b registers */
2378 if (b2b_bar == 0)
2379 bar_addr = peer_addr->bar0_addr;
2380 else if (b2b_bar == 2)
2381 bar_addr = peer_addr->bar2_addr64;
2382 else if (b2b_bar == 4 && !ndev->bar4_split)
2383 bar_addr = peer_addr->bar4_addr64;
2384 else if (b2b_bar == 4)
2385 bar_addr = peer_addr->bar4_addr32;
2386 else if (b2b_bar == 5)
2387 bar_addr = peer_addr->bar5_addr32;
2388 else
2389 return -EIO;
2390
2391 /* B2B_XLAT_OFFSET is 64bit, but can only take 32bit writes */
2392 dev_dbg(ndev_dev(ndev), "B2BXLAT %#018llx\n", bar_addr);
Dave Jiang2f887b92015-05-20 12:55:47 -04002393 iowrite32(bar_addr, mmio + XEON_B2B_XLAT_OFFSETL);
2394 iowrite32(bar_addr >> 32, mmio + XEON_B2B_XLAT_OFFSETU);
Allen Hubbee26a5842015-04-09 10:33:20 -04002395
2396 if (b2b_bar) {
2397 /* map peer ntb mmio config space registers */
2398 ndev->peer_mmio = pci_iomap(pdev, b2b_bar,
Dave Jiang2f887b92015-05-20 12:55:47 -04002399 XEON_B2B_MIN_SIZE);
Allen Hubbee26a5842015-04-09 10:33:20 -04002400 if (!ndev->peer_mmio)
2401 return -EIO;
Dave Jiang25ea9f22016-10-27 11:06:44 -07002402
2403 ndev->peer_addr = pci_resource_start(pdev, b2b_bar);
Allen Hubbee26a5842015-04-09 10:33:20 -04002404 }
2405
2406 return 0;
2407}
2408
Dave Jiang2f887b92015-05-20 12:55:47 -04002409static int xeon_init_ntb(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04002410{
2411 int rc;
Dave Jiang5ae0beb2015-05-19 16:59:34 -04002412 u32 ntb_ctl;
Allen Hubbee26a5842015-04-09 10:33:20 -04002413
2414 if (ndev->bar4_split)
2415 ndev->mw_count = HSX_SPLIT_BAR_MW_COUNT;
2416 else
Dave Jiang2f887b92015-05-20 12:55:47 -04002417 ndev->mw_count = XEON_MW_COUNT;
Allen Hubbee26a5842015-04-09 10:33:20 -04002418
Dave Jiang2f887b92015-05-20 12:55:47 -04002419 ndev->spad_count = XEON_SPAD_COUNT;
2420 ndev->db_count = XEON_DB_COUNT;
2421 ndev->db_link_mask = XEON_DB_LINK_BIT;
Allen Hubbee26a5842015-04-09 10:33:20 -04002422
2423 switch (ndev->ntb.topo) {
2424 case NTB_TOPO_PRI:
2425 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
2426 dev_err(ndev_dev(ndev), "NTB Primary config disabled\n");
2427 return -EINVAL;
2428 }
Dave Jiang5ae0beb2015-05-19 16:59:34 -04002429
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);
2434
Allen Hubbee26a5842015-04-09 10:33:20 -04002435 /* use half the spads for the peer */
2436 ndev->spad_count >>= 1;
Dave Jiang2f887b92015-05-20 12:55:47 -04002437 ndev->self_reg = &xeon_pri_reg;
2438 ndev->peer_reg = &xeon_sec_reg;
2439 ndev->xlat_reg = &xeon_sec_xlat;
Allen Hubbee26a5842015-04-09 10:33:20 -04002440 break;
2441
2442 case NTB_TOPO_SEC:
2443 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
2444 dev_err(ndev_dev(ndev), "NTB Secondary config disabled\n");
2445 return -EINVAL;
2446 }
2447 /* use half the spads for the peer */
2448 ndev->spad_count >>= 1;
Dave Jiang2f887b92015-05-20 12:55:47 -04002449 ndev->self_reg = &xeon_sec_reg;
2450 ndev->peer_reg = &xeon_pri_reg;
2451 ndev->xlat_reg = &xeon_pri_xlat;
Allen Hubbee26a5842015-04-09 10:33:20 -04002452 break;
2453
2454 case NTB_TOPO_B2B_USD:
2455 case NTB_TOPO_B2B_DSD:
Dave Jiang2f887b92015-05-20 12:55:47 -04002456 ndev->self_reg = &xeon_pri_reg;
2457 ndev->peer_reg = &xeon_b2b_reg;
2458 ndev->xlat_reg = &xeon_sec_xlat;
Allen Hubbee26a5842015-04-09 10:33:20 -04002459
2460 if (ndev->hwerr_flags & NTB_HWERR_SDOORBELL_LOCKUP) {
Dave Jiang2f887b92015-05-20 12:55:47 -04002461 ndev->peer_reg = &xeon_pri_reg;
Allen Hubbee26a5842015-04-09 10:33:20 -04002462
2463 if (b2b_mw_idx < 0)
2464 ndev->b2b_idx = b2b_mw_idx + ndev->mw_count;
2465 else
2466 ndev->b2b_idx = b2b_mw_idx;
2467
Allen Hubbe2aa2a77a2015-08-31 09:30:59 -04002468 if (ndev->b2b_idx >= ndev->mw_count) {
2469 dev_dbg(ndev_dev(ndev),
2470 "b2b_mw_idx %d invalid for mw_count %u\n",
2471 b2b_mw_idx, ndev->mw_count);
2472 return -EINVAL;
2473 }
2474
Allen Hubbee26a5842015-04-09 10:33:20 -04002475 dev_dbg(ndev_dev(ndev),
2476 "setting up b2b mw idx %d means %d\n",
2477 b2b_mw_idx, ndev->b2b_idx);
2478
2479 } else if (ndev->hwerr_flags & NTB_HWERR_B2BDOORBELL_BIT14) {
2480 dev_warn(ndev_dev(ndev), "Reduce doorbell count by 1\n");
2481 ndev->db_count -= 1;
2482 }
2483
2484 if (ndev->ntb.topo == NTB_TOPO_B2B_USD) {
Dave Jiang2f887b92015-05-20 12:55:47 -04002485 rc = xeon_setup_b2b_mw(ndev,
2486 &xeon_b2b_dsd_addr,
2487 &xeon_b2b_usd_addr);
Allen Hubbee26a5842015-04-09 10:33:20 -04002488 } else {
Dave Jiang2f887b92015-05-20 12:55:47 -04002489 rc = xeon_setup_b2b_mw(ndev,
2490 &xeon_b2b_usd_addr,
2491 &xeon_b2b_dsd_addr);
Allen Hubbee26a5842015-04-09 10:33:20 -04002492 }
2493 if (rc)
2494 return rc;
2495
2496 /* Enable Bus Master and Memory Space on the secondary side */
2497 iowrite16(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
Dave Jiang2f887b92015-05-20 12:55:47 -04002498 ndev->self_mmio + XEON_SPCICMD_OFFSET);
Allen Hubbee26a5842015-04-09 10:33:20 -04002499
2500 break;
2501
2502 default:
2503 return -EINVAL;
2504 }
2505
2506 ndev->db_valid_mask = BIT_ULL(ndev->db_count) - 1;
2507
2508 ndev->reg->db_iowrite(ndev->db_valid_mask,
2509 ndev->self_mmio +
2510 ndev->self_reg->db_mask);
2511
2512 return 0;
2513}
2514
Dave Jiang2f887b92015-05-20 12:55:47 -04002515static int xeon_init_dev(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04002516{
2517 struct pci_dev *pdev;
2518 u8 ppd;
2519 int rc, mem;
2520
Dave Jiangdd5d4d82015-05-08 12:24:40 -04002521 pdev = ndev_pdev(ndev);
2522
2523 switch (pdev->device) {
Allen Hubbee26a5842015-04-09 10:33:20 -04002524 /* There is a Xeon hardware errata related to writes to SDOORBELL or
2525 * B2BDOORBELL in conjunction with inbound access to NTB MMIO Space,
2526 * which may hang the system. To workaround this use the second memory
2527 * window to access the interrupt and scratch pad registers on the
2528 * remote system.
2529 */
Dave Jiangdd5d4d82015-05-08 12:24:40 -04002530 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
2531 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
2532 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
2533 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
2534 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
2535 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
2536 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
2537 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
2538 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
2539 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
2540 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
2541 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
Dave Jiang0a5d19d2015-07-13 08:07:18 -04002542 case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
2543 case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
2544 case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
Dave Jiangdd5d4d82015-05-08 12:24:40 -04002545 ndev->hwerr_flags |= NTB_HWERR_SDOORBELL_LOCKUP;
2546 break;
2547 }
Allen Hubbee26a5842015-04-09 10:33:20 -04002548
Dave Jiangdd5d4d82015-05-08 12:24:40 -04002549 switch (pdev->device) {
Allen Hubbee26a5842015-04-09 10:33:20 -04002550 /* There is a hardware errata related to accessing any register in
2551 * SB01BASE in the presence of bidirectional traffic crossing the NTB.
2552 */
Dave Jiangdd5d4d82015-05-08 12:24:40 -04002553 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
2554 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
2555 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
2556 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
2557 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
2558 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
Dave Jiang0a5d19d2015-07-13 08:07:18 -04002559 case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
2560 case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
2561 case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
Dave Jiangdd5d4d82015-05-08 12:24:40 -04002562 ndev->hwerr_flags |= NTB_HWERR_SB01BASE_LOCKUP;
2563 break;
2564 }
Allen Hubbee26a5842015-04-09 10:33:20 -04002565
Dave Jiangdd5d4d82015-05-08 12:24:40 -04002566 switch (pdev->device) {
Allen Hubbee26a5842015-04-09 10:33:20 -04002567 /* HW Errata on bit 14 of b2bdoorbell register. Writes will not be
2568 * mirrored to the remote system. Shrink the number of bits by one,
2569 * since bit 14 is the last bit.
2570 */
Dave Jiangdd5d4d82015-05-08 12:24:40 -04002571 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
2572 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
2573 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
2574 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
2575 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
2576 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
2577 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
2578 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
2579 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
2580 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
2581 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
2582 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
Dave Jiang0a5d19d2015-07-13 08:07:18 -04002583 case PCI_DEVICE_ID_INTEL_NTB_SS_BDX:
2584 case PCI_DEVICE_ID_INTEL_NTB_PS_BDX:
2585 case PCI_DEVICE_ID_INTEL_NTB_B2B_BDX:
Dave Jiangdd5d4d82015-05-08 12:24:40 -04002586 ndev->hwerr_flags |= NTB_HWERR_B2BDOORBELL_BIT14;
2587 break;
2588 }
Allen Hubbee26a5842015-04-09 10:33:20 -04002589
Dave Jiang2f887b92015-05-20 12:55:47 -04002590 ndev->reg = &xeon_reg;
Allen Hubbee26a5842015-04-09 10:33:20 -04002591
Dave Jiang2f887b92015-05-20 12:55:47 -04002592 rc = pci_read_config_byte(pdev, XEON_PPD_OFFSET, &ppd);
Allen Hubbee26a5842015-04-09 10:33:20 -04002593 if (rc)
2594 return -EIO;
2595
Dave Jiang2f887b92015-05-20 12:55:47 -04002596 ndev->ntb.topo = xeon_ppd_topo(ndev, ppd);
Allen Hubbee26a5842015-04-09 10:33:20 -04002597 dev_dbg(ndev_dev(ndev), "ppd %#x topo %s\n", ppd,
2598 ntb_topo_string(ndev->ntb.topo));
2599 if (ndev->ntb.topo == NTB_TOPO_NONE)
2600 return -EINVAL;
2601
2602 if (ndev->ntb.topo != NTB_TOPO_SEC) {
Dave Jiang2f887b92015-05-20 12:55:47 -04002603 ndev->bar4_split = xeon_ppd_bar4_split(ndev, ppd);
Allen Hubbee26a5842015-04-09 10:33:20 -04002604 dev_dbg(ndev_dev(ndev), "ppd %#x bar4_split %d\n",
2605 ppd, ndev->bar4_split);
2606 } else {
2607 /* This is a way for transparent BAR to figure out if we are
2608 * doing split BAR or not. There is no way for the hw on the
2609 * transparent side to know and set the PPD.
2610 */
2611 mem = pci_select_bars(pdev, IORESOURCE_MEM);
2612 ndev->bar4_split = hweight32(mem) ==
2613 HSX_SPLIT_BAR_MW_COUNT + 1;
2614 dev_dbg(ndev_dev(ndev), "mem %#x bar4_split %d\n",
2615 mem, ndev->bar4_split);
2616 }
2617
Dave Jiang2f887b92015-05-20 12:55:47 -04002618 rc = xeon_init_ntb(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04002619 if (rc)
2620 return rc;
2621
Dave Jiang2f887b92015-05-20 12:55:47 -04002622 return xeon_init_isr(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04002623}
2624
Dave Jiang2f887b92015-05-20 12:55:47 -04002625static void xeon_deinit_dev(struct intel_ntb_dev *ndev)
Allen Hubbee26a5842015-04-09 10:33:20 -04002626{
Dave Jiang2f887b92015-05-20 12:55:47 -04002627 xeon_deinit_isr(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04002628}
2629
2630static int intel_ntb_init_pci(struct intel_ntb_dev *ndev, struct pci_dev *pdev)
2631{
2632 int rc;
2633
2634 pci_set_drvdata(pdev, ndev);
2635
2636 rc = pci_enable_device(pdev);
2637 if (rc)
2638 goto err_pci_enable;
2639
2640 rc = pci_request_regions(pdev, NTB_NAME);
2641 if (rc)
2642 goto err_pci_regions;
2643
2644 pci_set_master(pdev);
2645
2646 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
2647 if (rc) {
2648 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
2649 if (rc)
2650 goto err_dma_mask;
2651 dev_warn(ndev_dev(ndev), "Cannot DMA highmem\n");
2652 }
2653
2654 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
2655 if (rc) {
2656 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
2657 if (rc)
2658 goto err_dma_mask;
2659 dev_warn(ndev_dev(ndev), "Cannot DMA consistent highmem\n");
2660 }
2661
2662 ndev->self_mmio = pci_iomap(pdev, 0, 0);
2663 if (!ndev->self_mmio) {
2664 rc = -EIO;
2665 goto err_mmio;
2666 }
2667 ndev->peer_mmio = ndev->self_mmio;
Dave Jiang25ea9f22016-10-27 11:06:44 -07002668 ndev->peer_addr = pci_resource_start(pdev, 0);
Allen Hubbee26a5842015-04-09 10:33:20 -04002669
2670 return 0;
2671
2672err_mmio:
2673err_dma_mask:
2674 pci_clear_master(pdev);
2675 pci_release_regions(pdev);
2676err_pci_regions:
2677 pci_disable_device(pdev);
2678err_pci_enable:
2679 pci_set_drvdata(pdev, NULL);
2680 return rc;
2681}
2682
2683static void intel_ntb_deinit_pci(struct intel_ntb_dev *ndev)
2684{
2685 struct pci_dev *pdev = ndev_pdev(ndev);
2686
2687 if (ndev->peer_mmio && ndev->peer_mmio != ndev->self_mmio)
2688 pci_iounmap(pdev, ndev->peer_mmio);
2689 pci_iounmap(pdev, ndev->self_mmio);
2690
2691 pci_clear_master(pdev);
2692 pci_release_regions(pdev);
2693 pci_disable_device(pdev);
2694 pci_set_drvdata(pdev, NULL);
2695}
2696
2697static inline void ndev_init_struct(struct intel_ntb_dev *ndev,
2698 struct pci_dev *pdev)
2699{
2700 ndev->ntb.pdev = pdev;
2701 ndev->ntb.topo = NTB_TOPO_NONE;
2702 ndev->ntb.ops = &intel_ntb_ops;
2703
2704 ndev->b2b_off = 0;
Allen Hubbe2aa2a77a2015-08-31 09:30:59 -04002705 ndev->b2b_idx = UINT_MAX;
Allen Hubbee26a5842015-04-09 10:33:20 -04002706
2707 ndev->bar4_split = 0;
2708
2709 ndev->mw_count = 0;
2710 ndev->spad_count = 0;
2711 ndev->db_count = 0;
2712 ndev->db_vec_count = 0;
2713 ndev->db_vec_shift = 0;
2714
2715 ndev->ntb_ctl = 0;
2716 ndev->lnk_sta = 0;
2717
2718 ndev->db_valid_mask = 0;
2719 ndev->db_link_mask = 0;
2720 ndev->db_mask = 0;
2721
2722 spin_lock_init(&ndev->db_mask_lock);
2723}
2724
2725static int intel_ntb_pci_probe(struct pci_dev *pdev,
2726 const struct pci_device_id *id)
2727{
2728 struct intel_ntb_dev *ndev;
Allen Hubbe0e041fb2015-05-19 12:04:52 -04002729 int rc, node;
2730
2731 node = dev_to_node(&pdev->dev);
Allen Hubbee26a5842015-04-09 10:33:20 -04002732
Dave Jiang2f887b92015-05-20 12:55:47 -04002733 if (pdev_is_atom(pdev)) {
Allen Hubbe0e041fb2015-05-19 12:04:52 -04002734 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04002735 if (!ndev) {
2736 rc = -ENOMEM;
2737 goto err_ndev;
2738 }
2739
2740 ndev_init_struct(ndev, pdev);
2741
2742 rc = intel_ntb_init_pci(ndev, pdev);
2743 if (rc)
2744 goto err_init_pci;
2745
Dave Jiang2f887b92015-05-20 12:55:47 -04002746 rc = atom_init_dev(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04002747 if (rc)
2748 goto err_init_dev;
2749
Dave Jiang2f887b92015-05-20 12:55:47 -04002750 } else if (pdev_is_xeon(pdev)) {
Allen Hubbe0e041fb2015-05-19 12:04:52 -04002751 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
Allen Hubbee26a5842015-04-09 10:33:20 -04002752 if (!ndev) {
2753 rc = -ENOMEM;
2754 goto err_ndev;
2755 }
2756
2757 ndev_init_struct(ndev, pdev);
2758
2759 rc = intel_ntb_init_pci(ndev, pdev);
2760 if (rc)
2761 goto err_init_pci;
2762
Dave Jiang2f887b92015-05-20 12:55:47 -04002763 rc = xeon_init_dev(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04002764 if (rc)
2765 goto err_init_dev;
2766
Dave Jiang783dfa62016-11-16 14:03:38 -07002767 } else if (pdev_is_skx_xeon(pdev)) {
2768 ndev = kzalloc_node(sizeof(*ndev), GFP_KERNEL, node);
2769 if (!ndev) {
2770 rc = -ENOMEM;
2771 goto err_ndev;
2772 }
2773
2774 ndev_init_struct(ndev, pdev);
2775 ndev->ntb.ops = &intel_ntb3_ops;
2776
2777 rc = intel_ntb_init_pci(ndev, pdev);
2778 if (rc)
2779 goto err_init_pci;
2780
2781 rc = skx_init_dev(ndev);
2782 if (rc)
2783 goto err_init_dev;
2784
Allen Hubbee26a5842015-04-09 10:33:20 -04002785 } else {
2786 rc = -EINVAL;
2787 goto err_ndev;
2788 }
2789
2790 ndev_reset_unsafe_flags(ndev);
2791
2792 ndev->reg->poll_link(ndev);
2793
2794 ndev_init_debugfs(ndev);
2795
2796 rc = ntb_register_device(&ndev->ntb);
2797 if (rc)
2798 goto err_register;
2799
Dave Jiang7eb38782015-06-15 08:21:33 -04002800 dev_info(&pdev->dev, "NTB device registered.\n");
2801
Allen Hubbee26a5842015-04-09 10:33:20 -04002802 return 0;
2803
2804err_register:
2805 ndev_deinit_debugfs(ndev);
Dave Jiang2f887b92015-05-20 12:55:47 -04002806 if (pdev_is_atom(pdev))
2807 atom_deinit_dev(ndev);
Dave Jiang783dfa62016-11-16 14:03:38 -07002808 else if (pdev_is_xeon(pdev) || pdev_is_skx_xeon(pdev))
Dave Jiang2f887b92015-05-20 12:55:47 -04002809 xeon_deinit_dev(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04002810err_init_dev:
2811 intel_ntb_deinit_pci(ndev);
2812err_init_pci:
2813 kfree(ndev);
2814err_ndev:
2815 return rc;
2816}
2817
2818static void intel_ntb_pci_remove(struct pci_dev *pdev)
2819{
2820 struct intel_ntb_dev *ndev = pci_get_drvdata(pdev);
2821
2822 ntb_unregister_device(&ndev->ntb);
2823 ndev_deinit_debugfs(ndev);
Dave Jiang2f887b92015-05-20 12:55:47 -04002824 if (pdev_is_atom(pdev))
2825 atom_deinit_dev(ndev);
Dave Jiang783dfa62016-11-16 14:03:38 -07002826 else if (pdev_is_xeon(pdev) || pdev_is_skx_xeon(pdev))
Dave Jiang2f887b92015-05-20 12:55:47 -04002827 xeon_deinit_dev(ndev);
Allen Hubbee26a5842015-04-09 10:33:20 -04002828 intel_ntb_deinit_pci(ndev);
2829 kfree(ndev);
2830}
2831
Dave Jiang2f887b92015-05-20 12:55:47 -04002832static const struct intel_ntb_reg atom_reg = {
2833 .poll_link = atom_poll_link,
2834 .link_is_up = atom_link_is_up,
2835 .db_ioread = atom_db_ioread,
2836 .db_iowrite = atom_db_iowrite,
Allen Hubbee26a5842015-04-09 10:33:20 -04002837 .db_size = sizeof(u64),
Dave Jiang2f887b92015-05-20 12:55:47 -04002838 .ntb_ctl = ATOM_NTBCNTL_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002839 .mw_bar = {2, 4},
2840};
2841
Dave Jiang2f887b92015-05-20 12:55:47 -04002842static const struct intel_ntb_alt_reg atom_pri_reg = {
2843 .db_bell = ATOM_PDOORBELL_OFFSET,
2844 .db_mask = ATOM_PDBMSK_OFFSET,
2845 .spad = ATOM_SPAD_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002846};
2847
Dave Jiang2f887b92015-05-20 12:55:47 -04002848static const struct intel_ntb_alt_reg atom_b2b_reg = {
2849 .db_bell = ATOM_B2B_DOORBELL_OFFSET,
2850 .spad = ATOM_B2B_SPAD_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002851};
2852
Dave Jiang2f887b92015-05-20 12:55:47 -04002853static const struct intel_ntb_xlat_reg atom_sec_xlat = {
2854 /* FIXME : .bar0_base = ATOM_SBAR0BASE_OFFSET, */
2855 /* FIXME : .bar2_limit = ATOM_SBAR2LMT_OFFSET, */
2856 .bar2_xlat = ATOM_SBAR2XLAT_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002857};
2858
Dave Jiang2f887b92015-05-20 12:55:47 -04002859static const struct intel_ntb_reg xeon_reg = {
2860 .poll_link = xeon_poll_link,
2861 .link_is_up = xeon_link_is_up,
2862 .db_ioread = xeon_db_ioread,
2863 .db_iowrite = xeon_db_iowrite,
Allen Hubbee26a5842015-04-09 10:33:20 -04002864 .db_size = sizeof(u32),
Dave Jiang2f887b92015-05-20 12:55:47 -04002865 .ntb_ctl = XEON_NTBCNTL_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002866 .mw_bar = {2, 4, 5},
2867};
2868
Dave Jiang2f887b92015-05-20 12:55:47 -04002869static const struct intel_ntb_alt_reg xeon_pri_reg = {
2870 .db_bell = XEON_PDOORBELL_OFFSET,
2871 .db_mask = XEON_PDBMSK_OFFSET,
2872 .spad = XEON_SPAD_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002873};
2874
Dave Jiang2f887b92015-05-20 12:55:47 -04002875static const struct intel_ntb_alt_reg xeon_sec_reg = {
2876 .db_bell = XEON_SDOORBELL_OFFSET,
2877 .db_mask = XEON_SDBMSK_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002878 /* second half of the scratchpads */
Dave Jiang2f887b92015-05-20 12:55:47 -04002879 .spad = XEON_SPAD_OFFSET + (XEON_SPAD_COUNT << 1),
Allen Hubbee26a5842015-04-09 10:33:20 -04002880};
2881
Dave Jiang2f887b92015-05-20 12:55:47 -04002882static const struct intel_ntb_alt_reg xeon_b2b_reg = {
2883 .db_bell = XEON_B2B_DOORBELL_OFFSET,
2884 .spad = XEON_B2B_SPAD_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002885};
2886
Dave Jiang2f887b92015-05-20 12:55:47 -04002887static const struct intel_ntb_xlat_reg xeon_pri_xlat = {
Allen Hubbee26a5842015-04-09 10:33:20 -04002888 /* Note: no primary .bar0_base visible to the secondary side.
2889 *
2890 * The secondary side cannot get the base address stored in primary
2891 * bars. The base address is necessary to set the limit register to
2892 * any value other than zero, or unlimited.
2893 *
2894 * WITHOUT THE BASE ADDRESS, THE SECONDARY SIDE CANNOT DISABLE the
2895 * window by setting the limit equal to base, nor can it limit the size
2896 * of the memory window by setting the limit to base + size.
2897 */
Dave Jiang2f887b92015-05-20 12:55:47 -04002898 .bar2_limit = XEON_PBAR23LMT_OFFSET,
2899 .bar2_xlat = XEON_PBAR23XLAT_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002900};
2901
Dave Jiang2f887b92015-05-20 12:55:47 -04002902static const struct intel_ntb_xlat_reg xeon_sec_xlat = {
2903 .bar0_base = XEON_SBAR0BASE_OFFSET,
2904 .bar2_limit = XEON_SBAR23LMT_OFFSET,
2905 .bar2_xlat = XEON_SBAR23XLAT_OFFSET,
Allen Hubbee26a5842015-04-09 10:33:20 -04002906};
2907
Dave Jiang2f887b92015-05-20 12:55:47 -04002908static struct intel_b2b_addr xeon_b2b_usd_addr = {
Dave Jiang8b782fa2015-09-24 13:03:05 -07002909 .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
2910 .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
2911 .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
2912 .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
Allen Hubbee26a5842015-04-09 10:33:20 -04002913};
2914
Dave Jiang2f887b92015-05-20 12:55:47 -04002915static struct intel_b2b_addr xeon_b2b_dsd_addr = {
Dave Jiang8b782fa2015-09-24 13:03:05 -07002916 .bar2_addr64 = XEON_B2B_BAR2_ADDR64,
2917 .bar4_addr64 = XEON_B2B_BAR4_ADDR64,
2918 .bar4_addr32 = XEON_B2B_BAR4_ADDR32,
2919 .bar5_addr32 = XEON_B2B_BAR5_ADDR32,
Allen Hubbee26a5842015-04-09 10:33:20 -04002920};
2921
Dave Jiang783dfa62016-11-16 14:03:38 -07002922static const struct intel_ntb_reg skx_reg = {
Dave Jiang939ada52017-02-16 16:22:36 -07002923 .poll_link = skx_poll_link,
Dave Jiang783dfa62016-11-16 14:03:38 -07002924 .link_is_up = xeon_link_is_up,
2925 .db_ioread = skx_db_ioread,
2926 .db_iowrite = skx_db_iowrite,
Dave Jiang5eb449e2017-06-08 12:46:45 -07002927 .db_size = sizeof(u32),
Dave Jiang783dfa62016-11-16 14:03:38 -07002928 .ntb_ctl = SKX_NTBCNTL_OFFSET,
2929 .mw_bar = {2, 4},
2930};
2931
2932static const struct intel_ntb_alt_reg skx_pri_reg = {
2933 .db_bell = SKX_EM_DOORBELL_OFFSET,
2934 .db_clear = SKX_IM_INT_STATUS_OFFSET,
2935 .db_mask = SKX_IM_INT_DISABLE_OFFSET,
2936 .spad = SKX_IM_SPAD_OFFSET,
2937};
2938
2939static const struct intel_ntb_alt_reg skx_b2b_reg = {
2940 .db_bell = SKX_IM_DOORBELL_OFFSET,
2941 .db_clear = SKX_EM_INT_STATUS_OFFSET,
2942 .db_mask = SKX_EM_INT_DISABLE_OFFSET,
2943 .spad = SKX_B2B_SPAD_OFFSET,
2944};
2945
2946static const struct intel_ntb_xlat_reg skx_sec_xlat = {
2947/* .bar0_base = SKX_EMBAR0_OFFSET, */
2948 .bar2_limit = SKX_IMBAR1XLMT_OFFSET,
2949 .bar2_xlat = SKX_IMBAR1XBASE_OFFSET,
2950};
2951
Allen Hubbee26a5842015-04-09 10:33:20 -04002952/* operations for primary side of local ntb */
2953static const struct ntb_dev_ops intel_ntb_ops = {
2954 .mw_count = intel_ntb_mw_count,
Serge Semin443b9a12017-01-11 03:11:33 +03002955 .mw_get_align = intel_ntb_mw_get_align,
Allen Hubbee26a5842015-04-09 10:33:20 -04002956 .mw_set_trans = intel_ntb_mw_set_trans,
Serge Semin443b9a12017-01-11 03:11:33 +03002957 .peer_mw_count = intel_ntb_peer_mw_count,
2958 .peer_mw_get_addr = intel_ntb_peer_mw_get_addr,
Allen Hubbee26a5842015-04-09 10:33:20 -04002959 .link_is_up = intel_ntb_link_is_up,
2960 .link_enable = intel_ntb_link_enable,
2961 .link_disable = intel_ntb_link_disable,
2962 .db_is_unsafe = intel_ntb_db_is_unsafe,
2963 .db_valid_mask = intel_ntb_db_valid_mask,
2964 .db_vector_count = intel_ntb_db_vector_count,
2965 .db_vector_mask = intel_ntb_db_vector_mask,
2966 .db_read = intel_ntb_db_read,
2967 .db_clear = intel_ntb_db_clear,
2968 .db_set_mask = intel_ntb_db_set_mask,
2969 .db_clear_mask = intel_ntb_db_clear_mask,
2970 .peer_db_addr = intel_ntb_peer_db_addr,
2971 .peer_db_set = intel_ntb_peer_db_set,
2972 .spad_is_unsafe = intel_ntb_spad_is_unsafe,
2973 .spad_count = intel_ntb_spad_count,
2974 .spad_read = intel_ntb_spad_read,
2975 .spad_write = intel_ntb_spad_write,
2976 .peer_spad_addr = intel_ntb_peer_spad_addr,
2977 .peer_spad_read = intel_ntb_peer_spad_read,
2978 .peer_spad_write = intel_ntb_peer_spad_write,
2979};
2980
Dave Jiang783dfa62016-11-16 14:03:38 -07002981static const struct ntb_dev_ops intel_ntb3_ops = {
2982 .mw_count = intel_ntb_mw_count,
Serge Semin443b9a12017-01-11 03:11:33 +03002983 .mw_get_align = intel_ntb_mw_get_align,
Dave Jiang783dfa62016-11-16 14:03:38 -07002984 .mw_set_trans = intel_ntb3_mw_set_trans,
Serge Semin443b9a12017-01-11 03:11:33 +03002985 .peer_mw_count = intel_ntb_peer_mw_count,
2986 .peer_mw_get_addr = intel_ntb_peer_mw_get_addr,
Dave Jiang783dfa62016-11-16 14:03:38 -07002987 .link_is_up = intel_ntb_link_is_up,
2988 .link_enable = intel_ntb3_link_enable,
2989 .link_disable = intel_ntb_link_disable,
2990 .db_valid_mask = intel_ntb_db_valid_mask,
2991 .db_vector_count = intel_ntb_db_vector_count,
2992 .db_vector_mask = intel_ntb_db_vector_mask,
2993 .db_read = intel_ntb3_db_read,
2994 .db_clear = intel_ntb3_db_clear,
2995 .db_set_mask = intel_ntb_db_set_mask,
2996 .db_clear_mask = intel_ntb_db_clear_mask,
2997 .peer_db_addr = intel_ntb_peer_db_addr,
2998 .peer_db_set = intel_ntb3_peer_db_set,
2999 .spad_is_unsafe = intel_ntb_spad_is_unsafe,
3000 .spad_count = intel_ntb_spad_count,
3001 .spad_read = intel_ntb_spad_read,
3002 .spad_write = intel_ntb_spad_write,
3003 .peer_spad_addr = intel_ntb_peer_spad_addr,
3004 .peer_spad_read = intel_ntb_peer_spad_read,
3005 .peer_spad_write = intel_ntb_peer_spad_write,
3006};
3007
Allen Hubbee26a5842015-04-09 10:33:20 -04003008static const struct file_operations intel_ntb_debugfs_info = {
3009 .owner = THIS_MODULE,
3010 .open = simple_open,
3011 .read = ndev_debugfs_read,
3012};
3013
3014static const struct pci_device_id intel_ntb_pci_tbl[] = {
Jon Masonfce8a7b2012-11-16 19:27:12 -07003015 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
3016 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
Jon Masonfce8a7b2012-11-16 19:27:12 -07003017 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
Jon Masonbe4dac02012-09-28 11:38:48 -07003018 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
3019 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
Dave Jiang0a5d19d2015-07-13 08:07:18 -04003020 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BDX)},
Jon Masonbe4dac02012-09-28 11:38:48 -07003021 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
3022 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
3023 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
3024 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
Dave Jiang0a5d19d2015-07-13 08:07:18 -04003025 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_BDX)},
Jon Masonbe4dac02012-09-28 11:38:48 -07003026 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
3027 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
3028 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
3029 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
Dave Jiang0a5d19d2015-07-13 08:07:18 -04003030 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_BDX)},
Dave Jiang783dfa62016-11-16 14:03:38 -07003031 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SKX)},
Jon Masonfce8a7b2012-11-16 19:27:12 -07003032 {0}
3033};
Allen Hubbee26a5842015-04-09 10:33:20 -04003034MODULE_DEVICE_TABLE(pci, intel_ntb_pci_tbl);
Jon Masonfce8a7b2012-11-16 19:27:12 -07003035
Allen Hubbee26a5842015-04-09 10:33:20 -04003036static struct pci_driver intel_ntb_pci_driver = {
3037 .name = KBUILD_MODNAME,
3038 .id_table = intel_ntb_pci_tbl,
3039 .probe = intel_ntb_pci_probe,
3040 .remove = intel_ntb_pci_remove,
Jon Mason6465d022014-04-07 10:55:47 -07003041};
3042
Allen Hubbee26a5842015-04-09 10:33:20 -04003043static int __init intel_ntb_pci_driver_init(void)
Jon Mason1517a3f2013-07-30 15:58:49 -07003044{
Dave Jiang7eb38782015-06-15 08:21:33 -04003045 pr_info("%s %s\n", NTB_DESC, NTB_VER);
3046
Allen Hubbee26a5842015-04-09 10:33:20 -04003047 if (debugfs_initialized())
Jon Mason1517a3f2013-07-30 15:58:49 -07003048 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
3049
Allen Hubbee26a5842015-04-09 10:33:20 -04003050 return pci_register_driver(&intel_ntb_pci_driver);
Jon Mason1517a3f2013-07-30 15:58:49 -07003051}
Allen Hubbee26a5842015-04-09 10:33:20 -04003052module_init(intel_ntb_pci_driver_init);
Jon Mason1517a3f2013-07-30 15:58:49 -07003053
Allen Hubbee26a5842015-04-09 10:33:20 -04003054static void __exit intel_ntb_pci_driver_exit(void)
Jon Mason1517a3f2013-07-30 15:58:49 -07003055{
Allen Hubbee26a5842015-04-09 10:33:20 -04003056 pci_unregister_driver(&intel_ntb_pci_driver);
Jon Mason1517a3f2013-07-30 15:58:49 -07003057
Allen Hubbee26a5842015-04-09 10:33:20 -04003058 debugfs_remove_recursive(debugfs_dir);
Jon Mason1517a3f2013-07-30 15:58:49 -07003059}
Allen Hubbee26a5842015-04-09 10:33:20 -04003060module_exit(intel_ntb_pci_driver_exit);
Jon Mason1517a3f2013-07-30 15:58:49 -07003061