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)
708 ndev->hw_type = SNB_HW;
710 rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &val);
714 if (val & SNB_PPD_DEV_TYPE)
715 ndev->dev_type = NTB_DEV_USD;
717 ndev->dev_type = NTB_DEV_DSD;
719 switch (val & SNB_PPD_CONN_TYPE) {
721 dev_info(&ndev->pdev->dev, "Conn Type = B2B\n");
722 ndev->conn_type = NTB_CONN_B2B;
723 ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
724 ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
725 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
726 ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
727 ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
728 ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
730 /* There is a Xeon hardware errata related to writes to
731 * SDOORBELL or B2BDOORBELL in conjunction with inbound access
732 * to NTB MMIO Space, which may hang the system. To workaround
733 * this use the second memory window to access the interrupt and
734 * scratch pad registers on the remote system.
736 if (xeon_errata_workaround) {
737 if (!ndev->mw[1].bar_sz)
740 ndev->limits.max_mw = SNB_ERRATA_MAX_MW;
741 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
742 ndev->reg_ofs.spad_write = ndev->mw[1].vbase +
744 ndev->reg_ofs.rdb = ndev->mw[1].vbase +
745 SNB_PDOORBELL_OFFSET;
747 /* Set the Limit register to 4k, the minimum size, to
748 * prevent an illegal access
750 writeq(ndev->mw[1].bar_sz + 0x1000, ndev->reg_base +
751 SNB_PBAR4LMT_OFFSET);
752 /* HW errata on the Limit registers. They can only be
753 * written when the base register is 4GB aligned and
754 * < 32bit. This should already be the case based on
755 * the driver defaults, but write the Limit registers
756 * first just in case.
759 ndev->limits.max_mw = SNB_MAX_MW;
761 /* HW Errata on bit 14 of b2bdoorbell register. Writes
762 * will not be mirrored to the remote system. Shrink
763 * the number of bits by one, since bit 14 is the last
766 ndev->limits.max_db_bits = SNB_MAX_DB_BITS - 1;
767 ndev->reg_ofs.spad_write = ndev->reg_base +
769 ndev->reg_ofs.rdb = ndev->reg_base +
770 SNB_B2B_DOORBELL_OFFSET;
772 /* Disable the Limit register, just incase it is set to
775 writeq(0, ndev->reg_base + SNB_PBAR4LMT_OFFSET);
776 /* HW errata on the Limit registers. They can only be
777 * written when the base register is 4GB aligned and
778 * < 32bit. This should already be the case based on
779 * the driver defaults, but write the Limit registers
780 * first just in case.
784 /* The Xeon errata workaround requires setting SBAR Base
785 * addresses to known values, so that the PBAR XLAT can be
786 * pointed at SBAR0 of the remote system.
788 if (ndev->dev_type == NTB_DEV_USD) {
789 writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
790 SNB_PBAR2XLAT_OFFSET);
791 if (xeon_errata_workaround)
792 writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
793 SNB_PBAR4XLAT_OFFSET);
795 writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
796 SNB_PBAR4XLAT_OFFSET);
797 /* B2B_XLAT_OFFSET is a 64bit register, but can
798 * only take 32bit writes
800 writel(SNB_MBAR01_DSD_ADDR & 0xffffffff,
801 ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
802 writel(SNB_MBAR01_DSD_ADDR >> 32,
803 ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
806 writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
807 SNB_SBAR0BASE_OFFSET);
808 writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
809 SNB_SBAR2BASE_OFFSET);
810 writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
811 SNB_SBAR4BASE_OFFSET);
813 writeq(SNB_MBAR23_USD_ADDR, ndev->reg_base +
814 SNB_PBAR2XLAT_OFFSET);
815 if (xeon_errata_workaround)
816 writeq(SNB_MBAR01_USD_ADDR, ndev->reg_base +
817 SNB_PBAR4XLAT_OFFSET);
819 writeq(SNB_MBAR45_USD_ADDR, ndev->reg_base +
820 SNB_PBAR4XLAT_OFFSET);
821 /* B2B_XLAT_OFFSET is a 64bit register, but can
822 * only take 32bit writes
824 writel(SNB_MBAR01_USD_ADDR & 0xffffffff,
825 ndev->reg_base + SNB_B2B_XLAT_OFFSETL);
826 writel(SNB_MBAR01_USD_ADDR >> 32,
827 ndev->reg_base + SNB_B2B_XLAT_OFFSETU);
829 writeq(SNB_MBAR01_DSD_ADDR, ndev->reg_base +
830 SNB_SBAR0BASE_OFFSET);
831 writeq(SNB_MBAR23_DSD_ADDR, ndev->reg_base +
832 SNB_SBAR2BASE_OFFSET);
833 writeq(SNB_MBAR45_DSD_ADDR, ndev->reg_base +
834 SNB_SBAR4BASE_OFFSET);
838 dev_info(&ndev->pdev->dev, "Conn Type = RP\n");
839 ndev->conn_type = NTB_CONN_RP;
841 if (xeon_errata_workaround) {
842 dev_err(&ndev->pdev->dev,
843 "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");
847 /* Scratch pads need to have exclusive access from the primary
848 * or secondary side. Halve the num spads so that each side can
849 * have an equal amount.
851 ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
852 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
853 /* Note: The SDOORBELL is the cause of the errata. You REALLY
854 * don't want to touch it.
856 ndev->reg_ofs.rdb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
857 ndev->reg_ofs.ldb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
858 ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
859 /* Offset the start of the spads to correspond to whether it is
860 * primary or secondary
862 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET +
863 ndev->limits.max_spads * 4;
864 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
865 ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
866 ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
867 ndev->limits.max_mw = SNB_MAX_MW;
869 case NTB_CONN_TRANSPARENT:
870 dev_info(&ndev->pdev->dev, "Conn Type = TRANSPARENT\n");
871 ndev->conn_type = NTB_CONN_TRANSPARENT;
872 /* Scratch pads need to have exclusive access from the primary
873 * or secondary side. Halve the num spads so that each side can
874 * have an equal amount.
876 ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS / 2;
877 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
878 ndev->reg_ofs.rdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
879 ndev->reg_ofs.ldb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
880 ndev->reg_ofs.ldb_mask = ndev->reg_base + SNB_SDBMSK_OFFSET;
881 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET;
882 /* Offset the start of the spads to correspond to whether it is
883 * primary or secondary
885 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET +
886 ndev->limits.max_spads * 4;
887 ndev->reg_ofs.bar2_xlat = ndev->reg_base + SNB_PBAR2XLAT_OFFSET;
888 ndev->reg_ofs.bar4_xlat = ndev->reg_base + SNB_PBAR4XLAT_OFFSET;
890 ndev->limits.max_mw = SNB_MAX_MW;
893 /* Most likely caused by the remote NTB-RP device not being
896 dev_err(&ndev->pdev->dev, "Unknown PPD %x\n", val);
900 ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
901 ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_SLINK_STATUS_OFFSET;
902 ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
904 ndev->limits.msix_cnt = SNB_MSIX_CNT;
905 ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
910 static int ntb_bwd_setup(struct ntb_device *ndev)
915 ndev->hw_type = BWD_HW;
917 rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
921 switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
923 ndev->conn_type = NTB_CONN_B2B;
927 dev_err(&ndev->pdev->dev, "Unsupported NTB configuration\n");
931 if (val & BWD_PPD_DEV_TYPE)
932 ndev->dev_type = NTB_DEV_DSD;
934 ndev->dev_type = NTB_DEV_USD;
936 /* Initiate PCI-E link training */
937 rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
938 val | BWD_PPD_INIT_LINK);
942 ndev->reg_ofs.ldb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
943 ndev->reg_ofs.ldb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
944 ndev->reg_ofs.rdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
945 ndev->reg_ofs.bar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
946 ndev->reg_ofs.bar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
947 ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
948 ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
949 ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
950 ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
951 ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
952 ndev->limits.max_mw = BWD_MAX_MW;
953 ndev->limits.max_spads = BWD_MAX_SPADS;
954 ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
955 ndev->limits.msix_cnt = BWD_MSIX_CNT;
956 ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
958 /* Since bwd doesn't have a link interrupt, setup a poll timer */
959 INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
960 INIT_DELAYED_WORK(&ndev->lr_timer, bwd_link_recovery);
961 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
966 static int ntb_device_setup(struct ntb_device *ndev)
970 if (is_ntb_xeon(ndev))
971 rc = ntb_xeon_setup(ndev);
972 else if (is_ntb_atom(ndev))
973 rc = ntb_bwd_setup(ndev);
980 dev_info(&ndev->pdev->dev, "Device Type = %s\n",
981 ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
983 if (ndev->conn_type == NTB_CONN_B2B)
984 /* Enable Bus Master and Memory Space on the secondary side */
985 writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER,
986 ndev->reg_ofs.spci_cmd);
991 static void ntb_device_free(struct ntb_device *ndev)
993 if (is_ntb_atom(ndev)) {
994 cancel_delayed_work_sync(&ndev->hb_timer);
995 cancel_delayed_work_sync(&ndev->lr_timer);
999 static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
1001 struct ntb_db_cb *db_cb = data;
1002 struct ntb_device *ndev = db_cb->ndev;
1005 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
1008 mask = readw(ndev->reg_ofs.ldb_mask);
1009 set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
1010 writew(mask, ndev->reg_ofs.ldb_mask);
1012 tasklet_schedule(&db_cb->irq_work);
1014 /* No need to check for the specific HB irq, any interrupt means
1017 ndev->last_ts = jiffies;
1019 writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.ldb);
1024 static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
1026 struct ntb_db_cb *db_cb = data;
1027 struct ntb_device *ndev = db_cb->ndev;
1030 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
1033 mask = readw(ndev->reg_ofs.ldb_mask);
1034 set_bit(db_cb->db_num * ndev->bits_per_vector, &mask);
1035 writew(mask, ndev->reg_ofs.ldb_mask);
1037 tasklet_schedule(&db_cb->irq_work);
1039 /* On Sandybridge, there are 16 bits in the interrupt register
1040 * but only 4 vectors. So, 5 bits are assigned to the first 3
1041 * vectors, with the 4th having a single bit for link
1044 writew(((1 << ndev->bits_per_vector) - 1) <<
1045 (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.ldb);
1050 /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
1051 static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
1053 struct ntb_device *ndev = dev;
1056 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
1058 rc = ntb_link_status(ndev);
1060 dev_err(&ndev->pdev->dev, "Error determining link status\n");
1062 /* bit 15 is always the link bit */
1063 writew(1 << SNB_LINK_DB, ndev->reg_ofs.ldb);
1068 static irqreturn_t ntb_interrupt(int irq, void *dev)
1070 struct ntb_device *ndev = dev;
1073 if (is_ntb_atom(ndev)) {
1074 u64 ldb = readq(ndev->reg_ofs.ldb);
1076 dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %Lx\n", irq, ldb);
1081 bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
1084 u16 ldb = readw(ndev->reg_ofs.ldb);
1086 dev_dbg(&ndev->pdev->dev, "irq %d - ldb = %x\n", irq, ldb);
1088 if (ldb & SNB_DB_HW_LINK) {
1089 xeon_event_msix_irq(irq, dev);
1090 ldb &= ~SNB_DB_HW_LINK;
1096 xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
1103 static int ntb_setup_snb_msix(struct ntb_device *ndev, int msix_entries)
1105 struct pci_dev *pdev = ndev->pdev;
1106 struct msix_entry *msix;
1109 if (msix_entries < ndev->limits.msix_cnt)
1112 rc = pci_enable_msix_exact(pdev, ndev->msix_entries, msix_entries);
1116 for (i = 0; i < msix_entries; i++) {
1117 msix = &ndev->msix_entries[i];
1118 WARN_ON(!msix->vector);
1120 if (i == msix_entries - 1) {
1121 rc = request_irq(msix->vector,
1122 xeon_event_msix_irq, 0,
1123 "ntb-event-msix", ndev);
1127 rc = request_irq(msix->vector,
1128 xeon_callback_msix_irq, 0,
1129 "ntb-callback-msix",
1136 ndev->num_msix = msix_entries;
1137 ndev->max_cbs = msix_entries - 1;
1143 /* Code never reaches here for entry nr 'ndev->num_msix - 1' */
1144 msix = &ndev->msix_entries[i];
1145 free_irq(msix->vector, &ndev->db_cb[i]);
1148 pci_disable_msix(pdev);
1154 static int ntb_setup_bwd_msix(struct ntb_device *ndev, int msix_entries)
1156 struct pci_dev *pdev = ndev->pdev;
1157 struct msix_entry *msix;
1160 msix_entries = pci_enable_msix_range(pdev, ndev->msix_entries,
1162 if (msix_entries < 0)
1163 return msix_entries;
1165 for (i = 0; i < msix_entries; i++) {
1166 msix = &ndev->msix_entries[i];
1167 WARN_ON(!msix->vector);
1169 rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
1170 "ntb-callback-msix", &ndev->db_cb[i]);
1175 ndev->num_msix = msix_entries;
1176 ndev->max_cbs = msix_entries;
1182 free_irq(msix->vector, &ndev->db_cb[i]);
1184 pci_disable_msix(pdev);
1190 static int ntb_setup_msix(struct ntb_device *ndev)
1192 struct pci_dev *pdev = ndev->pdev;
1196 msix_entries = pci_msix_vec_count(pdev);
1197 if (msix_entries < 0) {
1200 } else if (msix_entries > ndev->limits.msix_cnt) {
1205 ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
1207 if (!ndev->msix_entries) {
1212 for (i = 0; i < msix_entries; i++)
1213 ndev->msix_entries[i].entry = i;
1215 if (is_ntb_atom(ndev))
1216 rc = ntb_setup_bwd_msix(ndev, msix_entries);
1218 rc = ntb_setup_snb_msix(ndev, msix_entries);
1225 kfree(ndev->msix_entries);
1227 dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
1231 static int ntb_setup_msi(struct ntb_device *ndev)
1233 struct pci_dev *pdev = ndev->pdev;
1236 rc = pci_enable_msi(pdev);
1240 rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
1242 pci_disable_msi(pdev);
1243 dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
1250 static int ntb_setup_intx(struct ntb_device *ndev)
1252 struct pci_dev *pdev = ndev->pdev;
1257 /* Verify intx is enabled */
1260 rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
1268 static int ntb_setup_interrupts(struct ntb_device *ndev)
1272 /* On BWD, disable all interrupts. On SNB, disable all but Link
1273 * Interrupt. The rest will be unmasked as callbacks are registered.
1275 if (is_ntb_atom(ndev))
1276 writeq(~0, ndev->reg_ofs.ldb_mask);
1278 u16 var = 1 << SNB_LINK_DB;
1279 writew(~var, ndev->reg_ofs.ldb_mask);
1282 rc = ntb_setup_msix(ndev);
1286 ndev->bits_per_vector = 1;
1287 ndev->max_cbs = ndev->limits.max_db_bits;
1289 rc = ntb_setup_msi(ndev);
1293 rc = ntb_setup_intx(ndev);
1295 dev_err(&ndev->pdev->dev, "no usable interrupts\n");
1303 static void ntb_free_interrupts(struct ntb_device *ndev)
1305 struct pci_dev *pdev = ndev->pdev;
1307 /* mask interrupts */
1308 if (is_ntb_atom(ndev))
1309 writeq(~0, ndev->reg_ofs.ldb_mask);
1311 writew(~0, ndev->reg_ofs.ldb_mask);
1313 if (ndev->num_msix) {
1314 struct msix_entry *msix;
1317 for (i = 0; i < ndev->num_msix; i++) {
1318 msix = &ndev->msix_entries[i];
1319 if (is_ntb_xeon(ndev) && i == ndev->num_msix - 1)
1320 free_irq(msix->vector, ndev);
1322 free_irq(msix->vector, &ndev->db_cb[i]);
1324 pci_disable_msix(pdev);
1325 kfree(ndev->msix_entries);
1327 free_irq(pdev->irq, ndev);
1329 if (pci_dev_msi_enabled(pdev))
1330 pci_disable_msi(pdev);
1334 static int ntb_create_callbacks(struct ntb_device *ndev)
1338 /* Chicken-egg issue. We won't know how many callbacks are necessary
1339 * until we see how many MSI-X vectors we get, but these pointers need
1340 * to be passed into the MSI-X register function. So, we allocate the
1341 * max, knowing that they might not all be used, to work around this.
1343 ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
1344 sizeof(struct ntb_db_cb),
1349 for (i = 0; i < ndev->limits.max_db_bits; i++) {
1350 ndev->db_cb[i].db_num = i;
1351 ndev->db_cb[i].ndev = ndev;
1357 static void ntb_free_callbacks(struct ntb_device *ndev)
1361 for (i = 0; i < ndev->limits.max_db_bits; i++)
1362 ntb_unregister_db_callback(ndev, i);
1367 static ssize_t ntb_debugfs_read(struct file *filp, char __user *ubuf,
1368 size_t count, loff_t *offp)
1370 struct ntb_device *ndev;
1372 ssize_t ret, offset, out_count;
1376 buf = kmalloc(out_count, GFP_KERNEL);
1380 ndev = filp->private_data;
1382 offset += snprintf(buf + offset, out_count - offset,
1383 "NTB Device Information:\n");
1384 offset += snprintf(buf + offset, out_count - offset,
1385 "Connection Type - \t\t%s\n",
1386 ndev->conn_type == NTB_CONN_TRANSPARENT ?
1387 "Transparent" : (ndev->conn_type == NTB_CONN_B2B) ?
1388 "Back to back" : "Root Port");
1389 offset += snprintf(buf + offset, out_count - offset,
1390 "Device Type - \t\t\t%s\n",
1391 ndev->dev_type == NTB_DEV_USD ?
1392 "DSD/USP" : "USD/DSP");
1393 offset += snprintf(buf + offset, out_count - offset,
1394 "Max Number of Callbacks - \t%u\n",
1396 offset += snprintf(buf + offset, out_count - offset,
1397 "Link Status - \t\t\t%s\n",
1398 ntb_hw_link_status(ndev) ? "Up" : "Down");
1399 if (ntb_hw_link_status(ndev)) {
1400 offset += snprintf(buf + offset, out_count - offset,
1401 "Link Speed - \t\t\tPCI-E Gen %u\n",
1403 offset += snprintf(buf + offset, out_count - offset,
1404 "Link Width - \t\t\tx%u\n",
1408 if (is_ntb_xeon(ndev)) {
1413 offset += snprintf(buf + offset, out_count - offset,
1414 "\nNTB Device Statistics:\n");
1415 offset += snprintf(buf + offset, out_count - offset,
1416 "Upstream Memory Miss - \t%u\n",
1417 readw(ndev->reg_base +
1418 SNB_USMEMMISS_OFFSET));
1420 offset += snprintf(buf + offset, out_count - offset,
1421 "\nNTB Hardware Errors:\n");
1423 rc = pci_read_config_word(ndev->pdev, SNB_DEVSTS_OFFSET,
1426 offset += snprintf(buf + offset, out_count - offset,
1427 "DEVSTS - \t%#06x\n", status16);
1429 rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
1432 offset += snprintf(buf + offset, out_count - offset,
1433 "LNKSTS - \t%#06x\n", status16);
1435 rc = pci_read_config_dword(ndev->pdev, SNB_UNCERRSTS_OFFSET,
1438 offset += snprintf(buf + offset, out_count - offset,
1439 "UNCERRSTS - \t%#010x\n", status32);
1441 rc = pci_read_config_dword(ndev->pdev, SNB_CORERRSTS_OFFSET,
1444 offset += snprintf(buf + offset, out_count - offset,
1445 "CORERRSTS - \t%#010x\n", status32);
1448 if (offset > out_count)
1451 ret = simple_read_from_buffer(ubuf, count, offp, buf, offset);
1456 static const struct file_operations ntb_debugfs_info = {
1457 .owner = THIS_MODULE,
1458 .open = simple_open,
1459 .read = ntb_debugfs_read,
1462 static void ntb_setup_debugfs(struct ntb_device *ndev)
1464 if (!debugfs_initialized())
1468 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1470 ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
1472 if (ndev->debugfs_dir)
1473 ndev->debugfs_info = debugfs_create_file("info", S_IRUSR,
1479 static void ntb_free_debugfs(struct ntb_device *ndev)
1481 debugfs_remove_recursive(ndev->debugfs_dir);
1483 if (debugfs_dir && simple_empty(debugfs_dir)) {
1484 debugfs_remove_recursive(debugfs_dir);
1489 static void ntb_hw_link_up(struct ntb_device *ndev)
1491 if (ndev->conn_type == NTB_CONN_TRANSPARENT)
1492 ntb_link_event(ndev, NTB_LINK_UP);
1496 /* Let's bring the NTB link up */
1497 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
1498 ntb_cntl &= ~(NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK);
1499 ntb_cntl |= NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP;
1500 ntb_cntl |= NTB_CNTL_P2S_BAR45_SNOOP | NTB_CNTL_S2P_BAR45_SNOOP;
1501 writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
1505 static void ntb_hw_link_down(struct ntb_device *ndev)
1509 if (ndev->conn_type == NTB_CONN_TRANSPARENT) {
1510 ntb_link_event(ndev, NTB_LINK_DOWN);
1514 /* Bring NTB link down */
1515 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
1516 ntb_cntl &= ~(NTB_CNTL_P2S_BAR23_SNOOP | NTB_CNTL_S2P_BAR23_SNOOP);
1517 ntb_cntl &= ~(NTB_CNTL_P2S_BAR45_SNOOP | NTB_CNTL_S2P_BAR45_SNOOP);
1518 ntb_cntl |= NTB_CNTL_LINK_DISABLE | NTB_CNTL_CFG_LOCK;
1519 writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
1522 static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1524 struct ntb_device *ndev;
1527 ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
1532 ndev->link_status = NTB_LINK_DOWN;
1533 pci_set_drvdata(pdev, ndev);
1534 ntb_setup_debugfs(ndev);
1536 rc = pci_enable_device(pdev);
1540 pci_set_master(ndev->pdev);
1542 rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
1546 ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
1547 if (!ndev->reg_base) {
1548 dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
1553 for (i = 0; i < NTB_MAX_NUM_MW; i++) {
1554 ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
1556 ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
1557 ndev->mw[i].bar_sz);
1558 dev_info(&pdev->dev, "MW %d size %llu\n", i,
1559 (unsigned long long) ndev->mw[i].bar_sz);
1560 if (!ndev->mw[i].vbase) {
1561 dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
1568 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1570 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1574 dev_warn(&pdev->dev, "Cannot DMA highmem\n");
1577 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1579 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1583 dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
1586 rc = ntb_device_setup(ndev);
1590 rc = ntb_create_callbacks(ndev);
1594 rc = ntb_setup_interrupts(ndev);
1598 /* The scratchpad registers keep the values between rmmod/insmod,
1601 for (i = 0; i < ndev->limits.max_spads; i++) {
1602 ntb_write_local_spad(ndev, i, 0);
1603 ntb_write_remote_spad(ndev, i, 0);
1606 rc = ntb_transport_init(pdev);
1610 ntb_hw_link_up(ndev);
1615 ntb_free_interrupts(ndev);
1617 ntb_free_callbacks(ndev);
1619 ntb_device_free(ndev);
1621 for (i--; i >= 0; i--)
1622 iounmap(ndev->mw[i].vbase);
1623 iounmap(ndev->reg_base);
1625 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1627 pci_disable_device(pdev);
1629 ntb_free_debugfs(ndev);
1632 dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
1636 static void ntb_pci_remove(struct pci_dev *pdev)
1638 struct ntb_device *ndev = pci_get_drvdata(pdev);
1641 ntb_hw_link_down(ndev);
1643 ntb_transport_free(ndev->ntb_transport);
1645 ntb_free_interrupts(ndev);
1646 ntb_free_callbacks(ndev);
1647 ntb_device_free(ndev);
1649 for (i = 0; i < NTB_MAX_NUM_MW; i++)
1650 iounmap(ndev->mw[i].vbase);
1652 iounmap(ndev->reg_base);
1653 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1654 pci_disable_device(pdev);
1655 ntb_free_debugfs(ndev);
1659 static struct pci_driver ntb_pci_driver = {
1660 .name = KBUILD_MODNAME,
1661 .id_table = ntb_pci_tbl,
1662 .probe = ntb_pci_probe,
1663 .remove = ntb_pci_remove,
1666 module_pci_driver(ntb_pci_driver);