2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
8 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
17 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 * PCIe NTB Transport Linux driver
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
50 #include <linux/debugfs.h>
51 #include <linux/delay.h>
52 #include <linux/dmaengine.h>
53 #include <linux/dma-mapping.h>
54 #include <linux/errno.h>
55 #include <linux/export.h>
56 #include <linux/interrupt.h>
57 #include <linux/module.h>
58 #include <linux/pci.h>
59 #include <linux/slab.h>
60 #include <linux/types.h>
61 #include <linux/uaccess.h>
62 #include "linux/ntb.h"
63 #include "linux/ntb_transport.h"
65 #define NTB_TRANSPORT_VERSION 4
66 #define NTB_TRANSPORT_VER "4"
67 #define NTB_TRANSPORT_NAME "ntb_transport"
68 #define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
70 MODULE_DESCRIPTION(NTB_TRANSPORT_DESC
);
71 MODULE_VERSION(NTB_TRANSPORT_VER
);
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_AUTHOR("Intel Corporation");
75 static unsigned long max_mw_size
;
76 module_param(max_mw_size
, ulong
, 0644);
77 MODULE_PARM_DESC(max_mw_size
, "Limit size of large memory windows");
79 static unsigned int transport_mtu
= 0x10000;
80 module_param(transport_mtu
, uint
, 0644);
81 MODULE_PARM_DESC(transport_mtu
, "Maximum size of NTB transport packets");
83 static unsigned char max_num_clients
;
84 module_param(max_num_clients
, byte
, 0644);
85 MODULE_PARM_DESC(max_num_clients
, "Maximum number of NTB transport clients");
87 static unsigned int copy_bytes
= 1024;
88 module_param(copy_bytes
, uint
, 0644);
89 MODULE_PARM_DESC(copy_bytes
, "Threshold under which NTB will use the CPU to copy instead of DMA");
92 module_param(use_dma
, bool, 0644);
93 MODULE_PARM_DESC(use_dma
, "Use DMA engine to perform large data copy");
95 static struct dentry
*nt_debugfs_dir
;
97 struct ntb_queue_entry
{
98 /* ntb_queue list reference */
99 struct list_head entry
;
100 /* pointers to data to be transferred */
106 struct ntb_transport_qp
*qp
;
108 struct ntb_payload_header __iomem
*tx_hdr
;
109 struct ntb_payload_header
*rx_hdr
;
118 struct ntb_transport_qp
{
119 struct ntb_transport_ctx
*transport
;
120 struct ntb_dev
*ndev
;
122 struct dma_chan
*tx_dma_chan
;
123 struct dma_chan
*rx_dma_chan
;
129 u8 qp_num
; /* Only 64 QP's are allowed. 0-63 */
132 struct ntb_rx_info __iomem
*rx_info
;
133 struct ntb_rx_info
*remote_rx_info
;
135 void (*tx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
136 void *data
, int len
);
137 struct list_head tx_free_q
;
138 spinlock_t ntb_tx_free_q_lock
;
140 dma_addr_t tx_mw_phys
;
141 unsigned int tx_index
;
142 unsigned int tx_max_entry
;
143 unsigned int tx_max_frame
;
145 void (*rx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
146 void *data
, int len
);
147 struct list_head rx_post_q
;
148 struct list_head rx_pend_q
;
149 struct list_head rx_free_q
;
150 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
151 spinlock_t ntb_rx_q_lock
;
153 unsigned int rx_index
;
154 unsigned int rx_max_entry
;
155 unsigned int rx_max_frame
;
156 dma_cookie_t last_cookie
;
157 struct tasklet_struct rxc_db_work
;
159 void (*event_handler
)(void *data
, int status
);
160 struct delayed_work link_work
;
161 struct work_struct link_cleanup
;
163 struct dentry
*debugfs_dir
;
164 struct dentry
*debugfs_stats
;
185 struct ntb_transport_mw
{
186 phys_addr_t phys_addr
;
187 resource_size_t phys_size
;
188 resource_size_t xlat_align
;
189 resource_size_t xlat_align_size
;
197 struct ntb_transport_client_dev
{
198 struct list_head entry
;
199 struct ntb_transport_ctx
*nt
;
203 struct ntb_transport_ctx
{
204 struct list_head entry
;
205 struct list_head client_devs
;
207 struct ntb_dev
*ndev
;
209 struct ntb_transport_mw
*mw_vec
;
210 struct ntb_transport_qp
*qp_vec
;
211 unsigned int mw_count
;
212 unsigned int qp_count
;
217 struct delayed_work link_work
;
218 struct work_struct link_cleanup
;
220 struct dentry
*debugfs_node_dir
;
224 DESC_DONE_FLAG
= BIT(0),
225 LINK_DOWN_FLAG
= BIT(1),
228 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
255 #define DMA_RETRIES 20
256 #define DMA_OUT_RESOURCE_TO 50
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
;
262 static int ntb_transport_bus_match(struct device
*dev
,
263 struct device_driver
*drv
)
265 return !strncmp(dev_name(dev
), drv
->name
, strlen(drv
->name
));
268 static int ntb_transport_bus_probe(struct device
*dev
)
270 const struct ntb_transport_client
*client
;
275 client
= drv_client(dev
->driver
);
276 rc
= client
->probe(dev
);
283 static int ntb_transport_bus_remove(struct device
*dev
)
285 const struct ntb_transport_client
*client
;
287 client
= drv_client(dev
->driver
);
295 static struct bus_type ntb_transport_bus
= {
296 .name
= "ntb_transport",
297 .match
= ntb_transport_bus_match
,
298 .probe
= ntb_transport_bus_probe
,
299 .remove
= ntb_transport_bus_remove
,
302 static LIST_HEAD(ntb_transport_list
);
304 static int ntb_bus_init(struct ntb_transport_ctx
*nt
)
306 list_add_tail(&nt
->entry
, &ntb_transport_list
);
310 static void ntb_bus_remove(struct ntb_transport_ctx
*nt
)
312 struct ntb_transport_client_dev
*client_dev
, *cd
;
314 list_for_each_entry_safe(client_dev
, cd
, &nt
->client_devs
, entry
) {
315 dev_err(client_dev
->dev
.parent
, "%s still attached to bus, removing\n",
316 dev_name(&client_dev
->dev
));
317 list_del(&client_dev
->entry
);
318 device_unregister(&client_dev
->dev
);
321 list_del(&nt
->entry
);
324 static void ntb_transport_client_release(struct device
*dev
)
326 struct ntb_transport_client_dev
*client_dev
;
328 client_dev
= dev_client_dev(dev
);
333 * ntb_transport_unregister_client_dev - Unregister NTB client device
334 * @device_name: Name of NTB client device
336 * Unregister an NTB client device with the NTB transport layer
338 void ntb_transport_unregister_client_dev(char *device_name
)
340 struct ntb_transport_client_dev
*client
, *cd
;
341 struct ntb_transport_ctx
*nt
;
343 list_for_each_entry(nt
, &ntb_transport_list
, entry
)
344 list_for_each_entry_safe(client
, cd
, &nt
->client_devs
, entry
)
345 if (!strncmp(dev_name(&client
->dev
), device_name
,
346 strlen(device_name
))) {
347 list_del(&client
->entry
);
348 device_unregister(&client
->dev
);
351 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev
);
354 * ntb_transport_register_client_dev - Register NTB client device
355 * @device_name: Name of NTB client device
357 * Register an NTB client device with the NTB transport layer
359 int ntb_transport_register_client_dev(char *device_name
)
361 struct ntb_transport_client_dev
*client_dev
;
362 struct ntb_transport_ctx
*nt
;
366 if (list_empty(&ntb_transport_list
))
369 list_for_each_entry(nt
, &ntb_transport_list
, entry
) {
372 node
= dev_to_node(&nt
->ndev
->dev
);
374 client_dev
= kzalloc_node(sizeof(*client_dev
),
381 dev
= &client_dev
->dev
;
383 /* setup and register client devices */
384 dev_set_name(dev
, "%s%d", device_name
, i
);
385 dev
->bus
= &ntb_transport_bus
;
386 dev
->release
= ntb_transport_client_release
;
387 dev
->parent
= &nt
->ndev
->dev
;
389 rc
= device_register(dev
);
395 list_add_tail(&client_dev
->entry
, &nt
->client_devs
);
402 ntb_transport_unregister_client_dev(device_name
);
406 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev
);
409 * ntb_transport_register_client - Register NTB client driver
410 * @drv: NTB client driver to be registered
412 * Register an NTB client driver with the NTB transport layer
414 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
416 int ntb_transport_register_client(struct ntb_transport_client
*drv
)
418 drv
->driver
.bus
= &ntb_transport_bus
;
420 if (list_empty(&ntb_transport_list
))
423 return driver_register(&drv
->driver
);
425 EXPORT_SYMBOL_GPL(ntb_transport_register_client
);
428 * ntb_transport_unregister_client - Unregister NTB client driver
429 * @drv: NTB client driver to be unregistered
431 * Unregister an NTB client driver with the NTB transport layer
433 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
435 void ntb_transport_unregister_client(struct ntb_transport_client
*drv
)
437 driver_unregister(&drv
->driver
);
439 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client
);
441 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
444 struct ntb_transport_qp
*qp
;
446 ssize_t ret
, out_offset
, out_count
;
448 qp
= filp
->private_data
;
450 if (!qp
|| !qp
->link_is_up
)
455 buf
= kmalloc(out_count
, GFP_KERNEL
);
460 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
461 "\nNTB QP stats:\n\n");
462 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
463 "rx_bytes - \t%llu\n", qp
->rx_bytes
);
464 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
465 "rx_pkts - \t%llu\n", qp
->rx_pkts
);
466 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
467 "rx_memcpy - \t%llu\n", qp
->rx_memcpy
);
468 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
469 "rx_async - \t%llu\n", qp
->rx_async
);
470 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
471 "rx_ring_empty - %llu\n", qp
->rx_ring_empty
);
472 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
473 "rx_err_no_buf - %llu\n", qp
->rx_err_no_buf
);
474 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
475 "rx_err_oflow - \t%llu\n", qp
->rx_err_oflow
);
476 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
477 "rx_err_ver - \t%llu\n", qp
->rx_err_ver
);
478 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
479 "rx_buff - \t0x%p\n", qp
->rx_buff
);
480 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
481 "rx_index - \t%u\n", qp
->rx_index
);
482 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
483 "rx_max_entry - \t%u\n\n", qp
->rx_max_entry
);
485 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
486 "tx_bytes - \t%llu\n", qp
->tx_bytes
);
487 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
488 "tx_pkts - \t%llu\n", qp
->tx_pkts
);
489 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
490 "tx_memcpy - \t%llu\n", qp
->tx_memcpy
);
491 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
492 "tx_async - \t%llu\n", qp
->tx_async
);
493 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
494 "tx_ring_full - \t%llu\n", qp
->tx_ring_full
);
495 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
496 "tx_err_no_buf - %llu\n", qp
->tx_err_no_buf
);
497 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
498 "tx_mw - \t0x%p\n", qp
->tx_mw
);
499 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
500 "tx_index (H) - \t%u\n", qp
->tx_index
);
501 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
503 qp
->remote_rx_info
->entry
);
504 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
505 "tx_max_entry - \t%u\n", qp
->tx_max_entry
);
506 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
508 ntb_transport_tx_free_entry(qp
));
509 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
510 "DMA tx prep err - \t%llu\n",
511 qp
->dma_tx_prep_err
);
512 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
513 "DMA rx prep err - \t%llu\n",
514 qp
->dma_rx_prep_err
);
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 unsigned int rx_size
, num_qps_mw
;
601 unsigned int mw_num
, mw_count
, qp_count
;
604 mw_count
= nt
->mw_count
;
605 qp_count
= nt
->qp_count
;
607 mw_num
= QP_TO_MW(nt
, qp_num
);
608 mw
= &nt
->mw_vec
[mw_num
];
613 if (qp_count
% mw_count
&& mw_num
+ 1 < qp_count
/ mw_count
)
614 num_qps_mw
= qp_count
/ mw_count
+ 1;
616 num_qps_mw
= qp_count
/ mw_count
;
618 rx_size
= (unsigned int)mw
->xlat_size
/ num_qps_mw
;
619 qp
->rx_buff
= mw
->virt_addr
+ rx_size
* (qp_num
/ mw_count
);
620 rx_size
-= sizeof(struct ntb_rx_info
);
622 qp
->remote_rx_info
= qp
->rx_buff
+ rx_size
;
624 /* Due to housekeeping, there must be atleast 2 buffs */
625 qp
->rx_max_frame
= min(transport_mtu
, rx_size
/ 2);
626 qp
->rx_max_entry
= rx_size
/ qp
->rx_max_frame
;
629 qp
->remote_rx_info
->entry
= qp
->rx_max_entry
- 1;
631 /* setup the hdr offsets with 0's */
632 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
633 void *offset
= (qp
->rx_buff
+ qp
->rx_max_frame
* (i
+ 1) -
634 sizeof(struct ntb_payload_header
));
635 memset(offset
, 0, sizeof(struct ntb_payload_header
));
645 static void ntb_free_mw(struct ntb_transport_ctx
*nt
, int num_mw
)
647 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
648 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
653 ntb_mw_clear_trans(nt
->ndev
, num_mw
);
654 dma_free_coherent(&pdev
->dev
, mw
->buff_size
,
655 mw
->virt_addr
, mw
->dma_addr
);
658 mw
->virt_addr
= NULL
;
661 static int ntb_set_mw(struct ntb_transport_ctx
*nt
, int num_mw
,
662 resource_size_t size
)
664 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
665 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
666 size_t xlat_size
, buff_size
;
672 xlat_size
= round_up(size
, mw
->xlat_align_size
);
673 buff_size
= round_up(size
, mw
->xlat_align
);
675 /* No need to re-setup */
676 if (mw
->xlat_size
== xlat_size
)
680 ntb_free_mw(nt
, num_mw
);
682 /* Alloc memory for receiving data. Must be aligned */
683 mw
->xlat_size
= xlat_size
;
684 mw
->buff_size
= buff_size
;
686 mw
->virt_addr
= dma_alloc_coherent(&pdev
->dev
, buff_size
,
687 &mw
->dma_addr
, GFP_KERNEL
);
688 if (!mw
->virt_addr
) {
691 dev_err(&pdev
->dev
, "Unable to alloc MW buff of size %zu\n",
697 * we must ensure that the memory address allocated is BAR size
698 * aligned in order for the XLAT register to take the value. This
699 * is a requirement of the hardware. It is recommended to setup CMA
700 * for BAR sizes equal or greater than 4MB.
702 if (!IS_ALIGNED(mw
->dma_addr
, mw
->xlat_align
)) {
703 dev_err(&pdev
->dev
, "DMA memory %pad is not aligned\n",
705 ntb_free_mw(nt
, num_mw
);
709 /* Notify HW the memory location of the receive buffer */
710 rc
= ntb_mw_set_trans(nt
->ndev
, num_mw
, mw
->dma_addr
, mw
->xlat_size
);
712 dev_err(&pdev
->dev
, "Unable to set mw%d translation", num_mw
);
713 ntb_free_mw(nt
, num_mw
);
720 static void ntb_qp_link_down_reset(struct ntb_transport_qp
*qp
)
722 qp
->link_is_up
= false;
729 qp
->rx_ring_empty
= 0;
730 qp
->rx_err_no_buf
= 0;
731 qp
->rx_err_oflow
= 0;
737 qp
->tx_ring_full
= 0;
738 qp
->tx_err_no_buf
= 0;
741 qp
->dma_tx_prep_err
= 0;
742 qp
->dma_rx_prep_err
= 0;
745 static void ntb_qp_link_cleanup(struct ntb_transport_qp
*qp
)
747 struct ntb_transport_ctx
*nt
= qp
->transport
;
748 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
750 dev_info(&pdev
->dev
, "qp %d: Link Cleanup\n", qp
->qp_num
);
752 cancel_delayed_work_sync(&qp
->link_work
);
753 ntb_qp_link_down_reset(qp
);
755 if (qp
->event_handler
)
756 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
759 static void ntb_qp_link_cleanup_work(struct work_struct
*work
)
761 struct ntb_transport_qp
*qp
= container_of(work
,
762 struct ntb_transport_qp
,
764 struct ntb_transport_ctx
*nt
= qp
->transport
;
766 ntb_qp_link_cleanup(qp
);
769 schedule_delayed_work(&qp
->link_work
,
770 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
773 static void ntb_qp_link_down(struct ntb_transport_qp
*qp
)
775 schedule_work(&qp
->link_cleanup
);
778 static void ntb_transport_link_cleanup(struct ntb_transport_ctx
*nt
)
780 struct ntb_transport_qp
*qp
;
784 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
786 /* Pass along the info to any clients */
787 for (i
= 0; i
< nt
->qp_count
; i
++)
788 if (qp_bitmap_alloc
& BIT_ULL(i
)) {
790 ntb_qp_link_cleanup(qp
);
791 cancel_work_sync(&qp
->link_cleanup
);
792 cancel_delayed_work_sync(&qp
->link_work
);
796 cancel_delayed_work_sync(&nt
->link_work
);
798 /* The scratchpad registers keep the values if the remote side
799 * goes down, blast them now to give them a sane value the next
800 * time they are accessed
802 for (i
= 0; i
< MAX_SPAD
; i
++)
803 ntb_spad_write(nt
->ndev
, i
, 0);
806 static void ntb_transport_link_cleanup_work(struct work_struct
*work
)
808 struct ntb_transport_ctx
*nt
=
809 container_of(work
, struct ntb_transport_ctx
, link_cleanup
);
811 ntb_transport_link_cleanup(nt
);
814 static void ntb_transport_event_callback(void *data
)
816 struct ntb_transport_ctx
*nt
= data
;
818 if (ntb_link_is_up(nt
->ndev
, NULL
, NULL
) == 1)
819 schedule_delayed_work(&nt
->link_work
, 0);
821 schedule_work(&nt
->link_cleanup
);
824 static void ntb_transport_link_work(struct work_struct
*work
)
826 struct ntb_transport_ctx
*nt
=
827 container_of(work
, struct ntb_transport_ctx
, link_work
.work
);
828 struct ntb_dev
*ndev
= nt
->ndev
;
829 struct pci_dev
*pdev
= ndev
->pdev
;
830 resource_size_t size
;
834 /* send the local info, in the opposite order of the way we read it */
835 for (i
= 0; i
< nt
->mw_count
; i
++) {
836 size
= nt
->mw_vec
[i
].phys_size
;
838 if (max_mw_size
&& size
> max_mw_size
)
841 spad
= MW0_SZ_HIGH
+ (i
* 2);
842 ntb_peer_spad_write(ndev
, spad
, upper_32_bits(size
));
844 spad
= MW0_SZ_LOW
+ (i
* 2);
845 ntb_peer_spad_write(ndev
, spad
, lower_32_bits(size
));
848 ntb_peer_spad_write(ndev
, NUM_MWS
, nt
->mw_count
);
850 ntb_peer_spad_write(ndev
, NUM_QPS
, nt
->qp_count
);
852 ntb_peer_spad_write(ndev
, VERSION
, NTB_TRANSPORT_VERSION
);
854 /* Query the remote side for its info */
855 val
= ntb_spad_read(ndev
, VERSION
);
856 dev_dbg(&pdev
->dev
, "Remote version = %d\n", val
);
857 if (val
!= NTB_TRANSPORT_VERSION
)
860 val
= ntb_spad_read(ndev
, NUM_QPS
);
861 dev_dbg(&pdev
->dev
, "Remote max number of qps = %d\n", val
);
862 if (val
!= nt
->qp_count
)
865 val
= ntb_spad_read(ndev
, NUM_MWS
);
866 dev_dbg(&pdev
->dev
, "Remote number of mws = %d\n", val
);
867 if (val
!= nt
->mw_count
)
870 for (i
= 0; i
< nt
->mw_count
; i
++) {
873 val
= ntb_spad_read(ndev
, MW0_SZ_HIGH
+ (i
* 2));
874 val64
= (u64
)val
<< 32;
876 val
= ntb_spad_read(ndev
, MW0_SZ_LOW
+ (i
* 2));
879 dev_dbg(&pdev
->dev
, "Remote MW%d size = %#llx\n", i
, val64
);
881 rc
= ntb_set_mw(nt
, i
, val64
);
886 nt
->link_is_up
= true;
888 for (i
= 0; i
< nt
->qp_count
; i
++) {
889 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[i
];
891 ntb_transport_setup_qp_mw(nt
, i
);
893 if (qp
->client_ready
)
894 schedule_delayed_work(&qp
->link_work
, 0);
900 for (i
= 0; i
< nt
->mw_count
; i
++)
903 /* if there's an actual failure, we should just bail */
905 ntb_link_disable(ndev
);
910 if (ntb_link_is_up(ndev
, NULL
, NULL
) == 1)
911 schedule_delayed_work(&nt
->link_work
,
912 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
915 static void ntb_qp_link_work(struct work_struct
*work
)
917 struct ntb_transport_qp
*qp
= container_of(work
,
918 struct ntb_transport_qp
,
920 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
921 struct ntb_transport_ctx
*nt
= qp
->transport
;
924 WARN_ON(!nt
->link_is_up
);
926 val
= ntb_spad_read(nt
->ndev
, QP_LINKS
);
928 ntb_peer_spad_write(nt
->ndev
, QP_LINKS
, val
| BIT(qp
->qp_num
));
930 /* query remote spad for qp ready bits */
931 ntb_peer_spad_read(nt
->ndev
, QP_LINKS
);
932 dev_dbg_ratelimited(&pdev
->dev
, "Remote QP link status = %x\n", val
);
934 /* See if the remote side is up */
935 if (val
& BIT(qp
->qp_num
)) {
936 dev_info(&pdev
->dev
, "qp %d: Link Up\n", qp
->qp_num
);
937 qp
->link_is_up
= true;
940 if (qp
->event_handler
)
941 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
944 tasklet_schedule(&qp
->rxc_db_work
);
945 } else if (nt
->link_is_up
)
946 schedule_delayed_work(&qp
->link_work
,
947 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
950 static int ntb_transport_init_queue(struct ntb_transport_ctx
*nt
,
953 struct ntb_transport_qp
*qp
;
955 resource_size_t mw_size
;
956 unsigned int num_qps_mw
, tx_size
;
957 unsigned int mw_num
, mw_count
, qp_count
;
960 mw_count
= nt
->mw_count
;
961 qp_count
= nt
->qp_count
;
963 mw_num
= QP_TO_MW(nt
, qp_num
);
965 qp
= &nt
->qp_vec
[qp_num
];
969 qp
->client_ready
= false;
970 qp
->event_handler
= NULL
;
971 ntb_qp_link_down_reset(qp
);
973 if (qp_count
% mw_count
&& mw_num
+ 1 < qp_count
/ mw_count
)
974 num_qps_mw
= qp_count
/ mw_count
+ 1;
976 num_qps_mw
= qp_count
/ mw_count
;
978 mw_base
= nt
->mw_vec
[mw_num
].phys_addr
;
979 mw_size
= nt
->mw_vec
[mw_num
].phys_size
;
981 tx_size
= (unsigned int)mw_size
/ num_qps_mw
;
982 qp_offset
= tx_size
* (qp_num
/ mw_count
);
984 qp
->tx_mw
= nt
->mw_vec
[mw_num
].vbase
+ qp_offset
;
988 qp
->tx_mw_phys
= mw_base
+ qp_offset
;
992 tx_size
-= sizeof(struct ntb_rx_info
);
993 qp
->rx_info
= qp
->tx_mw
+ tx_size
;
995 /* Due to housekeeping, there must be atleast 2 buffs */
996 qp
->tx_max_frame
= min(transport_mtu
, tx_size
/ 2);
997 qp
->tx_max_entry
= tx_size
/ qp
->tx_max_frame
;
999 if (nt
->debugfs_node_dir
) {
1000 char debugfs_name
[4];
1002 snprintf(debugfs_name
, 4, "qp%d", qp_num
);
1003 qp
->debugfs_dir
= debugfs_create_dir(debugfs_name
,
1004 nt
->debugfs_node_dir
);
1006 qp
->debugfs_stats
= debugfs_create_file("stats", S_IRUSR
,
1007 qp
->debugfs_dir
, qp
,
1008 &ntb_qp_debugfs_stats
);
1010 qp
->debugfs_dir
= NULL
;
1011 qp
->debugfs_stats
= NULL
;
1014 INIT_DELAYED_WORK(&qp
->link_work
, ntb_qp_link_work
);
1015 INIT_WORK(&qp
->link_cleanup
, ntb_qp_link_cleanup_work
);
1017 spin_lock_init(&qp
->ntb_rx_q_lock
);
1018 spin_lock_init(&qp
->ntb_tx_free_q_lock
);
1020 INIT_LIST_HEAD(&qp
->rx_post_q
);
1021 INIT_LIST_HEAD(&qp
->rx_pend_q
);
1022 INIT_LIST_HEAD(&qp
->rx_free_q
);
1023 INIT_LIST_HEAD(&qp
->tx_free_q
);
1025 tasklet_init(&qp
->rxc_db_work
, ntb_transport_rxc_db
,
1031 static int ntb_transport_probe(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1033 struct ntb_transport_ctx
*nt
;
1034 struct ntb_transport_mw
*mw
;
1035 unsigned int mw_count
, qp_count
;
1040 if (ntb_db_is_unsafe(ndev
))
1042 "doorbell is unsafe, proceed anyway...\n");
1043 if (ntb_spad_is_unsafe(ndev
))
1045 "scratchpad is unsafe, proceed anyway...\n");
1047 node
= dev_to_node(&ndev
->dev
);
1049 nt
= kzalloc_node(sizeof(*nt
), GFP_KERNEL
, node
);
1055 mw_count
= ntb_mw_count(ndev
);
1057 nt
->mw_count
= mw_count
;
1059 nt
->mw_vec
= kzalloc_node(mw_count
* sizeof(*nt
->mw_vec
),
1066 for (i
= 0; i
< mw_count
; i
++) {
1067 mw
= &nt
->mw_vec
[i
];
1069 rc
= ntb_mw_get_range(ndev
, i
, &mw
->phys_addr
, &mw
->phys_size
,
1070 &mw
->xlat_align
, &mw
->xlat_align_size
);
1074 mw
->vbase
= ioremap_wc(mw
->phys_addr
, mw
->phys_size
);
1082 mw
->virt_addr
= NULL
;
1086 qp_bitmap
= ntb_db_valid_mask(ndev
);
1088 qp_count
= ilog2(qp_bitmap
);
1089 if (max_num_clients
&& max_num_clients
< qp_count
)
1090 qp_count
= max_num_clients
;
1091 else if (mw_count
< qp_count
)
1092 qp_count
= mw_count
;
1094 qp_bitmap
&= BIT_ULL(qp_count
) - 1;
1096 nt
->qp_count
= qp_count
;
1097 nt
->qp_bitmap
= qp_bitmap
;
1098 nt
->qp_bitmap_free
= qp_bitmap
;
1100 nt
->qp_vec
= kzalloc_node(qp_count
* sizeof(*nt
->qp_vec
),
1107 if (nt_debugfs_dir
) {
1108 nt
->debugfs_node_dir
=
1109 debugfs_create_dir(pci_name(ndev
->pdev
),
1113 for (i
= 0; i
< qp_count
; i
++) {
1114 rc
= ntb_transport_init_queue(nt
, i
);
1119 INIT_DELAYED_WORK(&nt
->link_work
, ntb_transport_link_work
);
1120 INIT_WORK(&nt
->link_cleanup
, ntb_transport_link_cleanup_work
);
1122 rc
= ntb_set_ctx(ndev
, nt
, &ntb_transport_ops
);
1126 INIT_LIST_HEAD(&nt
->client_devs
);
1127 rc
= ntb_bus_init(nt
);
1131 nt
->link_is_up
= false;
1132 ntb_link_enable(ndev
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
1133 ntb_link_event(ndev
);
1138 ntb_clear_ctx(ndev
);
1143 mw
= &nt
->mw_vec
[i
];
1152 static void ntb_transport_free(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1154 struct ntb_transport_ctx
*nt
= ndev
->ctx
;
1155 struct ntb_transport_qp
*qp
;
1156 u64 qp_bitmap_alloc
;
1159 ntb_transport_link_cleanup(nt
);
1160 cancel_work_sync(&nt
->link_cleanup
);
1161 cancel_delayed_work_sync(&nt
->link_work
);
1163 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
1165 /* verify that all the qp's are freed */
1166 for (i
= 0; i
< nt
->qp_count
; i
++) {
1167 qp
= &nt
->qp_vec
[i
];
1168 if (qp_bitmap_alloc
& BIT_ULL(i
))
1169 ntb_transport_free_queue(qp
);
1170 debugfs_remove_recursive(qp
->debugfs_dir
);
1173 ntb_link_disable(ndev
);
1174 ntb_clear_ctx(ndev
);
1178 for (i
= nt
->mw_count
; i
--; ) {
1180 iounmap(nt
->mw_vec
[i
].vbase
);
1188 static void ntb_complete_rxc(struct ntb_transport_qp
*qp
)
1190 struct ntb_queue_entry
*entry
;
1193 unsigned long irqflags
;
1195 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1197 while (!list_empty(&qp
->rx_post_q
)) {
1198 entry
= list_first_entry(&qp
->rx_post_q
,
1199 struct ntb_queue_entry
, entry
);
1200 if (!(entry
->flags
& DESC_DONE_FLAG
))
1203 entry
->rx_hdr
->flags
= 0;
1204 iowrite32(entry
->index
, &qp
->rx_info
->entry
);
1206 cb_data
= entry
->cb_data
;
1209 list_move_tail(&entry
->entry
, &qp
->rx_free_q
);
1211 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1213 if (qp
->rx_handler
&& qp
->client_ready
)
1214 qp
->rx_handler(qp
, qp
->cb_data
, cb_data
, len
);
1216 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1219 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1222 static void ntb_rx_copy_callback(void *data
)
1224 struct ntb_queue_entry
*entry
= data
;
1226 entry
->flags
|= DESC_DONE_FLAG
;
1228 ntb_complete_rxc(entry
->qp
);
1231 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
)
1233 void *buf
= entry
->buf
;
1234 size_t len
= entry
->len
;
1236 memcpy(buf
, offset
, len
);
1238 /* Ensure that the data is fully copied out before clearing the flag */
1241 ntb_rx_copy_callback(entry
);
1244 static void ntb_async_rx(struct ntb_queue_entry
*entry
, void *offset
)
1246 struct dma_async_tx_descriptor
*txd
;
1247 struct ntb_transport_qp
*qp
= entry
->qp
;
1248 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1249 struct dma_device
*device
;
1250 size_t pay_off
, buff_off
, len
;
1251 struct dmaengine_unmap_data
*unmap
;
1252 dma_cookie_t cookie
;
1253 void *buf
= entry
->buf
;
1261 if (len
< copy_bytes
)
1264 device
= chan
->device
;
1265 pay_off
= (size_t)offset
& ~PAGE_MASK
;
1266 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1268 if (!is_dma_copy_aligned(device
, pay_off
, buff_off
, len
))
1271 unmap
= dmaengine_get_unmap_data(device
->dev
, 2, GFP_NOWAIT
);
1276 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(offset
),
1277 pay_off
, len
, DMA_TO_DEVICE
);
1278 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1283 unmap
->addr
[1] = dma_map_page(device
->dev
, virt_to_page(buf
),
1284 buff_off
, len
, DMA_FROM_DEVICE
);
1285 if (dma_mapping_error(device
->dev
, unmap
->addr
[1]))
1288 unmap
->from_cnt
= 1;
1290 for (retries
= 0; retries
< DMA_RETRIES
; retries
++) {
1291 txd
= device
->device_prep_dma_memcpy(chan
, unmap
->addr
[1],
1292 unmap
->addr
[0], len
,
1293 DMA_PREP_INTERRUPT
);
1297 set_current_state(TASK_INTERRUPTIBLE
);
1298 schedule_timeout(DMA_OUT_RESOURCE_TO
);
1302 qp
->dma_rx_prep_err
++;
1306 txd
->callback
= ntb_rx_copy_callback
;
1307 txd
->callback_param
= entry
;
1308 dma_set_unmap(txd
, unmap
);
1310 cookie
= dmaengine_submit(txd
);
1311 if (dma_submit_error(cookie
))
1314 dmaengine_unmap_put(unmap
);
1316 qp
->last_cookie
= cookie
;
1323 dmaengine_unmap_put(unmap
);
1325 dmaengine_unmap_put(unmap
);
1327 ntb_memcpy_rx(entry
, offset
);
1331 static int ntb_process_rxc(struct ntb_transport_qp
*qp
)
1333 struct ntb_payload_header
*hdr
;
1334 struct ntb_queue_entry
*entry
;
1337 offset
= qp
->rx_buff
+ qp
->rx_max_frame
* qp
->rx_index
;
1338 hdr
= offset
+ qp
->rx_max_frame
- sizeof(struct ntb_payload_header
);
1340 dev_dbg(&qp
->ndev
->pdev
->dev
, "qp %d: RX ver %u len %d flags %x\n",
1341 qp
->qp_num
, hdr
->ver
, hdr
->len
, hdr
->flags
);
1343 if (!(hdr
->flags
& DESC_DONE_FLAG
)) {
1344 dev_dbg(&qp
->ndev
->pdev
->dev
, "done flag not set\n");
1345 qp
->rx_ring_empty
++;
1349 if (hdr
->flags
& LINK_DOWN_FLAG
) {
1350 dev_dbg(&qp
->ndev
->pdev
->dev
, "link down flag set\n");
1351 ntb_qp_link_down(qp
);
1356 if (hdr
->ver
!= (u32
)qp
->rx_pkts
) {
1357 dev_dbg(&qp
->ndev
->pdev
->dev
,
1358 "version mismatch, expected %llu - got %u\n",
1359 qp
->rx_pkts
, hdr
->ver
);
1364 entry
= ntb_list_mv(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
, &qp
->rx_post_q
);
1366 dev_dbg(&qp
->ndev
->pdev
->dev
, "no receive buffer\n");
1367 qp
->rx_err_no_buf
++;
1371 entry
->rx_hdr
= hdr
;
1372 entry
->index
= qp
->rx_index
;
1374 if (hdr
->len
> entry
->len
) {
1375 dev_dbg(&qp
->ndev
->pdev
->dev
,
1376 "receive buffer overflow! Wanted %d got %d\n",
1377 hdr
->len
, entry
->len
);
1381 entry
->flags
|= DESC_DONE_FLAG
;
1383 ntb_complete_rxc(qp
);
1385 dev_dbg(&qp
->ndev
->pdev
->dev
,
1386 "RX OK index %u ver %u size %d into buf size %d\n",
1387 qp
->rx_index
, hdr
->ver
, hdr
->len
, entry
->len
);
1389 qp
->rx_bytes
+= hdr
->len
;
1392 entry
->len
= hdr
->len
;
1394 ntb_async_rx(entry
, offset
);
1398 qp
->rx_index
%= qp
->rx_max_entry
;
1403 static void ntb_transport_rxc_db(unsigned long data
)
1405 struct ntb_transport_qp
*qp
= (void *)data
;
1408 dev_dbg(&qp
->ndev
->pdev
->dev
, "%s: doorbell %d received\n",
1409 __func__
, qp
->qp_num
);
1411 /* Limit the number of packets processed in a single interrupt to
1412 * provide fairness to others
1414 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
1415 rc
= ntb_process_rxc(qp
);
1420 if (i
&& qp
->rx_dma_chan
)
1421 dma_async_issue_pending(qp
->rx_dma_chan
);
1423 if (i
== qp
->rx_max_entry
) {
1424 /* there is more work to do */
1426 tasklet_schedule(&qp
->rxc_db_work
);
1427 } else if (ntb_db_read(qp
->ndev
) & BIT_ULL(qp
->qp_num
)) {
1428 /* the doorbell bit is set: clear it */
1429 ntb_db_clear(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1430 /* ntb_db_read ensures ntb_db_clear write is committed */
1431 ntb_db_read(qp
->ndev
);
1433 /* an interrupt may have arrived between finishing
1434 * ntb_process_rxc and clearing the doorbell bit:
1435 * there might be some more work to do.
1438 tasklet_schedule(&qp
->rxc_db_work
);
1442 static void ntb_tx_copy_callback(void *data
)
1444 struct ntb_queue_entry
*entry
= data
;
1445 struct ntb_transport_qp
*qp
= entry
->qp
;
1446 struct ntb_payload_header __iomem
*hdr
= entry
->tx_hdr
;
1448 iowrite32(entry
->flags
| DESC_DONE_FLAG
, &hdr
->flags
);
1450 ntb_peer_db_set(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1452 /* The entry length can only be zero if the packet is intended to be a
1453 * "link down" or similar. Since no payload is being sent in these
1454 * cases, there is nothing to add to the completion queue.
1456 if (entry
->len
> 0) {
1457 qp
->tx_bytes
+= entry
->len
;
1460 qp
->tx_handler(qp
, qp
->cb_data
, entry
->cb_data
,
1464 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
, &qp
->tx_free_q
);
1467 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
)
1469 #ifdef ARCH_HAS_NOCACHE_UACCESS
1471 * Using non-temporal mov to improve performance on non-cached
1472 * writes, even though we aren't actually copying from user space.
1474 __copy_from_user_inatomic_nocache(offset
, entry
->buf
, entry
->len
);
1476 memcpy_toio(offset
, entry
->buf
, entry
->len
);
1479 /* Ensure that the data is fully copied out before setting the flags */
1482 ntb_tx_copy_callback(entry
);
1485 static void ntb_async_tx(struct ntb_transport_qp
*qp
,
1486 struct ntb_queue_entry
*entry
)
1488 struct ntb_payload_header __iomem
*hdr
;
1489 struct dma_async_tx_descriptor
*txd
;
1490 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1491 struct dma_device
*device
;
1492 size_t dest_off
, buff_off
;
1493 struct dmaengine_unmap_data
*unmap
;
1495 dma_cookie_t cookie
;
1496 void __iomem
*offset
;
1497 size_t len
= entry
->len
;
1498 void *buf
= entry
->buf
;
1501 offset
= qp
->tx_mw
+ qp
->tx_max_frame
* qp
->tx_index
;
1502 hdr
= offset
+ qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1503 entry
->tx_hdr
= hdr
;
1505 iowrite32(entry
->len
, &hdr
->len
);
1506 iowrite32((u32
)qp
->tx_pkts
, &hdr
->ver
);
1511 if (len
< copy_bytes
)
1514 device
= chan
->device
;
1515 dest
= qp
->tx_mw_phys
+ qp
->tx_max_frame
* qp
->tx_index
;
1516 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1517 dest_off
= (size_t)dest
& ~PAGE_MASK
;
1519 if (!is_dma_copy_aligned(device
, buff_off
, dest_off
, len
))
1522 unmap
= dmaengine_get_unmap_data(device
->dev
, 1, GFP_NOWAIT
);
1527 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(buf
),
1528 buff_off
, len
, DMA_TO_DEVICE
);
1529 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1534 for (retries
= 0; retries
< DMA_RETRIES
; retries
++) {
1535 txd
= device
->device_prep_dma_memcpy(chan
, dest
, unmap
->addr
[0],
1536 len
, DMA_PREP_INTERRUPT
);
1540 set_current_state(TASK_INTERRUPTIBLE
);
1541 schedule_timeout(DMA_OUT_RESOURCE_TO
);
1545 qp
->dma_tx_prep_err
++;
1549 txd
->callback
= ntb_tx_copy_callback
;
1550 txd
->callback_param
= entry
;
1551 dma_set_unmap(txd
, unmap
);
1553 cookie
= dmaengine_submit(txd
);
1554 if (dma_submit_error(cookie
))
1557 dmaengine_unmap_put(unmap
);
1559 dma_async_issue_pending(chan
);
1564 dmaengine_unmap_put(unmap
);
1566 dmaengine_unmap_put(unmap
);
1568 ntb_memcpy_tx(entry
, offset
);
1572 static int ntb_process_tx(struct ntb_transport_qp
*qp
,
1573 struct ntb_queue_entry
*entry
)
1575 if (qp
->tx_index
== qp
->remote_rx_info
->entry
) {
1580 if (entry
->len
> qp
->tx_max_frame
- sizeof(struct ntb_payload_header
)) {
1582 qp
->tx_handler(qp
, qp
->cb_data
, NULL
, -EIO
);
1584 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1589 ntb_async_tx(qp
, entry
);
1592 qp
->tx_index
%= qp
->tx_max_entry
;
1599 static void ntb_send_link_down(struct ntb_transport_qp
*qp
)
1601 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
1602 struct ntb_queue_entry
*entry
;
1605 if (!qp
->link_is_up
)
1608 dev_info(&pdev
->dev
, "qp %d: Send Link Down\n", qp
->qp_num
);
1610 for (i
= 0; i
< NTB_LINK_DOWN_TIMEOUT
; i
++) {
1611 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1620 entry
->cb_data
= NULL
;
1623 entry
->flags
= LINK_DOWN_FLAG
;
1625 rc
= ntb_process_tx(qp
, entry
);
1627 dev_err(&pdev
->dev
, "ntb: QP%d unable to send linkdown msg\n",
1630 ntb_qp_link_down_reset(qp
);
1633 static bool ntb_dma_filter_fn(struct dma_chan
*chan
, void *node
)
1635 return dev_to_node(&chan
->dev
->device
) == (int)(unsigned long)node
;
1639 * ntb_transport_create_queue - Create a new NTB transport layer queue
1640 * @rx_handler: receive callback function
1641 * @tx_handler: transmit callback function
1642 * @event_handler: event callback function
1644 * Create a new NTB transport layer queue and provide the queue with a callback
1645 * routine for both transmit and receive. The receive callback routine will be
1646 * used to pass up data when the transport has received it on the queue. The
1647 * transmit callback routine will be called when the transport has completed the
1648 * transmission of the data on the queue and the data is ready to be freed.
1650 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1652 struct ntb_transport_qp
*
1653 ntb_transport_create_queue(void *data
, struct device
*client_dev
,
1654 const struct ntb_queue_handlers
*handlers
)
1656 struct ntb_dev
*ndev
;
1657 struct pci_dev
*pdev
;
1658 struct ntb_transport_ctx
*nt
;
1659 struct ntb_queue_entry
*entry
;
1660 struct ntb_transport_qp
*qp
;
1662 unsigned int free_queue
;
1663 dma_cap_mask_t dma_mask
;
1667 ndev
= dev_ntb(client_dev
->parent
);
1671 node
= dev_to_node(&ndev
->dev
);
1673 free_queue
= ffs(nt
->qp_bitmap
);
1677 /* decrement free_queue to make it zero based */
1680 qp
= &nt
->qp_vec
[free_queue
];
1681 qp_bit
= BIT_ULL(qp
->qp_num
);
1683 nt
->qp_bitmap_free
&= ~qp_bit
;
1686 qp
->rx_handler
= handlers
->rx_handler
;
1687 qp
->tx_handler
= handlers
->tx_handler
;
1688 qp
->event_handler
= handlers
->event_handler
;
1690 dma_cap_zero(dma_mask
);
1691 dma_cap_set(DMA_MEMCPY
, dma_mask
);
1695 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1696 (void *)(unsigned long)node
);
1697 if (!qp
->tx_dma_chan
)
1698 dev_info(&pdev
->dev
, "Unable to allocate TX DMA channel\n");
1701 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1702 (void *)(unsigned long)node
);
1703 if (!qp
->rx_dma_chan
)
1704 dev_info(&pdev
->dev
, "Unable to allocate RX DMA channel\n");
1706 qp
->tx_dma_chan
= NULL
;
1707 qp
->rx_dma_chan
= NULL
;
1710 dev_dbg(&pdev
->dev
, "Using %s memcpy for TX\n",
1711 qp
->tx_dma_chan
? "DMA" : "CPU");
1713 dev_dbg(&pdev
->dev
, "Using %s memcpy for RX\n",
1714 qp
->rx_dma_chan
? "DMA" : "CPU");
1716 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1717 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1722 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
1726 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1727 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1732 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1736 ntb_db_clear(qp
->ndev
, qp_bit
);
1737 ntb_db_clear_mask(qp
->ndev
, qp_bit
);
1739 dev_info(&pdev
->dev
, "NTB Transport QP %d created\n", qp
->qp_num
);
1744 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1747 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1749 if (qp
->tx_dma_chan
)
1750 dma_release_channel(qp
->tx_dma_chan
);
1751 if (qp
->rx_dma_chan
)
1752 dma_release_channel(qp
->rx_dma_chan
);
1753 nt
->qp_bitmap_free
|= qp_bit
;
1757 EXPORT_SYMBOL_GPL(ntb_transport_create_queue
);
1760 * ntb_transport_free_queue - Frees NTB transport queue
1761 * @qp: NTB queue to be freed
1763 * Frees NTB transport queue
1765 void ntb_transport_free_queue(struct ntb_transport_qp
*qp
)
1767 struct pci_dev
*pdev
;
1768 struct ntb_queue_entry
*entry
;
1774 pdev
= qp
->ndev
->pdev
;
1778 if (qp
->tx_dma_chan
) {
1779 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1780 /* Putting the dma_chan to NULL will force any new traffic to be
1781 * processed by the CPU instead of the DAM engine
1783 qp
->tx_dma_chan
= NULL
;
1785 /* Try to be nice and wait for any queued DMA engine
1786 * transactions to process before smashing it with a rock
1788 dma_sync_wait(chan
, qp
->last_cookie
);
1789 dmaengine_terminate_all(chan
);
1790 dma_release_channel(chan
);
1793 if (qp
->rx_dma_chan
) {
1794 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1795 /* Putting the dma_chan to NULL will force any new traffic to be
1796 * processed by the CPU instead of the DAM engine
1798 qp
->rx_dma_chan
= NULL
;
1800 /* Try to be nice and wait for any queued DMA engine
1801 * transactions to process before smashing it with a rock
1803 dma_sync_wait(chan
, qp
->last_cookie
);
1804 dmaengine_terminate_all(chan
);
1805 dma_release_channel(chan
);
1808 qp_bit
= BIT_ULL(qp
->qp_num
);
1810 ntb_db_set_mask(qp
->ndev
, qp_bit
);
1811 tasklet_kill(&qp
->rxc_db_work
);
1813 cancel_delayed_work_sync(&qp
->link_work
);
1816 qp
->rx_handler
= NULL
;
1817 qp
->tx_handler
= NULL
;
1818 qp
->event_handler
= NULL
;
1820 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1823 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
))) {
1824 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_pend_q\n");
1828 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_post_q
))) {
1829 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_post_q\n");
1833 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1836 qp
->transport
->qp_bitmap_free
|= qp_bit
;
1838 dev_info(&pdev
->dev
, "NTB Transport QP %d freed\n", qp
->qp_num
);
1840 EXPORT_SYMBOL_GPL(ntb_transport_free_queue
);
1843 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1844 * @qp: NTB queue to be freed
1845 * @len: pointer to variable to write enqueued buffers length
1847 * Dequeues unused buffers from receive queue. Should only be used during
1850 * RETURNS: NULL error value on error, or void* for success.
1852 void *ntb_transport_rx_remove(struct ntb_transport_qp
*qp
, unsigned int *len
)
1854 struct ntb_queue_entry
*entry
;
1857 if (!qp
|| qp
->client_ready
)
1860 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
);
1864 buf
= entry
->cb_data
;
1867 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
1871 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove
);
1874 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1875 * @qp: NTB transport layer queue the entry is to be enqueued on
1876 * @cb: per buffer pointer for callback function to use
1877 * @data: pointer to data buffer that incoming packets will be copied into
1878 * @len: length of the data buffer
1880 * Enqueue a new receive buffer onto the transport queue into which a NTB
1881 * payload can be received into.
1883 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1885 int ntb_transport_rx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1888 struct ntb_queue_entry
*entry
;
1893 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
);
1897 entry
->cb_data
= cb
;
1902 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
1905 tasklet_schedule(&qp
->rxc_db_work
);
1909 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue
);
1912 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1913 * @qp: NTB transport layer queue the entry is to be enqueued on
1914 * @cb: per buffer pointer for callback function to use
1915 * @data: pointer to data buffer that will be sent
1916 * @len: length of the data buffer
1918 * Enqueue a new transmit buffer onto the transport queue from which a NTB
1919 * payload will be transmitted. This assumes that a lock is being held to
1920 * serialize access to the qp.
1922 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1924 int ntb_transport_tx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1927 struct ntb_queue_entry
*entry
;
1930 if (!qp
|| !qp
->link_is_up
|| !len
)
1933 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1935 qp
->tx_err_no_buf
++;
1939 entry
->cb_data
= cb
;
1944 rc
= ntb_process_tx(qp
, entry
);
1946 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1951 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue
);
1954 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1955 * @qp: NTB transport layer queue to be enabled
1957 * Notify NTB transport layer of client readiness to use queue
1959 void ntb_transport_link_up(struct ntb_transport_qp
*qp
)
1964 qp
->client_ready
= true;
1966 if (qp
->transport
->link_is_up
)
1967 schedule_delayed_work(&qp
->link_work
, 0);
1969 EXPORT_SYMBOL_GPL(ntb_transport_link_up
);
1972 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1973 * @qp: NTB transport layer queue to be disabled
1975 * Notify NTB transport layer of client's desire to no longer receive data on
1976 * transport queue specified. It is the client's responsibility to ensure all
1977 * entries on queue are purged or otherwise handled appropriately.
1979 void ntb_transport_link_down(struct ntb_transport_qp
*qp
)
1986 qp
->client_ready
= false;
1988 val
= ntb_spad_read(qp
->ndev
, QP_LINKS
);
1990 ntb_peer_spad_write(qp
->ndev
, QP_LINKS
,
1991 val
& ~BIT(qp
->qp_num
));
1994 ntb_send_link_down(qp
);
1996 cancel_delayed_work_sync(&qp
->link_work
);
1998 EXPORT_SYMBOL_GPL(ntb_transport_link_down
);
2001 * ntb_transport_link_query - Query transport link state
2002 * @qp: NTB transport layer queue to be queried
2004 * Query connectivity to the remote system of the NTB transport queue
2006 * RETURNS: true for link up or false for link down
2008 bool ntb_transport_link_query(struct ntb_transport_qp
*qp
)
2013 return qp
->link_is_up
;
2015 EXPORT_SYMBOL_GPL(ntb_transport_link_query
);
2018 * ntb_transport_qp_num - Query the qp number
2019 * @qp: NTB transport layer queue to be queried
2021 * Query qp number of the NTB transport queue
2023 * RETURNS: a zero based number specifying the qp number
2025 unsigned char ntb_transport_qp_num(struct ntb_transport_qp
*qp
)
2032 EXPORT_SYMBOL_GPL(ntb_transport_qp_num
);
2035 * ntb_transport_max_size - Query the max payload size of a qp
2036 * @qp: NTB transport layer queue to be queried
2038 * Query the maximum payload size permissible on the given qp
2040 * RETURNS: the max payload size of a qp
2042 unsigned int ntb_transport_max_size(struct ntb_transport_qp
*qp
)
2044 unsigned int max_size
;
2045 unsigned int copy_align
;
2046 struct dma_chan
*rx_chan
, *tx_chan
;
2051 rx_chan
= qp
->rx_dma_chan
;
2052 tx_chan
= qp
->tx_dma_chan
;
2054 copy_align
= max(rx_chan
? rx_chan
->device
->copy_align
: 0,
2055 tx_chan
? tx_chan
->device
->copy_align
: 0);
2057 /* If DMA engine usage is possible, try to find the max size for that */
2058 max_size
= qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
2059 max_size
= round_down(max_size
, 1 << copy_align
);
2063 EXPORT_SYMBOL_GPL(ntb_transport_max_size
);
2065 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp
*qp
)
2067 unsigned int head
= qp
->tx_index
;
2068 unsigned int tail
= qp
->remote_rx_info
->entry
;
2070 return tail
> head
? tail
- head
: qp
->tx_max_entry
+ tail
- head
;
2072 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry
);
2074 static void ntb_transport_doorbell_callback(void *data
, int vector
)
2076 struct ntb_transport_ctx
*nt
= data
;
2077 struct ntb_transport_qp
*qp
;
2079 unsigned int qp_num
;
2081 db_bits
= (nt
->qp_bitmap
& ~nt
->qp_bitmap_free
&
2082 ntb_db_vector_mask(nt
->ndev
, vector
));
2085 qp_num
= __ffs(db_bits
);
2086 qp
= &nt
->qp_vec
[qp_num
];
2089 tasklet_schedule(&qp
->rxc_db_work
);
2091 db_bits
&= ~BIT_ULL(qp_num
);
2095 static const struct ntb_ctx_ops ntb_transport_ops
= {
2096 .link_event
= ntb_transport_event_callback
,
2097 .db_event
= ntb_transport_doorbell_callback
,
2100 static struct ntb_client ntb_transport_client
= {
2102 .probe
= ntb_transport_probe
,
2103 .remove
= ntb_transport_free
,
2107 static int __init
ntb_transport_init(void)
2111 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC
, NTB_TRANSPORT_VER
);
2113 if (debugfs_initialized())
2114 nt_debugfs_dir
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
2116 rc
= bus_register(&ntb_transport_bus
);
2120 rc
= ntb_register_client(&ntb_transport_client
);
2127 bus_unregister(&ntb_transport_bus
);
2129 debugfs_remove_recursive(nt_debugfs_dir
);
2132 module_init(ntb_transport_init
);
2134 static void __exit
ntb_transport_exit(void)
2136 debugfs_remove_recursive(nt_debugfs_dir
);
2138 ntb_unregister_client(&ntb_transport_client
);
2139 bus_unregister(&ntb_transport_bus
);
2141 module_exit(ntb_transport_exit
);