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"
69 #define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
71 MODULE_DESCRIPTION(NTB_TRANSPORT_DESC
);
72 MODULE_VERSION(NTB_TRANSPORT_VER
);
73 MODULE_LICENSE("Dual BSD/GPL");
74 MODULE_AUTHOR("Intel Corporation");
76 static unsigned long max_mw_size
;
77 module_param(max_mw_size
, ulong
, 0644);
78 MODULE_PARM_DESC(max_mw_size
, "Limit size of large memory windows");
80 static unsigned int transport_mtu
= 0x10000;
81 module_param(transport_mtu
, uint
, 0644);
82 MODULE_PARM_DESC(transport_mtu
, "Maximum size of NTB transport packets");
84 static unsigned char max_num_clients
;
85 module_param(max_num_clients
, byte
, 0644);
86 MODULE_PARM_DESC(max_num_clients
, "Maximum number of NTB transport clients");
88 static unsigned int copy_bytes
= 1024;
89 module_param(copy_bytes
, uint
, 0644);
90 MODULE_PARM_DESC(copy_bytes
, "Threshold under which NTB will use the CPU to copy instead of DMA");
93 module_param(use_dma
, bool, 0644);
94 MODULE_PARM_DESC(use_dma
, "Use DMA engine to perform large data copy");
96 static struct dentry
*nt_debugfs_dir
;
98 /* Only two-ports NTB devices are supported */
99 #define PIDX NTB_DEF_PEER_IDX
101 struct ntb_queue_entry
{
102 /* ntb_queue list reference */
103 struct list_head entry
;
104 /* pointers to data to be transferred */
111 unsigned int tx_index
;
112 unsigned int rx_index
;
114 struct ntb_transport_qp
*qp
;
116 struct ntb_payload_header __iomem
*tx_hdr
;
117 struct ntb_payload_header
*rx_hdr
;
125 struct ntb_transport_qp
{
126 struct ntb_transport_ctx
*transport
;
127 struct ntb_dev
*ndev
;
129 struct dma_chan
*tx_dma_chan
;
130 struct dma_chan
*rx_dma_chan
;
136 u8 qp_num
; /* Only 64 QP's are allowed. 0-63 */
139 struct ntb_rx_info __iomem
*rx_info
;
140 struct ntb_rx_info
*remote_rx_info
;
142 void (*tx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
143 void *data
, int len
);
144 struct list_head tx_free_q
;
145 spinlock_t ntb_tx_free_q_lock
;
147 dma_addr_t tx_mw_phys
;
148 unsigned int tx_index
;
149 unsigned int tx_max_entry
;
150 unsigned int tx_max_frame
;
152 void (*rx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
153 void *data
, int len
);
154 struct list_head rx_post_q
;
155 struct list_head rx_pend_q
;
156 struct list_head rx_free_q
;
157 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
158 spinlock_t ntb_rx_q_lock
;
160 unsigned int rx_index
;
161 unsigned int rx_max_entry
;
162 unsigned int rx_max_frame
;
163 unsigned int rx_alloc_entry
;
164 dma_cookie_t last_cookie
;
165 struct tasklet_struct rxc_db_work
;
167 void (*event_handler
)(void *data
, int status
);
168 struct delayed_work link_work
;
169 struct work_struct link_cleanup
;
171 struct dentry
*debugfs_dir
;
172 struct dentry
*debugfs_stats
;
191 struct ntb_transport_mw
{
192 phys_addr_t phys_addr
;
193 resource_size_t phys_size
;
203 struct ntb_transport_client_dev
{
204 struct list_head entry
;
205 struct ntb_transport_ctx
*nt
;
209 struct ntb_transport_ctx
{
210 struct list_head entry
;
211 struct list_head client_devs
;
213 struct ntb_dev
*ndev
;
215 struct ntb_transport_mw
*mw_vec
;
216 struct ntb_transport_qp
*qp_vec
;
217 unsigned int mw_count
;
218 unsigned int qp_count
;
223 struct delayed_work link_work
;
224 struct work_struct link_cleanup
;
226 struct dentry
*debugfs_node_dir
;
230 DESC_DONE_FLAG
= BIT(0),
231 LINK_DOWN_FLAG
= BIT(1),
234 struct ntb_payload_header
{
249 #define dev_client_dev(__dev) \
250 container_of((__dev), struct ntb_transport_client_dev, dev)
252 #define drv_client(__drv) \
253 container_of((__drv), struct ntb_transport_client, driver)
255 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
256 #define NTB_QP_DEF_NUM_ENTRIES 100
257 #define NTB_LINK_DOWN_TIMEOUT 10
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
;
262 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
263 struct ntb_queue_entry
*entry
);
264 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
);
265 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
);
266 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
);
269 static int ntb_transport_bus_match(struct device
*dev
,
270 struct device_driver
*drv
)
272 return !strncmp(dev_name(dev
), drv
->name
, strlen(drv
->name
));
275 static int ntb_transport_bus_probe(struct device
*dev
)
277 const struct ntb_transport_client
*client
;
282 client
= drv_client(dev
->driver
);
283 rc
= client
->probe(dev
);
290 static int ntb_transport_bus_remove(struct device
*dev
)
292 const struct ntb_transport_client
*client
;
294 client
= drv_client(dev
->driver
);
302 static struct bus_type ntb_transport_bus
= {
303 .name
= "ntb_transport",
304 .match
= ntb_transport_bus_match
,
305 .probe
= ntb_transport_bus_probe
,
306 .remove
= ntb_transport_bus_remove
,
309 static LIST_HEAD(ntb_transport_list
);
311 static int ntb_bus_init(struct ntb_transport_ctx
*nt
)
313 list_add_tail(&nt
->entry
, &ntb_transport_list
);
317 static void ntb_bus_remove(struct ntb_transport_ctx
*nt
)
319 struct ntb_transport_client_dev
*client_dev
, *cd
;
321 list_for_each_entry_safe(client_dev
, cd
, &nt
->client_devs
, entry
) {
322 dev_err(client_dev
->dev
.parent
, "%s still attached to bus, removing\n",
323 dev_name(&client_dev
->dev
));
324 list_del(&client_dev
->entry
);
325 device_unregister(&client_dev
->dev
);
328 list_del(&nt
->entry
);
331 static void ntb_transport_client_release(struct device
*dev
)
333 struct ntb_transport_client_dev
*client_dev
;
335 client_dev
= dev_client_dev(dev
);
340 * ntb_transport_unregister_client_dev - Unregister NTB client device
341 * @device_name: Name of NTB client device
343 * Unregister an NTB client device with the NTB transport layer
345 void ntb_transport_unregister_client_dev(char *device_name
)
347 struct ntb_transport_client_dev
*client
, *cd
;
348 struct ntb_transport_ctx
*nt
;
350 list_for_each_entry(nt
, &ntb_transport_list
, entry
)
351 list_for_each_entry_safe(client
, cd
, &nt
->client_devs
, entry
)
352 if (!strncmp(dev_name(&client
->dev
), device_name
,
353 strlen(device_name
))) {
354 list_del(&client
->entry
);
355 device_unregister(&client
->dev
);
358 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev
);
361 * ntb_transport_register_client_dev - Register NTB client device
362 * @device_name: Name of NTB client device
364 * Register an NTB client device with the NTB transport layer
366 int ntb_transport_register_client_dev(char *device_name
)
368 struct ntb_transport_client_dev
*client_dev
;
369 struct ntb_transport_ctx
*nt
;
373 if (list_empty(&ntb_transport_list
))
376 list_for_each_entry(nt
, &ntb_transport_list
, entry
) {
379 node
= dev_to_node(&nt
->ndev
->dev
);
381 client_dev
= kzalloc_node(sizeof(*client_dev
),
388 dev
= &client_dev
->dev
;
390 /* setup and register client devices */
391 dev_set_name(dev
, "%s%d", device_name
, i
);
392 dev
->bus
= &ntb_transport_bus
;
393 dev
->release
= ntb_transport_client_release
;
394 dev
->parent
= &nt
->ndev
->dev
;
396 rc
= device_register(dev
);
402 list_add_tail(&client_dev
->entry
, &nt
->client_devs
);
409 ntb_transport_unregister_client_dev(device_name
);
413 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev
);
416 * ntb_transport_register_client - Register NTB client driver
417 * @drv: NTB client driver to be registered
419 * Register an NTB client driver with the NTB transport layer
421 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
423 int ntb_transport_register_client(struct ntb_transport_client
*drv
)
425 drv
->driver
.bus
= &ntb_transport_bus
;
427 if (list_empty(&ntb_transport_list
))
430 return driver_register(&drv
->driver
);
432 EXPORT_SYMBOL_GPL(ntb_transport_register_client
);
435 * ntb_transport_unregister_client - Unregister NTB client driver
436 * @drv: NTB client driver to be unregistered
438 * Unregister an NTB client driver with the NTB transport layer
440 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
442 void ntb_transport_unregister_client(struct ntb_transport_client
*drv
)
444 driver_unregister(&drv
->driver
);
446 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client
);
448 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
451 struct ntb_transport_qp
*qp
;
453 ssize_t ret
, out_offset
, out_count
;
455 qp
= filp
->private_data
;
457 if (!qp
|| !qp
->link_is_up
)
462 buf
= kmalloc(out_count
, GFP_KERNEL
);
467 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
468 "\nNTB QP stats:\n\n");
469 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
470 "rx_bytes - \t%llu\n", qp
->rx_bytes
);
471 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
472 "rx_pkts - \t%llu\n", qp
->rx_pkts
);
473 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
474 "rx_memcpy - \t%llu\n", qp
->rx_memcpy
);
475 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
476 "rx_async - \t%llu\n", qp
->rx_async
);
477 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
478 "rx_ring_empty - %llu\n", qp
->rx_ring_empty
);
479 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
480 "rx_err_no_buf - %llu\n", qp
->rx_err_no_buf
);
481 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
482 "rx_err_oflow - \t%llu\n", qp
->rx_err_oflow
);
483 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
484 "rx_err_ver - \t%llu\n", qp
->rx_err_ver
);
485 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
486 "rx_buff - \t0x%p\n", qp
->rx_buff
);
487 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
488 "rx_index - \t%u\n", qp
->rx_index
);
489 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
490 "rx_max_entry - \t%u\n", qp
->rx_max_entry
);
491 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
492 "rx_alloc_entry - \t%u\n\n", qp
->rx_alloc_entry
);
494 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
495 "tx_bytes - \t%llu\n", qp
->tx_bytes
);
496 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
497 "tx_pkts - \t%llu\n", qp
->tx_pkts
);
498 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
499 "tx_memcpy - \t%llu\n", qp
->tx_memcpy
);
500 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
501 "tx_async - \t%llu\n", qp
->tx_async
);
502 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
503 "tx_ring_full - \t%llu\n", qp
->tx_ring_full
);
504 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
505 "tx_err_no_buf - %llu\n", qp
->tx_err_no_buf
);
506 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
507 "tx_mw - \t0x%p\n", qp
->tx_mw
);
508 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
509 "tx_index (H) - \t%u\n", qp
->tx_index
);
510 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
512 qp
->remote_rx_info
->entry
);
513 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
514 "tx_max_entry - \t%u\n", qp
->tx_max_entry
);
515 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
517 ntb_transport_tx_free_entry(qp
));
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 (mw_num
< 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_KERNEL
, 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
, PIDX
, num_mw
);
677 dma_free_coherent(&pdev
->dev
, mw
->alloc_size
,
678 mw
->alloc_addr
, mw
->dma_addr
);
682 mw
->alloc_addr
= NULL
;
683 mw
->virt_addr
= NULL
;
686 static int ntb_alloc_mw_buffer(struct ntb_transport_mw
*mw
,
687 struct device
*dma_dev
, size_t align
)
690 void *alloc_addr
, *virt_addr
;
693 alloc_addr
= dma_alloc_coherent(dma_dev
, mw
->alloc_size
,
694 &dma_addr
, GFP_KERNEL
);
696 dev_err(dma_dev
, "Unable to alloc MW buff of size %zu\n",
700 virt_addr
= alloc_addr
;
703 * we must ensure that the memory address allocated is BAR size
704 * aligned in order for the XLAT register to take the value. This
705 * is a requirement of the hardware. It is recommended to setup CMA
706 * for BAR sizes equal or greater than 4MB.
708 if (!IS_ALIGNED(dma_addr
, align
)) {
709 if (mw
->alloc_size
> mw
->buff_size
) {
710 virt_addr
= PTR_ALIGN(alloc_addr
, align
);
711 dma_addr
= ALIGN(dma_addr
, align
);
718 mw
->alloc_addr
= alloc_addr
;
719 mw
->virt_addr
= virt_addr
;
720 mw
->dma_addr
= dma_addr
;
725 dma_free_coherent(dma_dev
, mw
->alloc_size
, alloc_addr
, dma_addr
);
730 static int ntb_set_mw(struct ntb_transport_ctx
*nt
, int num_mw
,
731 resource_size_t size
)
733 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
734 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
735 size_t xlat_size
, buff_size
;
736 resource_size_t xlat_align
;
737 resource_size_t xlat_align_size
;
743 rc
= ntb_mw_get_align(nt
->ndev
, PIDX
, num_mw
, &xlat_align
,
744 &xlat_align_size
, NULL
);
748 xlat_size
= round_up(size
, xlat_align_size
);
749 buff_size
= round_up(size
, xlat_align
);
751 /* No need to re-setup */
752 if (mw
->xlat_size
== xlat_size
)
756 ntb_free_mw(nt
, num_mw
);
758 /* Alloc memory for receiving data. Must be aligned */
759 mw
->xlat_size
= xlat_size
;
760 mw
->buff_size
= buff_size
;
761 mw
->alloc_size
= buff_size
;
763 rc
= ntb_alloc_mw_buffer(mw
, &pdev
->dev
, xlat_align
);
766 rc
= ntb_alloc_mw_buffer(mw
, &pdev
->dev
, xlat_align
);
769 "Unable to alloc aligned MW buff\n");
777 /* Notify HW the memory location of the receive buffer */
778 rc
= ntb_mw_set_trans(nt
->ndev
, PIDX
, num_mw
, mw
->dma_addr
,
781 dev_err(&pdev
->dev
, "Unable to set mw%d translation", num_mw
);
782 ntb_free_mw(nt
, num_mw
);
789 static void ntb_qp_link_down_reset(struct ntb_transport_qp
*qp
)
791 qp
->link_is_up
= false;
798 qp
->rx_ring_empty
= 0;
799 qp
->rx_err_no_buf
= 0;
800 qp
->rx_err_oflow
= 0;
806 qp
->tx_ring_full
= 0;
807 qp
->tx_err_no_buf
= 0;
812 static void ntb_qp_link_cleanup(struct ntb_transport_qp
*qp
)
814 struct ntb_transport_ctx
*nt
= qp
->transport
;
815 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
817 dev_info(&pdev
->dev
, "qp %d: Link Cleanup\n", qp
->qp_num
);
819 cancel_delayed_work_sync(&qp
->link_work
);
820 ntb_qp_link_down_reset(qp
);
822 if (qp
->event_handler
)
823 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
826 static void ntb_qp_link_cleanup_work(struct work_struct
*work
)
828 struct ntb_transport_qp
*qp
= container_of(work
,
829 struct ntb_transport_qp
,
831 struct ntb_transport_ctx
*nt
= qp
->transport
;
833 ntb_qp_link_cleanup(qp
);
836 schedule_delayed_work(&qp
->link_work
,
837 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
840 static void ntb_qp_link_down(struct ntb_transport_qp
*qp
)
842 schedule_work(&qp
->link_cleanup
);
845 static void ntb_transport_link_cleanup(struct ntb_transport_ctx
*nt
)
847 struct ntb_transport_qp
*qp
;
849 unsigned int i
, count
;
851 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
853 /* Pass along the info to any clients */
854 for (i
= 0; i
< nt
->qp_count
; i
++)
855 if (qp_bitmap_alloc
& BIT_ULL(i
)) {
857 ntb_qp_link_cleanup(qp
);
858 cancel_work_sync(&qp
->link_cleanup
);
859 cancel_delayed_work_sync(&qp
->link_work
);
863 cancel_delayed_work_sync(&nt
->link_work
);
865 /* The scratchpad registers keep the values if the remote side
866 * goes down, blast them now to give them a sane value the next
867 * time they are accessed
869 count
= ntb_spad_count(nt
->ndev
);
870 for (i
= 0; i
< count
; i
++)
871 ntb_spad_write(nt
->ndev
, i
, 0);
874 static void ntb_transport_link_cleanup_work(struct work_struct
*work
)
876 struct ntb_transport_ctx
*nt
=
877 container_of(work
, struct ntb_transport_ctx
, link_cleanup
);
879 ntb_transport_link_cleanup(nt
);
882 static void ntb_transport_event_callback(void *data
)
884 struct ntb_transport_ctx
*nt
= data
;
886 if (ntb_link_is_up(nt
->ndev
, NULL
, NULL
) == 1)
887 schedule_delayed_work(&nt
->link_work
, 0);
889 schedule_work(&nt
->link_cleanup
);
892 static void ntb_transport_link_work(struct work_struct
*work
)
894 struct ntb_transport_ctx
*nt
=
895 container_of(work
, struct ntb_transport_ctx
, link_work
.work
);
896 struct ntb_dev
*ndev
= nt
->ndev
;
897 struct pci_dev
*pdev
= ndev
->pdev
;
898 resource_size_t size
;
902 /* send the local info, in the opposite order of the way we read it */
903 for (i
= 0; i
< nt
->mw_count
; i
++) {
904 size
= nt
->mw_vec
[i
].phys_size
;
906 if (max_mw_size
&& size
> max_mw_size
)
909 spad
= MW0_SZ_HIGH
+ (i
* 2);
910 ntb_peer_spad_write(ndev
, PIDX
, spad
, upper_32_bits(size
));
912 spad
= MW0_SZ_LOW
+ (i
* 2);
913 ntb_peer_spad_write(ndev
, PIDX
, spad
, lower_32_bits(size
));
916 ntb_peer_spad_write(ndev
, PIDX
, NUM_MWS
, nt
->mw_count
);
918 ntb_peer_spad_write(ndev
, PIDX
, NUM_QPS
, nt
->qp_count
);
920 ntb_peer_spad_write(ndev
, PIDX
, VERSION
, NTB_TRANSPORT_VERSION
);
922 /* Query the remote side for its info */
923 val
= ntb_spad_read(ndev
, VERSION
);
924 dev_dbg(&pdev
->dev
, "Remote version = %d\n", val
);
925 if (val
!= NTB_TRANSPORT_VERSION
)
928 val
= ntb_spad_read(ndev
, NUM_QPS
);
929 dev_dbg(&pdev
->dev
, "Remote max number of qps = %d\n", val
);
930 if (val
!= nt
->qp_count
)
933 val
= ntb_spad_read(ndev
, NUM_MWS
);
934 dev_dbg(&pdev
->dev
, "Remote number of mws = %d\n", val
);
935 if (val
!= nt
->mw_count
)
938 for (i
= 0; i
< nt
->mw_count
; i
++) {
941 val
= ntb_spad_read(ndev
, MW0_SZ_HIGH
+ (i
* 2));
942 val64
= (u64
)val
<< 32;
944 val
= ntb_spad_read(ndev
, MW0_SZ_LOW
+ (i
* 2));
947 dev_dbg(&pdev
->dev
, "Remote MW%d size = %#llx\n", i
, val64
);
949 rc
= ntb_set_mw(nt
, i
, val64
);
954 nt
->link_is_up
= true;
956 for (i
= 0; i
< nt
->qp_count
; i
++) {
957 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[i
];
959 ntb_transport_setup_qp_mw(nt
, i
);
961 if (qp
->client_ready
)
962 schedule_delayed_work(&qp
->link_work
, 0);
968 for (i
= 0; i
< nt
->mw_count
; i
++)
971 /* if there's an actual failure, we should just bail */
976 if (ntb_link_is_up(ndev
, NULL
, NULL
) == 1)
977 schedule_delayed_work(&nt
->link_work
,
978 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
981 static void ntb_qp_link_work(struct work_struct
*work
)
983 struct ntb_transport_qp
*qp
= container_of(work
,
984 struct ntb_transport_qp
,
986 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
987 struct ntb_transport_ctx
*nt
= qp
->transport
;
990 WARN_ON(!nt
->link_is_up
);
992 val
= ntb_spad_read(nt
->ndev
, QP_LINKS
);
994 ntb_peer_spad_write(nt
->ndev
, PIDX
, QP_LINKS
, val
| BIT(qp
->qp_num
));
996 /* query remote spad for qp ready bits */
997 dev_dbg_ratelimited(&pdev
->dev
, "Remote QP link status = %x\n", val
);
999 /* See if the remote side is up */
1000 if (val
& BIT(qp
->qp_num
)) {
1001 dev_info(&pdev
->dev
, "qp %d: Link Up\n", qp
->qp_num
);
1002 qp
->link_is_up
= true;
1005 if (qp
->event_handler
)
1006 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
1009 tasklet_schedule(&qp
->rxc_db_work
);
1010 } else if (nt
->link_is_up
)
1011 schedule_delayed_work(&qp
->link_work
,
1012 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
1015 static int ntb_transport_init_queue(struct ntb_transport_ctx
*nt
,
1016 unsigned int qp_num
)
1018 struct ntb_transport_qp
*qp
;
1019 phys_addr_t mw_base
;
1020 resource_size_t mw_size
;
1021 unsigned int num_qps_mw
, tx_size
;
1022 unsigned int mw_num
, mw_count
, qp_count
;
1025 mw_count
= nt
->mw_count
;
1026 qp_count
= nt
->qp_count
;
1028 mw_num
= QP_TO_MW(nt
, qp_num
);
1030 qp
= &nt
->qp_vec
[qp_num
];
1031 qp
->qp_num
= qp_num
;
1033 qp
->ndev
= nt
->ndev
;
1034 qp
->client_ready
= false;
1035 qp
->event_handler
= NULL
;
1036 ntb_qp_link_down_reset(qp
);
1038 if (mw_num
< qp_count
% mw_count
)
1039 num_qps_mw
= qp_count
/ mw_count
+ 1;
1041 num_qps_mw
= qp_count
/ mw_count
;
1043 mw_base
= nt
->mw_vec
[mw_num
].phys_addr
;
1044 mw_size
= nt
->mw_vec
[mw_num
].phys_size
;
1046 if (max_mw_size
&& mw_size
> max_mw_size
)
1047 mw_size
= max_mw_size
;
1049 tx_size
= (unsigned int)mw_size
/ num_qps_mw
;
1050 qp_offset
= tx_size
* (qp_num
/ mw_count
);
1052 qp
->tx_mw
= nt
->mw_vec
[mw_num
].vbase
+ qp_offset
;
1056 qp
->tx_mw_phys
= mw_base
+ qp_offset
;
1057 if (!qp
->tx_mw_phys
)
1060 tx_size
-= sizeof(struct ntb_rx_info
);
1061 qp
->rx_info
= qp
->tx_mw
+ tx_size
;
1063 /* Due to housekeeping, there must be atleast 2 buffs */
1064 qp
->tx_max_frame
= min(transport_mtu
, tx_size
/ 2);
1065 qp
->tx_max_entry
= tx_size
/ qp
->tx_max_frame
;
1067 if (nt
->debugfs_node_dir
) {
1068 char debugfs_name
[4];
1070 snprintf(debugfs_name
, 4, "qp%d", qp_num
);
1071 qp
->debugfs_dir
= debugfs_create_dir(debugfs_name
,
1072 nt
->debugfs_node_dir
);
1074 qp
->debugfs_stats
= debugfs_create_file("stats", S_IRUSR
,
1075 qp
->debugfs_dir
, qp
,
1076 &ntb_qp_debugfs_stats
);
1078 qp
->debugfs_dir
= NULL
;
1079 qp
->debugfs_stats
= NULL
;
1082 INIT_DELAYED_WORK(&qp
->link_work
, ntb_qp_link_work
);
1083 INIT_WORK(&qp
->link_cleanup
, ntb_qp_link_cleanup_work
);
1085 spin_lock_init(&qp
->ntb_rx_q_lock
);
1086 spin_lock_init(&qp
->ntb_tx_free_q_lock
);
1088 INIT_LIST_HEAD(&qp
->rx_post_q
);
1089 INIT_LIST_HEAD(&qp
->rx_pend_q
);
1090 INIT_LIST_HEAD(&qp
->rx_free_q
);
1091 INIT_LIST_HEAD(&qp
->tx_free_q
);
1093 tasklet_init(&qp
->rxc_db_work
, ntb_transport_rxc_db
,
1099 static int ntb_transport_probe(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1101 struct ntb_transport_ctx
*nt
;
1102 struct ntb_transport_mw
*mw
;
1103 unsigned int mw_count
, qp_count
, spad_count
, max_mw_count_for_spads
;
1108 mw_count
= ntb_peer_mw_count(ndev
);
1110 if (!ndev
->ops
->mw_set_trans
) {
1111 dev_err(&ndev
->dev
, "Inbound MW based NTB API is required\n");
1115 if (ntb_db_is_unsafe(ndev
))
1117 "doorbell is unsafe, proceed anyway...\n");
1118 if (ntb_spad_is_unsafe(ndev
))
1120 "scratchpad is unsafe, proceed anyway...\n");
1122 if (ntb_peer_port_count(ndev
) != NTB_DEF_PEER_CNT
)
1123 dev_warn(&ndev
->dev
, "Multi-port NTB devices unsupported\n");
1125 node
= dev_to_node(&ndev
->dev
);
1127 nt
= kzalloc_node(sizeof(*nt
), GFP_KERNEL
, node
);
1132 spad_count
= ntb_spad_count(ndev
);
1134 /* Limit the MW's based on the availability of scratchpads */
1136 if (spad_count
< NTB_TRANSPORT_MIN_SPADS
) {
1142 max_mw_count_for_spads
= (spad_count
- MW0_SZ_HIGH
) / 2;
1143 nt
->mw_count
= min(mw_count
, max_mw_count_for_spads
);
1145 nt
->mw_vec
= kcalloc_node(mw_count
, sizeof(*nt
->mw_vec
),
1152 for (i
= 0; i
< mw_count
; i
++) {
1153 mw
= &nt
->mw_vec
[i
];
1155 rc
= ntb_peer_mw_get_addr(ndev
, i
, &mw
->phys_addr
,
1160 mw
->vbase
= ioremap_wc(mw
->phys_addr
, mw
->phys_size
);
1168 mw
->virt_addr
= NULL
;
1172 qp_bitmap
= ntb_db_valid_mask(ndev
);
1174 qp_count
= ilog2(qp_bitmap
);
1175 if (max_num_clients
&& max_num_clients
< qp_count
)
1176 qp_count
= max_num_clients
;
1177 else if (nt
->mw_count
< qp_count
)
1178 qp_count
= nt
->mw_count
;
1180 qp_bitmap
&= BIT_ULL(qp_count
) - 1;
1182 nt
->qp_count
= qp_count
;
1183 nt
->qp_bitmap
= qp_bitmap
;
1184 nt
->qp_bitmap_free
= qp_bitmap
;
1186 nt
->qp_vec
= kcalloc_node(qp_count
, sizeof(*nt
->qp_vec
),
1193 if (nt_debugfs_dir
) {
1194 nt
->debugfs_node_dir
=
1195 debugfs_create_dir(pci_name(ndev
->pdev
),
1199 for (i
= 0; i
< qp_count
; i
++) {
1200 rc
= ntb_transport_init_queue(nt
, i
);
1205 INIT_DELAYED_WORK(&nt
->link_work
, ntb_transport_link_work
);
1206 INIT_WORK(&nt
->link_cleanup
, ntb_transport_link_cleanup_work
);
1208 rc
= ntb_set_ctx(ndev
, nt
, &ntb_transport_ops
);
1212 INIT_LIST_HEAD(&nt
->client_devs
);
1213 rc
= ntb_bus_init(nt
);
1217 nt
->link_is_up
= false;
1218 ntb_link_enable(ndev
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
1219 ntb_link_event(ndev
);
1224 ntb_clear_ctx(ndev
);
1229 mw
= &nt
->mw_vec
[i
];
1238 static void ntb_transport_free(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1240 struct ntb_transport_ctx
*nt
= ndev
->ctx
;
1241 struct ntb_transport_qp
*qp
;
1242 u64 qp_bitmap_alloc
;
1245 ntb_transport_link_cleanup(nt
);
1246 cancel_work_sync(&nt
->link_cleanup
);
1247 cancel_delayed_work_sync(&nt
->link_work
);
1249 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
1251 /* verify that all the qp's are freed */
1252 for (i
= 0; i
< nt
->qp_count
; i
++) {
1253 qp
= &nt
->qp_vec
[i
];
1254 if (qp_bitmap_alloc
& BIT_ULL(i
))
1255 ntb_transport_free_queue(qp
);
1256 debugfs_remove_recursive(qp
->debugfs_dir
);
1259 ntb_link_disable(ndev
);
1260 ntb_clear_ctx(ndev
);
1264 for (i
= nt
->mw_count
; i
--; ) {
1266 iounmap(nt
->mw_vec
[i
].vbase
);
1274 static void ntb_complete_rxc(struct ntb_transport_qp
*qp
)
1276 struct ntb_queue_entry
*entry
;
1279 unsigned long irqflags
;
1281 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1283 while (!list_empty(&qp
->rx_post_q
)) {
1284 entry
= list_first_entry(&qp
->rx_post_q
,
1285 struct ntb_queue_entry
, entry
);
1286 if (!(entry
->flags
& DESC_DONE_FLAG
))
1289 entry
->rx_hdr
->flags
= 0;
1290 iowrite32(entry
->rx_index
, &qp
->rx_info
->entry
);
1292 cb_data
= entry
->cb_data
;
1295 list_move_tail(&entry
->entry
, &qp
->rx_free_q
);
1297 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1299 if (qp
->rx_handler
&& qp
->client_ready
)
1300 qp
->rx_handler(qp
, qp
->cb_data
, cb_data
, len
);
1302 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1305 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1308 static void ntb_rx_copy_callback(void *data
,
1309 const struct dmaengine_result
*res
)
1311 struct ntb_queue_entry
*entry
= data
;
1313 /* we need to check DMA results if we are using DMA */
1315 enum dmaengine_tx_result dma_err
= res
->result
;
1318 case DMA_TRANS_READ_FAILED
:
1319 case DMA_TRANS_WRITE_FAILED
:
1322 case DMA_TRANS_ABORTED
:
1324 struct ntb_transport_qp
*qp
= entry
->qp
;
1325 void *offset
= qp
->rx_buff
+ qp
->rx_max_frame
*
1328 ntb_memcpy_rx(entry
, offset
);
1333 case DMA_TRANS_NOERROR
:
1339 entry
->flags
|= DESC_DONE_FLAG
;
1341 ntb_complete_rxc(entry
->qp
);
1344 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
)
1346 void *buf
= entry
->buf
;
1347 size_t len
= entry
->len
;
1349 memcpy(buf
, offset
, len
);
1351 /* Ensure that the data is fully copied out before clearing the flag */
1354 ntb_rx_copy_callback(entry
, NULL
);
1357 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
)
1359 struct dma_async_tx_descriptor
*txd
;
1360 struct ntb_transport_qp
*qp
= entry
->qp
;
1361 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1362 struct dma_device
*device
;
1363 size_t pay_off
, buff_off
, len
;
1364 struct dmaengine_unmap_data
*unmap
;
1365 dma_cookie_t cookie
;
1366 void *buf
= entry
->buf
;
1369 device
= chan
->device
;
1370 pay_off
= (size_t)offset
& ~PAGE_MASK
;
1371 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1373 if (!is_dma_copy_aligned(device
, pay_off
, buff_off
, len
))
1376 unmap
= dmaengine_get_unmap_data(device
->dev
, 2, GFP_NOWAIT
);
1381 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(offset
),
1382 pay_off
, len
, DMA_TO_DEVICE
);
1383 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1388 unmap
->addr
[1] = dma_map_page(device
->dev
, virt_to_page(buf
),
1389 buff_off
, len
, DMA_FROM_DEVICE
);
1390 if (dma_mapping_error(device
->dev
, unmap
->addr
[1]))
1393 unmap
->from_cnt
= 1;
1395 txd
= device
->device_prep_dma_memcpy(chan
, unmap
->addr
[1],
1396 unmap
->addr
[0], len
,
1397 DMA_PREP_INTERRUPT
);
1401 txd
->callback_result
= ntb_rx_copy_callback
;
1402 txd
->callback_param
= entry
;
1403 dma_set_unmap(txd
, unmap
);
1405 cookie
= dmaengine_submit(txd
);
1406 if (dma_submit_error(cookie
))
1409 dmaengine_unmap_put(unmap
);
1411 qp
->last_cookie
= cookie
;
1418 dmaengine_unmap_put(unmap
);
1420 dmaengine_unmap_put(unmap
);
1425 static void ntb_async_rx(struct ntb_queue_entry
*entry
, void *offset
)
1427 struct ntb_transport_qp
*qp
= entry
->qp
;
1428 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1434 if (entry
->len
< copy_bytes
)
1437 res
= ntb_async_rx_submit(entry
, offset
);
1441 if (!entry
->retries
)
1447 ntb_memcpy_rx(entry
, offset
);
1451 static int ntb_process_rxc(struct ntb_transport_qp
*qp
)
1453 struct ntb_payload_header
*hdr
;
1454 struct ntb_queue_entry
*entry
;
1457 offset
= qp
->rx_buff
+ qp
->rx_max_frame
* qp
->rx_index
;
1458 hdr
= offset
+ qp
->rx_max_frame
- sizeof(struct ntb_payload_header
);
1460 dev_dbg(&qp
->ndev
->pdev
->dev
, "qp %d: RX ver %u len %d flags %x\n",
1461 qp
->qp_num
, hdr
->ver
, hdr
->len
, hdr
->flags
);
1463 if (!(hdr
->flags
& DESC_DONE_FLAG
)) {
1464 dev_dbg(&qp
->ndev
->pdev
->dev
, "done flag not set\n");
1465 qp
->rx_ring_empty
++;
1469 if (hdr
->flags
& LINK_DOWN_FLAG
) {
1470 dev_dbg(&qp
->ndev
->pdev
->dev
, "link down flag set\n");
1471 ntb_qp_link_down(qp
);
1476 if (hdr
->ver
!= (u32
)qp
->rx_pkts
) {
1477 dev_dbg(&qp
->ndev
->pdev
->dev
,
1478 "version mismatch, expected %llu - got %u\n",
1479 qp
->rx_pkts
, hdr
->ver
);
1484 entry
= ntb_list_mv(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
, &qp
->rx_post_q
);
1486 dev_dbg(&qp
->ndev
->pdev
->dev
, "no receive buffer\n");
1487 qp
->rx_err_no_buf
++;
1491 entry
->rx_hdr
= hdr
;
1492 entry
->rx_index
= qp
->rx_index
;
1494 if (hdr
->len
> entry
->len
) {
1495 dev_dbg(&qp
->ndev
->pdev
->dev
,
1496 "receive buffer overflow! Wanted %d got %d\n",
1497 hdr
->len
, entry
->len
);
1501 entry
->flags
|= DESC_DONE_FLAG
;
1503 ntb_complete_rxc(qp
);
1505 dev_dbg(&qp
->ndev
->pdev
->dev
,
1506 "RX OK index %u ver %u size %d into buf size %d\n",
1507 qp
->rx_index
, hdr
->ver
, hdr
->len
, entry
->len
);
1509 qp
->rx_bytes
+= hdr
->len
;
1512 entry
->len
= hdr
->len
;
1514 ntb_async_rx(entry
, offset
);
1518 qp
->rx_index
%= qp
->rx_max_entry
;
1523 static void ntb_transport_rxc_db(unsigned long data
)
1525 struct ntb_transport_qp
*qp
= (void *)data
;
1528 dev_dbg(&qp
->ndev
->pdev
->dev
, "%s: doorbell %d received\n",
1529 __func__
, qp
->qp_num
);
1531 /* Limit the number of packets processed in a single interrupt to
1532 * provide fairness to others
1534 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
1535 rc
= ntb_process_rxc(qp
);
1540 if (i
&& qp
->rx_dma_chan
)
1541 dma_async_issue_pending(qp
->rx_dma_chan
);
1543 if (i
== qp
->rx_max_entry
) {
1544 /* there is more work to do */
1546 tasklet_schedule(&qp
->rxc_db_work
);
1547 } else if (ntb_db_read(qp
->ndev
) & BIT_ULL(qp
->qp_num
)) {
1548 /* the doorbell bit is set: clear it */
1549 ntb_db_clear(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1550 /* ntb_db_read ensures ntb_db_clear write is committed */
1551 ntb_db_read(qp
->ndev
);
1553 /* an interrupt may have arrived between finishing
1554 * ntb_process_rxc and clearing the doorbell bit:
1555 * there might be some more work to do.
1558 tasklet_schedule(&qp
->rxc_db_work
);
1562 static void ntb_tx_copy_callback(void *data
,
1563 const struct dmaengine_result
*res
)
1565 struct ntb_queue_entry
*entry
= data
;
1566 struct ntb_transport_qp
*qp
= entry
->qp
;
1567 struct ntb_payload_header __iomem
*hdr
= entry
->tx_hdr
;
1569 /* we need to check DMA results if we are using DMA */
1571 enum dmaengine_tx_result dma_err
= res
->result
;
1574 case DMA_TRANS_READ_FAILED
:
1575 case DMA_TRANS_WRITE_FAILED
:
1578 case DMA_TRANS_ABORTED
:
1580 void __iomem
*offset
=
1581 qp
->tx_mw
+ qp
->tx_max_frame
*
1584 /* resubmit via CPU */
1585 ntb_memcpy_tx(entry
, offset
);
1590 case DMA_TRANS_NOERROR
:
1596 iowrite32(entry
->flags
| DESC_DONE_FLAG
, &hdr
->flags
);
1598 ntb_peer_db_set(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1600 /* The entry length can only be zero if the packet is intended to be a
1601 * "link down" or similar. Since no payload is being sent in these
1602 * cases, there is nothing to add to the completion queue.
1604 if (entry
->len
> 0) {
1605 qp
->tx_bytes
+= entry
->len
;
1608 qp
->tx_handler(qp
, qp
->cb_data
, entry
->cb_data
,
1612 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
, &qp
->tx_free_q
);
1615 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
)
1617 #ifdef ARCH_HAS_NOCACHE_UACCESS
1619 * Using non-temporal mov to improve performance on non-cached
1620 * writes, even though we aren't actually copying from user space.
1622 __copy_from_user_inatomic_nocache(offset
, entry
->buf
, entry
->len
);
1624 memcpy_toio(offset
, entry
->buf
, entry
->len
);
1627 /* Ensure that the data is fully copied out before setting the flags */
1630 ntb_tx_copy_callback(entry
, NULL
);
1633 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
1634 struct ntb_queue_entry
*entry
)
1636 struct dma_async_tx_descriptor
*txd
;
1637 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1638 struct dma_device
*device
;
1639 size_t len
= entry
->len
;
1640 void *buf
= entry
->buf
;
1641 size_t dest_off
, buff_off
;
1642 struct dmaengine_unmap_data
*unmap
;
1644 dma_cookie_t cookie
;
1646 device
= chan
->device
;
1647 dest
= qp
->tx_mw_phys
+ qp
->tx_max_frame
* entry
->tx_index
;
1648 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1649 dest_off
= (size_t)dest
& ~PAGE_MASK
;
1651 if (!is_dma_copy_aligned(device
, buff_off
, dest_off
, len
))
1654 unmap
= dmaengine_get_unmap_data(device
->dev
, 1, GFP_NOWAIT
);
1659 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(buf
),
1660 buff_off
, len
, DMA_TO_DEVICE
);
1661 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1666 txd
= device
->device_prep_dma_memcpy(chan
, dest
, unmap
->addr
[0], len
,
1667 DMA_PREP_INTERRUPT
);
1671 txd
->callback_result
= ntb_tx_copy_callback
;
1672 txd
->callback_param
= entry
;
1673 dma_set_unmap(txd
, unmap
);
1675 cookie
= dmaengine_submit(txd
);
1676 if (dma_submit_error(cookie
))
1679 dmaengine_unmap_put(unmap
);
1681 dma_async_issue_pending(chan
);
1685 dmaengine_unmap_put(unmap
);
1687 dmaengine_unmap_put(unmap
);
1692 static void ntb_async_tx(struct ntb_transport_qp
*qp
,
1693 struct ntb_queue_entry
*entry
)
1695 struct ntb_payload_header __iomem
*hdr
;
1696 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1697 void __iomem
*offset
;
1700 entry
->tx_index
= qp
->tx_index
;
1701 offset
= qp
->tx_mw
+ qp
->tx_max_frame
* entry
->tx_index
;
1702 hdr
= offset
+ qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1703 entry
->tx_hdr
= hdr
;
1705 iowrite32(entry
->len
, &hdr
->len
);
1706 iowrite32((u32
)qp
->tx_pkts
, &hdr
->ver
);
1711 if (entry
->len
< copy_bytes
)
1714 res
= ntb_async_tx_submit(qp
, entry
);
1718 if (!entry
->retries
)
1724 ntb_memcpy_tx(entry
, offset
);
1728 static int ntb_process_tx(struct ntb_transport_qp
*qp
,
1729 struct ntb_queue_entry
*entry
)
1731 if (qp
->tx_index
== qp
->remote_rx_info
->entry
) {
1736 if (entry
->len
> qp
->tx_max_frame
- sizeof(struct ntb_payload_header
)) {
1738 qp
->tx_handler(qp
, qp
->cb_data
, NULL
, -EIO
);
1740 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1745 ntb_async_tx(qp
, entry
);
1748 qp
->tx_index
%= qp
->tx_max_entry
;
1755 static void ntb_send_link_down(struct ntb_transport_qp
*qp
)
1757 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
1758 struct ntb_queue_entry
*entry
;
1761 if (!qp
->link_is_up
)
1764 dev_info(&pdev
->dev
, "qp %d: Send Link Down\n", qp
->qp_num
);
1766 for (i
= 0; i
< NTB_LINK_DOWN_TIMEOUT
; i
++) {
1767 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1776 entry
->cb_data
= NULL
;
1779 entry
->flags
= LINK_DOWN_FLAG
;
1781 rc
= ntb_process_tx(qp
, entry
);
1783 dev_err(&pdev
->dev
, "ntb: QP%d unable to send linkdown msg\n",
1786 ntb_qp_link_down_reset(qp
);
1789 static bool ntb_dma_filter_fn(struct dma_chan
*chan
, void *node
)
1791 return dev_to_node(&chan
->dev
->device
) == (int)(unsigned long)node
;
1795 * ntb_transport_create_queue - Create a new NTB transport layer queue
1796 * @rx_handler: receive callback function
1797 * @tx_handler: transmit callback function
1798 * @event_handler: event callback function
1800 * Create a new NTB transport layer queue and provide the queue with a callback
1801 * routine for both transmit and receive. The receive callback routine will be
1802 * used to pass up data when the transport has received it on the queue. The
1803 * transmit callback routine will be called when the transport has completed the
1804 * transmission of the data on the queue and the data is ready to be freed.
1806 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1808 struct ntb_transport_qp
*
1809 ntb_transport_create_queue(void *data
, struct device
*client_dev
,
1810 const struct ntb_queue_handlers
*handlers
)
1812 struct ntb_dev
*ndev
;
1813 struct pci_dev
*pdev
;
1814 struct ntb_transport_ctx
*nt
;
1815 struct ntb_queue_entry
*entry
;
1816 struct ntb_transport_qp
*qp
;
1818 unsigned int free_queue
;
1819 dma_cap_mask_t dma_mask
;
1823 ndev
= dev_ntb(client_dev
->parent
);
1827 node
= dev_to_node(&ndev
->dev
);
1829 free_queue
= ffs(nt
->qp_bitmap_free
);
1833 /* decrement free_queue to make it zero based */
1836 qp
= &nt
->qp_vec
[free_queue
];
1837 qp_bit
= BIT_ULL(qp
->qp_num
);
1839 nt
->qp_bitmap_free
&= ~qp_bit
;
1842 qp
->rx_handler
= handlers
->rx_handler
;
1843 qp
->tx_handler
= handlers
->tx_handler
;
1844 qp
->event_handler
= handlers
->event_handler
;
1846 dma_cap_zero(dma_mask
);
1847 dma_cap_set(DMA_MEMCPY
, dma_mask
);
1851 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1852 (void *)(unsigned long)node
);
1853 if (!qp
->tx_dma_chan
)
1854 dev_info(&pdev
->dev
, "Unable to allocate TX DMA channel\n");
1857 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1858 (void *)(unsigned long)node
);
1859 if (!qp
->rx_dma_chan
)
1860 dev_info(&pdev
->dev
, "Unable to allocate RX DMA channel\n");
1862 qp
->tx_dma_chan
= NULL
;
1863 qp
->rx_dma_chan
= NULL
;
1866 dev_dbg(&pdev
->dev
, "Using %s memcpy for TX\n",
1867 qp
->tx_dma_chan
? "DMA" : "CPU");
1869 dev_dbg(&pdev
->dev
, "Using %s memcpy for RX\n",
1870 qp
->rx_dma_chan
? "DMA" : "CPU");
1872 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1873 entry
= kzalloc_node(sizeof(*entry
), GFP_KERNEL
, node
);
1878 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
1881 qp
->rx_alloc_entry
= NTB_QP_DEF_NUM_ENTRIES
;
1883 for (i
= 0; i
< qp
->tx_max_entry
; i
++) {
1884 entry
= kzalloc_node(sizeof(*entry
), GFP_KERNEL
, node
);
1889 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1893 ntb_db_clear(qp
->ndev
, qp_bit
);
1894 ntb_db_clear_mask(qp
->ndev
, qp_bit
);
1896 dev_info(&pdev
->dev
, "NTB Transport QP %d created\n", qp
->qp_num
);
1901 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1904 qp
->rx_alloc_entry
= 0;
1905 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1907 if (qp
->tx_dma_chan
)
1908 dma_release_channel(qp
->tx_dma_chan
);
1909 if (qp
->rx_dma_chan
)
1910 dma_release_channel(qp
->rx_dma_chan
);
1911 nt
->qp_bitmap_free
|= qp_bit
;
1915 EXPORT_SYMBOL_GPL(ntb_transport_create_queue
);
1918 * ntb_transport_free_queue - Frees NTB transport queue
1919 * @qp: NTB queue to be freed
1921 * Frees NTB transport queue
1923 void ntb_transport_free_queue(struct ntb_transport_qp
*qp
)
1925 struct pci_dev
*pdev
;
1926 struct ntb_queue_entry
*entry
;
1932 pdev
= qp
->ndev
->pdev
;
1936 if (qp
->tx_dma_chan
) {
1937 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1938 /* Putting the dma_chan to NULL will force any new traffic to be
1939 * processed by the CPU instead of the DAM engine
1941 qp
->tx_dma_chan
= NULL
;
1943 /* Try to be nice and wait for any queued DMA engine
1944 * transactions to process before smashing it with a rock
1946 dma_sync_wait(chan
, qp
->last_cookie
);
1947 dmaengine_terminate_all(chan
);
1948 dma_release_channel(chan
);
1951 if (qp
->rx_dma_chan
) {
1952 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1953 /* Putting the dma_chan to NULL will force any new traffic to be
1954 * processed by the CPU instead of the DAM engine
1956 qp
->rx_dma_chan
= NULL
;
1958 /* Try to be nice and wait for any queued DMA engine
1959 * transactions to process before smashing it with a rock
1961 dma_sync_wait(chan
, qp
->last_cookie
);
1962 dmaengine_terminate_all(chan
);
1963 dma_release_channel(chan
);
1966 qp_bit
= BIT_ULL(qp
->qp_num
);
1968 ntb_db_set_mask(qp
->ndev
, qp_bit
);
1969 tasklet_kill(&qp
->rxc_db_work
);
1971 cancel_delayed_work_sync(&qp
->link_work
);
1974 qp
->rx_handler
= NULL
;
1975 qp
->tx_handler
= NULL
;
1976 qp
->event_handler
= NULL
;
1978 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1981 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
))) {
1982 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_pend_q\n");
1986 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_post_q
))) {
1987 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_post_q\n");
1991 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1994 qp
->transport
->qp_bitmap_free
|= qp_bit
;
1996 dev_info(&pdev
->dev
, "NTB Transport QP %d freed\n", qp
->qp_num
);
1998 EXPORT_SYMBOL_GPL(ntb_transport_free_queue
);
2001 * ntb_transport_rx_remove - Dequeues enqueued rx packet
2002 * @qp: NTB queue to be freed
2003 * @len: pointer to variable to write enqueued buffers length
2005 * Dequeues unused buffers from receive queue. Should only be used during
2008 * RETURNS: NULL error value on error, or void* for success.
2010 void *ntb_transport_rx_remove(struct ntb_transport_qp
*qp
, unsigned int *len
)
2012 struct ntb_queue_entry
*entry
;
2015 if (!qp
|| qp
->client_ready
)
2018 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
);
2022 buf
= entry
->cb_data
;
2025 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
2029 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove
);
2032 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
2033 * @qp: NTB transport layer queue the entry is to be enqueued on
2034 * @cb: per buffer pointer for callback function to use
2035 * @data: pointer to data buffer that incoming packets will be copied into
2036 * @len: length of the data buffer
2038 * Enqueue a new receive buffer onto the transport queue into which a NTB
2039 * payload can be received into.
2041 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2043 int ntb_transport_rx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
2046 struct ntb_queue_entry
*entry
;
2051 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
);
2055 entry
->cb_data
= cb
;
2061 entry
->rx_index
= 0;
2063 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
2066 tasklet_schedule(&qp
->rxc_db_work
);
2070 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue
);
2073 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2074 * @qp: NTB transport layer queue the entry is to be enqueued on
2075 * @cb: per buffer pointer for callback function to use
2076 * @data: pointer to data buffer that will be sent
2077 * @len: length of the data buffer
2079 * Enqueue a new transmit buffer onto the transport queue from which a NTB
2080 * payload will be transmitted. This assumes that a lock is being held to
2081 * serialize access to the qp.
2083 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2085 int ntb_transport_tx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
2088 struct ntb_queue_entry
*entry
;
2091 if (!qp
|| !qp
->link_is_up
|| !len
)
2094 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
2096 qp
->tx_err_no_buf
++;
2100 entry
->cb_data
= cb
;
2106 entry
->tx_index
= 0;
2108 rc
= ntb_process_tx(qp
, entry
);
2110 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
2115 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue
);
2118 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2119 * @qp: NTB transport layer queue to be enabled
2121 * Notify NTB transport layer of client readiness to use queue
2123 void ntb_transport_link_up(struct ntb_transport_qp
*qp
)
2128 qp
->client_ready
= true;
2130 if (qp
->transport
->link_is_up
)
2131 schedule_delayed_work(&qp
->link_work
, 0);
2133 EXPORT_SYMBOL_GPL(ntb_transport_link_up
);
2136 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2137 * @qp: NTB transport layer queue to be disabled
2139 * Notify NTB transport layer of client's desire to no longer receive data on
2140 * transport queue specified. It is the client's responsibility to ensure all
2141 * entries on queue are purged or otherwise handled appropriately.
2143 void ntb_transport_link_down(struct ntb_transport_qp
*qp
)
2150 qp
->client_ready
= false;
2152 val
= ntb_spad_read(qp
->ndev
, QP_LINKS
);
2154 ntb_peer_spad_write(qp
->ndev
, PIDX
, QP_LINKS
, val
& ~BIT(qp
->qp_num
));
2157 ntb_send_link_down(qp
);
2159 cancel_delayed_work_sync(&qp
->link_work
);
2161 EXPORT_SYMBOL_GPL(ntb_transport_link_down
);
2164 * ntb_transport_link_query - Query transport link state
2165 * @qp: NTB transport layer queue to be queried
2167 * Query connectivity to the remote system of the NTB transport queue
2169 * RETURNS: true for link up or false for link down
2171 bool ntb_transport_link_query(struct ntb_transport_qp
*qp
)
2176 return qp
->link_is_up
;
2178 EXPORT_SYMBOL_GPL(ntb_transport_link_query
);
2181 * ntb_transport_qp_num - Query the qp number
2182 * @qp: NTB transport layer queue to be queried
2184 * Query qp number of the NTB transport queue
2186 * RETURNS: a zero based number specifying the qp number
2188 unsigned char ntb_transport_qp_num(struct ntb_transport_qp
*qp
)
2195 EXPORT_SYMBOL_GPL(ntb_transport_qp_num
);
2198 * ntb_transport_max_size - Query the max payload size of a qp
2199 * @qp: NTB transport layer queue to be queried
2201 * Query the maximum payload size permissible on the given qp
2203 * RETURNS: the max payload size of a qp
2205 unsigned int ntb_transport_max_size(struct ntb_transport_qp
*qp
)
2207 unsigned int max_size
;
2208 unsigned int copy_align
;
2209 struct dma_chan
*rx_chan
, *tx_chan
;
2214 rx_chan
= qp
->rx_dma_chan
;
2215 tx_chan
= qp
->tx_dma_chan
;
2217 copy_align
= max(rx_chan
? rx_chan
->device
->copy_align
: 0,
2218 tx_chan
? tx_chan
->device
->copy_align
: 0);
2220 /* If DMA engine usage is possible, try to find the max size for that */
2221 max_size
= qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
2222 max_size
= round_down(max_size
, 1 << copy_align
);
2226 EXPORT_SYMBOL_GPL(ntb_transport_max_size
);
2228 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp
*qp
)
2230 unsigned int head
= qp
->tx_index
;
2231 unsigned int tail
= qp
->remote_rx_info
->entry
;
2233 return tail
> head
? tail
- head
: qp
->tx_max_entry
+ tail
- head
;
2235 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry
);
2237 static void ntb_transport_doorbell_callback(void *data
, int vector
)
2239 struct ntb_transport_ctx
*nt
= data
;
2240 struct ntb_transport_qp
*qp
;
2242 unsigned int qp_num
;
2244 db_bits
= (nt
->qp_bitmap
& ~nt
->qp_bitmap_free
&
2245 ntb_db_vector_mask(nt
->ndev
, vector
));
2248 qp_num
= __ffs(db_bits
);
2249 qp
= &nt
->qp_vec
[qp_num
];
2252 tasklet_schedule(&qp
->rxc_db_work
);
2254 db_bits
&= ~BIT_ULL(qp_num
);
2258 static const struct ntb_ctx_ops ntb_transport_ops
= {
2259 .link_event
= ntb_transport_event_callback
,
2260 .db_event
= ntb_transport_doorbell_callback
,
2263 static struct ntb_client ntb_transport_client
= {
2265 .probe
= ntb_transport_probe
,
2266 .remove
= ntb_transport_free
,
2270 static int __init
ntb_transport_init(void)
2274 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC
, NTB_TRANSPORT_VER
);
2276 if (debugfs_initialized())
2277 nt_debugfs_dir
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
2279 rc
= bus_register(&ntb_transport_bus
);
2283 rc
= ntb_register_client(&ntb_transport_client
);
2290 bus_unregister(&ntb_transport_bus
);
2292 debugfs_remove_recursive(nt_debugfs_dir
);
2295 module_init(ntb_transport_init
);
2297 static void __exit
ntb_transport_exit(void)
2299 ntb_unregister_client(&ntb_transport_client
);
2300 bus_unregister(&ntb_transport_bus
);
2301 debugfs_remove_recursive(nt_debugfs_dir
);
2303 module_exit(ntb_transport_exit
);