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:
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 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;
98 void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data,
100 struct tasklet_struct rx_work;
101 struct list_head rx_pend_q;
102 struct list_head rx_free_q;
103 spinlock_t ntb_rx_pend_q_lock;
104 spinlock_t ntb_rx_free_q_lock;
109 void (*event_handler) (void *data, int status);
110 struct delayed_work link_work;
112 struct dentry *debugfs_dir;
113 struct dentry *debugfs_stats;
127 struct ntb_transport_mw {
133 struct ntb_transport_client_dev {
134 struct list_head entry;
138 struct ntb_transport {
139 struct list_head entry;
140 struct list_head client_devs;
142 struct ntb_device *ndev;
143 struct ntb_transport_mw mw[NTB_NUM_MW];
144 struct ntb_transport_qp *qps;
145 unsigned int max_qps;
146 unsigned long qp_bitmap;
148 struct delayed_work link_work;
149 struct dentry *debugfs_dir;
153 DESC_DONE_FLAG = 1 << 0,
154 LINK_DOWN_FLAG = 1 << 1,
157 struct ntb_payload_header {
172 #define QP_TO_MW(qp) ((qp) % NTB_NUM_MW)
173 #define NTB_QP_DEF_NUM_ENTRIES 100
174 #define NTB_LINK_DOWN_TIMEOUT 10
176 static int ntb_match_bus(struct device *dev, struct device_driver *drv)
178 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
181 static int ntb_client_probe(struct device *dev)
183 const struct ntb_client *drv = container_of(dev->driver,
184 struct ntb_client, driver);
185 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
189 if (drv && drv->probe)
190 rc = drv->probe(pdev);
197 static int ntb_client_remove(struct device *dev)
199 const struct ntb_client *drv = container_of(dev->driver,
200 struct ntb_client, driver);
201 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
203 if (drv && drv->remove)
211 struct bus_type ntb_bus_type = {
213 .match = ntb_match_bus,
214 .probe = ntb_client_probe,
215 .remove = ntb_client_remove,
218 static LIST_HEAD(ntb_transport_list);
220 static int ntb_bus_init(struct ntb_transport *nt)
222 if (list_empty(&ntb_transport_list)) {
223 int rc = bus_register(&ntb_bus_type);
228 list_add(&nt->entry, &ntb_transport_list);
233 static void ntb_bus_remove(struct ntb_transport *nt)
235 struct ntb_transport_client_dev *client_dev, *cd;
237 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
238 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
239 dev_name(&client_dev->dev));
240 list_del(&client_dev->entry);
241 device_unregister(&client_dev->dev);
244 list_del(&nt->entry);
246 if (list_empty(&ntb_transport_list))
247 bus_unregister(&ntb_bus_type);
250 static void ntb_client_release(struct device *dev)
252 struct ntb_transport_client_dev *client_dev;
253 client_dev = container_of(dev, struct ntb_transport_client_dev, dev);
259 * ntb_unregister_client_dev - Unregister NTB client device
260 * @device_name: Name of NTB client device
262 * Unregister an NTB client device with the NTB transport layer
264 void ntb_unregister_client_dev(char *device_name)
266 struct ntb_transport_client_dev *client, *cd;
267 struct ntb_transport *nt;
269 list_for_each_entry(nt, &ntb_transport_list, entry)
270 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
271 if (!strncmp(dev_name(&client->dev), device_name,
272 strlen(device_name))) {
273 list_del(&client->entry);
274 device_unregister(&client->dev);
277 EXPORT_SYMBOL_GPL(ntb_unregister_client_dev);
280 * ntb_register_client_dev - Register NTB client device
281 * @device_name: Name of NTB client device
283 * Register an NTB client device with the NTB transport layer
285 int ntb_register_client_dev(char *device_name)
287 struct ntb_transport_client_dev *client_dev;
288 struct ntb_transport *nt;
291 if (list_empty(&ntb_transport_list))
294 list_for_each_entry(nt, &ntb_transport_list, entry) {
297 client_dev = kzalloc(sizeof(struct ntb_transport_client_dev),
304 dev = &client_dev->dev;
306 /* setup and register client devices */
307 dev_set_name(dev, "%s", device_name);
308 dev->bus = &ntb_bus_type;
309 dev->release = ntb_client_release;
310 dev->parent = &ntb_query_pdev(nt->ndev)->dev;
312 rc = device_register(dev);
318 list_add_tail(&client_dev->entry, &nt->client_devs);
324 ntb_unregister_client_dev(device_name);
328 EXPORT_SYMBOL_GPL(ntb_register_client_dev);
331 * ntb_register_client - Register NTB client driver
332 * @drv: NTB client driver to be registered
334 * Register an NTB client driver with the NTB transport layer
336 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
338 int ntb_register_client(struct ntb_client *drv)
340 drv->driver.bus = &ntb_bus_type;
342 if (list_empty(&ntb_transport_list))
345 return driver_register(&drv->driver);
347 EXPORT_SYMBOL_GPL(ntb_register_client);
350 * ntb_unregister_client - Unregister NTB client driver
351 * @drv: NTB client driver to be unregistered
353 * Unregister an NTB client driver with the NTB transport layer
355 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
357 void ntb_unregister_client(struct ntb_client *drv)
359 driver_unregister(&drv->driver);
361 EXPORT_SYMBOL_GPL(ntb_unregister_client);
363 static int debugfs_open(struct inode *inode, struct file *filp)
365 filp->private_data = inode->i_private;
369 static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
372 struct ntb_transport_qp *qp;
374 ssize_t ret, out_offset, out_count;
378 qp = filp->private_data;
380 out_offset += snprintf(buf + out_offset, out_count - out_offset,
382 out_offset += snprintf(buf + out_offset, out_count - out_offset,
383 "rx_bytes - \t%llu\n", qp->rx_bytes);
384 out_offset += snprintf(buf + out_offset, out_count - out_offset,
385 "rx_pkts - \t%llu\n", qp->rx_pkts);
386 out_offset += snprintf(buf + out_offset, out_count - out_offset,
387 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
388 out_offset += snprintf(buf + out_offset, out_count - out_offset,
389 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
390 out_offset += snprintf(buf + out_offset, out_count - out_offset,
391 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
392 out_offset += snprintf(buf + out_offset, out_count - out_offset,
393 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
394 out_offset += snprintf(buf + out_offset, out_count - out_offset,
395 "rx_buff_begin - %p\n", qp->rx_buff_begin);
396 out_offset += snprintf(buf + out_offset, out_count - out_offset,
397 "rx_offset - \t%p\n", qp->rx_offset);
398 out_offset += snprintf(buf + out_offset, out_count - out_offset,
399 "rx_buff_end - \t%p\n", qp->rx_buff_end);
401 out_offset += snprintf(buf + out_offset, out_count - out_offset,
402 "tx_bytes - \t%llu\n", qp->tx_bytes);
403 out_offset += snprintf(buf + out_offset, out_count - out_offset,
404 "tx_pkts - \t%llu\n", qp->tx_pkts);
405 out_offset += snprintf(buf + out_offset, out_count - out_offset,
406 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
407 out_offset += snprintf(buf + out_offset, out_count - out_offset,
408 "tx_mw_begin - \t%p\n", qp->tx_mw_begin);
409 out_offset += snprintf(buf + out_offset, out_count - out_offset,
410 "tx_offset - \t%p\n", qp->tx_offset);
411 out_offset += snprintf(buf + out_offset, out_count - out_offset,
412 "tx_mw_end - \t%p\n", qp->tx_mw_end);
414 out_offset += snprintf(buf + out_offset, out_count - out_offset,
415 "QP Link %s\n", (qp->qp_link == NTB_LINK_UP) ?
418 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
422 static const struct file_operations ntb_qp_debugfs_stats = {
423 .owner = THIS_MODULE,
424 .open = debugfs_open,
425 .read = debugfs_read,
428 static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
429 struct list_head *list)
433 spin_lock_irqsave(lock, flags);
434 list_add_tail(entry, list);
435 spin_unlock_irqrestore(lock, flags);
438 static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
439 struct list_head *list)
441 struct ntb_queue_entry *entry;
444 spin_lock_irqsave(lock, flags);
445 if (list_empty(list)) {
449 entry = list_first_entry(list, struct ntb_queue_entry, entry);
450 list_del(&entry->entry);
452 spin_unlock_irqrestore(lock, flags);
457 static void ntb_transport_setup_qp_mw(struct ntb_transport *nt,
460 struct ntb_transport_qp *qp = &nt->qps[qp_num];
461 unsigned int size, num_qps_mw;
462 u8 mw_num = QP_TO_MW(qp_num);
464 WARN_ON(nt->mw[mw_num].virt_addr == 0);
466 if (nt->max_qps % NTB_NUM_MW && !mw_num)
467 num_qps_mw = nt->max_qps / NTB_NUM_MW +
468 (nt->max_qps % NTB_NUM_MW - mw_num);
470 num_qps_mw = nt->max_qps / NTB_NUM_MW;
472 size = nt->mw[mw_num].size / num_qps_mw;
474 qp->rx_buff_begin = nt->mw[mw_num].virt_addr +
475 (qp_num / NTB_NUM_MW * size);
476 qp->rx_buff_end = qp->rx_buff_begin + size;
477 qp->rx_offset = qp->rx_buff_begin;
479 qp->tx_mw_begin = ntb_get_mw_vbase(nt->ndev, mw_num) +
480 (qp_num / NTB_NUM_MW * size);
481 qp->tx_mw_end = qp->tx_mw_begin + size;
482 qp->tx_offset = qp->tx_mw_begin;
488 static int ntb_set_mw(struct ntb_transport *nt, int num_mw, unsigned int size)
490 struct ntb_transport_mw *mw = &nt->mw[num_mw];
491 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
494 /* Alloc memory for receiving data. Must be 4k aligned */
495 mw->size = ALIGN(size, 4096);
497 mw->virt_addr = dma_alloc_coherent(&pdev->dev, mw->size, &mw->dma_addr,
499 if (!mw->virt_addr) {
500 dev_err(&pdev->dev, "Unable to allocate MW buffer of size %d\n",
505 /* setup the hdr offsets with 0's */
506 for (offset = mw->virt_addr + transport_mtu -
507 sizeof(struct ntb_payload_header);
508 offset < mw->virt_addr + size; offset += transport_mtu)
509 memset(offset, 0, sizeof(struct ntb_payload_header));
511 /* Notify HW the memory location of the receive buffer */
512 ntb_set_mw_addr(nt->ndev, num_mw, mw->dma_addr);
517 static void ntb_qp_link_down(struct ntb_transport_qp *qp)
519 struct ntb_transport *nt = qp->transport;
520 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
522 if (qp->qp_link == NTB_LINK_DOWN) {
523 cancel_delayed_work_sync(&qp->link_work);
527 if (qp->event_handler)
528 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
530 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
531 qp->qp_link = NTB_LINK_DOWN;
533 if (nt->transport_link == NTB_LINK_UP)
534 schedule_delayed_work(&qp->link_work,
535 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
538 static void ntb_transport_conn_down(struct ntb_transport *nt)
542 if (nt->transport_link == NTB_LINK_DOWN)
543 cancel_delayed_work_sync(&nt->link_work);
545 nt->transport_link = NTB_LINK_DOWN;
547 /* Pass along the info to any clients */
548 for (i = 0; i < nt->max_qps; i++)
549 if (!test_bit(i, &nt->qp_bitmap))
550 ntb_qp_link_down(&nt->qps[i]);
552 /* The scratchpad registers keep the values if the remote side
553 * goes down, blast them now to give them a sane value the next
554 * time they are accessed
556 for (i = 0; i < MAX_SPAD; i++)
557 ntb_write_local_spad(nt->ndev, i, 0);
560 static void ntb_transport_event_callback(void *data, enum ntb_hw_event event)
562 struct ntb_transport *nt = data;
565 case NTB_EVENT_HW_LINK_UP:
566 schedule_delayed_work(&nt->link_work, 0);
568 case NTB_EVENT_HW_LINK_DOWN:
569 ntb_transport_conn_down(nt);
576 static void ntb_transport_link_work(struct work_struct *work)
578 struct ntb_transport *nt = container_of(work, struct ntb_transport,
580 struct ntb_device *ndev = nt->ndev;
581 struct pci_dev *pdev = ntb_query_pdev(ndev);
585 /* send the local info */
586 rc = ntb_write_remote_spad(ndev, VERSION, NTB_TRANSPORT_VERSION);
588 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
593 rc = ntb_write_remote_spad(ndev, MW0_SZ, ntb_get_mw_size(ndev, 0));
595 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
596 (u32) ntb_get_mw_size(ndev, 0), MW0_SZ);
600 rc = ntb_write_remote_spad(ndev, MW1_SZ, ntb_get_mw_size(ndev, 1));
602 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
603 (u32) ntb_get_mw_size(ndev, 1), MW1_SZ);
607 rc = ntb_write_remote_spad(ndev, NUM_QPS, nt->max_qps);
609 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
610 nt->max_qps, NUM_QPS);
614 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
616 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
620 rc = ntb_write_remote_spad(ndev, QP_LINKS, val);
622 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
627 /* Query the remote side for its info */
628 rc = ntb_read_remote_spad(ndev, VERSION, &val);
630 dev_err(&pdev->dev, "Error reading remote spad %d\n", VERSION);
634 if (val != NTB_TRANSPORT_VERSION)
636 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
638 rc = ntb_read_remote_spad(ndev, NUM_QPS, &val);
640 dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_QPS);
644 if (val != nt->max_qps)
646 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
648 rc = ntb_read_remote_spad(ndev, MW0_SZ, &val);
650 dev_err(&pdev->dev, "Error reading remote spad %d\n", MW0_SZ);
656 dev_dbg(&pdev->dev, "Remote MW0 size = %d\n", val);
658 rc = ntb_set_mw(nt, 0, val);
662 rc = ntb_read_remote_spad(ndev, MW1_SZ, &val);
664 dev_err(&pdev->dev, "Error reading remote spad %d\n", MW1_SZ);
670 dev_dbg(&pdev->dev, "Remote MW1 size = %d\n", val);
672 rc = ntb_set_mw(nt, 1, val);
676 nt->transport_link = NTB_LINK_UP;
678 for (i = 0; i < nt->max_qps; i++) {
679 struct ntb_transport_qp *qp = &nt->qps[i];
681 ntb_transport_setup_qp_mw(nt, i);
683 if (qp->client_ready == NTB_LINK_UP)
684 schedule_delayed_work(&qp->link_work, 0);
690 if (ntb_hw_link_status(ndev))
691 schedule_delayed_work(&nt->link_work,
692 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
695 static void ntb_qp_link_work(struct work_struct *work)
697 struct ntb_transport_qp *qp = container_of(work,
698 struct ntb_transport_qp,
700 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
701 struct ntb_transport *nt = qp->transport;
704 WARN_ON(nt->transport_link != NTB_LINK_UP);
706 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
708 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
712 rc = ntb_write_remote_spad(nt->ndev, QP_LINKS, val | 1 << qp->qp_num);
714 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
715 val | 1 << qp->qp_num, QP_LINKS);
717 /* query remote spad for qp ready bits */
718 rc = ntb_read_remote_spad(nt->ndev, QP_LINKS, &val);
720 dev_err(&pdev->dev, "Error reading remote spad %d\n", QP_LINKS);
722 dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val);
724 /* See if the remote side is up */
725 if (1 << qp->qp_num & val) {
726 qp->qp_link = NTB_LINK_UP;
728 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
729 if (qp->event_handler)
730 qp->event_handler(qp->cb_data, NTB_LINK_UP);
731 } else if (nt->transport_link == NTB_LINK_UP)
732 schedule_delayed_work(&qp->link_work,
733 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
736 static void ntb_transport_init_queue(struct ntb_transport *nt,
739 struct ntb_transport_qp *qp;
741 qp = &nt->qps[qp_num];
745 qp->qp_link = NTB_LINK_DOWN;
746 qp->client_ready = NTB_LINK_DOWN;
747 qp->event_handler = NULL;
749 if (nt->debugfs_dir) {
750 char debugfs_name[4];
752 snprintf(debugfs_name, 4, "qp%d", qp_num);
753 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
756 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
758 &ntb_qp_debugfs_stats);
761 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
763 spin_lock_init(&qp->ntb_rx_pend_q_lock);
764 spin_lock_init(&qp->ntb_rx_free_q_lock);
765 spin_lock_init(&qp->ntb_tx_free_q_lock);
767 INIT_LIST_HEAD(&qp->rx_pend_q);
768 INIT_LIST_HEAD(&qp->rx_free_q);
769 INIT_LIST_HEAD(&qp->tx_free_q);
772 int ntb_transport_init(struct pci_dev *pdev)
774 struct ntb_transport *nt;
777 nt = kzalloc(sizeof(struct ntb_transport), GFP_KERNEL);
781 if (debugfs_initialized())
782 nt->debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
784 nt->debugfs_dir = NULL;
786 nt->ndev = ntb_register_transport(pdev, nt);
792 nt->max_qps = min(nt->ndev->max_cbs, max_num_clients);
794 nt->qps = kcalloc(nt->max_qps, sizeof(struct ntb_transport_qp),
801 nt->qp_bitmap = ((u64) 1 << nt->max_qps) - 1;
803 for (i = 0; i < nt->max_qps; i++)
804 ntb_transport_init_queue(nt, i);
806 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
808 rc = ntb_register_event_callback(nt->ndev,
809 ntb_transport_event_callback);
813 INIT_LIST_HEAD(&nt->client_devs);
814 rc = ntb_bus_init(nt);
818 if (ntb_hw_link_status(nt->ndev))
819 schedule_delayed_work(&nt->link_work, 0);
824 ntb_unregister_event_callback(nt->ndev);
828 ntb_unregister_transport(nt->ndev);
830 debugfs_remove_recursive(nt->debugfs_dir);
835 void ntb_transport_free(void *transport)
837 struct ntb_transport *nt = transport;
838 struct pci_dev *pdev;
841 nt->transport_link = NTB_LINK_DOWN;
843 /* verify that all the qp's are freed */
844 for (i = 0; i < nt->max_qps; i++)
845 if (!test_bit(i, &nt->qp_bitmap))
846 ntb_transport_free_queue(&nt->qps[i]);
850 cancel_delayed_work_sync(&nt->link_work);
852 debugfs_remove_recursive(nt->debugfs_dir);
854 ntb_unregister_event_callback(nt->ndev);
856 pdev = ntb_query_pdev(nt->ndev);
858 for (i = 0; i < NTB_NUM_MW; i++)
859 if (nt->mw[i].virt_addr)
860 dma_free_coherent(&pdev->dev, nt->mw[i].size,
865 ntb_unregister_transport(nt->ndev);
869 static void ntb_rx_copy_task(struct ntb_transport_qp *qp,
870 struct ntb_queue_entry *entry, void *offset)
873 struct ntb_payload_header *hdr;
875 BUG_ON(offset < qp->rx_buff_begin ||
876 offset + transport_mtu >= qp->rx_buff_end);
878 hdr = offset + transport_mtu - sizeof(struct ntb_payload_header);
879 entry->len = hdr->len;
881 memcpy(entry->buf, offset, entry->len);
883 /* Ensure that the data is fully copied out before clearing the flag */
887 if (qp->rx_handler && qp->client_ready == NTB_LINK_UP)
888 qp->rx_handler(qp, qp->cb_data, entry->cb_data, entry->len);
890 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
893 static int ntb_process_rxc(struct ntb_transport_qp *qp)
895 struct ntb_payload_header *hdr;
896 struct ntb_queue_entry *entry;
899 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
901 hdr = offset + transport_mtu -
902 sizeof(struct ntb_payload_header);
903 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
904 "no buffer - HDR ver %llu, len %d, flags %x\n",
905 hdr->ver, hdr->len, hdr->flags);
910 offset = qp->rx_offset;
911 hdr = offset + transport_mtu - sizeof(struct ntb_payload_header);
913 if (!(hdr->flags & DESC_DONE_FLAG)) {
914 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
920 if (hdr->ver != qp->rx_pkts) {
921 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
922 "qp %d: version mismatch, expected %llu - got %llu\n",
923 qp->qp_num, qp->rx_pkts, hdr->ver);
924 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
930 if (hdr->flags & LINK_DOWN_FLAG) {
931 ntb_qp_link_down(qp);
933 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
936 /* Ensure that the data is fully copied out before clearing the
944 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
945 "rx offset %p, ver %llu - %d payload received, buf size %d\n",
946 qp->rx_offset, hdr->ver, hdr->len, entry->len);
948 if (hdr->len <= entry->len)
949 ntb_rx_copy_task(qp, entry, offset);
951 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
954 /* Ensure that the data is fully copied out before clearing the
960 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
961 "RX overflow! Wanted %d got %d\n",
962 hdr->len, entry->len);
965 qp->rx_bytes += hdr->len;
969 qp->rx_offset += transport_mtu;
970 if (qp->rx_offset + transport_mtu >= qp->rx_buff_end)
971 qp->rx_offset = qp->rx_buff_begin;
976 static void ntb_transport_rx(unsigned long data)
978 struct ntb_transport_qp *qp = (struct ntb_transport_qp *)data;
982 rc = ntb_process_rxc(qp);
986 static void ntb_transport_rxc_db(void *data, int db_num)
988 struct ntb_transport_qp *qp = data;
990 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%s: doorbell %d received\n",
993 tasklet_schedule(&qp->rx_work);
996 static void ntb_tx_copy_task(struct ntb_transport_qp *qp,
997 struct ntb_queue_entry *entry,
1000 struct ntb_payload_header *hdr;
1002 BUG_ON(offset < qp->tx_mw_begin ||
1003 offset + transport_mtu >= qp->tx_mw_end);
1005 memcpy_toio(offset, entry->buf, entry->len);
1007 hdr = offset + transport_mtu - sizeof(struct ntb_payload_header);
1008 hdr->len = entry->len;
1009 hdr->ver = qp->tx_pkts;
1011 /* Ensure that the data is fully copied out before setting the flag */
1013 hdr->flags = entry->flags | DESC_DONE_FLAG;
1015 ntb_ring_sdb(qp->ndev, qp->qp_num);
1017 /* The entry length can only be zero if the packet is intended to be a
1018 * "link down" or similar. Since no payload is being sent in these
1019 * cases, there is nothing to add to the completion queue.
1021 if (entry->len > 0) {
1022 qp->tx_bytes += entry->len;
1025 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1029 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1032 static int ntb_process_tx(struct ntb_transport_qp *qp,
1033 struct ntb_queue_entry *entry)
1035 struct ntb_payload_header *hdr;
1038 offset = qp->tx_offset;
1039 hdr = offset + transport_mtu - sizeof(struct ntb_payload_header);
1041 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%lld - offset %p, tx %p, entry len %d flags %x buff %p\n",
1042 qp->tx_pkts, offset, qp->tx_offset, entry->len, entry->flags,
1049 if (entry->len > transport_mtu - sizeof(struct ntb_payload_header)) {
1051 qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1053 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1058 ntb_tx_copy_task(qp, entry, offset);
1060 qp->tx_offset += transport_mtu;
1061 if (qp->tx_offset + transport_mtu >= qp->tx_mw_end)
1062 qp->tx_offset = qp->tx_mw_begin;
1069 static void ntb_send_link_down(struct ntb_transport_qp *qp)
1071 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1072 struct ntb_queue_entry *entry;
1075 if (qp->qp_link == NTB_LINK_DOWN)
1078 qp->qp_link = NTB_LINK_DOWN;
1079 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
1081 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1082 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock,
1092 entry->cb_data = NULL;
1095 entry->flags = LINK_DOWN_FLAG;
1097 rc = ntb_process_tx(qp, entry);
1099 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1104 * ntb_transport_create_queue - Create a new NTB transport layer queue
1105 * @rx_handler: receive callback function
1106 * @tx_handler: transmit callback function
1107 * @event_handler: event callback function
1109 * Create a new NTB transport layer queue and provide the queue with a callback
1110 * routine for both transmit and receive. The receive callback routine will be
1111 * used to pass up data when the transport has received it on the queue. The
1112 * transmit callback routine will be called when the transport has completed the
1113 * transmission of the data on the queue and the data is ready to be freed.
1115 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1117 struct ntb_transport_qp *
1118 ntb_transport_create_queue(void *data, struct pci_dev *pdev,
1119 const struct ntb_queue_handlers *handlers)
1121 struct ntb_queue_entry *entry;
1122 struct ntb_transport_qp *qp;
1123 struct ntb_transport *nt;
1124 unsigned int free_queue;
1127 nt = ntb_find_transport(pdev);
1131 free_queue = ffs(nt->qp_bitmap);
1135 /* decrement free_queue to make it zero based */
1138 clear_bit(free_queue, &nt->qp_bitmap);
1140 qp = &nt->qps[free_queue];
1142 qp->rx_handler = handlers->rx_handler;
1143 qp->tx_handler = handlers->tx_handler;
1144 qp->event_handler = handlers->event_handler;
1146 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1147 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1151 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
1155 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1156 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1160 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1164 tasklet_init(&qp->rx_work, ntb_transport_rx, (unsigned long) qp);
1166 rc = ntb_register_db_callback(qp->ndev, free_queue, qp,
1167 ntb_transport_rxc_db);
1171 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1176 tasklet_disable(&qp->rx_work);
1179 ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1183 ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
1185 set_bit(free_queue, &nt->qp_bitmap);
1189 EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1192 * ntb_transport_free_queue - Frees NTB transport queue
1193 * @qp: NTB queue to be freed
1195 * Frees NTB transport queue
1197 void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1199 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1200 struct ntb_queue_entry *entry;
1205 cancel_delayed_work_sync(&qp->link_work);
1207 ntb_unregister_db_callback(qp->ndev, qp->qp_num);
1208 tasklet_disable(&qp->rx_work);
1211 ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
1215 ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q))) {
1216 dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n");
1221 ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1224 set_bit(qp->qp_num, &qp->transport->qp_bitmap);
1226 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1228 EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1231 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1232 * @qp: NTB queue to be freed
1233 * @len: pointer to variable to write enqueued buffers length
1235 * Dequeues unused buffers from receive queue. Should only be used during
1238 * RETURNS: NULL error value on error, or void* for success.
1240 void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1242 struct ntb_queue_entry *entry;
1245 if (!qp || qp->client_ready == NTB_LINK_UP)
1248 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1252 buf = entry->cb_data;
1255 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
1260 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1263 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1264 * @qp: NTB transport layer queue the entry is to be enqueued on
1265 * @cb: per buffer pointer for callback function to use
1266 * @data: pointer to data buffer that incoming packets will be copied into
1267 * @len: length of the data buffer
1269 * Enqueue a new receive buffer onto the transport queue into which a NTB
1270 * payload can be received into.
1272 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1274 int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1277 struct ntb_queue_entry *entry;
1282 entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q);
1286 entry->cb_data = cb;
1290 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
1295 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1298 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1299 * @qp: NTB transport layer queue the entry is to be enqueued on
1300 * @cb: per buffer pointer for callback function to use
1301 * @data: pointer to data buffer that will be sent
1302 * @len: length of the data buffer
1304 * Enqueue a new transmit buffer onto the transport queue from which a NTB
1305 * payload will be transmitted. This assumes that a lock is behing held to
1306 * serialize access to the qp.
1308 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1310 int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1313 struct ntb_queue_entry *entry;
1316 if (!qp || qp->qp_link != NTB_LINK_UP || !len)
1319 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1323 entry->cb_data = cb;
1328 rc = ntb_process_tx(qp, entry);
1330 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1335 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1338 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1339 * @qp: NTB transport layer queue to be enabled
1341 * Notify NTB transport layer of client readiness to use queue
1343 void ntb_transport_link_up(struct ntb_transport_qp *qp)
1348 qp->client_ready = NTB_LINK_UP;
1350 if (qp->transport->transport_link == NTB_LINK_UP)
1351 schedule_delayed_work(&qp->link_work, 0);
1353 EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1356 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1357 * @qp: NTB transport layer queue to be disabled
1359 * Notify NTB transport layer of client's desire to no longer receive data on
1360 * transport queue specified. It is the client's responsibility to ensure all
1361 * entries on queue are purged or otherwise handled appropraitely.
1363 void ntb_transport_link_down(struct ntb_transport_qp *qp)
1365 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1371 qp->client_ready = NTB_LINK_DOWN;
1373 rc = ntb_read_local_spad(qp->ndev, QP_LINKS, &val);
1375 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
1379 rc = ntb_write_remote_spad(qp->ndev, QP_LINKS,
1380 val & ~(1 << qp->qp_num));
1382 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
1383 val & ~(1 << qp->qp_num), QP_LINKS);
1385 if (qp->qp_link == NTB_LINK_UP)
1386 ntb_send_link_down(qp);
1388 cancel_delayed_work_sync(&qp->link_work);
1390 EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1393 * ntb_transport_link_query - Query transport link state
1394 * @qp: NTB transport layer queue to be queried
1396 * Query connectivity to the remote system of the NTB transport queue
1398 * RETURNS: true for link up or false for link down
1400 bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1402 return qp->qp_link == NTB_LINK_UP;
1404 EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1407 * ntb_transport_qp_num - Query the qp number
1408 * @qp: NTB transport layer queue to be queried
1410 * Query qp number of the NTB transport queue
1412 * RETURNS: a zero based number specifying the qp number
1414 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1418 EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1421 * ntb_transport_max_size - Query the max payload size of a qp
1422 * @qp: NTB transport layer queue to be queried
1424 * Query the maximum payload size permissible on the given qp
1426 * RETURNS: the max payload size of a qp
1429 ntb_transport_max_size(__attribute__((unused)) struct ntb_transport_qp *qp)
1431 return transport_mtu - sizeof(struct ntb_payload_header);
1433 EXPORT_SYMBOL_GPL(ntb_transport_max_size);