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 struct ntb_queue_entry
{
99 /* ntb_queue list reference */
100 struct list_head entry
;
101 /* pointers to data to be transferred */
108 unsigned int tx_index
;
109 unsigned int rx_index
;
111 struct ntb_transport_qp
*qp
;
113 struct ntb_payload_header __iomem
*tx_hdr
;
114 struct ntb_payload_header
*rx_hdr
;
122 struct ntb_transport_qp
{
123 struct ntb_transport_ctx
*transport
;
124 struct ntb_dev
*ndev
;
126 struct dma_chan
*tx_dma_chan
;
127 struct dma_chan
*rx_dma_chan
;
133 u8 qp_num
; /* Only 64 QP's are allowed. 0-63 */
136 struct ntb_rx_info __iomem
*rx_info
;
137 struct ntb_rx_info
*remote_rx_info
;
139 void (*tx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
140 void *data
, int len
);
141 struct list_head tx_free_q
;
142 spinlock_t ntb_tx_free_q_lock
;
144 dma_addr_t tx_mw_phys
;
145 unsigned int tx_index
;
146 unsigned int tx_max_entry
;
147 unsigned int tx_max_frame
;
149 void (*rx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
150 void *data
, int len
);
151 struct list_head rx_post_q
;
152 struct list_head rx_pend_q
;
153 struct list_head rx_free_q
;
154 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
155 spinlock_t ntb_rx_q_lock
;
157 unsigned int rx_index
;
158 unsigned int rx_max_entry
;
159 unsigned int rx_max_frame
;
160 unsigned int rx_alloc_entry
;
161 dma_cookie_t last_cookie
;
162 struct tasklet_struct rxc_db_work
;
164 void (*event_handler
)(void *data
, int status
);
165 struct delayed_work link_work
;
166 struct work_struct link_cleanup
;
168 struct dentry
*debugfs_dir
;
169 struct dentry
*debugfs_stats
;
188 struct ntb_transport_mw
{
189 phys_addr_t phys_addr
;
190 resource_size_t phys_size
;
191 resource_size_t xlat_align
;
192 resource_size_t xlat_align_size
;
200 struct ntb_transport_client_dev
{
201 struct list_head entry
;
202 struct ntb_transport_ctx
*nt
;
206 struct ntb_transport_ctx
{
207 struct list_head entry
;
208 struct list_head client_devs
;
210 struct ntb_dev
*ndev
;
212 struct ntb_transport_mw
*mw_vec
;
213 struct ntb_transport_qp
*qp_vec
;
214 unsigned int mw_count
;
215 unsigned int qp_count
;
220 struct delayed_work link_work
;
221 struct work_struct link_cleanup
;
223 struct dentry
*debugfs_node_dir
;
227 DESC_DONE_FLAG
= BIT(0),
228 LINK_DOWN_FLAG
= BIT(1),
231 struct ntb_payload_header
{
246 #define dev_client_dev(__dev) \
247 container_of((__dev), struct ntb_transport_client_dev, dev)
249 #define drv_client(__drv) \
250 container_of((__drv), struct ntb_transport_client, driver)
252 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
253 #define NTB_QP_DEF_NUM_ENTRIES 100
254 #define NTB_LINK_DOWN_TIMEOUT 10
256 static void ntb_transport_rxc_db(unsigned long data
);
257 static const struct ntb_ctx_ops ntb_transport_ops
;
258 static struct ntb_client ntb_transport_client
;
259 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
260 struct ntb_queue_entry
*entry
);
261 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
);
262 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
);
263 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
);
266 static int ntb_transport_bus_match(struct device
*dev
,
267 struct device_driver
*drv
)
269 return !strncmp(dev_name(dev
), drv
->name
, strlen(drv
->name
));
272 static int ntb_transport_bus_probe(struct device
*dev
)
274 const struct ntb_transport_client
*client
;
279 client
= drv_client(dev
->driver
);
280 rc
= client
->probe(dev
);
287 static int ntb_transport_bus_remove(struct device
*dev
)
289 const struct ntb_transport_client
*client
;
291 client
= drv_client(dev
->driver
);
299 static struct bus_type ntb_transport_bus
= {
300 .name
= "ntb_transport",
301 .match
= ntb_transport_bus_match
,
302 .probe
= ntb_transport_bus_probe
,
303 .remove
= ntb_transport_bus_remove
,
306 static LIST_HEAD(ntb_transport_list
);
308 static int ntb_bus_init(struct ntb_transport_ctx
*nt
)
310 list_add_tail(&nt
->entry
, &ntb_transport_list
);
314 static void ntb_bus_remove(struct ntb_transport_ctx
*nt
)
316 struct ntb_transport_client_dev
*client_dev
, *cd
;
318 list_for_each_entry_safe(client_dev
, cd
, &nt
->client_devs
, entry
) {
319 dev_err(client_dev
->dev
.parent
, "%s still attached to bus, removing\n",
320 dev_name(&client_dev
->dev
));
321 list_del(&client_dev
->entry
);
322 device_unregister(&client_dev
->dev
);
325 list_del(&nt
->entry
);
328 static void ntb_transport_client_release(struct device
*dev
)
330 struct ntb_transport_client_dev
*client_dev
;
332 client_dev
= dev_client_dev(dev
);
337 * ntb_transport_unregister_client_dev - Unregister NTB client device
338 * @device_name: Name of NTB client device
340 * Unregister an NTB client device with the NTB transport layer
342 void ntb_transport_unregister_client_dev(char *device_name
)
344 struct ntb_transport_client_dev
*client
, *cd
;
345 struct ntb_transport_ctx
*nt
;
347 list_for_each_entry(nt
, &ntb_transport_list
, entry
)
348 list_for_each_entry_safe(client
, cd
, &nt
->client_devs
, entry
)
349 if (!strncmp(dev_name(&client
->dev
), device_name
,
350 strlen(device_name
))) {
351 list_del(&client
->entry
);
352 device_unregister(&client
->dev
);
355 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev
);
358 * ntb_transport_register_client_dev - Register NTB client device
359 * @device_name: Name of NTB client device
361 * Register an NTB client device with the NTB transport layer
363 int ntb_transport_register_client_dev(char *device_name
)
365 struct ntb_transport_client_dev
*client_dev
;
366 struct ntb_transport_ctx
*nt
;
370 if (list_empty(&ntb_transport_list
))
373 list_for_each_entry(nt
, &ntb_transport_list
, entry
) {
376 node
= dev_to_node(&nt
->ndev
->dev
);
378 client_dev
= kzalloc_node(sizeof(*client_dev
),
385 dev
= &client_dev
->dev
;
387 /* setup and register client devices */
388 dev_set_name(dev
, "%s%d", device_name
, i
);
389 dev
->bus
= &ntb_transport_bus
;
390 dev
->release
= ntb_transport_client_release
;
391 dev
->parent
= &nt
->ndev
->dev
;
393 rc
= device_register(dev
);
399 list_add_tail(&client_dev
->entry
, &nt
->client_devs
);
406 ntb_transport_unregister_client_dev(device_name
);
410 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev
);
413 * ntb_transport_register_client - Register NTB client driver
414 * @drv: NTB client driver to be registered
416 * Register an NTB client driver with the NTB transport layer
418 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
420 int ntb_transport_register_client(struct ntb_transport_client
*drv
)
422 drv
->driver
.bus
= &ntb_transport_bus
;
424 if (list_empty(&ntb_transport_list
))
427 return driver_register(&drv
->driver
);
429 EXPORT_SYMBOL_GPL(ntb_transport_register_client
);
432 * ntb_transport_unregister_client - Unregister NTB client driver
433 * @drv: NTB client driver to be unregistered
435 * Unregister an NTB client driver with the NTB transport layer
437 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
439 void ntb_transport_unregister_client(struct ntb_transport_client
*drv
)
441 driver_unregister(&drv
->driver
);
443 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client
);
445 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
448 struct ntb_transport_qp
*qp
;
450 ssize_t ret
, out_offset
, out_count
;
452 qp
= filp
->private_data
;
454 if (!qp
|| !qp
->link_is_up
)
459 buf
= kmalloc(out_count
, GFP_KERNEL
);
464 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
465 "\nNTB QP stats:\n\n");
466 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
467 "rx_bytes - \t%llu\n", qp
->rx_bytes
);
468 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
469 "rx_pkts - \t%llu\n", qp
->rx_pkts
);
470 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
471 "rx_memcpy - \t%llu\n", qp
->rx_memcpy
);
472 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
473 "rx_async - \t%llu\n", qp
->rx_async
);
474 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
475 "rx_ring_empty - %llu\n", qp
->rx_ring_empty
);
476 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
477 "rx_err_no_buf - %llu\n", qp
->rx_err_no_buf
);
478 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
479 "rx_err_oflow - \t%llu\n", qp
->rx_err_oflow
);
480 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
481 "rx_err_ver - \t%llu\n", qp
->rx_err_ver
);
482 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
483 "rx_buff - \t0x%p\n", qp
->rx_buff
);
484 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
485 "rx_index - \t%u\n", qp
->rx_index
);
486 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
487 "rx_max_entry - \t%u\n", qp
->rx_max_entry
);
488 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
489 "rx_alloc_entry - \t%u\n\n", qp
->rx_alloc_entry
);
491 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
492 "tx_bytes - \t%llu\n", qp
->tx_bytes
);
493 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
494 "tx_pkts - \t%llu\n", qp
->tx_pkts
);
495 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
496 "tx_memcpy - \t%llu\n", qp
->tx_memcpy
);
497 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
498 "tx_async - \t%llu\n", qp
->tx_async
);
499 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
500 "tx_ring_full - \t%llu\n", qp
->tx_ring_full
);
501 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
502 "tx_err_no_buf - %llu\n", qp
->tx_err_no_buf
);
503 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
504 "tx_mw - \t0x%p\n", qp
->tx_mw
);
505 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
506 "tx_index (H) - \t%u\n", qp
->tx_index
);
507 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
509 qp
->remote_rx_info
->entry
);
510 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
511 "tx_max_entry - \t%u\n", qp
->tx_max_entry
);
512 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
514 ntb_transport_tx_free_entry(qp
));
516 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
518 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
519 "Using TX DMA - \t%s\n",
520 qp
->tx_dma_chan
? "Yes" : "No");
521 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
522 "Using RX DMA - \t%s\n",
523 qp
->rx_dma_chan
? "Yes" : "No");
524 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
526 qp
->link_is_up
? "Up" : "Down");
527 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
530 if (out_offset
> out_count
)
531 out_offset
= out_count
;
533 ret
= simple_read_from_buffer(ubuf
, count
, offp
, buf
, out_offset
);
538 static const struct file_operations ntb_qp_debugfs_stats
= {
539 .owner
= THIS_MODULE
,
541 .read
= debugfs_read
,
544 static void ntb_list_add(spinlock_t
*lock
, struct list_head
*entry
,
545 struct list_head
*list
)
549 spin_lock_irqsave(lock
, flags
);
550 list_add_tail(entry
, list
);
551 spin_unlock_irqrestore(lock
, flags
);
554 static struct ntb_queue_entry
*ntb_list_rm(spinlock_t
*lock
,
555 struct list_head
*list
)
557 struct ntb_queue_entry
*entry
;
560 spin_lock_irqsave(lock
, flags
);
561 if (list_empty(list
)) {
565 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
566 list_del(&entry
->entry
);
569 spin_unlock_irqrestore(lock
, flags
);
574 static struct ntb_queue_entry
*ntb_list_mv(spinlock_t
*lock
,
575 struct list_head
*list
,
576 struct list_head
*to_list
)
578 struct ntb_queue_entry
*entry
;
581 spin_lock_irqsave(lock
, flags
);
583 if (list_empty(list
)) {
586 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
587 list_move_tail(&entry
->entry
, to_list
);
590 spin_unlock_irqrestore(lock
, flags
);
595 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx
*nt
,
598 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[qp_num
];
599 struct ntb_transport_mw
*mw
;
600 struct ntb_dev
*ndev
= nt
->ndev
;
601 struct ntb_queue_entry
*entry
;
602 unsigned int rx_size
, num_qps_mw
;
603 unsigned int mw_num
, mw_count
, qp_count
;
607 mw_count
= nt
->mw_count
;
608 qp_count
= nt
->qp_count
;
610 mw_num
= QP_TO_MW(nt
, qp_num
);
611 mw
= &nt
->mw_vec
[mw_num
];
616 if (mw_num
< qp_count
% mw_count
)
617 num_qps_mw
= qp_count
/ mw_count
+ 1;
619 num_qps_mw
= qp_count
/ mw_count
;
621 rx_size
= (unsigned int)mw
->xlat_size
/ num_qps_mw
;
622 qp
->rx_buff
= mw
->virt_addr
+ rx_size
* (qp_num
/ mw_count
);
623 rx_size
-= sizeof(struct ntb_rx_info
);
625 qp
->remote_rx_info
= qp
->rx_buff
+ rx_size
;
627 /* Due to housekeeping, there must be atleast 2 buffs */
628 qp
->rx_max_frame
= min(transport_mtu
, rx_size
/ 2);
629 qp
->rx_max_entry
= rx_size
/ qp
->rx_max_frame
;
633 * Checking to see if we have more entries than the default.
634 * We should add additional entries if that is the case so we
635 * can be in sync with the transport frames.
637 node
= dev_to_node(&ndev
->dev
);
638 for (i
= qp
->rx_alloc_entry
; i
< qp
->rx_max_entry
; i
++) {
639 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
644 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
646 qp
->rx_alloc_entry
++;
649 qp
->remote_rx_info
->entry
= qp
->rx_max_entry
- 1;
651 /* setup the hdr offsets with 0's */
652 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
653 void *offset
= (qp
->rx_buff
+ qp
->rx_max_frame
* (i
+ 1) -
654 sizeof(struct ntb_payload_header
));
655 memset(offset
, 0, sizeof(struct ntb_payload_header
));
665 static void ntb_free_mw(struct ntb_transport_ctx
*nt
, int num_mw
)
667 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
668 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
673 ntb_mw_clear_trans(nt
->ndev
, num_mw
);
674 dma_free_coherent(&pdev
->dev
, mw
->buff_size
,
675 mw
->virt_addr
, mw
->dma_addr
);
678 mw
->virt_addr
= NULL
;
681 static int ntb_set_mw(struct ntb_transport_ctx
*nt
, int num_mw
,
682 resource_size_t size
)
684 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
685 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
686 size_t xlat_size
, buff_size
;
692 xlat_size
= round_up(size
, mw
->xlat_align_size
);
693 buff_size
= round_up(size
, mw
->xlat_align
);
695 /* No need to re-setup */
696 if (mw
->xlat_size
== xlat_size
)
700 ntb_free_mw(nt
, num_mw
);
702 /* Alloc memory for receiving data. Must be aligned */
703 mw
->xlat_size
= xlat_size
;
704 mw
->buff_size
= buff_size
;
706 mw
->virt_addr
= dma_alloc_coherent(&pdev
->dev
, buff_size
,
707 &mw
->dma_addr
, GFP_KERNEL
);
708 if (!mw
->virt_addr
) {
711 dev_err(&pdev
->dev
, "Unable to alloc MW buff of size %zu\n",
717 * we must ensure that the memory address allocated is BAR size
718 * aligned in order for the XLAT register to take the value. This
719 * is a requirement of the hardware. It is recommended to setup CMA
720 * for BAR sizes equal or greater than 4MB.
722 if (!IS_ALIGNED(mw
->dma_addr
, mw
->xlat_align
)) {
723 dev_err(&pdev
->dev
, "DMA memory %pad is not aligned\n",
725 ntb_free_mw(nt
, num_mw
);
729 /* Notify HW the memory location of the receive buffer */
730 rc
= ntb_mw_set_trans(nt
->ndev
, num_mw
, mw
->dma_addr
, mw
->xlat_size
);
732 dev_err(&pdev
->dev
, "Unable to set mw%d translation", num_mw
);
733 ntb_free_mw(nt
, num_mw
);
740 static void ntb_qp_link_down_reset(struct ntb_transport_qp
*qp
)
742 qp
->link_is_up
= false;
749 qp
->rx_ring_empty
= 0;
750 qp
->rx_err_no_buf
= 0;
751 qp
->rx_err_oflow
= 0;
757 qp
->tx_ring_full
= 0;
758 qp
->tx_err_no_buf
= 0;
763 static void ntb_qp_link_cleanup(struct ntb_transport_qp
*qp
)
765 struct ntb_transport_ctx
*nt
= qp
->transport
;
766 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
768 dev_info(&pdev
->dev
, "qp %d: Link Cleanup\n", qp
->qp_num
);
770 cancel_delayed_work_sync(&qp
->link_work
);
771 ntb_qp_link_down_reset(qp
);
773 if (qp
->event_handler
)
774 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
777 static void ntb_qp_link_cleanup_work(struct work_struct
*work
)
779 struct ntb_transport_qp
*qp
= container_of(work
,
780 struct ntb_transport_qp
,
782 struct ntb_transport_ctx
*nt
= qp
->transport
;
784 ntb_qp_link_cleanup(qp
);
787 schedule_delayed_work(&qp
->link_work
,
788 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
791 static void ntb_qp_link_down(struct ntb_transport_qp
*qp
)
793 schedule_work(&qp
->link_cleanup
);
796 static void ntb_transport_link_cleanup(struct ntb_transport_ctx
*nt
)
798 struct ntb_transport_qp
*qp
;
800 unsigned int i
, count
;
802 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
804 /* Pass along the info to any clients */
805 for (i
= 0; i
< nt
->qp_count
; i
++)
806 if (qp_bitmap_alloc
& BIT_ULL(i
)) {
808 ntb_qp_link_cleanup(qp
);
809 cancel_work_sync(&qp
->link_cleanup
);
810 cancel_delayed_work_sync(&qp
->link_work
);
814 cancel_delayed_work_sync(&nt
->link_work
);
816 /* The scratchpad registers keep the values if the remote side
817 * goes down, blast them now to give them a sane value the next
818 * time they are accessed
820 count
= ntb_spad_count(nt
->ndev
);
821 for (i
= 0; i
< count
; i
++)
822 ntb_spad_write(nt
->ndev
, i
, 0);
825 static void ntb_transport_link_cleanup_work(struct work_struct
*work
)
827 struct ntb_transport_ctx
*nt
=
828 container_of(work
, struct ntb_transport_ctx
, link_cleanup
);
830 ntb_transport_link_cleanup(nt
);
833 static void ntb_transport_event_callback(void *data
)
835 struct ntb_transport_ctx
*nt
= data
;
837 if (ntb_link_is_up(nt
->ndev
, NULL
, NULL
) == 1)
838 schedule_delayed_work(&nt
->link_work
, 0);
840 schedule_work(&nt
->link_cleanup
);
843 static void ntb_transport_link_work(struct work_struct
*work
)
845 struct ntb_transport_ctx
*nt
=
846 container_of(work
, struct ntb_transport_ctx
, link_work
.work
);
847 struct ntb_dev
*ndev
= nt
->ndev
;
848 struct pci_dev
*pdev
= ndev
->pdev
;
849 resource_size_t size
;
853 /* send the local info, in the opposite order of the way we read it */
854 for (i
= 0; i
< nt
->mw_count
; i
++) {
855 size
= nt
->mw_vec
[i
].phys_size
;
857 if (max_mw_size
&& size
> max_mw_size
)
860 spad
= MW0_SZ_HIGH
+ (i
* 2);
861 ntb_peer_spad_write(ndev
, spad
, upper_32_bits(size
));
863 spad
= MW0_SZ_LOW
+ (i
* 2);
864 ntb_peer_spad_write(ndev
, spad
, lower_32_bits(size
));
867 ntb_peer_spad_write(ndev
, NUM_MWS
, nt
->mw_count
);
869 ntb_peer_spad_write(ndev
, NUM_QPS
, nt
->qp_count
);
871 ntb_peer_spad_write(ndev
, VERSION
, NTB_TRANSPORT_VERSION
);
873 /* Query the remote side for its info */
874 val
= ntb_spad_read(ndev
, VERSION
);
875 dev_dbg(&pdev
->dev
, "Remote version = %d\n", val
);
876 if (val
!= NTB_TRANSPORT_VERSION
)
879 val
= ntb_spad_read(ndev
, NUM_QPS
);
880 dev_dbg(&pdev
->dev
, "Remote max number of qps = %d\n", val
);
881 if (val
!= nt
->qp_count
)
884 val
= ntb_spad_read(ndev
, NUM_MWS
);
885 dev_dbg(&pdev
->dev
, "Remote number of mws = %d\n", val
);
886 if (val
!= nt
->mw_count
)
889 for (i
= 0; i
< nt
->mw_count
; i
++) {
892 val
= ntb_spad_read(ndev
, MW0_SZ_HIGH
+ (i
* 2));
893 val64
= (u64
)val
<< 32;
895 val
= ntb_spad_read(ndev
, MW0_SZ_LOW
+ (i
* 2));
898 dev_dbg(&pdev
->dev
, "Remote MW%d size = %#llx\n", i
, val64
);
900 rc
= ntb_set_mw(nt
, i
, val64
);
905 nt
->link_is_up
= true;
907 for (i
= 0; i
< nt
->qp_count
; i
++) {
908 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[i
];
910 ntb_transport_setup_qp_mw(nt
, i
);
912 if (qp
->client_ready
)
913 schedule_delayed_work(&qp
->link_work
, 0);
919 for (i
= 0; i
< nt
->mw_count
; i
++)
922 /* if there's an actual failure, we should just bail */
924 ntb_link_disable(ndev
);
929 if (ntb_link_is_up(ndev
, NULL
, NULL
) == 1)
930 schedule_delayed_work(&nt
->link_work
,
931 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
934 static void ntb_qp_link_work(struct work_struct
*work
)
936 struct ntb_transport_qp
*qp
= container_of(work
,
937 struct ntb_transport_qp
,
939 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
940 struct ntb_transport_ctx
*nt
= qp
->transport
;
943 WARN_ON(!nt
->link_is_up
);
945 val
= ntb_spad_read(nt
->ndev
, QP_LINKS
);
947 ntb_peer_spad_write(nt
->ndev
, QP_LINKS
, val
| BIT(qp
->qp_num
));
949 /* query remote spad for qp ready bits */
950 dev_dbg_ratelimited(&pdev
->dev
, "Remote QP link status = %x\n", val
);
952 /* See if the remote side is up */
953 if (val
& BIT(qp
->qp_num
)) {
954 dev_info(&pdev
->dev
, "qp %d: Link Up\n", qp
->qp_num
);
955 qp
->link_is_up
= true;
958 if (qp
->event_handler
)
959 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
962 tasklet_schedule(&qp
->rxc_db_work
);
963 } else if (nt
->link_is_up
)
964 schedule_delayed_work(&qp
->link_work
,
965 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
968 static int ntb_transport_init_queue(struct ntb_transport_ctx
*nt
,
971 struct ntb_transport_qp
*qp
;
973 resource_size_t mw_size
;
974 unsigned int num_qps_mw
, tx_size
;
975 unsigned int mw_num
, mw_count
, qp_count
;
978 mw_count
= nt
->mw_count
;
979 qp_count
= nt
->qp_count
;
981 mw_num
= QP_TO_MW(nt
, qp_num
);
983 qp
= &nt
->qp_vec
[qp_num
];
987 qp
->client_ready
= false;
988 qp
->event_handler
= NULL
;
989 ntb_qp_link_down_reset(qp
);
991 if (mw_num
< qp_count
% mw_count
)
992 num_qps_mw
= qp_count
/ mw_count
+ 1;
994 num_qps_mw
= qp_count
/ mw_count
;
996 mw_base
= nt
->mw_vec
[mw_num
].phys_addr
;
997 mw_size
= nt
->mw_vec
[mw_num
].phys_size
;
999 tx_size
= (unsigned int)mw_size
/ num_qps_mw
;
1000 qp_offset
= tx_size
* (qp_num
/ mw_count
);
1002 qp
->tx_mw
= nt
->mw_vec
[mw_num
].vbase
+ qp_offset
;
1006 qp
->tx_mw_phys
= mw_base
+ qp_offset
;
1007 if (!qp
->tx_mw_phys
)
1010 tx_size
-= sizeof(struct ntb_rx_info
);
1011 qp
->rx_info
= qp
->tx_mw
+ tx_size
;
1013 /* Due to housekeeping, there must be atleast 2 buffs */
1014 qp
->tx_max_frame
= min(transport_mtu
, tx_size
/ 2);
1015 qp
->tx_max_entry
= tx_size
/ qp
->tx_max_frame
;
1017 if (nt
->debugfs_node_dir
) {
1018 char debugfs_name
[4];
1020 snprintf(debugfs_name
, 4, "qp%d", qp_num
);
1021 qp
->debugfs_dir
= debugfs_create_dir(debugfs_name
,
1022 nt
->debugfs_node_dir
);
1024 qp
->debugfs_stats
= debugfs_create_file("stats", S_IRUSR
,
1025 qp
->debugfs_dir
, qp
,
1026 &ntb_qp_debugfs_stats
);
1028 qp
->debugfs_dir
= NULL
;
1029 qp
->debugfs_stats
= NULL
;
1032 INIT_DELAYED_WORK(&qp
->link_work
, ntb_qp_link_work
);
1033 INIT_WORK(&qp
->link_cleanup
, ntb_qp_link_cleanup_work
);
1035 spin_lock_init(&qp
->ntb_rx_q_lock
);
1036 spin_lock_init(&qp
->ntb_tx_free_q_lock
);
1038 INIT_LIST_HEAD(&qp
->rx_post_q
);
1039 INIT_LIST_HEAD(&qp
->rx_pend_q
);
1040 INIT_LIST_HEAD(&qp
->rx_free_q
);
1041 INIT_LIST_HEAD(&qp
->tx_free_q
);
1043 tasklet_init(&qp
->rxc_db_work
, ntb_transport_rxc_db
,
1049 static int ntb_transport_probe(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1051 struct ntb_transport_ctx
*nt
;
1052 struct ntb_transport_mw
*mw
;
1053 unsigned int mw_count
, qp_count
, spad_count
, max_mw_count_for_spads
;
1058 mw_count
= ntb_mw_count(ndev
);
1060 if (ntb_db_is_unsafe(ndev
))
1062 "doorbell is unsafe, proceed anyway...\n");
1063 if (ntb_spad_is_unsafe(ndev
))
1065 "scratchpad is unsafe, proceed anyway...\n");
1067 node
= dev_to_node(&ndev
->dev
);
1069 nt
= kzalloc_node(sizeof(*nt
), GFP_KERNEL
, node
);
1074 spad_count
= ntb_spad_count(ndev
);
1076 /* Limit the MW's based on the availability of scratchpads */
1078 if (spad_count
< NTB_TRANSPORT_MIN_SPADS
) {
1084 max_mw_count_for_spads
= (spad_count
- MW0_SZ_HIGH
) / 2;
1085 nt
->mw_count
= min(mw_count
, max_mw_count_for_spads
);
1087 nt
->mw_vec
= kzalloc_node(mw_count
* sizeof(*nt
->mw_vec
),
1094 for (i
= 0; i
< mw_count
; i
++) {
1095 mw
= &nt
->mw_vec
[i
];
1097 rc
= ntb_mw_get_range(ndev
, i
, &mw
->phys_addr
, &mw
->phys_size
,
1098 &mw
->xlat_align
, &mw
->xlat_align_size
);
1102 mw
->vbase
= ioremap_wc(mw
->phys_addr
, mw
->phys_size
);
1110 mw
->virt_addr
= NULL
;
1114 qp_bitmap
= ntb_db_valid_mask(ndev
);
1116 qp_count
= ilog2(qp_bitmap
);
1117 if (max_num_clients
&& max_num_clients
< qp_count
)
1118 qp_count
= max_num_clients
;
1119 else if (nt
->mw_count
< qp_count
)
1120 qp_count
= nt
->mw_count
;
1122 qp_bitmap
&= BIT_ULL(qp_count
) - 1;
1124 nt
->qp_count
= qp_count
;
1125 nt
->qp_bitmap
= qp_bitmap
;
1126 nt
->qp_bitmap_free
= qp_bitmap
;
1128 nt
->qp_vec
= kzalloc_node(qp_count
* sizeof(*nt
->qp_vec
),
1135 if (nt_debugfs_dir
) {
1136 nt
->debugfs_node_dir
=
1137 debugfs_create_dir(pci_name(ndev
->pdev
),
1141 for (i
= 0; i
< qp_count
; i
++) {
1142 rc
= ntb_transport_init_queue(nt
, i
);
1147 INIT_DELAYED_WORK(&nt
->link_work
, ntb_transport_link_work
);
1148 INIT_WORK(&nt
->link_cleanup
, ntb_transport_link_cleanup_work
);
1150 rc
= ntb_set_ctx(ndev
, nt
, &ntb_transport_ops
);
1154 INIT_LIST_HEAD(&nt
->client_devs
);
1155 rc
= ntb_bus_init(nt
);
1159 nt
->link_is_up
= false;
1160 ntb_link_enable(ndev
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
1161 ntb_link_event(ndev
);
1166 ntb_clear_ctx(ndev
);
1171 mw
= &nt
->mw_vec
[i
];
1180 static void ntb_transport_free(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1182 struct ntb_transport_ctx
*nt
= ndev
->ctx
;
1183 struct ntb_transport_qp
*qp
;
1184 u64 qp_bitmap_alloc
;
1187 ntb_transport_link_cleanup(nt
);
1188 cancel_work_sync(&nt
->link_cleanup
);
1189 cancel_delayed_work_sync(&nt
->link_work
);
1191 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
1193 /* verify that all the qp's are freed */
1194 for (i
= 0; i
< nt
->qp_count
; i
++) {
1195 qp
= &nt
->qp_vec
[i
];
1196 if (qp_bitmap_alloc
& BIT_ULL(i
))
1197 ntb_transport_free_queue(qp
);
1198 debugfs_remove_recursive(qp
->debugfs_dir
);
1201 ntb_link_disable(ndev
);
1202 ntb_clear_ctx(ndev
);
1206 for (i
= nt
->mw_count
; i
--; ) {
1208 iounmap(nt
->mw_vec
[i
].vbase
);
1216 static void ntb_complete_rxc(struct ntb_transport_qp
*qp
)
1218 struct ntb_queue_entry
*entry
;
1221 unsigned long irqflags
;
1223 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1225 while (!list_empty(&qp
->rx_post_q
)) {
1226 entry
= list_first_entry(&qp
->rx_post_q
,
1227 struct ntb_queue_entry
, entry
);
1228 if (!(entry
->flags
& DESC_DONE_FLAG
))
1231 entry
->rx_hdr
->flags
= 0;
1232 iowrite32(entry
->rx_index
, &qp
->rx_info
->entry
);
1234 cb_data
= entry
->cb_data
;
1237 list_move_tail(&entry
->entry
, &qp
->rx_free_q
);
1239 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1241 if (qp
->rx_handler
&& qp
->client_ready
)
1242 qp
->rx_handler(qp
, qp
->cb_data
, cb_data
, len
);
1244 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1247 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1250 static void ntb_rx_copy_callback(void *data
,
1251 const struct dmaengine_result
*res
)
1253 struct ntb_queue_entry
*entry
= data
;
1255 /* we need to check DMA results if we are using DMA */
1257 enum dmaengine_tx_result dma_err
= res
->result
;
1260 case DMA_TRANS_READ_FAILED
:
1261 case DMA_TRANS_WRITE_FAILED
:
1263 case DMA_TRANS_ABORTED
:
1265 struct ntb_transport_qp
*qp
= entry
->qp
;
1266 void *offset
= qp
->rx_buff
+ qp
->rx_max_frame
*
1269 ntb_memcpy_rx(entry
, offset
);
1274 case DMA_TRANS_NOERROR
:
1280 entry
->flags
|= DESC_DONE_FLAG
;
1282 ntb_complete_rxc(entry
->qp
);
1285 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
)
1287 void *buf
= entry
->buf
;
1288 size_t len
= entry
->len
;
1290 memcpy(buf
, offset
, len
);
1292 /* Ensure that the data is fully copied out before clearing the flag */
1295 ntb_rx_copy_callback(entry
, NULL
);
1298 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
)
1300 struct dma_async_tx_descriptor
*txd
;
1301 struct ntb_transport_qp
*qp
= entry
->qp
;
1302 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1303 struct dma_device
*device
;
1304 size_t pay_off
, buff_off
, len
;
1305 struct dmaengine_unmap_data
*unmap
;
1306 dma_cookie_t cookie
;
1307 void *buf
= entry
->buf
;
1310 device
= chan
->device
;
1311 pay_off
= (size_t)offset
& ~PAGE_MASK
;
1312 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1314 if (!is_dma_copy_aligned(device
, pay_off
, buff_off
, len
))
1317 unmap
= dmaengine_get_unmap_data(device
->dev
, 2, GFP_NOWAIT
);
1322 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(offset
),
1323 pay_off
, len
, DMA_TO_DEVICE
);
1324 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1329 unmap
->addr
[1] = dma_map_page(device
->dev
, virt_to_page(buf
),
1330 buff_off
, len
, DMA_FROM_DEVICE
);
1331 if (dma_mapping_error(device
->dev
, unmap
->addr
[1]))
1334 unmap
->from_cnt
= 1;
1336 txd
= device
->device_prep_dma_memcpy(chan
, unmap
->addr
[1],
1337 unmap
->addr
[0], len
,
1338 DMA_PREP_INTERRUPT
);
1342 txd
->callback_result
= ntb_rx_copy_callback
;
1343 txd
->callback_param
= entry
;
1344 dma_set_unmap(txd
, unmap
);
1346 cookie
= dmaengine_submit(txd
);
1347 if (dma_submit_error(cookie
))
1350 dmaengine_unmap_put(unmap
);
1352 qp
->last_cookie
= cookie
;
1359 dmaengine_unmap_put(unmap
);
1361 dmaengine_unmap_put(unmap
);
1366 static void ntb_async_rx(struct ntb_queue_entry
*entry
, void *offset
)
1368 struct ntb_transport_qp
*qp
= entry
->qp
;
1369 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1375 if (entry
->len
< copy_bytes
)
1378 res
= ntb_async_rx_submit(entry
, offset
);
1382 if (!entry
->retries
)
1388 ntb_memcpy_rx(entry
, offset
);
1392 static int ntb_process_rxc(struct ntb_transport_qp
*qp
)
1394 struct ntb_payload_header
*hdr
;
1395 struct ntb_queue_entry
*entry
;
1398 offset
= qp
->rx_buff
+ qp
->rx_max_frame
* qp
->rx_index
;
1399 hdr
= offset
+ qp
->rx_max_frame
- sizeof(struct ntb_payload_header
);
1401 dev_dbg(&qp
->ndev
->pdev
->dev
, "qp %d: RX ver %u len %d flags %x\n",
1402 qp
->qp_num
, hdr
->ver
, hdr
->len
, hdr
->flags
);
1404 if (!(hdr
->flags
& DESC_DONE_FLAG
)) {
1405 dev_dbg(&qp
->ndev
->pdev
->dev
, "done flag not set\n");
1406 qp
->rx_ring_empty
++;
1410 if (hdr
->flags
& LINK_DOWN_FLAG
) {
1411 dev_dbg(&qp
->ndev
->pdev
->dev
, "link down flag set\n");
1412 ntb_qp_link_down(qp
);
1417 if (hdr
->ver
!= (u32
)qp
->rx_pkts
) {
1418 dev_dbg(&qp
->ndev
->pdev
->dev
,
1419 "version mismatch, expected %llu - got %u\n",
1420 qp
->rx_pkts
, hdr
->ver
);
1425 entry
= ntb_list_mv(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
, &qp
->rx_post_q
);
1427 dev_dbg(&qp
->ndev
->pdev
->dev
, "no receive buffer\n");
1428 qp
->rx_err_no_buf
++;
1432 entry
->rx_hdr
= hdr
;
1433 entry
->rx_index
= qp
->rx_index
;
1435 if (hdr
->len
> entry
->len
) {
1436 dev_dbg(&qp
->ndev
->pdev
->dev
,
1437 "receive buffer overflow! Wanted %d got %d\n",
1438 hdr
->len
, entry
->len
);
1442 entry
->flags
|= DESC_DONE_FLAG
;
1444 ntb_complete_rxc(qp
);
1446 dev_dbg(&qp
->ndev
->pdev
->dev
,
1447 "RX OK index %u ver %u size %d into buf size %d\n",
1448 qp
->rx_index
, hdr
->ver
, hdr
->len
, entry
->len
);
1450 qp
->rx_bytes
+= hdr
->len
;
1453 entry
->len
= hdr
->len
;
1455 ntb_async_rx(entry
, offset
);
1459 qp
->rx_index
%= qp
->rx_max_entry
;
1464 static void ntb_transport_rxc_db(unsigned long data
)
1466 struct ntb_transport_qp
*qp
= (void *)data
;
1469 dev_dbg(&qp
->ndev
->pdev
->dev
, "%s: doorbell %d received\n",
1470 __func__
, qp
->qp_num
);
1472 /* Limit the number of packets processed in a single interrupt to
1473 * provide fairness to others
1475 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
1476 rc
= ntb_process_rxc(qp
);
1481 if (i
&& qp
->rx_dma_chan
)
1482 dma_async_issue_pending(qp
->rx_dma_chan
);
1484 if (i
== qp
->rx_max_entry
) {
1485 /* there is more work to do */
1487 tasklet_schedule(&qp
->rxc_db_work
);
1488 } else if (ntb_db_read(qp
->ndev
) & BIT_ULL(qp
->qp_num
)) {
1489 /* the doorbell bit is set: clear it */
1490 ntb_db_clear(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1491 /* ntb_db_read ensures ntb_db_clear write is committed */
1492 ntb_db_read(qp
->ndev
);
1494 /* an interrupt may have arrived between finishing
1495 * ntb_process_rxc and clearing the doorbell bit:
1496 * there might be some more work to do.
1499 tasklet_schedule(&qp
->rxc_db_work
);
1503 static void ntb_tx_copy_callback(void *data
,
1504 const struct dmaengine_result
*res
)
1506 struct ntb_queue_entry
*entry
= data
;
1507 struct ntb_transport_qp
*qp
= entry
->qp
;
1508 struct ntb_payload_header __iomem
*hdr
= entry
->tx_hdr
;
1510 /* we need to check DMA results if we are using DMA */
1512 enum dmaengine_tx_result dma_err
= res
->result
;
1515 case DMA_TRANS_READ_FAILED
:
1516 case DMA_TRANS_WRITE_FAILED
:
1518 case DMA_TRANS_ABORTED
:
1520 void __iomem
*offset
=
1521 qp
->tx_mw
+ qp
->tx_max_frame
*
1524 /* resubmit via CPU */
1525 ntb_memcpy_tx(entry
, offset
);
1530 case DMA_TRANS_NOERROR
:
1536 iowrite32(entry
->flags
| DESC_DONE_FLAG
, &hdr
->flags
);
1538 ntb_peer_db_set(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1540 /* The entry length can only be zero if the packet is intended to be a
1541 * "link down" or similar. Since no payload is being sent in these
1542 * cases, there is nothing to add to the completion queue.
1544 if (entry
->len
> 0) {
1545 qp
->tx_bytes
+= entry
->len
;
1548 qp
->tx_handler(qp
, qp
->cb_data
, entry
->cb_data
,
1552 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
, &qp
->tx_free_q
);
1555 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
)
1557 #ifdef ARCH_HAS_NOCACHE_UACCESS
1559 * Using non-temporal mov to improve performance on non-cached
1560 * writes, even though we aren't actually copying from user space.
1562 __copy_from_user_inatomic_nocache(offset
, entry
->buf
, entry
->len
);
1564 memcpy_toio(offset
, entry
->buf
, entry
->len
);
1567 /* Ensure that the data is fully copied out before setting the flags */
1570 ntb_tx_copy_callback(entry
, NULL
);
1573 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
1574 struct ntb_queue_entry
*entry
)
1576 struct dma_async_tx_descriptor
*txd
;
1577 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1578 struct dma_device
*device
;
1579 size_t len
= entry
->len
;
1580 void *buf
= entry
->buf
;
1581 size_t dest_off
, buff_off
;
1582 struct dmaengine_unmap_data
*unmap
;
1584 dma_cookie_t cookie
;
1586 device
= chan
->device
;
1587 dest
= qp
->tx_mw_phys
+ qp
->tx_max_frame
* entry
->tx_index
;
1588 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1589 dest_off
= (size_t)dest
& ~PAGE_MASK
;
1591 if (!is_dma_copy_aligned(device
, buff_off
, dest_off
, len
))
1594 unmap
= dmaengine_get_unmap_data(device
->dev
, 1, GFP_NOWAIT
);
1599 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(buf
),
1600 buff_off
, len
, DMA_TO_DEVICE
);
1601 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1606 txd
= device
->device_prep_dma_memcpy(chan
, dest
, unmap
->addr
[0], len
,
1607 DMA_PREP_INTERRUPT
);
1611 txd
->callback_result
= ntb_tx_copy_callback
;
1612 txd
->callback_param
= entry
;
1613 dma_set_unmap(txd
, unmap
);
1615 cookie
= dmaengine_submit(txd
);
1616 if (dma_submit_error(cookie
))
1619 dmaengine_unmap_put(unmap
);
1621 dma_async_issue_pending(chan
);
1625 dmaengine_unmap_put(unmap
);
1627 dmaengine_unmap_put(unmap
);
1632 static void ntb_async_tx(struct ntb_transport_qp
*qp
,
1633 struct ntb_queue_entry
*entry
)
1635 struct ntb_payload_header __iomem
*hdr
;
1636 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1637 void __iomem
*offset
;
1640 entry
->tx_index
= qp
->tx_index
;
1641 offset
= qp
->tx_mw
+ qp
->tx_max_frame
* entry
->tx_index
;
1642 hdr
= offset
+ qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1643 entry
->tx_hdr
= hdr
;
1645 iowrite32(entry
->len
, &hdr
->len
);
1646 iowrite32((u32
)qp
->tx_pkts
, &hdr
->ver
);
1651 if (entry
->len
< copy_bytes
)
1654 res
= ntb_async_tx_submit(qp
, entry
);
1658 if (!entry
->retries
)
1664 ntb_memcpy_tx(entry
, offset
);
1668 static int ntb_process_tx(struct ntb_transport_qp
*qp
,
1669 struct ntb_queue_entry
*entry
)
1671 if (qp
->tx_index
== qp
->remote_rx_info
->entry
) {
1676 if (entry
->len
> qp
->tx_max_frame
- sizeof(struct ntb_payload_header
)) {
1678 qp
->tx_handler(qp
, qp
->cb_data
, NULL
, -EIO
);
1680 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1685 ntb_async_tx(qp
, entry
);
1688 qp
->tx_index
%= qp
->tx_max_entry
;
1695 static void ntb_send_link_down(struct ntb_transport_qp
*qp
)
1697 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
1698 struct ntb_queue_entry
*entry
;
1701 if (!qp
->link_is_up
)
1704 dev_info(&pdev
->dev
, "qp %d: Send Link Down\n", qp
->qp_num
);
1706 for (i
= 0; i
< NTB_LINK_DOWN_TIMEOUT
; i
++) {
1707 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1716 entry
->cb_data
= NULL
;
1719 entry
->flags
= LINK_DOWN_FLAG
;
1721 rc
= ntb_process_tx(qp
, entry
);
1723 dev_err(&pdev
->dev
, "ntb: QP%d unable to send linkdown msg\n",
1726 ntb_qp_link_down_reset(qp
);
1729 static bool ntb_dma_filter_fn(struct dma_chan
*chan
, void *node
)
1731 return dev_to_node(&chan
->dev
->device
) == (int)(unsigned long)node
;
1735 * ntb_transport_create_queue - Create a new NTB transport layer queue
1736 * @rx_handler: receive callback function
1737 * @tx_handler: transmit callback function
1738 * @event_handler: event callback function
1740 * Create a new NTB transport layer queue and provide the queue with a callback
1741 * routine for both transmit and receive. The receive callback routine will be
1742 * used to pass up data when the transport has received it on the queue. The
1743 * transmit callback routine will be called when the transport has completed the
1744 * transmission of the data on the queue and the data is ready to be freed.
1746 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1748 struct ntb_transport_qp
*
1749 ntb_transport_create_queue(void *data
, struct device
*client_dev
,
1750 const struct ntb_queue_handlers
*handlers
)
1752 struct ntb_dev
*ndev
;
1753 struct pci_dev
*pdev
;
1754 struct ntb_transport_ctx
*nt
;
1755 struct ntb_queue_entry
*entry
;
1756 struct ntb_transport_qp
*qp
;
1758 unsigned int free_queue
;
1759 dma_cap_mask_t dma_mask
;
1763 ndev
= dev_ntb(client_dev
->parent
);
1767 node
= dev_to_node(&ndev
->dev
);
1769 free_queue
= ffs(nt
->qp_bitmap_free
);
1773 /* decrement free_queue to make it zero based */
1776 qp
= &nt
->qp_vec
[free_queue
];
1777 qp_bit
= BIT_ULL(qp
->qp_num
);
1779 nt
->qp_bitmap_free
&= ~qp_bit
;
1782 qp
->rx_handler
= handlers
->rx_handler
;
1783 qp
->tx_handler
= handlers
->tx_handler
;
1784 qp
->event_handler
= handlers
->event_handler
;
1786 dma_cap_zero(dma_mask
);
1787 dma_cap_set(DMA_MEMCPY
, dma_mask
);
1791 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1792 (void *)(unsigned long)node
);
1793 if (!qp
->tx_dma_chan
)
1794 dev_info(&pdev
->dev
, "Unable to allocate TX DMA channel\n");
1797 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1798 (void *)(unsigned long)node
);
1799 if (!qp
->rx_dma_chan
)
1800 dev_info(&pdev
->dev
, "Unable to allocate RX DMA channel\n");
1802 qp
->tx_dma_chan
= NULL
;
1803 qp
->rx_dma_chan
= NULL
;
1806 dev_dbg(&pdev
->dev
, "Using %s memcpy for TX\n",
1807 qp
->tx_dma_chan
? "DMA" : "CPU");
1809 dev_dbg(&pdev
->dev
, "Using %s memcpy for RX\n",
1810 qp
->rx_dma_chan
? "DMA" : "CPU");
1812 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1813 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1818 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
1821 qp
->rx_alloc_entry
= NTB_QP_DEF_NUM_ENTRIES
;
1823 for (i
= 0; i
< qp
->tx_max_entry
; i
++) {
1824 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1829 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1833 ntb_db_clear(qp
->ndev
, qp_bit
);
1834 ntb_db_clear_mask(qp
->ndev
, qp_bit
);
1836 dev_info(&pdev
->dev
, "NTB Transport QP %d created\n", qp
->qp_num
);
1841 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1844 qp
->rx_alloc_entry
= 0;
1845 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1847 if (qp
->tx_dma_chan
)
1848 dma_release_channel(qp
->tx_dma_chan
);
1849 if (qp
->rx_dma_chan
)
1850 dma_release_channel(qp
->rx_dma_chan
);
1851 nt
->qp_bitmap_free
|= qp_bit
;
1855 EXPORT_SYMBOL_GPL(ntb_transport_create_queue
);
1858 * ntb_transport_free_queue - Frees NTB transport queue
1859 * @qp: NTB queue to be freed
1861 * Frees NTB transport queue
1863 void ntb_transport_free_queue(struct ntb_transport_qp
*qp
)
1865 struct pci_dev
*pdev
;
1866 struct ntb_queue_entry
*entry
;
1872 pdev
= qp
->ndev
->pdev
;
1876 if (qp
->tx_dma_chan
) {
1877 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1878 /* Putting the dma_chan to NULL will force any new traffic to be
1879 * processed by the CPU instead of the DAM engine
1881 qp
->tx_dma_chan
= NULL
;
1883 /* Try to be nice and wait for any queued DMA engine
1884 * transactions to process before smashing it with a rock
1886 dma_sync_wait(chan
, qp
->last_cookie
);
1887 dmaengine_terminate_all(chan
);
1888 dma_release_channel(chan
);
1891 if (qp
->rx_dma_chan
) {
1892 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1893 /* Putting the dma_chan to NULL will force any new traffic to be
1894 * processed by the CPU instead of the DAM engine
1896 qp
->rx_dma_chan
= NULL
;
1898 /* Try to be nice and wait for any queued DMA engine
1899 * transactions to process before smashing it with a rock
1901 dma_sync_wait(chan
, qp
->last_cookie
);
1902 dmaengine_terminate_all(chan
);
1903 dma_release_channel(chan
);
1906 qp_bit
= BIT_ULL(qp
->qp_num
);
1908 ntb_db_set_mask(qp
->ndev
, qp_bit
);
1909 tasklet_kill(&qp
->rxc_db_work
);
1911 cancel_delayed_work_sync(&qp
->link_work
);
1914 qp
->rx_handler
= NULL
;
1915 qp
->tx_handler
= NULL
;
1916 qp
->event_handler
= NULL
;
1918 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1921 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
))) {
1922 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_pend_q\n");
1926 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_post_q
))) {
1927 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_post_q\n");
1931 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1934 qp
->transport
->qp_bitmap_free
|= qp_bit
;
1936 dev_info(&pdev
->dev
, "NTB Transport QP %d freed\n", qp
->qp_num
);
1938 EXPORT_SYMBOL_GPL(ntb_transport_free_queue
);
1941 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1942 * @qp: NTB queue to be freed
1943 * @len: pointer to variable to write enqueued buffers length
1945 * Dequeues unused buffers from receive queue. Should only be used during
1948 * RETURNS: NULL error value on error, or void* for success.
1950 void *ntb_transport_rx_remove(struct ntb_transport_qp
*qp
, unsigned int *len
)
1952 struct ntb_queue_entry
*entry
;
1955 if (!qp
|| qp
->client_ready
)
1958 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
);
1962 buf
= entry
->cb_data
;
1965 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
1969 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove
);
1972 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1973 * @qp: NTB transport layer queue the entry is to be enqueued on
1974 * @cb: per buffer pointer for callback function to use
1975 * @data: pointer to data buffer that incoming packets will be copied into
1976 * @len: length of the data buffer
1978 * Enqueue a new receive buffer onto the transport queue into which a NTB
1979 * payload can be received into.
1981 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1983 int ntb_transport_rx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1986 struct ntb_queue_entry
*entry
;
1991 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
);
1995 entry
->cb_data
= cb
;
2001 entry
->rx_index
= 0;
2003 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
2006 tasklet_schedule(&qp
->rxc_db_work
);
2010 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue
);
2013 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2014 * @qp: NTB transport layer queue the entry is to be enqueued on
2015 * @cb: per buffer pointer for callback function to use
2016 * @data: pointer to data buffer that will be sent
2017 * @len: length of the data buffer
2019 * Enqueue a new transmit buffer onto the transport queue from which a NTB
2020 * payload will be transmitted. This assumes that a lock is being held to
2021 * serialize access to the qp.
2023 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2025 int ntb_transport_tx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
2028 struct ntb_queue_entry
*entry
;
2031 if (!qp
|| !qp
->link_is_up
|| !len
)
2034 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
2036 qp
->tx_err_no_buf
++;
2040 entry
->cb_data
= cb
;
2046 entry
->tx_index
= 0;
2048 rc
= ntb_process_tx(qp
, entry
);
2050 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
2055 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue
);
2058 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2059 * @qp: NTB transport layer queue to be enabled
2061 * Notify NTB transport layer of client readiness to use queue
2063 void ntb_transport_link_up(struct ntb_transport_qp
*qp
)
2068 qp
->client_ready
= true;
2070 if (qp
->transport
->link_is_up
)
2071 schedule_delayed_work(&qp
->link_work
, 0);
2073 EXPORT_SYMBOL_GPL(ntb_transport_link_up
);
2076 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2077 * @qp: NTB transport layer queue to be disabled
2079 * Notify NTB transport layer of client's desire to no longer receive data on
2080 * transport queue specified. It is the client's responsibility to ensure all
2081 * entries on queue are purged or otherwise handled appropriately.
2083 void ntb_transport_link_down(struct ntb_transport_qp
*qp
)
2090 qp
->client_ready
= false;
2092 val
= ntb_spad_read(qp
->ndev
, QP_LINKS
);
2094 ntb_peer_spad_write(qp
->ndev
, QP_LINKS
,
2095 val
& ~BIT(qp
->qp_num
));
2098 ntb_send_link_down(qp
);
2100 cancel_delayed_work_sync(&qp
->link_work
);
2102 EXPORT_SYMBOL_GPL(ntb_transport_link_down
);
2105 * ntb_transport_link_query - Query transport link state
2106 * @qp: NTB transport layer queue to be queried
2108 * Query connectivity to the remote system of the NTB transport queue
2110 * RETURNS: true for link up or false for link down
2112 bool ntb_transport_link_query(struct ntb_transport_qp
*qp
)
2117 return qp
->link_is_up
;
2119 EXPORT_SYMBOL_GPL(ntb_transport_link_query
);
2122 * ntb_transport_qp_num - Query the qp number
2123 * @qp: NTB transport layer queue to be queried
2125 * Query qp number of the NTB transport queue
2127 * RETURNS: a zero based number specifying the qp number
2129 unsigned char ntb_transport_qp_num(struct ntb_transport_qp
*qp
)
2136 EXPORT_SYMBOL_GPL(ntb_transport_qp_num
);
2139 * ntb_transport_max_size - Query the max payload size of a qp
2140 * @qp: NTB transport layer queue to be queried
2142 * Query the maximum payload size permissible on the given qp
2144 * RETURNS: the max payload size of a qp
2146 unsigned int ntb_transport_max_size(struct ntb_transport_qp
*qp
)
2148 unsigned int max_size
;
2149 unsigned int copy_align
;
2150 struct dma_chan
*rx_chan
, *tx_chan
;
2155 rx_chan
= qp
->rx_dma_chan
;
2156 tx_chan
= qp
->tx_dma_chan
;
2158 copy_align
= max(rx_chan
? rx_chan
->device
->copy_align
: 0,
2159 tx_chan
? tx_chan
->device
->copy_align
: 0);
2161 /* If DMA engine usage is possible, try to find the max size for that */
2162 max_size
= qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
2163 max_size
= round_down(max_size
, 1 << copy_align
);
2167 EXPORT_SYMBOL_GPL(ntb_transport_max_size
);
2169 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp
*qp
)
2171 unsigned int head
= qp
->tx_index
;
2172 unsigned int tail
= qp
->remote_rx_info
->entry
;
2174 return tail
> head
? tail
- head
: qp
->tx_max_entry
+ tail
- head
;
2176 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry
);
2178 static void ntb_transport_doorbell_callback(void *data
, int vector
)
2180 struct ntb_transport_ctx
*nt
= data
;
2181 struct ntb_transport_qp
*qp
;
2183 unsigned int qp_num
;
2185 db_bits
= (nt
->qp_bitmap
& ~nt
->qp_bitmap_free
&
2186 ntb_db_vector_mask(nt
->ndev
, vector
));
2189 qp_num
= __ffs(db_bits
);
2190 qp
= &nt
->qp_vec
[qp_num
];
2193 tasklet_schedule(&qp
->rxc_db_work
);
2195 db_bits
&= ~BIT_ULL(qp_num
);
2199 static const struct ntb_ctx_ops ntb_transport_ops
= {
2200 .link_event
= ntb_transport_event_callback
,
2201 .db_event
= ntb_transport_doorbell_callback
,
2204 static struct ntb_client ntb_transport_client
= {
2206 .probe
= ntb_transport_probe
,
2207 .remove
= ntb_transport_free
,
2211 static int __init
ntb_transport_init(void)
2215 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC
, NTB_TRANSPORT_VER
);
2217 if (debugfs_initialized())
2218 nt_debugfs_dir
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
2220 rc
= bus_register(&ntb_transport_bus
);
2224 rc
= ntb_register_client(&ntb_transport_client
);
2231 bus_unregister(&ntb_transport_bus
);
2233 debugfs_remove_recursive(nt_debugfs_dir
);
2236 module_init(ntb_transport_init
);
2238 static void __exit
ntb_transport_exit(void)
2240 ntb_unregister_client(&ntb_transport_client
);
2241 bus_unregister(&ntb_transport_bus
);
2242 debugfs_remove_recursive(nt_debugfs_dir
);
2244 module_exit(ntb_transport_exit
);