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.
8 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
17 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 * PCIe NTB Transport Linux driver
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
50 #include <linux/debugfs.h>
51 #include <linux/delay.h>
52 #include <linux/dmaengine.h>
53 #include <linux/dma-mapping.h>
54 #include <linux/errno.h>
55 #include <linux/export.h>
56 #include <linux/interrupt.h>
57 #include <linux/module.h>
58 #include <linux/pci.h>
59 #include <linux/slab.h>
60 #include <linux/types.h>
61 #include <linux/uaccess.h>
62 #include "linux/ntb.h"
63 #include "linux/ntb_transport.h"
65 #define NTB_TRANSPORT_VERSION 4
66 #define NTB_TRANSPORT_VER "4"
67 #define NTB_TRANSPORT_NAME "ntb_transport"
68 #define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
69 #define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
71 MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
72 MODULE_VERSION(NTB_TRANSPORT_VER);
73 MODULE_LICENSE("Dual BSD/GPL");
74 MODULE_AUTHOR("Intel Corporation");
76 static unsigned long max_mw_size;
77 module_param(max_mw_size, ulong, 0644);
78 MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
80 static unsigned int transport_mtu = 0x10000;
81 module_param(transport_mtu, uint, 0644);
82 MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
84 static unsigned char max_num_clients;
85 module_param(max_num_clients, byte, 0644);
86 MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
88 static unsigned int copy_bytes = 1024;
89 module_param(copy_bytes, uint, 0644);
90 MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
93 module_param(use_dma, bool, 0644);
94 MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
98 module_param(use_msi, bool, 0644);
99 MODULE_PARM_DESC(use_msi, "Use MSI interrupts instead of doorbells");
102 static struct dentry *nt_debugfs_dir;
104 /* Only two-ports NTB devices are supported */
105 #define PIDX NTB_DEF_PEER_IDX
107 struct ntb_queue_entry {
108 /* ntb_queue list reference */
109 struct list_head entry;
110 /* pointers to data to be transferred */
117 unsigned int tx_index;
118 unsigned int rx_index;
120 struct ntb_transport_qp *qp;
122 struct ntb_payload_header __iomem *tx_hdr;
123 struct ntb_payload_header *rx_hdr;
131 struct ntb_transport_qp {
132 struct ntb_transport_ctx *transport;
133 struct ntb_dev *ndev;
135 struct dma_chan *tx_dma_chan;
136 struct dma_chan *rx_dma_chan;
142 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
145 struct ntb_rx_info __iomem *rx_info;
146 struct ntb_rx_info *remote_rx_info;
148 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
149 void *data, int len);
150 struct list_head tx_free_q;
151 spinlock_t ntb_tx_free_q_lock;
153 phys_addr_t tx_mw_phys;
155 dma_addr_t tx_mw_dma_addr;
156 unsigned int tx_index;
157 unsigned int tx_max_entry;
158 unsigned int tx_max_frame;
160 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
161 void *data, int len);
162 struct list_head rx_post_q;
163 struct list_head rx_pend_q;
164 struct list_head rx_free_q;
165 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
166 spinlock_t ntb_rx_q_lock;
168 unsigned int rx_index;
169 unsigned int rx_max_entry;
170 unsigned int rx_max_frame;
171 unsigned int rx_alloc_entry;
172 dma_cookie_t last_cookie;
173 struct tasklet_struct rxc_db_work;
175 void (*event_handler)(void *data, int status);
176 struct delayed_work link_work;
177 struct work_struct link_cleanup;
179 struct dentry *debugfs_dir;
180 struct dentry *debugfs_stats;
200 struct ntb_msi_desc msi_desc;
201 struct ntb_msi_desc peer_msi_desc;
204 struct ntb_transport_mw {
205 phys_addr_t phys_addr;
206 resource_size_t phys_size;
216 struct ntb_transport_client_dev {
217 struct list_head entry;
218 struct ntb_transport_ctx *nt;
222 struct ntb_transport_ctx {
223 struct list_head entry;
224 struct list_head client_devs;
226 struct ntb_dev *ndev;
228 struct ntb_transport_mw *mw_vec;
229 struct ntb_transport_qp *qp_vec;
230 unsigned int mw_count;
231 unsigned int qp_count;
236 unsigned int msi_spad_offset;
240 struct delayed_work link_work;
241 struct work_struct link_cleanup;
243 struct dentry *debugfs_node_dir;
247 DESC_DONE_FLAG = BIT(0),
248 LINK_DOWN_FLAG = BIT(1),
251 struct ntb_payload_header {
266 #define dev_client_dev(__dev) \
267 container_of((__dev), struct ntb_transport_client_dev, dev)
269 #define drv_client(__drv) \
270 container_of((__drv), struct ntb_transport_client, driver)
272 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
273 #define NTB_QP_DEF_NUM_ENTRIES 100
274 #define NTB_LINK_DOWN_TIMEOUT 10
276 static void ntb_transport_rxc_db(unsigned long data);
277 static const struct ntb_ctx_ops ntb_transport_ops;
278 static struct ntb_client ntb_transport_client;
279 static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
280 struct ntb_queue_entry *entry);
281 static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
282 static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
283 static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
286 static int ntb_transport_bus_match(struct device *dev,
287 struct device_driver *drv)
289 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
292 static int ntb_transport_bus_probe(struct device *dev)
294 const struct ntb_transport_client *client;
299 client = drv_client(dev->driver);
300 rc = client->probe(dev);
307 static void ntb_transport_bus_remove(struct device *dev)
309 const struct ntb_transport_client *client;
311 client = drv_client(dev->driver);
317 static struct bus_type ntb_transport_bus = {
318 .name = "ntb_transport",
319 .match = ntb_transport_bus_match,
320 .probe = ntb_transport_bus_probe,
321 .remove = ntb_transport_bus_remove,
324 static LIST_HEAD(ntb_transport_list);
326 static int ntb_bus_init(struct ntb_transport_ctx *nt)
328 list_add_tail(&nt->entry, &ntb_transport_list);
332 static void ntb_bus_remove(struct ntb_transport_ctx *nt)
334 struct ntb_transport_client_dev *client_dev, *cd;
336 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
337 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
338 dev_name(&client_dev->dev));
339 list_del(&client_dev->entry);
340 device_unregister(&client_dev->dev);
343 list_del(&nt->entry);
346 static void ntb_transport_client_release(struct device *dev)
348 struct ntb_transport_client_dev *client_dev;
350 client_dev = dev_client_dev(dev);
355 * ntb_transport_unregister_client_dev - Unregister NTB client device
356 * @device_name: Name of NTB client device
358 * Unregister an NTB client device with the NTB transport layer
360 void ntb_transport_unregister_client_dev(char *device_name)
362 struct ntb_transport_client_dev *client, *cd;
363 struct ntb_transport_ctx *nt;
365 list_for_each_entry(nt, &ntb_transport_list, entry)
366 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
367 if (!strncmp(dev_name(&client->dev), device_name,
368 strlen(device_name))) {
369 list_del(&client->entry);
370 device_unregister(&client->dev);
373 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
376 * ntb_transport_register_client_dev - Register NTB client device
377 * @device_name: Name of NTB client device
379 * Register an NTB client device with the NTB transport layer
381 int ntb_transport_register_client_dev(char *device_name)
383 struct ntb_transport_client_dev *client_dev;
384 struct ntb_transport_ctx *nt;
388 if (list_empty(&ntb_transport_list))
391 list_for_each_entry(nt, &ntb_transport_list, entry) {
394 node = dev_to_node(&nt->ndev->dev);
396 client_dev = kzalloc_node(sizeof(*client_dev),
403 dev = &client_dev->dev;
405 /* setup and register client devices */
406 dev_set_name(dev, "%s%d", device_name, i);
407 dev->bus = &ntb_transport_bus;
408 dev->release = ntb_transport_client_release;
409 dev->parent = &nt->ndev->dev;
411 rc = device_register(dev);
417 list_add_tail(&client_dev->entry, &nt->client_devs);
424 ntb_transport_unregister_client_dev(device_name);
428 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
431 * ntb_transport_register_client - Register NTB client driver
432 * @drv: NTB client driver to be registered
434 * Register an NTB client driver with the NTB transport layer
436 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
438 int ntb_transport_register_client(struct ntb_transport_client *drv)
440 drv->driver.bus = &ntb_transport_bus;
442 if (list_empty(&ntb_transport_list))
445 return driver_register(&drv->driver);
447 EXPORT_SYMBOL_GPL(ntb_transport_register_client);
450 * ntb_transport_unregister_client - Unregister NTB client driver
451 * @drv: NTB client driver to be unregistered
453 * Unregister an NTB client driver with the NTB transport layer
455 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
457 void ntb_transport_unregister_client(struct ntb_transport_client *drv)
459 driver_unregister(&drv->driver);
461 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
463 static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
466 struct ntb_transport_qp *qp;
468 ssize_t ret, out_offset, out_count;
470 qp = filp->private_data;
472 if (!qp || !qp->link_is_up)
477 buf = kmalloc(out_count, GFP_KERNEL);
482 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
483 "\nNTB QP stats:\n\n");
484 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
485 "rx_bytes - \t%llu\n", qp->rx_bytes);
486 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
487 "rx_pkts - \t%llu\n", qp->rx_pkts);
488 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
489 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
490 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
491 "rx_async - \t%llu\n", qp->rx_async);
492 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
493 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
494 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
495 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
496 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
497 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
498 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
499 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
500 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
501 "rx_buff - \t0x%p\n", qp->rx_buff);
502 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
503 "rx_index - \t%u\n", qp->rx_index);
504 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
505 "rx_max_entry - \t%u\n", qp->rx_max_entry);
506 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
507 "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
509 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
510 "tx_bytes - \t%llu\n", qp->tx_bytes);
511 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
512 "tx_pkts - \t%llu\n", qp->tx_pkts);
513 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
514 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
515 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
516 "tx_async - \t%llu\n", qp->tx_async);
517 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
518 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
519 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
520 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
521 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
522 "tx_mw - \t0x%p\n", qp->tx_mw);
523 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
524 "tx_index (H) - \t%u\n", qp->tx_index);
525 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
527 qp->remote_rx_info->entry);
528 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
529 "tx_max_entry - \t%u\n", qp->tx_max_entry);
530 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
532 ntb_transport_tx_free_entry(qp));
534 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
536 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
537 "Using TX DMA - \t%s\n",
538 qp->tx_dma_chan ? "Yes" : "No");
539 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
540 "Using RX DMA - \t%s\n",
541 qp->rx_dma_chan ? "Yes" : "No");
542 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
544 qp->link_is_up ? "Up" : "Down");
545 out_offset += scnprintf(buf + out_offset, out_count - out_offset,
548 if (out_offset > out_count)
549 out_offset = out_count;
551 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
556 static const struct file_operations ntb_qp_debugfs_stats = {
557 .owner = THIS_MODULE,
559 .read = debugfs_read,
562 static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
563 struct list_head *list)
567 spin_lock_irqsave(lock, flags);
568 list_add_tail(entry, list);
569 spin_unlock_irqrestore(lock, flags);
572 static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
573 struct list_head *list)
575 struct ntb_queue_entry *entry;
578 spin_lock_irqsave(lock, flags);
579 if (list_empty(list)) {
583 entry = list_first_entry(list, struct ntb_queue_entry, entry);
584 list_del(&entry->entry);
587 spin_unlock_irqrestore(lock, flags);
592 static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
593 struct list_head *list,
594 struct list_head *to_list)
596 struct ntb_queue_entry *entry;
599 spin_lock_irqsave(lock, flags);
601 if (list_empty(list)) {
604 entry = list_first_entry(list, struct ntb_queue_entry, entry);
605 list_move_tail(&entry->entry, to_list);
608 spin_unlock_irqrestore(lock, flags);
613 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
616 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
617 struct ntb_transport_mw *mw;
618 struct ntb_dev *ndev = nt->ndev;
619 struct ntb_queue_entry *entry;
620 unsigned int rx_size, num_qps_mw;
621 unsigned int mw_num, mw_count, qp_count;
625 mw_count = nt->mw_count;
626 qp_count = nt->qp_count;
628 mw_num = QP_TO_MW(nt, qp_num);
629 mw = &nt->mw_vec[mw_num];
634 if (mw_num < qp_count % mw_count)
635 num_qps_mw = qp_count / mw_count + 1;
637 num_qps_mw = qp_count / mw_count;
639 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
640 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
641 rx_size -= sizeof(struct ntb_rx_info);
643 qp->remote_rx_info = qp->rx_buff + rx_size;
645 /* Due to housekeeping, there must be atleast 2 buffs */
646 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
647 qp->rx_max_entry = rx_size / qp->rx_max_frame;
651 * Checking to see if we have more entries than the default.
652 * We should add additional entries if that is the case so we
653 * can be in sync with the transport frames.
655 node = dev_to_node(&ndev->dev);
656 for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
657 entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
662 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
664 qp->rx_alloc_entry++;
667 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
669 /* setup the hdr offsets with 0's */
670 for (i = 0; i < qp->rx_max_entry; i++) {
671 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
672 sizeof(struct ntb_payload_header));
673 memset(offset, 0, sizeof(struct ntb_payload_header));
683 static irqreturn_t ntb_transport_isr(int irq, void *dev)
685 struct ntb_transport_qp *qp = dev;
687 tasklet_schedule(&qp->rxc_db_work);
692 static void ntb_transport_setup_qp_peer_msi(struct ntb_transport_ctx *nt,
695 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
696 int spad = qp_num * 2 + nt->msi_spad_offset;
701 if (spad >= ntb_spad_count(nt->ndev))
704 qp->peer_msi_desc.addr_offset =
705 ntb_peer_spad_read(qp->ndev, PIDX, spad);
706 qp->peer_msi_desc.data =
707 ntb_peer_spad_read(qp->ndev, PIDX, spad + 1);
709 dev_dbg(&qp->ndev->pdev->dev, "QP%d Peer MSI addr=%x data=%x\n",
710 qp_num, qp->peer_msi_desc.addr_offset, qp->peer_msi_desc.data);
712 if (qp->peer_msi_desc.addr_offset) {
714 dev_info(&qp->ndev->pdev->dev,
715 "Using MSI interrupts for QP%d\n", qp_num);
719 static void ntb_transport_setup_qp_msi(struct ntb_transport_ctx *nt,
722 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
723 int spad = qp_num * 2 + nt->msi_spad_offset;
729 if (spad >= ntb_spad_count(nt->ndev)) {
730 dev_warn_once(&qp->ndev->pdev->dev,
731 "Not enough SPADS to use MSI interrupts\n");
735 ntb_spad_write(qp->ndev, spad, 0);
736 ntb_spad_write(qp->ndev, spad + 1, 0);
739 qp->msi_irq = ntbm_msi_request_irq(qp->ndev, ntb_transport_isr,
742 if (qp->msi_irq < 0) {
743 dev_warn(&qp->ndev->pdev->dev,
744 "Unable to allocate MSI interrupt for qp%d\n",
750 rc = ntb_spad_write(qp->ndev, spad, qp->msi_desc.addr_offset);
752 goto err_free_interrupt;
754 rc = ntb_spad_write(qp->ndev, spad + 1, qp->msi_desc.data);
756 goto err_free_interrupt;
758 dev_dbg(&qp->ndev->pdev->dev, "QP%d MSI %d addr=%x data=%x\n",
759 qp_num, qp->msi_irq, qp->msi_desc.addr_offset,
765 devm_free_irq(&nt->ndev->dev, qp->msi_irq, qp);
768 static void ntb_transport_msi_peer_desc_changed(struct ntb_transport_ctx *nt)
772 dev_dbg(&nt->ndev->pdev->dev, "Peer MSI descriptors changed");
774 for (i = 0; i < nt->qp_count; i++)
775 ntb_transport_setup_qp_peer_msi(nt, i);
778 static void ntb_transport_msi_desc_changed(void *data)
780 struct ntb_transport_ctx *nt = data;
783 dev_dbg(&nt->ndev->pdev->dev, "MSI descriptors changed");
785 for (i = 0; i < nt->qp_count; i++)
786 ntb_transport_setup_qp_msi(nt, i);
788 ntb_peer_db_set(nt->ndev, nt->msi_db_mask);
791 static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
793 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
794 struct pci_dev *pdev = nt->ndev->pdev;
799 ntb_mw_clear_trans(nt->ndev, PIDX, num_mw);
800 dma_free_coherent(&pdev->dev, mw->alloc_size,
801 mw->alloc_addr, mw->dma_addr);
805 mw->alloc_addr = NULL;
806 mw->virt_addr = NULL;
809 static int ntb_alloc_mw_buffer(struct ntb_transport_mw *mw,
810 struct device *dma_dev, size_t align)
813 void *alloc_addr, *virt_addr;
816 alloc_addr = dma_alloc_coherent(dma_dev, mw->alloc_size,
817 &dma_addr, GFP_KERNEL);
819 dev_err(dma_dev, "Unable to alloc MW buff of size %zu\n",
823 virt_addr = alloc_addr;
826 * we must ensure that the memory address allocated is BAR size
827 * aligned in order for the XLAT register to take the value. This
828 * is a requirement of the hardware. It is recommended to setup CMA
829 * for BAR sizes equal or greater than 4MB.
831 if (!IS_ALIGNED(dma_addr, align)) {
832 if (mw->alloc_size > mw->buff_size) {
833 virt_addr = PTR_ALIGN(alloc_addr, align);
834 dma_addr = ALIGN(dma_addr, align);
841 mw->alloc_addr = alloc_addr;
842 mw->virt_addr = virt_addr;
843 mw->dma_addr = dma_addr;
848 dma_free_coherent(dma_dev, mw->alloc_size, alloc_addr, dma_addr);
853 static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
854 resource_size_t size)
856 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
857 struct pci_dev *pdev = nt->ndev->pdev;
858 size_t xlat_size, buff_size;
859 resource_size_t xlat_align;
860 resource_size_t xlat_align_size;
866 rc = ntb_mw_get_align(nt->ndev, PIDX, num_mw, &xlat_align,
867 &xlat_align_size, NULL);
871 xlat_size = round_up(size, xlat_align_size);
872 buff_size = round_up(size, xlat_align);
874 /* No need to re-setup */
875 if (mw->xlat_size == xlat_size)
879 ntb_free_mw(nt, num_mw);
881 /* Alloc memory for receiving data. Must be aligned */
882 mw->xlat_size = xlat_size;
883 mw->buff_size = buff_size;
884 mw->alloc_size = buff_size;
886 rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
889 rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
892 "Unable to alloc aligned MW buff\n");
900 /* Notify HW the memory location of the receive buffer */
901 rc = ntb_mw_set_trans(nt->ndev, PIDX, num_mw, mw->dma_addr,
904 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
905 ntb_free_mw(nt, num_mw);
912 static void ntb_qp_link_context_reset(struct ntb_transport_qp *qp)
914 qp->link_is_up = false;
921 qp->rx_ring_empty = 0;
922 qp->rx_err_no_buf = 0;
923 qp->rx_err_oflow = 0;
929 qp->tx_ring_full = 0;
930 qp->tx_err_no_buf = 0;
935 static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
937 ntb_qp_link_context_reset(qp);
938 if (qp->remote_rx_info)
939 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
942 static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
944 struct ntb_transport_ctx *nt = qp->transport;
945 struct pci_dev *pdev = nt->ndev->pdev;
947 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
949 cancel_delayed_work_sync(&qp->link_work);
950 ntb_qp_link_down_reset(qp);
952 if (qp->event_handler)
953 qp->event_handler(qp->cb_data, qp->link_is_up);
956 static void ntb_qp_link_cleanup_work(struct work_struct *work)
958 struct ntb_transport_qp *qp = container_of(work,
959 struct ntb_transport_qp,
961 struct ntb_transport_ctx *nt = qp->transport;
963 ntb_qp_link_cleanup(qp);
966 schedule_delayed_work(&qp->link_work,
967 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
970 static void ntb_qp_link_down(struct ntb_transport_qp *qp)
972 schedule_work(&qp->link_cleanup);
975 static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
977 struct ntb_transport_qp *qp;
979 unsigned int i, count;
981 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
983 /* Pass along the info to any clients */
984 for (i = 0; i < nt->qp_count; i++)
985 if (qp_bitmap_alloc & BIT_ULL(i)) {
987 ntb_qp_link_cleanup(qp);
988 cancel_work_sync(&qp->link_cleanup);
989 cancel_delayed_work_sync(&qp->link_work);
993 cancel_delayed_work_sync(&nt->link_work);
995 for (i = 0; i < nt->mw_count; i++)
998 /* The scratchpad registers keep the values if the remote side
999 * goes down, blast them now to give them a sane value the next
1000 * time they are accessed
1002 count = ntb_spad_count(nt->ndev);
1003 for (i = 0; i < count; i++)
1004 ntb_spad_write(nt->ndev, i, 0);
1007 static void ntb_transport_link_cleanup_work(struct work_struct *work)
1009 struct ntb_transport_ctx *nt =
1010 container_of(work, struct ntb_transport_ctx, link_cleanup);
1012 ntb_transport_link_cleanup(nt);
1015 static void ntb_transport_event_callback(void *data)
1017 struct ntb_transport_ctx *nt = data;
1019 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
1020 schedule_delayed_work(&nt->link_work, 0);
1022 schedule_work(&nt->link_cleanup);
1025 static void ntb_transport_link_work(struct work_struct *work)
1027 struct ntb_transport_ctx *nt =
1028 container_of(work, struct ntb_transport_ctx, link_work.work);
1029 struct ntb_dev *ndev = nt->ndev;
1030 struct pci_dev *pdev = ndev->pdev;
1031 resource_size_t size;
1033 int rc = 0, i, spad;
1035 /* send the local info, in the opposite order of the way we read it */
1038 rc = ntb_msi_setup_mws(ndev);
1040 dev_warn(&pdev->dev,
1041 "Failed to register MSI memory window: %d\n",
1043 nt->use_msi = false;
1047 for (i = 0; i < nt->qp_count; i++)
1048 ntb_transport_setup_qp_msi(nt, i);
1050 for (i = 0; i < nt->mw_count; i++) {
1051 size = nt->mw_vec[i].phys_size;
1053 if (max_mw_size && size > max_mw_size)
1056 spad = MW0_SZ_HIGH + (i * 2);
1057 ntb_peer_spad_write(ndev, PIDX, spad, upper_32_bits(size));
1059 spad = MW0_SZ_LOW + (i * 2);
1060 ntb_peer_spad_write(ndev, PIDX, spad, lower_32_bits(size));
1063 ntb_peer_spad_write(ndev, PIDX, NUM_MWS, nt->mw_count);
1065 ntb_peer_spad_write(ndev, PIDX, NUM_QPS, nt->qp_count);
1067 ntb_peer_spad_write(ndev, PIDX, VERSION, NTB_TRANSPORT_VERSION);
1069 /* Query the remote side for its info */
1070 val = ntb_spad_read(ndev, VERSION);
1071 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
1072 if (val != NTB_TRANSPORT_VERSION)
1075 val = ntb_spad_read(ndev, NUM_QPS);
1076 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
1077 if (val != nt->qp_count)
1080 val = ntb_spad_read(ndev, NUM_MWS);
1081 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
1082 if (val != nt->mw_count)
1085 for (i = 0; i < nt->mw_count; i++) {
1088 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
1089 val64 = (u64)val << 32;
1091 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
1094 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
1096 rc = ntb_set_mw(nt, i, val64);
1101 nt->link_is_up = true;
1103 for (i = 0; i < nt->qp_count; i++) {
1104 struct ntb_transport_qp *qp = &nt->qp_vec[i];
1106 ntb_transport_setup_qp_mw(nt, i);
1107 ntb_transport_setup_qp_peer_msi(nt, i);
1109 if (qp->client_ready)
1110 schedule_delayed_work(&qp->link_work, 0);
1116 for (i = 0; i < nt->mw_count; i++)
1119 /* if there's an actual failure, we should just bail */
1124 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
1125 schedule_delayed_work(&nt->link_work,
1126 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
1129 static void ntb_qp_link_work(struct work_struct *work)
1131 struct ntb_transport_qp *qp = container_of(work,
1132 struct ntb_transport_qp,
1134 struct pci_dev *pdev = qp->ndev->pdev;
1135 struct ntb_transport_ctx *nt = qp->transport;
1138 WARN_ON(!nt->link_is_up);
1140 val = ntb_spad_read(nt->ndev, QP_LINKS);
1142 ntb_peer_spad_write(nt->ndev, PIDX, QP_LINKS, val | BIT(qp->qp_num));
1144 /* query remote spad for qp ready bits */
1145 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
1147 /* See if the remote side is up */
1148 if (val & BIT(qp->qp_num)) {
1149 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
1150 qp->link_is_up = true;
1153 if (qp->event_handler)
1154 qp->event_handler(qp->cb_data, qp->link_is_up);
1157 tasklet_schedule(&qp->rxc_db_work);
1158 } else if (nt->link_is_up)
1159 schedule_delayed_work(&qp->link_work,
1160 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
1163 static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
1164 unsigned int qp_num)
1166 struct ntb_transport_qp *qp;
1167 phys_addr_t mw_base;
1168 resource_size_t mw_size;
1169 unsigned int num_qps_mw, tx_size;
1170 unsigned int mw_num, mw_count, qp_count;
1173 mw_count = nt->mw_count;
1174 qp_count = nt->qp_count;
1176 mw_num = QP_TO_MW(nt, qp_num);
1178 qp = &nt->qp_vec[qp_num];
1179 qp->qp_num = qp_num;
1181 qp->ndev = nt->ndev;
1182 qp->client_ready = false;
1183 qp->event_handler = NULL;
1184 ntb_qp_link_context_reset(qp);
1186 if (mw_num < qp_count % mw_count)
1187 num_qps_mw = qp_count / mw_count + 1;
1189 num_qps_mw = qp_count / mw_count;
1191 mw_base = nt->mw_vec[mw_num].phys_addr;
1192 mw_size = nt->mw_vec[mw_num].phys_size;
1194 if (max_mw_size && mw_size > max_mw_size)
1195 mw_size = max_mw_size;
1197 tx_size = (unsigned int)mw_size / num_qps_mw;
1198 qp_offset = tx_size * (qp_num / mw_count);
1200 qp->tx_mw_size = tx_size;
1201 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
1205 qp->tx_mw_phys = mw_base + qp_offset;
1206 if (!qp->tx_mw_phys)
1209 tx_size -= sizeof(struct ntb_rx_info);
1210 qp->rx_info = qp->tx_mw + tx_size;
1212 /* Due to housekeeping, there must be atleast 2 buffs */
1213 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
1214 qp->tx_max_entry = tx_size / qp->tx_max_frame;
1216 if (nt->debugfs_node_dir) {
1217 char debugfs_name[4];
1219 snprintf(debugfs_name, 4, "qp%d", qp_num);
1220 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
1221 nt->debugfs_node_dir);
1223 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1224 qp->debugfs_dir, qp,
1225 &ntb_qp_debugfs_stats);
1227 qp->debugfs_dir = NULL;
1228 qp->debugfs_stats = NULL;
1231 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
1232 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
1234 spin_lock_init(&qp->ntb_rx_q_lock);
1235 spin_lock_init(&qp->ntb_tx_free_q_lock);
1237 INIT_LIST_HEAD(&qp->rx_post_q);
1238 INIT_LIST_HEAD(&qp->rx_pend_q);
1239 INIT_LIST_HEAD(&qp->rx_free_q);
1240 INIT_LIST_HEAD(&qp->tx_free_q);
1242 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1248 static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
1250 struct ntb_transport_ctx *nt;
1251 struct ntb_transport_mw *mw;
1252 unsigned int mw_count, qp_count, spad_count, max_mw_count_for_spads;
1257 mw_count = ntb_peer_mw_count(ndev);
1259 if (!ndev->ops->mw_set_trans) {
1260 dev_err(&ndev->dev, "Inbound MW based NTB API is required\n");
1264 if (ntb_db_is_unsafe(ndev))
1266 "doorbell is unsafe, proceed anyway...\n");
1267 if (ntb_spad_is_unsafe(ndev))
1269 "scratchpad is unsafe, proceed anyway...\n");
1271 if (ntb_peer_port_count(ndev) != NTB_DEF_PEER_CNT)
1272 dev_warn(&ndev->dev, "Multi-port NTB devices unsupported\n");
1274 node = dev_to_node(&ndev->dev);
1276 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
1283 * If we are using MSI, and have at least one extra memory window,
1284 * we will reserve the last MW for the MSI window.
1286 if (use_msi && mw_count > 1) {
1287 rc = ntb_msi_init(ndev, ntb_transport_msi_desc_changed);
1294 spad_count = ntb_spad_count(ndev);
1296 /* Limit the MW's based on the availability of scratchpads */
1298 if (spad_count < NTB_TRANSPORT_MIN_SPADS) {
1304 max_mw_count_for_spads = (spad_count - MW0_SZ_HIGH) / 2;
1305 nt->mw_count = min(mw_count, max_mw_count_for_spads);
1307 nt->msi_spad_offset = nt->mw_count * 2 + MW0_SZ_HIGH;
1309 nt->mw_vec = kcalloc_node(mw_count, sizeof(*nt->mw_vec),
1316 for (i = 0; i < mw_count; i++) {
1317 mw = &nt->mw_vec[i];
1319 rc = ntb_peer_mw_get_addr(ndev, i, &mw->phys_addr,
1324 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
1332 mw->virt_addr = NULL;
1336 qp_bitmap = ntb_db_valid_mask(ndev);
1338 qp_count = ilog2(qp_bitmap);
1341 nt->msi_db_mask = 1 << qp_count;
1342 ntb_db_clear_mask(ndev, nt->msi_db_mask);
1345 if (max_num_clients && max_num_clients < qp_count)
1346 qp_count = max_num_clients;
1347 else if (nt->mw_count < qp_count)
1348 qp_count = nt->mw_count;
1350 qp_bitmap &= BIT_ULL(qp_count) - 1;
1352 nt->qp_count = qp_count;
1353 nt->qp_bitmap = qp_bitmap;
1354 nt->qp_bitmap_free = qp_bitmap;
1356 nt->qp_vec = kcalloc_node(qp_count, sizeof(*nt->qp_vec),
1363 if (nt_debugfs_dir) {
1364 nt->debugfs_node_dir =
1365 debugfs_create_dir(pci_name(ndev->pdev),
1369 for (i = 0; i < qp_count; i++) {
1370 rc = ntb_transport_init_queue(nt, i);
1375 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
1376 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
1378 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
1382 INIT_LIST_HEAD(&nt->client_devs);
1383 rc = ntb_bus_init(nt);
1387 nt->link_is_up = false;
1388 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1389 ntb_link_event(ndev);
1394 ntb_clear_ctx(ndev);
1399 mw = &nt->mw_vec[i];
1408 static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
1410 struct ntb_transport_ctx *nt = ndev->ctx;
1411 struct ntb_transport_qp *qp;
1412 u64 qp_bitmap_alloc;
1415 ntb_transport_link_cleanup(nt);
1416 cancel_work_sync(&nt->link_cleanup);
1417 cancel_delayed_work_sync(&nt->link_work);
1419 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
1421 /* verify that all the qp's are freed */
1422 for (i = 0; i < nt->qp_count; i++) {
1423 qp = &nt->qp_vec[i];
1424 if (qp_bitmap_alloc & BIT_ULL(i))
1425 ntb_transport_free_queue(qp);
1426 debugfs_remove_recursive(qp->debugfs_dir);
1429 ntb_link_disable(ndev);
1430 ntb_clear_ctx(ndev);
1434 for (i = nt->mw_count; i--; ) {
1436 iounmap(nt->mw_vec[i].vbase);
1444 static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1446 struct ntb_queue_entry *entry;
1449 unsigned long irqflags;
1451 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1453 while (!list_empty(&qp->rx_post_q)) {
1454 entry = list_first_entry(&qp->rx_post_q,
1455 struct ntb_queue_entry, entry);
1456 if (!(entry->flags & DESC_DONE_FLAG))
1459 entry->rx_hdr->flags = 0;
1460 iowrite32(entry->rx_index, &qp->rx_info->entry);
1462 cb_data = entry->cb_data;
1465 list_move_tail(&entry->entry, &qp->rx_free_q);
1467 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1469 if (qp->rx_handler && qp->client_ready)
1470 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1472 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1475 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1478 static void ntb_rx_copy_callback(void *data,
1479 const struct dmaengine_result *res)
1481 struct ntb_queue_entry *entry = data;
1483 /* we need to check DMA results if we are using DMA */
1485 enum dmaengine_tx_result dma_err = res->result;
1488 case DMA_TRANS_READ_FAILED:
1489 case DMA_TRANS_WRITE_FAILED:
1492 case DMA_TRANS_ABORTED:
1494 struct ntb_transport_qp *qp = entry->qp;
1495 void *offset = qp->rx_buff + qp->rx_max_frame *
1498 ntb_memcpy_rx(entry, offset);
1503 case DMA_TRANS_NOERROR:
1509 entry->flags |= DESC_DONE_FLAG;
1511 ntb_complete_rxc(entry->qp);
1514 static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1516 void *buf = entry->buf;
1517 size_t len = entry->len;
1519 memcpy(buf, offset, len);
1521 /* Ensure that the data is fully copied out before clearing the flag */
1524 ntb_rx_copy_callback(entry, NULL);
1527 static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
1529 struct dma_async_tx_descriptor *txd;
1530 struct ntb_transport_qp *qp = entry->qp;
1531 struct dma_chan *chan = qp->rx_dma_chan;
1532 struct dma_device *device;
1533 size_t pay_off, buff_off, len;
1534 struct dmaengine_unmap_data *unmap;
1535 dma_cookie_t cookie;
1536 void *buf = entry->buf;
1539 device = chan->device;
1540 pay_off = (size_t)offset & ~PAGE_MASK;
1541 buff_off = (size_t)buf & ~PAGE_MASK;
1543 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
1546 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1551 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1552 pay_off, len, DMA_TO_DEVICE);
1553 if (dma_mapping_error(device->dev, unmap->addr[0]))
1558 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1559 buff_off, len, DMA_FROM_DEVICE);
1560 if (dma_mapping_error(device->dev, unmap->addr[1]))
1563 unmap->from_cnt = 1;
1565 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
1566 unmap->addr[0], len,
1567 DMA_PREP_INTERRUPT);
1571 txd->callback_result = ntb_rx_copy_callback;
1572 txd->callback_param = entry;
1573 dma_set_unmap(txd, unmap);
1575 cookie = dmaengine_submit(txd);
1576 if (dma_submit_error(cookie))
1579 dmaengine_unmap_put(unmap);
1581 qp->last_cookie = cookie;
1588 dmaengine_unmap_put(unmap);
1590 dmaengine_unmap_put(unmap);
1595 static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
1597 struct ntb_transport_qp *qp = entry->qp;
1598 struct dma_chan *chan = qp->rx_dma_chan;
1604 if (entry->len < copy_bytes)
1607 res = ntb_async_rx_submit(entry, offset);
1611 if (!entry->retries)
1617 ntb_memcpy_rx(entry, offset);
1621 static int ntb_process_rxc(struct ntb_transport_qp *qp)
1623 struct ntb_payload_header *hdr;
1624 struct ntb_queue_entry *entry;
1627 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1628 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1630 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1631 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
1633 if (!(hdr->flags & DESC_DONE_FLAG)) {
1634 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
1635 qp->rx_ring_empty++;
1639 if (hdr->flags & LINK_DOWN_FLAG) {
1640 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1641 ntb_qp_link_down(qp);
1646 if (hdr->ver != (u32)qp->rx_pkts) {
1647 dev_dbg(&qp->ndev->pdev->dev,
1648 "version mismatch, expected %llu - got %u\n",
1649 qp->rx_pkts, hdr->ver);
1654 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
1656 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1657 qp->rx_err_no_buf++;
1661 entry->rx_hdr = hdr;
1662 entry->rx_index = qp->rx_index;
1664 if (hdr->len > entry->len) {
1665 dev_dbg(&qp->ndev->pdev->dev,
1666 "receive buffer overflow! Wanted %d got %d\n",
1667 hdr->len, entry->len);
1671 entry->flags |= DESC_DONE_FLAG;
1673 ntb_complete_rxc(qp);
1675 dev_dbg(&qp->ndev->pdev->dev,
1676 "RX OK index %u ver %u size %d into buf size %d\n",
1677 qp->rx_index, hdr->ver, hdr->len, entry->len);
1679 qp->rx_bytes += hdr->len;
1682 entry->len = hdr->len;
1684 ntb_async_rx(entry, offset);
1688 qp->rx_index %= qp->rx_max_entry;
1693 static void ntb_transport_rxc_db(unsigned long data)
1695 struct ntb_transport_qp *qp = (void *)data;
1698 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1699 __func__, qp->qp_num);
1701 /* Limit the number of packets processed in a single interrupt to
1702 * provide fairness to others
1704 for (i = 0; i < qp->rx_max_entry; i++) {
1705 rc = ntb_process_rxc(qp);
1710 if (i && qp->rx_dma_chan)
1711 dma_async_issue_pending(qp->rx_dma_chan);
1713 if (i == qp->rx_max_entry) {
1714 /* there is more work to do */
1716 tasklet_schedule(&qp->rxc_db_work);
1717 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1718 /* the doorbell bit is set: clear it */
1719 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1720 /* ntb_db_read ensures ntb_db_clear write is committed */
1721 ntb_db_read(qp->ndev);
1723 /* an interrupt may have arrived between finishing
1724 * ntb_process_rxc and clearing the doorbell bit:
1725 * there might be some more work to do.
1728 tasklet_schedule(&qp->rxc_db_work);
1732 static void ntb_tx_copy_callback(void *data,
1733 const struct dmaengine_result *res)
1735 struct ntb_queue_entry *entry = data;
1736 struct ntb_transport_qp *qp = entry->qp;
1737 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
1739 /* we need to check DMA results if we are using DMA */
1741 enum dmaengine_tx_result dma_err = res->result;
1744 case DMA_TRANS_READ_FAILED:
1745 case DMA_TRANS_WRITE_FAILED:
1748 case DMA_TRANS_ABORTED:
1750 void __iomem *offset =
1751 qp->tx_mw + qp->tx_max_frame *
1754 /* resubmit via CPU */
1755 ntb_memcpy_tx(entry, offset);
1760 case DMA_TRANS_NOERROR:
1766 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
1769 ntb_msi_peer_trigger(qp->ndev, PIDX, &qp->peer_msi_desc);
1771 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
1773 /* The entry length can only be zero if the packet is intended to be a
1774 * "link down" or similar. Since no payload is being sent in these
1775 * cases, there is nothing to add to the completion queue.
1777 if (entry->len > 0) {
1778 qp->tx_bytes += entry->len;
1781 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1785 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1788 static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1790 #ifdef ARCH_HAS_NOCACHE_UACCESS
1792 * Using non-temporal mov to improve performance on non-cached
1793 * writes, even though we aren't actually copying from user space.
1795 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1797 memcpy_toio(offset, entry->buf, entry->len);
1800 /* Ensure that the data is fully copied out before setting the flags */
1803 ntb_tx_copy_callback(entry, NULL);
1806 static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1807 struct ntb_queue_entry *entry)
1809 struct dma_async_tx_descriptor *txd;
1810 struct dma_chan *chan = qp->tx_dma_chan;
1811 struct dma_device *device;
1812 size_t len = entry->len;
1813 void *buf = entry->buf;
1814 size_t dest_off, buff_off;
1815 struct dmaengine_unmap_data *unmap;
1817 dma_cookie_t cookie;
1819 device = chan->device;
1820 dest = qp->tx_mw_dma_addr + qp->tx_max_frame * entry->tx_index;
1821 buff_off = (size_t)buf & ~PAGE_MASK;
1822 dest_off = (size_t)dest & ~PAGE_MASK;
1824 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1827 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1832 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1833 buff_off, len, DMA_TO_DEVICE);
1834 if (dma_mapping_error(device->dev, unmap->addr[0]))
1839 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
1840 DMA_PREP_INTERRUPT);
1844 txd->callback_result = ntb_tx_copy_callback;
1845 txd->callback_param = entry;
1846 dma_set_unmap(txd, unmap);
1848 cookie = dmaengine_submit(txd);
1849 if (dma_submit_error(cookie))
1852 dmaengine_unmap_put(unmap);
1854 dma_async_issue_pending(chan);
1858 dmaengine_unmap_put(unmap);
1860 dmaengine_unmap_put(unmap);
1865 static void ntb_async_tx(struct ntb_transport_qp *qp,
1866 struct ntb_queue_entry *entry)
1868 struct ntb_payload_header __iomem *hdr;
1869 struct dma_chan *chan = qp->tx_dma_chan;
1870 void __iomem *offset;
1873 entry->tx_index = qp->tx_index;
1874 offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
1875 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1876 entry->tx_hdr = hdr;
1878 iowrite32(entry->len, &hdr->len);
1879 iowrite32((u32)qp->tx_pkts, &hdr->ver);
1884 if (entry->len < copy_bytes)
1887 res = ntb_async_tx_submit(qp, entry);
1891 if (!entry->retries)
1897 ntb_memcpy_tx(entry, offset);
1901 static int ntb_process_tx(struct ntb_transport_qp *qp,
1902 struct ntb_queue_entry *entry)
1904 if (!ntb_transport_tx_free_entry(qp)) {
1909 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
1911 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
1913 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1918 ntb_async_tx(qp, entry);
1921 qp->tx_index %= qp->tx_max_entry;
1928 static void ntb_send_link_down(struct ntb_transport_qp *qp)
1930 struct pci_dev *pdev = qp->ndev->pdev;
1931 struct ntb_queue_entry *entry;
1934 if (!qp->link_is_up)
1937 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
1939 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1940 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1949 entry->cb_data = NULL;
1952 entry->flags = LINK_DOWN_FLAG;
1954 rc = ntb_process_tx(qp, entry);
1956 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1959 ntb_qp_link_down_reset(qp);
1962 static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1964 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1968 * ntb_transport_create_queue - Create a new NTB transport layer queue
1969 * @rx_handler: receive callback function
1970 * @tx_handler: transmit callback function
1971 * @event_handler: event callback function
1973 * Create a new NTB transport layer queue and provide the queue with a callback
1974 * routine for both transmit and receive. The receive callback routine will be
1975 * used to pass up data when the transport has received it on the queue. The
1976 * transmit callback routine will be called when the transport has completed the
1977 * transmission of the data on the queue and the data is ready to be freed.
1979 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1981 struct ntb_transport_qp *
1982 ntb_transport_create_queue(void *data, struct device *client_dev,
1983 const struct ntb_queue_handlers *handlers)
1985 struct ntb_dev *ndev;
1986 struct pci_dev *pdev;
1987 struct ntb_transport_ctx *nt;
1988 struct ntb_queue_entry *entry;
1989 struct ntb_transport_qp *qp;
1991 unsigned int free_queue;
1992 dma_cap_mask_t dma_mask;
1996 ndev = dev_ntb(client_dev->parent);
2000 node = dev_to_node(&ndev->dev);
2002 free_queue = ffs(nt->qp_bitmap_free);
2006 /* decrement free_queue to make it zero based */
2009 qp = &nt->qp_vec[free_queue];
2010 qp_bit = BIT_ULL(qp->qp_num);
2012 nt->qp_bitmap_free &= ~qp_bit;
2015 qp->rx_handler = handlers->rx_handler;
2016 qp->tx_handler = handlers->tx_handler;
2017 qp->event_handler = handlers->event_handler;
2019 dma_cap_zero(dma_mask);
2020 dma_cap_set(DMA_MEMCPY, dma_mask);
2024 dma_request_channel(dma_mask, ntb_dma_filter_fn,
2025 (void *)(unsigned long)node);
2026 if (!qp->tx_dma_chan)
2027 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
2030 dma_request_channel(dma_mask, ntb_dma_filter_fn,
2031 (void *)(unsigned long)node);
2032 if (!qp->rx_dma_chan)
2033 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
2035 qp->tx_dma_chan = NULL;
2036 qp->rx_dma_chan = NULL;
2039 qp->tx_mw_dma_addr = 0;
2040 if (qp->tx_dma_chan) {
2041 qp->tx_mw_dma_addr =
2042 dma_map_resource(qp->tx_dma_chan->device->dev,
2043 qp->tx_mw_phys, qp->tx_mw_size,
2044 DMA_FROM_DEVICE, 0);
2045 if (dma_mapping_error(qp->tx_dma_chan->device->dev,
2046 qp->tx_mw_dma_addr)) {
2047 qp->tx_mw_dma_addr = 0;
2052 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
2053 qp->tx_dma_chan ? "DMA" : "CPU");
2055 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
2056 qp->rx_dma_chan ? "DMA" : "CPU");
2058 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
2059 entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
2064 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
2067 qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
2069 for (i = 0; i < qp->tx_max_entry; i++) {
2070 entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
2075 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2079 ntb_db_clear(qp->ndev, qp_bit);
2080 ntb_db_clear_mask(qp->ndev, qp_bit);
2082 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
2087 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
2090 qp->rx_alloc_entry = 0;
2091 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
2093 if (qp->tx_mw_dma_addr)
2094 dma_unmap_resource(qp->tx_dma_chan->device->dev,
2095 qp->tx_mw_dma_addr, qp->tx_mw_size,
2096 DMA_FROM_DEVICE, 0);
2097 if (qp->tx_dma_chan)
2098 dma_release_channel(qp->tx_dma_chan);
2099 if (qp->rx_dma_chan)
2100 dma_release_channel(qp->rx_dma_chan);
2101 nt->qp_bitmap_free |= qp_bit;
2105 EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
2108 * ntb_transport_free_queue - Frees NTB transport queue
2109 * @qp: NTB queue to be freed
2111 * Frees NTB transport queue
2113 void ntb_transport_free_queue(struct ntb_transport_qp *qp)
2115 struct pci_dev *pdev;
2116 struct ntb_queue_entry *entry;
2122 pdev = qp->ndev->pdev;
2126 if (qp->tx_dma_chan) {
2127 struct dma_chan *chan = qp->tx_dma_chan;
2128 /* Putting the dma_chan to NULL will force any new traffic to be
2129 * processed by the CPU instead of the DAM engine
2131 qp->tx_dma_chan = NULL;
2133 /* Try to be nice and wait for any queued DMA engine
2134 * transactions to process before smashing it with a rock
2136 dma_sync_wait(chan, qp->last_cookie);
2137 dmaengine_terminate_all(chan);
2139 dma_unmap_resource(chan->device->dev,
2140 qp->tx_mw_dma_addr, qp->tx_mw_size,
2141 DMA_FROM_DEVICE, 0);
2143 dma_release_channel(chan);
2146 if (qp->rx_dma_chan) {
2147 struct dma_chan *chan = qp->rx_dma_chan;
2148 /* Putting the dma_chan to NULL will force any new traffic to be
2149 * processed by the CPU instead of the DAM engine
2151 qp->rx_dma_chan = NULL;
2153 /* Try to be nice and wait for any queued DMA engine
2154 * transactions to process before smashing it with a rock
2156 dma_sync_wait(chan, qp->last_cookie);
2157 dmaengine_terminate_all(chan);
2158 dma_release_channel(chan);
2161 qp_bit = BIT_ULL(qp->qp_num);
2163 ntb_db_set_mask(qp->ndev, qp_bit);
2164 tasklet_kill(&qp->rxc_db_work);
2166 cancel_delayed_work_sync(&qp->link_work);
2169 qp->rx_handler = NULL;
2170 qp->tx_handler = NULL;
2171 qp->event_handler = NULL;
2173 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
2176 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
2177 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
2181 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
2182 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
2186 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
2189 qp->transport->qp_bitmap_free |= qp_bit;
2191 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
2193 EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
2196 * ntb_transport_rx_remove - Dequeues enqueued rx packet
2197 * @qp: NTB queue to be freed
2198 * @len: pointer to variable to write enqueued buffers length
2200 * Dequeues unused buffers from receive queue. Should only be used during
2203 * RETURNS: NULL error value on error, or void* for success.
2205 void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
2207 struct ntb_queue_entry *entry;
2210 if (!qp || qp->client_ready)
2213 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
2217 buf = entry->cb_data;
2220 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
2224 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
2227 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
2228 * @qp: NTB transport layer queue the entry is to be enqueued on
2229 * @cb: per buffer pointer for callback function to use
2230 * @data: pointer to data buffer that incoming packets will be copied into
2231 * @len: length of the data buffer
2233 * Enqueue a new receive buffer onto the transport queue into which a NTB
2234 * payload can be received into.
2236 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2238 int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2241 struct ntb_queue_entry *entry;
2246 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
2250 entry->cb_data = cb;
2256 entry->rx_index = 0;
2258 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
2261 tasklet_schedule(&qp->rxc_db_work);
2265 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2268 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2269 * @qp: NTB transport layer queue the entry is to be enqueued on
2270 * @cb: per buffer pointer for callback function to use
2271 * @data: pointer to data buffer that will be sent
2272 * @len: length of the data buffer
2274 * Enqueue a new transmit buffer onto the transport queue from which a NTB
2275 * payload will be transmitted. This assumes that a lock is being held to
2276 * serialize access to the qp.
2278 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2280 int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2283 struct ntb_queue_entry *entry;
2289 /* If the qp link is down already, just ignore. */
2290 if (!qp->link_is_up)
2293 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
2295 qp->tx_err_no_buf++;
2299 entry->cb_data = cb;
2305 entry->tx_index = 0;
2307 rc = ntb_process_tx(qp, entry);
2309 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2314 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2317 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2318 * @qp: NTB transport layer queue to be enabled
2320 * Notify NTB transport layer of client readiness to use queue
2322 void ntb_transport_link_up(struct ntb_transport_qp *qp)
2327 qp->client_ready = true;
2329 if (qp->transport->link_is_up)
2330 schedule_delayed_work(&qp->link_work, 0);
2332 EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2335 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2336 * @qp: NTB transport layer queue to be disabled
2338 * Notify NTB transport layer of client's desire to no longer receive data on
2339 * transport queue specified. It is the client's responsibility to ensure all
2340 * entries on queue are purged or otherwise handled appropriately.
2342 void ntb_transport_link_down(struct ntb_transport_qp *qp)
2349 qp->client_ready = false;
2351 val = ntb_spad_read(qp->ndev, QP_LINKS);
2353 ntb_peer_spad_write(qp->ndev, PIDX, QP_LINKS, val & ~BIT(qp->qp_num));
2356 ntb_send_link_down(qp);
2358 cancel_delayed_work_sync(&qp->link_work);
2360 EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2363 * ntb_transport_link_query - Query transport link state
2364 * @qp: NTB transport layer queue to be queried
2366 * Query connectivity to the remote system of the NTB transport queue
2368 * RETURNS: true for link up or false for link down
2370 bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2375 return qp->link_is_up;
2377 EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2380 * ntb_transport_qp_num - Query the qp number
2381 * @qp: NTB transport layer queue to be queried
2383 * Query qp number of the NTB transport queue
2385 * RETURNS: a zero based number specifying the qp number
2387 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2394 EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2397 * ntb_transport_max_size - Query the max payload size of a qp
2398 * @qp: NTB transport layer queue to be queried
2400 * Query the maximum payload size permissible on the given qp
2402 * RETURNS: the max payload size of a qp
2404 unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
2406 unsigned int max_size;
2407 unsigned int copy_align;
2408 struct dma_chan *rx_chan, *tx_chan;
2413 rx_chan = qp->rx_dma_chan;
2414 tx_chan = qp->tx_dma_chan;
2416 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2417 tx_chan ? tx_chan->device->copy_align : 0);
2419 /* If DMA engine usage is possible, try to find the max size for that */
2420 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2421 max_size = round_down(max_size, 1 << copy_align);
2425 EXPORT_SYMBOL_GPL(ntb_transport_max_size);
2427 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2429 unsigned int head = qp->tx_index;
2430 unsigned int tail = qp->remote_rx_info->entry;
2432 return tail >= head ? tail - head : qp->tx_max_entry + tail - head;
2434 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2436 static void ntb_transport_doorbell_callback(void *data, int vector)
2438 struct ntb_transport_ctx *nt = data;
2439 struct ntb_transport_qp *qp;
2441 unsigned int qp_num;
2443 if (ntb_db_read(nt->ndev) & nt->msi_db_mask) {
2444 ntb_transport_msi_peer_desc_changed(nt);
2445 ntb_db_clear(nt->ndev, nt->msi_db_mask);
2448 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2449 ntb_db_vector_mask(nt->ndev, vector));
2452 qp_num = __ffs(db_bits);
2453 qp = &nt->qp_vec[qp_num];
2456 tasklet_schedule(&qp->rxc_db_work);
2458 db_bits &= ~BIT_ULL(qp_num);
2462 static const struct ntb_ctx_ops ntb_transport_ops = {
2463 .link_event = ntb_transport_event_callback,
2464 .db_event = ntb_transport_doorbell_callback,
2467 static struct ntb_client ntb_transport_client = {
2469 .probe = ntb_transport_probe,
2470 .remove = ntb_transport_free,
2474 static int __init ntb_transport_init(void)
2478 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2480 if (debugfs_initialized())
2481 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2483 rc = bus_register(&ntb_transport_bus);
2487 rc = ntb_register_client(&ntb_transport_client);
2494 bus_unregister(&ntb_transport_bus);
2496 debugfs_remove_recursive(nt_debugfs_dir);
2499 module_init(ntb_transport_init);
2501 static void __exit ntb_transport_exit(void)
2503 ntb_unregister_client(&ntb_transport_client);
2504 bus_unregister(&ntb_transport_bus);
2505 debugfs_remove_recursive(nt_debugfs_dir);
2507 module_exit(ntb_transport_exit);