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
;
201 struct ntb_transport_client_dev
{
202 struct list_head entry
;
203 struct ntb_transport_ctx
*nt
;
207 struct ntb_transport_ctx
{
208 struct list_head entry
;
209 struct list_head client_devs
;
211 struct ntb_dev
*ndev
;
213 struct ntb_transport_mw
*mw_vec
;
214 struct ntb_transport_qp
*qp_vec
;
215 unsigned int mw_count
;
216 unsigned int qp_count
;
221 struct delayed_work link_work
;
222 struct work_struct link_cleanup
;
224 struct dentry
*debugfs_node_dir
;
228 DESC_DONE_FLAG
= BIT(0),
229 LINK_DOWN_FLAG
= BIT(1),
232 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
257 static void ntb_transport_rxc_db(unsigned long data
);
258 static const struct ntb_ctx_ops ntb_transport_ops
;
259 static struct ntb_client ntb_transport_client
;
260 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
261 struct ntb_queue_entry
*entry
);
262 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
);
263 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
);
264 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
);
267 static int ntb_transport_bus_match(struct device
*dev
,
268 struct device_driver
*drv
)
270 return !strncmp(dev_name(dev
), drv
->name
, strlen(drv
->name
));
273 static int ntb_transport_bus_probe(struct device
*dev
)
275 const struct ntb_transport_client
*client
;
280 client
= drv_client(dev
->driver
);
281 rc
= client
->probe(dev
);
288 static int ntb_transport_bus_remove(struct device
*dev
)
290 const struct ntb_transport_client
*client
;
292 client
= drv_client(dev
->driver
);
300 static struct bus_type ntb_transport_bus
= {
301 .name
= "ntb_transport",
302 .match
= ntb_transport_bus_match
,
303 .probe
= ntb_transport_bus_probe
,
304 .remove
= ntb_transport_bus_remove
,
307 static LIST_HEAD(ntb_transport_list
);
309 static int ntb_bus_init(struct ntb_transport_ctx
*nt
)
311 list_add_tail(&nt
->entry
, &ntb_transport_list
);
315 static void ntb_bus_remove(struct ntb_transport_ctx
*nt
)
317 struct ntb_transport_client_dev
*client_dev
, *cd
;
319 list_for_each_entry_safe(client_dev
, cd
, &nt
->client_devs
, entry
) {
320 dev_err(client_dev
->dev
.parent
, "%s still attached to bus, removing\n",
321 dev_name(&client_dev
->dev
));
322 list_del(&client_dev
->entry
);
323 device_unregister(&client_dev
->dev
);
326 list_del(&nt
->entry
);
329 static void ntb_transport_client_release(struct device
*dev
)
331 struct ntb_transport_client_dev
*client_dev
;
333 client_dev
= dev_client_dev(dev
);
338 * ntb_transport_unregister_client_dev - Unregister NTB client device
339 * @device_name: Name of NTB client device
341 * Unregister an NTB client device with the NTB transport layer
343 void ntb_transport_unregister_client_dev(char *device_name
)
345 struct ntb_transport_client_dev
*client
, *cd
;
346 struct ntb_transport_ctx
*nt
;
348 list_for_each_entry(nt
, &ntb_transport_list
, entry
)
349 list_for_each_entry_safe(client
, cd
, &nt
->client_devs
, entry
)
350 if (!strncmp(dev_name(&client
->dev
), device_name
,
351 strlen(device_name
))) {
352 list_del(&client
->entry
);
353 device_unregister(&client
->dev
);
356 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev
);
359 * ntb_transport_register_client_dev - Register NTB client device
360 * @device_name: Name of NTB client device
362 * Register an NTB client device with the NTB transport layer
364 int ntb_transport_register_client_dev(char *device_name
)
366 struct ntb_transport_client_dev
*client_dev
;
367 struct ntb_transport_ctx
*nt
;
371 if (list_empty(&ntb_transport_list
))
374 list_for_each_entry(nt
, &ntb_transport_list
, entry
) {
377 node
= dev_to_node(&nt
->ndev
->dev
);
379 client_dev
= kzalloc_node(sizeof(*client_dev
),
386 dev
= &client_dev
->dev
;
388 /* setup and register client devices */
389 dev_set_name(dev
, "%s%d", device_name
, i
);
390 dev
->bus
= &ntb_transport_bus
;
391 dev
->release
= ntb_transport_client_release
;
392 dev
->parent
= &nt
->ndev
->dev
;
394 rc
= device_register(dev
);
400 list_add_tail(&client_dev
->entry
, &nt
->client_devs
);
407 ntb_transport_unregister_client_dev(device_name
);
411 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev
);
414 * ntb_transport_register_client - Register NTB client driver
415 * @drv: NTB client driver to be registered
417 * Register an NTB client driver with the NTB transport layer
419 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
421 int ntb_transport_register_client(struct ntb_transport_client
*drv
)
423 drv
->driver
.bus
= &ntb_transport_bus
;
425 if (list_empty(&ntb_transport_list
))
428 return driver_register(&drv
->driver
);
430 EXPORT_SYMBOL_GPL(ntb_transport_register_client
);
433 * ntb_transport_unregister_client - Unregister NTB client driver
434 * @drv: NTB client driver to be unregistered
436 * Unregister an NTB client driver with the NTB transport layer
438 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
440 void ntb_transport_unregister_client(struct ntb_transport_client
*drv
)
442 driver_unregister(&drv
->driver
);
444 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client
);
446 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
449 struct ntb_transport_qp
*qp
;
451 ssize_t ret
, out_offset
, out_count
;
453 qp
= filp
->private_data
;
455 if (!qp
|| !qp
->link_is_up
)
460 buf
= kmalloc(out_count
, GFP_KERNEL
);
465 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
466 "\nNTB QP stats:\n\n");
467 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
468 "rx_bytes - \t%llu\n", qp
->rx_bytes
);
469 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
470 "rx_pkts - \t%llu\n", qp
->rx_pkts
);
471 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
472 "rx_memcpy - \t%llu\n", qp
->rx_memcpy
);
473 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
474 "rx_async - \t%llu\n", qp
->rx_async
);
475 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
476 "rx_ring_empty - %llu\n", qp
->rx_ring_empty
);
477 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
478 "rx_err_no_buf - %llu\n", qp
->rx_err_no_buf
);
479 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
480 "rx_err_oflow - \t%llu\n", qp
->rx_err_oflow
);
481 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
482 "rx_err_ver - \t%llu\n", qp
->rx_err_ver
);
483 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
484 "rx_buff - \t0x%p\n", qp
->rx_buff
);
485 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
486 "rx_index - \t%u\n", qp
->rx_index
);
487 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
488 "rx_max_entry - \t%u\n", qp
->rx_max_entry
);
489 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
490 "rx_alloc_entry - \t%u\n\n", qp
->rx_alloc_entry
);
492 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
493 "tx_bytes - \t%llu\n", qp
->tx_bytes
);
494 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
495 "tx_pkts - \t%llu\n", qp
->tx_pkts
);
496 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
497 "tx_memcpy - \t%llu\n", qp
->tx_memcpy
);
498 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
499 "tx_async - \t%llu\n", qp
->tx_async
);
500 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
501 "tx_ring_full - \t%llu\n", qp
->tx_ring_full
);
502 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
503 "tx_err_no_buf - %llu\n", qp
->tx_err_no_buf
);
504 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
505 "tx_mw - \t0x%p\n", qp
->tx_mw
);
506 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
507 "tx_index (H) - \t%u\n", qp
->tx_index
);
508 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
510 qp
->remote_rx_info
->entry
);
511 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
512 "tx_max_entry - \t%u\n", qp
->tx_max_entry
);
513 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
515 ntb_transport_tx_free_entry(qp
));
517 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
519 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
520 "Using TX DMA - \t%s\n",
521 qp
->tx_dma_chan
? "Yes" : "No");
522 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
523 "Using RX DMA - \t%s\n",
524 qp
->rx_dma_chan
? "Yes" : "No");
525 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
527 qp
->link_is_up
? "Up" : "Down");
528 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
531 if (out_offset
> out_count
)
532 out_offset
= out_count
;
534 ret
= simple_read_from_buffer(ubuf
, count
, offp
, buf
, out_offset
);
539 static const struct file_operations ntb_qp_debugfs_stats
= {
540 .owner
= THIS_MODULE
,
542 .read
= debugfs_read
,
545 static void ntb_list_add(spinlock_t
*lock
, struct list_head
*entry
,
546 struct list_head
*list
)
550 spin_lock_irqsave(lock
, flags
);
551 list_add_tail(entry
, list
);
552 spin_unlock_irqrestore(lock
, flags
);
555 static struct ntb_queue_entry
*ntb_list_rm(spinlock_t
*lock
,
556 struct list_head
*list
)
558 struct ntb_queue_entry
*entry
;
561 spin_lock_irqsave(lock
, flags
);
562 if (list_empty(list
)) {
566 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
567 list_del(&entry
->entry
);
570 spin_unlock_irqrestore(lock
, flags
);
575 static struct ntb_queue_entry
*ntb_list_mv(spinlock_t
*lock
,
576 struct list_head
*list
,
577 struct list_head
*to_list
)
579 struct ntb_queue_entry
*entry
;
582 spin_lock_irqsave(lock
, flags
);
584 if (list_empty(list
)) {
587 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
588 list_move_tail(&entry
->entry
, to_list
);
591 spin_unlock_irqrestore(lock
, flags
);
596 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx
*nt
,
599 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[qp_num
];
600 struct ntb_transport_mw
*mw
;
601 struct ntb_dev
*ndev
= nt
->ndev
;
602 struct ntb_queue_entry
*entry
;
603 unsigned int rx_size
, num_qps_mw
;
604 unsigned int mw_num
, mw_count
, qp_count
;
608 mw_count
= nt
->mw_count
;
609 qp_count
= nt
->qp_count
;
611 mw_num
= QP_TO_MW(nt
, qp_num
);
612 mw
= &nt
->mw_vec
[mw_num
];
617 if (mw_num
< qp_count
% mw_count
)
618 num_qps_mw
= qp_count
/ mw_count
+ 1;
620 num_qps_mw
= qp_count
/ mw_count
;
622 rx_size
= (unsigned int)mw
->xlat_size
/ num_qps_mw
;
623 qp
->rx_buff
= mw
->virt_addr
+ rx_size
* (qp_num
/ mw_count
);
624 rx_size
-= sizeof(struct ntb_rx_info
);
626 qp
->remote_rx_info
= qp
->rx_buff
+ rx_size
;
628 /* Due to housekeeping, there must be atleast 2 buffs */
629 qp
->rx_max_frame
= min(transport_mtu
, rx_size
/ 2);
630 qp
->rx_max_entry
= rx_size
/ qp
->rx_max_frame
;
634 * Checking to see if we have more entries than the default.
635 * We should add additional entries if that is the case so we
636 * can be in sync with the transport frames.
638 node
= dev_to_node(&ndev
->dev
);
639 for (i
= qp
->rx_alloc_entry
; i
< qp
->rx_max_entry
; i
++) {
640 entry
= kzalloc_node(sizeof(*entry
), GFP_KERNEL
, node
);
645 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
647 qp
->rx_alloc_entry
++;
650 qp
->remote_rx_info
->entry
= qp
->rx_max_entry
- 1;
652 /* setup the hdr offsets with 0's */
653 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
654 void *offset
= (qp
->rx_buff
+ qp
->rx_max_frame
* (i
+ 1) -
655 sizeof(struct ntb_payload_header
));
656 memset(offset
, 0, sizeof(struct ntb_payload_header
));
666 static void ntb_free_mw(struct ntb_transport_ctx
*nt
, int num_mw
)
668 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
669 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
674 ntb_mw_clear_trans(nt
->ndev
, PIDX
, num_mw
);
675 dma_free_coherent(&pdev
->dev
, mw
->buff_size
,
676 mw
->virt_addr
, mw
->dma_addr
);
679 mw
->virt_addr
= NULL
;
682 static int ntb_set_mw(struct ntb_transport_ctx
*nt
, int num_mw
,
683 resource_size_t size
)
685 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
686 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
687 size_t xlat_size
, buff_size
;
688 resource_size_t xlat_align
;
689 resource_size_t xlat_align_size
;
695 rc
= ntb_mw_get_align(nt
->ndev
, PIDX
, num_mw
, &xlat_align
,
696 &xlat_align_size
, NULL
);
700 xlat_size
= round_up(size
, xlat_align_size
);
701 buff_size
= round_up(size
, xlat_align
);
703 /* No need to re-setup */
704 if (mw
->xlat_size
== xlat_size
)
708 ntb_free_mw(nt
, num_mw
);
710 /* Alloc memory for receiving data. Must be aligned */
711 mw
->xlat_size
= xlat_size
;
712 mw
->buff_size
= buff_size
;
714 mw
->virt_addr
= dma_alloc_coherent(&pdev
->dev
, buff_size
,
715 &mw
->dma_addr
, GFP_KERNEL
);
716 if (!mw
->virt_addr
) {
719 dev_err(&pdev
->dev
, "Unable to alloc MW buff of size %zu\n",
725 * we must ensure that the memory address allocated is BAR size
726 * aligned in order for the XLAT register to take the value. This
727 * is a requirement of the hardware. It is recommended to setup CMA
728 * for BAR sizes equal or greater than 4MB.
730 if (!IS_ALIGNED(mw
->dma_addr
, xlat_align
)) {
731 dev_err(&pdev
->dev
, "DMA memory %pad is not aligned\n",
733 ntb_free_mw(nt
, num_mw
);
737 /* Notify HW the memory location of the receive buffer */
738 rc
= ntb_mw_set_trans(nt
->ndev
, PIDX
, num_mw
, mw
->dma_addr
,
741 dev_err(&pdev
->dev
, "Unable to set mw%d translation", num_mw
);
742 ntb_free_mw(nt
, num_mw
);
749 static void ntb_qp_link_down_reset(struct ntb_transport_qp
*qp
)
751 qp
->link_is_up
= false;
758 qp
->rx_ring_empty
= 0;
759 qp
->rx_err_no_buf
= 0;
760 qp
->rx_err_oflow
= 0;
766 qp
->tx_ring_full
= 0;
767 qp
->tx_err_no_buf
= 0;
772 static void ntb_qp_link_cleanup(struct ntb_transport_qp
*qp
)
774 struct ntb_transport_ctx
*nt
= qp
->transport
;
775 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
777 dev_info(&pdev
->dev
, "qp %d: Link Cleanup\n", qp
->qp_num
);
779 cancel_delayed_work_sync(&qp
->link_work
);
780 ntb_qp_link_down_reset(qp
);
782 if (qp
->event_handler
)
783 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
786 static void ntb_qp_link_cleanup_work(struct work_struct
*work
)
788 struct ntb_transport_qp
*qp
= container_of(work
,
789 struct ntb_transport_qp
,
791 struct ntb_transport_ctx
*nt
= qp
->transport
;
793 ntb_qp_link_cleanup(qp
);
796 schedule_delayed_work(&qp
->link_work
,
797 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
800 static void ntb_qp_link_down(struct ntb_transport_qp
*qp
)
802 schedule_work(&qp
->link_cleanup
);
805 static void ntb_transport_link_cleanup(struct ntb_transport_ctx
*nt
)
807 struct ntb_transport_qp
*qp
;
809 unsigned int i
, count
;
811 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
813 /* Pass along the info to any clients */
814 for (i
= 0; i
< nt
->qp_count
; i
++)
815 if (qp_bitmap_alloc
& BIT_ULL(i
)) {
817 ntb_qp_link_cleanup(qp
);
818 cancel_work_sync(&qp
->link_cleanup
);
819 cancel_delayed_work_sync(&qp
->link_work
);
823 cancel_delayed_work_sync(&nt
->link_work
);
825 /* The scratchpad registers keep the values if the remote side
826 * goes down, blast them now to give them a sane value the next
827 * time they are accessed
829 count
= ntb_spad_count(nt
->ndev
);
830 for (i
= 0; i
< count
; i
++)
831 ntb_spad_write(nt
->ndev
, i
, 0);
834 static void ntb_transport_link_cleanup_work(struct work_struct
*work
)
836 struct ntb_transport_ctx
*nt
=
837 container_of(work
, struct ntb_transport_ctx
, link_cleanup
);
839 ntb_transport_link_cleanup(nt
);
842 static void ntb_transport_event_callback(void *data
)
844 struct ntb_transport_ctx
*nt
= data
;
846 if (ntb_link_is_up(nt
->ndev
, NULL
, NULL
) == 1)
847 schedule_delayed_work(&nt
->link_work
, 0);
849 schedule_work(&nt
->link_cleanup
);
852 static void ntb_transport_link_work(struct work_struct
*work
)
854 struct ntb_transport_ctx
*nt
=
855 container_of(work
, struct ntb_transport_ctx
, link_work
.work
);
856 struct ntb_dev
*ndev
= nt
->ndev
;
857 struct pci_dev
*pdev
= ndev
->pdev
;
858 resource_size_t size
;
862 /* send the local info, in the opposite order of the way we read it */
863 for (i
= 0; i
< nt
->mw_count
; i
++) {
864 size
= nt
->mw_vec
[i
].phys_size
;
866 if (max_mw_size
&& size
> max_mw_size
)
869 spad
= MW0_SZ_HIGH
+ (i
* 2);
870 ntb_peer_spad_write(ndev
, PIDX
, spad
, upper_32_bits(size
));
872 spad
= MW0_SZ_LOW
+ (i
* 2);
873 ntb_peer_spad_write(ndev
, PIDX
, spad
, lower_32_bits(size
));
876 ntb_peer_spad_write(ndev
, PIDX
, NUM_MWS
, nt
->mw_count
);
878 ntb_peer_spad_write(ndev
, PIDX
, NUM_QPS
, nt
->qp_count
);
880 ntb_peer_spad_write(ndev
, PIDX
, VERSION
, NTB_TRANSPORT_VERSION
);
882 /* Query the remote side for its info */
883 val
= ntb_spad_read(ndev
, VERSION
);
884 dev_dbg(&pdev
->dev
, "Remote version = %d\n", val
);
885 if (val
!= NTB_TRANSPORT_VERSION
)
888 val
= ntb_spad_read(ndev
, NUM_QPS
);
889 dev_dbg(&pdev
->dev
, "Remote max number of qps = %d\n", val
);
890 if (val
!= nt
->qp_count
)
893 val
= ntb_spad_read(ndev
, NUM_MWS
);
894 dev_dbg(&pdev
->dev
, "Remote number of mws = %d\n", val
);
895 if (val
!= nt
->mw_count
)
898 for (i
= 0; i
< nt
->mw_count
; i
++) {
901 val
= ntb_spad_read(ndev
, MW0_SZ_HIGH
+ (i
* 2));
902 val64
= (u64
)val
<< 32;
904 val
= ntb_spad_read(ndev
, MW0_SZ_LOW
+ (i
* 2));
907 dev_dbg(&pdev
->dev
, "Remote MW%d size = %#llx\n", i
, val64
);
909 rc
= ntb_set_mw(nt
, i
, val64
);
914 nt
->link_is_up
= true;
916 for (i
= 0; i
< nt
->qp_count
; i
++) {
917 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[i
];
919 ntb_transport_setup_qp_mw(nt
, i
);
921 if (qp
->client_ready
)
922 schedule_delayed_work(&qp
->link_work
, 0);
928 for (i
= 0; i
< nt
->mw_count
; i
++)
931 /* if there's an actual failure, we should just bail */
936 if (ntb_link_is_up(ndev
, NULL
, NULL
) == 1)
937 schedule_delayed_work(&nt
->link_work
,
938 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
941 static void ntb_qp_link_work(struct work_struct
*work
)
943 struct ntb_transport_qp
*qp
= container_of(work
,
944 struct ntb_transport_qp
,
946 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
947 struct ntb_transport_ctx
*nt
= qp
->transport
;
950 WARN_ON(!nt
->link_is_up
);
952 val
= ntb_spad_read(nt
->ndev
, QP_LINKS
);
954 ntb_peer_spad_write(nt
->ndev
, PIDX
, QP_LINKS
, val
| BIT(qp
->qp_num
));
956 /* query remote spad for qp ready bits */
957 dev_dbg_ratelimited(&pdev
->dev
, "Remote QP link status = %x\n", val
);
959 /* See if the remote side is up */
960 if (val
& BIT(qp
->qp_num
)) {
961 dev_info(&pdev
->dev
, "qp %d: Link Up\n", qp
->qp_num
);
962 qp
->link_is_up
= true;
965 if (qp
->event_handler
)
966 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
969 tasklet_schedule(&qp
->rxc_db_work
);
970 } else if (nt
->link_is_up
)
971 schedule_delayed_work(&qp
->link_work
,
972 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
975 static int ntb_transport_init_queue(struct ntb_transport_ctx
*nt
,
978 struct ntb_transport_qp
*qp
;
980 resource_size_t mw_size
;
981 unsigned int num_qps_mw
, tx_size
;
982 unsigned int mw_num
, mw_count
, qp_count
;
985 mw_count
= nt
->mw_count
;
986 qp_count
= nt
->qp_count
;
988 mw_num
= QP_TO_MW(nt
, qp_num
);
990 qp
= &nt
->qp_vec
[qp_num
];
994 qp
->client_ready
= false;
995 qp
->event_handler
= NULL
;
996 ntb_qp_link_down_reset(qp
);
998 if (mw_num
< qp_count
% mw_count
)
999 num_qps_mw
= qp_count
/ mw_count
+ 1;
1001 num_qps_mw
= qp_count
/ mw_count
;
1003 mw_base
= nt
->mw_vec
[mw_num
].phys_addr
;
1004 mw_size
= nt
->mw_vec
[mw_num
].phys_size
;
1006 if (max_mw_size
&& mw_size
> max_mw_size
)
1007 mw_size
= max_mw_size
;
1009 tx_size
= (unsigned int)mw_size
/ num_qps_mw
;
1010 qp_offset
= tx_size
* (qp_num
/ mw_count
);
1012 qp
->tx_mw
= nt
->mw_vec
[mw_num
].vbase
+ qp_offset
;
1016 qp
->tx_mw_phys
= mw_base
+ qp_offset
;
1017 if (!qp
->tx_mw_phys
)
1020 tx_size
-= sizeof(struct ntb_rx_info
);
1021 qp
->rx_info
= qp
->tx_mw
+ tx_size
;
1023 /* Due to housekeeping, there must be atleast 2 buffs */
1024 qp
->tx_max_frame
= min(transport_mtu
, tx_size
/ 2);
1025 qp
->tx_max_entry
= tx_size
/ qp
->tx_max_frame
;
1027 if (nt
->debugfs_node_dir
) {
1028 char debugfs_name
[4];
1030 snprintf(debugfs_name
, 4, "qp%d", qp_num
);
1031 qp
->debugfs_dir
= debugfs_create_dir(debugfs_name
,
1032 nt
->debugfs_node_dir
);
1034 qp
->debugfs_stats
= debugfs_create_file("stats", S_IRUSR
,
1035 qp
->debugfs_dir
, qp
,
1036 &ntb_qp_debugfs_stats
);
1038 qp
->debugfs_dir
= NULL
;
1039 qp
->debugfs_stats
= NULL
;
1042 INIT_DELAYED_WORK(&qp
->link_work
, ntb_qp_link_work
);
1043 INIT_WORK(&qp
->link_cleanup
, ntb_qp_link_cleanup_work
);
1045 spin_lock_init(&qp
->ntb_rx_q_lock
);
1046 spin_lock_init(&qp
->ntb_tx_free_q_lock
);
1048 INIT_LIST_HEAD(&qp
->rx_post_q
);
1049 INIT_LIST_HEAD(&qp
->rx_pend_q
);
1050 INIT_LIST_HEAD(&qp
->rx_free_q
);
1051 INIT_LIST_HEAD(&qp
->tx_free_q
);
1053 tasklet_init(&qp
->rxc_db_work
, ntb_transport_rxc_db
,
1059 static int ntb_transport_probe(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1061 struct ntb_transport_ctx
*nt
;
1062 struct ntb_transport_mw
*mw
;
1063 unsigned int mw_count
, qp_count
, spad_count
, max_mw_count_for_spads
;
1068 mw_count
= ntb_peer_mw_count(ndev
);
1070 if (!ndev
->ops
->mw_set_trans
) {
1071 dev_err(&ndev
->dev
, "Inbound MW based NTB API is required\n");
1075 if (ntb_db_is_unsafe(ndev
))
1077 "doorbell is unsafe, proceed anyway...\n");
1078 if (ntb_spad_is_unsafe(ndev
))
1080 "scratchpad is unsafe, proceed anyway...\n");
1082 if (ntb_peer_port_count(ndev
) != NTB_DEF_PEER_CNT
)
1083 dev_warn(&ndev
->dev
, "Multi-port NTB devices unsupported\n");
1085 node
= dev_to_node(&ndev
->dev
);
1087 nt
= kzalloc_node(sizeof(*nt
), GFP_KERNEL
, node
);
1092 spad_count
= ntb_spad_count(ndev
);
1094 /* Limit the MW's based on the availability of scratchpads */
1096 if (spad_count
< NTB_TRANSPORT_MIN_SPADS
) {
1102 max_mw_count_for_spads
= (spad_count
- MW0_SZ_HIGH
) / 2;
1103 nt
->mw_count
= min(mw_count
, max_mw_count_for_spads
);
1105 nt
->mw_vec
= kcalloc_node(mw_count
, sizeof(*nt
->mw_vec
),
1112 for (i
= 0; i
< mw_count
; i
++) {
1113 mw
= &nt
->mw_vec
[i
];
1115 rc
= ntb_peer_mw_get_addr(ndev
, i
, &mw
->phys_addr
,
1120 mw
->vbase
= ioremap_wc(mw
->phys_addr
, mw
->phys_size
);
1128 mw
->virt_addr
= NULL
;
1132 qp_bitmap
= ntb_db_valid_mask(ndev
);
1134 qp_count
= ilog2(qp_bitmap
);
1135 if (max_num_clients
&& max_num_clients
< qp_count
)
1136 qp_count
= max_num_clients
;
1137 else if (nt
->mw_count
< qp_count
)
1138 qp_count
= nt
->mw_count
;
1140 qp_bitmap
&= BIT_ULL(qp_count
) - 1;
1142 nt
->qp_count
= qp_count
;
1143 nt
->qp_bitmap
= qp_bitmap
;
1144 nt
->qp_bitmap_free
= qp_bitmap
;
1146 nt
->qp_vec
= kcalloc_node(qp_count
, sizeof(*nt
->qp_vec
),
1153 if (nt_debugfs_dir
) {
1154 nt
->debugfs_node_dir
=
1155 debugfs_create_dir(pci_name(ndev
->pdev
),
1159 for (i
= 0; i
< qp_count
; i
++) {
1160 rc
= ntb_transport_init_queue(nt
, i
);
1165 INIT_DELAYED_WORK(&nt
->link_work
, ntb_transport_link_work
);
1166 INIT_WORK(&nt
->link_cleanup
, ntb_transport_link_cleanup_work
);
1168 rc
= ntb_set_ctx(ndev
, nt
, &ntb_transport_ops
);
1172 INIT_LIST_HEAD(&nt
->client_devs
);
1173 rc
= ntb_bus_init(nt
);
1177 nt
->link_is_up
= false;
1178 ntb_link_enable(ndev
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
1179 ntb_link_event(ndev
);
1184 ntb_clear_ctx(ndev
);
1189 mw
= &nt
->mw_vec
[i
];
1198 static void ntb_transport_free(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1200 struct ntb_transport_ctx
*nt
= ndev
->ctx
;
1201 struct ntb_transport_qp
*qp
;
1202 u64 qp_bitmap_alloc
;
1205 ntb_transport_link_cleanup(nt
);
1206 cancel_work_sync(&nt
->link_cleanup
);
1207 cancel_delayed_work_sync(&nt
->link_work
);
1209 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
1211 /* verify that all the qp's are freed */
1212 for (i
= 0; i
< nt
->qp_count
; i
++) {
1213 qp
= &nt
->qp_vec
[i
];
1214 if (qp_bitmap_alloc
& BIT_ULL(i
))
1215 ntb_transport_free_queue(qp
);
1216 debugfs_remove_recursive(qp
->debugfs_dir
);
1219 ntb_link_disable(ndev
);
1220 ntb_clear_ctx(ndev
);
1224 for (i
= nt
->mw_count
; i
--; ) {
1226 iounmap(nt
->mw_vec
[i
].vbase
);
1234 static void ntb_complete_rxc(struct ntb_transport_qp
*qp
)
1236 struct ntb_queue_entry
*entry
;
1239 unsigned long irqflags
;
1241 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1243 while (!list_empty(&qp
->rx_post_q
)) {
1244 entry
= list_first_entry(&qp
->rx_post_q
,
1245 struct ntb_queue_entry
, entry
);
1246 if (!(entry
->flags
& DESC_DONE_FLAG
))
1249 entry
->rx_hdr
->flags
= 0;
1250 iowrite32(entry
->rx_index
, &qp
->rx_info
->entry
);
1252 cb_data
= entry
->cb_data
;
1255 list_move_tail(&entry
->entry
, &qp
->rx_free_q
);
1257 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1259 if (qp
->rx_handler
&& qp
->client_ready
)
1260 qp
->rx_handler(qp
, qp
->cb_data
, cb_data
, len
);
1262 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1265 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1268 static void ntb_rx_copy_callback(void *data
,
1269 const struct dmaengine_result
*res
)
1271 struct ntb_queue_entry
*entry
= data
;
1273 /* we need to check DMA results if we are using DMA */
1275 enum dmaengine_tx_result dma_err
= res
->result
;
1278 case DMA_TRANS_READ_FAILED
:
1279 case DMA_TRANS_WRITE_FAILED
:
1281 case DMA_TRANS_ABORTED
:
1283 struct ntb_transport_qp
*qp
= entry
->qp
;
1284 void *offset
= qp
->rx_buff
+ qp
->rx_max_frame
*
1287 ntb_memcpy_rx(entry
, offset
);
1292 case DMA_TRANS_NOERROR
:
1298 entry
->flags
|= DESC_DONE_FLAG
;
1300 ntb_complete_rxc(entry
->qp
);
1303 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
)
1305 void *buf
= entry
->buf
;
1306 size_t len
= entry
->len
;
1308 memcpy(buf
, offset
, len
);
1310 /* Ensure that the data is fully copied out before clearing the flag */
1313 ntb_rx_copy_callback(entry
, NULL
);
1316 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
)
1318 struct dma_async_tx_descriptor
*txd
;
1319 struct ntb_transport_qp
*qp
= entry
->qp
;
1320 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1321 struct dma_device
*device
;
1322 size_t pay_off
, buff_off
, len
;
1323 struct dmaengine_unmap_data
*unmap
;
1324 dma_cookie_t cookie
;
1325 void *buf
= entry
->buf
;
1328 device
= chan
->device
;
1329 pay_off
= (size_t)offset
& ~PAGE_MASK
;
1330 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1332 if (!is_dma_copy_aligned(device
, pay_off
, buff_off
, len
))
1335 unmap
= dmaengine_get_unmap_data(device
->dev
, 2, GFP_NOWAIT
);
1340 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(offset
),
1341 pay_off
, len
, DMA_TO_DEVICE
);
1342 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1347 unmap
->addr
[1] = dma_map_page(device
->dev
, virt_to_page(buf
),
1348 buff_off
, len
, DMA_FROM_DEVICE
);
1349 if (dma_mapping_error(device
->dev
, unmap
->addr
[1]))
1352 unmap
->from_cnt
= 1;
1354 txd
= device
->device_prep_dma_memcpy(chan
, unmap
->addr
[1],
1355 unmap
->addr
[0], len
,
1356 DMA_PREP_INTERRUPT
);
1360 txd
->callback_result
= ntb_rx_copy_callback
;
1361 txd
->callback_param
= entry
;
1362 dma_set_unmap(txd
, unmap
);
1364 cookie
= dmaengine_submit(txd
);
1365 if (dma_submit_error(cookie
))
1368 dmaengine_unmap_put(unmap
);
1370 qp
->last_cookie
= cookie
;
1377 dmaengine_unmap_put(unmap
);
1379 dmaengine_unmap_put(unmap
);
1384 static void ntb_async_rx(struct ntb_queue_entry
*entry
, void *offset
)
1386 struct ntb_transport_qp
*qp
= entry
->qp
;
1387 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1393 if (entry
->len
< copy_bytes
)
1396 res
= ntb_async_rx_submit(entry
, offset
);
1400 if (!entry
->retries
)
1406 ntb_memcpy_rx(entry
, offset
);
1410 static int ntb_process_rxc(struct ntb_transport_qp
*qp
)
1412 struct ntb_payload_header
*hdr
;
1413 struct ntb_queue_entry
*entry
;
1416 offset
= qp
->rx_buff
+ qp
->rx_max_frame
* qp
->rx_index
;
1417 hdr
= offset
+ qp
->rx_max_frame
- sizeof(struct ntb_payload_header
);
1419 dev_dbg(&qp
->ndev
->pdev
->dev
, "qp %d: RX ver %u len %d flags %x\n",
1420 qp
->qp_num
, hdr
->ver
, hdr
->len
, hdr
->flags
);
1422 if (!(hdr
->flags
& DESC_DONE_FLAG
)) {
1423 dev_dbg(&qp
->ndev
->pdev
->dev
, "done flag not set\n");
1424 qp
->rx_ring_empty
++;
1428 if (hdr
->flags
& LINK_DOWN_FLAG
) {
1429 dev_dbg(&qp
->ndev
->pdev
->dev
, "link down flag set\n");
1430 ntb_qp_link_down(qp
);
1435 if (hdr
->ver
!= (u32
)qp
->rx_pkts
) {
1436 dev_dbg(&qp
->ndev
->pdev
->dev
,
1437 "version mismatch, expected %llu - got %u\n",
1438 qp
->rx_pkts
, hdr
->ver
);
1443 entry
= ntb_list_mv(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
, &qp
->rx_post_q
);
1445 dev_dbg(&qp
->ndev
->pdev
->dev
, "no receive buffer\n");
1446 qp
->rx_err_no_buf
++;
1450 entry
->rx_hdr
= hdr
;
1451 entry
->rx_index
= qp
->rx_index
;
1453 if (hdr
->len
> entry
->len
) {
1454 dev_dbg(&qp
->ndev
->pdev
->dev
,
1455 "receive buffer overflow! Wanted %d got %d\n",
1456 hdr
->len
, entry
->len
);
1460 entry
->flags
|= DESC_DONE_FLAG
;
1462 ntb_complete_rxc(qp
);
1464 dev_dbg(&qp
->ndev
->pdev
->dev
,
1465 "RX OK index %u ver %u size %d into buf size %d\n",
1466 qp
->rx_index
, hdr
->ver
, hdr
->len
, entry
->len
);
1468 qp
->rx_bytes
+= hdr
->len
;
1471 entry
->len
= hdr
->len
;
1473 ntb_async_rx(entry
, offset
);
1477 qp
->rx_index
%= qp
->rx_max_entry
;
1482 static void ntb_transport_rxc_db(unsigned long data
)
1484 struct ntb_transport_qp
*qp
= (void *)data
;
1487 dev_dbg(&qp
->ndev
->pdev
->dev
, "%s: doorbell %d received\n",
1488 __func__
, qp
->qp_num
);
1490 /* Limit the number of packets processed in a single interrupt to
1491 * provide fairness to others
1493 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
1494 rc
= ntb_process_rxc(qp
);
1499 if (i
&& qp
->rx_dma_chan
)
1500 dma_async_issue_pending(qp
->rx_dma_chan
);
1502 if (i
== qp
->rx_max_entry
) {
1503 /* there is more work to do */
1505 tasklet_schedule(&qp
->rxc_db_work
);
1506 } else if (ntb_db_read(qp
->ndev
) & BIT_ULL(qp
->qp_num
)) {
1507 /* the doorbell bit is set: clear it */
1508 ntb_db_clear(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1509 /* ntb_db_read ensures ntb_db_clear write is committed */
1510 ntb_db_read(qp
->ndev
);
1512 /* an interrupt may have arrived between finishing
1513 * ntb_process_rxc and clearing the doorbell bit:
1514 * there might be some more work to do.
1517 tasklet_schedule(&qp
->rxc_db_work
);
1521 static void ntb_tx_copy_callback(void *data
,
1522 const struct dmaengine_result
*res
)
1524 struct ntb_queue_entry
*entry
= data
;
1525 struct ntb_transport_qp
*qp
= entry
->qp
;
1526 struct ntb_payload_header __iomem
*hdr
= entry
->tx_hdr
;
1528 /* we need to check DMA results if we are using DMA */
1530 enum dmaengine_tx_result dma_err
= res
->result
;
1533 case DMA_TRANS_READ_FAILED
:
1534 case DMA_TRANS_WRITE_FAILED
:
1536 case DMA_TRANS_ABORTED
:
1538 void __iomem
*offset
=
1539 qp
->tx_mw
+ qp
->tx_max_frame
*
1542 /* resubmit via CPU */
1543 ntb_memcpy_tx(entry
, offset
);
1548 case DMA_TRANS_NOERROR
:
1554 iowrite32(entry
->flags
| DESC_DONE_FLAG
, &hdr
->flags
);
1556 ntb_peer_db_set(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1558 /* The entry length can only be zero if the packet is intended to be a
1559 * "link down" or similar. Since no payload is being sent in these
1560 * cases, there is nothing to add to the completion queue.
1562 if (entry
->len
> 0) {
1563 qp
->tx_bytes
+= entry
->len
;
1566 qp
->tx_handler(qp
, qp
->cb_data
, entry
->cb_data
,
1570 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
, &qp
->tx_free_q
);
1573 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
)
1575 #ifdef ARCH_HAS_NOCACHE_UACCESS
1577 * Using non-temporal mov to improve performance on non-cached
1578 * writes, even though we aren't actually copying from user space.
1580 __copy_from_user_inatomic_nocache(offset
, entry
->buf
, entry
->len
);
1582 memcpy_toio(offset
, entry
->buf
, entry
->len
);
1585 /* Ensure that the data is fully copied out before setting the flags */
1588 ntb_tx_copy_callback(entry
, NULL
);
1591 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
1592 struct ntb_queue_entry
*entry
)
1594 struct dma_async_tx_descriptor
*txd
;
1595 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1596 struct dma_device
*device
;
1597 size_t len
= entry
->len
;
1598 void *buf
= entry
->buf
;
1599 size_t dest_off
, buff_off
;
1600 struct dmaengine_unmap_data
*unmap
;
1602 dma_cookie_t cookie
;
1604 device
= chan
->device
;
1605 dest
= qp
->tx_mw_phys
+ qp
->tx_max_frame
* entry
->tx_index
;
1606 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1607 dest_off
= (size_t)dest
& ~PAGE_MASK
;
1609 if (!is_dma_copy_aligned(device
, buff_off
, dest_off
, len
))
1612 unmap
= dmaengine_get_unmap_data(device
->dev
, 1, GFP_NOWAIT
);
1617 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(buf
),
1618 buff_off
, len
, DMA_TO_DEVICE
);
1619 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1624 txd
= device
->device_prep_dma_memcpy(chan
, dest
, unmap
->addr
[0], len
,
1625 DMA_PREP_INTERRUPT
);
1629 txd
->callback_result
= ntb_tx_copy_callback
;
1630 txd
->callback_param
= entry
;
1631 dma_set_unmap(txd
, unmap
);
1633 cookie
= dmaengine_submit(txd
);
1634 if (dma_submit_error(cookie
))
1637 dmaengine_unmap_put(unmap
);
1639 dma_async_issue_pending(chan
);
1643 dmaengine_unmap_put(unmap
);
1645 dmaengine_unmap_put(unmap
);
1650 static void ntb_async_tx(struct ntb_transport_qp
*qp
,
1651 struct ntb_queue_entry
*entry
)
1653 struct ntb_payload_header __iomem
*hdr
;
1654 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1655 void __iomem
*offset
;
1658 entry
->tx_index
= qp
->tx_index
;
1659 offset
= qp
->tx_mw
+ qp
->tx_max_frame
* entry
->tx_index
;
1660 hdr
= offset
+ qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1661 entry
->tx_hdr
= hdr
;
1663 iowrite32(entry
->len
, &hdr
->len
);
1664 iowrite32((u32
)qp
->tx_pkts
, &hdr
->ver
);
1669 if (entry
->len
< copy_bytes
)
1672 res
= ntb_async_tx_submit(qp
, entry
);
1676 if (!entry
->retries
)
1682 ntb_memcpy_tx(entry
, offset
);
1686 static int ntb_process_tx(struct ntb_transport_qp
*qp
,
1687 struct ntb_queue_entry
*entry
)
1689 if (qp
->tx_index
== qp
->remote_rx_info
->entry
) {
1694 if (entry
->len
> qp
->tx_max_frame
- sizeof(struct ntb_payload_header
)) {
1696 qp
->tx_handler(qp
, qp
->cb_data
, NULL
, -EIO
);
1698 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1703 ntb_async_tx(qp
, entry
);
1706 qp
->tx_index
%= qp
->tx_max_entry
;
1713 static void ntb_send_link_down(struct ntb_transport_qp
*qp
)
1715 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
1716 struct ntb_queue_entry
*entry
;
1719 if (!qp
->link_is_up
)
1722 dev_info(&pdev
->dev
, "qp %d: Send Link Down\n", qp
->qp_num
);
1724 for (i
= 0; i
< NTB_LINK_DOWN_TIMEOUT
; i
++) {
1725 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1734 entry
->cb_data
= NULL
;
1737 entry
->flags
= LINK_DOWN_FLAG
;
1739 rc
= ntb_process_tx(qp
, entry
);
1741 dev_err(&pdev
->dev
, "ntb: QP%d unable to send linkdown msg\n",
1744 ntb_qp_link_down_reset(qp
);
1747 static bool ntb_dma_filter_fn(struct dma_chan
*chan
, void *node
)
1749 return dev_to_node(&chan
->dev
->device
) == (int)(unsigned long)node
;
1753 * ntb_transport_create_queue - Create a new NTB transport layer queue
1754 * @rx_handler: receive callback function
1755 * @tx_handler: transmit callback function
1756 * @event_handler: event callback function
1758 * Create a new NTB transport layer queue and provide the queue with a callback
1759 * routine for both transmit and receive. The receive callback routine will be
1760 * used to pass up data when the transport has received it on the queue. The
1761 * transmit callback routine will be called when the transport has completed the
1762 * transmission of the data on the queue and the data is ready to be freed.
1764 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1766 struct ntb_transport_qp
*
1767 ntb_transport_create_queue(void *data
, struct device
*client_dev
,
1768 const struct ntb_queue_handlers
*handlers
)
1770 struct ntb_dev
*ndev
;
1771 struct pci_dev
*pdev
;
1772 struct ntb_transport_ctx
*nt
;
1773 struct ntb_queue_entry
*entry
;
1774 struct ntb_transport_qp
*qp
;
1776 unsigned int free_queue
;
1777 dma_cap_mask_t dma_mask
;
1781 ndev
= dev_ntb(client_dev
->parent
);
1785 node
= dev_to_node(&ndev
->dev
);
1787 free_queue
= ffs(nt
->qp_bitmap_free
);
1791 /* decrement free_queue to make it zero based */
1794 qp
= &nt
->qp_vec
[free_queue
];
1795 qp_bit
= BIT_ULL(qp
->qp_num
);
1797 nt
->qp_bitmap_free
&= ~qp_bit
;
1800 qp
->rx_handler
= handlers
->rx_handler
;
1801 qp
->tx_handler
= handlers
->tx_handler
;
1802 qp
->event_handler
= handlers
->event_handler
;
1804 dma_cap_zero(dma_mask
);
1805 dma_cap_set(DMA_MEMCPY
, dma_mask
);
1809 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1810 (void *)(unsigned long)node
);
1811 if (!qp
->tx_dma_chan
)
1812 dev_info(&pdev
->dev
, "Unable to allocate TX DMA channel\n");
1815 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1816 (void *)(unsigned long)node
);
1817 if (!qp
->rx_dma_chan
)
1818 dev_info(&pdev
->dev
, "Unable to allocate RX DMA channel\n");
1820 qp
->tx_dma_chan
= NULL
;
1821 qp
->rx_dma_chan
= NULL
;
1824 dev_dbg(&pdev
->dev
, "Using %s memcpy for TX\n",
1825 qp
->tx_dma_chan
? "DMA" : "CPU");
1827 dev_dbg(&pdev
->dev
, "Using %s memcpy for RX\n",
1828 qp
->rx_dma_chan
? "DMA" : "CPU");
1830 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1831 entry
= kzalloc_node(sizeof(*entry
), GFP_KERNEL
, node
);
1836 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
1839 qp
->rx_alloc_entry
= NTB_QP_DEF_NUM_ENTRIES
;
1841 for (i
= 0; i
< qp
->tx_max_entry
; i
++) {
1842 entry
= kzalloc_node(sizeof(*entry
), GFP_KERNEL
, node
);
1847 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1851 ntb_db_clear(qp
->ndev
, qp_bit
);
1852 ntb_db_clear_mask(qp
->ndev
, qp_bit
);
1854 dev_info(&pdev
->dev
, "NTB Transport QP %d created\n", qp
->qp_num
);
1859 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1862 qp
->rx_alloc_entry
= 0;
1863 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1865 if (qp
->tx_dma_chan
)
1866 dma_release_channel(qp
->tx_dma_chan
);
1867 if (qp
->rx_dma_chan
)
1868 dma_release_channel(qp
->rx_dma_chan
);
1869 nt
->qp_bitmap_free
|= qp_bit
;
1873 EXPORT_SYMBOL_GPL(ntb_transport_create_queue
);
1876 * ntb_transport_free_queue - Frees NTB transport queue
1877 * @qp: NTB queue to be freed
1879 * Frees NTB transport queue
1881 void ntb_transport_free_queue(struct ntb_transport_qp
*qp
)
1883 struct pci_dev
*pdev
;
1884 struct ntb_queue_entry
*entry
;
1890 pdev
= qp
->ndev
->pdev
;
1894 if (qp
->tx_dma_chan
) {
1895 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1896 /* Putting the dma_chan to NULL will force any new traffic to be
1897 * processed by the CPU instead of the DAM engine
1899 qp
->tx_dma_chan
= NULL
;
1901 /* Try to be nice and wait for any queued DMA engine
1902 * transactions to process before smashing it with a rock
1904 dma_sync_wait(chan
, qp
->last_cookie
);
1905 dmaengine_terminate_all(chan
);
1906 dma_release_channel(chan
);
1909 if (qp
->rx_dma_chan
) {
1910 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1911 /* Putting the dma_chan to NULL will force any new traffic to be
1912 * processed by the CPU instead of the DAM engine
1914 qp
->rx_dma_chan
= NULL
;
1916 /* Try to be nice and wait for any queued DMA engine
1917 * transactions to process before smashing it with a rock
1919 dma_sync_wait(chan
, qp
->last_cookie
);
1920 dmaengine_terminate_all(chan
);
1921 dma_release_channel(chan
);
1924 qp_bit
= BIT_ULL(qp
->qp_num
);
1926 ntb_db_set_mask(qp
->ndev
, qp_bit
);
1927 tasklet_kill(&qp
->rxc_db_work
);
1929 cancel_delayed_work_sync(&qp
->link_work
);
1932 qp
->rx_handler
= NULL
;
1933 qp
->tx_handler
= NULL
;
1934 qp
->event_handler
= NULL
;
1936 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1939 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
))) {
1940 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_pend_q\n");
1944 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_post_q
))) {
1945 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_post_q\n");
1949 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1952 qp
->transport
->qp_bitmap_free
|= qp_bit
;
1954 dev_info(&pdev
->dev
, "NTB Transport QP %d freed\n", qp
->qp_num
);
1956 EXPORT_SYMBOL_GPL(ntb_transport_free_queue
);
1959 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1960 * @qp: NTB queue to be freed
1961 * @len: pointer to variable to write enqueued buffers length
1963 * Dequeues unused buffers from receive queue. Should only be used during
1966 * RETURNS: NULL error value on error, or void* for success.
1968 void *ntb_transport_rx_remove(struct ntb_transport_qp
*qp
, unsigned int *len
)
1970 struct ntb_queue_entry
*entry
;
1973 if (!qp
|| qp
->client_ready
)
1976 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
);
1980 buf
= entry
->cb_data
;
1983 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
1987 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove
);
1990 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1991 * @qp: NTB transport layer queue the entry is to be enqueued on
1992 * @cb: per buffer pointer for callback function to use
1993 * @data: pointer to data buffer that incoming packets will be copied into
1994 * @len: length of the data buffer
1996 * Enqueue a new receive buffer onto the transport queue into which a NTB
1997 * payload can be received into.
1999 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2001 int ntb_transport_rx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
2004 struct ntb_queue_entry
*entry
;
2009 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
);
2013 entry
->cb_data
= cb
;
2019 entry
->rx_index
= 0;
2021 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
2024 tasklet_schedule(&qp
->rxc_db_work
);
2028 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue
);
2031 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2032 * @qp: NTB transport layer queue the entry is to be enqueued on
2033 * @cb: per buffer pointer for callback function to use
2034 * @data: pointer to data buffer that will be sent
2035 * @len: length of the data buffer
2037 * Enqueue a new transmit buffer onto the transport queue from which a NTB
2038 * payload will be transmitted. This assumes that a lock is being held to
2039 * serialize access to the qp.
2041 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2043 int ntb_transport_tx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
2046 struct ntb_queue_entry
*entry
;
2049 if (!qp
|| !qp
->link_is_up
|| !len
)
2052 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
2054 qp
->tx_err_no_buf
++;
2058 entry
->cb_data
= cb
;
2064 entry
->tx_index
= 0;
2066 rc
= ntb_process_tx(qp
, entry
);
2068 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
2073 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue
);
2076 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2077 * @qp: NTB transport layer queue to be enabled
2079 * Notify NTB transport layer of client readiness to use queue
2081 void ntb_transport_link_up(struct ntb_transport_qp
*qp
)
2086 qp
->client_ready
= true;
2088 if (qp
->transport
->link_is_up
)
2089 schedule_delayed_work(&qp
->link_work
, 0);
2091 EXPORT_SYMBOL_GPL(ntb_transport_link_up
);
2094 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2095 * @qp: NTB transport layer queue to be disabled
2097 * Notify NTB transport layer of client's desire to no longer receive data on
2098 * transport queue specified. It is the client's responsibility to ensure all
2099 * entries on queue are purged or otherwise handled appropriately.
2101 void ntb_transport_link_down(struct ntb_transport_qp
*qp
)
2108 qp
->client_ready
= false;
2110 val
= ntb_spad_read(qp
->ndev
, QP_LINKS
);
2112 ntb_peer_spad_write(qp
->ndev
, PIDX
, QP_LINKS
, val
& ~BIT(qp
->qp_num
));
2115 ntb_send_link_down(qp
);
2117 cancel_delayed_work_sync(&qp
->link_work
);
2119 EXPORT_SYMBOL_GPL(ntb_transport_link_down
);
2122 * ntb_transport_link_query - Query transport link state
2123 * @qp: NTB transport layer queue to be queried
2125 * Query connectivity to the remote system of the NTB transport queue
2127 * RETURNS: true for link up or false for link down
2129 bool ntb_transport_link_query(struct ntb_transport_qp
*qp
)
2134 return qp
->link_is_up
;
2136 EXPORT_SYMBOL_GPL(ntb_transport_link_query
);
2139 * ntb_transport_qp_num - Query the qp number
2140 * @qp: NTB transport layer queue to be queried
2142 * Query qp number of the NTB transport queue
2144 * RETURNS: a zero based number specifying the qp number
2146 unsigned char ntb_transport_qp_num(struct ntb_transport_qp
*qp
)
2153 EXPORT_SYMBOL_GPL(ntb_transport_qp_num
);
2156 * ntb_transport_max_size - Query the max payload size of a qp
2157 * @qp: NTB transport layer queue to be queried
2159 * Query the maximum payload size permissible on the given qp
2161 * RETURNS: the max payload size of a qp
2163 unsigned int ntb_transport_max_size(struct ntb_transport_qp
*qp
)
2165 unsigned int max_size
;
2166 unsigned int copy_align
;
2167 struct dma_chan
*rx_chan
, *tx_chan
;
2172 rx_chan
= qp
->rx_dma_chan
;
2173 tx_chan
= qp
->tx_dma_chan
;
2175 copy_align
= max(rx_chan
? rx_chan
->device
->copy_align
: 0,
2176 tx_chan
? tx_chan
->device
->copy_align
: 0);
2178 /* If DMA engine usage is possible, try to find the max size for that */
2179 max_size
= qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
2180 max_size
= round_down(max_size
, 1 << copy_align
);
2184 EXPORT_SYMBOL_GPL(ntb_transport_max_size
);
2186 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp
*qp
)
2188 unsigned int head
= qp
->tx_index
;
2189 unsigned int tail
= qp
->remote_rx_info
->entry
;
2191 return tail
> head
? tail
- head
: qp
->tx_max_entry
+ tail
- head
;
2193 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry
);
2195 static void ntb_transport_doorbell_callback(void *data
, int vector
)
2197 struct ntb_transport_ctx
*nt
= data
;
2198 struct ntb_transport_qp
*qp
;
2200 unsigned int qp_num
;
2202 db_bits
= (nt
->qp_bitmap
& ~nt
->qp_bitmap_free
&
2203 ntb_db_vector_mask(nt
->ndev
, vector
));
2206 qp_num
= __ffs(db_bits
);
2207 qp
= &nt
->qp_vec
[qp_num
];
2210 tasklet_schedule(&qp
->rxc_db_work
);
2212 db_bits
&= ~BIT_ULL(qp_num
);
2216 static const struct ntb_ctx_ops ntb_transport_ops
= {
2217 .link_event
= ntb_transport_event_callback
,
2218 .db_event
= ntb_transport_doorbell_callback
,
2221 static struct ntb_client ntb_transport_client
= {
2223 .probe
= ntb_transport_probe
,
2224 .remove
= ntb_transport_free
,
2228 static int __init
ntb_transport_init(void)
2232 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC
, NTB_TRANSPORT_VER
);
2234 if (debugfs_initialized())
2235 nt_debugfs_dir
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
2237 rc
= bus_register(&ntb_transport_bus
);
2241 rc
= ntb_register_client(&ntb_transport_client
);
2248 bus_unregister(&ntb_transport_bus
);
2250 debugfs_remove_recursive(nt_debugfs_dir
);
2253 module_init(ntb_transport_init
);
2255 static void __exit
ntb_transport_exit(void)
2257 ntb_unregister_client(&ntb_transport_client
);
2258 bus_unregister(&ntb_transport_bus
);
2259 debugfs_remove_recursive(nt_debugfs_dir
);
2261 module_exit(ntb_transport_exit
);