2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
15 * Copyright(c) 2012 Intel Corporation. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
21 * * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * * Redistributions in binary form must reproduce the above copy
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
27 * * Neither the name of Intel Corporation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * Intel PCIe NTB Linux driver
45 * Contact Information:
46 * Jon Mason <jon.mason@intel.com>
48 #include <linux/debugfs.h>
49 #include <linux/delay.h>
50 #include <linux/init.h>
51 #include <linux/interrupt.h>
52 #include <linux/module.h>
53 #include <linux/pci.h>
54 #include <linux/random.h>
55 #include <linux/slab.h>
59 #define NTB_NAME "Intel(R) PCI-E Non-Transparent Bridge Driver"
62 MODULE_DESCRIPTION(NTB_NAME);
63 MODULE_VERSION(NTB_VER);
64 MODULE_LICENSE("Dual BSD/GPL");
65 MODULE_AUTHOR("Intel Corporation");
67 static bool xeon_errata_workaround = true;
68 module_param(xeon_errata_workaround, bool, 0644);
69 MODULE_PARM_DESC(xeon_errata_workaround, "Workaround for the Xeon Errata");
72 NTB_CONN_TRANSPARENT = 0,
87 static struct dentry *debugfs_dir;
89 #define BWD_LINK_RECOVERY_TIME 500
91 /* Translate memory window 0,1 to BAR 2,4 */
92 #define MW_TO_BAR(mw) (mw * NTB_MAX_NUM_MW + 2)
94 static const struct pci_device_id ntb_pci_tbl[] = {
95 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
96 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
97 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
98 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_IVT)},
99 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_HSX)},
100 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_JSF)},
101 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_SNB)},
102 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_IVT)},
103 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_PS_HSX)},
104 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_JSF)},
105 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_SNB)},
106 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_IVT)},
107 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_SS_HSX)},
110 MODULE_DEVICE_TABLE(pci, ntb_pci_tbl);
112 static int is_ntb_xeon(struct ntb_device *ndev)
114 switch (ndev->pdev->device) {
115 case PCI_DEVICE_ID_INTEL_NTB_SS_JSF:
116 case PCI_DEVICE_ID_INTEL_NTB_SS_SNB:
117 case PCI_DEVICE_ID_INTEL_NTB_SS_IVT:
118 case PCI_DEVICE_ID_INTEL_NTB_SS_HSX:
119 case PCI_DEVICE_ID_INTEL_NTB_PS_JSF:
120 case PCI_DEVICE_ID_INTEL_NTB_PS_SNB:
121 case PCI_DEVICE_ID_INTEL_NTB_PS_IVT:
122 case PCI_DEVICE_ID_INTEL_NTB_PS_HSX:
123 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
124 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
125 case PCI_DEVICE_ID_INTEL_NTB_B2B_IVT:
126 case PCI_DEVICE_ID_INTEL_NTB_B2B_HSX:
135 static int is_ntb_atom(struct ntb_device *ndev)
137 switch (ndev->pdev->device) {
138 case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
148 * ntb_register_event_callback() - register event callback
149 * @ndev: pointer to ntb_device instance
150 * @func: callback function to register
152 * This function registers a callback for any HW driver events such as link
153 * up/down, power management notices and etc.
155 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
157 int ntb_register_event_callback(struct ntb_device *ndev,
158 void (*func)(void *handle,
159 enum ntb_hw_event event))
164 ndev->event_cb = func;
170 * ntb_unregister_event_callback() - unregisters the event callback
171 * @ndev: pointer to ntb_device instance
173 * This function unregisters the existing callback from transport
175 void ntb_unregister_event_callback(struct ntb_device *ndev)
177 ndev->event_cb = NULL;
180 static void ntb_irq_work(unsigned long data)
182 struct ntb_db_cb *db_cb = (struct ntb_db_cb *)data;
185 rc = db_cb->callback(db_cb->data, db_cb->db_num);
187 tasklet_schedule(&db_cb->irq_work);
189 struct ntb_device *ndev = db_cb->ndev;
192 mask = readw(ndev->reg_ofs.ldb_mask);
193 clear_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
194 writew(mask, ndev->reg_ofs.ldb_mask);
199 * ntb_register_db_callback() - register a callback for doorbell interrupt
200 * @ndev: pointer to ntb_device instance
201 * @idx: doorbell index to register callback, zero based
202 * @data: pointer to be returned to caller with every callback
203 * @func: callback function to register
205 * This function registers a callback function for the doorbell interrupt
206 * on the primary side. The function will unmask the doorbell as well to
209 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
211 int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
212 void *data, int (*func)(void *data, int db_num))
216 if (idx >= ndev->max_cbs || ndev->db_cb[idx].callback) {
217 dev_warn(&ndev->pdev->dev, "Invalid Index.\n");
221 ndev->db_cb[idx].callback = func;
222 ndev->db_cb[idx].data = data;
223 ndev->db_cb[idx].ndev = ndev;
225 tasklet_init(&ndev->db_cb[idx].irq_work, ntb_irq_work,
226 (unsigned long) &ndev->db_cb[idx]);
228 /* unmask interrupt */
229 mask = readw(ndev->reg_ofs.ldb_mask);
230 clear_bit(idx * ndev->bits_per_vector, &mask);
231 writew(mask, ndev->reg_ofs.ldb_mask);
237 * ntb_unregister_db_callback() - unregister a callback for doorbell interrupt
238 * @ndev: pointer to ntb_device instance
239 * @idx: doorbell index to register callback, zero based
241 * This function unregisters a callback function for the doorbell interrupt
242 * on the primary side. The function will also mask the said doorbell.
244 void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx)
248 if (idx >= ndev->max_cbs || !ndev->db_cb[idx].callback)
251 mask = readw(ndev->reg_ofs.ldb_mask);
252 set_bit(idx * ndev->bits_per_vector, &mask);
253 writew(mask, ndev->reg_ofs.ldb_mask);
255 tasklet_disable(&ndev->db_cb[idx].irq_work);
257 ndev->db_cb[idx].callback = NULL;
261 * ntb_find_transport() - find the transport pointer
262 * @transport: pointer to pci device
264 * Given the pci device pointer, return the transport pointer passed in when
265 * the transport attached when it was inited.
267 * RETURNS: pointer to transport.
269 void *ntb_find_transport(struct pci_dev *pdev)
271 struct ntb_device *ndev = pci_get_drvdata(pdev);
272 return ndev->ntb_transport;
276 * ntb_register_transport() - Register NTB transport with NTB HW driver
277 * @transport: transport identifier
279 * This function allows a transport to reserve the hardware driver for
282 * RETURNS: pointer to ntb_device, NULL on error.
284 struct ntb_device *ntb_register_transport(struct pci_dev *pdev, void *transport)
286 struct ntb_device *ndev = pci_get_drvdata(pdev);
288 if (ndev->ntb_transport)
291 ndev->ntb_transport = transport;
296 * ntb_unregister_transport() - Unregister the transport with the NTB HW driver
297 * @ndev - ntb_device of the transport to be freed
299 * This function unregisters the transport from the HW driver and performs any
300 * necessary cleanups.
302 void ntb_unregister_transport(struct ntb_device *ndev)
306 if (!ndev->ntb_transport)
309 for (i = 0; i < ndev->max_cbs; i++)
310 ntb_unregister_db_callback(ndev, i);
312 ntb_unregister_event_callback(ndev);
313 ndev->ntb_transport = NULL;
317 * ntb_write_local_spad() - write to the secondary scratchpad register
318 * @ndev: pointer to ntb_device instance
319 * @idx: index to the scratchpad register, 0 based
320 * @val: the data value to put into the register
322 * This function allows writing of a 32bit value to the indexed scratchpad
323 * register. This writes over the data mirrored to the local scratchpad register
324 * by the remote system.
326 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
328 int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
330 if (idx >= ndev->limits.max_spads)
333 dev_dbg(&ndev->pdev->dev, "Writing %x to local scratch pad index %d\n",
335 writel(val, ndev->reg_ofs.spad_read + idx * 4);
341 * ntb_read_local_spad() - read from the primary scratchpad register
342 * @ndev: pointer to ntb_device instance
343 * @idx: index to scratchpad register, 0 based
344 * @val: pointer to 32bit integer for storing the register value
346 * This function allows reading of the 32bit scratchpad register on
347 * the primary (internal) side. This allows the local system to read data
348 * written and mirrored to the scratchpad register by the remote system.
350 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
352 int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
354 if (idx >= ndev->limits.max_spads)
357 *val = readl(ndev->reg_ofs.spad_write + idx * 4);
358 dev_dbg(&ndev->pdev->dev,
359 "Reading %x from local scratch pad index %d\n", *val, idx);
365 * ntb_write_remote_spad() - write to the secondary scratchpad register
366 * @ndev: pointer to ntb_device instance
367 * @idx: index to the scratchpad register, 0 based
368 * @val: the data value to put into the register
370 * This function allows writing of a 32bit value to the indexed scratchpad
371 * register. The register resides on the secondary (external) side. This allows
372 * the local system to write data to be mirrored to the remote systems
373 * scratchpad register.
375 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
377 int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
379 if (idx >= ndev->limits.max_spads)
382 dev_dbg(&ndev->pdev->dev, "Writing %x to remote scratch pad index %d\n",
384 writel(val, ndev->reg_ofs.spad_write + idx * 4);
390 * ntb_read_remote_spad() - read from the primary scratchpad register
391 * @ndev: pointer to ntb_device instance
392 * @idx: index to scratchpad register, 0 based
393 * @val: pointer to 32bit integer for storing the register value
395 * This function allows reading of the 32bit scratchpad register on
396 * the primary (internal) side. This alloows the local system to read the data
397 * it wrote to be mirrored on the remote system.
399 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
401 int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
403 if (idx >= ndev->limits.max_spads)
406 *val = readl(ndev->reg_ofs.spad_read + idx * 4);
407 dev_dbg(&ndev->pdev->dev,
408 "Reading %x from remote scratch pad index %d\n", *val, idx);
414 * ntb_get_mw_base() - get addr for the NTB memory window
415 * @ndev: pointer to ntb_device instance
416 * @mw: memory window number
418 * This function provides the base address of the memory window specified.
420 * RETURNS: address, or NULL on error.
422 resource_size_t ntb_get_mw_base(struct ntb_device *ndev, unsigned int mw)
424 if (mw >= ntb_max_mw(ndev))
427 return pci_resource_start(ndev->pdev, MW_TO_BAR(mw));
431 * ntb_get_mw_vbase() - get virtual addr for the NTB memory window
432 * @ndev: pointer to ntb_device instance
433 * @mw: memory window number
435 * This function provides the base virtual address of the memory window
438 * RETURNS: pointer to virtual address, or NULL on error.
440 void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
442 if (mw >= ntb_max_mw(ndev))
445 return ndev->mw[mw].vbase;
449 * ntb_get_mw_size() - return size of NTB memory window
450 * @ndev: pointer to ntb_device instance
451 * @mw: memory window number
453 * This function provides the physical size of the memory window specified
455 * RETURNS: the size of the memory window or zero on error
457 u64 ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
459 if (mw >= ntb_max_mw(ndev))
462 return ndev->mw[mw].bar_sz;
466 * ntb_set_mw_addr - set the memory window address
467 * @ndev: pointer to ntb_device instance
468 * @mw: memory window number
469 * @addr: base address for data
471 * This function sets the base physical address of the memory window. This
472 * memory address is where data from the remote system will be transfered into
473 * or out of depending on how the transport is configured.
475 void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
477 if (mw >= ntb_max_mw(ndev))
480 dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
483 ndev->mw[mw].phys_addr = addr;
485 switch (MW_TO_BAR(mw)) {
487 writeq(addr, ndev->reg_ofs.bar2_xlat);
490 writeq(addr, ndev->reg_ofs.bar4_xlat);
496 * ntb_ring_doorbell() - Set the doorbell on the secondary/external side
497 * @ndev: pointer to ntb_device instance
498 * @db: doorbell to ring
500 * This function allows triggering of a doorbell on the secondary/external
501 * side that will initiate an interrupt on the remote host
503 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
505 void ntb_ring_doorbell(struct ntb_device *ndev, unsigned int db)
507 dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
509 if (ndev->hw_type == BWD_HW)
510 writeq((u64) 1 << db, ndev->reg_ofs.rdb);
512 writew(((1 << ndev->bits_per_vector) - 1) <<
513 (db * ndev->bits_per_vector), ndev->reg_ofs.rdb);
516 static void bwd_recover_link(struct ntb_device *ndev)
520 /* Driver resets the NTB ModPhy lanes - magic! */
521 writeb(0xe0, ndev->reg_base + BWD_MODPHY_PCSREG6);
522 writeb(0x40, ndev->reg_base + BWD_MODPHY_PCSREG4);
523 writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG4);
524 writeb(0x60, ndev->reg_base + BWD_MODPHY_PCSREG6);
526 /* Driver waits 100ms to allow the NTB ModPhy to settle */
529 /* Clear AER Errors, write to clear */
530 status = readl(ndev->reg_base + BWD_ERRCORSTS_OFFSET);
531 dev_dbg(&ndev->pdev->dev, "ERRCORSTS = %x\n", status);
532 status &= PCI_ERR_COR_REP_ROLL;
533 writel(status, ndev->reg_base + BWD_ERRCORSTS_OFFSET);
535 /* Clear unexpected electrical idle event in LTSSM, write to clear */
536 status = readl(ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
537 dev_dbg(&ndev->pdev->dev, "LTSSMERRSTS0 = %x\n", status);
538 status |= BWD_LTSSMERRSTS0_UNEXPECTEDEI;
539 writel(status, ndev->reg_base + BWD_LTSSMERRSTS0_OFFSET);
541 /* Clear DeSkew Buffer error, write to clear */
542 status = readl(ndev->reg_base + BWD_DESKEWSTS_OFFSET);
543 dev_dbg(&ndev->pdev->dev, "DESKEWSTS = %x\n", status);
544 status |= BWD_DESKEWSTS_DBERR;
545 writel(status, ndev->reg_base + BWD_DESKEWSTS_OFFSET);
547 status = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
548 dev_dbg(&ndev->pdev->dev, "IBSTERRRCRVSTS0 = %x\n", status);
549 status &= BWD_IBIST_ERR_OFLOW;
550 writel(status, ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
552 /* Releases the NTB state machine to allow the link to retrain */
553 status = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
554 dev_dbg(&ndev->pdev->dev, "LTSSMSTATEJMP = %x\n", status);
555 status &= ~BWD_LTSSMSTATEJMP_FORCEDETECT;
556 writel(status, ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
559 static void ntb_link_event(struct ntb_device *ndev, int link_state)
563 if (ndev->link_status == link_state)
566 if (link_state == NTB_LINK_UP) {
569 dev_info(&ndev->pdev->dev, "Link Up\n");
570 ndev->link_status = NTB_LINK_UP;
571 event = NTB_EVENT_HW_LINK_UP;
573 if (is_ntb_atom(ndev) ||
574 ndev->conn_type == NTB_CONN_TRANSPARENT)
575 status = readw(ndev->reg_ofs.lnk_stat);
577 int rc = pci_read_config_word(ndev->pdev,
578 SNB_LINK_STATUS_OFFSET,
584 ndev->link_width = (status & NTB_LINK_WIDTH_MASK) >> 4;
585 ndev->link_speed = (status & NTB_LINK_SPEED_MASK);
586 dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
587 ndev->link_width, ndev->link_speed);
589 dev_info(&ndev->pdev->dev, "Link Down\n");
590 ndev->link_status = NTB_LINK_DOWN;
591 event = NTB_EVENT_HW_LINK_DOWN;
592 /* Don't modify link width/speed, we need it in link recovery */
595 /* notify the upper layer if we have an event change */
597 ndev->event_cb(ndev->ntb_transport, event);
600 static int ntb_link_status(struct ntb_device *ndev)
604 if (is_ntb_atom(ndev)) {
607 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
608 if (ntb_cntl & BWD_CNTL_LINK_DOWN)
609 link_state = NTB_LINK_DOWN;
611 link_state = NTB_LINK_UP;
616 rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
621 if (status & NTB_LINK_STATUS_ACTIVE)
622 link_state = NTB_LINK_UP;
624 link_state = NTB_LINK_DOWN;
627 ntb_link_event(ndev, link_state);
632 static void bwd_link_recovery(struct work_struct *work)
634 struct ntb_device *ndev = container_of(work, struct ntb_device,
638 bwd_recover_link(ndev);
639 /* There is a potential race between the 2 NTB devices recovering at the
640 * same time. If the times are the same, the link will not recover and
641 * the driver will be stuck in this loop forever. Add a random interval
642 * to the recovery time to prevent this race.
644 msleep(BWD_LINK_RECOVERY_TIME + prandom_u32() % BWD_LINK_RECOVERY_TIME);
646 status32 = readl(ndev->reg_base + BWD_LTSSMSTATEJMP_OFFSET);
647 if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT)
650 status32 = readl(ndev->reg_base + BWD_IBSTERRRCRVSTS0_OFFSET);
651 if (status32 & BWD_IBIST_ERR_OFLOW)
654 status32 = readl(ndev->reg_ofs.lnk_cntl);
655 if (!(status32 & BWD_CNTL_LINK_DOWN)) {
656 unsigned char speed, width;
659 status16 = readw(ndev->reg_ofs.lnk_stat);
660 width = (status16 & NTB_LINK_WIDTH_MASK) >> 4;
661 speed = (status16 & NTB_LINK_SPEED_MASK);
662 if (ndev->link_width != width || ndev->link_speed != speed)
666 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
670 schedule_delayed_work(&ndev->lr_timer, NTB_HB_TIMEOUT);
673 /* BWD doesn't have link status interrupt, poll on that platform */
674 static void bwd_link_poll(struct work_struct *work)
676 struct ntb_device *ndev = container_of(work, struct ntb_device,
678 unsigned long ts = jiffies;
680 /* If we haven't gotten an interrupt in a while, check the BWD link
683 if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
684 int rc = ntb_link_status(ndev);
686 dev_err(&ndev->pdev->dev,
687 "Error determining link status\n");
689 /* Check to see if a link error is the cause of the link down */
690 if (ndev->link_status == NTB_LINK_DOWN) {
691 u32 status32 = readl(ndev->reg_base +
692 BWD_LTSSMSTATEJMP_OFFSET);
693 if (status32 & BWD_LTSSMSTATEJMP_FORCEDETECT) {
694 schedule_delayed_work(&ndev->lr_timer, 0);
700 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
703 static int ntb_xeon_setup(struct ntb_device *ndev)
705 switch (ndev->conn_type) {
707 ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
708 ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
709 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
710 ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
711 ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
712 ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
714 /* There is a Xeon hardware errata related to writes to
715 * SDOORBELL or B2BDOORBELL in conjunction with inbound access
716 * to NTB MMIO Space, which may hang the system. To workaround
717 * this use the second memory window to access the interrupt and
718 * scratch pad registers on the remote system.
720 if (xeon_errata_workaround) {
721 if (!ndev->mw[1].bar_sz)
724 ndev->limits.max_mw = SNB_ERRATA_MAX_MW;
725 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
726 ndev->reg_ofs.spad_write = ndev->mw[1].vbase +
728 ndev->reg_ofs.rdb = ndev->mw[1].vbase +
729 SNB_PDOORBELL_OFFSET;
731 /* Set the Limit register to 4k, the minimum size, to
732 * prevent an illegal access
734 writeq(ndev->mw[1].bar_sz + 0x1000, ndev->reg_base +
735 SNB_PBAR4LMT_OFFSET);
736 /* HW errata on the Limit registers. They can only be
737 * written when the base register is 4GB aligned and
738 * < 32bit. This should already be the case based on
739 * the driver defaults, but write the Limit registers
740 * first just in case.
743 ndev->limits.max_mw = SNB_MAX_MW;
745 /* HW Errata on bit 14 of b2bdoorbell register. Writes
746 * will not be mirrored to the remote system. Shrink
747 * the number of bits by one, since bit 14 is the last
750 ndev->limits.max_db_bits = SNB_MAX_DB_BITS - 1;
751 ndev->reg_ofs.spad_write = ndev->reg_base +
753 ndev->reg_ofs.rdb = ndev->reg_base +
754 SNB_B2B_DOORBELL_OFFSET;
756 /* Disable the Limit register, just incase it is set to
759 writeq(0, ndev->reg_base + SNB_PBAR4LMT_OFFSET);
760 /* HW errata on the Limit registers. They can only be
761 * written when the base register is 4GB aligned and
762 * < 32bit. This should already be the case based on
763 * the driver defaults, but write the Limit registers
764 * first just in case.
768 /* The Xeon errata workaround requires setting SBAR Base
769 * addresses to known values, so that the PBAR XLAT can be
770 * pointed at SBAR0 of the remote system.
772 if (ndev->dev_type == NTB_DEV_USD) {
773 writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
774 SNB_PBAR2XLAT_OFFSET);
775 if (xeon_errata_workaround)
776 writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
777 SNB_PBAR4XLAT_OFFSET);
779 writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
780 SNB_PBAR4XLAT_OFFSET);
781 /* B2B_XLAT_OFFSET is a 64bit register, but can
782 * only take 32bit writes
784 writel(SNB_MBAR01_DSD_ADDR & 0xffffffff,
785 ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
786 writel(SNB_MBAR01_DSD_ADDR >> 32,
787 ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
790 writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
791 SNB_SBAR0BASE_OFFSET);
792 writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
793 SNB_SBAR2BASE_OFFSET);
794 writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
795 SNB_SBAR4BASE_OFFSET);
797 writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
798 SNB_PBAR2XLAT_OFFSET);
799 if (xeon_errata_workaround)
800 writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
801 SNB_PBAR4XLAT_OFFSET);
803 writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
804 SNB_PBAR4XLAT_OFFSET);
805 /* B2B_XLAT_OFFSET is a 64bit register, but can
806 * only take 32bit writes
808 writel(SNB_MBAR01_USD_ADDR & 0xffffffff,
809 ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
810 writel(SNB_MBAR01_USD_ADDR >> 32,
811 ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
813 writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
814 SNB_SBAR0BASE_OFFSET);
815 writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
816 SNB_SBAR2BASE_OFFSET);
817 writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
818 SNB_SBAR4BASE_OFFSET);
822 if (xeon_errata_workaround) {
823 dev_err(&ndev->pdev->dev,
824 "NTB-RP disabled due to hardware errata. To disregard this warning and potentially lock-up the system, add the parameter 'xeon_errata_workaround=0'.\n");
828 /* Scratch pads need to have exclusive access from the primary
829 * or secondary side. Halve the num spads so that each side can
830 * have an equal amount.
832 ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
833 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
834 /* Note: The SDOORBELL is the cause of the errata. You REALLY
835 * don't want to touch it.
837 ndev->reg_ofs.rdb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
838 ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
839 ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
840 /* Offset the start of the spads to correspond to whether it is
841 * primary or secondary
843 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET +
844 ndev->limits.max_spads * 4;
845 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
846 ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
847 ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
848 ndev->limits.max_mw = SNB_MAX_MW;
850 case NTB_CONN_TRANSPARENT:
851 /* Scratch pads need to have exclusive access from the primary
852 * or secondary side. Halve the num spads so that each side can
853 * have an equal amount.
855 ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
856 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
857 ndev->reg_ofs.rdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
858 ndev->reg_ofs.ldb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
859 ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_SDBMSK_OFFSET;
860 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET;
861 /* Offset the start of the spads to correspond to whether it is
862 * primary or secondary
864 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET +
865 ndev->limits.max_spads * 4;
866 ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_PBAR2XLAT_OFFSET;
867 ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_PBAR4XLAT_OFFSET;
869 ndev->limits.max_mw = SNB_MAX_MW;
873 * we should never hit this. the detect function should've
874 * take cared of everything.
879 ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
880 ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_SLINK_STATUS_OFFSET;
881 ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
883 ndev->limits.msix_cnt = SNB_MSIX_CNT;
884 ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
889 static int ntb_bwd_setup(struct ntb_device *ndev)
894 ndev->hw_type = BWD_HW;
896 rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
900 switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
902 ndev->conn_type = NTB_CONN_B2B;
906 dev_err(&ndev->pdev->dev, "Unsupported NTB configuration\n");
910 if (val & BWD_PPD_DEV_TYPE)
911 ndev->dev_type = NTB_DEV_DSD;
913 ndev->dev_type = NTB_DEV_USD;
915 /* Initiate PCI-E link training */
916 rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
917 val | BWD_PPD_INIT_LINK);
921 ndev->reg_ofs.ldb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
922 ndev->reg_ofs.ldb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
923 ndev->reg_ofs.rdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
924 ndev->reg_ofs.bar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
925 ndev->reg_ofs.bar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
926 ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
927 ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
928 ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
929 ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
930 ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
931 ndev->limits.max_mw = BWD_MAX_MW;
932 ndev->limits.max_spads = BWD_MAX_SPADS;
933 ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
934 ndev->limits.msix_cnt = BWD_MSIX_CNT;
935 ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
937 /* Since bwd doesn't have a link interrupt, setup a poll timer */
938 INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
939 INIT_DELAYED_WORK(&ndev->lr_timer, bwd_link_recovery);
940 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
945 static int ntb_device_setup(struct ntb_device *ndev)
949 if (is_ntb_xeon(ndev))
950 rc = ntb_xeon_setup(ndev);
951 else if (is_ntb_atom(ndev))
952 rc = ntb_bwd_setup(ndev);
959 if (ndev->conn_type == NTB_CONN_B2B)
960 /* Enable Bus Master and Memory Space on the secondary side */
961 writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
962 ndev->reg_ofs.spci_cmd);
967 static void ntb_device_free(struct ntb_device *ndev)
969 if (is_ntb_atom(ndev)) {
970 cancel_delayed_work_sync(&ndev->hb_timer);
971 cancel_delayed_work_sync(&ndev->lr_timer);
975 static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
977 struct ntb_db_cb *db_cb = data;
978 struct ntb_device *ndev = db_cb->ndev;
981 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
984 mask = readw(ndev->reg_ofs.ldb_mask);
985 set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
986 writew(mask, ndev->reg_ofs.ldb_mask);
988 tasklet_schedule(&db_cb->irq_work);
990 /* No need to check for the specific HB irq, any interrupt means
993 ndev->last_ts = jiffies;
995 writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.ldb);
1000 static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
1002 struct ntb_db_cb *db_cb = data;
1003 struct ntb_device *ndev = db_cb->ndev;
1006 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
1009 mask = readw(ndev->reg_ofs.ldb_mask);
1010 set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
1011 writew(mask, ndev->reg_ofs.ldb_mask);
1013 tasklet_schedule(&db_cb->irq_work);
1015 /* On Sandybridge, there are 16 bits in the interrupt register
1016 * but only 4 vectors. So, 5 bits are assigned to the first 3
1017 * vectors, with the 4th having a single bit for link
1020 writew(((1 << ndev->bits_per_vector) - 1) <<
1021 (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.ldb);
1026 /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
1027 static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
1029 struct ntb_device *ndev = dev;
1032 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
1034 rc = ntb_link_status(ndev);
1036 dev_err(&ndev->pdev->dev, "Error determining link status\n");
1038 /* bit 15 is always the link bit */
1039 writew(1 << SNB_LINK_DB, ndev->reg_ofs.ldb);
1044 static irqreturn_t ntb_interrupt(int irq, void *dev)
1046 struct ntb_device *ndev = dev;
1049 if (is_ntb_atom(ndev)) {
1050 u64 ldb = readq(ndev->reg_ofs.ldb);
1052 dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %Lx\n", irq, ldb);
1057 bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
1060 u16 ldb = readw(ndev->reg_ofs.ldb);
1062 dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %x\n", irq, ldb);
1064 if (ldb & SNB_DB_HW_LINK) {
1065 xeon_event_msix_irq(irq, dev);
1066 ldb &= ~SNB_DB_HW_LINK;
1072 xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
1079 static int ntb_setup_snb_msix(struct ntb_device *ndev, int msix_entries)
1081 struct pci_dev *pdev = ndev->pdev;
1082 struct msix_entry *msix;
1085 if (msix_entries < ndev->limits.msix_cnt)
1088 rc = pci_enable_msix_exact(pdev, ndev->msix_entries, msix_entries);
1092 for (i = 0; i < msix_entries; i++) {
1093 msix = &ndev->msix_entries[i];
1094 WARN_ON(!msix->vector);
1096 if (i == msix_entries - 1) {
1097 rc = request_irq(msix->vector,
1098 xeon_event_msix_irq, 0,
1099 "ntb-event-msix", ndev);
1103 rc = request_irq(msix->vector,
1104 xeon_callback_msix_irq, 0,
1105 "ntb-callback-msix",
1112 ndev->num_msix = msix_entries;
1113 ndev->max_cbs = msix_entries - 1;
1119 /* Code never reaches here for entry nr 'ndev->num_msix - 1' */
1120 msix = &ndev->msix_entries[i];
1121 free_irq(msix->vector, &ndev->db_cb[i]);
1124 pci_disable_msix(pdev);
1130 static int ntb_setup_bwd_msix(struct ntb_device *ndev, int msix_entries)
1132 struct pci_dev *pdev = ndev->pdev;
1133 struct msix_entry *msix;
1136 msix_entries = pci_enable_msix_range(pdev, ndev->msix_entries,
1138 if (msix_entries < 0)
1139 return msix_entries;
1141 for (i = 0; i < msix_entries; i++) {
1142 msix = &ndev->msix_entries[i];
1143 WARN_ON(!msix->vector);
1145 rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
1146 "ntb-callback-msix", &ndev->db_cb[i]);
1151 ndev->num_msix = msix_entries;
1152 ndev->max_cbs = msix_entries;
1158 free_irq(msix->vector, &ndev->db_cb[i]);
1160 pci_disable_msix(pdev);
1166 static int ntb_setup_msix(struct ntb_device *ndev)
1168 struct pci_dev *pdev = ndev->pdev;
1172 msix_entries = pci_msix_vec_count(pdev);
1173 if (msix_entries < 0) {
1176 } else if (msix_entries > ndev->limits.msix_cnt) {
1181 ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
1183 if (!ndev->msix_entries) {
1188 for (i = 0; i < msix_entries; i++)
1189 ndev->msix_entries[i].entry = i;
1191 if (is_ntb_atom(ndev))
1192 rc = ntb_setup_bwd_msix(ndev, msix_entries);
1194 rc = ntb_setup_snb_msix(ndev, msix_entries);
1201 kfree(ndev->msix_entries);
1203 dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
1207 static int ntb_setup_msi(struct ntb_device *ndev)
1209 struct pci_dev *pdev = ndev->pdev;
1212 rc = pci_enable_msi(pdev);
1216 rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
1218 pci_disable_msi(pdev);
1219 dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
1226 static int ntb_setup_intx(struct ntb_device *ndev)
1228 struct pci_dev *pdev = ndev->pdev;
1233 /* Verify intx is enabled */
1236 rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
1244 static int ntb_setup_interrupts(struct ntb_device *ndev)
1248 /* On BWD, disable all interrupts. On SNB, disable all but Link
1249 * Interrupt. The rest will be unmasked as callbacks are registered.
1251 if (is_ntb_atom(ndev))
1252 writeq(~0, ndev->reg_ofs.ldb_mask);
1254 u16 var = 1 << SNB_LINK_DB;
1255 writew(~var, ndev->reg_ofs.ldb_mask);
1258 rc = ntb_setup_msix(ndev);
1262 ndev->bits_per_vector = 1;
1263 ndev->max_cbs = ndev->limits.max_db_bits;
1265 rc = ntb_setup_msi(ndev);
1269 rc = ntb_setup_intx(ndev);
1271 dev_err(&ndev->pdev->dev, "no usable interrupts\n");
1279 static void ntb_free_interrupts(struct ntb_device *ndev)
1281 struct pci_dev *pdev = ndev->pdev;
1283 /* mask interrupts */
1284 if (is_ntb_atom(ndev))
1285 writeq(~0, ndev->reg_ofs.ldb_mask);
1287 writew(~0, ndev->reg_ofs.ldb_mask);
1289 if (ndev->num_msix) {
1290 struct msix_entry *msix;
1293 for (i = 0; i < ndev->num_msix; i++) {
1294 msix = &ndev->msix_entries[i];
1295 if (is_ntb_xeon(ndev) && i == ndev->num_msix - 1)
1296 free_irq(msix->vector, ndev);
1298 free_irq(msix->vector, &ndev->db_cb[i]);
1300 pci_disable_msix(pdev);
1301 kfree(ndev->msix_entries);
1303 free_irq(pdev->irq, ndev);
1305 if (pci_dev_msi_enabled(pdev))
1306 pci_disable_msi(pdev);
1310 static int ntb_create_callbacks(struct ntb_device *ndev)
1314 /* Chicken-egg issue. We won't know how many callbacks are necessary
1315 * until we see how many MSI-X vectors we get, but these pointers need
1316 * to be passed into the MSI-X register function. So, we allocate the
1317 * max, knowing that they might not all be used, to work around this.
1319 ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
1320 sizeof(struct ntb_db_cb),
1325 for (i = 0; i < ndev->limits.max_db_bits; i++) {
1326 ndev->db_cb[i].db_num = i;
1327 ndev->db_cb[i].ndev = ndev;
1333 static void ntb_free_callbacks(struct ntb_device *ndev)
1337 for (i = 0; i < ndev->limits.max_db_bits; i++)
1338 ntb_unregister_db_callback(ndev, i);
1343 static ssize_t ntb_debugfs_read(struct file *filp, char __user *ubuf,
1344 size_t count, loff_t *offp)
1346 struct ntb_device *ndev;
1348 ssize_t ret, offset, out_count;
1352 buf = kmalloc(out_count, GFP_KERNEL);
1356 ndev = filp->private_data;
1358 offset += snprintf(buf + offset, out_count - offset,
1359 "NTB Device Information:\n");
1360 offset += snprintf(buf + offset, out_count - offset,
1361 "Connection Type - \t\t%s\n",
1362 ndev->conn_type == NTB_CONN_TRANSPARENT ?
1363 "Transparent" : (ndev->conn_type == NTB_CONN_B2B) ?
1364 "Back to back" : "Root Port");
1365 offset += snprintf(buf + offset, out_count - offset,
1366 "Device Type - \t\t\t%s\n",
1367 ndev->dev_type == NTB_DEV_USD ?
1368 "DSD/USP" : "USD/DSP");
1369 offset += snprintf(buf + offset, out_count - offset,
1370 "Max Number of Callbacks - \t%u\n",
1372 offset += snprintf(buf + offset, out_count - offset,
1373 "Link Status - \t\t\t%s\n",
1374 ntb_hw_link_status(ndev) ? "Up" : "Down");
1375 if (ntb_hw_link_status(ndev)) {
1376 offset += snprintf(buf + offset, out_count - offset,
1377 "Link Speed - \t\t\tPCI-E Gen %u\n",
1379 offset += snprintf(buf + offset, out_count - offset,
1380 "Link Width - \t\t\tx%u\n",
1384 if (is_ntb_xeon(ndev)) {
1389 offset += snprintf(buf + offset, out_count - offset,
1390 "\nNTB Device Statistics:\n");
1391 offset += snprintf(buf + offset, out_count - offset,
1392 "Upstream Memory Miss - \t%u\n",
1393 readw(ndev->reg_base +
1394 SNB_USMEMMISS_OFFSET));
1396 offset += snprintf(buf + offset, out_count - offset,
1397 "\nNTB Hardware Errors:\n");
1399 rc = pci_read_config_word(ndev->pdev, SNB_DEVSTS_OFFSET,
1402 offset += snprintf(buf + offset, out_count - offset,
1403 "DEVSTS - \t%#06x\n", status16);
1405 rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
1408 offset += snprintf(buf + offset, out_count - offset,
1409 "LNKSTS - \t%#06x\n", status16);
1411 rc = pci_read_config_dword(ndev->pdev, SNB_UNCERRSTS_OFFSET,
1414 offset += snprintf(buf + offset, out_count - offset,
1415 "UNCERRSTS - \t%#010x\n", status32);
1417 rc = pci_read_config_dword(ndev->pdev, SNB_CORERRSTS_OFFSET,
1420 offset += snprintf(buf + offset, out_count - offset,
1421 "CORERRSTS - \t%#010x\n", status32);
1424 if (offset > out_count)
1427 ret = simple_read_from_buffer(ubuf, count, offp, buf, offset);
1432 static const struct file_operations ntb_debugfs_info = {
1433 .owner = THIS_MODULE,
1434 .open = simple_open,
1435 .read = ntb_debugfs_read,
1438 static void ntb_setup_debugfs(struct ntb_device *ndev)
1440 if (!debugfs_initialized())
1444 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1446 ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
1448 if (ndev->debugfs_dir)
1449 ndev->debugfs_info = debugfs_create_file("info", S_IRUSR,
1455 static void ntb_free_debugfs(struct ntb_device *ndev)
1457 debugfs_remove_recursive(ndev->debugfs_dir);
1459 if (debugfs_dir && simple_empty(debugfs_dir)) {
1460 debugfs_remove_recursive(debugfs_dir);
1465 static void ntb_hw_link_up(struct ntb_device *ndev)
1467 if (ndev->conn_type == NTB_CONN_TRANSPARENT)
1468 ntb_link_event(ndev, NTB_LINK_UP);
1472 /* Let's bring the NTB link up */
1473 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
1474 ntb_cntl &= ~(NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK);
1475 ntb_cntl |= NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP;
1476 ntb_cntl |= NTB_CNTL_P2S_BAR45_SNOOP | NTB_CNTL_S2P_BAR45_SNOOP;
1477 writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
1481 static void ntb_hw_link_down(struct ntb_device *ndev)
1485 if (ndev->conn_type == NTB_CONN_TRANSPARENT) {
1486 ntb_link_event(ndev, NTB_LINK_DOWN);
1490 /* Bring NTB link down */
1491 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
1492 ntb_cntl &= ~(NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP);
1493 ntb_cntl &= ~(NTB_CNTL_P2S_BAR45_SNOOP | NTB_CNTL_S2P_BAR45_SNOOP);
1494 ntb_cntl |= NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK;
1495 writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
1498 static int ntb_xeon_detect(struct ntb_device *ndev)
1503 ndev->hw_type = SNB_HW;
1505 rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &ppd);
1509 if (ppd & SNB_PPD_DEV_TYPE)
1510 ndev->dev_type = NTB_DEV_USD;
1512 ndev->dev_type = NTB_DEV_DSD;
1514 switch (ppd & SNB_PPD_CONN_TYPE) {
1516 dev_info(&ndev->pdev->dev, "Conn Type = B2B\n");
1517 ndev->conn_type = NTB_CONN_B2B;
1520 dev_info(&ndev->pdev->dev, "Conn Type = RP\n");
1521 ndev->conn_type = NTB_CONN_RP;
1523 case NTB_CONN_TRANSPARENT:
1524 dev_info(&ndev->pdev->dev, "Conn Type = TRANSPARENT\n");
1525 ndev->conn_type = NTB_CONN_TRANSPARENT;
1527 * This mode is default to USD/DSP. HW does not report
1528 * properly in transparent mode as it has no knowledge of
1529 * NTB. We will just force correct here.
1531 ndev->dev_type = NTB_DEV_USD;
1534 dev_err(&ndev->pdev->dev, "Unknown PPD %x\n", ppd);
1541 static int ntb_atom_detect(struct ntb_device *ndev)
1546 ndev->hw_type = BWD_HW;
1548 rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &ppd);
1552 switch ((ppd & BWD_PPD_CONN_TYPE) >> 8) {
1554 dev_info(&ndev->pdev->dev, "Conn Type = B2B\n");
1555 ndev->conn_type = NTB_CONN_B2B;
1559 dev_err(&ndev->pdev->dev, "Unsupported NTB configuration\n");
1563 if (ppd & BWD_PPD_DEV_TYPE)
1564 ndev->dev_type = NTB_DEV_DSD;
1566 ndev->dev_type = NTB_DEV_USD;
1571 static int ntb_device_detect(struct ntb_device *ndev)
1575 if (is_ntb_xeon(ndev))
1576 rc = ntb_xeon_detect(ndev);
1577 else if (is_ntb_atom(ndev))
1578 rc = ntb_atom_detect(ndev);
1582 dev_info(&ndev->pdev->dev, "Device Type = %s\n",
1583 ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
1588 static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1590 struct ntb_device *ndev;
1593 ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
1598 ndev->link_status = NTB_LINK_DOWN;
1599 pci_set_drvdata(pdev, ndev);
1600 ntb_setup_debugfs(ndev);
1602 rc = pci_enable_device(pdev);
1606 pci_set_master(ndev->pdev);
1608 rc = ntb_device_detect(ndev);
1612 rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
1616 ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
1617 if (!ndev->reg_base) {
1618 dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
1623 for (i = 0; i < NTB_MAX_NUM_MW; i++) {
1624 ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
1626 ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
1627 ndev->mw[i].bar_sz);
1628 dev_info(&pdev->dev, "MW %d size %llu\n", i,
1629 (unsigned long long) ndev->mw[i].bar_sz);
1630 if (!ndev->mw[i].vbase) {
1631 dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
1638 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1640 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1644 dev_warn(&pdev->dev, "Cannot DMA highmem\n");
1647 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1649 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1653 dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
1656 rc = ntb_device_setup(ndev);
1660 rc = ntb_create_callbacks(ndev);
1664 rc = ntb_setup_interrupts(ndev);
1668 /* The scratchpad registers keep the values between rmmod/insmod,
1671 for (i = 0; i < ndev->limits.max_spads; i++) {
1672 ntb_write_local_spad(ndev, i, 0);
1673 ntb_write_remote_spad(ndev, i, 0);
1676 rc = ntb_transport_init(pdev);
1680 ntb_hw_link_up(ndev);
1685 ntb_free_interrupts(ndev);
1687 ntb_free_callbacks(ndev);
1689 ntb_device_free(ndev);
1691 for (i--; i >= 0; i--)
1692 iounmap(ndev->mw[i].vbase);
1693 iounmap(ndev->reg_base);
1695 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1697 pci_disable_device(pdev);
1699 ntb_free_debugfs(ndev);
1702 dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
1706 static void ntb_pci_remove(struct pci_dev *pdev)
1708 struct ntb_device *ndev = pci_get_drvdata(pdev);
1711 ntb_hw_link_down(ndev);
1713 ntb_transport_free(ndev->ntb_transport);
1715 ntb_free_interrupts(ndev);
1716 ntb_free_callbacks(ndev);
1717 ntb_device_free(ndev);
1719 for (i = 0; i < NTB_MAX_NUM_MW; i++)
1720 iounmap(ndev->mw[i].vbase);
1722 iounmap(ndev->reg_base);
1723 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1724 pci_disable_device(pdev);
1725 ntb_free_debugfs(ndev);
1729 static struct pci_driver ntb_pci_driver = {
1730 .name = KBUILD_MODNAME,
1731 .id_table = ntb_pci_tbl,
1732 .probe = ntb_pci_probe,
1733 .remove = ntb_pci_remove,
1736 module_pci_driver(ntb_pci_driver);