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.
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.
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
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
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");
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 */
106 struct ntb_transport_qp
*qp
;
108 struct ntb_payload_header __iomem
*tx_hdr
;
109 struct ntb_payload_header
*rx_hdr
;
118 struct ntb_transport_qp
{
119 struct ntb_transport_ctx
*transport
;
120 struct ntb_dev
*ndev
;
122 struct dma_chan
*tx_dma_chan
;
123 struct dma_chan
*rx_dma_chan
;
129 u8 qp_num
; /* Only 64 QP's are allowed. 0-63 */
132 struct ntb_rx_info __iomem
*rx_info
;
133 struct ntb_rx_info
*remote_rx_info
;
135 void (*tx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
136 void *data
, int len
);
137 struct list_head tx_free_q
;
138 spinlock_t ntb_tx_free_q_lock
;
140 dma_addr_t tx_mw_phys
;
141 unsigned int tx_index
;
142 unsigned int tx_max_entry
;
143 unsigned int tx_max_frame
;
145 void (*rx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
146 void *data
, int len
);
147 struct list_head rx_post_q
;
148 struct list_head rx_pend_q
;
149 struct list_head rx_free_q
;
150 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
151 spinlock_t ntb_rx_q_lock
;
153 unsigned int rx_index
;
154 unsigned int rx_max_entry
;
155 unsigned int rx_max_frame
;
156 unsigned int rx_alloc_entry
;
157 dma_cookie_t last_cookie
;
158 struct tasklet_struct rxc_db_work
;
160 void (*event_handler
)(void *data
, int status
);
161 struct delayed_work link_work
;
162 struct work_struct link_cleanup
;
164 struct dentry
*debugfs_dir
;
165 struct dentry
*debugfs_stats
;
186 struct ntb_transport_mw
{
187 phys_addr_t phys_addr
;
188 resource_size_t phys_size
;
189 resource_size_t xlat_align
;
190 resource_size_t xlat_align_size
;
198 struct ntb_transport_client_dev
{
199 struct list_head entry
;
200 struct ntb_transport_ctx
*nt
;
204 struct ntb_transport_ctx
{
205 struct list_head entry
;
206 struct list_head client_devs
;
208 struct ntb_dev
*ndev
;
210 struct ntb_transport_mw
*mw_vec
;
211 struct ntb_transport_qp
*qp_vec
;
212 unsigned int mw_count
;
213 unsigned int qp_count
;
218 struct delayed_work link_work
;
219 struct work_struct link_cleanup
;
221 struct dentry
*debugfs_node_dir
;
225 DESC_DONE_FLAG
= BIT(0),
226 LINK_DOWN_FLAG
= BIT(1),
229 struct ntb_payload_header
{
247 #define dev_client_dev(__dev) \
248 container_of((__dev), struct ntb_transport_client_dev, dev)
250 #define drv_client(__drv) \
251 container_of((__drv), struct ntb_transport_client, driver)
253 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
254 #define NTB_QP_DEF_NUM_ENTRIES 100
255 #define NTB_LINK_DOWN_TIMEOUT 10
256 #define DMA_RETRIES 20
257 #define DMA_OUT_RESOURCE_TO 50
259 static void ntb_transport_rxc_db(unsigned long data
);
260 static const struct ntb_ctx_ops ntb_transport_ops
;
261 static struct ntb_client ntb_transport_client
;
263 static int ntb_transport_bus_match(struct device
*dev
,
264 struct device_driver
*drv
)
266 return !strncmp(dev_name(dev
), drv
->name
, strlen(drv
->name
));
269 static int ntb_transport_bus_probe(struct device
*dev
)
271 const struct ntb_transport_client
*client
;
276 client
= drv_client(dev
->driver
);
277 rc
= client
->probe(dev
);
284 static int ntb_transport_bus_remove(struct device
*dev
)
286 const struct ntb_transport_client
*client
;
288 client
= drv_client(dev
->driver
);
296 static struct bus_type ntb_transport_bus
= {
297 .name
= "ntb_transport",
298 .match
= ntb_transport_bus_match
,
299 .probe
= ntb_transport_bus_probe
,
300 .remove
= ntb_transport_bus_remove
,
303 static LIST_HEAD(ntb_transport_list
);
305 static int ntb_bus_init(struct ntb_transport_ctx
*nt
)
307 list_add_tail(&nt
->entry
, &ntb_transport_list
);
311 static void ntb_bus_remove(struct ntb_transport_ctx
*nt
)
313 struct ntb_transport_client_dev
*client_dev
, *cd
;
315 list_for_each_entry_safe(client_dev
, cd
, &nt
->client_devs
, entry
) {
316 dev_err(client_dev
->dev
.parent
, "%s still attached to bus, removing\n",
317 dev_name(&client_dev
->dev
));
318 list_del(&client_dev
->entry
);
319 device_unregister(&client_dev
->dev
);
322 list_del(&nt
->entry
);
325 static void ntb_transport_client_release(struct device
*dev
)
327 struct ntb_transport_client_dev
*client_dev
;
329 client_dev
= dev_client_dev(dev
);
334 * ntb_transport_unregister_client_dev - Unregister NTB client device
335 * @device_name: Name of NTB client device
337 * Unregister an NTB client device with the NTB transport layer
339 void ntb_transport_unregister_client_dev(char *device_name
)
341 struct ntb_transport_client_dev
*client
, *cd
;
342 struct ntb_transport_ctx
*nt
;
344 list_for_each_entry(nt
, &ntb_transport_list
, entry
)
345 list_for_each_entry_safe(client
, cd
, &nt
->client_devs
, entry
)
346 if (!strncmp(dev_name(&client
->dev
), device_name
,
347 strlen(device_name
))) {
348 list_del(&client
->entry
);
349 device_unregister(&client
->dev
);
352 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev
);
355 * ntb_transport_register_client_dev - Register NTB client device
356 * @device_name: Name of NTB client device
358 * Register an NTB client device with the NTB transport layer
360 int ntb_transport_register_client_dev(char *device_name
)
362 struct ntb_transport_client_dev
*client_dev
;
363 struct ntb_transport_ctx
*nt
;
367 if (list_empty(&ntb_transport_list
))
370 list_for_each_entry(nt
, &ntb_transport_list
, entry
) {
373 node
= dev_to_node(&nt
->ndev
->dev
);
375 client_dev
= kzalloc_node(sizeof(*client_dev
),
382 dev
= &client_dev
->dev
;
384 /* setup and register client devices */
385 dev_set_name(dev
, "%s%d", device_name
, i
);
386 dev
->bus
= &ntb_transport_bus
;
387 dev
->release
= ntb_transport_client_release
;
388 dev
->parent
= &nt
->ndev
->dev
;
390 rc
= device_register(dev
);
396 list_add_tail(&client_dev
->entry
, &nt
->client_devs
);
403 ntb_transport_unregister_client_dev(device_name
);
407 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev
);
410 * ntb_transport_register_client - Register NTB client driver
411 * @drv: NTB client driver to be registered
413 * Register an NTB client driver with the NTB transport layer
415 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
417 int ntb_transport_register_client(struct ntb_transport_client
*drv
)
419 drv
->driver
.bus
= &ntb_transport_bus
;
421 if (list_empty(&ntb_transport_list
))
424 return driver_register(&drv
->driver
);
426 EXPORT_SYMBOL_GPL(ntb_transport_register_client
);
429 * ntb_transport_unregister_client - Unregister NTB client driver
430 * @drv: NTB client driver to be unregistered
432 * Unregister an NTB client driver with the NTB transport layer
434 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
436 void ntb_transport_unregister_client(struct ntb_transport_client
*drv
)
438 driver_unregister(&drv
->driver
);
440 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client
);
442 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
445 struct ntb_transport_qp
*qp
;
447 ssize_t ret
, out_offset
, out_count
;
449 qp
= filp
->private_data
;
451 if (!qp
|| !qp
->link_is_up
)
456 buf
= kmalloc(out_count
, GFP_KERNEL
);
461 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
462 "\nNTB QP stats:\n\n");
463 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
464 "rx_bytes - \t%llu\n", qp
->rx_bytes
);
465 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
466 "rx_pkts - \t%llu\n", qp
->rx_pkts
);
467 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
468 "rx_memcpy - \t%llu\n", qp
->rx_memcpy
);
469 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
470 "rx_async - \t%llu\n", qp
->rx_async
);
471 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
472 "rx_ring_empty - %llu\n", qp
->rx_ring_empty
);
473 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
474 "rx_err_no_buf - %llu\n", qp
->rx_err_no_buf
);
475 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
476 "rx_err_oflow - \t%llu\n", qp
->rx_err_oflow
);
477 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
478 "rx_err_ver - \t%llu\n", qp
->rx_err_ver
);
479 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
480 "rx_buff - \t0x%p\n", qp
->rx_buff
);
481 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
482 "rx_index - \t%u\n", qp
->rx_index
);
483 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
484 "rx_max_entry - \t%u\n", qp
->rx_max_entry
);
485 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
486 "rx_alloc_entry - \t%u\n\n", qp
->rx_alloc_entry
);
488 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
489 "tx_bytes - \t%llu\n", qp
->tx_bytes
);
490 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
491 "tx_pkts - \t%llu\n", qp
->tx_pkts
);
492 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
493 "tx_memcpy - \t%llu\n", qp
->tx_memcpy
);
494 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
495 "tx_async - \t%llu\n", qp
->tx_async
);
496 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
497 "tx_ring_full - \t%llu\n", qp
->tx_ring_full
);
498 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
499 "tx_err_no_buf - %llu\n", qp
->tx_err_no_buf
);
500 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
501 "tx_mw - \t0x%p\n", qp
->tx_mw
);
502 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
503 "tx_index (H) - \t%u\n", qp
->tx_index
);
504 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
506 qp
->remote_rx_info
->entry
);
507 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
508 "tx_max_entry - \t%u\n", qp
->tx_max_entry
);
509 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
511 ntb_transport_tx_free_entry(qp
));
512 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
513 "DMA tx prep err - \t%llu\n",
514 qp
->dma_tx_prep_err
);
515 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
516 "DMA rx prep err - \t%llu\n",
517 qp
->dma_rx_prep_err
);
519 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
521 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
522 "Using TX DMA - \t%s\n",
523 qp
->tx_dma_chan
? "Yes" : "No");
524 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
525 "Using RX DMA - \t%s\n",
526 qp
->rx_dma_chan
? "Yes" : "No");
527 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
529 qp
->link_is_up
? "Up" : "Down");
530 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
533 if (out_offset
> out_count
)
534 out_offset
= out_count
;
536 ret
= simple_read_from_buffer(ubuf
, count
, offp
, buf
, out_offset
);
541 static const struct file_operations ntb_qp_debugfs_stats
= {
542 .owner
= THIS_MODULE
,
544 .read
= debugfs_read
,
547 static void ntb_list_add(spinlock_t
*lock
, struct list_head
*entry
,
548 struct list_head
*list
)
552 spin_lock_irqsave(lock
, flags
);
553 list_add_tail(entry
, list
);
554 spin_unlock_irqrestore(lock
, flags
);
557 static struct ntb_queue_entry
*ntb_list_rm(spinlock_t
*lock
,
558 struct list_head
*list
)
560 struct ntb_queue_entry
*entry
;
563 spin_lock_irqsave(lock
, flags
);
564 if (list_empty(list
)) {
568 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
569 list_del(&entry
->entry
);
572 spin_unlock_irqrestore(lock
, flags
);
577 static struct ntb_queue_entry
*ntb_list_mv(spinlock_t
*lock
,
578 struct list_head
*list
,
579 struct list_head
*to_list
)
581 struct ntb_queue_entry
*entry
;
584 spin_lock_irqsave(lock
, flags
);
586 if (list_empty(list
)) {
589 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
590 list_move_tail(&entry
->entry
, to_list
);
593 spin_unlock_irqrestore(lock
, flags
);
598 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx
*nt
,
601 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[qp_num
];
602 struct ntb_transport_mw
*mw
;
603 struct ntb_dev
*ndev
= nt
->ndev
;
604 struct ntb_queue_entry
*entry
;
605 unsigned int rx_size
, num_qps_mw
;
606 unsigned int mw_num
, mw_count
, qp_count
;
610 mw_count
= nt
->mw_count
;
611 qp_count
= nt
->qp_count
;
613 mw_num
= QP_TO_MW(nt
, qp_num
);
614 mw
= &nt
->mw_vec
[mw_num
];
619 if (qp_count
% mw_count
&& mw_num
+ 1 < qp_count
/ mw_count
)
620 num_qps_mw
= qp_count
/ mw_count
+ 1;
622 num_qps_mw
= qp_count
/ mw_count
;
624 rx_size
= (unsigned int)mw
->xlat_size
/ num_qps_mw
;
625 qp
->rx_buff
= mw
->virt_addr
+ rx_size
* (qp_num
/ mw_count
);
626 rx_size
-= sizeof(struct ntb_rx_info
);
628 qp
->remote_rx_info
= qp
->rx_buff
+ rx_size
;
630 /* Due to housekeeping, there must be atleast 2 buffs */
631 qp
->rx_max_frame
= min(transport_mtu
, rx_size
/ 2);
632 qp
->rx_max_entry
= rx_size
/ qp
->rx_max_frame
;
636 * Checking to see if we have more entries than the default.
637 * We should add additional entries if that is the case so we
638 * can be in sync with the transport frames.
640 node
= dev_to_node(&ndev
->dev
);
641 for (i
= qp
->rx_alloc_entry
; i
< qp
->rx_max_entry
; i
++) {
642 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
647 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
649 qp
->rx_alloc_entry
++;
652 qp
->remote_rx_info
->entry
= qp
->rx_max_entry
- 1;
654 /* setup the hdr offsets with 0's */
655 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
656 void *offset
= (qp
->rx_buff
+ qp
->rx_max_frame
* (i
+ 1) -
657 sizeof(struct ntb_payload_header
));
658 memset(offset
, 0, sizeof(struct ntb_payload_header
));
668 static void ntb_free_mw(struct ntb_transport_ctx
*nt
, int num_mw
)
670 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
671 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
676 ntb_mw_clear_trans(nt
->ndev
, num_mw
);
677 dma_free_coherent(&pdev
->dev
, mw
->buff_size
,
678 mw
->virt_addr
, mw
->dma_addr
);
681 mw
->virt_addr
= NULL
;
684 static int ntb_set_mw(struct ntb_transport_ctx
*nt
, int num_mw
,
685 resource_size_t size
)
687 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
688 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
689 size_t xlat_size
, buff_size
;
695 xlat_size
= round_up(size
, mw
->xlat_align_size
);
696 buff_size
= round_up(size
, mw
->xlat_align
);
698 /* No need to re-setup */
699 if (mw
->xlat_size
== xlat_size
)
703 ntb_free_mw(nt
, num_mw
);
705 /* Alloc memory for receiving data. Must be aligned */
706 mw
->xlat_size
= xlat_size
;
707 mw
->buff_size
= buff_size
;
709 mw
->virt_addr
= dma_alloc_coherent(&pdev
->dev
, buff_size
,
710 &mw
->dma_addr
, GFP_KERNEL
);
711 if (!mw
->virt_addr
) {
714 dev_err(&pdev
->dev
, "Unable to alloc MW buff of size %zu\n",
720 * we must ensure that the memory address allocated is BAR size
721 * aligned in order for the XLAT register to take the value. This
722 * is a requirement of the hardware. It is recommended to setup CMA
723 * for BAR sizes equal or greater than 4MB.
725 if (!IS_ALIGNED(mw
->dma_addr
, mw
->xlat_align
)) {
726 dev_err(&pdev
->dev
, "DMA memory %pad is not aligned\n",
728 ntb_free_mw(nt
, num_mw
);
732 /* Notify HW the memory location of the receive buffer */
733 rc
= ntb_mw_set_trans(nt
->ndev
, num_mw
, mw
->dma_addr
, mw
->xlat_size
);
735 dev_err(&pdev
->dev
, "Unable to set mw%d translation", num_mw
);
736 ntb_free_mw(nt
, num_mw
);
743 static void ntb_qp_link_down_reset(struct ntb_transport_qp
*qp
)
745 qp
->link_is_up
= false;
752 qp
->rx_ring_empty
= 0;
753 qp
->rx_err_no_buf
= 0;
754 qp
->rx_err_oflow
= 0;
760 qp
->tx_ring_full
= 0;
761 qp
->tx_err_no_buf
= 0;
764 qp
->dma_tx_prep_err
= 0;
765 qp
->dma_rx_prep_err
= 0;
768 static void ntb_qp_link_cleanup(struct ntb_transport_qp
*qp
)
770 struct ntb_transport_ctx
*nt
= qp
->transport
;
771 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
773 dev_info(&pdev
->dev
, "qp %d: Link Cleanup\n", qp
->qp_num
);
775 cancel_delayed_work_sync(&qp
->link_work
);
776 ntb_qp_link_down_reset(qp
);
778 if (qp
->event_handler
)
779 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
782 static void ntb_qp_link_cleanup_work(struct work_struct
*work
)
784 struct ntb_transport_qp
*qp
= container_of(work
,
785 struct ntb_transport_qp
,
787 struct ntb_transport_ctx
*nt
= qp
->transport
;
789 ntb_qp_link_cleanup(qp
);
792 schedule_delayed_work(&qp
->link_work
,
793 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
796 static void ntb_qp_link_down(struct ntb_transport_qp
*qp
)
798 schedule_work(&qp
->link_cleanup
);
801 static void ntb_transport_link_cleanup(struct ntb_transport_ctx
*nt
)
803 struct ntb_transport_qp
*qp
;
807 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
809 /* Pass along the info to any clients */
810 for (i
= 0; i
< nt
->qp_count
; i
++)
811 if (qp_bitmap_alloc
& BIT_ULL(i
)) {
813 ntb_qp_link_cleanup(qp
);
814 cancel_work_sync(&qp
->link_cleanup
);
815 cancel_delayed_work_sync(&qp
->link_work
);
819 cancel_delayed_work_sync(&nt
->link_work
);
821 /* The scratchpad registers keep the values if the remote side
822 * goes down, blast them now to give them a sane value the next
823 * time they are accessed
825 for (i
= 0; i
< MAX_SPAD
; i
++)
826 ntb_spad_write(nt
->ndev
, i
, 0);
829 static void ntb_transport_link_cleanup_work(struct work_struct
*work
)
831 struct ntb_transport_ctx
*nt
=
832 container_of(work
, struct ntb_transport_ctx
, link_cleanup
);
834 ntb_transport_link_cleanup(nt
);
837 static void ntb_transport_event_callback(void *data
)
839 struct ntb_transport_ctx
*nt
= data
;
841 if (ntb_link_is_up(nt
->ndev
, NULL
, NULL
) == 1)
842 schedule_delayed_work(&nt
->link_work
, 0);
844 schedule_work(&nt
->link_cleanup
);
847 static void ntb_transport_link_work(struct work_struct
*work
)
849 struct ntb_transport_ctx
*nt
=
850 container_of(work
, struct ntb_transport_ctx
, link_work
.work
);
851 struct ntb_dev
*ndev
= nt
->ndev
;
852 struct pci_dev
*pdev
= ndev
->pdev
;
853 resource_size_t size
;
857 /* send the local info, in the opposite order of the way we read it */
858 for (i
= 0; i
< nt
->mw_count
; i
++) {
859 size
= nt
->mw_vec
[i
].phys_size
;
861 if (max_mw_size
&& size
> max_mw_size
)
864 spad
= MW0_SZ_HIGH
+ (i
* 2);
865 ntb_peer_spad_write(ndev
, spad
, upper_32_bits(size
));
867 spad
= MW0_SZ_LOW
+ (i
* 2);
868 ntb_peer_spad_write(ndev
, spad
, lower_32_bits(size
));
871 ntb_peer_spad_write(ndev
, NUM_MWS
, nt
->mw_count
);
873 ntb_peer_spad_write(ndev
, NUM_QPS
, nt
->qp_count
);
875 ntb_peer_spad_write(ndev
, VERSION
, NTB_TRANSPORT_VERSION
);
877 /* Query the remote side for its info */
878 val
= ntb_spad_read(ndev
, VERSION
);
879 dev_dbg(&pdev
->dev
, "Remote version = %d\n", val
);
880 if (val
!= NTB_TRANSPORT_VERSION
)
883 val
= ntb_spad_read(ndev
, NUM_QPS
);
884 dev_dbg(&pdev
->dev
, "Remote max number of qps = %d\n", val
);
885 if (val
!= nt
->qp_count
)
888 val
= ntb_spad_read(ndev
, NUM_MWS
);
889 dev_dbg(&pdev
->dev
, "Remote number of mws = %d\n", val
);
890 if (val
!= nt
->mw_count
)
893 for (i
= 0; i
< nt
->mw_count
; i
++) {
896 val
= ntb_spad_read(ndev
, MW0_SZ_HIGH
+ (i
* 2));
897 val64
= (u64
)val
<< 32;
899 val
= ntb_spad_read(ndev
, MW0_SZ_LOW
+ (i
* 2));
902 dev_dbg(&pdev
->dev
, "Remote MW%d size = %#llx\n", i
, val64
);
904 rc
= ntb_set_mw(nt
, i
, val64
);
909 nt
->link_is_up
= true;
911 for (i
= 0; i
< nt
->qp_count
; i
++) {
912 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[i
];
914 ntb_transport_setup_qp_mw(nt
, i
);
916 if (qp
->client_ready
)
917 schedule_delayed_work(&qp
->link_work
, 0);
923 for (i
= 0; i
< nt
->mw_count
; i
++)
926 /* if there's an actual failure, we should just bail */
928 ntb_link_disable(ndev
);
933 if (ntb_link_is_up(ndev
, NULL
, NULL
) == 1)
934 schedule_delayed_work(&nt
->link_work
,
935 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
938 static void ntb_qp_link_work(struct work_struct
*work
)
940 struct ntb_transport_qp
*qp
= container_of(work
,
941 struct ntb_transport_qp
,
943 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
944 struct ntb_transport_ctx
*nt
= qp
->transport
;
947 WARN_ON(!nt
->link_is_up
);
949 val
= ntb_spad_read(nt
->ndev
, QP_LINKS
);
951 ntb_peer_spad_write(nt
->ndev
, QP_LINKS
, val
| BIT(qp
->qp_num
));
953 /* query remote spad for qp ready bits */
954 ntb_peer_spad_read(nt
->ndev
, QP_LINKS
);
955 dev_dbg_ratelimited(&pdev
->dev
, "Remote QP link status = %x\n", val
);
957 /* See if the remote side is up */
958 if (val
& BIT(qp
->qp_num
)) {
959 dev_info(&pdev
->dev
, "qp %d: Link Up\n", qp
->qp_num
);
960 qp
->link_is_up
= true;
963 if (qp
->event_handler
)
964 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
967 tasklet_schedule(&qp
->rxc_db_work
);
968 } else if (nt
->link_is_up
)
969 schedule_delayed_work(&qp
->link_work
,
970 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
973 static int ntb_transport_init_queue(struct ntb_transport_ctx
*nt
,
976 struct ntb_transport_qp
*qp
;
978 resource_size_t mw_size
;
979 unsigned int num_qps_mw
, tx_size
;
980 unsigned int mw_num
, mw_count
, qp_count
;
983 mw_count
= nt
->mw_count
;
984 qp_count
= nt
->qp_count
;
986 mw_num
= QP_TO_MW(nt
, qp_num
);
988 qp
= &nt
->qp_vec
[qp_num
];
992 qp
->client_ready
= false;
993 qp
->event_handler
= NULL
;
994 ntb_qp_link_down_reset(qp
);
996 if (qp_count
% mw_count
&& mw_num
+ 1 < qp_count
/ mw_count
)
997 num_qps_mw
= qp_count
/ mw_count
+ 1;
999 num_qps_mw
= qp_count
/ mw_count
;
1001 mw_base
= nt
->mw_vec
[mw_num
].phys_addr
;
1002 mw_size
= nt
->mw_vec
[mw_num
].phys_size
;
1004 tx_size
= (unsigned int)mw_size
/ num_qps_mw
;
1005 qp_offset
= tx_size
* (qp_num
/ mw_count
);
1007 qp
->tx_mw
= nt
->mw_vec
[mw_num
].vbase
+ qp_offset
;
1011 qp
->tx_mw_phys
= mw_base
+ qp_offset
;
1012 if (!qp
->tx_mw_phys
)
1015 tx_size
-= sizeof(struct ntb_rx_info
);
1016 qp
->rx_info
= qp
->tx_mw
+ tx_size
;
1018 /* Due to housekeeping, there must be atleast 2 buffs */
1019 qp
->tx_max_frame
= min(transport_mtu
, tx_size
/ 2);
1020 qp
->tx_max_entry
= tx_size
/ qp
->tx_max_frame
;
1022 if (nt
->debugfs_node_dir
) {
1023 char debugfs_name
[4];
1025 snprintf(debugfs_name
, 4, "qp%d", qp_num
);
1026 qp
->debugfs_dir
= debugfs_create_dir(debugfs_name
,
1027 nt
->debugfs_node_dir
);
1029 qp
->debugfs_stats
= debugfs_create_file("stats", S_IRUSR
,
1030 qp
->debugfs_dir
, qp
,
1031 &ntb_qp_debugfs_stats
);
1033 qp
->debugfs_dir
= NULL
;
1034 qp
->debugfs_stats
= NULL
;
1037 INIT_DELAYED_WORK(&qp
->link_work
, ntb_qp_link_work
);
1038 INIT_WORK(&qp
->link_cleanup
, ntb_qp_link_cleanup_work
);
1040 spin_lock_init(&qp
->ntb_rx_q_lock
);
1041 spin_lock_init(&qp
->ntb_tx_free_q_lock
);
1043 INIT_LIST_HEAD(&qp
->rx_post_q
);
1044 INIT_LIST_HEAD(&qp
->rx_pend_q
);
1045 INIT_LIST_HEAD(&qp
->rx_free_q
);
1046 INIT_LIST_HEAD(&qp
->tx_free_q
);
1048 tasklet_init(&qp
->rxc_db_work
, ntb_transport_rxc_db
,
1054 static int ntb_transport_probe(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1056 struct ntb_transport_ctx
*nt
;
1057 struct ntb_transport_mw
*mw
;
1058 unsigned int mw_count
, qp_count
;
1063 mw_count
= ntb_mw_count(ndev
);
1064 if (ntb_spad_count(ndev
) < (NUM_MWS
+ 1 + mw_count
* 2)) {
1065 dev_err(&ndev
->dev
, "Not enough scratch pad registers for %s",
1066 NTB_TRANSPORT_NAME
);
1070 if (ntb_db_is_unsafe(ndev
))
1072 "doorbell is unsafe, proceed anyway...\n");
1073 if (ntb_spad_is_unsafe(ndev
))
1075 "scratchpad is unsafe, proceed anyway...\n");
1077 node
= dev_to_node(&ndev
->dev
);
1079 nt
= kzalloc_node(sizeof(*nt
), GFP_KERNEL
, node
);
1085 nt
->mw_count
= mw_count
;
1087 nt
->mw_vec
= kzalloc_node(mw_count
* sizeof(*nt
->mw_vec
),
1094 for (i
= 0; i
< mw_count
; i
++) {
1095 mw
= &nt
->mw_vec
[i
];
1097 rc
= ntb_mw_get_range(ndev
, i
, &mw
->phys_addr
, &mw
->phys_size
,
1098 &mw
->xlat_align
, &mw
->xlat_align_size
);
1102 mw
->vbase
= ioremap_wc(mw
->phys_addr
, mw
->phys_size
);
1110 mw
->virt_addr
= NULL
;
1114 qp_bitmap
= ntb_db_valid_mask(ndev
);
1116 qp_count
= ilog2(qp_bitmap
);
1117 if (max_num_clients
&& max_num_clients
< qp_count
)
1118 qp_count
= max_num_clients
;
1119 else if (mw_count
< qp_count
)
1120 qp_count
= mw_count
;
1122 qp_bitmap
&= BIT_ULL(qp_count
) - 1;
1124 nt
->qp_count
= qp_count
;
1125 nt
->qp_bitmap
= qp_bitmap
;
1126 nt
->qp_bitmap_free
= qp_bitmap
;
1128 nt
->qp_vec
= kzalloc_node(qp_count
* sizeof(*nt
->qp_vec
),
1135 if (nt_debugfs_dir
) {
1136 nt
->debugfs_node_dir
=
1137 debugfs_create_dir(pci_name(ndev
->pdev
),
1141 for (i
= 0; i
< qp_count
; i
++) {
1142 rc
= ntb_transport_init_queue(nt
, i
);
1147 INIT_DELAYED_WORK(&nt
->link_work
, ntb_transport_link_work
);
1148 INIT_WORK(&nt
->link_cleanup
, ntb_transport_link_cleanup_work
);
1150 rc
= ntb_set_ctx(ndev
, nt
, &ntb_transport_ops
);
1154 INIT_LIST_HEAD(&nt
->client_devs
);
1155 rc
= ntb_bus_init(nt
);
1159 nt
->link_is_up
= false;
1160 ntb_link_enable(ndev
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
1161 ntb_link_event(ndev
);
1166 ntb_clear_ctx(ndev
);
1171 mw
= &nt
->mw_vec
[i
];
1180 static void ntb_transport_free(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1182 struct ntb_transport_ctx
*nt
= ndev
->ctx
;
1183 struct ntb_transport_qp
*qp
;
1184 u64 qp_bitmap_alloc
;
1187 ntb_transport_link_cleanup(nt
);
1188 cancel_work_sync(&nt
->link_cleanup
);
1189 cancel_delayed_work_sync(&nt
->link_work
);
1191 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
1193 /* verify that all the qp's are freed */
1194 for (i
= 0; i
< nt
->qp_count
; i
++) {
1195 qp
= &nt
->qp_vec
[i
];
1196 if (qp_bitmap_alloc
& BIT_ULL(i
))
1197 ntb_transport_free_queue(qp
);
1198 debugfs_remove_recursive(qp
->debugfs_dir
);
1201 ntb_link_disable(ndev
);
1202 ntb_clear_ctx(ndev
);
1206 for (i
= nt
->mw_count
; i
--; ) {
1208 iounmap(nt
->mw_vec
[i
].vbase
);
1216 static void ntb_complete_rxc(struct ntb_transport_qp
*qp
)
1218 struct ntb_queue_entry
*entry
;
1221 unsigned long irqflags
;
1223 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1225 while (!list_empty(&qp
->rx_post_q
)) {
1226 entry
= list_first_entry(&qp
->rx_post_q
,
1227 struct ntb_queue_entry
, entry
);
1228 if (!(entry
->flags
& DESC_DONE_FLAG
))
1231 entry
->rx_hdr
->flags
= 0;
1232 iowrite32(entry
->index
, &qp
->rx_info
->entry
);
1234 cb_data
= entry
->cb_data
;
1237 list_move_tail(&entry
->entry
, &qp
->rx_free_q
);
1239 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1241 if (qp
->rx_handler
&& qp
->client_ready
)
1242 qp
->rx_handler(qp
, qp
->cb_data
, cb_data
, len
);
1244 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1247 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1250 static void ntb_rx_copy_callback(void *data
)
1252 struct ntb_queue_entry
*entry
= data
;
1254 entry
->flags
|= DESC_DONE_FLAG
;
1256 ntb_complete_rxc(entry
->qp
);
1259 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
)
1261 void *buf
= entry
->buf
;
1262 size_t len
= entry
->len
;
1264 memcpy(buf
, offset
, len
);
1266 /* Ensure that the data is fully copied out before clearing the flag */
1269 ntb_rx_copy_callback(entry
);
1272 static void ntb_async_rx(struct ntb_queue_entry
*entry
, void *offset
)
1274 struct dma_async_tx_descriptor
*txd
;
1275 struct ntb_transport_qp
*qp
= entry
->qp
;
1276 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1277 struct dma_device
*device
;
1278 size_t pay_off
, buff_off
, len
;
1279 struct dmaengine_unmap_data
*unmap
;
1280 dma_cookie_t cookie
;
1281 void *buf
= entry
->buf
;
1289 if (len
< copy_bytes
)
1292 device
= chan
->device
;
1293 pay_off
= (size_t)offset
& ~PAGE_MASK
;
1294 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1296 if (!is_dma_copy_aligned(device
, pay_off
, buff_off
, len
))
1299 unmap
= dmaengine_get_unmap_data(device
->dev
, 2, GFP_NOWAIT
);
1304 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(offset
),
1305 pay_off
, len
, DMA_TO_DEVICE
);
1306 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1311 unmap
->addr
[1] = dma_map_page(device
->dev
, virt_to_page(buf
),
1312 buff_off
, len
, DMA_FROM_DEVICE
);
1313 if (dma_mapping_error(device
->dev
, unmap
->addr
[1]))
1316 unmap
->from_cnt
= 1;
1318 for (retries
= 0; retries
< DMA_RETRIES
; retries
++) {
1319 txd
= device
->device_prep_dma_memcpy(chan
, unmap
->addr
[1],
1320 unmap
->addr
[0], len
,
1321 DMA_PREP_INTERRUPT
);
1325 set_current_state(TASK_INTERRUPTIBLE
);
1326 schedule_timeout(DMA_OUT_RESOURCE_TO
);
1330 qp
->dma_rx_prep_err
++;
1334 txd
->callback
= ntb_rx_copy_callback
;
1335 txd
->callback_param
= entry
;
1336 dma_set_unmap(txd
, unmap
);
1338 cookie
= dmaengine_submit(txd
);
1339 if (dma_submit_error(cookie
))
1342 dmaengine_unmap_put(unmap
);
1344 qp
->last_cookie
= cookie
;
1351 dmaengine_unmap_put(unmap
);
1353 dmaengine_unmap_put(unmap
);
1355 ntb_memcpy_rx(entry
, offset
);
1359 static int ntb_process_rxc(struct ntb_transport_qp
*qp
)
1361 struct ntb_payload_header
*hdr
;
1362 struct ntb_queue_entry
*entry
;
1365 offset
= qp
->rx_buff
+ qp
->rx_max_frame
* qp
->rx_index
;
1366 hdr
= offset
+ qp
->rx_max_frame
- sizeof(struct ntb_payload_header
);
1368 dev_dbg(&qp
->ndev
->pdev
->dev
, "qp %d: RX ver %u len %d flags %x\n",
1369 qp
->qp_num
, hdr
->ver
, hdr
->len
, hdr
->flags
);
1371 if (!(hdr
->flags
& DESC_DONE_FLAG
)) {
1372 dev_dbg(&qp
->ndev
->pdev
->dev
, "done flag not set\n");
1373 qp
->rx_ring_empty
++;
1377 if (hdr
->flags
& LINK_DOWN_FLAG
) {
1378 dev_dbg(&qp
->ndev
->pdev
->dev
, "link down flag set\n");
1379 ntb_qp_link_down(qp
);
1384 if (hdr
->ver
!= (u32
)qp
->rx_pkts
) {
1385 dev_dbg(&qp
->ndev
->pdev
->dev
,
1386 "version mismatch, expected %llu - got %u\n",
1387 qp
->rx_pkts
, hdr
->ver
);
1392 entry
= ntb_list_mv(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
, &qp
->rx_post_q
);
1394 dev_dbg(&qp
->ndev
->pdev
->dev
, "no receive buffer\n");
1395 qp
->rx_err_no_buf
++;
1399 entry
->rx_hdr
= hdr
;
1400 entry
->index
= qp
->rx_index
;
1402 if (hdr
->len
> entry
->len
) {
1403 dev_dbg(&qp
->ndev
->pdev
->dev
,
1404 "receive buffer overflow! Wanted %d got %d\n",
1405 hdr
->len
, entry
->len
);
1409 entry
->flags
|= DESC_DONE_FLAG
;
1411 ntb_complete_rxc(qp
);
1413 dev_dbg(&qp
->ndev
->pdev
->dev
,
1414 "RX OK index %u ver %u size %d into buf size %d\n",
1415 qp
->rx_index
, hdr
->ver
, hdr
->len
, entry
->len
);
1417 qp
->rx_bytes
+= hdr
->len
;
1420 entry
->len
= hdr
->len
;
1422 ntb_async_rx(entry
, offset
);
1426 qp
->rx_index
%= qp
->rx_max_entry
;
1431 static void ntb_transport_rxc_db(unsigned long data
)
1433 struct ntb_transport_qp
*qp
= (void *)data
;
1436 dev_dbg(&qp
->ndev
->pdev
->dev
, "%s: doorbell %d received\n",
1437 __func__
, qp
->qp_num
);
1439 /* Limit the number of packets processed in a single interrupt to
1440 * provide fairness to others
1442 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
1443 rc
= ntb_process_rxc(qp
);
1448 if (i
&& qp
->rx_dma_chan
)
1449 dma_async_issue_pending(qp
->rx_dma_chan
);
1451 if (i
== qp
->rx_max_entry
) {
1452 /* there is more work to do */
1454 tasklet_schedule(&qp
->rxc_db_work
);
1455 } else if (ntb_db_read(qp
->ndev
) & BIT_ULL(qp
->qp_num
)) {
1456 /* the doorbell bit is set: clear it */
1457 ntb_db_clear(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1458 /* ntb_db_read ensures ntb_db_clear write is committed */
1459 ntb_db_read(qp
->ndev
);
1461 /* an interrupt may have arrived between finishing
1462 * ntb_process_rxc and clearing the doorbell bit:
1463 * there might be some more work to do.
1466 tasklet_schedule(&qp
->rxc_db_work
);
1470 static void ntb_tx_copy_callback(void *data
)
1472 struct ntb_queue_entry
*entry
= data
;
1473 struct ntb_transport_qp
*qp
= entry
->qp
;
1474 struct ntb_payload_header __iomem
*hdr
= entry
->tx_hdr
;
1476 iowrite32(entry
->flags
| DESC_DONE_FLAG
, &hdr
->flags
);
1478 ntb_peer_db_set(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1480 /* The entry length can only be zero if the packet is intended to be a
1481 * "link down" or similar. Since no payload is being sent in these
1482 * cases, there is nothing to add to the completion queue.
1484 if (entry
->len
> 0) {
1485 qp
->tx_bytes
+= entry
->len
;
1488 qp
->tx_handler(qp
, qp
->cb_data
, entry
->cb_data
,
1492 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
, &qp
->tx_free_q
);
1495 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
)
1497 #ifdef ARCH_HAS_NOCACHE_UACCESS
1499 * Using non-temporal mov to improve performance on non-cached
1500 * writes, even though we aren't actually copying from user space.
1502 __copy_from_user_inatomic_nocache(offset
, entry
->buf
, entry
->len
);
1504 memcpy_toio(offset
, entry
->buf
, entry
->len
);
1507 /* Ensure that the data is fully copied out before setting the flags */
1510 ntb_tx_copy_callback(entry
);
1513 static void ntb_async_tx(struct ntb_transport_qp
*qp
,
1514 struct ntb_queue_entry
*entry
)
1516 struct ntb_payload_header __iomem
*hdr
;
1517 struct dma_async_tx_descriptor
*txd
;
1518 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1519 struct dma_device
*device
;
1520 size_t dest_off
, buff_off
;
1521 struct dmaengine_unmap_data
*unmap
;
1523 dma_cookie_t cookie
;
1524 void __iomem
*offset
;
1525 size_t len
= entry
->len
;
1526 void *buf
= entry
->buf
;
1529 offset
= qp
->tx_mw
+ qp
->tx_max_frame
* qp
->tx_index
;
1530 hdr
= offset
+ qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1531 entry
->tx_hdr
= hdr
;
1533 iowrite32(entry
->len
, &hdr
->len
);
1534 iowrite32((u32
)qp
->tx_pkts
, &hdr
->ver
);
1539 if (len
< copy_bytes
)
1542 device
= chan
->device
;
1543 dest
= qp
->tx_mw_phys
+ qp
->tx_max_frame
* qp
->tx_index
;
1544 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1545 dest_off
= (size_t)dest
& ~PAGE_MASK
;
1547 if (!is_dma_copy_aligned(device
, buff_off
, dest_off
, len
))
1550 unmap
= dmaengine_get_unmap_data(device
->dev
, 1, GFP_NOWAIT
);
1555 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(buf
),
1556 buff_off
, len
, DMA_TO_DEVICE
);
1557 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1562 for (retries
= 0; retries
< DMA_RETRIES
; retries
++) {
1563 txd
= device
->device_prep_dma_memcpy(chan
, dest
, unmap
->addr
[0],
1564 len
, DMA_PREP_INTERRUPT
);
1568 set_current_state(TASK_INTERRUPTIBLE
);
1569 schedule_timeout(DMA_OUT_RESOURCE_TO
);
1573 qp
->dma_tx_prep_err
++;
1577 txd
->callback
= ntb_tx_copy_callback
;
1578 txd
->callback_param
= entry
;
1579 dma_set_unmap(txd
, unmap
);
1581 cookie
= dmaengine_submit(txd
);
1582 if (dma_submit_error(cookie
))
1585 dmaengine_unmap_put(unmap
);
1587 dma_async_issue_pending(chan
);
1592 dmaengine_unmap_put(unmap
);
1594 dmaengine_unmap_put(unmap
);
1596 ntb_memcpy_tx(entry
, offset
);
1600 static int ntb_process_tx(struct ntb_transport_qp
*qp
,
1601 struct ntb_queue_entry
*entry
)
1603 if (qp
->tx_index
== qp
->remote_rx_info
->entry
) {
1608 if (entry
->len
> qp
->tx_max_frame
- sizeof(struct ntb_payload_header
)) {
1610 qp
->tx_handler(qp
, qp
->cb_data
, NULL
, -EIO
);
1612 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1617 ntb_async_tx(qp
, entry
);
1620 qp
->tx_index
%= qp
->tx_max_entry
;
1627 static void ntb_send_link_down(struct ntb_transport_qp
*qp
)
1629 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
1630 struct ntb_queue_entry
*entry
;
1633 if (!qp
->link_is_up
)
1636 dev_info(&pdev
->dev
, "qp %d: Send Link Down\n", qp
->qp_num
);
1638 for (i
= 0; i
< NTB_LINK_DOWN_TIMEOUT
; i
++) {
1639 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1648 entry
->cb_data
= NULL
;
1651 entry
->flags
= LINK_DOWN_FLAG
;
1653 rc
= ntb_process_tx(qp
, entry
);
1655 dev_err(&pdev
->dev
, "ntb: QP%d unable to send linkdown msg\n",
1658 ntb_qp_link_down_reset(qp
);
1661 static bool ntb_dma_filter_fn(struct dma_chan
*chan
, void *node
)
1663 return dev_to_node(&chan
->dev
->device
) == (int)(unsigned long)node
;
1667 * ntb_transport_create_queue - Create a new NTB transport layer queue
1668 * @rx_handler: receive callback function
1669 * @tx_handler: transmit callback function
1670 * @event_handler: event callback function
1672 * Create a new NTB transport layer queue and provide the queue with a callback
1673 * routine for both transmit and receive. The receive callback routine will be
1674 * used to pass up data when the transport has received it on the queue. The
1675 * transmit callback routine will be called when the transport has completed the
1676 * transmission of the data on the queue and the data is ready to be freed.
1678 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1680 struct ntb_transport_qp
*
1681 ntb_transport_create_queue(void *data
, struct device
*client_dev
,
1682 const struct ntb_queue_handlers
*handlers
)
1684 struct ntb_dev
*ndev
;
1685 struct pci_dev
*pdev
;
1686 struct ntb_transport_ctx
*nt
;
1687 struct ntb_queue_entry
*entry
;
1688 struct ntb_transport_qp
*qp
;
1690 unsigned int free_queue
;
1691 dma_cap_mask_t dma_mask
;
1695 ndev
= dev_ntb(client_dev
->parent
);
1699 node
= dev_to_node(&ndev
->dev
);
1701 free_queue
= ffs(nt
->qp_bitmap
);
1705 /* decrement free_queue to make it zero based */
1708 qp
= &nt
->qp_vec
[free_queue
];
1709 qp_bit
= BIT_ULL(qp
->qp_num
);
1711 nt
->qp_bitmap_free
&= ~qp_bit
;
1714 qp
->rx_handler
= handlers
->rx_handler
;
1715 qp
->tx_handler
= handlers
->tx_handler
;
1716 qp
->event_handler
= handlers
->event_handler
;
1718 dma_cap_zero(dma_mask
);
1719 dma_cap_set(DMA_MEMCPY
, dma_mask
);
1723 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1724 (void *)(unsigned long)node
);
1725 if (!qp
->tx_dma_chan
)
1726 dev_info(&pdev
->dev
, "Unable to allocate TX DMA channel\n");
1729 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1730 (void *)(unsigned long)node
);
1731 if (!qp
->rx_dma_chan
)
1732 dev_info(&pdev
->dev
, "Unable to allocate RX DMA channel\n");
1734 qp
->tx_dma_chan
= NULL
;
1735 qp
->rx_dma_chan
= NULL
;
1738 dev_dbg(&pdev
->dev
, "Using %s memcpy for TX\n",
1739 qp
->tx_dma_chan
? "DMA" : "CPU");
1741 dev_dbg(&pdev
->dev
, "Using %s memcpy for RX\n",
1742 qp
->rx_dma_chan
? "DMA" : "CPU");
1744 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1745 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1750 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
1753 qp
->rx_alloc_entry
= NTB_QP_DEF_NUM_ENTRIES
;
1755 for (i
= 0; i
< qp
->tx_max_entry
; i
++) {
1756 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1761 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1765 ntb_db_clear(qp
->ndev
, qp_bit
);
1766 ntb_db_clear_mask(qp
->ndev
, qp_bit
);
1768 dev_info(&pdev
->dev
, "NTB Transport QP %d created\n", qp
->qp_num
);
1773 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1776 qp
->rx_alloc_entry
= 0;
1777 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1779 if (qp
->tx_dma_chan
)
1780 dma_release_channel(qp
->tx_dma_chan
);
1781 if (qp
->rx_dma_chan
)
1782 dma_release_channel(qp
->rx_dma_chan
);
1783 nt
->qp_bitmap_free
|= qp_bit
;
1787 EXPORT_SYMBOL_GPL(ntb_transport_create_queue
);
1790 * ntb_transport_free_queue - Frees NTB transport queue
1791 * @qp: NTB queue to be freed
1793 * Frees NTB transport queue
1795 void ntb_transport_free_queue(struct ntb_transport_qp
*qp
)
1797 struct pci_dev
*pdev
;
1798 struct ntb_queue_entry
*entry
;
1804 pdev
= qp
->ndev
->pdev
;
1808 if (qp
->tx_dma_chan
) {
1809 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1810 /* Putting the dma_chan to NULL will force any new traffic to be
1811 * processed by the CPU instead of the DAM engine
1813 qp
->tx_dma_chan
= NULL
;
1815 /* Try to be nice and wait for any queued DMA engine
1816 * transactions to process before smashing it with a rock
1818 dma_sync_wait(chan
, qp
->last_cookie
);
1819 dmaengine_terminate_all(chan
);
1820 dma_release_channel(chan
);
1823 if (qp
->rx_dma_chan
) {
1824 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1825 /* Putting the dma_chan to NULL will force any new traffic to be
1826 * processed by the CPU instead of the DAM engine
1828 qp
->rx_dma_chan
= NULL
;
1830 /* Try to be nice and wait for any queued DMA engine
1831 * transactions to process before smashing it with a rock
1833 dma_sync_wait(chan
, qp
->last_cookie
);
1834 dmaengine_terminate_all(chan
);
1835 dma_release_channel(chan
);
1838 qp_bit
= BIT_ULL(qp
->qp_num
);
1840 ntb_db_set_mask(qp
->ndev
, qp_bit
);
1841 tasklet_kill(&qp
->rxc_db_work
);
1843 cancel_delayed_work_sync(&qp
->link_work
);
1846 qp
->rx_handler
= NULL
;
1847 qp
->tx_handler
= NULL
;
1848 qp
->event_handler
= NULL
;
1850 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1853 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
))) {
1854 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_pend_q\n");
1858 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_post_q
))) {
1859 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_post_q\n");
1863 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1866 qp
->transport
->qp_bitmap_free
|= qp_bit
;
1868 dev_info(&pdev
->dev
, "NTB Transport QP %d freed\n", qp
->qp_num
);
1870 EXPORT_SYMBOL_GPL(ntb_transport_free_queue
);
1873 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1874 * @qp: NTB queue to be freed
1875 * @len: pointer to variable to write enqueued buffers length
1877 * Dequeues unused buffers from receive queue. Should only be used during
1880 * RETURNS: NULL error value on error, or void* for success.
1882 void *ntb_transport_rx_remove(struct ntb_transport_qp
*qp
, unsigned int *len
)
1884 struct ntb_queue_entry
*entry
;
1887 if (!qp
|| qp
->client_ready
)
1890 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
);
1894 buf
= entry
->cb_data
;
1897 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
1901 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove
);
1904 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1905 * @qp: NTB transport layer queue the entry is to be enqueued on
1906 * @cb: per buffer pointer for callback function to use
1907 * @data: pointer to data buffer that incoming packets will be copied into
1908 * @len: length of the data buffer
1910 * Enqueue a new receive buffer onto the transport queue into which a NTB
1911 * payload can be received into.
1913 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1915 int ntb_transport_rx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1918 struct ntb_queue_entry
*entry
;
1923 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
);
1927 entry
->cb_data
= cb
;
1932 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
1935 tasklet_schedule(&qp
->rxc_db_work
);
1939 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue
);
1942 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1943 * @qp: NTB transport layer queue the entry is to be enqueued on
1944 * @cb: per buffer pointer for callback function to use
1945 * @data: pointer to data buffer that will be sent
1946 * @len: length of the data buffer
1948 * Enqueue a new transmit buffer onto the transport queue from which a NTB
1949 * payload will be transmitted. This assumes that a lock is being held to
1950 * serialize access to the qp.
1952 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1954 int ntb_transport_tx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1957 struct ntb_queue_entry
*entry
;
1960 if (!qp
|| !qp
->link_is_up
|| !len
)
1963 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1965 qp
->tx_err_no_buf
++;
1969 entry
->cb_data
= cb
;
1974 rc
= ntb_process_tx(qp
, entry
);
1976 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1981 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue
);
1984 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1985 * @qp: NTB transport layer queue to be enabled
1987 * Notify NTB transport layer of client readiness to use queue
1989 void ntb_transport_link_up(struct ntb_transport_qp
*qp
)
1994 qp
->client_ready
= true;
1996 if (qp
->transport
->link_is_up
)
1997 schedule_delayed_work(&qp
->link_work
, 0);
1999 EXPORT_SYMBOL_GPL(ntb_transport_link_up
);
2002 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2003 * @qp: NTB transport layer queue to be disabled
2005 * Notify NTB transport layer of client's desire to no longer receive data on
2006 * transport queue specified. It is the client's responsibility to ensure all
2007 * entries on queue are purged or otherwise handled appropriately.
2009 void ntb_transport_link_down(struct ntb_transport_qp
*qp
)
2016 qp
->client_ready
= false;
2018 val
= ntb_spad_read(qp
->ndev
, QP_LINKS
);
2020 ntb_peer_spad_write(qp
->ndev
, QP_LINKS
,
2021 val
& ~BIT(qp
->qp_num
));
2024 ntb_send_link_down(qp
);
2026 cancel_delayed_work_sync(&qp
->link_work
);
2028 EXPORT_SYMBOL_GPL(ntb_transport_link_down
);
2031 * ntb_transport_link_query - Query transport link state
2032 * @qp: NTB transport layer queue to be queried
2034 * Query connectivity to the remote system of the NTB transport queue
2036 * RETURNS: true for link up or false for link down
2038 bool ntb_transport_link_query(struct ntb_transport_qp
*qp
)
2043 return qp
->link_is_up
;
2045 EXPORT_SYMBOL_GPL(ntb_transport_link_query
);
2048 * ntb_transport_qp_num - Query the qp number
2049 * @qp: NTB transport layer queue to be queried
2051 * Query qp number of the NTB transport queue
2053 * RETURNS: a zero based number specifying the qp number
2055 unsigned char ntb_transport_qp_num(struct ntb_transport_qp
*qp
)
2062 EXPORT_SYMBOL_GPL(ntb_transport_qp_num
);
2065 * ntb_transport_max_size - Query the max payload size of a qp
2066 * @qp: NTB transport layer queue to be queried
2068 * Query the maximum payload size permissible on the given qp
2070 * RETURNS: the max payload size of a qp
2072 unsigned int ntb_transport_max_size(struct ntb_transport_qp
*qp
)
2074 unsigned int max_size
;
2075 unsigned int copy_align
;
2076 struct dma_chan
*rx_chan
, *tx_chan
;
2081 rx_chan
= qp
->rx_dma_chan
;
2082 tx_chan
= qp
->tx_dma_chan
;
2084 copy_align
= max(rx_chan
? rx_chan
->device
->copy_align
: 0,
2085 tx_chan
? tx_chan
->device
->copy_align
: 0);
2087 /* If DMA engine usage is possible, try to find the max size for that */
2088 max_size
= qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
2089 max_size
= round_down(max_size
, 1 << copy_align
);
2093 EXPORT_SYMBOL_GPL(ntb_transport_max_size
);
2095 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp
*qp
)
2097 unsigned int head
= qp
->tx_index
;
2098 unsigned int tail
= qp
->remote_rx_info
->entry
;
2100 return tail
> head
? tail
- head
: qp
->tx_max_entry
+ tail
- head
;
2102 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry
);
2104 static void ntb_transport_doorbell_callback(void *data
, int vector
)
2106 struct ntb_transport_ctx
*nt
= data
;
2107 struct ntb_transport_qp
*qp
;
2109 unsigned int qp_num
;
2111 db_bits
= (nt
->qp_bitmap
& ~nt
->qp_bitmap_free
&
2112 ntb_db_vector_mask(nt
->ndev
, vector
));
2115 qp_num
= __ffs(db_bits
);
2116 qp
= &nt
->qp_vec
[qp_num
];
2119 tasklet_schedule(&qp
->rxc_db_work
);
2121 db_bits
&= ~BIT_ULL(qp_num
);
2125 static const struct ntb_ctx_ops ntb_transport_ops
= {
2126 .link_event
= ntb_transport_event_callback
,
2127 .db_event
= ntb_transport_doorbell_callback
,
2130 static struct ntb_client ntb_transport_client
= {
2132 .probe
= ntb_transport_probe
,
2133 .remove
= ntb_transport_free
,
2137 static int __init
ntb_transport_init(void)
2141 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC
, NTB_TRANSPORT_VER
);
2143 if (debugfs_initialized())
2144 nt_debugfs_dir
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
2146 rc
= bus_register(&ntb_transport_bus
);
2150 rc
= ntb_register_client(&ntb_transport_client
);
2157 bus_unregister(&ntb_transport_bus
);
2159 debugfs_remove_recursive(nt_debugfs_dir
);
2162 module_init(ntb_transport_init
);
2164 static void __exit
ntb_transport_exit(void)
2166 debugfs_remove_recursive(nt_debugfs_dir
);
2168 ntb_unregister_client(&ntb_transport_client
);
2169 bus_unregister(&ntb_transport_bus
);
2171 module_exit(ntb_transport_exit
);