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;
114 struct dentry *debugfs_dir;
115 struct dentry *debugfs_stats;
129 struct ntb_transport_mw {
135 struct ntb_transport_client_dev {
136 struct list_head entry;
140 struct ntb_transport {
141 struct list_head entry;
142 struct list_head client_devs;
144 struct ntb_device *ndev;
145 struct ntb_transport_mw mw[NTB_NUM_MW];
146 struct ntb_transport_qp *qps;
147 unsigned int max_qps;
148 unsigned long qp_bitmap;
150 struct delayed_work link_work;
151 struct dentry *debugfs_dir;
155 DESC_DONE_FLAG = 1 << 0,
156 LINK_DOWN_FLAG = 1 << 1,
159 struct ntb_payload_header {
174 #define QP_TO_MW(qp) ((qp) % NTB_NUM_MW)
175 #define NTB_QP_DEF_NUM_ENTRIES 100
176 #define NTB_LINK_DOWN_TIMEOUT 10
178 static int ntb_match_bus(struct device *dev, struct device_driver *drv)
180 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
183 static int ntb_client_probe(struct device *dev)
185 const struct ntb_client *drv = container_of(dev->driver,
186 struct ntb_client, driver);
187 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
191 if (drv && drv->probe)
192 rc = drv->probe(pdev);
199 static int ntb_client_remove(struct device *dev)
201 const struct ntb_client *drv = container_of(dev->driver,
202 struct ntb_client, driver);
203 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
205 if (drv && drv->remove)
213 struct bus_type ntb_bus_type = {
215 .match = ntb_match_bus,
216 .probe = ntb_client_probe,
217 .remove = ntb_client_remove,
220 static LIST_HEAD(ntb_transport_list);
222 static int ntb_bus_init(struct ntb_transport *nt)
224 if (list_empty(&ntb_transport_list)) {
225 int rc = bus_register(&ntb_bus_type);
230 list_add(&nt->entry, &ntb_transport_list);
235 static void ntb_bus_remove(struct ntb_transport *nt)
237 struct ntb_transport_client_dev *client_dev, *cd;
239 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
240 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
241 dev_name(&client_dev->dev));
242 list_del(&client_dev->entry);
243 device_unregister(&client_dev->dev);
246 list_del(&nt->entry);
248 if (list_empty(&ntb_transport_list))
249 bus_unregister(&ntb_bus_type);
252 static void ntb_client_release(struct device *dev)
254 struct ntb_transport_client_dev *client_dev;
255 client_dev = container_of(dev, struct ntb_transport_client_dev, dev);
261 * ntb_unregister_client_dev - Unregister NTB client device
262 * @device_name: Name of NTB client device
264 * Unregister an NTB client device with the NTB transport layer
266 void ntb_unregister_client_dev(char *device_name)
268 struct ntb_transport_client_dev *client, *cd;
269 struct ntb_transport *nt;
271 list_for_each_entry(nt, &ntb_transport_list, entry)
272 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
273 if (!strncmp(dev_name(&client->dev), device_name,
274 strlen(device_name))) {
275 list_del(&client->entry);
276 device_unregister(&client->dev);
279 EXPORT_SYMBOL_GPL(ntb_unregister_client_dev);
282 * ntb_register_client_dev - Register NTB client device
283 * @device_name: Name of NTB client device
285 * Register an NTB client device with the NTB transport layer
287 int ntb_register_client_dev(char *device_name)
289 struct ntb_transport_client_dev *client_dev;
290 struct ntb_transport *nt;
293 if (list_empty(&ntb_transport_list))
296 list_for_each_entry(nt, &ntb_transport_list, entry) {
299 client_dev = kzalloc(sizeof(struct ntb_transport_client_dev),
306 dev = &client_dev->dev;
308 /* setup and register client devices */
309 dev_set_name(dev, "%s", device_name);
310 dev->bus = &ntb_bus_type;
311 dev->release = ntb_client_release;
312 dev->parent = &ntb_query_pdev(nt->ndev)->dev;
314 rc = device_register(dev);
320 list_add_tail(&client_dev->entry, &nt->client_devs);
326 ntb_unregister_client_dev(device_name);
330 EXPORT_SYMBOL_GPL(ntb_register_client_dev);
333 * ntb_register_client - Register NTB client driver
334 * @drv: NTB client driver to be registered
336 * Register an NTB client driver with the NTB transport layer
338 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
340 int ntb_register_client(struct ntb_client *drv)
342 drv->driver.bus = &ntb_bus_type;
344 if (list_empty(&ntb_transport_list))
347 return driver_register(&drv->driver);
349 EXPORT_SYMBOL_GPL(ntb_register_client);
352 * ntb_unregister_client - Unregister NTB client driver
353 * @drv: NTB client driver to be unregistered
355 * Unregister an NTB client driver with the NTB transport layer
357 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
359 void ntb_unregister_client(struct ntb_client *drv)
361 driver_unregister(&drv->driver);
363 EXPORT_SYMBOL_GPL(ntb_unregister_client);
365 static int debugfs_open(struct inode *inode, struct file *filp)
367 filp->private_data = inode->i_private;
371 static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
374 struct ntb_transport_qp *qp;
376 ssize_t ret, out_offset, out_count;
380 qp = filp->private_data;
382 out_offset += snprintf(buf + out_offset, out_count - out_offset,
384 out_offset += snprintf(buf + out_offset, out_count - out_offset,
385 "rx_bytes - \t%llu\n", qp->rx_bytes);
386 out_offset += snprintf(buf + out_offset, out_count - out_offset,
387 "rx_pkts - \t%llu\n", qp->rx_pkts);
388 out_offset += snprintf(buf + out_offset, out_count - out_offset,
389 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
390 out_offset += snprintf(buf + out_offset, out_count - out_offset,
391 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
392 out_offset += snprintf(buf + out_offset, out_count - out_offset,
393 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
394 out_offset += snprintf(buf + out_offset, out_count - out_offset,
395 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
396 out_offset += snprintf(buf + out_offset, out_count - out_offset,
397 "rx_buff_begin - %p\n", qp->rx_buff_begin);
398 out_offset += snprintf(buf + out_offset, out_count - out_offset,
399 "rx_offset - \t%p\n", qp->rx_offset);
400 out_offset += snprintf(buf + out_offset, out_count - out_offset,
401 "rx_buff_end - \t%p\n", qp->rx_buff_end);
403 out_offset += snprintf(buf + out_offset, out_count - out_offset,
404 "tx_bytes - \t%llu\n", qp->tx_bytes);
405 out_offset += snprintf(buf + out_offset, out_count - out_offset,
406 "tx_pkts - \t%llu\n", qp->tx_pkts);
407 out_offset += snprintf(buf + out_offset, out_count - out_offset,
408 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
409 out_offset += snprintf(buf + out_offset, out_count - out_offset,
410 "tx_mw_begin - \t%p\n", qp->tx_mw_begin);
411 out_offset += snprintf(buf + out_offset, out_count - out_offset,
412 "tx_offset - \t%p\n", qp->tx_offset);
413 out_offset += snprintf(buf + out_offset, out_count - out_offset,
414 "tx_mw_end - \t%p\n", qp->tx_mw_end);
416 out_offset += snprintf(buf + out_offset, out_count - out_offset,
417 "QP Link %s\n", (qp->qp_link == NTB_LINK_UP) ?
420 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
424 static const struct file_operations ntb_qp_debugfs_stats = {
425 .owner = THIS_MODULE,
426 .open = debugfs_open,
427 .read = debugfs_read,
430 static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
431 struct list_head *list)
435 spin_lock_irqsave(lock, flags);
436 list_add_tail(entry, list);
437 spin_unlock_irqrestore(lock, flags);
440 static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
441 struct list_head *list)
443 struct ntb_queue_entry *entry;
446 spin_lock_irqsave(lock, flags);
447 if (list_empty(list)) {
451 entry = list_first_entry(list, struct ntb_queue_entry, entry);
452 list_del(&entry->entry);
454 spin_unlock_irqrestore(lock, flags);
459 static void ntb_transport_setup_qp_mw(struct ntb_transport *nt,
462 struct ntb_transport_qp *qp = &nt->qps[qp_num];
463 unsigned int rx_size, num_qps_mw;
464 u8 mw_num = QP_TO_MW(qp_num);
467 WARN_ON(nt->mw[mw_num].virt_addr == 0);
469 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
470 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
472 num_qps_mw = nt->max_qps / NTB_NUM_MW;
474 rx_size = nt->mw[mw_num].size / num_qps_mw;
475 qp->rx_buff_begin = nt->mw[mw_num].virt_addr +
476 (qp_num / NTB_NUM_MW * rx_size);
477 qp->rx_buff_end = qp->rx_buff_begin + rx_size;
478 qp->rx_offset = qp->rx_buff_begin;
479 qp->rx_max_frame = min(transport_mtu, rx_size);
481 /* setup the hdr offsets with 0's */
482 for (offset = qp->rx_buff_begin + qp->rx_max_frame -
483 sizeof(struct ntb_payload_header);
484 offset < qp->rx_buff_end; offset += qp->rx_max_frame)
485 memset(offset, 0, sizeof(struct ntb_payload_header));
491 static int ntb_set_mw(struct ntb_transport *nt, int num_mw, unsigned int size)
493 struct ntb_transport_mw *mw = &nt->mw[num_mw];
494 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
496 /* Alloc memory for receiving data. Must be 4k aligned */
497 mw->size = ALIGN(size, 4096);
499 mw->virt_addr = dma_alloc_coherent(&pdev->dev, mw->size, &mw->dma_addr,
501 if (!mw->virt_addr) {
502 dev_err(&pdev->dev, "Unable to allocate MW buffer of size %d\n",
507 /* Notify HW the memory location of the receive buffer */
508 ntb_set_mw_addr(nt->ndev, num_mw, mw->dma_addr);
513 static void ntb_qp_link_down(struct ntb_transport_qp *qp)
515 struct ntb_transport *nt = qp->transport;
516 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
518 if (qp->qp_link == NTB_LINK_DOWN) {
519 cancel_delayed_work_sync(&qp->link_work);
523 if (qp->event_handler)
524 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
526 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
527 qp->qp_link = NTB_LINK_DOWN;
529 if (nt->transport_link == NTB_LINK_UP)
530 schedule_delayed_work(&qp->link_work,
531 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
534 static void ntb_transport_conn_down(struct ntb_transport *nt)
538 if (nt->transport_link == NTB_LINK_DOWN)
539 cancel_delayed_work_sync(&nt->link_work);
541 nt->transport_link = NTB_LINK_DOWN;
543 /* Pass along the info to any clients */
544 for (i = 0; i < nt->max_qps; i++)
545 if (!test_bit(i, &nt->qp_bitmap))
546 ntb_qp_link_down(&nt->qps[i]);
548 /* The scratchpad registers keep the values if the remote side
549 * goes down, blast them now to give them a sane value the next
550 * time they are accessed
552 for (i = 0; i < MAX_SPAD; i++)
553 ntb_write_local_spad(nt->ndev, i, 0);
556 static void ntb_transport_event_callback(void *data, enum ntb_hw_event event)
558 struct ntb_transport *nt = data;
561 case NTB_EVENT_HW_LINK_UP:
562 schedule_delayed_work(&nt->link_work, 0);
564 case NTB_EVENT_HW_LINK_DOWN:
565 ntb_transport_conn_down(nt);
572 static void ntb_transport_link_work(struct work_struct *work)
574 struct ntb_transport *nt = container_of(work, struct ntb_transport,
576 struct ntb_device *ndev = nt->ndev;
577 struct pci_dev *pdev = ntb_query_pdev(ndev);
581 /* send the local info */
582 rc = ntb_write_remote_spad(ndev, VERSION, NTB_TRANSPORT_VERSION);
584 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
589 rc = ntb_write_remote_spad(ndev, MW0_SZ, ntb_get_mw_size(ndev, 0));
591 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
592 (u32) ntb_get_mw_size(ndev, 0), MW0_SZ);
596 rc = ntb_write_remote_spad(ndev, MW1_SZ, ntb_get_mw_size(ndev, 1));
598 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
599 (u32) ntb_get_mw_size(ndev, 1), MW1_SZ);
603 rc = ntb_write_remote_spad(ndev, NUM_QPS, nt->max_qps);
605 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
606 nt->max_qps, NUM_QPS);
610 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
612 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
616 rc = ntb_write_remote_spad(ndev, QP_LINKS, val);
618 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
623 /* Query the remote side for its info */
624 rc = ntb_read_remote_spad(ndev, VERSION, &val);
626 dev_err(&pdev->dev, "Error reading remote spad %d\n", VERSION);
630 if (val != NTB_TRANSPORT_VERSION)
632 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
634 rc = ntb_read_remote_spad(ndev, NUM_QPS, &val);
636 dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_QPS);
640 if (val != nt->max_qps)
642 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
644 rc = ntb_read_remote_spad(ndev, MW0_SZ, &val);
646 dev_err(&pdev->dev, "Error reading remote spad %d\n", MW0_SZ);
652 dev_dbg(&pdev->dev, "Remote MW0 size = %d\n", val);
654 rc = ntb_set_mw(nt, 0, val);
658 rc = ntb_read_remote_spad(ndev, MW1_SZ, &val);
660 dev_err(&pdev->dev, "Error reading remote spad %d\n", MW1_SZ);
666 dev_dbg(&pdev->dev, "Remote MW1 size = %d\n", val);
668 rc = ntb_set_mw(nt, 1, val);
672 nt->transport_link = NTB_LINK_UP;
674 for (i = 0; i < nt->max_qps; i++) {
675 struct ntb_transport_qp *qp = &nt->qps[i];
677 ntb_transport_setup_qp_mw(nt, i);
679 if (qp->client_ready == NTB_LINK_UP)
680 schedule_delayed_work(&qp->link_work, 0);
686 if (ntb_hw_link_status(ndev))
687 schedule_delayed_work(&nt->link_work,
688 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
691 static void ntb_qp_link_work(struct work_struct *work)
693 struct ntb_transport_qp *qp = container_of(work,
694 struct ntb_transport_qp,
696 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
697 struct ntb_transport *nt = qp->transport;
700 WARN_ON(nt->transport_link != NTB_LINK_UP);
702 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
704 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
708 rc = ntb_write_remote_spad(nt->ndev, QP_LINKS, val | 1 << qp->qp_num);
710 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
711 val | 1 << qp->qp_num, QP_LINKS);
713 /* query remote spad for qp ready bits */
714 rc = ntb_read_remote_spad(nt->ndev, QP_LINKS, &val);
716 dev_err(&pdev->dev, "Error reading remote spad %d\n", QP_LINKS);
718 dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val);
720 /* See if the remote side is up */
721 if (1 << qp->qp_num & val) {
722 qp->qp_link = NTB_LINK_UP;
724 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
725 if (qp->event_handler)
726 qp->event_handler(qp->cb_data, NTB_LINK_UP);
727 } else if (nt->transport_link == NTB_LINK_UP)
728 schedule_delayed_work(&qp->link_work,
729 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
732 static void ntb_transport_init_queue(struct ntb_transport *nt,
735 struct ntb_transport_qp *qp;
736 unsigned int num_qps_mw, tx_size;
737 u8 mw_num = QP_TO_MW(qp_num);
739 qp = &nt->qps[qp_num];
743 qp->qp_link = NTB_LINK_DOWN;
744 qp->client_ready = NTB_LINK_DOWN;
745 qp->event_handler = NULL;
747 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
748 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
750 num_qps_mw = nt->max_qps / NTB_NUM_MW;
752 tx_size = ntb_get_mw_size(qp->ndev, mw_num) / num_qps_mw;
753 qp->tx_mw_begin = ntb_get_mw_vbase(nt->ndev, mw_num) +
754 (qp_num / NTB_NUM_MW * tx_size);
755 qp->tx_mw_end = qp->tx_mw_begin + tx_size;
756 qp->tx_offset = qp->tx_mw_begin;
757 qp->tx_max_frame = min(transport_mtu, tx_size);
759 if (nt->debugfs_dir) {
760 char debugfs_name[4];
762 snprintf(debugfs_name, 4, "qp%d", qp_num);
763 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
766 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
768 &ntb_qp_debugfs_stats);
771 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
773 spin_lock_init(&qp->ntb_rx_pend_q_lock);
774 spin_lock_init(&qp->ntb_rx_free_q_lock);
775 spin_lock_init(&qp->ntb_tx_free_q_lock);
777 INIT_LIST_HEAD(&qp->rx_pend_q);
778 INIT_LIST_HEAD(&qp->rx_free_q);
779 INIT_LIST_HEAD(&qp->tx_free_q);
782 int ntb_transport_init(struct pci_dev *pdev)
784 struct ntb_transport *nt;
787 nt = kzalloc(sizeof(struct ntb_transport), GFP_KERNEL);
791 if (debugfs_initialized())
792 nt->debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
794 nt->debugfs_dir = NULL;
796 nt->ndev = ntb_register_transport(pdev, nt);
802 nt->max_qps = min(nt->ndev->max_cbs, max_num_clients);
804 nt->qps = kcalloc(nt->max_qps, sizeof(struct ntb_transport_qp),
811 nt->qp_bitmap = ((u64) 1 << nt->max_qps) - 1;
813 for (i = 0; i < nt->max_qps; i++)
814 ntb_transport_init_queue(nt, i);
816 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
818 rc = ntb_register_event_callback(nt->ndev,
819 ntb_transport_event_callback);
823 INIT_LIST_HEAD(&nt->client_devs);
824 rc = ntb_bus_init(nt);
828 if (ntb_hw_link_status(nt->ndev))
829 schedule_delayed_work(&nt->link_work, 0);
834 ntb_unregister_event_callback(nt->ndev);
838 ntb_unregister_transport(nt->ndev);
840 debugfs_remove_recursive(nt->debugfs_dir);
845 void ntb_transport_free(void *transport)
847 struct ntb_transport *nt = transport;
848 struct pci_dev *pdev;
851 nt->transport_link = NTB_LINK_DOWN;
853 /* verify that all the qp's are freed */
854 for (i = 0; i < nt->max_qps; i++)
855 if (!test_bit(i, &nt->qp_bitmap))
856 ntb_transport_free_queue(&nt->qps[i]);
860 cancel_delayed_work_sync(&nt->link_work);
862 debugfs_remove_recursive(nt->debugfs_dir);
864 ntb_unregister_event_callback(nt->ndev);
866 pdev = ntb_query_pdev(nt->ndev);
868 for (i = 0; i < NTB_NUM_MW; i++)
869 if (nt->mw[i].virt_addr)
870 dma_free_coherent(&pdev->dev, nt->mw[i].size,
875 ntb_unregister_transport(nt->ndev);
879 static void ntb_rx_copy_task(struct ntb_transport_qp *qp,
880 struct ntb_queue_entry *entry, void *offset)
883 struct ntb_payload_header *hdr;
885 BUG_ON(offset < qp->rx_buff_begin ||
886 offset + qp->rx_max_frame >= qp->rx_buff_end);
888 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
889 entry->len = hdr->len;
891 memcpy(entry->buf, offset, entry->len);
893 /* Ensure that the data is fully copied out before clearing the flag */
897 if (qp->rx_handler && qp->client_ready == NTB_LINK_UP)
898 qp->rx_handler(qp, qp->cb_data, entry->cb_data, entry->len);
900 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
903 static int ntb_process_rxc(struct ntb_transport_qp *qp)
905 struct ntb_payload_header *hdr;
906 struct ntb_queue_entry *entry;
909 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
911 hdr = offset + qp->rx_max_frame -
912 sizeof(struct ntb_payload_header);
913 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
914 "no buffer - HDR ver %llu, len %d, flags %x\n",
915 hdr->ver, hdr->len, hdr->flags);
920 offset = qp->rx_offset;
921 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
923 if (!(hdr->flags & DESC_DONE_FLAG)) {
924 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
930 if (hdr->ver != qp->rx_pkts) {
931 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
932 "qp %d: version mismatch, expected %llu - got %llu\n",
933 qp->qp_num, qp->rx_pkts, hdr->ver);
934 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
940 if (hdr->flags & LINK_DOWN_FLAG) {
941 ntb_qp_link_down(qp);
943 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
946 /* Ensure that the data is fully copied out before clearing the
954 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
955 "rx offset %p, ver %llu - %d payload received, buf size %d\n",
956 qp->rx_offset, hdr->ver, hdr->len, entry->len);
958 if (hdr->len <= entry->len)
959 ntb_rx_copy_task(qp, entry, offset);
961 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
964 /* Ensure that the data is fully copied out before clearing the
970 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
971 "RX overflow! Wanted %d got %d\n",
972 hdr->len, entry->len);
975 qp->rx_bytes += hdr->len;
979 qp->rx_offset += qp->rx_max_frame;
980 if (qp->rx_offset + qp->rx_max_frame >= qp->rx_buff_end)
981 qp->rx_offset = qp->rx_buff_begin;
986 static void ntb_transport_rx(unsigned long data)
988 struct ntb_transport_qp *qp = (struct ntb_transport_qp *)data;
992 rc = ntb_process_rxc(qp);
996 static void ntb_transport_rxc_db(void *data, int db_num)
998 struct ntb_transport_qp *qp = data;
1000 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%s: doorbell %d received\n",
1003 tasklet_schedule(&qp->rx_work);
1006 static void ntb_tx_copy_task(struct ntb_transport_qp *qp,
1007 struct ntb_queue_entry *entry,
1010 struct ntb_payload_header *hdr;
1012 BUG_ON(offset < qp->tx_mw_begin ||
1013 offset + qp->tx_max_frame >= qp->tx_mw_end);
1015 memcpy_toio(offset, entry->buf, entry->len);
1017 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1018 hdr->len = entry->len;
1019 hdr->ver = qp->tx_pkts;
1021 /* Ensure that the data is fully copied out before setting the flag */
1023 hdr->flags = entry->flags | DESC_DONE_FLAG;
1025 ntb_ring_sdb(qp->ndev, qp->qp_num);
1027 /* The entry length can only be zero if the packet is intended to be a
1028 * "link down" or similar. Since no payload is being sent in these
1029 * cases, there is nothing to add to the completion queue.
1031 if (entry->len > 0) {
1032 qp->tx_bytes += entry->len;
1035 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1039 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1042 static int ntb_process_tx(struct ntb_transport_qp *qp,
1043 struct ntb_queue_entry *entry)
1045 struct ntb_payload_header *hdr;
1048 offset = qp->tx_offset;
1049 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1051 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%lld - offset %p, tx %p, entry len %d flags %x buff %p\n",
1052 qp->tx_pkts, offset, qp->tx_offset, entry->len, entry->flags,
1059 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
1061 qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1063 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1068 ntb_tx_copy_task(qp, entry, offset);
1070 qp->tx_offset += qp->tx_max_frame;
1071 if (qp->tx_offset + qp->tx_max_frame >= qp->tx_mw_end)
1072 qp->tx_offset = qp->tx_mw_begin;
1079 static void ntb_send_link_down(struct ntb_transport_qp *qp)
1081 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1082 struct ntb_queue_entry *entry;
1085 if (qp->qp_link == NTB_LINK_DOWN)
1088 qp->qp_link = NTB_LINK_DOWN;
1089 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
1091 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1092 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock,
1102 entry->cb_data = NULL;
1105 entry->flags = LINK_DOWN_FLAG;
1107 rc = ntb_process_tx(qp, entry);
1109 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1114 * ntb_transport_create_queue - Create a new NTB transport layer queue
1115 * @rx_handler: receive callback function
1116 * @tx_handler: transmit callback function
1117 * @event_handler: event callback function
1119 * Create a new NTB transport layer queue and provide the queue with a callback
1120 * routine for both transmit and receive. The receive callback routine will be
1121 * used to pass up data when the transport has received it on the queue. The
1122 * transmit callback routine will be called when the transport has completed the
1123 * transmission of the data on the queue and the data is ready to be freed.
1125 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1127 struct ntb_transport_qp *
1128 ntb_transport_create_queue(void *data, struct pci_dev *pdev,
1129 const struct ntb_queue_handlers *handlers)
1131 struct ntb_queue_entry *entry;
1132 struct ntb_transport_qp *qp;
1133 struct ntb_transport *nt;
1134 unsigned int free_queue;
1137 nt = ntb_find_transport(pdev);
1141 free_queue = ffs(nt->qp_bitmap);
1145 /* decrement free_queue to make it zero based */
1148 clear_bit(free_queue, &nt->qp_bitmap);
1150 qp = &nt->qps[free_queue];
1152 qp->rx_handler = handlers->rx_handler;
1153 qp->tx_handler = handlers->tx_handler;
1154 qp->event_handler = handlers->event_handler;
1156 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1157 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1161 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
1165 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1166 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1170 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1174 tasklet_init(&qp->rx_work, ntb_transport_rx, (unsigned long) qp);
1176 rc = ntb_register_db_callback(qp->ndev, free_queue, qp,
1177 ntb_transport_rxc_db);
1181 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1186 tasklet_disable(&qp->rx_work);
1189 ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1193 ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
1195 set_bit(free_queue, &nt->qp_bitmap);
1199 EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1202 * ntb_transport_free_queue - Frees NTB transport queue
1203 * @qp: NTB queue to be freed
1205 * Frees NTB transport queue
1207 void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1209 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1210 struct ntb_queue_entry *entry;
1215 cancel_delayed_work_sync(&qp->link_work);
1217 ntb_unregister_db_callback(qp->ndev, qp->qp_num);
1218 tasklet_disable(&qp->rx_work);
1221 ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
1225 ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q))) {
1226 dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n");
1231 ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1234 set_bit(qp->qp_num, &qp->transport->qp_bitmap);
1236 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1238 EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1241 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1242 * @qp: NTB queue to be freed
1243 * @len: pointer to variable to write enqueued buffers length
1245 * Dequeues unused buffers from receive queue. Should only be used during
1248 * RETURNS: NULL error value on error, or void* for success.
1250 void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1252 struct ntb_queue_entry *entry;
1255 if (!qp || qp->client_ready == NTB_LINK_UP)
1258 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1262 buf = entry->cb_data;
1265 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
1270 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1273 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1274 * @qp: NTB transport layer queue the entry is to be enqueued on
1275 * @cb: per buffer pointer for callback function to use
1276 * @data: pointer to data buffer that incoming packets will be copied into
1277 * @len: length of the data buffer
1279 * Enqueue a new receive buffer onto the transport queue into which a NTB
1280 * payload can be received into.
1282 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1284 int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1287 struct ntb_queue_entry *entry;
1292 entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q);
1296 entry->cb_data = cb;
1300 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
1305 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1308 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1309 * @qp: NTB transport layer queue the entry is to be enqueued on
1310 * @cb: per buffer pointer for callback function to use
1311 * @data: pointer to data buffer that will be sent
1312 * @len: length of the data buffer
1314 * Enqueue a new transmit buffer onto the transport queue from which a NTB
1315 * payload will be transmitted. This assumes that a lock is behing held to
1316 * serialize access to the qp.
1318 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1320 int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1323 struct ntb_queue_entry *entry;
1326 if (!qp || qp->qp_link != NTB_LINK_UP || !len)
1329 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1333 entry->cb_data = cb;
1338 rc = ntb_process_tx(qp, entry);
1340 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1345 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1348 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1349 * @qp: NTB transport layer queue to be enabled
1351 * Notify NTB transport layer of client readiness to use queue
1353 void ntb_transport_link_up(struct ntb_transport_qp *qp)
1358 qp->client_ready = NTB_LINK_UP;
1360 if (qp->transport->transport_link == NTB_LINK_UP)
1361 schedule_delayed_work(&qp->link_work, 0);
1363 EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1366 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1367 * @qp: NTB transport layer queue to be disabled
1369 * Notify NTB transport layer of client's desire to no longer receive data on
1370 * transport queue specified. It is the client's responsibility to ensure all
1371 * entries on queue are purged or otherwise handled appropraitely.
1373 void ntb_transport_link_down(struct ntb_transport_qp *qp)
1375 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1381 qp->client_ready = NTB_LINK_DOWN;
1383 rc = ntb_read_local_spad(qp->ndev, QP_LINKS, &val);
1385 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
1389 rc = ntb_write_remote_spad(qp->ndev, QP_LINKS,
1390 val & ~(1 << qp->qp_num));
1392 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
1393 val & ~(1 << qp->qp_num), QP_LINKS);
1395 if (qp->qp_link == NTB_LINK_UP)
1396 ntb_send_link_down(qp);
1398 cancel_delayed_work_sync(&qp->link_work);
1400 EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1403 * ntb_transport_link_query - Query transport link state
1404 * @qp: NTB transport layer queue to be queried
1406 * Query connectivity to the remote system of the NTB transport queue
1408 * RETURNS: true for link up or false for link down
1410 bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1412 return qp->qp_link == NTB_LINK_UP;
1414 EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1417 * ntb_transport_qp_num - Query the qp number
1418 * @qp: NTB transport layer queue to be queried
1420 * Query qp number of the NTB transport queue
1422 * RETURNS: a zero based number specifying the qp number
1424 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1428 EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1431 * ntb_transport_max_size - Query the max payload size of a qp
1432 * @qp: NTB transport layer queue to be queried
1434 * Query the maximum payload size permissible on the given qp
1436 * RETURNS: the max payload size of a qp
1438 unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
1440 return qp->tx_max_frame - sizeof(struct ntb_payload_header);
1442 EXPORT_SYMBOL_GPL(ntb_transport_max_size);