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