2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
8 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
17 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 * PCIe NTB Transport Linux driver
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
50 #include <linux/debugfs.h>
51 #include <linux/delay.h>
52 #include <linux/dmaengine.h>
53 #include <linux/dma-mapping.h>
54 #include <linux/errno.h>
55 #include <linux/export.h>
56 #include <linux/interrupt.h>
57 #include <linux/module.h>
58 #include <linux/pci.h>
59 #include <linux/slab.h>
60 #include <linux/types.h>
61 #include <linux/uaccess.h>
62 #include "linux/ntb.h"
63 #include "linux/ntb_transport.h"
65 #define NTB_TRANSPORT_VERSION 4
66 #define NTB_TRANSPORT_VER "4"
67 #define NTB_TRANSPORT_NAME "ntb_transport"
68 #define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
70 MODULE_DESCRIPTION(NTB_TRANSPORT_DESC
);
71 MODULE_VERSION(NTB_TRANSPORT_VER
);
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_AUTHOR("Intel Corporation");
75 static unsigned long max_mw_size
;
76 module_param(max_mw_size
, ulong
, 0644);
77 MODULE_PARM_DESC(max_mw_size
, "Limit size of large memory windows");
79 static unsigned int transport_mtu
= 0x10000;
80 module_param(transport_mtu
, uint
, 0644);
81 MODULE_PARM_DESC(transport_mtu
, "Maximum size of NTB transport packets");
83 static unsigned char max_num_clients
;
84 module_param(max_num_clients
, byte
, 0644);
85 MODULE_PARM_DESC(max_num_clients
, "Maximum number of NTB transport clients");
87 static unsigned int copy_bytes
= 1024;
88 module_param(copy_bytes
, uint
, 0644);
89 MODULE_PARM_DESC(copy_bytes
, "Threshold under which NTB will use the CPU to copy instead of DMA");
92 module_param(use_dma
, bool, 0644);
93 MODULE_PARM_DESC(use_dma
, "Use DMA engine to perform large data copy");
95 static struct dentry
*nt_debugfs_dir
;
97 struct ntb_queue_entry
{
98 /* ntb_queue list reference */
99 struct list_head entry
;
100 /* pointers to data to be transferred */
107 unsigned int tx_index
;
108 unsigned int rx_index
;
110 struct ntb_transport_qp
*qp
;
112 struct ntb_payload_header __iomem
*tx_hdr
;
113 struct ntb_payload_header
*rx_hdr
;
121 struct ntb_transport_qp
{
122 struct ntb_transport_ctx
*transport
;
123 struct ntb_dev
*ndev
;
125 struct dma_chan
*tx_dma_chan
;
126 struct dma_chan
*rx_dma_chan
;
132 u8 qp_num
; /* Only 64 QP's are allowed. 0-63 */
135 struct ntb_rx_info __iomem
*rx_info
;
136 struct ntb_rx_info
*remote_rx_info
;
138 void (*tx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
139 void *data
, int len
);
140 struct list_head tx_free_q
;
141 spinlock_t ntb_tx_free_q_lock
;
143 dma_addr_t tx_mw_phys
;
144 unsigned int tx_index
;
145 unsigned int tx_max_entry
;
146 unsigned int tx_max_frame
;
148 void (*rx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
149 void *data
, int len
);
150 struct list_head rx_post_q
;
151 struct list_head rx_pend_q
;
152 struct list_head rx_free_q
;
153 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
154 spinlock_t ntb_rx_q_lock
;
156 unsigned int rx_index
;
157 unsigned int rx_max_entry
;
158 unsigned int rx_max_frame
;
159 unsigned int rx_alloc_entry
;
160 dma_cookie_t last_cookie
;
161 struct tasklet_struct rxc_db_work
;
163 void (*event_handler
)(void *data
, int status
);
164 struct delayed_work link_work
;
165 struct work_struct link_cleanup
;
167 struct dentry
*debugfs_dir
;
168 struct dentry
*debugfs_stats
;
187 struct ntb_transport_mw
{
188 phys_addr_t phys_addr
;
189 resource_size_t phys_size
;
190 resource_size_t xlat_align
;
191 resource_size_t xlat_align_size
;
199 struct ntb_transport_client_dev
{
200 struct list_head entry
;
201 struct ntb_transport_ctx
*nt
;
205 struct ntb_transport_ctx
{
206 struct list_head entry
;
207 struct list_head client_devs
;
209 struct ntb_dev
*ndev
;
211 struct ntb_transport_mw
*mw_vec
;
212 struct ntb_transport_qp
*qp_vec
;
213 unsigned int mw_count
;
214 unsigned int qp_count
;
219 struct delayed_work link_work
;
220 struct work_struct link_cleanup
;
222 struct dentry
*debugfs_node_dir
;
226 DESC_DONE_FLAG
= BIT(0),
227 LINK_DOWN_FLAG
= BIT(1),
230 struct ntb_payload_header
{
248 #define dev_client_dev(__dev) \
249 container_of((__dev), struct ntb_transport_client_dev, dev)
251 #define drv_client(__drv) \
252 container_of((__drv), struct ntb_transport_client, driver)
254 #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count)
255 #define NTB_QP_DEF_NUM_ENTRIES 100
256 #define NTB_LINK_DOWN_TIMEOUT 10
258 static void ntb_transport_rxc_db(unsigned long data
);
259 static const struct ntb_ctx_ops ntb_transport_ops
;
260 static struct ntb_client ntb_transport_client
;
261 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
262 struct ntb_queue_entry
*entry
);
263 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
);
264 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
);
265 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
);
268 static int ntb_transport_bus_match(struct device
*dev
,
269 struct device_driver
*drv
)
271 return !strncmp(dev_name(dev
), drv
->name
, strlen(drv
->name
));
274 static int ntb_transport_bus_probe(struct device
*dev
)
276 const struct ntb_transport_client
*client
;
281 client
= drv_client(dev
->driver
);
282 rc
= client
->probe(dev
);
289 static int ntb_transport_bus_remove(struct device
*dev
)
291 const struct ntb_transport_client
*client
;
293 client
= drv_client(dev
->driver
);
301 static struct bus_type ntb_transport_bus
= {
302 .name
= "ntb_transport",
303 .match
= ntb_transport_bus_match
,
304 .probe
= ntb_transport_bus_probe
,
305 .remove
= ntb_transport_bus_remove
,
308 static LIST_HEAD(ntb_transport_list
);
310 static int ntb_bus_init(struct ntb_transport_ctx
*nt
)
312 list_add_tail(&nt
->entry
, &ntb_transport_list
);
316 static void ntb_bus_remove(struct ntb_transport_ctx
*nt
)
318 struct ntb_transport_client_dev
*client_dev
, *cd
;
320 list_for_each_entry_safe(client_dev
, cd
, &nt
->client_devs
, entry
) {
321 dev_err(client_dev
->dev
.parent
, "%s still attached to bus, removing\n",
322 dev_name(&client_dev
->dev
));
323 list_del(&client_dev
->entry
);
324 device_unregister(&client_dev
->dev
);
327 list_del(&nt
->entry
);
330 static void ntb_transport_client_release(struct device
*dev
)
332 struct ntb_transport_client_dev
*client_dev
;
334 client_dev
= dev_client_dev(dev
);
339 * ntb_transport_unregister_client_dev - Unregister NTB client device
340 * @device_name: Name of NTB client device
342 * Unregister an NTB client device with the NTB transport layer
344 void ntb_transport_unregister_client_dev(char *device_name
)
346 struct ntb_transport_client_dev
*client
, *cd
;
347 struct ntb_transport_ctx
*nt
;
349 list_for_each_entry(nt
, &ntb_transport_list
, entry
)
350 list_for_each_entry_safe(client
, cd
, &nt
->client_devs
, entry
)
351 if (!strncmp(dev_name(&client
->dev
), device_name
,
352 strlen(device_name
))) {
353 list_del(&client
->entry
);
354 device_unregister(&client
->dev
);
357 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev
);
360 * ntb_transport_register_client_dev - Register NTB client device
361 * @device_name: Name of NTB client device
363 * Register an NTB client device with the NTB transport layer
365 int ntb_transport_register_client_dev(char *device_name
)
367 struct ntb_transport_client_dev
*client_dev
;
368 struct ntb_transport_ctx
*nt
;
372 if (list_empty(&ntb_transport_list
))
375 list_for_each_entry(nt
, &ntb_transport_list
, entry
) {
378 node
= dev_to_node(&nt
->ndev
->dev
);
380 client_dev
= kzalloc_node(sizeof(*client_dev
),
387 dev
= &client_dev
->dev
;
389 /* setup and register client devices */
390 dev_set_name(dev
, "%s%d", device_name
, i
);
391 dev
->bus
= &ntb_transport_bus
;
392 dev
->release
= ntb_transport_client_release
;
393 dev
->parent
= &nt
->ndev
->dev
;
395 rc
= device_register(dev
);
401 list_add_tail(&client_dev
->entry
, &nt
->client_devs
);
408 ntb_transport_unregister_client_dev(device_name
);
412 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev
);
415 * ntb_transport_register_client - Register NTB client driver
416 * @drv: NTB client driver to be registered
418 * Register an NTB client driver with the NTB transport layer
420 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
422 int ntb_transport_register_client(struct ntb_transport_client
*drv
)
424 drv
->driver
.bus
= &ntb_transport_bus
;
426 if (list_empty(&ntb_transport_list
))
429 return driver_register(&drv
->driver
);
431 EXPORT_SYMBOL_GPL(ntb_transport_register_client
);
434 * ntb_transport_unregister_client - Unregister NTB client driver
435 * @drv: NTB client driver to be unregistered
437 * Unregister an NTB client driver with the NTB transport layer
439 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
441 void ntb_transport_unregister_client(struct ntb_transport_client
*drv
)
443 driver_unregister(&drv
->driver
);
445 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client
);
447 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
450 struct ntb_transport_qp
*qp
;
452 ssize_t ret
, out_offset
, out_count
;
454 qp
= filp
->private_data
;
456 if (!qp
|| !qp
->link_is_up
)
461 buf
= kmalloc(out_count
, GFP_KERNEL
);
466 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
467 "\nNTB QP stats:\n\n");
468 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
469 "rx_bytes - \t%llu\n", qp
->rx_bytes
);
470 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
471 "rx_pkts - \t%llu\n", qp
->rx_pkts
);
472 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
473 "rx_memcpy - \t%llu\n", qp
->rx_memcpy
);
474 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
475 "rx_async - \t%llu\n", qp
->rx_async
);
476 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
477 "rx_ring_empty - %llu\n", qp
->rx_ring_empty
);
478 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
479 "rx_err_no_buf - %llu\n", qp
->rx_err_no_buf
);
480 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
481 "rx_err_oflow - \t%llu\n", qp
->rx_err_oflow
);
482 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
483 "rx_err_ver - \t%llu\n", qp
->rx_err_ver
);
484 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
485 "rx_buff - \t0x%p\n", qp
->rx_buff
);
486 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
487 "rx_index - \t%u\n", qp
->rx_index
);
488 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
489 "rx_max_entry - \t%u\n", qp
->rx_max_entry
);
490 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
491 "rx_alloc_entry - \t%u\n\n", qp
->rx_alloc_entry
);
493 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
494 "tx_bytes - \t%llu\n", qp
->tx_bytes
);
495 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
496 "tx_pkts - \t%llu\n", qp
->tx_pkts
);
497 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
498 "tx_memcpy - \t%llu\n", qp
->tx_memcpy
);
499 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
500 "tx_async - \t%llu\n", qp
->tx_async
);
501 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
502 "tx_ring_full - \t%llu\n", qp
->tx_ring_full
);
503 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
504 "tx_err_no_buf - %llu\n", qp
->tx_err_no_buf
);
505 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
506 "tx_mw - \t0x%p\n", qp
->tx_mw
);
507 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
508 "tx_index (H) - \t%u\n", qp
->tx_index
);
509 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
511 qp
->remote_rx_info
->entry
);
512 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
513 "tx_max_entry - \t%u\n", qp
->tx_max_entry
);
514 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
516 ntb_transport_tx_free_entry(qp
));
518 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
520 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
521 "Using TX DMA - \t%s\n",
522 qp
->tx_dma_chan
? "Yes" : "No");
523 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
524 "Using RX DMA - \t%s\n",
525 qp
->rx_dma_chan
? "Yes" : "No");
526 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
528 qp
->link_is_up
? "Up" : "Down");
529 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
532 if (out_offset
> out_count
)
533 out_offset
= out_count
;
535 ret
= simple_read_from_buffer(ubuf
, count
, offp
, buf
, out_offset
);
540 static const struct file_operations ntb_qp_debugfs_stats
= {
541 .owner
= THIS_MODULE
,
543 .read
= debugfs_read
,
546 static void ntb_list_add(spinlock_t
*lock
, struct list_head
*entry
,
547 struct list_head
*list
)
551 spin_lock_irqsave(lock
, flags
);
552 list_add_tail(entry
, list
);
553 spin_unlock_irqrestore(lock
, flags
);
556 static struct ntb_queue_entry
*ntb_list_rm(spinlock_t
*lock
,
557 struct list_head
*list
)
559 struct ntb_queue_entry
*entry
;
562 spin_lock_irqsave(lock
, flags
);
563 if (list_empty(list
)) {
567 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
568 list_del(&entry
->entry
);
571 spin_unlock_irqrestore(lock
, flags
);
576 static struct ntb_queue_entry
*ntb_list_mv(spinlock_t
*lock
,
577 struct list_head
*list
,
578 struct list_head
*to_list
)
580 struct ntb_queue_entry
*entry
;
583 spin_lock_irqsave(lock
, flags
);
585 if (list_empty(list
)) {
588 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
589 list_move_tail(&entry
->entry
, to_list
);
592 spin_unlock_irqrestore(lock
, flags
);
597 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx
*nt
,
600 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[qp_num
];
601 struct ntb_transport_mw
*mw
;
602 struct ntb_dev
*ndev
= nt
->ndev
;
603 struct ntb_queue_entry
*entry
;
604 unsigned int rx_size
, num_qps_mw
;
605 unsigned int mw_num
, mw_count
, qp_count
;
609 mw_count
= nt
->mw_count
;
610 qp_count
= nt
->qp_count
;
612 mw_num
= QP_TO_MW(nt
, qp_num
);
613 mw
= &nt
->mw_vec
[mw_num
];
618 if (mw_num
< qp_count
% mw_count
)
619 num_qps_mw
= qp_count
/ mw_count
+ 1;
621 num_qps_mw
= qp_count
/ mw_count
;
623 rx_size
= (unsigned int)mw
->xlat_size
/ num_qps_mw
;
624 qp
->rx_buff
= mw
->virt_addr
+ rx_size
* (qp_num
/ mw_count
);
625 rx_size
-= sizeof(struct ntb_rx_info
);
627 qp
->remote_rx_info
= qp
->rx_buff
+ rx_size
;
629 /* Due to housekeeping, there must be atleast 2 buffs */
630 qp
->rx_max_frame
= min(transport_mtu
, rx_size
/ 2);
631 qp
->rx_max_entry
= rx_size
/ qp
->rx_max_frame
;
635 * Checking to see if we have more entries than the default.
636 * We should add additional entries if that is the case so we
637 * can be in sync with the transport frames.
639 node
= dev_to_node(&ndev
->dev
);
640 for (i
= qp
->rx_alloc_entry
; i
< qp
->rx_max_entry
; i
++) {
641 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
646 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
648 qp
->rx_alloc_entry
++;
651 qp
->remote_rx_info
->entry
= qp
->rx_max_entry
- 1;
653 /* setup the hdr offsets with 0's */
654 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
655 void *offset
= (qp
->rx_buff
+ qp
->rx_max_frame
* (i
+ 1) -
656 sizeof(struct ntb_payload_header
));
657 memset(offset
, 0, sizeof(struct ntb_payload_header
));
667 static void ntb_free_mw(struct ntb_transport_ctx
*nt
, int num_mw
)
669 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
670 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
675 ntb_mw_clear_trans(nt
->ndev
, num_mw
);
676 dma_free_coherent(&pdev
->dev
, mw
->buff_size
,
677 mw
->virt_addr
, mw
->dma_addr
);
680 mw
->virt_addr
= NULL
;
683 static int ntb_set_mw(struct ntb_transport_ctx
*nt
, int num_mw
,
684 resource_size_t size
)
686 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
687 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
688 size_t xlat_size
, buff_size
;
694 xlat_size
= round_up(size
, mw
->xlat_align_size
);
695 buff_size
= round_up(size
, mw
->xlat_align
);
697 /* No need to re-setup */
698 if (mw
->xlat_size
== xlat_size
)
702 ntb_free_mw(nt
, num_mw
);
704 /* Alloc memory for receiving data. Must be aligned */
705 mw
->xlat_size
= xlat_size
;
706 mw
->buff_size
= buff_size
;
708 mw
->virt_addr
= dma_alloc_coherent(&pdev
->dev
, buff_size
,
709 &mw
->dma_addr
, GFP_KERNEL
);
710 if (!mw
->virt_addr
) {
713 dev_err(&pdev
->dev
, "Unable to alloc MW buff of size %zu\n",
719 * we must ensure that the memory address allocated is BAR size
720 * aligned in order for the XLAT register to take the value. This
721 * is a requirement of the hardware. It is recommended to setup CMA
722 * for BAR sizes equal or greater than 4MB.
724 if (!IS_ALIGNED(mw
->dma_addr
, mw
->xlat_align
)) {
725 dev_err(&pdev
->dev
, "DMA memory %pad is not aligned\n",
727 ntb_free_mw(nt
, num_mw
);
731 /* Notify HW the memory location of the receive buffer */
732 rc
= ntb_mw_set_trans(nt
->ndev
, num_mw
, mw
->dma_addr
, mw
->xlat_size
);
734 dev_err(&pdev
->dev
, "Unable to set mw%d translation", num_mw
);
735 ntb_free_mw(nt
, num_mw
);
742 static void ntb_qp_link_down_reset(struct ntb_transport_qp
*qp
)
744 qp
->link_is_up
= false;
751 qp
->rx_ring_empty
= 0;
752 qp
->rx_err_no_buf
= 0;
753 qp
->rx_err_oflow
= 0;
759 qp
->tx_ring_full
= 0;
760 qp
->tx_err_no_buf
= 0;
765 static void ntb_qp_link_cleanup(struct ntb_transport_qp
*qp
)
767 struct ntb_transport_ctx
*nt
= qp
->transport
;
768 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
770 dev_info(&pdev
->dev
, "qp %d: Link Cleanup\n", qp
->qp_num
);
772 cancel_delayed_work_sync(&qp
->link_work
);
773 ntb_qp_link_down_reset(qp
);
775 if (qp
->event_handler
)
776 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
779 static void ntb_qp_link_cleanup_work(struct work_struct
*work
)
781 struct ntb_transport_qp
*qp
= container_of(work
,
782 struct ntb_transport_qp
,
784 struct ntb_transport_ctx
*nt
= qp
->transport
;
786 ntb_qp_link_cleanup(qp
);
789 schedule_delayed_work(&qp
->link_work
,
790 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
793 static void ntb_qp_link_down(struct ntb_transport_qp
*qp
)
795 schedule_work(&qp
->link_cleanup
);
798 static void ntb_transport_link_cleanup(struct ntb_transport_ctx
*nt
)
800 struct ntb_transport_qp
*qp
;
804 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
806 /* Pass along the info to any clients */
807 for (i
= 0; i
< nt
->qp_count
; i
++)
808 if (qp_bitmap_alloc
& BIT_ULL(i
)) {
810 ntb_qp_link_cleanup(qp
);
811 cancel_work_sync(&qp
->link_cleanup
);
812 cancel_delayed_work_sync(&qp
->link_work
);
816 cancel_delayed_work_sync(&nt
->link_work
);
818 /* The scratchpad registers keep the values if the remote side
819 * goes down, blast them now to give them a sane value the next
820 * time they are accessed
822 for (i
= 0; i
< MAX_SPAD
; i
++)
823 ntb_spad_write(nt
->ndev
, i
, 0);
826 static void ntb_transport_link_cleanup_work(struct work_struct
*work
)
828 struct ntb_transport_ctx
*nt
=
829 container_of(work
, struct ntb_transport_ctx
, link_cleanup
);
831 ntb_transport_link_cleanup(nt
);
834 static void ntb_transport_event_callback(void *data
)
836 struct ntb_transport_ctx
*nt
= data
;
838 if (ntb_link_is_up(nt
->ndev
, NULL
, NULL
) == 1)
839 schedule_delayed_work(&nt
->link_work
, 0);
841 schedule_work(&nt
->link_cleanup
);
844 static void ntb_transport_link_work(struct work_struct
*work
)
846 struct ntb_transport_ctx
*nt
=
847 container_of(work
, struct ntb_transport_ctx
, link_work
.work
);
848 struct ntb_dev
*ndev
= nt
->ndev
;
849 struct pci_dev
*pdev
= ndev
->pdev
;
850 resource_size_t size
;
854 /* send the local info, in the opposite order of the way we read it */
855 for (i
= 0; i
< nt
->mw_count
; i
++) {
856 size
= nt
->mw_vec
[i
].phys_size
;
858 if (max_mw_size
&& size
> max_mw_size
)
861 spad
= MW0_SZ_HIGH
+ (i
* 2);
862 ntb_peer_spad_write(ndev
, spad
, upper_32_bits(size
));
864 spad
= MW0_SZ_LOW
+ (i
* 2);
865 ntb_peer_spad_write(ndev
, spad
, lower_32_bits(size
));
868 ntb_peer_spad_write(ndev
, NUM_MWS
, nt
->mw_count
);
870 ntb_peer_spad_write(ndev
, NUM_QPS
, nt
->qp_count
);
872 ntb_peer_spad_write(ndev
, VERSION
, NTB_TRANSPORT_VERSION
);
874 /* Query the remote side for its info */
875 val
= ntb_spad_read(ndev
, VERSION
);
876 dev_dbg(&pdev
->dev
, "Remote version = %d\n", val
);
877 if (val
!= NTB_TRANSPORT_VERSION
)
880 val
= ntb_spad_read(ndev
, NUM_QPS
);
881 dev_dbg(&pdev
->dev
, "Remote max number of qps = %d\n", val
);
882 if (val
!= nt
->qp_count
)
885 val
= ntb_spad_read(ndev
, NUM_MWS
);
886 dev_dbg(&pdev
->dev
, "Remote number of mws = %d\n", val
);
887 if (val
!= nt
->mw_count
)
890 for (i
= 0; i
< nt
->mw_count
; i
++) {
893 val
= ntb_spad_read(ndev
, MW0_SZ_HIGH
+ (i
* 2));
894 val64
= (u64
)val
<< 32;
896 val
= ntb_spad_read(ndev
, MW0_SZ_LOW
+ (i
* 2));
899 dev_dbg(&pdev
->dev
, "Remote MW%d size = %#llx\n", i
, val64
);
901 rc
= ntb_set_mw(nt
, i
, val64
);
906 nt
->link_is_up
= true;
908 for (i
= 0; i
< nt
->qp_count
; i
++) {
909 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[i
];
911 ntb_transport_setup_qp_mw(nt
, i
);
913 if (qp
->client_ready
)
914 schedule_delayed_work(&qp
->link_work
, 0);
920 for (i
= 0; i
< nt
->mw_count
; i
++)
923 /* if there's an actual failure, we should just bail */
928 if (ntb_link_is_up(ndev
, NULL
, NULL
) == 1)
929 schedule_delayed_work(&nt
->link_work
,
930 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
933 static void ntb_qp_link_work(struct work_struct
*work
)
935 struct ntb_transport_qp
*qp
= container_of(work
,
936 struct ntb_transport_qp
,
938 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
939 struct ntb_transport_ctx
*nt
= qp
->transport
;
942 WARN_ON(!nt
->link_is_up
);
944 val
= ntb_spad_read(nt
->ndev
, QP_LINKS
);
946 ntb_peer_spad_write(nt
->ndev
, QP_LINKS
, val
| BIT(qp
->qp_num
));
948 /* query remote spad for qp ready bits */
949 ntb_peer_spad_read(nt
->ndev
, QP_LINKS
);
950 dev_dbg_ratelimited(&pdev
->dev
, "Remote QP link status = %x\n", val
);
952 /* See if the remote side is up */
953 if (val
& BIT(qp
->qp_num
)) {
954 dev_info(&pdev
->dev
, "qp %d: Link Up\n", qp
->qp_num
);
955 qp
->link_is_up
= true;
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
;
1058 mw_count
= ntb_mw_count(ndev
);
1059 if (ntb_spad_count(ndev
) < (NUM_MWS
+ 1 + mw_count
* 2)) {
1060 dev_err(&ndev
->dev
, "Not enough scratch pad registers for %s",
1061 NTB_TRANSPORT_NAME
);
1065 if (ntb_db_is_unsafe(ndev
))
1067 "doorbell is unsafe, proceed anyway...\n");
1068 if (ntb_spad_is_unsafe(ndev
))
1070 "scratchpad is unsafe, proceed anyway...\n");
1072 node
= dev_to_node(&ndev
->dev
);
1074 nt
= kzalloc_node(sizeof(*nt
), GFP_KERNEL
, node
);
1080 nt
->mw_count
= mw_count
;
1082 nt
->mw_vec
= kzalloc_node(mw_count
* sizeof(*nt
->mw_vec
),
1089 for (i
= 0; i
< mw_count
; i
++) {
1090 mw
= &nt
->mw_vec
[i
];
1092 rc
= ntb_mw_get_range(ndev
, i
, &mw
->phys_addr
, &mw
->phys_size
,
1093 &mw
->xlat_align
, &mw
->xlat_align_size
);
1097 mw
->vbase
= ioremap_wc(mw
->phys_addr
, mw
->phys_size
);
1105 mw
->virt_addr
= NULL
;
1109 qp_bitmap
= ntb_db_valid_mask(ndev
);
1111 qp_count
= ilog2(qp_bitmap
);
1112 if (max_num_clients
&& max_num_clients
< qp_count
)
1113 qp_count
= max_num_clients
;
1114 else if (nt
->mw_count
< qp_count
)
1115 qp_count
= nt
->mw_count
;
1117 qp_bitmap
&= BIT_ULL(qp_count
) - 1;
1119 nt
->qp_count
= qp_count
;
1120 nt
->qp_bitmap
= qp_bitmap
;
1121 nt
->qp_bitmap_free
= qp_bitmap
;
1123 nt
->qp_vec
= kzalloc_node(qp_count
* sizeof(*nt
->qp_vec
),
1130 if (nt_debugfs_dir
) {
1131 nt
->debugfs_node_dir
=
1132 debugfs_create_dir(pci_name(ndev
->pdev
),
1136 for (i
= 0; i
< qp_count
; i
++) {
1137 rc
= ntb_transport_init_queue(nt
, i
);
1142 INIT_DELAYED_WORK(&nt
->link_work
, ntb_transport_link_work
);
1143 INIT_WORK(&nt
->link_cleanup
, ntb_transport_link_cleanup_work
);
1145 rc
= ntb_set_ctx(ndev
, nt
, &ntb_transport_ops
);
1149 INIT_LIST_HEAD(&nt
->client_devs
);
1150 rc
= ntb_bus_init(nt
);
1154 nt
->link_is_up
= false;
1155 ntb_link_enable(ndev
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
1156 ntb_link_event(ndev
);
1161 ntb_clear_ctx(ndev
);
1166 mw
= &nt
->mw_vec
[i
];
1175 static void ntb_transport_free(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1177 struct ntb_transport_ctx
*nt
= ndev
->ctx
;
1178 struct ntb_transport_qp
*qp
;
1179 u64 qp_bitmap_alloc
;
1182 ntb_transport_link_cleanup(nt
);
1183 cancel_work_sync(&nt
->link_cleanup
);
1184 cancel_delayed_work_sync(&nt
->link_work
);
1186 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
1188 /* verify that all the qp's are freed */
1189 for (i
= 0; i
< nt
->qp_count
; i
++) {
1190 qp
= &nt
->qp_vec
[i
];
1191 if (qp_bitmap_alloc
& BIT_ULL(i
))
1192 ntb_transport_free_queue(qp
);
1193 debugfs_remove_recursive(qp
->debugfs_dir
);
1196 ntb_link_disable(ndev
);
1197 ntb_clear_ctx(ndev
);
1201 for (i
= nt
->mw_count
; i
--; ) {
1203 iounmap(nt
->mw_vec
[i
].vbase
);
1211 static void ntb_complete_rxc(struct ntb_transport_qp
*qp
)
1213 struct ntb_queue_entry
*entry
;
1216 unsigned long irqflags
;
1218 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1220 while (!list_empty(&qp
->rx_post_q
)) {
1221 entry
= list_first_entry(&qp
->rx_post_q
,
1222 struct ntb_queue_entry
, entry
);
1223 if (!(entry
->flags
& DESC_DONE_FLAG
))
1226 entry
->rx_hdr
->flags
= 0;
1227 iowrite32(entry
->rx_index
, &qp
->rx_info
->entry
);
1229 cb_data
= entry
->cb_data
;
1232 list_move_tail(&entry
->entry
, &qp
->rx_free_q
);
1234 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1236 if (qp
->rx_handler
&& qp
->client_ready
)
1237 qp
->rx_handler(qp
, qp
->cb_data
, cb_data
, len
);
1239 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1242 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1245 static void ntb_rx_copy_callback(void *data
,
1246 const struct dmaengine_result
*res
)
1248 struct ntb_queue_entry
*entry
= data
;
1250 /* we need to check DMA results if we are using DMA */
1252 enum dmaengine_tx_result dma_err
= res
->result
;
1255 case DMA_TRANS_READ_FAILED
:
1256 case DMA_TRANS_WRITE_FAILED
:
1258 case DMA_TRANS_ABORTED
:
1260 struct ntb_transport_qp
*qp
= entry
->qp
;
1261 void *offset
= qp
->rx_buff
+ qp
->rx_max_frame
*
1264 ntb_memcpy_rx(entry
, offset
);
1269 case DMA_TRANS_NOERROR
:
1275 entry
->flags
|= DESC_DONE_FLAG
;
1277 ntb_complete_rxc(entry
->qp
);
1280 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
)
1282 void *buf
= entry
->buf
;
1283 size_t len
= entry
->len
;
1285 memcpy(buf
, offset
, len
);
1287 /* Ensure that the data is fully copied out before clearing the flag */
1290 ntb_rx_copy_callback(entry
, NULL
);
1293 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
)
1295 struct dma_async_tx_descriptor
*txd
;
1296 struct ntb_transport_qp
*qp
= entry
->qp
;
1297 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1298 struct dma_device
*device
;
1299 size_t pay_off
, buff_off
, len
;
1300 struct dmaengine_unmap_data
*unmap
;
1301 dma_cookie_t cookie
;
1302 void *buf
= entry
->buf
;
1305 device
= chan
->device
;
1306 pay_off
= (size_t)offset
& ~PAGE_MASK
;
1307 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1309 if (!is_dma_copy_aligned(device
, pay_off
, buff_off
, len
))
1312 unmap
= dmaengine_get_unmap_data(device
->dev
, 2, GFP_NOWAIT
);
1317 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(offset
),
1318 pay_off
, len
, DMA_TO_DEVICE
);
1319 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1324 unmap
->addr
[1] = dma_map_page(device
->dev
, virt_to_page(buf
),
1325 buff_off
, len
, DMA_FROM_DEVICE
);
1326 if (dma_mapping_error(device
->dev
, unmap
->addr
[1]))
1329 unmap
->from_cnt
= 1;
1331 txd
= device
->device_prep_dma_memcpy(chan
, unmap
->addr
[1],
1332 unmap
->addr
[0], len
,
1333 DMA_PREP_INTERRUPT
);
1337 txd
->callback_result
= ntb_rx_copy_callback
;
1338 txd
->callback_param
= entry
;
1339 dma_set_unmap(txd
, unmap
);
1341 cookie
= dmaengine_submit(txd
);
1342 if (dma_submit_error(cookie
))
1345 dmaengine_unmap_put(unmap
);
1347 qp
->last_cookie
= cookie
;
1354 dmaengine_unmap_put(unmap
);
1356 dmaengine_unmap_put(unmap
);
1361 static void ntb_async_rx(struct ntb_queue_entry
*entry
, void *offset
)
1363 struct ntb_transport_qp
*qp
= entry
->qp
;
1364 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1370 if (entry
->len
< copy_bytes
)
1373 res
= ntb_async_rx_submit(entry
, offset
);
1377 if (!entry
->retries
)
1383 ntb_memcpy_rx(entry
, offset
);
1387 static int ntb_process_rxc(struct ntb_transport_qp
*qp
)
1389 struct ntb_payload_header
*hdr
;
1390 struct ntb_queue_entry
*entry
;
1393 offset
= qp
->rx_buff
+ qp
->rx_max_frame
* qp
->rx_index
;
1394 hdr
= offset
+ qp
->rx_max_frame
- sizeof(struct ntb_payload_header
);
1396 dev_dbg(&qp
->ndev
->pdev
->dev
, "qp %d: RX ver %u len %d flags %x\n",
1397 qp
->qp_num
, hdr
->ver
, hdr
->len
, hdr
->flags
);
1399 if (!(hdr
->flags
& DESC_DONE_FLAG
)) {
1400 dev_dbg(&qp
->ndev
->pdev
->dev
, "done flag not set\n");
1401 qp
->rx_ring_empty
++;
1405 if (hdr
->flags
& LINK_DOWN_FLAG
) {
1406 dev_dbg(&qp
->ndev
->pdev
->dev
, "link down flag set\n");
1407 ntb_qp_link_down(qp
);
1412 if (hdr
->ver
!= (u32
)qp
->rx_pkts
) {
1413 dev_dbg(&qp
->ndev
->pdev
->dev
,
1414 "version mismatch, expected %llu - got %u\n",
1415 qp
->rx_pkts
, hdr
->ver
);
1420 entry
= ntb_list_mv(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
, &qp
->rx_post_q
);
1422 dev_dbg(&qp
->ndev
->pdev
->dev
, "no receive buffer\n");
1423 qp
->rx_err_no_buf
++;
1427 entry
->rx_hdr
= hdr
;
1428 entry
->rx_index
= qp
->rx_index
;
1430 if (hdr
->len
> entry
->len
) {
1431 dev_dbg(&qp
->ndev
->pdev
->dev
,
1432 "receive buffer overflow! Wanted %d got %d\n",
1433 hdr
->len
, entry
->len
);
1437 entry
->flags
|= DESC_DONE_FLAG
;
1439 ntb_complete_rxc(qp
);
1441 dev_dbg(&qp
->ndev
->pdev
->dev
,
1442 "RX OK index %u ver %u size %d into buf size %d\n",
1443 qp
->rx_index
, hdr
->ver
, hdr
->len
, entry
->len
);
1445 qp
->rx_bytes
+= hdr
->len
;
1448 entry
->len
= hdr
->len
;
1450 ntb_async_rx(entry
, offset
);
1454 qp
->rx_index
%= qp
->rx_max_entry
;
1459 static void ntb_transport_rxc_db(unsigned long data
)
1461 struct ntb_transport_qp
*qp
= (void *)data
;
1464 dev_dbg(&qp
->ndev
->pdev
->dev
, "%s: doorbell %d received\n",
1465 __func__
, qp
->qp_num
);
1467 /* Limit the number of packets processed in a single interrupt to
1468 * provide fairness to others
1470 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
1471 rc
= ntb_process_rxc(qp
);
1476 if (i
&& qp
->rx_dma_chan
)
1477 dma_async_issue_pending(qp
->rx_dma_chan
);
1479 if (i
== qp
->rx_max_entry
) {
1480 /* there is more work to do */
1482 tasklet_schedule(&qp
->rxc_db_work
);
1483 } else if (ntb_db_read(qp
->ndev
) & BIT_ULL(qp
->qp_num
)) {
1484 /* the doorbell bit is set: clear it */
1485 ntb_db_clear(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1486 /* ntb_db_read ensures ntb_db_clear write is committed */
1487 ntb_db_read(qp
->ndev
);
1489 /* an interrupt may have arrived between finishing
1490 * ntb_process_rxc and clearing the doorbell bit:
1491 * there might be some more work to do.
1494 tasklet_schedule(&qp
->rxc_db_work
);
1498 static void ntb_tx_copy_callback(void *data
,
1499 const struct dmaengine_result
*res
)
1501 struct ntb_queue_entry
*entry
= data
;
1502 struct ntb_transport_qp
*qp
= entry
->qp
;
1503 struct ntb_payload_header __iomem
*hdr
= entry
->tx_hdr
;
1505 /* we need to check DMA results if we are using DMA */
1507 enum dmaengine_tx_result dma_err
= res
->result
;
1510 case DMA_TRANS_READ_FAILED
:
1511 case DMA_TRANS_WRITE_FAILED
:
1513 case DMA_TRANS_ABORTED
:
1515 void __iomem
*offset
=
1516 qp
->tx_mw
+ qp
->tx_max_frame
*
1519 /* resubmit via CPU */
1520 ntb_memcpy_tx(entry
, offset
);
1525 case DMA_TRANS_NOERROR
:
1531 iowrite32(entry
->flags
| DESC_DONE_FLAG
, &hdr
->flags
);
1533 ntb_peer_db_set(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1535 /* The entry length can only be zero if the packet is intended to be a
1536 * "link down" or similar. Since no payload is being sent in these
1537 * cases, there is nothing to add to the completion queue.
1539 if (entry
->len
> 0) {
1540 qp
->tx_bytes
+= entry
->len
;
1543 qp
->tx_handler(qp
, qp
->cb_data
, entry
->cb_data
,
1547 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
, &qp
->tx_free_q
);
1550 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
)
1552 #ifdef ARCH_HAS_NOCACHE_UACCESS
1554 * Using non-temporal mov to improve performance on non-cached
1555 * writes, even though we aren't actually copying from user space.
1557 __copy_from_user_inatomic_nocache(offset
, entry
->buf
, entry
->len
);
1559 memcpy_toio(offset
, entry
->buf
, entry
->len
);
1562 /* Ensure that the data is fully copied out before setting the flags */
1565 ntb_tx_copy_callback(entry
, NULL
);
1568 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
1569 struct ntb_queue_entry
*entry
)
1571 struct dma_async_tx_descriptor
*txd
;
1572 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1573 struct dma_device
*device
;
1574 size_t len
= entry
->len
;
1575 void *buf
= entry
->buf
;
1576 size_t dest_off
, buff_off
;
1577 struct dmaengine_unmap_data
*unmap
;
1579 dma_cookie_t cookie
;
1581 device
= chan
->device
;
1582 dest
= qp
->tx_mw_phys
+ qp
->tx_max_frame
* entry
->tx_index
;
1583 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1584 dest_off
= (size_t)dest
& ~PAGE_MASK
;
1586 if (!is_dma_copy_aligned(device
, buff_off
, dest_off
, len
))
1589 unmap
= dmaengine_get_unmap_data(device
->dev
, 1, GFP_NOWAIT
);
1594 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(buf
),
1595 buff_off
, len
, DMA_TO_DEVICE
);
1596 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1601 txd
= device
->device_prep_dma_memcpy(chan
, dest
, unmap
->addr
[0], len
,
1602 DMA_PREP_INTERRUPT
);
1606 txd
->callback_result
= ntb_tx_copy_callback
;
1607 txd
->callback_param
= entry
;
1608 dma_set_unmap(txd
, unmap
);
1610 cookie
= dmaengine_submit(txd
);
1611 if (dma_submit_error(cookie
))
1614 dmaengine_unmap_put(unmap
);
1616 dma_async_issue_pending(chan
);
1620 dmaengine_unmap_put(unmap
);
1622 dmaengine_unmap_put(unmap
);
1627 static void ntb_async_tx(struct ntb_transport_qp
*qp
,
1628 struct ntb_queue_entry
*entry
)
1630 struct ntb_payload_header __iomem
*hdr
;
1631 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1632 void __iomem
*offset
;
1635 entry
->tx_index
= qp
->tx_index
;
1636 offset
= qp
->tx_mw
+ qp
->tx_max_frame
* entry
->tx_index
;
1637 hdr
= offset
+ qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1638 entry
->tx_hdr
= hdr
;
1640 iowrite32(entry
->len
, &hdr
->len
);
1641 iowrite32((u32
)qp
->tx_pkts
, &hdr
->ver
);
1646 if (entry
->len
< copy_bytes
)
1649 res
= ntb_async_tx_submit(qp
, entry
);
1653 if (!entry
->retries
)
1659 ntb_memcpy_tx(entry
, offset
);
1663 static int ntb_process_tx(struct ntb_transport_qp
*qp
,
1664 struct ntb_queue_entry
*entry
)
1666 if (qp
->tx_index
== qp
->remote_rx_info
->entry
) {
1671 if (entry
->len
> qp
->tx_max_frame
- sizeof(struct ntb_payload_header
)) {
1673 qp
->tx_handler(qp
, qp
->cb_data
, NULL
, -EIO
);
1675 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1680 ntb_async_tx(qp
, entry
);
1683 qp
->tx_index
%= qp
->tx_max_entry
;
1690 static void ntb_send_link_down(struct ntb_transport_qp
*qp
)
1692 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
1693 struct ntb_queue_entry
*entry
;
1696 if (!qp
->link_is_up
)
1699 dev_info(&pdev
->dev
, "qp %d: Send Link Down\n", qp
->qp_num
);
1701 for (i
= 0; i
< NTB_LINK_DOWN_TIMEOUT
; i
++) {
1702 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1711 entry
->cb_data
= NULL
;
1714 entry
->flags
= LINK_DOWN_FLAG
;
1716 rc
= ntb_process_tx(qp
, entry
);
1718 dev_err(&pdev
->dev
, "ntb: QP%d unable to send linkdown msg\n",
1721 ntb_qp_link_down_reset(qp
);
1724 static bool ntb_dma_filter_fn(struct dma_chan
*chan
, void *node
)
1726 return dev_to_node(&chan
->dev
->device
) == (int)(unsigned long)node
;
1730 * ntb_transport_create_queue - Create a new NTB transport layer queue
1731 * @rx_handler: receive callback function
1732 * @tx_handler: transmit callback function
1733 * @event_handler: event callback function
1735 * Create a new NTB transport layer queue and provide the queue with a callback
1736 * routine for both transmit and receive. The receive callback routine will be
1737 * used to pass up data when the transport has received it on the queue. The
1738 * transmit callback routine will be called when the transport has completed the
1739 * transmission of the data on the queue and the data is ready to be freed.
1741 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1743 struct ntb_transport_qp
*
1744 ntb_transport_create_queue(void *data
, struct device
*client_dev
,
1745 const struct ntb_queue_handlers
*handlers
)
1747 struct ntb_dev
*ndev
;
1748 struct pci_dev
*pdev
;
1749 struct ntb_transport_ctx
*nt
;
1750 struct ntb_queue_entry
*entry
;
1751 struct ntb_transport_qp
*qp
;
1753 unsigned int free_queue
;
1754 dma_cap_mask_t dma_mask
;
1758 ndev
= dev_ntb(client_dev
->parent
);
1762 node
= dev_to_node(&ndev
->dev
);
1764 free_queue
= ffs(nt
->qp_bitmap_free
);
1768 /* decrement free_queue to make it zero based */
1771 qp
= &nt
->qp_vec
[free_queue
];
1772 qp_bit
= BIT_ULL(qp
->qp_num
);
1774 nt
->qp_bitmap_free
&= ~qp_bit
;
1777 qp
->rx_handler
= handlers
->rx_handler
;
1778 qp
->tx_handler
= handlers
->tx_handler
;
1779 qp
->event_handler
= handlers
->event_handler
;
1781 dma_cap_zero(dma_mask
);
1782 dma_cap_set(DMA_MEMCPY
, dma_mask
);
1786 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1787 (void *)(unsigned long)node
);
1788 if (!qp
->tx_dma_chan
)
1789 dev_info(&pdev
->dev
, "Unable to allocate TX DMA channel\n");
1792 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1793 (void *)(unsigned long)node
);
1794 if (!qp
->rx_dma_chan
)
1795 dev_info(&pdev
->dev
, "Unable to allocate RX DMA channel\n");
1797 qp
->tx_dma_chan
= NULL
;
1798 qp
->rx_dma_chan
= NULL
;
1801 dev_dbg(&pdev
->dev
, "Using %s memcpy for TX\n",
1802 qp
->tx_dma_chan
? "DMA" : "CPU");
1804 dev_dbg(&pdev
->dev
, "Using %s memcpy for RX\n",
1805 qp
->rx_dma_chan
? "DMA" : "CPU");
1807 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1808 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1813 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
1816 qp
->rx_alloc_entry
= NTB_QP_DEF_NUM_ENTRIES
;
1818 for (i
= 0; i
< qp
->tx_max_entry
; i
++) {
1819 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1824 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1828 ntb_db_clear(qp
->ndev
, qp_bit
);
1829 ntb_db_clear_mask(qp
->ndev
, qp_bit
);
1831 dev_info(&pdev
->dev
, "NTB Transport QP %d created\n", qp
->qp_num
);
1836 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1839 qp
->rx_alloc_entry
= 0;
1840 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1842 if (qp
->tx_dma_chan
)
1843 dma_release_channel(qp
->tx_dma_chan
);
1844 if (qp
->rx_dma_chan
)
1845 dma_release_channel(qp
->rx_dma_chan
);
1846 nt
->qp_bitmap_free
|= qp_bit
;
1850 EXPORT_SYMBOL_GPL(ntb_transport_create_queue
);
1853 * ntb_transport_free_queue - Frees NTB transport queue
1854 * @qp: NTB queue to be freed
1856 * Frees NTB transport queue
1858 void ntb_transport_free_queue(struct ntb_transport_qp
*qp
)
1860 struct pci_dev
*pdev
;
1861 struct ntb_queue_entry
*entry
;
1867 pdev
= qp
->ndev
->pdev
;
1871 if (qp
->tx_dma_chan
) {
1872 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1873 /* Putting the dma_chan to NULL will force any new traffic to be
1874 * processed by the CPU instead of the DAM engine
1876 qp
->tx_dma_chan
= NULL
;
1878 /* Try to be nice and wait for any queued DMA engine
1879 * transactions to process before smashing it with a rock
1881 dma_sync_wait(chan
, qp
->last_cookie
);
1882 dmaengine_terminate_all(chan
);
1883 dma_release_channel(chan
);
1886 if (qp
->rx_dma_chan
) {
1887 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1888 /* Putting the dma_chan to NULL will force any new traffic to be
1889 * processed by the CPU instead of the DAM engine
1891 qp
->rx_dma_chan
= NULL
;
1893 /* Try to be nice and wait for any queued DMA engine
1894 * transactions to process before smashing it with a rock
1896 dma_sync_wait(chan
, qp
->last_cookie
);
1897 dmaengine_terminate_all(chan
);
1898 dma_release_channel(chan
);
1901 qp_bit
= BIT_ULL(qp
->qp_num
);
1903 ntb_db_set_mask(qp
->ndev
, qp_bit
);
1904 tasklet_kill(&qp
->rxc_db_work
);
1906 cancel_delayed_work_sync(&qp
->link_work
);
1909 qp
->rx_handler
= NULL
;
1910 qp
->tx_handler
= NULL
;
1911 qp
->event_handler
= NULL
;
1913 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1916 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
))) {
1917 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_pend_q\n");
1921 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_post_q
))) {
1922 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_post_q\n");
1926 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1929 qp
->transport
->qp_bitmap_free
|= qp_bit
;
1931 dev_info(&pdev
->dev
, "NTB Transport QP %d freed\n", qp
->qp_num
);
1933 EXPORT_SYMBOL_GPL(ntb_transport_free_queue
);
1936 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1937 * @qp: NTB queue to be freed
1938 * @len: pointer to variable to write enqueued buffers length
1940 * Dequeues unused buffers from receive queue. Should only be used during
1943 * RETURNS: NULL error value on error, or void* for success.
1945 void *ntb_transport_rx_remove(struct ntb_transport_qp
*qp
, unsigned int *len
)
1947 struct ntb_queue_entry
*entry
;
1950 if (!qp
|| qp
->client_ready
)
1953 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
);
1957 buf
= entry
->cb_data
;
1960 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
1964 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove
);
1967 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1968 * @qp: NTB transport layer queue the entry is to be enqueued on
1969 * @cb: per buffer pointer for callback function to use
1970 * @data: pointer to data buffer that incoming packets will be copied into
1971 * @len: length of the data buffer
1973 * Enqueue a new receive buffer onto the transport queue into which a NTB
1974 * payload can be received into.
1976 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1978 int ntb_transport_rx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1981 struct ntb_queue_entry
*entry
;
1986 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
);
1990 entry
->cb_data
= cb
;
1996 entry
->rx_index
= 0;
1998 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
2001 tasklet_schedule(&qp
->rxc_db_work
);
2005 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue
);
2008 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2009 * @qp: NTB transport layer queue the entry is to be enqueued on
2010 * @cb: per buffer pointer for callback function to use
2011 * @data: pointer to data buffer that will be sent
2012 * @len: length of the data buffer
2014 * Enqueue a new transmit buffer onto the transport queue from which a NTB
2015 * payload will be transmitted. This assumes that a lock is being held to
2016 * serialize access to the qp.
2018 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2020 int ntb_transport_tx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
2023 struct ntb_queue_entry
*entry
;
2026 if (!qp
|| !qp
->link_is_up
|| !len
)
2029 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
2031 qp
->tx_err_no_buf
++;
2035 entry
->cb_data
= cb
;
2041 entry
->tx_index
= 0;
2043 rc
= ntb_process_tx(qp
, entry
);
2045 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
2050 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue
);
2053 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2054 * @qp: NTB transport layer queue to be enabled
2056 * Notify NTB transport layer of client readiness to use queue
2058 void ntb_transport_link_up(struct ntb_transport_qp
*qp
)
2063 qp
->client_ready
= true;
2065 if (qp
->transport
->link_is_up
)
2066 schedule_delayed_work(&qp
->link_work
, 0);
2068 EXPORT_SYMBOL_GPL(ntb_transport_link_up
);
2071 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2072 * @qp: NTB transport layer queue to be disabled
2074 * Notify NTB transport layer of client's desire to no longer receive data on
2075 * transport queue specified. It is the client's responsibility to ensure all
2076 * entries on queue are purged or otherwise handled appropriately.
2078 void ntb_transport_link_down(struct ntb_transport_qp
*qp
)
2085 qp
->client_ready
= false;
2087 val
= ntb_spad_read(qp
->ndev
, QP_LINKS
);
2089 ntb_peer_spad_write(qp
->ndev
, QP_LINKS
,
2090 val
& ~BIT(qp
->qp_num
));
2093 ntb_send_link_down(qp
);
2095 cancel_delayed_work_sync(&qp
->link_work
);
2097 EXPORT_SYMBOL_GPL(ntb_transport_link_down
);
2100 * ntb_transport_link_query - Query transport link state
2101 * @qp: NTB transport layer queue to be queried
2103 * Query connectivity to the remote system of the NTB transport queue
2105 * RETURNS: true for link up or false for link down
2107 bool ntb_transport_link_query(struct ntb_transport_qp
*qp
)
2112 return qp
->link_is_up
;
2114 EXPORT_SYMBOL_GPL(ntb_transport_link_query
);
2117 * ntb_transport_qp_num - Query the qp number
2118 * @qp: NTB transport layer queue to be queried
2120 * Query qp number of the NTB transport queue
2122 * RETURNS: a zero based number specifying the qp number
2124 unsigned char ntb_transport_qp_num(struct ntb_transport_qp
*qp
)
2131 EXPORT_SYMBOL_GPL(ntb_transport_qp_num
);
2134 * ntb_transport_max_size - Query the max payload size of a qp
2135 * @qp: NTB transport layer queue to be queried
2137 * Query the maximum payload size permissible on the given qp
2139 * RETURNS: the max payload size of a qp
2141 unsigned int ntb_transport_max_size(struct ntb_transport_qp
*qp
)
2143 unsigned int max_size
;
2144 unsigned int copy_align
;
2145 struct dma_chan
*rx_chan
, *tx_chan
;
2150 rx_chan
= qp
->rx_dma_chan
;
2151 tx_chan
= qp
->tx_dma_chan
;
2153 copy_align
= max(rx_chan
? rx_chan
->device
->copy_align
: 0,
2154 tx_chan
? tx_chan
->device
->copy_align
: 0);
2156 /* If DMA engine usage is possible, try to find the max size for that */
2157 max_size
= qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
2158 max_size
= round_down(max_size
, 1 << copy_align
);
2162 EXPORT_SYMBOL_GPL(ntb_transport_max_size
);
2164 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp
*qp
)
2166 unsigned int head
= qp
->tx_index
;
2167 unsigned int tail
= qp
->remote_rx_info
->entry
;
2169 return tail
> head
? tail
- head
: qp
->tx_max_entry
+ tail
- head
;
2171 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry
);
2173 static void ntb_transport_doorbell_callback(void *data
, int vector
)
2175 struct ntb_transport_ctx
*nt
= data
;
2176 struct ntb_transport_qp
*qp
;
2178 unsigned int qp_num
;
2180 db_bits
= (nt
->qp_bitmap
& ~nt
->qp_bitmap_free
&
2181 ntb_db_vector_mask(nt
->ndev
, vector
));
2184 qp_num
= __ffs(db_bits
);
2185 qp
= &nt
->qp_vec
[qp_num
];
2188 tasklet_schedule(&qp
->rxc_db_work
);
2190 db_bits
&= ~BIT_ULL(qp_num
);
2194 static const struct ntb_ctx_ops ntb_transport_ops
= {
2195 .link_event
= ntb_transport_event_callback
,
2196 .db_event
= ntb_transport_doorbell_callback
,
2199 static struct ntb_client ntb_transport_client
= {
2201 .probe
= ntb_transport_probe
,
2202 .remove
= ntb_transport_free
,
2206 static int __init
ntb_transport_init(void)
2210 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC
, NTB_TRANSPORT_VER
);
2212 if (debugfs_initialized())
2213 nt_debugfs_dir
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
2215 rc
= bus_register(&ntb_transport_bus
);
2219 rc
= ntb_register_client(&ntb_transport_client
);
2226 bus_unregister(&ntb_transport_bus
);
2228 debugfs_remove_recursive(nt_debugfs_dir
);
2231 module_init(ntb_transport_init
);
2233 static void __exit
ntb_transport_exit(void)
2235 ntb_unregister_client(&ntb_transport_client
);
2236 bus_unregister(&ntb_transport_bus
);
2237 debugfs_remove_recursive(nt_debugfs_dir
);
2239 module_exit(ntb_transport_exit
);