x86/bugs, KVM: Extend speculation control for VIRT_SPEC_CTRL
[linux/fpc-iii.git] / drivers / ntb / ntb_transport.c
blob24222a5d8df26f0965b003ee4856a92123a5f0ca
1 /*
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.
5 * GPL LICENSE SUMMARY
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
8 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
14 * BSD LICENSE
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
17 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
28 * distribution.
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 * PCIe NTB Transport Linux driver
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
50 #include <linux/debugfs.h>
51 #include <linux/delay.h>
52 #include <linux/dmaengine.h>
53 #include <linux/dma-mapping.h>
54 #include <linux/errno.h>
55 #include <linux/export.h>
56 #include <linux/interrupt.h>
57 #include <linux/module.h>
58 #include <linux/pci.h>
59 #include <linux/slab.h>
60 #include <linux/types.h>
61 #include <linux/uaccess.h>
62 #include "linux/ntb.h"
63 #include "linux/ntb_transport.h"
65 #define NTB_TRANSPORT_VERSION 4
66 #define NTB_TRANSPORT_VER "4"
67 #define NTB_TRANSPORT_NAME "ntb_transport"
68 #define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
70 MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
71 MODULE_VERSION(NTB_TRANSPORT_VER);
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_AUTHOR("Intel Corporation");
75 static unsigned long max_mw_size;
76 module_param(max_mw_size, ulong, 0644);
77 MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
79 static unsigned int transport_mtu = 0x10000;
80 module_param(transport_mtu, uint, 0644);
81 MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
83 static unsigned char max_num_clients;
84 module_param(max_num_clients, byte, 0644);
85 MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
87 static unsigned int copy_bytes = 1024;
88 module_param(copy_bytes, uint, 0644);
89 MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
91 static bool use_dma;
92 module_param(use_dma, bool, 0644);
93 MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
95 static struct dentry *nt_debugfs_dir;
97 struct ntb_queue_entry {
98 /* ntb_queue list reference */
99 struct list_head entry;
100 /* pointers to data to be transferred */
101 void *cb_data;
102 void *buf;
103 unsigned int len;
104 unsigned int flags;
105 int retries;
106 int errors;
107 unsigned int tx_index;
108 unsigned int rx_index;
110 struct ntb_transport_qp *qp;
111 union {
112 struct ntb_payload_header __iomem *tx_hdr;
113 struct ntb_payload_header *rx_hdr;
117 struct ntb_rx_info {
118 unsigned int entry;
121 struct ntb_transport_qp {
122 struct ntb_transport_ctx *transport;
123 struct ntb_dev *ndev;
124 void *cb_data;
125 struct dma_chan *tx_dma_chan;
126 struct dma_chan *rx_dma_chan;
128 bool client_ready;
129 bool link_is_up;
130 bool active;
132 u8 qp_num; /* Only 64 QP's are allowed. 0-63 */
133 u64 qp_bit;
135 struct ntb_rx_info __iomem *rx_info;
136 struct ntb_rx_info *remote_rx_info;
138 void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
139 void *data, int len);
140 struct list_head tx_free_q;
141 spinlock_t ntb_tx_free_q_lock;
142 void __iomem *tx_mw;
143 dma_addr_t tx_mw_phys;
144 unsigned int tx_index;
145 unsigned int tx_max_entry;
146 unsigned int tx_max_frame;
148 void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
149 void *data, int len);
150 struct list_head rx_post_q;
151 struct list_head rx_pend_q;
152 struct list_head rx_free_q;
153 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
154 spinlock_t ntb_rx_q_lock;
155 void *rx_buff;
156 unsigned int rx_index;
157 unsigned int rx_max_entry;
158 unsigned int rx_max_frame;
159 unsigned int rx_alloc_entry;
160 dma_cookie_t last_cookie;
161 struct tasklet_struct rxc_db_work;
163 void (*event_handler)(void *data, int status);
164 struct delayed_work link_work;
165 struct work_struct link_cleanup;
167 struct dentry *debugfs_dir;
168 struct dentry *debugfs_stats;
170 /* Stats */
171 u64 rx_bytes;
172 u64 rx_pkts;
173 u64 rx_ring_empty;
174 u64 rx_err_no_buf;
175 u64 rx_err_oflow;
176 u64 rx_err_ver;
177 u64 rx_memcpy;
178 u64 rx_async;
179 u64 tx_bytes;
180 u64 tx_pkts;
181 u64 tx_ring_full;
182 u64 tx_err_no_buf;
183 u64 tx_memcpy;
184 u64 tx_async;
187 struct ntb_transport_mw {
188 phys_addr_t phys_addr;
189 resource_size_t phys_size;
190 resource_size_t xlat_align;
191 resource_size_t xlat_align_size;
192 void __iomem *vbase;
193 size_t xlat_size;
194 size_t buff_size;
195 void *virt_addr;
196 dma_addr_t dma_addr;
199 struct ntb_transport_client_dev {
200 struct list_head entry;
201 struct ntb_transport_ctx *nt;
202 struct device dev;
205 struct ntb_transport_ctx {
206 struct list_head entry;
207 struct list_head client_devs;
209 struct ntb_dev *ndev;
211 struct ntb_transport_mw *mw_vec;
212 struct ntb_transport_qp *qp_vec;
213 unsigned int mw_count;
214 unsigned int qp_count;
215 u64 qp_bitmap;
216 u64 qp_bitmap_free;
218 bool link_is_up;
219 struct delayed_work link_work;
220 struct work_struct link_cleanup;
222 struct dentry *debugfs_node_dir;
225 enum {
226 DESC_DONE_FLAG = BIT(0),
227 LINK_DOWN_FLAG = BIT(1),
230 struct ntb_payload_header {
231 unsigned int ver;
232 unsigned int len;
233 unsigned int flags;
236 enum {
237 VERSION = 0,
238 QP_LINKS,
239 NUM_QPS,
240 NUM_MWS,
241 MW0_SZ_HIGH,
242 MW0_SZ_LOW,
243 MW1_SZ_HIGH,
244 MW1_SZ_LOW,
245 MAX_SPAD,
248 #define dev_client_dev(__dev) \
249 container_of((__dev), struct ntb_transport_client_dev, dev)
251 #define drv_client(__drv) \
252 container_of((__drv), struct ntb_transport_client, driver)
254 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
255 #define NTB_QP_DEF_NUM_ENTRIES 100
256 #define NTB_LINK_DOWN_TIMEOUT 10
258 static void ntb_transport_rxc_db(unsigned long data);
259 static const struct ntb_ctx_ops ntb_transport_ops;
260 static struct ntb_client ntb_transport_client;
261 static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
262 struct ntb_queue_entry *entry);
263 static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
264 static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
265 static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
268 static int ntb_transport_bus_match(struct device *dev,
269 struct device_driver *drv)
271 return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
274 static int ntb_transport_bus_probe(struct device *dev)
276 const struct ntb_transport_client *client;
277 int rc = -EINVAL;
279 get_device(dev);
281 client = drv_client(dev->driver);
282 rc = client->probe(dev);
283 if (rc)
284 put_device(dev);
286 return rc;
289 static int ntb_transport_bus_remove(struct device *dev)
291 const struct ntb_transport_client *client;
293 client = drv_client(dev->driver);
294 client->remove(dev);
296 put_device(dev);
298 return 0;
301 static struct bus_type ntb_transport_bus = {
302 .name = "ntb_transport",
303 .match = ntb_transport_bus_match,
304 .probe = ntb_transport_bus_probe,
305 .remove = ntb_transport_bus_remove,
308 static LIST_HEAD(ntb_transport_list);
310 static int ntb_bus_init(struct ntb_transport_ctx *nt)
312 list_add_tail(&nt->entry, &ntb_transport_list);
313 return 0;
316 static void ntb_bus_remove(struct ntb_transport_ctx *nt)
318 struct ntb_transport_client_dev *client_dev, *cd;
320 list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
321 dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
322 dev_name(&client_dev->dev));
323 list_del(&client_dev->entry);
324 device_unregister(&client_dev->dev);
327 list_del(&nt->entry);
330 static void ntb_transport_client_release(struct device *dev)
332 struct ntb_transport_client_dev *client_dev;
334 client_dev = dev_client_dev(dev);
335 kfree(client_dev);
339 * ntb_transport_unregister_client_dev - Unregister NTB client device
340 * @device_name: Name of NTB client device
342 * Unregister an NTB client device with the NTB transport layer
344 void ntb_transport_unregister_client_dev(char *device_name)
346 struct ntb_transport_client_dev *client, *cd;
347 struct ntb_transport_ctx *nt;
349 list_for_each_entry(nt, &ntb_transport_list, entry)
350 list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
351 if (!strncmp(dev_name(&client->dev), device_name,
352 strlen(device_name))) {
353 list_del(&client->entry);
354 device_unregister(&client->dev);
357 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
360 * ntb_transport_register_client_dev - Register NTB client device
361 * @device_name: Name of NTB client device
363 * Register an NTB client device with the NTB transport layer
365 int ntb_transport_register_client_dev(char *device_name)
367 struct ntb_transport_client_dev *client_dev;
368 struct ntb_transport_ctx *nt;
369 int node;
370 int rc, i = 0;
372 if (list_empty(&ntb_transport_list))
373 return -ENODEV;
375 list_for_each_entry(nt, &ntb_transport_list, entry) {
376 struct device *dev;
378 node = dev_to_node(&nt->ndev->dev);
380 client_dev = kzalloc_node(sizeof(*client_dev),
381 GFP_KERNEL, node);
382 if (!client_dev) {
383 rc = -ENOMEM;
384 goto err;
387 dev = &client_dev->dev;
389 /* setup and register client devices */
390 dev_set_name(dev, "%s%d", device_name, i);
391 dev->bus = &ntb_transport_bus;
392 dev->release = ntb_transport_client_release;
393 dev->parent = &nt->ndev->dev;
395 rc = device_register(dev);
396 if (rc) {
397 kfree(client_dev);
398 goto err;
401 list_add_tail(&client_dev->entry, &nt->client_devs);
402 i++;
405 return 0;
407 err:
408 ntb_transport_unregister_client_dev(device_name);
410 return rc;
412 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
415 * ntb_transport_register_client - Register NTB client driver
416 * @drv: NTB client driver to be registered
418 * Register an NTB client driver with the NTB transport layer
420 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
422 int ntb_transport_register_client(struct ntb_transport_client *drv)
424 drv->driver.bus = &ntb_transport_bus;
426 if (list_empty(&ntb_transport_list))
427 return -ENODEV;
429 return driver_register(&drv->driver);
431 EXPORT_SYMBOL_GPL(ntb_transport_register_client);
434 * ntb_transport_unregister_client - Unregister NTB client driver
435 * @drv: NTB client driver to be unregistered
437 * Unregister an NTB client driver with the NTB transport layer
439 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
441 void ntb_transport_unregister_client(struct ntb_transport_client *drv)
443 driver_unregister(&drv->driver);
445 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
447 static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
448 loff_t *offp)
450 struct ntb_transport_qp *qp;
451 char *buf;
452 ssize_t ret, out_offset, out_count;
454 qp = filp->private_data;
456 if (!qp || !qp->link_is_up)
457 return 0;
459 out_count = 1000;
461 buf = kmalloc(out_count, GFP_KERNEL);
462 if (!buf)
463 return -ENOMEM;
465 out_offset = 0;
466 out_offset += snprintf(buf + out_offset, out_count - out_offset,
467 "\nNTB QP stats:\n\n");
468 out_offset += snprintf(buf + out_offset, out_count - out_offset,
469 "rx_bytes - \t%llu\n", qp->rx_bytes);
470 out_offset += snprintf(buf + out_offset, out_count - out_offset,
471 "rx_pkts - \t%llu\n", qp->rx_pkts);
472 out_offset += snprintf(buf + out_offset, out_count - out_offset,
473 "rx_memcpy - \t%llu\n", qp->rx_memcpy);
474 out_offset += snprintf(buf + out_offset, out_count - out_offset,
475 "rx_async - \t%llu\n", qp->rx_async);
476 out_offset += snprintf(buf + out_offset, out_count - out_offset,
477 "rx_ring_empty - %llu\n", qp->rx_ring_empty);
478 out_offset += snprintf(buf + out_offset, out_count - out_offset,
479 "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
480 out_offset += snprintf(buf + out_offset, out_count - out_offset,
481 "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
482 out_offset += snprintf(buf + out_offset, out_count - out_offset,
483 "rx_err_ver - \t%llu\n", qp->rx_err_ver);
484 out_offset += snprintf(buf + out_offset, out_count - out_offset,
485 "rx_buff - \t0x%p\n", qp->rx_buff);
486 out_offset += snprintf(buf + out_offset, out_count - out_offset,
487 "rx_index - \t%u\n", qp->rx_index);
488 out_offset += snprintf(buf + out_offset, out_count - out_offset,
489 "rx_max_entry - \t%u\n", qp->rx_max_entry);
490 out_offset += snprintf(buf + out_offset, out_count - out_offset,
491 "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
493 out_offset += snprintf(buf + out_offset, out_count - out_offset,
494 "tx_bytes - \t%llu\n", qp->tx_bytes);
495 out_offset += snprintf(buf + out_offset, out_count - out_offset,
496 "tx_pkts - \t%llu\n", qp->tx_pkts);
497 out_offset += snprintf(buf + out_offset, out_count - out_offset,
498 "tx_memcpy - \t%llu\n", qp->tx_memcpy);
499 out_offset += snprintf(buf + out_offset, out_count - out_offset,
500 "tx_async - \t%llu\n", qp->tx_async);
501 out_offset += snprintf(buf + out_offset, out_count - out_offset,
502 "tx_ring_full - \t%llu\n", qp->tx_ring_full);
503 out_offset += snprintf(buf + out_offset, out_count - out_offset,
504 "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
505 out_offset += snprintf(buf + out_offset, out_count - out_offset,
506 "tx_mw - \t0x%p\n", qp->tx_mw);
507 out_offset += snprintf(buf + out_offset, out_count - out_offset,
508 "tx_index (H) - \t%u\n", qp->tx_index);
509 out_offset += snprintf(buf + out_offset, out_count - out_offset,
510 "RRI (T) - \t%u\n",
511 qp->remote_rx_info->entry);
512 out_offset += snprintf(buf + out_offset, out_count - out_offset,
513 "tx_max_entry - \t%u\n", qp->tx_max_entry);
514 out_offset += snprintf(buf + out_offset, out_count - out_offset,
515 "free tx - \t%u\n",
516 ntb_transport_tx_free_entry(qp));
518 out_offset += snprintf(buf + out_offset, out_count - out_offset,
519 "\n");
520 out_offset += snprintf(buf + out_offset, out_count - out_offset,
521 "Using TX DMA - \t%s\n",
522 qp->tx_dma_chan ? "Yes" : "No");
523 out_offset += snprintf(buf + out_offset, out_count - out_offset,
524 "Using RX DMA - \t%s\n",
525 qp->rx_dma_chan ? "Yes" : "No");
526 out_offset += snprintf(buf + out_offset, out_count - out_offset,
527 "QP Link - \t%s\n",
528 qp->link_is_up ? "Up" : "Down");
529 out_offset += snprintf(buf + out_offset, out_count - out_offset,
530 "\n");
532 if (out_offset > out_count)
533 out_offset = out_count;
535 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
536 kfree(buf);
537 return ret;
540 static const struct file_operations ntb_qp_debugfs_stats = {
541 .owner = THIS_MODULE,
542 .open = simple_open,
543 .read = debugfs_read,
546 static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
547 struct list_head *list)
549 unsigned long flags;
551 spin_lock_irqsave(lock, flags);
552 list_add_tail(entry, list);
553 spin_unlock_irqrestore(lock, flags);
556 static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
557 struct list_head *list)
559 struct ntb_queue_entry *entry;
560 unsigned long flags;
562 spin_lock_irqsave(lock, flags);
563 if (list_empty(list)) {
564 entry = NULL;
565 goto out;
567 entry = list_first_entry(list, struct ntb_queue_entry, entry);
568 list_del(&entry->entry);
570 out:
571 spin_unlock_irqrestore(lock, flags);
573 return entry;
576 static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
577 struct list_head *list,
578 struct list_head *to_list)
580 struct ntb_queue_entry *entry;
581 unsigned long flags;
583 spin_lock_irqsave(lock, flags);
585 if (list_empty(list)) {
586 entry = NULL;
587 } else {
588 entry = list_first_entry(list, struct ntb_queue_entry, entry);
589 list_move_tail(&entry->entry, to_list);
592 spin_unlock_irqrestore(lock, flags);
594 return entry;
597 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
598 unsigned int qp_num)
600 struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
601 struct ntb_transport_mw *mw;
602 struct ntb_dev *ndev = nt->ndev;
603 struct ntb_queue_entry *entry;
604 unsigned int rx_size, num_qps_mw;
605 unsigned int mw_num, mw_count, qp_count;
606 unsigned int i;
607 int node;
609 mw_count = nt->mw_count;
610 qp_count = nt->qp_count;
612 mw_num = QP_TO_MW(nt, qp_num);
613 mw = &nt->mw_vec[mw_num];
615 if (!mw->virt_addr)
616 return -ENOMEM;
618 if (mw_num < qp_count % mw_count)
619 num_qps_mw = qp_count / mw_count + 1;
620 else
621 num_qps_mw = qp_count / mw_count;
623 rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
624 qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
625 rx_size -= sizeof(struct ntb_rx_info);
627 qp->remote_rx_info = qp->rx_buff + rx_size;
629 /* Due to housekeeping, there must be atleast 2 buffs */
630 qp->rx_max_frame = min(transport_mtu, rx_size / 2);
631 qp->rx_max_entry = rx_size / qp->rx_max_frame;
632 qp->rx_index = 0;
635 * Checking to see if we have more entries than the default.
636 * We should add additional entries if that is the case so we
637 * can be in sync with the transport frames.
639 node = dev_to_node(&ndev->dev);
640 for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
641 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
642 if (!entry)
643 return -ENOMEM;
645 entry->qp = qp;
646 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
647 &qp->rx_free_q);
648 qp->rx_alloc_entry++;
651 qp->remote_rx_info->entry = qp->rx_max_entry - 1;
653 /* setup the hdr offsets with 0's */
654 for (i = 0; i < qp->rx_max_entry; i++) {
655 void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
656 sizeof(struct ntb_payload_header));
657 memset(offset, 0, sizeof(struct ntb_payload_header));
660 qp->rx_pkts = 0;
661 qp->tx_pkts = 0;
662 qp->tx_index = 0;
664 return 0;
667 static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
669 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
670 struct pci_dev *pdev = nt->ndev->pdev;
672 if (!mw->virt_addr)
673 return;
675 ntb_mw_clear_trans(nt->ndev, num_mw);
676 dma_free_coherent(&pdev->dev, mw->buff_size,
677 mw->virt_addr, mw->dma_addr);
678 mw->xlat_size = 0;
679 mw->buff_size = 0;
680 mw->virt_addr = NULL;
683 static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
684 resource_size_t size)
686 struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
687 struct pci_dev *pdev = nt->ndev->pdev;
688 size_t xlat_size, buff_size;
689 int rc;
691 if (!size)
692 return -EINVAL;
694 xlat_size = round_up(size, mw->xlat_align_size);
695 buff_size = round_up(size, mw->xlat_align);
697 /* No need to re-setup */
698 if (mw->xlat_size == xlat_size)
699 return 0;
701 if (mw->buff_size)
702 ntb_free_mw(nt, num_mw);
704 /* Alloc memory for receiving data. Must be aligned */
705 mw->xlat_size = xlat_size;
706 mw->buff_size = buff_size;
708 mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
709 &mw->dma_addr, GFP_KERNEL);
710 if (!mw->virt_addr) {
711 mw->xlat_size = 0;
712 mw->buff_size = 0;
713 dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
714 buff_size);
715 return -ENOMEM;
719 * we must ensure that the memory address allocated is BAR size
720 * aligned in order for the XLAT register to take the value. This
721 * is a requirement of the hardware. It is recommended to setup CMA
722 * for BAR sizes equal or greater than 4MB.
724 if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
725 dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
726 &mw->dma_addr);
727 ntb_free_mw(nt, num_mw);
728 return -ENOMEM;
731 /* Notify HW the memory location of the receive buffer */
732 rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
733 if (rc) {
734 dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
735 ntb_free_mw(nt, num_mw);
736 return -EIO;
739 return 0;
742 static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
744 qp->link_is_up = false;
745 qp->active = false;
747 qp->tx_index = 0;
748 qp->rx_index = 0;
749 qp->rx_bytes = 0;
750 qp->rx_pkts = 0;
751 qp->rx_ring_empty = 0;
752 qp->rx_err_no_buf = 0;
753 qp->rx_err_oflow = 0;
754 qp->rx_err_ver = 0;
755 qp->rx_memcpy = 0;
756 qp->rx_async = 0;
757 qp->tx_bytes = 0;
758 qp->tx_pkts = 0;
759 qp->tx_ring_full = 0;
760 qp->tx_err_no_buf = 0;
761 qp->tx_memcpy = 0;
762 qp->tx_async = 0;
765 static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
767 struct ntb_transport_ctx *nt = qp->transport;
768 struct pci_dev *pdev = nt->ndev->pdev;
770 dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
772 cancel_delayed_work_sync(&qp->link_work);
773 ntb_qp_link_down_reset(qp);
775 if (qp->event_handler)
776 qp->event_handler(qp->cb_data, qp->link_is_up);
779 static void ntb_qp_link_cleanup_work(struct work_struct *work)
781 struct ntb_transport_qp *qp = container_of(work,
782 struct ntb_transport_qp,
783 link_cleanup);
784 struct ntb_transport_ctx *nt = qp->transport;
786 ntb_qp_link_cleanup(qp);
788 if (nt->link_is_up)
789 schedule_delayed_work(&qp->link_work,
790 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
793 static void ntb_qp_link_down(struct ntb_transport_qp *qp)
795 schedule_work(&qp->link_cleanup);
798 static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
800 struct ntb_transport_qp *qp;
801 u64 qp_bitmap_alloc;
802 int i;
804 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
806 /* Pass along the info to any clients */
807 for (i = 0; i < nt->qp_count; i++)
808 if (qp_bitmap_alloc & BIT_ULL(i)) {
809 qp = &nt->qp_vec[i];
810 ntb_qp_link_cleanup(qp);
811 cancel_work_sync(&qp->link_cleanup);
812 cancel_delayed_work_sync(&qp->link_work);
815 if (!nt->link_is_up)
816 cancel_delayed_work_sync(&nt->link_work);
818 /* The scratchpad registers keep the values if the remote side
819 * goes down, blast them now to give them a sane value the next
820 * time they are accessed
822 for (i = 0; i < MAX_SPAD; i++)
823 ntb_spad_write(nt->ndev, i, 0);
826 static void ntb_transport_link_cleanup_work(struct work_struct *work)
828 struct ntb_transport_ctx *nt =
829 container_of(work, struct ntb_transport_ctx, link_cleanup);
831 ntb_transport_link_cleanup(nt);
834 static void ntb_transport_event_callback(void *data)
836 struct ntb_transport_ctx *nt = data;
838 if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
839 schedule_delayed_work(&nt->link_work, 0);
840 else
841 schedule_work(&nt->link_cleanup);
844 static void ntb_transport_link_work(struct work_struct *work)
846 struct ntb_transport_ctx *nt =
847 container_of(work, struct ntb_transport_ctx, link_work.work);
848 struct ntb_dev *ndev = nt->ndev;
849 struct pci_dev *pdev = ndev->pdev;
850 resource_size_t size;
851 u32 val;
852 int rc = 0, i, spad;
854 /* send the local info, in the opposite order of the way we read it */
855 for (i = 0; i < nt->mw_count; i++) {
856 size = nt->mw_vec[i].phys_size;
858 if (max_mw_size && size > max_mw_size)
859 size = max_mw_size;
861 spad = MW0_SZ_HIGH + (i * 2);
862 ntb_peer_spad_write(ndev, spad, upper_32_bits(size));
864 spad = MW0_SZ_LOW + (i * 2);
865 ntb_peer_spad_write(ndev, spad, lower_32_bits(size));
868 ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
870 ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
872 ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
874 /* Query the remote side for its info */
875 val = ntb_spad_read(ndev, VERSION);
876 dev_dbg(&pdev->dev, "Remote version = %d\n", val);
877 if (val != NTB_TRANSPORT_VERSION)
878 goto out;
880 val = ntb_spad_read(ndev, NUM_QPS);
881 dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
882 if (val != nt->qp_count)
883 goto out;
885 val = ntb_spad_read(ndev, NUM_MWS);
886 dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
887 if (val != nt->mw_count)
888 goto out;
890 for (i = 0; i < nt->mw_count; i++) {
891 u64 val64;
893 val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
894 val64 = (u64)val << 32;
896 val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
897 val64 |= val;
899 dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
901 rc = ntb_set_mw(nt, i, val64);
902 if (rc)
903 goto out1;
906 nt->link_is_up = true;
908 for (i = 0; i < nt->qp_count; i++) {
909 struct ntb_transport_qp *qp = &nt->qp_vec[i];
911 ntb_transport_setup_qp_mw(nt, i);
913 if (qp->client_ready)
914 schedule_delayed_work(&qp->link_work, 0);
917 return;
919 out1:
920 for (i = 0; i < nt->mw_count; i++)
921 ntb_free_mw(nt, i);
923 /* if there's an actual failure, we should just bail */
924 if (rc < 0)
925 return;
927 out:
928 if (ntb_link_is_up(ndev, NULL, NULL) == 1)
929 schedule_delayed_work(&nt->link_work,
930 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
933 static void ntb_qp_link_work(struct work_struct *work)
935 struct ntb_transport_qp *qp = container_of(work,
936 struct ntb_transport_qp,
937 link_work.work);
938 struct pci_dev *pdev = qp->ndev->pdev;
939 struct ntb_transport_ctx *nt = qp->transport;
940 int val;
942 WARN_ON(!nt->link_is_up);
944 val = ntb_spad_read(nt->ndev, QP_LINKS);
946 ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
948 /* query remote spad for qp ready bits */
949 ntb_peer_spad_read(nt->ndev, QP_LINKS);
950 dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
952 /* See if the remote side is up */
953 if (val & BIT(qp->qp_num)) {
954 dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
955 qp->link_is_up = true;
956 qp->active = true;
958 if (qp->event_handler)
959 qp->event_handler(qp->cb_data, qp->link_is_up);
961 if (qp->active)
962 tasklet_schedule(&qp->rxc_db_work);
963 } else if (nt->link_is_up)
964 schedule_delayed_work(&qp->link_work,
965 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
968 static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
969 unsigned int qp_num)
971 struct ntb_transport_qp *qp;
972 phys_addr_t mw_base;
973 resource_size_t mw_size;
974 unsigned int num_qps_mw, tx_size;
975 unsigned int mw_num, mw_count, qp_count;
976 u64 qp_offset;
978 mw_count = nt->mw_count;
979 qp_count = nt->qp_count;
981 mw_num = QP_TO_MW(nt, qp_num);
983 qp = &nt->qp_vec[qp_num];
984 qp->qp_num = qp_num;
985 qp->transport = nt;
986 qp->ndev = nt->ndev;
987 qp->client_ready = false;
988 qp->event_handler = NULL;
989 ntb_qp_link_down_reset(qp);
991 if (mw_num < qp_count % mw_count)
992 num_qps_mw = qp_count / mw_count + 1;
993 else
994 num_qps_mw = qp_count / mw_count;
996 mw_base = nt->mw_vec[mw_num].phys_addr;
997 mw_size = nt->mw_vec[mw_num].phys_size;
999 tx_size = (unsigned int)mw_size / num_qps_mw;
1000 qp_offset = tx_size * (qp_num / mw_count);
1002 qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
1003 if (!qp->tx_mw)
1004 return -EINVAL;
1006 qp->tx_mw_phys = mw_base + qp_offset;
1007 if (!qp->tx_mw_phys)
1008 return -EINVAL;
1010 tx_size -= sizeof(struct ntb_rx_info);
1011 qp->rx_info = qp->tx_mw + tx_size;
1013 /* Due to housekeeping, there must be atleast 2 buffs */
1014 qp->tx_max_frame = min(transport_mtu, tx_size / 2);
1015 qp->tx_max_entry = tx_size / qp->tx_max_frame;
1017 if (nt->debugfs_node_dir) {
1018 char debugfs_name[4];
1020 snprintf(debugfs_name, 4, "qp%d", qp_num);
1021 qp->debugfs_dir = debugfs_create_dir(debugfs_name,
1022 nt->debugfs_node_dir);
1024 qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1025 qp->debugfs_dir, qp,
1026 &ntb_qp_debugfs_stats);
1027 } else {
1028 qp->debugfs_dir = NULL;
1029 qp->debugfs_stats = NULL;
1032 INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
1033 INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
1035 spin_lock_init(&qp->ntb_rx_q_lock);
1036 spin_lock_init(&qp->ntb_tx_free_q_lock);
1038 INIT_LIST_HEAD(&qp->rx_post_q);
1039 INIT_LIST_HEAD(&qp->rx_pend_q);
1040 INIT_LIST_HEAD(&qp->rx_free_q);
1041 INIT_LIST_HEAD(&qp->tx_free_q);
1043 tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1044 (unsigned long)qp);
1046 return 0;
1049 static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
1051 struct ntb_transport_ctx *nt;
1052 struct ntb_transport_mw *mw;
1053 unsigned int mw_count, qp_count;
1054 u64 qp_bitmap;
1055 int node;
1056 int rc, i;
1058 mw_count = ntb_mw_count(ndev);
1059 if (ntb_spad_count(ndev) < (NUM_MWS + 1 + mw_count * 2)) {
1060 dev_err(&ndev->dev, "Not enough scratch pad registers for %s",
1061 NTB_TRANSPORT_NAME);
1062 return -EIO;
1065 if (ntb_db_is_unsafe(ndev))
1066 dev_dbg(&ndev->dev,
1067 "doorbell is unsafe, proceed anyway...\n");
1068 if (ntb_spad_is_unsafe(ndev))
1069 dev_dbg(&ndev->dev,
1070 "scratchpad is unsafe, proceed anyway...\n");
1072 node = dev_to_node(&ndev->dev);
1074 nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
1075 if (!nt)
1076 return -ENOMEM;
1078 nt->ndev = ndev;
1080 nt->mw_count = mw_count;
1082 nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1083 GFP_KERNEL, node);
1084 if (!nt->mw_vec) {
1085 rc = -ENOMEM;
1086 goto err;
1089 for (i = 0; i < mw_count; i++) {
1090 mw = &nt->mw_vec[i];
1092 rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
1093 &mw->xlat_align, &mw->xlat_align_size);
1094 if (rc)
1095 goto err1;
1097 mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
1098 if (!mw->vbase) {
1099 rc = -ENOMEM;
1100 goto err1;
1103 mw->buff_size = 0;
1104 mw->xlat_size = 0;
1105 mw->virt_addr = NULL;
1106 mw->dma_addr = 0;
1109 qp_bitmap = ntb_db_valid_mask(ndev);
1111 qp_count = ilog2(qp_bitmap);
1112 if (max_num_clients && max_num_clients < qp_count)
1113 qp_count = max_num_clients;
1114 else if (nt->mw_count < qp_count)
1115 qp_count = nt->mw_count;
1117 qp_bitmap &= BIT_ULL(qp_count) - 1;
1119 nt->qp_count = qp_count;
1120 nt->qp_bitmap = qp_bitmap;
1121 nt->qp_bitmap_free = qp_bitmap;
1123 nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1124 GFP_KERNEL, node);
1125 if (!nt->qp_vec) {
1126 rc = -ENOMEM;
1127 goto err1;
1130 if (nt_debugfs_dir) {
1131 nt->debugfs_node_dir =
1132 debugfs_create_dir(pci_name(ndev->pdev),
1133 nt_debugfs_dir);
1136 for (i = 0; i < qp_count; i++) {
1137 rc = ntb_transport_init_queue(nt, i);
1138 if (rc)
1139 goto err2;
1142 INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
1143 INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
1145 rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
1146 if (rc)
1147 goto err2;
1149 INIT_LIST_HEAD(&nt->client_devs);
1150 rc = ntb_bus_init(nt);
1151 if (rc)
1152 goto err3;
1154 nt->link_is_up = false;
1155 ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1156 ntb_link_event(ndev);
1158 return 0;
1160 err3:
1161 ntb_clear_ctx(ndev);
1162 err2:
1163 kfree(nt->qp_vec);
1164 err1:
1165 while (i--) {
1166 mw = &nt->mw_vec[i];
1167 iounmap(mw->vbase);
1169 kfree(nt->mw_vec);
1170 err:
1171 kfree(nt);
1172 return rc;
1175 static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
1177 struct ntb_transport_ctx *nt = ndev->ctx;
1178 struct ntb_transport_qp *qp;
1179 u64 qp_bitmap_alloc;
1180 int i;
1182 ntb_transport_link_cleanup(nt);
1183 cancel_work_sync(&nt->link_cleanup);
1184 cancel_delayed_work_sync(&nt->link_work);
1186 qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
1188 /* verify that all the qp's are freed */
1189 for (i = 0; i < nt->qp_count; i++) {
1190 qp = &nt->qp_vec[i];
1191 if (qp_bitmap_alloc & BIT_ULL(i))
1192 ntb_transport_free_queue(qp);
1193 debugfs_remove_recursive(qp->debugfs_dir);
1196 ntb_link_disable(ndev);
1197 ntb_clear_ctx(ndev);
1199 ntb_bus_remove(nt);
1201 for (i = nt->mw_count; i--; ) {
1202 ntb_free_mw(nt, i);
1203 iounmap(nt->mw_vec[i].vbase);
1206 kfree(nt->qp_vec);
1207 kfree(nt->mw_vec);
1208 kfree(nt);
1211 static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1213 struct ntb_queue_entry *entry;
1214 void *cb_data;
1215 unsigned int len;
1216 unsigned long irqflags;
1218 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1220 while (!list_empty(&qp->rx_post_q)) {
1221 entry = list_first_entry(&qp->rx_post_q,
1222 struct ntb_queue_entry, entry);
1223 if (!(entry->flags & DESC_DONE_FLAG))
1224 break;
1226 entry->rx_hdr->flags = 0;
1227 iowrite32(entry->rx_index, &qp->rx_info->entry);
1229 cb_data = entry->cb_data;
1230 len = entry->len;
1232 list_move_tail(&entry->entry, &qp->rx_free_q);
1234 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1236 if (qp->rx_handler && qp->client_ready)
1237 qp->rx_handler(qp, qp->cb_data, cb_data, len);
1239 spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1242 spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1245 static void ntb_rx_copy_callback(void *data,
1246 const struct dmaengine_result *res)
1248 struct ntb_queue_entry *entry = data;
1250 /* we need to check DMA results if we are using DMA */
1251 if (res) {
1252 enum dmaengine_tx_result dma_err = res->result;
1254 switch (dma_err) {
1255 case DMA_TRANS_READ_FAILED:
1256 case DMA_TRANS_WRITE_FAILED:
1257 entry->errors++;
1258 case DMA_TRANS_ABORTED:
1260 struct ntb_transport_qp *qp = entry->qp;
1261 void *offset = qp->rx_buff + qp->rx_max_frame *
1262 qp->rx_index;
1264 ntb_memcpy_rx(entry, offset);
1265 qp->rx_memcpy++;
1266 return;
1269 case DMA_TRANS_NOERROR:
1270 default:
1271 break;
1275 entry->flags |= DESC_DONE_FLAG;
1277 ntb_complete_rxc(entry->qp);
1280 static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1282 void *buf = entry->buf;
1283 size_t len = entry->len;
1285 memcpy(buf, offset, len);
1287 /* Ensure that the data is fully copied out before clearing the flag */
1288 wmb();
1290 ntb_rx_copy_callback(entry, NULL);
1293 static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
1295 struct dma_async_tx_descriptor *txd;
1296 struct ntb_transport_qp *qp = entry->qp;
1297 struct dma_chan *chan = qp->rx_dma_chan;
1298 struct dma_device *device;
1299 size_t pay_off, buff_off, len;
1300 struct dmaengine_unmap_data *unmap;
1301 dma_cookie_t cookie;
1302 void *buf = entry->buf;
1304 len = entry->len;
1305 device = chan->device;
1306 pay_off = (size_t)offset & ~PAGE_MASK;
1307 buff_off = (size_t)buf & ~PAGE_MASK;
1309 if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
1310 goto err;
1312 unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1313 if (!unmap)
1314 goto err;
1316 unmap->len = len;
1317 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1318 pay_off, len, DMA_TO_DEVICE);
1319 if (dma_mapping_error(device->dev, unmap->addr[0]))
1320 goto err_get_unmap;
1322 unmap->to_cnt = 1;
1324 unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1325 buff_off, len, DMA_FROM_DEVICE);
1326 if (dma_mapping_error(device->dev, unmap->addr[1]))
1327 goto err_get_unmap;
1329 unmap->from_cnt = 1;
1331 txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
1332 unmap->addr[0], len,
1333 DMA_PREP_INTERRUPT);
1334 if (!txd)
1335 goto err_get_unmap;
1337 txd->callback_result = ntb_rx_copy_callback;
1338 txd->callback_param = entry;
1339 dma_set_unmap(txd, unmap);
1341 cookie = dmaengine_submit(txd);
1342 if (dma_submit_error(cookie))
1343 goto err_set_unmap;
1345 dmaengine_unmap_put(unmap);
1347 qp->last_cookie = cookie;
1349 qp->rx_async++;
1351 return 0;
1353 err_set_unmap:
1354 dmaengine_unmap_put(unmap);
1355 err_get_unmap:
1356 dmaengine_unmap_put(unmap);
1357 err:
1358 return -ENXIO;
1361 static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
1363 struct ntb_transport_qp *qp = entry->qp;
1364 struct dma_chan *chan = qp->rx_dma_chan;
1365 int res;
1367 if (!chan)
1368 goto err;
1370 if (entry->len < copy_bytes)
1371 goto err;
1373 res = ntb_async_rx_submit(entry, offset);
1374 if (res < 0)
1375 goto err;
1377 if (!entry->retries)
1378 qp->rx_async++;
1380 return;
1382 err:
1383 ntb_memcpy_rx(entry, offset);
1384 qp->rx_memcpy++;
1387 static int ntb_process_rxc(struct ntb_transport_qp *qp)
1389 struct ntb_payload_header *hdr;
1390 struct ntb_queue_entry *entry;
1391 void *offset;
1393 offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1394 hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1396 dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1397 qp->qp_num, hdr->ver, hdr->len, hdr->flags);
1399 if (!(hdr->flags & DESC_DONE_FLAG)) {
1400 dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
1401 qp->rx_ring_empty++;
1402 return -EAGAIN;
1405 if (hdr->flags & LINK_DOWN_FLAG) {
1406 dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1407 ntb_qp_link_down(qp);
1408 hdr->flags = 0;
1409 return -EAGAIN;
1412 if (hdr->ver != (u32)qp->rx_pkts) {
1413 dev_dbg(&qp->ndev->pdev->dev,
1414 "version mismatch, expected %llu - got %u\n",
1415 qp->rx_pkts, hdr->ver);
1416 qp->rx_err_ver++;
1417 return -EIO;
1420 entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
1421 if (!entry) {
1422 dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1423 qp->rx_err_no_buf++;
1424 return -EAGAIN;
1427 entry->rx_hdr = hdr;
1428 entry->rx_index = qp->rx_index;
1430 if (hdr->len > entry->len) {
1431 dev_dbg(&qp->ndev->pdev->dev,
1432 "receive buffer overflow! Wanted %d got %d\n",
1433 hdr->len, entry->len);
1434 qp->rx_err_oflow++;
1436 entry->len = -EIO;
1437 entry->flags |= DESC_DONE_FLAG;
1439 ntb_complete_rxc(qp);
1440 } else {
1441 dev_dbg(&qp->ndev->pdev->dev,
1442 "RX OK index %u ver %u size %d into buf size %d\n",
1443 qp->rx_index, hdr->ver, hdr->len, entry->len);
1445 qp->rx_bytes += hdr->len;
1446 qp->rx_pkts++;
1448 entry->len = hdr->len;
1450 ntb_async_rx(entry, offset);
1453 qp->rx_index++;
1454 qp->rx_index %= qp->rx_max_entry;
1456 return 0;
1459 static void ntb_transport_rxc_db(unsigned long data)
1461 struct ntb_transport_qp *qp = (void *)data;
1462 int rc, i;
1464 dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1465 __func__, qp->qp_num);
1467 /* Limit the number of packets processed in a single interrupt to
1468 * provide fairness to others
1470 for (i = 0; i < qp->rx_max_entry; i++) {
1471 rc = ntb_process_rxc(qp);
1472 if (rc)
1473 break;
1476 if (i && qp->rx_dma_chan)
1477 dma_async_issue_pending(qp->rx_dma_chan);
1479 if (i == qp->rx_max_entry) {
1480 /* there is more work to do */
1481 if (qp->active)
1482 tasklet_schedule(&qp->rxc_db_work);
1483 } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1484 /* the doorbell bit is set: clear it */
1485 ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1486 /* ntb_db_read ensures ntb_db_clear write is committed */
1487 ntb_db_read(qp->ndev);
1489 /* an interrupt may have arrived between finishing
1490 * ntb_process_rxc and clearing the doorbell bit:
1491 * there might be some more work to do.
1493 if (qp->active)
1494 tasklet_schedule(&qp->rxc_db_work);
1498 static void ntb_tx_copy_callback(void *data,
1499 const struct dmaengine_result *res)
1501 struct ntb_queue_entry *entry = data;
1502 struct ntb_transport_qp *qp = entry->qp;
1503 struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
1505 /* we need to check DMA results if we are using DMA */
1506 if (res) {
1507 enum dmaengine_tx_result dma_err = res->result;
1509 switch (dma_err) {
1510 case DMA_TRANS_READ_FAILED:
1511 case DMA_TRANS_WRITE_FAILED:
1512 entry->errors++;
1513 case DMA_TRANS_ABORTED:
1515 void __iomem *offset =
1516 qp->tx_mw + qp->tx_max_frame *
1517 entry->tx_index;
1519 /* resubmit via CPU */
1520 ntb_memcpy_tx(entry, offset);
1521 qp->tx_memcpy++;
1522 return;
1525 case DMA_TRANS_NOERROR:
1526 default:
1527 break;
1531 iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
1533 ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
1535 /* The entry length can only be zero if the packet is intended to be a
1536 * "link down" or similar. Since no payload is being sent in these
1537 * cases, there is nothing to add to the completion queue.
1539 if (entry->len > 0) {
1540 qp->tx_bytes += entry->len;
1542 if (qp->tx_handler)
1543 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1544 entry->len);
1547 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1550 static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1552 #ifdef ARCH_HAS_NOCACHE_UACCESS
1554 * Using non-temporal mov to improve performance on non-cached
1555 * writes, even though we aren't actually copying from user space.
1557 __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1558 #else
1559 memcpy_toio(offset, entry->buf, entry->len);
1560 #endif
1562 /* Ensure that the data is fully copied out before setting the flags */
1563 wmb();
1565 ntb_tx_copy_callback(entry, NULL);
1568 static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1569 struct ntb_queue_entry *entry)
1571 struct dma_async_tx_descriptor *txd;
1572 struct dma_chan *chan = qp->tx_dma_chan;
1573 struct dma_device *device;
1574 size_t len = entry->len;
1575 void *buf = entry->buf;
1576 size_t dest_off, buff_off;
1577 struct dmaengine_unmap_data *unmap;
1578 dma_addr_t dest;
1579 dma_cookie_t cookie;
1581 device = chan->device;
1582 dest = qp->tx_mw_phys + qp->tx_max_frame * entry->tx_index;
1583 buff_off = (size_t)buf & ~PAGE_MASK;
1584 dest_off = (size_t)dest & ~PAGE_MASK;
1586 if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1587 goto err;
1589 unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1590 if (!unmap)
1591 goto err;
1593 unmap->len = len;
1594 unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1595 buff_off, len, DMA_TO_DEVICE);
1596 if (dma_mapping_error(device->dev, unmap->addr[0]))
1597 goto err_get_unmap;
1599 unmap->to_cnt = 1;
1601 txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
1602 DMA_PREP_INTERRUPT);
1603 if (!txd)
1604 goto err_get_unmap;
1606 txd->callback_result = ntb_tx_copy_callback;
1607 txd->callback_param = entry;
1608 dma_set_unmap(txd, unmap);
1610 cookie = dmaengine_submit(txd);
1611 if (dma_submit_error(cookie))
1612 goto err_set_unmap;
1614 dmaengine_unmap_put(unmap);
1616 dma_async_issue_pending(chan);
1618 return 0;
1619 err_set_unmap:
1620 dmaengine_unmap_put(unmap);
1621 err_get_unmap:
1622 dmaengine_unmap_put(unmap);
1623 err:
1624 return -ENXIO;
1627 static void ntb_async_tx(struct ntb_transport_qp *qp,
1628 struct ntb_queue_entry *entry)
1630 struct ntb_payload_header __iomem *hdr;
1631 struct dma_chan *chan = qp->tx_dma_chan;
1632 void __iomem *offset;
1633 int res;
1635 entry->tx_index = qp->tx_index;
1636 offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
1637 hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1638 entry->tx_hdr = hdr;
1640 iowrite32(entry->len, &hdr->len);
1641 iowrite32((u32)qp->tx_pkts, &hdr->ver);
1643 if (!chan)
1644 goto err;
1646 if (entry->len < copy_bytes)
1647 goto err;
1649 res = ntb_async_tx_submit(qp, entry);
1650 if (res < 0)
1651 goto err;
1653 if (!entry->retries)
1654 qp->tx_async++;
1656 return;
1658 err:
1659 ntb_memcpy_tx(entry, offset);
1660 qp->tx_memcpy++;
1663 static int ntb_process_tx(struct ntb_transport_qp *qp,
1664 struct ntb_queue_entry *entry)
1666 if (qp->tx_index == qp->remote_rx_info->entry) {
1667 qp->tx_ring_full++;
1668 return -EAGAIN;
1671 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
1672 if (qp->tx_handler)
1673 qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
1675 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1676 &qp->tx_free_q);
1677 return 0;
1680 ntb_async_tx(qp, entry);
1682 qp->tx_index++;
1683 qp->tx_index %= qp->tx_max_entry;
1685 qp->tx_pkts++;
1687 return 0;
1690 static void ntb_send_link_down(struct ntb_transport_qp *qp)
1692 struct pci_dev *pdev = qp->ndev->pdev;
1693 struct ntb_queue_entry *entry;
1694 int i, rc;
1696 if (!qp->link_is_up)
1697 return;
1699 dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
1701 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1702 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1703 if (entry)
1704 break;
1705 msleep(100);
1708 if (!entry)
1709 return;
1711 entry->cb_data = NULL;
1712 entry->buf = NULL;
1713 entry->len = 0;
1714 entry->flags = LINK_DOWN_FLAG;
1716 rc = ntb_process_tx(qp, entry);
1717 if (rc)
1718 dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1719 qp->qp_num);
1721 ntb_qp_link_down_reset(qp);
1724 static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1726 return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1730 * ntb_transport_create_queue - Create a new NTB transport layer queue
1731 * @rx_handler: receive callback function
1732 * @tx_handler: transmit callback function
1733 * @event_handler: event callback function
1735 * Create a new NTB transport layer queue and provide the queue with a callback
1736 * routine for both transmit and receive. The receive callback routine will be
1737 * used to pass up data when the transport has received it on the queue. The
1738 * transmit callback routine will be called when the transport has completed the
1739 * transmission of the data on the queue and the data is ready to be freed.
1741 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1743 struct ntb_transport_qp *
1744 ntb_transport_create_queue(void *data, struct device *client_dev,
1745 const struct ntb_queue_handlers *handlers)
1747 struct ntb_dev *ndev;
1748 struct pci_dev *pdev;
1749 struct ntb_transport_ctx *nt;
1750 struct ntb_queue_entry *entry;
1751 struct ntb_transport_qp *qp;
1752 u64 qp_bit;
1753 unsigned int free_queue;
1754 dma_cap_mask_t dma_mask;
1755 int node;
1756 int i;
1758 ndev = dev_ntb(client_dev->parent);
1759 pdev = ndev->pdev;
1760 nt = ndev->ctx;
1762 node = dev_to_node(&ndev->dev);
1764 free_queue = ffs(nt->qp_bitmap_free);
1765 if (!free_queue)
1766 goto err;
1768 /* decrement free_queue to make it zero based */
1769 free_queue--;
1771 qp = &nt->qp_vec[free_queue];
1772 qp_bit = BIT_ULL(qp->qp_num);
1774 nt->qp_bitmap_free &= ~qp_bit;
1776 qp->cb_data = data;
1777 qp->rx_handler = handlers->rx_handler;
1778 qp->tx_handler = handlers->tx_handler;
1779 qp->event_handler = handlers->event_handler;
1781 dma_cap_zero(dma_mask);
1782 dma_cap_set(DMA_MEMCPY, dma_mask);
1784 if (use_dma) {
1785 qp->tx_dma_chan =
1786 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1787 (void *)(unsigned long)node);
1788 if (!qp->tx_dma_chan)
1789 dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
1791 qp->rx_dma_chan =
1792 dma_request_channel(dma_mask, ntb_dma_filter_fn,
1793 (void *)(unsigned long)node);
1794 if (!qp->rx_dma_chan)
1795 dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
1796 } else {
1797 qp->tx_dma_chan = NULL;
1798 qp->rx_dma_chan = NULL;
1801 dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
1802 qp->tx_dma_chan ? "DMA" : "CPU");
1804 dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
1805 qp->rx_dma_chan ? "DMA" : "CPU");
1807 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1808 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
1809 if (!entry)
1810 goto err1;
1812 entry->qp = qp;
1813 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
1814 &qp->rx_free_q);
1816 qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
1818 for (i = 0; i < qp->tx_max_entry; i++) {
1819 entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
1820 if (!entry)
1821 goto err2;
1823 entry->qp = qp;
1824 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1825 &qp->tx_free_q);
1828 ntb_db_clear(qp->ndev, qp_bit);
1829 ntb_db_clear_mask(qp->ndev, qp_bit);
1831 dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1833 return qp;
1835 err2:
1836 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1837 kfree(entry);
1838 err1:
1839 qp->rx_alloc_entry = 0;
1840 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
1841 kfree(entry);
1842 if (qp->tx_dma_chan)
1843 dma_release_channel(qp->tx_dma_chan);
1844 if (qp->rx_dma_chan)
1845 dma_release_channel(qp->rx_dma_chan);
1846 nt->qp_bitmap_free |= qp_bit;
1847 err:
1848 return NULL;
1850 EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1853 * ntb_transport_free_queue - Frees NTB transport queue
1854 * @qp: NTB queue to be freed
1856 * Frees NTB transport queue
1858 void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1860 struct pci_dev *pdev;
1861 struct ntb_queue_entry *entry;
1862 u64 qp_bit;
1864 if (!qp)
1865 return;
1867 pdev = qp->ndev->pdev;
1869 qp->active = false;
1871 if (qp->tx_dma_chan) {
1872 struct dma_chan *chan = qp->tx_dma_chan;
1873 /* Putting the dma_chan to NULL will force any new traffic to be
1874 * processed by the CPU instead of the DAM engine
1876 qp->tx_dma_chan = NULL;
1878 /* Try to be nice and wait for any queued DMA engine
1879 * transactions to process before smashing it with a rock
1881 dma_sync_wait(chan, qp->last_cookie);
1882 dmaengine_terminate_all(chan);
1883 dma_release_channel(chan);
1886 if (qp->rx_dma_chan) {
1887 struct dma_chan *chan = qp->rx_dma_chan;
1888 /* Putting the dma_chan to NULL will force any new traffic to be
1889 * processed by the CPU instead of the DAM engine
1891 qp->rx_dma_chan = NULL;
1893 /* Try to be nice and wait for any queued DMA engine
1894 * transactions to process before smashing it with a rock
1896 dma_sync_wait(chan, qp->last_cookie);
1897 dmaengine_terminate_all(chan);
1898 dma_release_channel(chan);
1901 qp_bit = BIT_ULL(qp->qp_num);
1903 ntb_db_set_mask(qp->ndev, qp_bit);
1904 tasklet_kill(&qp->rxc_db_work);
1906 cancel_delayed_work_sync(&qp->link_work);
1908 qp->cb_data = NULL;
1909 qp->rx_handler = NULL;
1910 qp->tx_handler = NULL;
1911 qp->event_handler = NULL;
1913 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
1914 kfree(entry);
1916 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1917 dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1918 kfree(entry);
1921 while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1922 dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
1923 kfree(entry);
1926 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1927 kfree(entry);
1929 qp->transport->qp_bitmap_free |= qp_bit;
1931 dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1933 EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1936 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1937 * @qp: NTB queue to be freed
1938 * @len: pointer to variable to write enqueued buffers length
1940 * Dequeues unused buffers from receive queue. Should only be used during
1941 * shutdown of qp.
1943 * RETURNS: NULL error value on error, or void* for success.
1945 void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1947 struct ntb_queue_entry *entry;
1948 void *buf;
1950 if (!qp || qp->client_ready)
1951 return NULL;
1953 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
1954 if (!entry)
1955 return NULL;
1957 buf = entry->cb_data;
1958 *len = entry->len;
1960 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
1962 return buf;
1964 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1967 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1968 * @qp: NTB transport layer queue the entry is to be enqueued on
1969 * @cb: per buffer pointer for callback function to use
1970 * @data: pointer to data buffer that incoming packets will be copied into
1971 * @len: length of the data buffer
1973 * Enqueue a new receive buffer onto the transport queue into which a NTB
1974 * payload can be received into.
1976 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1978 int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1979 unsigned int len)
1981 struct ntb_queue_entry *entry;
1983 if (!qp)
1984 return -EINVAL;
1986 entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
1987 if (!entry)
1988 return -ENOMEM;
1990 entry->cb_data = cb;
1991 entry->buf = data;
1992 entry->len = len;
1993 entry->flags = 0;
1994 entry->retries = 0;
1995 entry->errors = 0;
1996 entry->rx_index = 0;
1998 ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
2000 if (qp->active)
2001 tasklet_schedule(&qp->rxc_db_work);
2003 return 0;
2005 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2008 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2009 * @qp: NTB transport layer queue the entry is to be enqueued on
2010 * @cb: per buffer pointer for callback function to use
2011 * @data: pointer to data buffer that will be sent
2012 * @len: length of the data buffer
2014 * Enqueue a new transmit buffer onto the transport queue from which a NTB
2015 * payload will be transmitted. This assumes that a lock is being held to
2016 * serialize access to the qp.
2018 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2020 int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2021 unsigned int len)
2023 struct ntb_queue_entry *entry;
2024 int rc;
2026 if (!qp || !qp->link_is_up || !len)
2027 return -EINVAL;
2029 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
2030 if (!entry) {
2031 qp->tx_err_no_buf++;
2032 return -EBUSY;
2035 entry->cb_data = cb;
2036 entry->buf = data;
2037 entry->len = len;
2038 entry->flags = 0;
2039 entry->errors = 0;
2040 entry->retries = 0;
2041 entry->tx_index = 0;
2043 rc = ntb_process_tx(qp, entry);
2044 if (rc)
2045 ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2046 &qp->tx_free_q);
2048 return rc;
2050 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2053 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2054 * @qp: NTB transport layer queue to be enabled
2056 * Notify NTB transport layer of client readiness to use queue
2058 void ntb_transport_link_up(struct ntb_transport_qp *qp)
2060 if (!qp)
2061 return;
2063 qp->client_ready = true;
2065 if (qp->transport->link_is_up)
2066 schedule_delayed_work(&qp->link_work, 0);
2068 EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2071 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2072 * @qp: NTB transport layer queue to be disabled
2074 * Notify NTB transport layer of client's desire to no longer receive data on
2075 * transport queue specified. It is the client's responsibility to ensure all
2076 * entries on queue are purged or otherwise handled appropriately.
2078 void ntb_transport_link_down(struct ntb_transport_qp *qp)
2080 int val;
2082 if (!qp)
2083 return;
2085 qp->client_ready = false;
2087 val = ntb_spad_read(qp->ndev, QP_LINKS);
2089 ntb_peer_spad_write(qp->ndev, QP_LINKS,
2090 val & ~BIT(qp->qp_num));
2092 if (qp->link_is_up)
2093 ntb_send_link_down(qp);
2094 else
2095 cancel_delayed_work_sync(&qp->link_work);
2097 EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2100 * ntb_transport_link_query - Query transport link state
2101 * @qp: NTB transport layer queue to be queried
2103 * Query connectivity to the remote system of the NTB transport queue
2105 * RETURNS: true for link up or false for link down
2107 bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2109 if (!qp)
2110 return false;
2112 return qp->link_is_up;
2114 EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2117 * ntb_transport_qp_num - Query the qp number
2118 * @qp: NTB transport layer queue to be queried
2120 * Query qp number of the NTB transport queue
2122 * RETURNS: a zero based number specifying the qp number
2124 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2126 if (!qp)
2127 return 0;
2129 return qp->qp_num;
2131 EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2134 * ntb_transport_max_size - Query the max payload size of a qp
2135 * @qp: NTB transport layer queue to be queried
2137 * Query the maximum payload size permissible on the given qp
2139 * RETURNS: the max payload size of a qp
2141 unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
2143 unsigned int max_size;
2144 unsigned int copy_align;
2145 struct dma_chan *rx_chan, *tx_chan;
2147 if (!qp)
2148 return 0;
2150 rx_chan = qp->rx_dma_chan;
2151 tx_chan = qp->tx_dma_chan;
2153 copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
2154 tx_chan ? tx_chan->device->copy_align : 0);
2156 /* If DMA engine usage is possible, try to find the max size for that */
2157 max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2158 max_size = round_down(max_size, 1 << copy_align);
2160 return max_size;
2162 EXPORT_SYMBOL_GPL(ntb_transport_max_size);
2164 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2166 unsigned int head = qp->tx_index;
2167 unsigned int tail = qp->remote_rx_info->entry;
2169 return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2171 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2173 static void ntb_transport_doorbell_callback(void *data, int vector)
2175 struct ntb_transport_ctx *nt = data;
2176 struct ntb_transport_qp *qp;
2177 u64 db_bits;
2178 unsigned int qp_num;
2180 db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2181 ntb_db_vector_mask(nt->ndev, vector));
2183 while (db_bits) {
2184 qp_num = __ffs(db_bits);
2185 qp = &nt->qp_vec[qp_num];
2187 if (qp->active)
2188 tasklet_schedule(&qp->rxc_db_work);
2190 db_bits &= ~BIT_ULL(qp_num);
2194 static const struct ntb_ctx_ops ntb_transport_ops = {
2195 .link_event = ntb_transport_event_callback,
2196 .db_event = ntb_transport_doorbell_callback,
2199 static struct ntb_client ntb_transport_client = {
2200 .ops = {
2201 .probe = ntb_transport_probe,
2202 .remove = ntb_transport_free,
2206 static int __init ntb_transport_init(void)
2208 int rc;
2210 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2212 if (debugfs_initialized())
2213 nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2215 rc = bus_register(&ntb_transport_bus);
2216 if (rc)
2217 goto err_bus;
2219 rc = ntb_register_client(&ntb_transport_client);
2220 if (rc)
2221 goto err_client;
2223 return 0;
2225 err_client:
2226 bus_unregister(&ntb_transport_bus);
2227 err_bus:
2228 debugfs_remove_recursive(nt_debugfs_dir);
2229 return rc;
2231 module_init(ntb_transport_init);
2233 static void __exit ntb_transport_exit(void)
2235 ntb_unregister_client(&ntb_transport_client);
2236 bus_unregister(&ntb_transport_bus);
2237 debugfs_remove_recursive(nt_debugfs_dir);
2239 module_exit(ntb_transport_exit);