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/dma-mapping.h>
51 #include <linux/errno.h>
52 #include <linux/export.h>
53 #include <linux/interrupt.h>
54 #include <linux/module.h>
55 #include <linux/pci.h>
56 #include <linux/slab.h>
57 #include <linux/types.h>
58 #include <linux/ntb.h>
61 #define NTB_TRANSPORT_VERSION 1
63 static unsigned int transport_mtu = 0x401E;
64 module_param(transport_mtu, uint, 0644);
65 MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
67 static unsigned char max_num_clients = 2;
68 module_param(max_num_clients, byte, 0644);
69 MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
71 struct ntb_queue_entry {
72 /* ntb_queue list reference */
73 struct list_head entry;
74 /* pointers to data to be transfered */
81 struct ntb_transport_qp {
82 struct ntb_transport *transport;
83 struct ntb_device *ndev;
88 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
90 void (*tx_handler) (struct ntb_transport_qp *qp, void *qp_data,
92 struct list_head tx_free_q;
93 spinlock_t ntb_tx_free_q_lock;
97 unsigned int tx_max_frame;
99 void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data,
100 void *data, int len);
101 struct tasklet_struct rx_work;
102 struct list_head rx_pend_q;
103 struct list_head rx_free_q;
104 spinlock_t ntb_rx_pend_q_lock;
105 spinlock_t ntb_rx_free_q_lock;
109 unsigned int rx_max_frame;
111 void (*event_handler) (void *data, int status);
112 struct delayed_work link_work;
113 struct work_struct link_cleanup;
115 struct dentry *debugfs_dir;
116 struct dentry *debugfs_stats;
130 struct ntb_transport_mw {
136 struct ntb_transport_client_dev {
137 struct list_head entry;
141 struct ntb_transport {
142 struct list_head entry;
143 struct list_head client_devs;
145 struct ntb_device *ndev;
146 struct ntb_transport_mw mw[NTB_NUM_MW];
147 struct ntb_transport_qp *qps;
148 unsigned int max_qps;
149 unsigned long qp_bitmap;
151 struct delayed_work link_work;
152 struct work_struct link_cleanup;
153 struct dentry *debugfs_dir;
157 DESC_DONE_FLAG = 1 << 0,
158 LINK_DOWN_FLAG = 1 << 1,
161 struct ntb_payload_header {
176 #define QP_TO_MW(qp) ((qp) % NTB_NUM_MW)
177 #define NTB_QP_DEF_NUM_ENTRIES 100
178 #define NTB_LINK_DOWN_TIMEOUT 10
180 static int ntb_match_bus(struct device *dev, struct device_driver *drv)
182 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
185 static int ntb_client_probe(struct device *dev)
187 const struct ntb_client *drv = container_of(dev->driver,
188 struct ntb_client, driver);
189 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
193 if (drv && drv->probe)
194 rc = drv->probe(pdev);
201 static int ntb_client_remove(struct device *dev)
203 const struct ntb_client *drv = container_of(dev->driver,
204 struct ntb_client, driver);
205 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
207 if (drv && drv->remove)
215 static struct bus_type ntb_bus_type = {
217 .match = ntb_match_bus,
218 .probe = ntb_client_probe,
219 .remove = ntb_client_remove,
222 static LIST_HEAD(ntb_transport_list);
224 static int ntb_bus_init(struct ntb_transport *nt)
226 if (list_empty(&ntb_transport_list)) {
227 int rc = bus_register(&ntb_bus_type);
232 list_add(&nt->entry, &ntb_transport_list);
237 static void ntb_bus_remove(struct ntb_transport *nt)
239 struct ntb_transport_client_dev *client_dev, *cd;
241 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
242 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
243 dev_name(&client_dev->dev));
244 list_del(&client_dev->entry);
245 device_unregister(&client_dev->dev);
248 list_del(&nt->entry);
250 if (list_empty(&ntb_transport_list))
251 bus_unregister(&ntb_bus_type);
254 static void ntb_client_release(struct device *dev)
256 struct ntb_transport_client_dev *client_dev;
257 client_dev = container_of(dev, struct ntb_transport_client_dev, dev);
263 * ntb_unregister_client_dev - Unregister NTB client device
264 * @device_name: Name of NTB client device
266 * Unregister an NTB client device with the NTB transport layer
268 void ntb_unregister_client_dev(char *device_name)
270 struct ntb_transport_client_dev *client, *cd;
271 struct ntb_transport *nt;
273 list_for_each_entry(nt, &ntb_transport_list, entry)
274 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
275 if (!strncmp(dev_name(&client->dev), device_name,
276 strlen(device_name))) {
277 list_del(&client->entry);
278 device_unregister(&client->dev);
281 EXPORT_SYMBOL_GPL(ntb_unregister_client_dev);
284 * ntb_register_client_dev - Register NTB client device
285 * @device_name: Name of NTB client device
287 * Register an NTB client device with the NTB transport layer
289 int ntb_register_client_dev(char *device_name)
291 struct ntb_transport_client_dev *client_dev;
292 struct ntb_transport *nt;
295 if (list_empty(&ntb_transport_list))
298 list_for_each_entry(nt, &ntb_transport_list, entry) {
301 client_dev = kzalloc(sizeof(struct ntb_transport_client_dev),
308 dev = &client_dev->dev;
310 /* setup and register client devices */
311 dev_set_name(dev, "%s", device_name);
312 dev->bus = &ntb_bus_type;
313 dev->release = ntb_client_release;
314 dev->parent = &ntb_query_pdev(nt->ndev)->dev;
316 rc = device_register(dev);
322 list_add_tail(&client_dev->entry, &nt->client_devs);
328 ntb_unregister_client_dev(device_name);
332 EXPORT_SYMBOL_GPL(ntb_register_client_dev);
335 * ntb_register_client - Register NTB client driver
336 * @drv: NTB client driver to be registered
338 * Register an NTB client driver with the NTB transport layer
340 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
342 int ntb_register_client(struct ntb_client *drv)
344 drv->driver.bus = &ntb_bus_type;
346 if (list_empty(&ntb_transport_list))
349 return driver_register(&drv->driver);
351 EXPORT_SYMBOL_GPL(ntb_register_client);
354 * ntb_unregister_client - Unregister NTB client driver
355 * @drv: NTB client driver to be unregistered
357 * Unregister an NTB client driver with the NTB transport layer
359 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
361 void ntb_unregister_client(struct ntb_client *drv)
363 driver_unregister(&drv->driver);
365 EXPORT_SYMBOL_GPL(ntb_unregister_client);
367 static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
370 struct ntb_transport_qp *qp;
372 ssize_t ret, out_offset, out_count;
376 qp = filp->private_data;
378 out_offset += snprintf(buf + out_offset, out_count - out_offset,
380 out_offset += snprintf(buf + out_offset, out_count - out_offset,
381 "rx_bytes - \t%llu\n", qp->rx_bytes);
382 out_offset += snprintf(buf + out_offset, out_count - out_offset,
383 "rx_pkts - \t%llu\n", qp->rx_pkts);
384 out_offset += snprintf(buf + out_offset, out_count - out_offset,
385 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
386 out_offset += snprintf(buf + out_offset, out_count - out_offset,
387 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
388 out_offset += snprintf(buf + out_offset, out_count - out_offset,
389 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
390 out_offset += snprintf(buf + out_offset, out_count - out_offset,
391 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
392 out_offset += snprintf(buf + out_offset, out_count - out_offset,
393 "rx_buff_begin - %p\n", qp->rx_buff_begin);
394 out_offset += snprintf(buf + out_offset, out_count - out_offset,
395 "rx_offset - \t%p\n", qp->rx_offset);
396 out_offset += snprintf(buf + out_offset, out_count - out_offset,
397 "rx_buff_end - \t%p\n", qp->rx_buff_end);
399 out_offset += snprintf(buf + out_offset, out_count - out_offset,
400 "tx_bytes - \t%llu\n", qp->tx_bytes);
401 out_offset += snprintf(buf + out_offset, out_count - out_offset,
402 "tx_pkts - \t%llu\n", qp->tx_pkts);
403 out_offset += snprintf(buf + out_offset, out_count - out_offset,
404 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
405 out_offset += snprintf(buf + out_offset, out_count - out_offset,
406 "tx_mw_begin - \t%p\n", qp->tx_mw_begin);
407 out_offset += snprintf(buf + out_offset, out_count - out_offset,
408 "tx_offset - \t%p\n", qp->tx_offset);
409 out_offset += snprintf(buf + out_offset, out_count - out_offset,
410 "tx_mw_end - \t%p\n", qp->tx_mw_end);
412 out_offset += snprintf(buf + out_offset, out_count - out_offset,
413 "QP Link %s\n", (qp->qp_link == NTB_LINK_UP) ?
416 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
420 static const struct file_operations ntb_qp_debugfs_stats = {
421 .owner = THIS_MODULE,
423 .read = debugfs_read,
426 static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
427 struct list_head *list)
431 spin_lock_irqsave(lock, flags);
432 list_add_tail(entry, list);
433 spin_unlock_irqrestore(lock, flags);
436 static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
437 struct list_head *list)
439 struct ntb_queue_entry *entry;
442 spin_lock_irqsave(lock, flags);
443 if (list_empty(list)) {
447 entry = list_first_entry(list, struct ntb_queue_entry, entry);
448 list_del(&entry->entry);
450 spin_unlock_irqrestore(lock, flags);
455 static void ntb_transport_setup_qp_mw(struct ntb_transport *nt,
458 struct ntb_transport_qp *qp = &nt->qps[qp_num];
459 unsigned int rx_size, num_qps_mw;
460 u8 mw_num = QP_TO_MW(qp_num);
463 WARN_ON(nt->mw[mw_num].virt_addr == 0);
465 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
466 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
468 num_qps_mw = nt->max_qps / NTB_NUM_MW;
470 rx_size = nt->mw[mw_num].size / num_qps_mw;
471 qp->rx_buff_begin = nt->mw[mw_num].virt_addr +
472 (qp_num / NTB_NUM_MW * rx_size);
473 qp->rx_buff_end = qp->rx_buff_begin + rx_size;
474 qp->rx_offset = qp->rx_buff_begin;
475 qp->rx_max_frame = min(transport_mtu, rx_size);
477 /* setup the hdr offsets with 0's */
478 for (offset = qp->rx_buff_begin + qp->rx_max_frame -
479 sizeof(struct ntb_payload_header);
480 offset < qp->rx_buff_end; offset += qp->rx_max_frame)
481 memset(offset, 0, sizeof(struct ntb_payload_header));
487 static int ntb_set_mw(struct ntb_transport *nt, int num_mw, unsigned int size)
489 struct ntb_transport_mw *mw = &nt->mw[num_mw];
490 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
492 /* Alloc memory for receiving data. Must be 4k aligned */
493 mw->size = ALIGN(size, 4096);
495 mw->virt_addr = dma_alloc_coherent(&pdev->dev, mw->size, &mw->dma_addr,
497 if (!mw->virt_addr) {
498 dev_err(&pdev->dev, "Unable to allocate MW buffer of size %d\n",
503 /* Notify HW the memory location of the receive buffer */
504 ntb_set_mw_addr(nt->ndev, num_mw, mw->dma_addr);
509 static void ntb_qp_link_cleanup(struct work_struct *work)
511 struct ntb_transport_qp *qp = container_of(work,
512 struct ntb_transport_qp,
514 struct ntb_transport *nt = qp->transport;
515 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
517 if (qp->qp_link == NTB_LINK_DOWN) {
518 cancel_delayed_work_sync(&qp->link_work);
522 if (qp->event_handler)
523 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
525 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
526 qp->qp_link = NTB_LINK_DOWN;
528 if (nt->transport_link == NTB_LINK_UP)
529 schedule_delayed_work(&qp->link_work,
530 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
533 static void ntb_qp_link_down(struct ntb_transport_qp *qp)
535 schedule_work(&qp->link_cleanup);
538 static void ntb_transport_link_cleanup(struct work_struct *work)
540 struct ntb_transport *nt = container_of(work, struct ntb_transport,
544 if (nt->transport_link == NTB_LINK_DOWN)
545 cancel_delayed_work_sync(&nt->link_work);
547 nt->transport_link = NTB_LINK_DOWN;
549 /* Pass along the info to any clients */
550 for (i = 0; i < nt->max_qps; i++)
551 if (!test_bit(i, &nt->qp_bitmap))
552 ntb_qp_link_down(&nt->qps[i]);
554 /* The scratchpad registers keep the values if the remote side
555 * goes down, blast them now to give them a sane value the next
556 * time they are accessed
558 for (i = 0; i < MAX_SPAD; i++)
559 ntb_write_local_spad(nt->ndev, i, 0);
562 static void ntb_transport_event_callback(void *data, enum ntb_hw_event event)
564 struct ntb_transport *nt = data;
567 case NTB_EVENT_HW_LINK_UP:
568 schedule_delayed_work(&nt->link_work, 0);
570 case NTB_EVENT_HW_LINK_DOWN:
571 schedule_work(&nt->link_cleanup);
578 static void ntb_transport_link_work(struct work_struct *work)
580 struct ntb_transport *nt = container_of(work, struct ntb_transport,
582 struct ntb_device *ndev = nt->ndev;
583 struct pci_dev *pdev = ntb_query_pdev(ndev);
587 /* send the local info */
588 rc = ntb_write_remote_spad(ndev, VERSION, NTB_TRANSPORT_VERSION);
590 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
595 rc = ntb_write_remote_spad(ndev, MW0_SZ, ntb_get_mw_size(ndev, 0));
597 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
598 (u32) ntb_get_mw_size(ndev, 0), MW0_SZ);
602 rc = ntb_write_remote_spad(ndev, MW1_SZ, ntb_get_mw_size(ndev, 1));
604 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
605 (u32) ntb_get_mw_size(ndev, 1), MW1_SZ);
609 rc = ntb_write_remote_spad(ndev, NUM_QPS, nt->max_qps);
611 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
612 nt->max_qps, NUM_QPS);
616 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
618 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
622 rc = ntb_write_remote_spad(ndev, QP_LINKS, val);
624 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
629 /* Query the remote side for its info */
630 rc = ntb_read_remote_spad(ndev, VERSION, &val);
632 dev_err(&pdev->dev, "Error reading remote spad %d\n", VERSION);
636 if (val != NTB_TRANSPORT_VERSION)
638 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
640 rc = ntb_read_remote_spad(ndev, NUM_QPS, &val);
642 dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_QPS);
646 if (val != nt->max_qps)
648 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
650 rc = ntb_read_remote_spad(ndev, MW0_SZ, &val);
652 dev_err(&pdev->dev, "Error reading remote spad %d\n", MW0_SZ);
658 dev_dbg(&pdev->dev, "Remote MW0 size = %d\n", val);
660 rc = ntb_set_mw(nt, 0, val);
664 rc = ntb_read_remote_spad(ndev, MW1_SZ, &val);
666 dev_err(&pdev->dev, "Error reading remote spad %d\n", MW1_SZ);
672 dev_dbg(&pdev->dev, "Remote MW1 size = %d\n", val);
674 rc = ntb_set_mw(nt, 1, val);
678 nt->transport_link = NTB_LINK_UP;
680 for (i = 0; i < nt->max_qps; i++) {
681 struct ntb_transport_qp *qp = &nt->qps[i];
683 ntb_transport_setup_qp_mw(nt, i);
685 if (qp->client_ready == NTB_LINK_UP)
686 schedule_delayed_work(&qp->link_work, 0);
692 if (ntb_hw_link_status(ndev))
693 schedule_delayed_work(&nt->link_work,
694 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
697 static void ntb_qp_link_work(struct work_struct *work)
699 struct ntb_transport_qp *qp = container_of(work,
700 struct ntb_transport_qp,
702 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
703 struct ntb_transport *nt = qp->transport;
706 WARN_ON(nt->transport_link != NTB_LINK_UP);
708 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
710 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
714 rc = ntb_write_remote_spad(nt->ndev, QP_LINKS, val | 1 << qp->qp_num);
716 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
717 val | 1 << qp->qp_num, QP_LINKS);
719 /* query remote spad for qp ready bits */
720 rc = ntb_read_remote_spad(nt->ndev, QP_LINKS, &val);
722 dev_err(&pdev->dev, "Error reading remote spad %d\n", QP_LINKS);
724 dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val);
726 /* See if the remote side is up */
727 if (1 << qp->qp_num & val) {
728 qp->qp_link = NTB_LINK_UP;
730 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
731 if (qp->event_handler)
732 qp->event_handler(qp->cb_data, NTB_LINK_UP);
733 } else if (nt->transport_link == NTB_LINK_UP)
734 schedule_delayed_work(&qp->link_work,
735 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
738 static void ntb_transport_init_queue(struct ntb_transport *nt,
741 struct ntb_transport_qp *qp;
742 unsigned int num_qps_mw, tx_size;
743 u8 mw_num = QP_TO_MW(qp_num);
745 qp = &nt->qps[qp_num];
749 qp->qp_link = NTB_LINK_DOWN;
750 qp->client_ready = NTB_LINK_DOWN;
751 qp->event_handler = NULL;
753 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
754 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
756 num_qps_mw = nt->max_qps / NTB_NUM_MW;
758 tx_size = ntb_get_mw_size(qp->ndev, mw_num) / num_qps_mw;
759 qp->tx_mw_begin = ntb_get_mw_vbase(nt->ndev, mw_num) +
760 (qp_num / NTB_NUM_MW * tx_size);
761 qp->tx_mw_end = qp->tx_mw_begin + tx_size;
762 qp->tx_offset = qp->tx_mw_begin;
763 qp->tx_max_frame = min(transport_mtu, tx_size);
765 if (nt->debugfs_dir) {
766 char debugfs_name[4];
768 snprintf(debugfs_name, 4, "qp%d", qp_num);
769 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
772 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
774 &ntb_qp_debugfs_stats);
777 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
778 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup);
780 spin_lock_init(&qp->ntb_rx_pend_q_lock);
781 spin_lock_init(&qp->ntb_rx_free_q_lock);
782 spin_lock_init(&qp->ntb_tx_free_q_lock);
784 INIT_LIST_HEAD(&qp->rx_pend_q);
785 INIT_LIST_HEAD(&qp->rx_free_q);
786 INIT_LIST_HEAD(&qp->tx_free_q);
789 int ntb_transport_init(struct pci_dev *pdev)
791 struct ntb_transport *nt;
794 nt = kzalloc(sizeof(struct ntb_transport), GFP_KERNEL);
798 if (debugfs_initialized())
799 nt->debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
801 nt->debugfs_dir = NULL;
803 nt->ndev = ntb_register_transport(pdev, nt);
809 nt->max_qps = min(nt->ndev->max_cbs, max_num_clients);
811 nt->qps = kcalloc(nt->max_qps, sizeof(struct ntb_transport_qp),
818 nt->qp_bitmap = ((u64) 1 << nt->max_qps) - 1;
820 for (i = 0; i < nt->max_qps; i++)
821 ntb_transport_init_queue(nt, i);
823 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
824 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup);
826 rc = ntb_register_event_callback(nt->ndev,
827 ntb_transport_event_callback);
831 INIT_LIST_HEAD(&nt->client_devs);
832 rc = ntb_bus_init(nt);
836 if (ntb_hw_link_status(nt->ndev))
837 schedule_delayed_work(&nt->link_work, 0);
842 ntb_unregister_event_callback(nt->ndev);
846 ntb_unregister_transport(nt->ndev);
848 debugfs_remove_recursive(nt->debugfs_dir);
853 void ntb_transport_free(void *transport)
855 struct ntb_transport *nt = transport;
856 struct pci_dev *pdev;
859 nt->transport_link = NTB_LINK_DOWN;
861 /* verify that all the qp's are freed */
862 for (i = 0; i < nt->max_qps; i++)
863 if (!test_bit(i, &nt->qp_bitmap))
864 ntb_transport_free_queue(&nt->qps[i]);
868 cancel_delayed_work_sync(&nt->link_work);
870 debugfs_remove_recursive(nt->debugfs_dir);
872 ntb_unregister_event_callback(nt->ndev);
874 pdev = ntb_query_pdev(nt->ndev);
876 for (i = 0; i < NTB_NUM_MW; i++)
877 if (nt->mw[i].virt_addr)
878 dma_free_coherent(&pdev->dev, nt->mw[i].size,
883 ntb_unregister_transport(nt->ndev);
887 static void ntb_rx_copy_task(struct ntb_transport_qp *qp,
888 struct ntb_queue_entry *entry, void *offset)
891 struct ntb_payload_header *hdr;
893 BUG_ON(offset < qp->rx_buff_begin ||
894 offset + qp->rx_max_frame >= qp->rx_buff_end);
896 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
897 entry->len = hdr->len;
899 memcpy(entry->buf, offset, entry->len);
901 /* Ensure that the data is fully copied out before clearing the flag */
905 if (qp->rx_handler && qp->client_ready == NTB_LINK_UP)
906 qp->rx_handler(qp, qp->cb_data, entry->cb_data, entry->len);
908 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
911 static int ntb_process_rxc(struct ntb_transport_qp *qp)
913 struct ntb_payload_header *hdr;
914 struct ntb_queue_entry *entry;
917 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
919 hdr = offset + qp->rx_max_frame -
920 sizeof(struct ntb_payload_header);
921 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
922 "no buffer - HDR ver %llu, len %d, flags %x\n",
923 hdr->ver, hdr->len, hdr->flags);
928 offset = qp->rx_offset;
929 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
931 if (!(hdr->flags & DESC_DONE_FLAG)) {
932 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
938 if (hdr->ver != qp->rx_pkts) {
939 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
940 "qp %d: version mismatch, expected %llu - got %llu\n",
941 qp->qp_num, qp->rx_pkts, hdr->ver);
942 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
948 if (hdr->flags & LINK_DOWN_FLAG) {
949 ntb_qp_link_down(qp);
951 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
954 /* Ensure that the data is fully copied out before clearing the
962 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
963 "rx offset %p, ver %llu - %d payload received, buf size %d\n",
964 qp->rx_offset, hdr->ver, hdr->len, entry->len);
966 if (hdr->len <= entry->len)
967 ntb_rx_copy_task(qp, entry, offset);
969 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
972 /* Ensure that the data is fully copied out before clearing the
978 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
979 "RX overflow! Wanted %d got %d\n",
980 hdr->len, entry->len);
983 qp->rx_bytes += hdr->len;
987 qp->rx_offset += qp->rx_max_frame;
988 if (qp->rx_offset + qp->rx_max_frame >= qp->rx_buff_end)
989 qp->rx_offset = qp->rx_buff_begin;
994 static void ntb_transport_rx(unsigned long data)
996 struct ntb_transport_qp *qp = (struct ntb_transport_qp *)data;
1000 rc = ntb_process_rxc(qp);
1004 static void ntb_transport_rxc_db(void *data, int db_num)
1006 struct ntb_transport_qp *qp = data;
1008 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%s: doorbell %d received\n",
1011 tasklet_schedule(&qp->rx_work);
1014 static void ntb_tx_copy_task(struct ntb_transport_qp *qp,
1015 struct ntb_queue_entry *entry,
1018 struct ntb_payload_header *hdr;
1020 BUG_ON(offset < qp->tx_mw_begin ||
1021 offset + qp->tx_max_frame >= qp->tx_mw_end);
1023 memcpy_toio(offset, entry->buf, entry->len);
1025 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1026 hdr->len = entry->len;
1027 hdr->ver = qp->tx_pkts;
1029 /* Ensure that the data is fully copied out before setting the flag */
1031 hdr->flags = entry->flags | DESC_DONE_FLAG;
1033 ntb_ring_sdb(qp->ndev, qp->qp_num);
1035 /* The entry length can only be zero if the packet is intended to be a
1036 * "link down" or similar. Since no payload is being sent in these
1037 * cases, there is nothing to add to the completion queue.
1039 if (entry->len > 0) {
1040 qp->tx_bytes += entry->len;
1043 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1047 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1050 static int ntb_process_tx(struct ntb_transport_qp *qp,
1051 struct ntb_queue_entry *entry)
1053 struct ntb_payload_header *hdr;
1056 offset = qp->tx_offset;
1057 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1059 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%lld - offset %p, tx %p, entry len %d flags %x buff %p\n",
1060 qp->tx_pkts, offset, qp->tx_offset, entry->len, entry->flags,
1067 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
1069 qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1071 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1076 ntb_tx_copy_task(qp, entry, offset);
1078 qp->tx_offset += qp->tx_max_frame;
1079 if (qp->tx_offset + qp->tx_max_frame >= qp->tx_mw_end)
1080 qp->tx_offset = qp->tx_mw_begin;
1087 static void ntb_send_link_down(struct ntb_transport_qp *qp)
1089 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1090 struct ntb_queue_entry *entry;
1093 if (qp->qp_link == NTB_LINK_DOWN)
1096 qp->qp_link = NTB_LINK_DOWN;
1097 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
1099 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1100 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock,
1110 entry->cb_data = NULL;
1113 entry->flags = LINK_DOWN_FLAG;
1115 rc = ntb_process_tx(qp, entry);
1117 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1122 * ntb_transport_create_queue - Create a new NTB transport layer queue
1123 * @rx_handler: receive callback function
1124 * @tx_handler: transmit callback function
1125 * @event_handler: event callback function
1127 * Create a new NTB transport layer queue and provide the queue with a callback
1128 * routine for both transmit and receive. The receive callback routine will be
1129 * used to pass up data when the transport has received it on the queue. The
1130 * transmit callback routine will be called when the transport has completed the
1131 * transmission of the data on the queue and the data is ready to be freed.
1133 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1135 struct ntb_transport_qp *
1136 ntb_transport_create_queue(void *data, struct pci_dev *pdev,
1137 const struct ntb_queue_handlers *handlers)
1139 struct ntb_queue_entry *entry;
1140 struct ntb_transport_qp *qp;
1141 struct ntb_transport *nt;
1142 unsigned int free_queue;
1145 nt = ntb_find_transport(pdev);
1149 free_queue = ffs(nt->qp_bitmap);
1153 /* decrement free_queue to make it zero based */
1156 clear_bit(free_queue, &nt->qp_bitmap);
1158 qp = &nt->qps[free_queue];
1160 qp->rx_handler = handlers->rx_handler;
1161 qp->tx_handler = handlers->tx_handler;
1162 qp->event_handler = handlers->event_handler;
1164 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1165 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1169 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
1173 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1174 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1178 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1182 tasklet_init(&qp->rx_work, ntb_transport_rx, (unsigned long) qp);
1184 rc = ntb_register_db_callback(qp->ndev, free_queue, qp,
1185 ntb_transport_rxc_db);
1189 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1194 tasklet_disable(&qp->rx_work);
1197 ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1201 ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
1203 set_bit(free_queue, &nt->qp_bitmap);
1207 EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1210 * ntb_transport_free_queue - Frees NTB transport queue
1211 * @qp: NTB queue to be freed
1213 * Frees NTB transport queue
1215 void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1217 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1218 struct ntb_queue_entry *entry;
1223 cancel_delayed_work_sync(&qp->link_work);
1225 ntb_unregister_db_callback(qp->ndev, qp->qp_num);
1226 tasklet_disable(&qp->rx_work);
1229 ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
1233 ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q))) {
1234 dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n");
1239 ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1242 set_bit(qp->qp_num, &qp->transport->qp_bitmap);
1244 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1246 EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1249 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1250 * @qp: NTB queue to be freed
1251 * @len: pointer to variable to write enqueued buffers length
1253 * Dequeues unused buffers from receive queue. Should only be used during
1256 * RETURNS: NULL error value on error, or void* for success.
1258 void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1260 struct ntb_queue_entry *entry;
1263 if (!qp || qp->client_ready == NTB_LINK_UP)
1266 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1270 buf = entry->cb_data;
1273 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
1278 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1281 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1282 * @qp: NTB transport layer queue the entry is to be enqueued on
1283 * @cb: per buffer pointer for callback function to use
1284 * @data: pointer to data buffer that incoming packets will be copied into
1285 * @len: length of the data buffer
1287 * Enqueue a new receive buffer onto the transport queue into which a NTB
1288 * payload can be received into.
1290 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1292 int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1295 struct ntb_queue_entry *entry;
1300 entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q);
1304 entry->cb_data = cb;
1308 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
1313 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1316 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1317 * @qp: NTB transport layer queue the entry is to be enqueued on
1318 * @cb: per buffer pointer for callback function to use
1319 * @data: pointer to data buffer that will be sent
1320 * @len: length of the data buffer
1322 * Enqueue a new transmit buffer onto the transport queue from which a NTB
1323 * payload will be transmitted. This assumes that a lock is behing held to
1324 * serialize access to the qp.
1326 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1328 int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1331 struct ntb_queue_entry *entry;
1334 if (!qp || qp->qp_link != NTB_LINK_UP || !len)
1337 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1341 entry->cb_data = cb;
1346 rc = ntb_process_tx(qp, entry);
1348 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1353 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1356 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1357 * @qp: NTB transport layer queue to be enabled
1359 * Notify NTB transport layer of client readiness to use queue
1361 void ntb_transport_link_up(struct ntb_transport_qp *qp)
1366 qp->client_ready = NTB_LINK_UP;
1368 if (qp->transport->transport_link == NTB_LINK_UP)
1369 schedule_delayed_work(&qp->link_work, 0);
1371 EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1374 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1375 * @qp: NTB transport layer queue to be disabled
1377 * Notify NTB transport layer of client's desire to no longer receive data on
1378 * transport queue specified. It is the client's responsibility to ensure all
1379 * entries on queue are purged or otherwise handled appropraitely.
1381 void ntb_transport_link_down(struct ntb_transport_qp *qp)
1383 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1389 qp->client_ready = NTB_LINK_DOWN;
1391 rc = ntb_read_local_spad(qp->ndev, QP_LINKS, &val);
1393 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
1397 rc = ntb_write_remote_spad(qp->ndev, QP_LINKS,
1398 val & ~(1 << qp->qp_num));
1400 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
1401 val & ~(1 << qp->qp_num), QP_LINKS);
1403 if (qp->qp_link == NTB_LINK_UP)
1404 ntb_send_link_down(qp);
1406 cancel_delayed_work_sync(&qp->link_work);
1408 EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1411 * ntb_transport_link_query - Query transport link state
1412 * @qp: NTB transport layer queue to be queried
1414 * Query connectivity to the remote system of the NTB transport queue
1416 * RETURNS: true for link up or false for link down
1418 bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1420 return qp->qp_link == NTB_LINK_UP;
1422 EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1425 * ntb_transport_qp_num - Query the qp number
1426 * @qp: NTB transport layer queue to be queried
1428 * Query qp number of the NTB transport queue
1430 * RETURNS: a zero based number specifying the qp number
1432 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1436 EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1439 * ntb_transport_max_size - Query the max payload size of a qp
1440 * @qp: NTB transport layer queue to be queried
1442 * Query the maximum payload size permissible on the given qp
1444 * RETURNS: the max payload size of a qp
1446 unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
1448 return qp->tx_max_frame - sizeof(struct ntb_payload_header);
1450 EXPORT_SYMBOL_GPL(ntb_transport_max_size);