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/init.h>
50 #include <linux/interrupt.h>
51 #include <linux/module.h>
52 #include <linux/pci.h>
53 #include <linux/slab.h>
57 #define NTB_NAME "Intel(R) PCI-E Non-Transparent Bridge Driver"
58 #define NTB_VER "0.25"
60 MODULE_DESCRIPTION(NTB_NAME);
61 MODULE_VERSION(NTB_VER);
62 MODULE_LICENSE("Dual BSD/GPL");
63 MODULE_AUTHOR("Intel Corporation");
81 static struct dentry *debugfs_dir;
83 /* Translate memory window 0,1 to BAR 2,4 */
84 #define MW_TO_BAR(mw) (mw * 2 + 2)
86 static DEFINE_PCI_DEVICE_TABLE(ntb_pci_tbl) = {
87 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_BWD)},
88 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_JSF)},
89 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_CLASSIC_JSF)},
90 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_RP_JSF)},
91 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_RP_SNB)},
92 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_B2B_SNB)},
93 {PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_NTB_CLASSIC_SNB)},
96 MODULE_DEVICE_TABLE(pci, ntb_pci_tbl);
99 * ntb_register_event_callback() - register event callback
100 * @ndev: pointer to ntb_device instance
101 * @func: callback function to register
103 * This function registers a callback for any HW driver events such as link
104 * up/down, power management notices and etc.
106 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
108 int ntb_register_event_callback(struct ntb_device *ndev,
109 void (*func)(void *handle, enum ntb_hw_event event))
114 ndev->event_cb = func;
120 * ntb_unregister_event_callback() - unregisters the event callback
121 * @ndev: pointer to ntb_device instance
123 * This function unregisters the existing callback from transport
125 void ntb_unregister_event_callback(struct ntb_device *ndev)
127 ndev->event_cb = NULL;
131 * ntb_register_db_callback() - register a callback for doorbell interrupt
132 * @ndev: pointer to ntb_device instance
133 * @idx: doorbell index to register callback, zero based
134 * @func: callback function to register
136 * This function registers a callback function for the doorbell interrupt
137 * on the primary side. The function will unmask the doorbell as well to
140 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
142 int ntb_register_db_callback(struct ntb_device *ndev, unsigned int idx,
143 void *data, void (*func)(void *data, int db_num))
147 if (idx >= ndev->max_cbs || ndev->db_cb[idx].callback) {
148 dev_warn(&ndev->pdev->dev, "Invalid Index.\n");
152 ndev->db_cb[idx].callback = func;
153 ndev->db_cb[idx].data = data;
155 /* unmask interrupt */
156 mask = readw(ndev->reg_ofs.pdb_mask);
157 clear_bit(idx * ndev->bits_per_vector, &mask);
158 writew(mask, ndev->reg_ofs.pdb_mask);
164 * ntb_unregister_db_callback() - unregister a callback for doorbell interrupt
165 * @ndev: pointer to ntb_device instance
166 * @idx: doorbell index to register callback, zero based
168 * This function unregisters a callback function for the doorbell interrupt
169 * on the primary side. The function will also mask the said doorbell.
171 void ntb_unregister_db_callback(struct ntb_device *ndev, unsigned int idx)
175 if (idx >= ndev->max_cbs || !ndev->db_cb[idx].callback)
178 mask = readw(ndev->reg_ofs.pdb_mask);
179 set_bit(idx * ndev->bits_per_vector, &mask);
180 writew(mask, ndev->reg_ofs.pdb_mask);
182 ndev->db_cb[idx].callback = NULL;
186 * ntb_find_transport() - find the transport pointer
187 * @transport: pointer to pci device
189 * Given the pci device pointer, return the transport pointer passed in when
190 * the transport attached when it was inited.
192 * RETURNS: pointer to transport.
194 void *ntb_find_transport(struct pci_dev *pdev)
196 struct ntb_device *ndev = pci_get_drvdata(pdev);
197 return ndev->ntb_transport;
201 * ntb_register_transport() - Register NTB transport with NTB HW driver
202 * @transport: transport identifier
204 * This function allows a transport to reserve the hardware driver for
207 * RETURNS: pointer to ntb_device, NULL on error.
209 struct ntb_device *ntb_register_transport(struct pci_dev *pdev, void *transport)
211 struct ntb_device *ndev = pci_get_drvdata(pdev);
213 if (ndev->ntb_transport)
216 ndev->ntb_transport = transport;
221 * ntb_unregister_transport() - Unregister the transport with the NTB HW driver
222 * @ndev - ntb_device of the transport to be freed
224 * This function unregisters the transport from the HW driver and performs any
225 * necessary cleanups.
227 void ntb_unregister_transport(struct ntb_device *ndev)
231 if (!ndev->ntb_transport)
234 for (i = 0; i < ndev->max_cbs; i++)
235 ntb_unregister_db_callback(ndev, i);
237 ntb_unregister_event_callback(ndev);
238 ndev->ntb_transport = NULL;
242 * ntb_write_local_spad() - write to the secondary scratchpad register
243 * @ndev: pointer to ntb_device instance
244 * @idx: index to the scratchpad register, 0 based
245 * @val: the data value to put into the register
247 * This function allows writing of a 32bit value to the indexed scratchpad
248 * register. This writes over the data mirrored to the local scratchpad register
249 * by the remote system.
251 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
253 int ntb_write_local_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
255 if (idx >= ndev->limits.max_spads)
258 dev_dbg(&ndev->pdev->dev, "Writing %x to local scratch pad index %d\n",
260 writel(val, ndev->reg_ofs.spad_read + idx * 4);
266 * ntb_read_local_spad() - read from the primary scratchpad register
267 * @ndev: pointer to ntb_device instance
268 * @idx: index to scratchpad register, 0 based
269 * @val: pointer to 32bit integer for storing the register value
271 * This function allows reading of the 32bit scratchpad register on
272 * the primary (internal) side. This allows the local system to read data
273 * written and mirrored to the scratchpad register by the remote system.
275 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
277 int ntb_read_local_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
279 if (idx >= ndev->limits.max_spads)
282 *val = readl(ndev->reg_ofs.spad_write + idx * 4);
283 dev_dbg(&ndev->pdev->dev,
284 "Reading %x from local scratch pad index %d\n", *val, idx);
290 * ntb_write_remote_spad() - write to the secondary scratchpad register
291 * @ndev: pointer to ntb_device instance
292 * @idx: index to the scratchpad register, 0 based
293 * @val: the data value to put into the register
295 * This function allows writing of a 32bit value to the indexed scratchpad
296 * register. The register resides on the secondary (external) side. This allows
297 * the local system to write data to be mirrored to the remote systems
298 * scratchpad register.
300 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
302 int ntb_write_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 val)
304 if (idx >= ndev->limits.max_spads)
307 dev_dbg(&ndev->pdev->dev, "Writing %x to remote scratch pad index %d\n",
309 writel(val, ndev->reg_ofs.spad_write + idx * 4);
315 * ntb_read_remote_spad() - read from the primary scratchpad register
316 * @ndev: pointer to ntb_device instance
317 * @idx: index to scratchpad register, 0 based
318 * @val: pointer to 32bit integer for storing the register value
320 * This function allows reading of the 32bit scratchpad register on
321 * the primary (internal) side. This alloows the local system to read the data
322 * it wrote to be mirrored on the remote system.
324 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
326 int ntb_read_remote_spad(struct ntb_device *ndev, unsigned int idx, u32 *val)
328 if (idx >= ndev->limits.max_spads)
331 *val = readl(ndev->reg_ofs.spad_read + idx * 4);
332 dev_dbg(&ndev->pdev->dev,
333 "Reading %x from remote scratch pad index %d\n", *val, idx);
339 * ntb_get_mw_vbase() - get virtual addr for the NTB memory window
340 * @ndev: pointer to ntb_device instance
341 * @mw: memory window number
343 * This function provides the base virtual address of the memory window
346 * RETURNS: pointer to virtual address, or NULL on error.
348 void __iomem *ntb_get_mw_vbase(struct ntb_device *ndev, unsigned int mw)
350 if (mw >= NTB_NUM_MW)
353 return ndev->mw[mw].vbase;
357 * ntb_get_mw_size() - return size of NTB memory window
358 * @ndev: pointer to ntb_device instance
359 * @mw: memory window number
361 * This function provides the physical size of the memory window specified
363 * RETURNS: the size of the memory window or zero on error
365 resource_size_t ntb_get_mw_size(struct ntb_device *ndev, unsigned int mw)
367 if (mw >= NTB_NUM_MW)
370 return ndev->mw[mw].bar_sz;
374 * ntb_set_mw_addr - set the memory window address
375 * @ndev: pointer to ntb_device instance
376 * @mw: memory window number
377 * @addr: base address for data
379 * This function sets the base physical address of the memory window. This
380 * memory address is where data from the remote system will be transfered into
381 * or out of depending on how the transport is configured.
383 void ntb_set_mw_addr(struct ntb_device *ndev, unsigned int mw, u64 addr)
385 if (mw >= NTB_NUM_MW)
388 dev_dbg(&ndev->pdev->dev, "Writing addr %Lx to BAR %d\n", addr,
391 ndev->mw[mw].phys_addr = addr;
393 switch (MW_TO_BAR(mw)) {
395 writeq(addr, ndev->reg_ofs.sbar2_xlat);
398 writeq(addr, ndev->reg_ofs.sbar4_xlat);
404 * ntb_ring_sdb() - Set the doorbell on the secondary/external side
405 * @ndev: pointer to ntb_device instance
406 * @db: doorbell to ring
408 * This function allows triggering of a doorbell on the secondary/external
409 * side that will initiate an interrupt on the remote host
411 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
413 void ntb_ring_sdb(struct ntb_device *ndev, unsigned int db)
415 dev_dbg(&ndev->pdev->dev, "%s: ringing doorbell %d\n", __func__, db);
417 if (ndev->hw_type == BWD_HW)
418 writeq((u64) 1 << db, ndev->reg_ofs.sdb);
420 writew(((1 << ndev->bits_per_vector) - 1) <<
421 (db * ndev->bits_per_vector), ndev->reg_ofs.sdb);
424 static void ntb_link_event(struct ntb_device *ndev, int link_state)
428 if (ndev->link_status == link_state)
431 if (link_state == NTB_LINK_UP) {
434 dev_info(&ndev->pdev->dev, "Link Up\n");
435 ndev->link_status = NTB_LINK_UP;
436 event = NTB_EVENT_HW_LINK_UP;
438 if (ndev->hw_type == BWD_HW)
439 status = readw(ndev->reg_ofs.lnk_stat);
441 int rc = pci_read_config_word(ndev->pdev,
442 SNB_LINK_STATUS_OFFSET,
447 dev_info(&ndev->pdev->dev, "Link Width %d, Link Speed %d\n",
448 (status & NTB_LINK_WIDTH_MASK) >> 4,
449 (status & NTB_LINK_SPEED_MASK));
451 dev_info(&ndev->pdev->dev, "Link Down\n");
452 ndev->link_status = NTB_LINK_DOWN;
453 event = NTB_EVENT_HW_LINK_DOWN;
456 /* notify the upper layer if we have an event change */
458 ndev->event_cb(ndev->ntb_transport, event);
461 static int ntb_link_status(struct ntb_device *ndev)
465 if (ndev->hw_type == BWD_HW) {
468 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
469 if (ntb_cntl & BWD_CNTL_LINK_DOWN)
470 link_state = NTB_LINK_DOWN;
472 link_state = NTB_LINK_UP;
477 rc = pci_read_config_word(ndev->pdev, SNB_LINK_STATUS_OFFSET,
482 if (status & NTB_LINK_STATUS_ACTIVE)
483 link_state = NTB_LINK_UP;
485 link_state = NTB_LINK_DOWN;
488 ntb_link_event(ndev, link_state);
493 /* BWD doesn't have link status interrupt, poll on that platform */
494 static void bwd_link_poll(struct work_struct *work)
496 struct ntb_device *ndev = container_of(work, struct ntb_device,
498 unsigned long ts = jiffies;
500 /* If we haven't gotten an interrupt in a while, check the BWD link
503 if (ts > ndev->last_ts + NTB_HB_TIMEOUT) {
504 int rc = ntb_link_status(ndev);
506 dev_err(&ndev->pdev->dev,
507 "Error determining link status\n");
510 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
513 static int ntb_xeon_setup(struct ntb_device *ndev)
518 ndev->hw_type = SNB_HW;
520 rc = pci_read_config_byte(ndev->pdev, NTB_PPD_OFFSET, &val);
524 switch (val & SNB_PPD_CONN_TYPE) {
526 ndev->conn_type = NTB_CONN_B2B;
528 case NTB_CONN_CLASSIC:
531 dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
535 if (val & SNB_PPD_DEV_TYPE)
536 ndev->dev_type = NTB_DEV_USD;
538 ndev->dev_type = NTB_DEV_DSD;
540 ndev->reg_ofs.pdb = ndev->reg_base + SNB_PDOORBELL_OFFSET;
541 ndev->reg_ofs.pdb_mask = ndev->reg_base + SNB_PDBMSK_OFFSET;
542 ndev->reg_ofs.sbar2_xlat = ndev->reg_base + SNB_SBAR2XLAT_OFFSET;
543 ndev->reg_ofs.sbar4_xlat = ndev->reg_base + SNB_SBAR4XLAT_OFFSET;
544 ndev->reg_ofs.lnk_cntl = ndev->reg_base + SNB_NTBCNTL_OFFSET;
545 ndev->reg_ofs.lnk_stat = ndev->reg_base + SNB_LINK_STATUS_OFFSET;
546 ndev->reg_ofs.spad_read = ndev->reg_base + SNB_SPAD_OFFSET;
547 ndev->reg_ofs.spci_cmd = ndev->reg_base + SNB_PCICMD_OFFSET;
549 if (ndev->conn_type == NTB_CONN_B2B) {
550 ndev->reg_ofs.sdb = ndev->reg_base + SNB_B2B_DOORBELL_OFFSET;
551 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_B2B_SPAD_OFFSET;
552 ndev->limits.max_spads = SNB_MAX_B2B_SPADS;
554 ndev->reg_ofs.sdb = ndev->reg_base + SNB_SDOORBELL_OFFSET;
555 ndev->reg_ofs.spad_write = ndev->reg_base + SNB_SPAD_OFFSET;
556 ndev->limits.max_spads = SNB_MAX_COMPAT_SPADS;
559 ndev->limits.max_db_bits = SNB_MAX_DB_BITS;
560 ndev->limits.msix_cnt = SNB_MSIX_CNT;
561 ndev->bits_per_vector = SNB_DB_BITS_PER_VEC;
566 static int ntb_bwd_setup(struct ntb_device *ndev)
571 ndev->hw_type = BWD_HW;
573 rc = pci_read_config_dword(ndev->pdev, NTB_PPD_OFFSET, &val);
577 switch ((val & BWD_PPD_CONN_TYPE) >> 8) {
579 ndev->conn_type = NTB_CONN_B2B;
583 dev_err(&ndev->pdev->dev, "Only B2B supported at this time\n");
587 if (val & BWD_PPD_DEV_TYPE)
588 ndev->dev_type = NTB_DEV_DSD;
590 ndev->dev_type = NTB_DEV_USD;
592 /* Initiate PCI-E link training */
593 rc = pci_write_config_dword(ndev->pdev, NTB_PPD_OFFSET,
594 val | BWD_PPD_INIT_LINK);
598 ndev->reg_ofs.pdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
599 ndev->reg_ofs.pdb_mask = ndev->reg_base + BWD_PDBMSK_OFFSET;
600 ndev->reg_ofs.sbar2_xlat = ndev->reg_base + BWD_SBAR2XLAT_OFFSET;
601 ndev->reg_ofs.sbar4_xlat = ndev->reg_base + BWD_SBAR4XLAT_OFFSET;
602 ndev->reg_ofs.lnk_cntl = ndev->reg_base + BWD_NTBCNTL_OFFSET;
603 ndev->reg_ofs.lnk_stat = ndev->reg_base + BWD_LINK_STATUS_OFFSET;
604 ndev->reg_ofs.spad_read = ndev->reg_base + BWD_SPAD_OFFSET;
605 ndev->reg_ofs.spci_cmd = ndev->reg_base + BWD_PCICMD_OFFSET;
607 if (ndev->conn_type == NTB_CONN_B2B) {
608 ndev->reg_ofs.sdb = ndev->reg_base + BWD_B2B_DOORBELL_OFFSET;
609 ndev->reg_ofs.spad_write = ndev->reg_base + BWD_B2B_SPAD_OFFSET;
610 ndev->limits.max_spads = BWD_MAX_SPADS;
612 ndev->reg_ofs.sdb = ndev->reg_base + BWD_PDOORBELL_OFFSET;
613 ndev->reg_ofs.spad_write = ndev->reg_base + BWD_SPAD_OFFSET;
614 ndev->limits.max_spads = BWD_MAX_COMPAT_SPADS;
617 ndev->limits.max_db_bits = BWD_MAX_DB_BITS;
618 ndev->limits.msix_cnt = BWD_MSIX_CNT;
619 ndev->bits_per_vector = BWD_DB_BITS_PER_VEC;
621 /* Since bwd doesn't have a link interrupt, setup a poll timer */
622 INIT_DELAYED_WORK(&ndev->hb_timer, bwd_link_poll);
623 schedule_delayed_work(&ndev->hb_timer, NTB_HB_TIMEOUT);
628 static int ntb_device_setup(struct ntb_device *ndev)
632 switch (ndev->pdev->device) {
633 case PCI_DEVICE_ID_INTEL_NTB_2ND_SNB:
634 case PCI_DEVICE_ID_INTEL_NTB_RP_JSF:
635 case PCI_DEVICE_ID_INTEL_NTB_RP_SNB:
636 case PCI_DEVICE_ID_INTEL_NTB_CLASSIC_JSF:
637 case PCI_DEVICE_ID_INTEL_NTB_CLASSIC_SNB:
638 case PCI_DEVICE_ID_INTEL_NTB_B2B_JSF:
639 case PCI_DEVICE_ID_INTEL_NTB_B2B_SNB:
640 rc = ntb_xeon_setup(ndev);
642 case PCI_DEVICE_ID_INTEL_NTB_B2B_BWD:
643 rc = ntb_bwd_setup(ndev);
652 dev_info(&ndev->pdev->dev, "Device Type = %s\n",
653 ndev->dev_type == NTB_DEV_USD ? "USD/DSP" : "DSD/USP");
655 /* Enable Bus Master and Memory Space on the secondary side */
656 writew(PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER, ndev->reg_ofs.spci_cmd);
661 static void ntb_device_free(struct ntb_device *ndev)
663 if (ndev->hw_type == BWD_HW)
664 cancel_delayed_work_sync(&ndev->hb_timer);
667 static irqreturn_t bwd_callback_msix_irq(int irq, void *data)
669 struct ntb_db_cb *db_cb = data;
670 struct ntb_device *ndev = db_cb->ndev;
672 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
676 db_cb->callback(db_cb->data, db_cb->db_num);
678 /* No need to check for the specific HB irq, any interrupt means
681 ndev->last_ts = jiffies;
683 writeq((u64) 1 << db_cb->db_num, ndev->reg_ofs.pdb);
688 static irqreturn_t xeon_callback_msix_irq(int irq, void *data)
690 struct ntb_db_cb *db_cb = data;
691 struct ntb_device *ndev = db_cb->ndev;
693 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for DB %d\n", irq,
697 db_cb->callback(db_cb->data, db_cb->db_num);
699 /* On Sandybridge, there are 16 bits in the interrupt register
700 * but only 4 vectors. So, 5 bits are assigned to the first 3
701 * vectors, with the 4th having a single bit for link
704 writew(((1 << ndev->bits_per_vector) - 1) <<
705 (db_cb->db_num * ndev->bits_per_vector), ndev->reg_ofs.pdb);
710 /* Since we do not have a HW doorbell in BWD, this is only used in JF/JT */
711 static irqreturn_t xeon_event_msix_irq(int irq, void *dev)
713 struct ntb_device *ndev = dev;
716 dev_dbg(&ndev->pdev->dev, "MSI-X irq %d received for Events\n", irq);
718 rc = ntb_link_status(ndev);
720 dev_err(&ndev->pdev->dev, "Error determining link status\n");
722 /* bit 15 is always the link bit */
723 writew(1 << ndev->limits.max_db_bits, ndev->reg_ofs.pdb);
728 static irqreturn_t ntb_interrupt(int irq, void *dev)
730 struct ntb_device *ndev = dev;
733 if (ndev->hw_type == BWD_HW) {
734 u64 pdb = readq(ndev->reg_ofs.pdb);
736 dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %Lx\n", irq, pdb);
741 bwd_callback_msix_irq(irq, &ndev->db_cb[i]);
744 u16 pdb = readw(ndev->reg_ofs.pdb);
746 dev_dbg(&ndev->pdev->dev, "irq %d - pdb = %x sdb %x\n", irq,
747 pdb, readw(ndev->reg_ofs.sdb));
749 if (pdb & SNB_DB_HW_LINK) {
750 xeon_event_msix_irq(irq, dev);
751 pdb &= ~SNB_DB_HW_LINK;
757 xeon_callback_msix_irq(irq, &ndev->db_cb[i]);
764 static int ntb_setup_msix(struct ntb_device *ndev)
766 struct pci_dev *pdev = ndev->pdev;
767 struct msix_entry *msix;
772 pos = pci_find_capability(pdev, PCI_CAP_ID_MSIX);
778 rc = pci_read_config_word(pdev, pos + PCI_MSIX_FLAGS, &val);
782 msix_entries = msix_table_size(val);
783 if (msix_entries > ndev->limits.msix_cnt) {
788 ndev->msix_entries = kmalloc(sizeof(struct msix_entry) * msix_entries,
790 if (!ndev->msix_entries) {
795 for (i = 0; i < msix_entries; i++)
796 ndev->msix_entries[i].entry = i;
798 rc = pci_enable_msix(pdev, ndev->msix_entries, msix_entries);
802 /* On SNB, the link interrupt is always tied to 4th vector. If
803 * we can't get all 4, then we can't use MSI-X.
805 if (ndev->hw_type != BWD_HW) {
811 "Only %d MSI-X vectors. Limiting the number of queues to that number.\n",
816 for (i = 0; i < msix_entries; i++) {
817 msix = &ndev->msix_entries[i];
818 WARN_ON(!msix->vector);
820 /* Use the last MSI-X vector for Link status */
821 if (ndev->hw_type == BWD_HW) {
822 rc = request_irq(msix->vector, bwd_callback_msix_irq, 0,
823 "ntb-callback-msix", &ndev->db_cb[i]);
827 if (i == msix_entries - 1) {
828 rc = request_irq(msix->vector,
829 xeon_event_msix_irq, 0,
830 "ntb-event-msix", ndev);
834 rc = request_irq(msix->vector,
835 xeon_callback_msix_irq, 0,
844 ndev->num_msix = msix_entries;
845 if (ndev->hw_type == BWD_HW)
846 ndev->max_cbs = msix_entries;
848 ndev->max_cbs = msix_entries - 1;
854 msix = &ndev->msix_entries[i];
855 if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
856 free_irq(msix->vector, ndev);
858 free_irq(msix->vector, &ndev->db_cb[i]);
860 pci_disable_msix(pdev);
862 kfree(ndev->msix_entries);
863 dev_err(&pdev->dev, "Error allocating MSI-X interrupt\n");
869 static int ntb_setup_msi(struct ntb_device *ndev)
871 struct pci_dev *pdev = ndev->pdev;
874 rc = pci_enable_msi(pdev);
878 rc = request_irq(pdev->irq, ntb_interrupt, 0, "ntb-msi", ndev);
880 pci_disable_msi(pdev);
881 dev_err(&pdev->dev, "Error allocating MSI interrupt\n");
888 static int ntb_setup_intx(struct ntb_device *ndev)
890 struct pci_dev *pdev = ndev->pdev;
895 /* Verify intx is enabled */
898 rc = request_irq(pdev->irq, ntb_interrupt, IRQF_SHARED, "ntb-intx",
906 static int ntb_setup_interrupts(struct ntb_device *ndev)
910 /* On BWD, disable all interrupts. On SNB, disable all but Link
911 * Interrupt. The rest will be unmasked as callbacks are registered.
913 if (ndev->hw_type == BWD_HW)
914 writeq(~0, ndev->reg_ofs.pdb_mask);
916 writew(~(1 << ndev->limits.max_db_bits),
917 ndev->reg_ofs.pdb_mask);
919 rc = ntb_setup_msix(ndev);
923 ndev->bits_per_vector = 1;
924 ndev->max_cbs = ndev->limits.max_db_bits;
926 rc = ntb_setup_msi(ndev);
930 rc = ntb_setup_intx(ndev);
932 dev_err(&ndev->pdev->dev, "no usable interrupts\n");
940 static void ntb_free_interrupts(struct ntb_device *ndev)
942 struct pci_dev *pdev = ndev->pdev;
944 /* mask interrupts */
945 if (ndev->hw_type == BWD_HW)
946 writeq(~0, ndev->reg_ofs.pdb_mask);
948 writew(~0, ndev->reg_ofs.pdb_mask);
950 if (ndev->num_msix) {
951 struct msix_entry *msix;
954 for (i = 0; i < ndev->num_msix; i++) {
955 msix = &ndev->msix_entries[i];
956 if (ndev->hw_type != BWD_HW && i == ndev->num_msix - 1)
957 free_irq(msix->vector, ndev);
959 free_irq(msix->vector, &ndev->db_cb[i]);
961 pci_disable_msix(pdev);
963 free_irq(pdev->irq, ndev);
965 if (pci_dev_msi_enabled(pdev))
966 pci_disable_msi(pdev);
970 static int ntb_create_callbacks(struct ntb_device *ndev)
974 /* Checken-egg issue. We won't know how many callbacks are necessary
975 * until we see how many MSI-X vectors we get, but these pointers need
976 * to be passed into the MSI-X register fucntion. So, we allocate the
977 * max, knowing that they might not all be used, to work around this.
979 ndev->db_cb = kcalloc(ndev->limits.max_db_bits,
980 sizeof(struct ntb_db_cb),
985 for (i = 0; i < ndev->limits.max_db_bits; i++) {
986 ndev->db_cb[i].db_num = i;
987 ndev->db_cb[i].ndev = ndev;
993 static void ntb_free_callbacks(struct ntb_device *ndev)
997 for (i = 0; i < ndev->limits.max_db_bits; i++)
998 ntb_unregister_db_callback(ndev, i);
1003 static void ntb_setup_debugfs(struct ntb_device *ndev)
1005 if (!debugfs_initialized())
1009 debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
1011 ndev->debugfs_dir = debugfs_create_dir(pci_name(ndev->pdev),
1015 static void ntb_free_debugfs(struct ntb_device *ndev)
1017 debugfs_remove_recursive(ndev->debugfs_dir);
1019 if (debugfs_dir && simple_empty(debugfs_dir)) {
1020 debugfs_remove_recursive(debugfs_dir);
1025 static int ntb_pci_probe(struct pci_dev *pdev, const struct pci_device_id *id)
1027 struct ntb_device *ndev;
1030 ndev = kzalloc(sizeof(struct ntb_device), GFP_KERNEL);
1035 ndev->link_status = NTB_LINK_DOWN;
1036 pci_set_drvdata(pdev, ndev);
1037 ntb_setup_debugfs(ndev);
1039 rc = pci_enable_device(pdev);
1043 pci_set_master(ndev->pdev);
1045 rc = pci_request_selected_regions(pdev, NTB_BAR_MASK, KBUILD_MODNAME);
1049 ndev->reg_base = pci_ioremap_bar(pdev, NTB_BAR_MMIO);
1050 if (!ndev->reg_base) {
1051 dev_warn(&pdev->dev, "Cannot remap BAR 0\n");
1056 for (i = 0; i < NTB_NUM_MW; i++) {
1057 ndev->mw[i].bar_sz = pci_resource_len(pdev, MW_TO_BAR(i));
1059 ioremap_wc(pci_resource_start(pdev, MW_TO_BAR(i)),
1060 ndev->mw[i].bar_sz);
1061 dev_info(&pdev->dev, "MW %d size %llu\n", i,
1062 pci_resource_len(pdev, MW_TO_BAR(i)));
1063 if (!ndev->mw[i].vbase) {
1064 dev_warn(&pdev->dev, "Cannot remap BAR %d\n",
1071 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
1073 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1077 dev_warn(&pdev->dev, "Cannot DMA highmem\n");
1080 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
1082 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1086 dev_warn(&pdev->dev, "Cannot DMA consistent highmem\n");
1089 rc = ntb_device_setup(ndev);
1093 rc = ntb_create_callbacks(ndev);
1097 rc = ntb_setup_interrupts(ndev);
1101 /* The scratchpad registers keep the values between rmmod/insmod,
1104 for (i = 0; i < ndev->limits.max_spads; i++) {
1105 ntb_write_local_spad(ndev, i, 0);
1106 ntb_write_remote_spad(ndev, i, 0);
1109 rc = ntb_transport_init(pdev);
1113 /* Let's bring the NTB link up */
1114 writel(NTB_CNTL_BAR23_SNOOP | NTB_CNTL_BAR45_SNOOP,
1115 ndev->reg_ofs.lnk_cntl);
1120 ntb_free_interrupts(ndev);
1122 ntb_free_callbacks(ndev);
1124 ntb_device_free(ndev);
1126 for (i--; i >= 0; i--)
1127 iounmap(ndev->mw[i].vbase);
1128 iounmap(ndev->reg_base);
1130 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1132 pci_disable_device(pdev);
1134 ntb_free_debugfs(ndev);
1137 dev_err(&pdev->dev, "Error loading %s module\n", KBUILD_MODNAME);
1141 static void ntb_pci_remove(struct pci_dev *pdev)
1143 struct ntb_device *ndev = pci_get_drvdata(pdev);
1147 /* Bring NTB link down */
1148 ntb_cntl = readl(ndev->reg_ofs.lnk_cntl);
1149 ntb_cntl |= NTB_LINK_DISABLE;
1150 writel(ntb_cntl, ndev->reg_ofs.lnk_cntl);
1152 ntb_transport_free(ndev->ntb_transport);
1154 ntb_free_interrupts(ndev);
1155 ntb_free_callbacks(ndev);
1156 ntb_device_free(ndev);
1158 for (i = 0; i < NTB_NUM_MW; i++)
1159 iounmap(ndev->mw[i].vbase);
1161 iounmap(ndev->reg_base);
1162 pci_release_selected_regions(pdev, NTB_BAR_MASK);
1163 pci_disable_device(pdev);
1164 ntb_free_debugfs(ndev);
1168 static struct pci_driver ntb_pci_driver = {
1169 .name = KBUILD_MODNAME,
1170 .id_table = ntb_pci_tbl,
1171 .probe = ntb_pci_probe,
1172 .remove = ntb_pci_remove,
1174 module_pci_driver(ntb_pci_driver);