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 3
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 */
85 struct ntb_transport_qp {
86 struct ntb_transport *transport;
87 struct ntb_device *ndev;
92 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
94 struct ntb_rx_info __iomem *rx_info;
95 struct ntb_rx_info *remote_rx_info;
97 void (*tx_handler) (struct ntb_transport_qp *qp, void *qp_data,
99 struct list_head tx_free_q;
100 spinlock_t ntb_tx_free_q_lock;
102 unsigned int tx_index;
103 unsigned int tx_max_entry;
104 unsigned int tx_max_frame;
106 void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data,
107 void *data, int len);
108 struct tasklet_struct rx_work;
109 struct list_head rx_pend_q;
110 struct list_head rx_free_q;
111 spinlock_t ntb_rx_pend_q_lock;
112 spinlock_t ntb_rx_free_q_lock;
114 unsigned int rx_index;
115 unsigned int rx_max_entry;
116 unsigned int rx_max_frame;
118 void (*event_handler) (void *data, int status);
119 struct delayed_work link_work;
120 struct work_struct link_cleanup;
122 struct dentry *debugfs_dir;
123 struct dentry *debugfs_stats;
137 struct ntb_transport_mw {
143 struct ntb_transport_client_dev {
144 struct list_head entry;
148 struct ntb_transport {
149 struct list_head entry;
150 struct list_head client_devs;
152 struct ntb_device *ndev;
153 struct ntb_transport_mw mw[NTB_NUM_MW];
154 struct ntb_transport_qp *qps;
155 unsigned int max_qps;
156 unsigned long qp_bitmap;
158 struct delayed_work link_work;
159 struct work_struct link_cleanup;
160 struct dentry *debugfs_dir;
164 DESC_DONE_FLAG = 1 << 0,
165 LINK_DOWN_FLAG = 1 << 1,
168 struct ntb_payload_header {
186 #define QP_TO_MW(qp) ((qp) % NTB_NUM_MW)
187 #define NTB_QP_DEF_NUM_ENTRIES 100
188 #define NTB_LINK_DOWN_TIMEOUT 10
190 static int ntb_match_bus(struct device *dev, struct device_driver *drv)
192 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
195 static int ntb_client_probe(struct device *dev)
197 const struct ntb_client *drv = container_of(dev->driver,
198 struct ntb_client, driver);
199 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
203 if (drv && drv->probe)
204 rc = drv->probe(pdev);
211 static int ntb_client_remove(struct device *dev)
213 const struct ntb_client *drv = container_of(dev->driver,
214 struct ntb_client, driver);
215 struct pci_dev *pdev = container_of(dev->parent, struct pci_dev, dev);
217 if (drv && drv->remove)
225 static struct bus_type ntb_bus_type = {
227 .match = ntb_match_bus,
228 .probe = ntb_client_probe,
229 .remove = ntb_client_remove,
232 static LIST_HEAD(ntb_transport_list);
234 static int ntb_bus_init(struct ntb_transport *nt)
236 if (list_empty(&ntb_transport_list)) {
237 int rc = bus_register(&ntb_bus_type);
242 list_add(&nt->entry, &ntb_transport_list);
247 static void ntb_bus_remove(struct ntb_transport *nt)
249 struct ntb_transport_client_dev *client_dev, *cd;
251 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
252 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
253 dev_name(&client_dev->dev));
254 list_del(&client_dev->entry);
255 device_unregister(&client_dev->dev);
258 list_del(&nt->entry);
260 if (list_empty(&ntb_transport_list))
261 bus_unregister(&ntb_bus_type);
264 static void ntb_client_release(struct device *dev)
266 struct ntb_transport_client_dev *client_dev;
267 client_dev = container_of(dev, struct ntb_transport_client_dev, dev);
273 * ntb_unregister_client_dev - Unregister NTB client device
274 * @device_name: Name of NTB client device
276 * Unregister an NTB client device with the NTB transport layer
278 void ntb_unregister_client_dev(char *device_name)
280 struct ntb_transport_client_dev *client, *cd;
281 struct ntb_transport *nt;
283 list_for_each_entry(nt, &ntb_transport_list, entry)
284 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
285 if (!strncmp(dev_name(&client->dev), device_name,
286 strlen(device_name))) {
287 list_del(&client->entry);
288 device_unregister(&client->dev);
291 EXPORT_SYMBOL_GPL(ntb_unregister_client_dev);
294 * ntb_register_client_dev - Register NTB client device
295 * @device_name: Name of NTB client device
297 * Register an NTB client device with the NTB transport layer
299 int ntb_register_client_dev(char *device_name)
301 struct ntb_transport_client_dev *client_dev;
302 struct ntb_transport *nt;
305 if (list_empty(&ntb_transport_list))
308 list_for_each_entry(nt, &ntb_transport_list, entry) {
311 client_dev = kzalloc(sizeof(struct ntb_transport_client_dev),
318 dev = &client_dev->dev;
320 /* setup and register client devices */
321 dev_set_name(dev, "%s", device_name);
322 dev->bus = &ntb_bus_type;
323 dev->release = ntb_client_release;
324 dev->parent = &ntb_query_pdev(nt->ndev)->dev;
326 rc = device_register(dev);
332 list_add_tail(&client_dev->entry, &nt->client_devs);
338 ntb_unregister_client_dev(device_name);
342 EXPORT_SYMBOL_GPL(ntb_register_client_dev);
345 * ntb_register_client - Register NTB client driver
346 * @drv: NTB client driver to be registered
348 * Register an NTB client driver with the NTB transport layer
350 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
352 int ntb_register_client(struct ntb_client *drv)
354 drv->driver.bus = &ntb_bus_type;
356 if (list_empty(&ntb_transport_list))
359 return driver_register(&drv->driver);
361 EXPORT_SYMBOL_GPL(ntb_register_client);
364 * ntb_unregister_client - Unregister NTB client driver
365 * @drv: NTB client driver to be unregistered
367 * Unregister an NTB client driver with the NTB transport layer
369 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
371 void ntb_unregister_client(struct ntb_client *drv)
373 driver_unregister(&drv->driver);
375 EXPORT_SYMBOL_GPL(ntb_unregister_client);
377 static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
380 struct ntb_transport_qp *qp;
382 ssize_t ret, out_offset, out_count;
386 buf = kmalloc(out_count, GFP_KERNEL);
390 qp = filp->private_data;
392 out_offset += snprintf(buf + out_offset, out_count - out_offset,
394 out_offset += snprintf(buf + out_offset, out_count - out_offset,
395 "rx_bytes - \t%llu\n", qp->rx_bytes);
396 out_offset += snprintf(buf + out_offset, out_count - out_offset,
397 "rx_pkts - \t%llu\n", qp->rx_pkts);
398 out_offset += snprintf(buf + out_offset, out_count - out_offset,
399 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
400 out_offset += snprintf(buf + out_offset, out_count - out_offset,
401 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
402 out_offset += snprintf(buf + out_offset, out_count - out_offset,
403 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
404 out_offset += snprintf(buf + out_offset, out_count - out_offset,
405 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
406 out_offset += snprintf(buf + out_offset, out_count - out_offset,
407 "rx_buff - \t%p\n", qp->rx_buff);
408 out_offset += snprintf(buf + out_offset, out_count - out_offset,
409 "rx_index - \t%u\n", qp->rx_index);
410 out_offset += snprintf(buf + out_offset, out_count - out_offset,
411 "rx_max_entry - \t%u\n", qp->rx_max_entry);
413 out_offset += snprintf(buf + out_offset, out_count - out_offset,
414 "tx_bytes - \t%llu\n", qp->tx_bytes);
415 out_offset += snprintf(buf + out_offset, out_count - out_offset,
416 "tx_pkts - \t%llu\n", qp->tx_pkts);
417 out_offset += snprintf(buf + out_offset, out_count - out_offset,
418 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
419 out_offset += snprintf(buf + out_offset, out_count - out_offset,
420 "tx_mw - \t%p\n", qp->tx_mw);
421 out_offset += snprintf(buf + out_offset, out_count - out_offset,
422 "tx_index - \t%u\n", qp->tx_index);
423 out_offset += snprintf(buf + out_offset, out_count - out_offset,
424 "tx_max_entry - \t%u\n", qp->tx_max_entry);
426 out_offset += snprintf(buf + out_offset, out_count - out_offset,
427 "\nQP Link %s\n", (qp->qp_link == NTB_LINK_UP) ?
429 if (out_offset > out_count)
430 out_offset = out_count;
432 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
437 static const struct file_operations ntb_qp_debugfs_stats = {
438 .owner = THIS_MODULE,
440 .read = debugfs_read,
443 static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
444 struct list_head *list)
448 spin_lock_irqsave(lock, flags);
449 list_add_tail(entry, list);
450 spin_unlock_irqrestore(lock, flags);
453 static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
454 struct list_head *list)
456 struct ntb_queue_entry *entry;
459 spin_lock_irqsave(lock, flags);
460 if (list_empty(list)) {
464 entry = list_first_entry(list, struct ntb_queue_entry, entry);
465 list_del(&entry->entry);
467 spin_unlock_irqrestore(lock, flags);
472 static void ntb_transport_setup_qp_mw(struct ntb_transport *nt,
475 struct ntb_transport_qp *qp = &nt->qps[qp_num];
476 unsigned int rx_size, num_qps_mw;
477 u8 mw_num = QP_TO_MW(qp_num);
480 WARN_ON(nt->mw[mw_num].virt_addr == NULL);
482 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
483 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
485 num_qps_mw = nt->max_qps / NTB_NUM_MW;
487 rx_size = (unsigned int) nt->mw[mw_num].size / num_qps_mw;
488 qp->remote_rx_info = nt->mw[mw_num].virt_addr +
489 (qp_num / NTB_NUM_MW * rx_size);
490 rx_size -= sizeof(struct ntb_rx_info);
492 qp->rx_buff = qp->remote_rx_info + 1;
493 qp->rx_max_frame = min(transport_mtu, rx_size);
494 qp->rx_max_entry = rx_size / qp->rx_max_frame;
497 qp->remote_rx_info->entry = qp->rx_max_entry;
499 /* setup the hdr offsets with 0's */
500 for (i = 0; i < qp->rx_max_entry; i++) {
501 void *offset = qp->rx_buff + qp->rx_max_frame * (i + 1) -
502 sizeof(struct ntb_payload_header);
503 memset(offset, 0, sizeof(struct ntb_payload_header));
511 static void ntb_free_mw(struct ntb_transport *nt, int num_mw)
513 struct ntb_transport_mw *mw = &nt->mw[num_mw];
514 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
519 dma_free_coherent(&pdev->dev, mw->size, mw->virt_addr, mw->dma_addr);
520 mw->virt_addr = NULL;
523 static int ntb_set_mw(struct ntb_transport *nt, int num_mw, unsigned int size)
525 struct ntb_transport_mw *mw = &nt->mw[num_mw];
526 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
528 /* No need to re-setup */
529 if (mw->size == ALIGN(size, 4096))
533 ntb_free_mw(nt, num_mw);
535 /* Alloc memory for receiving data. Must be 4k aligned */
536 mw->size = ALIGN(size, 4096);
538 mw->virt_addr = dma_alloc_coherent(&pdev->dev, mw->size, &mw->dma_addr,
540 if (!mw->virt_addr) {
542 dev_err(&pdev->dev, "Unable to allocate MW buffer of size %d\n",
547 /* Notify HW the memory location of the receive buffer */
548 ntb_set_mw_addr(nt->ndev, num_mw, mw->dma_addr);
553 static void ntb_qp_link_cleanup(struct work_struct *work)
555 struct ntb_transport_qp *qp = container_of(work,
556 struct ntb_transport_qp,
558 struct ntb_transport *nt = qp->transport;
559 struct pci_dev *pdev = ntb_query_pdev(nt->ndev);
561 if (qp->qp_link == NTB_LINK_DOWN) {
562 cancel_delayed_work_sync(&qp->link_work);
566 if (qp->event_handler)
567 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
569 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
570 qp->qp_link = NTB_LINK_DOWN;
572 if (nt->transport_link == NTB_LINK_UP)
573 schedule_delayed_work(&qp->link_work,
574 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
577 static void ntb_qp_link_down(struct ntb_transport_qp *qp)
579 schedule_work(&qp->link_cleanup);
582 static void ntb_transport_link_cleanup(struct work_struct *work)
584 struct ntb_transport *nt = container_of(work, struct ntb_transport,
588 if (nt->transport_link == NTB_LINK_DOWN)
589 cancel_delayed_work_sync(&nt->link_work);
591 nt->transport_link = NTB_LINK_DOWN;
593 /* Pass along the info to any clients */
594 for (i = 0; i < nt->max_qps; i++)
595 if (!test_bit(i, &nt->qp_bitmap))
596 ntb_qp_link_down(&nt->qps[i]);
598 /* The scratchpad registers keep the values if the remote side
599 * goes down, blast them now to give them a sane value the next
600 * time they are accessed
602 for (i = 0; i < MAX_SPAD; i++)
603 ntb_write_local_spad(nt->ndev, i, 0);
606 static void ntb_transport_event_callback(void *data, enum ntb_hw_event event)
608 struct ntb_transport *nt = data;
611 case NTB_EVENT_HW_LINK_UP:
612 schedule_delayed_work(&nt->link_work, 0);
614 case NTB_EVENT_HW_LINK_DOWN:
615 schedule_work(&nt->link_cleanup);
622 static void ntb_transport_link_work(struct work_struct *work)
624 struct ntb_transport *nt = container_of(work, struct ntb_transport,
626 struct ntb_device *ndev = nt->ndev;
627 struct pci_dev *pdev = ntb_query_pdev(ndev);
631 /* send the local info, in the opposite order of the way we read it */
632 for (i = 0; i < NTB_NUM_MW; i++) {
633 rc = ntb_write_remote_spad(ndev, MW0_SZ_HIGH + (i * 2),
634 ntb_get_mw_size(ndev, i) >> 32);
636 dev_err(&pdev->dev, "Error writing %u to remote spad %d\n",
637 (u32)(ntb_get_mw_size(ndev, i) >> 32),
638 MW0_SZ_HIGH + (i * 2));
642 rc = ntb_write_remote_spad(ndev, MW0_SZ_LOW + (i * 2),
643 (u32) ntb_get_mw_size(ndev, i));
645 dev_err(&pdev->dev, "Error writing %u to remote spad %d\n",
646 (u32) ntb_get_mw_size(ndev, i),
647 MW0_SZ_LOW + (i * 2));
652 rc = ntb_write_remote_spad(ndev, NUM_MWS, NTB_NUM_MW);
654 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
655 NTB_NUM_MW, NUM_MWS);
659 rc = ntb_write_remote_spad(ndev, NUM_QPS, nt->max_qps);
661 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
662 nt->max_qps, NUM_QPS);
666 rc = ntb_write_remote_spad(ndev, VERSION, NTB_TRANSPORT_VERSION);
668 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
669 NTB_TRANSPORT_VERSION, VERSION);
673 /* Query the remote side for its info */
674 rc = ntb_read_remote_spad(ndev, VERSION, &val);
676 dev_err(&pdev->dev, "Error reading remote spad %d\n", VERSION);
680 if (val != NTB_TRANSPORT_VERSION)
682 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
684 rc = ntb_read_remote_spad(ndev, NUM_QPS, &val);
686 dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_QPS);
690 if (val != nt->max_qps)
692 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
694 rc = ntb_read_remote_spad(ndev, NUM_MWS, &val);
696 dev_err(&pdev->dev, "Error reading remote spad %d\n", NUM_MWS);
700 if (val != NTB_NUM_MW)
702 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
704 for (i = 0; i < NTB_NUM_MW; i++) {
707 rc = ntb_read_remote_spad(ndev, MW0_SZ_HIGH + (i * 2), &val);
709 dev_err(&pdev->dev, "Error reading remote spad %d\n",
710 MW0_SZ_HIGH + (i * 2));
714 val64 = (u64) val << 32;
716 rc = ntb_read_remote_spad(ndev, MW0_SZ_LOW + (i * 2), &val);
718 dev_err(&pdev->dev, "Error reading remote spad %d\n",
719 MW0_SZ_LOW + (i * 2));
725 dev_dbg(&pdev->dev, "Remote MW%d size = %llu\n", i, val64);
727 rc = ntb_set_mw(nt, i, val64);
732 nt->transport_link = NTB_LINK_UP;
734 for (i = 0; i < nt->max_qps; i++) {
735 struct ntb_transport_qp *qp = &nt->qps[i];
737 ntb_transport_setup_qp_mw(nt, i);
739 if (qp->client_ready == NTB_LINK_UP)
740 schedule_delayed_work(&qp->link_work, 0);
746 for (i = 0; i < NTB_NUM_MW; i++)
749 if (ntb_hw_link_status(ndev))
750 schedule_delayed_work(&nt->link_work,
751 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
754 static void ntb_qp_link_work(struct work_struct *work)
756 struct ntb_transport_qp *qp = container_of(work,
757 struct ntb_transport_qp,
759 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
760 struct ntb_transport *nt = qp->transport;
763 WARN_ON(nt->transport_link != NTB_LINK_UP);
765 rc = ntb_read_local_spad(nt->ndev, QP_LINKS, &val);
767 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
771 rc = ntb_write_remote_spad(nt->ndev, QP_LINKS, val | 1 << qp->qp_num);
773 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
774 val | 1 << qp->qp_num, QP_LINKS);
776 /* query remote spad for qp ready bits */
777 rc = ntb_read_remote_spad(nt->ndev, QP_LINKS, &val);
779 dev_err(&pdev->dev, "Error reading remote spad %d\n", QP_LINKS);
781 dev_dbg(&pdev->dev, "Remote QP link status = %x\n", val);
783 /* See if the remote side is up */
784 if (1 << qp->qp_num & val) {
785 qp->qp_link = NTB_LINK_UP;
787 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
788 if (qp->event_handler)
789 qp->event_handler(qp->cb_data, NTB_LINK_UP);
790 } else if (nt->transport_link == NTB_LINK_UP)
791 schedule_delayed_work(&qp->link_work,
792 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
795 static void ntb_transport_init_queue(struct ntb_transport *nt,
798 struct ntb_transport_qp *qp;
799 unsigned int num_qps_mw, tx_size;
800 u8 mw_num = QP_TO_MW(qp_num);
802 qp = &nt->qps[qp_num];
806 qp->qp_link = NTB_LINK_DOWN;
807 qp->client_ready = NTB_LINK_DOWN;
808 qp->event_handler = NULL;
810 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
811 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
813 num_qps_mw = nt->max_qps / NTB_NUM_MW;
815 tx_size = (unsigned int) ntb_get_mw_size(qp->ndev, mw_num) / num_qps_mw;
816 qp->rx_info = ntb_get_mw_vbase(nt->ndev, mw_num) +
817 (qp_num / NTB_NUM_MW * tx_size);
818 tx_size -= sizeof(struct ntb_rx_info);
820 qp->tx_mw = qp->rx_info + 1;
821 qp->tx_max_frame = min(transport_mtu, tx_size);
822 qp->tx_max_entry = tx_size / qp->tx_max_frame;
824 if (nt->debugfs_dir) {
825 char debugfs_name[4];
827 snprintf(debugfs_name, 4, "qp%d", qp_num);
828 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
831 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
833 &ntb_qp_debugfs_stats);
836 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
837 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup);
839 spin_lock_init(&qp->ntb_rx_pend_q_lock);
840 spin_lock_init(&qp->ntb_rx_free_q_lock);
841 spin_lock_init(&qp->ntb_tx_free_q_lock);
843 INIT_LIST_HEAD(&qp->rx_pend_q);
844 INIT_LIST_HEAD(&qp->rx_free_q);
845 INIT_LIST_HEAD(&qp->tx_free_q);
848 int ntb_transport_init(struct pci_dev *pdev)
850 struct ntb_transport *nt;
853 nt = kzalloc(sizeof(struct ntb_transport), GFP_KERNEL);
857 if (debugfs_initialized())
858 nt->debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
860 nt->debugfs_dir = NULL;
862 nt->ndev = ntb_register_transport(pdev, nt);
868 nt->max_qps = min(nt->ndev->max_cbs, max_num_clients);
870 nt->qps = kcalloc(nt->max_qps, sizeof(struct ntb_transport_qp),
877 nt->qp_bitmap = ((u64) 1 << nt->max_qps) - 1;
879 for (i = 0; i < nt->max_qps; i++)
880 ntb_transport_init_queue(nt, i);
882 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
883 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup);
885 rc = ntb_register_event_callback(nt->ndev,
886 ntb_transport_event_callback);
890 INIT_LIST_HEAD(&nt->client_devs);
891 rc = ntb_bus_init(nt);
895 if (ntb_hw_link_status(nt->ndev))
896 schedule_delayed_work(&nt->link_work, 0);
901 ntb_unregister_event_callback(nt->ndev);
905 ntb_unregister_transport(nt->ndev);
907 debugfs_remove_recursive(nt->debugfs_dir);
912 void ntb_transport_free(void *transport)
914 struct ntb_transport *nt = transport;
915 struct pci_dev *pdev;
918 nt->transport_link = NTB_LINK_DOWN;
920 /* verify that all the qp's are freed */
921 for (i = 0; i < nt->max_qps; i++)
922 if (!test_bit(i, &nt->qp_bitmap))
923 ntb_transport_free_queue(&nt->qps[i]);
927 cancel_delayed_work_sync(&nt->link_work);
929 debugfs_remove_recursive(nt->debugfs_dir);
931 ntb_unregister_event_callback(nt->ndev);
933 pdev = ntb_query_pdev(nt->ndev);
935 for (i = 0; i < NTB_NUM_MW; i++)
939 ntb_unregister_transport(nt->ndev);
943 static void ntb_rx_copy_task(struct ntb_transport_qp *qp,
944 struct ntb_queue_entry *entry, void *offset)
946 void *cb_data = entry->cb_data;
947 unsigned int len = entry->len;
949 memcpy(entry->buf, offset, entry->len);
951 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
953 if (qp->rx_handler && qp->client_ready == NTB_LINK_UP)
954 qp->rx_handler(qp, qp->cb_data, cb_data, len);
957 static int ntb_process_rxc(struct ntb_transport_qp *qp)
959 struct ntb_payload_header *hdr;
960 struct ntb_queue_entry *entry;
963 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
964 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
966 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
968 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
969 "no buffer - HDR ver %u, len %d, flags %x\n",
970 hdr->ver, hdr->len, hdr->flags);
975 if (!(hdr->flags & DESC_DONE_FLAG)) {
976 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
982 if (hdr->ver != (u32) qp->rx_pkts) {
983 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
984 "qp %d: version mismatch, expected %llu - got %u\n",
985 qp->qp_num, qp->rx_pkts, hdr->ver);
986 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
992 if (hdr->flags & LINK_DOWN_FLAG) {
993 ntb_qp_link_down(qp);
995 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
1000 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
1001 "rx offset %u, ver %u - %d payload received, buf size %d\n",
1002 qp->rx_index, hdr->ver, hdr->len, entry->len);
1004 if (hdr->len <= entry->len) {
1005 entry->len = hdr->len;
1006 ntb_rx_copy_task(qp, entry, offset);
1008 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry,
1012 dev_dbg(&ntb_query_pdev(qp->ndev)->dev,
1013 "RX overflow! Wanted %d got %d\n",
1014 hdr->len, entry->len);
1017 qp->rx_bytes += hdr->len;
1021 /* Ensure that the data is fully copied out before clearing the flag */
1024 iowrite32(qp->rx_index, &qp->rx_info->entry);
1027 qp->rx_index %= qp->rx_max_entry;
1032 static void ntb_transport_rx(unsigned long data)
1034 struct ntb_transport_qp *qp = (struct ntb_transport_qp *)data;
1038 rc = ntb_process_rxc(qp);
1042 static void ntb_transport_rxc_db(void *data, int db_num)
1044 struct ntb_transport_qp *qp = data;
1046 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%s: doorbell %d received\n",
1049 tasklet_schedule(&qp->rx_work);
1052 static void ntb_tx_copy_task(struct ntb_transport_qp *qp,
1053 struct ntb_queue_entry *entry,
1054 void __iomem *offset)
1056 struct ntb_payload_header __iomem *hdr;
1058 memcpy_toio(offset, entry->buf, entry->len);
1060 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1061 iowrite32(entry->len, &hdr->len);
1062 iowrite32((u32) qp->tx_pkts, &hdr->ver);
1064 /* Ensure that the data is fully copied out before setting the flag */
1066 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
1068 ntb_ring_sdb(qp->ndev, qp->qp_num);
1070 /* The entry length can only be zero if the packet is intended to be a
1071 * "link down" or similar. Since no payload is being sent in these
1072 * cases, there is nothing to add to the completion queue.
1074 if (entry->len > 0) {
1075 qp->tx_bytes += entry->len;
1078 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1082 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1085 static int ntb_process_tx(struct ntb_transport_qp *qp,
1086 struct ntb_queue_entry *entry)
1088 void __iomem *offset;
1090 offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
1092 dev_dbg(&ntb_query_pdev(qp->ndev)->dev, "%lld - offset %p, tx %u, entry len %d flags %x buff %p\n",
1093 qp->tx_pkts, offset, qp->tx_index, entry->len, entry->flags,
1095 if (qp->tx_index == qp->remote_rx_info->entry) {
1100 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
1102 qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1104 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1109 ntb_tx_copy_task(qp, entry, offset);
1112 qp->tx_index %= qp->tx_max_entry;
1119 static void ntb_send_link_down(struct ntb_transport_qp *qp)
1121 struct pci_dev *pdev = ntb_query_pdev(qp->ndev);
1122 struct ntb_queue_entry *entry;
1125 if (qp->qp_link == NTB_LINK_DOWN)
1128 qp->qp_link = NTB_LINK_DOWN;
1129 dev_info(&pdev->dev, "qp %d: Link Down\n", qp->qp_num);
1131 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1132 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1141 entry->cb_data = NULL;
1144 entry->flags = LINK_DOWN_FLAG;
1146 rc = ntb_process_tx(qp, entry);
1148 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1153 * ntb_transport_create_queue - Create a new NTB transport layer queue
1154 * @rx_handler: receive callback function
1155 * @tx_handler: transmit callback function
1156 * @event_handler: event callback function
1158 * Create a new NTB transport layer queue and provide the queue with a callback
1159 * routine for both transmit and receive. The receive callback routine will be
1160 * used to pass up data when the transport has received it on the queue. The
1161 * transmit callback routine will be called when the transport has completed the
1162 * transmission of the data on the queue and the data is ready to be freed.
1164 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1166 struct ntb_transport_qp *
1167 ntb_transport_create_queue(void *data, struct pci_dev *pdev,
1168 const struct ntb_queue_handlers *handlers)
1170 struct ntb_queue_entry *entry;
1171 struct ntb_transport_qp *qp;
1172 struct ntb_transport *nt;
1173 unsigned int free_queue;
1176 nt = ntb_find_transport(pdev);
1180 free_queue = ffs(nt->qp_bitmap);
1184 /* decrement free_queue to make it zero based */
1187 clear_bit(free_queue, &nt->qp_bitmap);
1189 qp = &nt->qps[free_queue];
1191 qp->rx_handler = handlers->rx_handler;
1192 qp->tx_handler = handlers->tx_handler;
1193 qp->event_handler = handlers->event_handler;
1195 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1196 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1200 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry,
1204 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1205 entry = kzalloc(sizeof(struct ntb_queue_entry), GFP_ATOMIC);
1209 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1213 tasklet_init(&qp->rx_work, ntb_transport_rx, (unsigned long) qp);
1215 rc = ntb_register_db_callback(qp->ndev, free_queue, qp,
1216 ntb_transport_rxc_db);
1220 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1225 tasklet_disable(&qp->rx_work);
1227 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1230 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
1232 set_bit(free_queue, &nt->qp_bitmap);
1236 EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1239 * ntb_transport_free_queue - Frees NTB transport queue
1240 * @qp: NTB queue to be freed
1242 * Frees NTB transport queue
1244 void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1246 struct pci_dev *pdev;
1247 struct ntb_queue_entry *entry;
1252 pdev = ntb_query_pdev(qp->ndev);
1254 cancel_delayed_work_sync(&qp->link_work);
1256 ntb_unregister_db_callback(qp->ndev, qp->qp_num);
1257 tasklet_disable(&qp->rx_work);
1259 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
1262 while ((entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q))) {
1263 dev_warn(&pdev->dev, "Freeing item from a non-empty queue\n");
1267 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1270 set_bit(qp->qp_num, &qp->transport->qp_bitmap);
1272 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1274 EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1277 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1278 * @qp: NTB queue to be freed
1279 * @len: pointer to variable to write enqueued buffers length
1281 * Dequeues unused buffers from receive queue. Should only be used during
1284 * RETURNS: NULL error value on error, or void* for success.
1286 void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1288 struct ntb_queue_entry *entry;
1291 if (!qp || qp->client_ready == NTB_LINK_UP)
1294 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
1298 buf = entry->cb_data;
1301 ntb_list_add(&qp->ntb_rx_free_q_lock, &entry->entry, &qp->rx_free_q);
1305 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1308 * ntb_transport_rx_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 incoming packets will be copied into
1312 * @len: length of the data buffer
1314 * Enqueue a new receive buffer onto the transport queue into which a NTB
1315 * payload can be received into.
1317 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1319 int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1322 struct ntb_queue_entry *entry;
1327 entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q);
1331 entry->cb_data = cb;
1335 ntb_list_add(&qp->ntb_rx_pend_q_lock, &entry->entry, &qp->rx_pend_q);
1339 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1342 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1343 * @qp: NTB transport layer queue the entry is to be enqueued on
1344 * @cb: per buffer pointer for callback function to use
1345 * @data: pointer to data buffer that will be sent
1346 * @len: length of the data buffer
1348 * Enqueue a new transmit buffer onto the transport queue from which a NTB
1349 * payload will be transmitted. This assumes that a lock is behing held to
1350 * serialize access to the qp.
1352 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1354 int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1357 struct ntb_queue_entry *entry;
1360 if (!qp || qp->qp_link != NTB_LINK_UP || !len)
1363 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1367 entry->cb_data = cb;
1372 rc = ntb_process_tx(qp, entry);
1374 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1379 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1382 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1383 * @qp: NTB transport layer queue to be enabled
1385 * Notify NTB transport layer of client readiness to use queue
1387 void ntb_transport_link_up(struct ntb_transport_qp *qp)
1392 qp->client_ready = NTB_LINK_UP;
1394 if (qp->transport->transport_link == NTB_LINK_UP)
1395 schedule_delayed_work(&qp->link_work, 0);
1397 EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1400 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1401 * @qp: NTB transport layer queue to be disabled
1403 * Notify NTB transport layer of client's desire to no longer receive data on
1404 * transport queue specified. It is the client's responsibility to ensure all
1405 * entries on queue are purged or otherwise handled appropraitely.
1407 void ntb_transport_link_down(struct ntb_transport_qp *qp)
1409 struct pci_dev *pdev;
1415 pdev = ntb_query_pdev(qp->ndev);
1416 qp->client_ready = NTB_LINK_DOWN;
1418 rc = ntb_read_local_spad(qp->ndev, QP_LINKS, &val);
1420 dev_err(&pdev->dev, "Error reading spad %d\n", QP_LINKS);
1424 rc = ntb_write_remote_spad(qp->ndev, QP_LINKS,
1425 val & ~(1 << qp->qp_num));
1427 dev_err(&pdev->dev, "Error writing %x to remote spad %d\n",
1428 val & ~(1 << qp->qp_num), QP_LINKS);
1430 if (qp->qp_link == NTB_LINK_UP)
1431 ntb_send_link_down(qp);
1433 cancel_delayed_work_sync(&qp->link_work);
1435 EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1438 * ntb_transport_link_query - Query transport link state
1439 * @qp: NTB transport layer queue to be queried
1441 * Query connectivity to the remote system of the NTB transport queue
1443 * RETURNS: true for link up or false for link down
1445 bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1450 return qp->qp_link == NTB_LINK_UP;
1452 EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1455 * ntb_transport_qp_num - Query the qp number
1456 * @qp: NTB transport layer queue to be queried
1458 * Query qp number of the NTB transport queue
1460 * RETURNS: a zero based number specifying the qp number
1462 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1469 EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1472 * ntb_transport_max_size - Query the max payload size of a qp
1473 * @qp: NTB transport layer queue to be queried
1475 * Query the maximum payload size permissible on the given qp
1477 * RETURNS: the max payload size of a qp
1479 unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
1484 return qp->tx_max_frame - sizeof(struct ntb_payload_header);
1486 EXPORT_SYMBOL_GPL(ntb_transport_max_size);