2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2012 Intel Corporation. All rights reserved.
8 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of version 2 of the GNU General Public License as
12 * published by the Free Software Foundation.
16 * Copyright(c) 2012 Intel Corporation. All rights reserved.
17 * Copyright (C) 2015 EMC Corporation. All Rights Reserved.
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * * Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * * Redistributions in binary form must reproduce the above copy
26 * notice, this list of conditions and the following disclaimer in
27 * the documentation and/or other materials provided with the
29 * * Neither the name of Intel Corporation nor the names of its
30 * contributors may be used to endorse or promote products derived
31 * from this software without specific prior written permission.
33 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45 * PCIe NTB Transport Linux driver
47 * Contact Information:
48 * Jon Mason <jon.mason@intel.com>
50 #include <linux/debugfs.h>
51 #include <linux/delay.h>
52 #include <linux/dmaengine.h>
53 #include <linux/dma-mapping.h>
54 #include <linux/errno.h>
55 #include <linux/export.h>
56 #include <linux/interrupt.h>
57 #include <linux/module.h>
58 #include <linux/pci.h>
59 #include <linux/slab.h>
60 #include <linux/types.h>
61 #include <linux/uaccess.h>
62 #include "linux/ntb.h"
63 #include "linux/ntb_transport.h"
65 #define NTB_TRANSPORT_VERSION 4
66 #define NTB_TRANSPORT_VER "4"
67 #define NTB_TRANSPORT_NAME "ntb_transport"
68 #define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB"
69 #define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
71 MODULE_DESCRIPTION(NTB_TRANSPORT_DESC
);
72 MODULE_VERSION(NTB_TRANSPORT_VER
);
73 MODULE_LICENSE("Dual BSD/GPL");
74 MODULE_AUTHOR("Intel Corporation");
76 static unsigned long max_mw_size
;
77 module_param(max_mw_size
, ulong
, 0644);
78 MODULE_PARM_DESC(max_mw_size
, "Limit size of large memory windows");
80 static unsigned int transport_mtu
= 0x10000;
81 module_param(transport_mtu
, uint
, 0644);
82 MODULE_PARM_DESC(transport_mtu
, "Maximum size of NTB transport packets");
84 static unsigned char max_num_clients
;
85 module_param(max_num_clients
, byte
, 0644);
86 MODULE_PARM_DESC(max_num_clients
, "Maximum number of NTB transport clients");
88 static unsigned int copy_bytes
= 1024;
89 module_param(copy_bytes
, uint
, 0644);
90 MODULE_PARM_DESC(copy_bytes
, "Threshold under which NTB will use the CPU to copy instead of DMA");
93 module_param(use_dma
, bool, 0644);
94 MODULE_PARM_DESC(use_dma
, "Use DMA engine to perform large data copy");
96 static struct dentry
*nt_debugfs_dir
;
98 struct ntb_queue_entry
{
99 /* ntb_queue list reference */
100 struct list_head entry
;
101 /* pointers to data to be transferred */
108 unsigned int tx_index
;
109 unsigned int rx_index
;
111 struct ntb_transport_qp
*qp
;
113 struct ntb_payload_header __iomem
*tx_hdr
;
114 struct ntb_payload_header
*rx_hdr
;
122 struct ntb_transport_qp
{
123 struct ntb_transport_ctx
*transport
;
124 struct ntb_dev
*ndev
;
126 struct dma_chan
*tx_dma_chan
;
127 struct dma_chan
*rx_dma_chan
;
133 u8 qp_num
; /* Only 64 QP's are allowed. 0-63 */
136 struct ntb_rx_info __iomem
*rx_info
;
137 struct ntb_rx_info
*remote_rx_info
;
139 void (*tx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
140 void *data
, int len
);
141 struct list_head tx_free_q
;
142 spinlock_t ntb_tx_free_q_lock
;
144 dma_addr_t tx_mw_phys
;
145 unsigned int tx_index
;
146 unsigned int tx_max_entry
;
147 unsigned int tx_max_frame
;
149 void (*rx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
150 void *data
, int len
);
151 struct list_head rx_post_q
;
152 struct list_head rx_pend_q
;
153 struct list_head rx_free_q
;
154 /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
155 spinlock_t ntb_rx_q_lock
;
157 unsigned int rx_index
;
158 unsigned int rx_max_entry
;
159 unsigned int rx_max_frame
;
160 unsigned int rx_alloc_entry
;
161 dma_cookie_t last_cookie
;
162 struct tasklet_struct rxc_db_work
;
164 void (*event_handler
)(void *data
, int status
);
165 struct delayed_work link_work
;
166 struct work_struct link_cleanup
;
168 struct dentry
*debugfs_dir
;
169 struct dentry
*debugfs_stats
;
190 struct ntb_transport_mw
{
191 phys_addr_t phys_addr
;
192 resource_size_t phys_size
;
193 resource_size_t xlat_align
;
194 resource_size_t xlat_align_size
;
202 struct ntb_transport_client_dev
{
203 struct list_head entry
;
204 struct ntb_transport_ctx
*nt
;
208 struct ntb_transport_ctx
{
209 struct list_head entry
;
210 struct list_head client_devs
;
212 struct ntb_dev
*ndev
;
214 struct ntb_transport_mw
*mw_vec
;
215 struct ntb_transport_qp
*qp_vec
;
216 unsigned int mw_count
;
217 unsigned int qp_count
;
222 struct delayed_work link_work
;
223 struct work_struct link_cleanup
;
225 struct dentry
*debugfs_node_dir
;
229 DESC_DONE_FLAG
= BIT(0),
230 LINK_DOWN_FLAG
= BIT(1),
233 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
257 #define DMA_RETRIES 20
258 #define DMA_OUT_RESOURCE_TO msecs_to_jiffies(50)
260 static void ntb_transport_rxc_db(unsigned long data
);
261 static const struct ntb_ctx_ops ntb_transport_ops
;
262 static struct ntb_client ntb_transport_client
;
263 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
264 struct ntb_queue_entry
*entry
);
265 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
);
266 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
);
267 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
);
270 static int ntb_transport_bus_match(struct device
*dev
,
271 struct device_driver
*drv
)
273 return !strncmp(dev_name(dev
), drv
->name
, strlen(drv
->name
));
276 static int ntb_transport_bus_probe(struct device
*dev
)
278 const struct ntb_transport_client
*client
;
283 client
= drv_client(dev
->driver
);
284 rc
= client
->probe(dev
);
291 static int ntb_transport_bus_remove(struct device
*dev
)
293 const struct ntb_transport_client
*client
;
295 client
= drv_client(dev
->driver
);
303 static struct bus_type ntb_transport_bus
= {
304 .name
= "ntb_transport",
305 .match
= ntb_transport_bus_match
,
306 .probe
= ntb_transport_bus_probe
,
307 .remove
= ntb_transport_bus_remove
,
310 static LIST_HEAD(ntb_transport_list
);
312 static int ntb_bus_init(struct ntb_transport_ctx
*nt
)
314 list_add_tail(&nt
->entry
, &ntb_transport_list
);
318 static void ntb_bus_remove(struct ntb_transport_ctx
*nt
)
320 struct ntb_transport_client_dev
*client_dev
, *cd
;
322 list_for_each_entry_safe(client_dev
, cd
, &nt
->client_devs
, entry
) {
323 dev_err(client_dev
->dev
.parent
, "%s still attached to bus, removing\n",
324 dev_name(&client_dev
->dev
));
325 list_del(&client_dev
->entry
);
326 device_unregister(&client_dev
->dev
);
329 list_del(&nt
->entry
);
332 static void ntb_transport_client_release(struct device
*dev
)
334 struct ntb_transport_client_dev
*client_dev
;
336 client_dev
= dev_client_dev(dev
);
341 * ntb_transport_unregister_client_dev - Unregister NTB client device
342 * @device_name: Name of NTB client device
344 * Unregister an NTB client device with the NTB transport layer
346 void ntb_transport_unregister_client_dev(char *device_name
)
348 struct ntb_transport_client_dev
*client
, *cd
;
349 struct ntb_transport_ctx
*nt
;
351 list_for_each_entry(nt
, &ntb_transport_list
, entry
)
352 list_for_each_entry_safe(client
, cd
, &nt
->client_devs
, entry
)
353 if (!strncmp(dev_name(&client
->dev
), device_name
,
354 strlen(device_name
))) {
355 list_del(&client
->entry
);
356 device_unregister(&client
->dev
);
359 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev
);
362 * ntb_transport_register_client_dev - Register NTB client device
363 * @device_name: Name of NTB client device
365 * Register an NTB client device with the NTB transport layer
367 int ntb_transport_register_client_dev(char *device_name
)
369 struct ntb_transport_client_dev
*client_dev
;
370 struct ntb_transport_ctx
*nt
;
374 if (list_empty(&ntb_transport_list
))
377 list_for_each_entry(nt
, &ntb_transport_list
, entry
) {
380 node
= dev_to_node(&nt
->ndev
->dev
);
382 client_dev
= kzalloc_node(sizeof(*client_dev
),
389 dev
= &client_dev
->dev
;
391 /* setup and register client devices */
392 dev_set_name(dev
, "%s%d", device_name
, i
);
393 dev
->bus
= &ntb_transport_bus
;
394 dev
->release
= ntb_transport_client_release
;
395 dev
->parent
= &nt
->ndev
->dev
;
397 rc
= device_register(dev
);
403 list_add_tail(&client_dev
->entry
, &nt
->client_devs
);
410 ntb_transport_unregister_client_dev(device_name
);
414 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev
);
417 * ntb_transport_register_client - Register NTB client driver
418 * @drv: NTB client driver to be registered
420 * Register an NTB client driver with the NTB transport layer
422 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
424 int ntb_transport_register_client(struct ntb_transport_client
*drv
)
426 drv
->driver
.bus
= &ntb_transport_bus
;
428 if (list_empty(&ntb_transport_list
))
431 return driver_register(&drv
->driver
);
433 EXPORT_SYMBOL_GPL(ntb_transport_register_client
);
436 * ntb_transport_unregister_client - Unregister NTB client driver
437 * @drv: NTB client driver to be unregistered
439 * Unregister an NTB client driver with the NTB transport layer
441 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
443 void ntb_transport_unregister_client(struct ntb_transport_client
*drv
)
445 driver_unregister(&drv
->driver
);
447 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client
);
449 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
452 struct ntb_transport_qp
*qp
;
454 ssize_t ret
, out_offset
, out_count
;
456 qp
= filp
->private_data
;
458 if (!qp
|| !qp
->link_is_up
)
463 buf
= kmalloc(out_count
, GFP_KERNEL
);
468 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
469 "\nNTB QP stats:\n\n");
470 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
471 "rx_bytes - \t%llu\n", qp
->rx_bytes
);
472 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
473 "rx_pkts - \t%llu\n", qp
->rx_pkts
);
474 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
475 "rx_memcpy - \t%llu\n", qp
->rx_memcpy
);
476 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
477 "rx_async - \t%llu\n", qp
->rx_async
);
478 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
479 "rx_ring_empty - %llu\n", qp
->rx_ring_empty
);
480 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
481 "rx_err_no_buf - %llu\n", qp
->rx_err_no_buf
);
482 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
483 "rx_err_oflow - \t%llu\n", qp
->rx_err_oflow
);
484 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
485 "rx_err_ver - \t%llu\n", qp
->rx_err_ver
);
486 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
487 "rx_buff - \t0x%p\n", qp
->rx_buff
);
488 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
489 "rx_index - \t%u\n", qp
->rx_index
);
490 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
491 "rx_max_entry - \t%u\n", qp
->rx_max_entry
);
492 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
493 "rx_alloc_entry - \t%u\n\n", qp
->rx_alloc_entry
);
495 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
496 "tx_bytes - \t%llu\n", qp
->tx_bytes
);
497 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
498 "tx_pkts - \t%llu\n", qp
->tx_pkts
);
499 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
500 "tx_memcpy - \t%llu\n", qp
->tx_memcpy
);
501 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
502 "tx_async - \t%llu\n", qp
->tx_async
);
503 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
504 "tx_ring_full - \t%llu\n", qp
->tx_ring_full
);
505 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
506 "tx_err_no_buf - %llu\n", qp
->tx_err_no_buf
);
507 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
508 "tx_mw - \t0x%p\n", qp
->tx_mw
);
509 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
510 "tx_index (H) - \t%u\n", qp
->tx_index
);
511 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
513 qp
->remote_rx_info
->entry
);
514 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
515 "tx_max_entry - \t%u\n", qp
->tx_max_entry
);
516 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
518 ntb_transport_tx_free_entry(qp
));
519 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
520 "DMA tx prep err - \t%llu\n",
521 qp
->dma_tx_prep_err
);
522 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
523 "DMA rx prep err - \t%llu\n",
524 qp
->dma_rx_prep_err
);
526 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
528 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
529 "Using TX DMA - \t%s\n",
530 qp
->tx_dma_chan
? "Yes" : "No");
531 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
532 "Using RX DMA - \t%s\n",
533 qp
->rx_dma_chan
? "Yes" : "No");
534 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
536 qp
->link_is_up
? "Up" : "Down");
537 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
540 if (out_offset
> out_count
)
541 out_offset
= out_count
;
543 ret
= simple_read_from_buffer(ubuf
, count
, offp
, buf
, out_offset
);
548 static const struct file_operations ntb_qp_debugfs_stats
= {
549 .owner
= THIS_MODULE
,
551 .read
= debugfs_read
,
554 static void ntb_list_add(spinlock_t
*lock
, struct list_head
*entry
,
555 struct list_head
*list
)
559 spin_lock_irqsave(lock
, flags
);
560 list_add_tail(entry
, list
);
561 spin_unlock_irqrestore(lock
, flags
);
564 static struct ntb_queue_entry
*ntb_list_rm(spinlock_t
*lock
,
565 struct list_head
*list
)
567 struct ntb_queue_entry
*entry
;
570 spin_lock_irqsave(lock
, flags
);
571 if (list_empty(list
)) {
575 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
576 list_del(&entry
->entry
);
579 spin_unlock_irqrestore(lock
, flags
);
584 static struct ntb_queue_entry
*ntb_list_mv(spinlock_t
*lock
,
585 struct list_head
*list
,
586 struct list_head
*to_list
)
588 struct ntb_queue_entry
*entry
;
591 spin_lock_irqsave(lock
, flags
);
593 if (list_empty(list
)) {
596 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
597 list_move_tail(&entry
->entry
, to_list
);
600 spin_unlock_irqrestore(lock
, flags
);
605 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx
*nt
,
608 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[qp_num
];
609 struct ntb_transport_mw
*mw
;
610 struct ntb_dev
*ndev
= nt
->ndev
;
611 struct ntb_queue_entry
*entry
;
612 unsigned int rx_size
, num_qps_mw
;
613 unsigned int mw_num
, mw_count
, qp_count
;
617 mw_count
= nt
->mw_count
;
618 qp_count
= nt
->qp_count
;
620 mw_num
= QP_TO_MW(nt
, qp_num
);
621 mw
= &nt
->mw_vec
[mw_num
];
626 if (qp_count
% mw_count
&& mw_num
+ 1 < qp_count
/ mw_count
)
627 num_qps_mw
= qp_count
/ mw_count
+ 1;
629 num_qps_mw
= qp_count
/ mw_count
;
631 rx_size
= (unsigned int)mw
->xlat_size
/ num_qps_mw
;
632 qp
->rx_buff
= mw
->virt_addr
+ rx_size
* (qp_num
/ mw_count
);
633 rx_size
-= sizeof(struct ntb_rx_info
);
635 qp
->remote_rx_info
= qp
->rx_buff
+ rx_size
;
637 /* Due to housekeeping, there must be atleast 2 buffs */
638 qp
->rx_max_frame
= min(transport_mtu
, rx_size
/ 2);
639 qp
->rx_max_entry
= rx_size
/ qp
->rx_max_frame
;
643 * Checking to see if we have more entries than the default.
644 * We should add additional entries if that is the case so we
645 * can be in sync with the transport frames.
647 node
= dev_to_node(&ndev
->dev
);
648 for (i
= qp
->rx_alloc_entry
; i
< qp
->rx_max_entry
; i
++) {
649 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
654 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
656 qp
->rx_alloc_entry
++;
659 qp
->remote_rx_info
->entry
= qp
->rx_max_entry
- 1;
661 /* setup the hdr offsets with 0's */
662 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
663 void *offset
= (qp
->rx_buff
+ qp
->rx_max_frame
* (i
+ 1) -
664 sizeof(struct ntb_payload_header
));
665 memset(offset
, 0, sizeof(struct ntb_payload_header
));
675 static void ntb_free_mw(struct ntb_transport_ctx
*nt
, int num_mw
)
677 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
678 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
683 ntb_mw_clear_trans(nt
->ndev
, num_mw
);
684 dma_free_coherent(&pdev
->dev
, mw
->buff_size
,
685 mw
->virt_addr
, mw
->dma_addr
);
688 mw
->virt_addr
= NULL
;
691 static int ntb_set_mw(struct ntb_transport_ctx
*nt
, int num_mw
,
692 resource_size_t size
)
694 struct ntb_transport_mw
*mw
= &nt
->mw_vec
[num_mw
];
695 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
696 size_t xlat_size
, buff_size
;
702 xlat_size
= round_up(size
, mw
->xlat_align_size
);
703 buff_size
= round_up(size
, mw
->xlat_align
);
705 /* No need to re-setup */
706 if (mw
->xlat_size
== xlat_size
)
710 ntb_free_mw(nt
, num_mw
);
712 /* Alloc memory for receiving data. Must be aligned */
713 mw
->xlat_size
= xlat_size
;
714 mw
->buff_size
= buff_size
;
716 mw
->virt_addr
= dma_alloc_coherent(&pdev
->dev
, buff_size
,
717 &mw
->dma_addr
, GFP_KERNEL
);
718 if (!mw
->virt_addr
) {
721 dev_err(&pdev
->dev
, "Unable to alloc MW buff of size %zu\n",
727 * we must ensure that the memory address allocated is BAR size
728 * aligned in order for the XLAT register to take the value. This
729 * is a requirement of the hardware. It is recommended to setup CMA
730 * for BAR sizes equal or greater than 4MB.
732 if (!IS_ALIGNED(mw
->dma_addr
, mw
->xlat_align
)) {
733 dev_err(&pdev
->dev
, "DMA memory %pad is not aligned\n",
735 ntb_free_mw(nt
, num_mw
);
739 /* Notify HW the memory location of the receive buffer */
740 rc
= ntb_mw_set_trans(nt
->ndev
, num_mw
, mw
->dma_addr
, mw
->xlat_size
);
742 dev_err(&pdev
->dev
, "Unable to set mw%d translation", num_mw
);
743 ntb_free_mw(nt
, num_mw
);
750 static void ntb_qp_link_down_reset(struct ntb_transport_qp
*qp
)
752 qp
->link_is_up
= false;
759 qp
->rx_ring_empty
= 0;
760 qp
->rx_err_no_buf
= 0;
761 qp
->rx_err_oflow
= 0;
767 qp
->tx_ring_full
= 0;
768 qp
->tx_err_no_buf
= 0;
771 qp
->dma_tx_prep_err
= 0;
772 qp
->dma_rx_prep_err
= 0;
775 static void ntb_qp_link_cleanup(struct ntb_transport_qp
*qp
)
777 struct ntb_transport_ctx
*nt
= qp
->transport
;
778 struct pci_dev
*pdev
= nt
->ndev
->pdev
;
780 dev_info(&pdev
->dev
, "qp %d: Link Cleanup\n", qp
->qp_num
);
782 cancel_delayed_work_sync(&qp
->link_work
);
783 ntb_qp_link_down_reset(qp
);
785 if (qp
->event_handler
)
786 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
789 static void ntb_qp_link_cleanup_work(struct work_struct
*work
)
791 struct ntb_transport_qp
*qp
= container_of(work
,
792 struct ntb_transport_qp
,
794 struct ntb_transport_ctx
*nt
= qp
->transport
;
796 ntb_qp_link_cleanup(qp
);
799 schedule_delayed_work(&qp
->link_work
,
800 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
803 static void ntb_qp_link_down(struct ntb_transport_qp
*qp
)
805 schedule_work(&qp
->link_cleanup
);
808 static void ntb_transport_link_cleanup(struct ntb_transport_ctx
*nt
)
810 struct ntb_transport_qp
*qp
;
812 unsigned int i
, count
;
814 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
816 /* Pass along the info to any clients */
817 for (i
= 0; i
< nt
->qp_count
; i
++)
818 if (qp_bitmap_alloc
& BIT_ULL(i
)) {
820 ntb_qp_link_cleanup(qp
);
821 cancel_work_sync(&qp
->link_cleanup
);
822 cancel_delayed_work_sync(&qp
->link_work
);
826 cancel_delayed_work_sync(&nt
->link_work
);
828 /* The scratchpad registers keep the values if the remote side
829 * goes down, blast them now to give them a sane value the next
830 * time they are accessed
832 count
= ntb_spad_count(nt
->ndev
);
833 for (i
= 0; i
< count
; i
++)
834 ntb_spad_write(nt
->ndev
, i
, 0);
837 static void ntb_transport_link_cleanup_work(struct work_struct
*work
)
839 struct ntb_transport_ctx
*nt
=
840 container_of(work
, struct ntb_transport_ctx
, link_cleanup
);
842 ntb_transport_link_cleanup(nt
);
845 static void ntb_transport_event_callback(void *data
)
847 struct ntb_transport_ctx
*nt
= data
;
849 if (ntb_link_is_up(nt
->ndev
, NULL
, NULL
) == 1)
850 schedule_delayed_work(&nt
->link_work
, 0);
852 schedule_work(&nt
->link_cleanup
);
855 static void ntb_transport_link_work(struct work_struct
*work
)
857 struct ntb_transport_ctx
*nt
=
858 container_of(work
, struct ntb_transport_ctx
, link_work
.work
);
859 struct ntb_dev
*ndev
= nt
->ndev
;
860 struct pci_dev
*pdev
= ndev
->pdev
;
861 resource_size_t size
;
865 /* send the local info, in the opposite order of the way we read it */
866 for (i
= 0; i
< nt
->mw_count
; i
++) {
867 size
= nt
->mw_vec
[i
].phys_size
;
869 if (max_mw_size
&& size
> max_mw_size
)
872 spad
= MW0_SZ_HIGH
+ (i
* 2);
873 ntb_peer_spad_write(ndev
, spad
, upper_32_bits(size
));
875 spad
= MW0_SZ_LOW
+ (i
* 2);
876 ntb_peer_spad_write(ndev
, spad
, lower_32_bits(size
));
879 ntb_peer_spad_write(ndev
, NUM_MWS
, nt
->mw_count
);
881 ntb_peer_spad_write(ndev
, NUM_QPS
, nt
->qp_count
);
883 ntb_peer_spad_write(ndev
, VERSION
, NTB_TRANSPORT_VERSION
);
885 /* Query the remote side for its info */
886 val
= ntb_spad_read(ndev
, VERSION
);
887 dev_dbg(&pdev
->dev
, "Remote version = %d\n", val
);
888 if (val
!= NTB_TRANSPORT_VERSION
)
891 val
= ntb_spad_read(ndev
, NUM_QPS
);
892 dev_dbg(&pdev
->dev
, "Remote max number of qps = %d\n", val
);
893 if (val
!= nt
->qp_count
)
896 val
= ntb_spad_read(ndev
, NUM_MWS
);
897 dev_dbg(&pdev
->dev
, "Remote number of mws = %d\n", val
);
898 if (val
!= nt
->mw_count
)
901 for (i
= 0; i
< nt
->mw_count
; i
++) {
904 val
= ntb_spad_read(ndev
, MW0_SZ_HIGH
+ (i
* 2));
905 val64
= (u64
)val
<< 32;
907 val
= ntb_spad_read(ndev
, MW0_SZ_LOW
+ (i
* 2));
910 dev_dbg(&pdev
->dev
, "Remote MW%d size = %#llx\n", i
, val64
);
912 rc
= ntb_set_mw(nt
, i
, val64
);
917 nt
->link_is_up
= true;
919 for (i
= 0; i
< nt
->qp_count
; i
++) {
920 struct ntb_transport_qp
*qp
= &nt
->qp_vec
[i
];
922 ntb_transport_setup_qp_mw(nt
, i
);
924 if (qp
->client_ready
)
925 schedule_delayed_work(&qp
->link_work
, 0);
931 for (i
= 0; i
< nt
->mw_count
; i
++)
934 /* if there's an actual failure, we should just bail */
936 ntb_link_disable(ndev
);
941 if (ntb_link_is_up(ndev
, NULL
, NULL
) == 1)
942 schedule_delayed_work(&nt
->link_work
,
943 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
946 static void ntb_qp_link_work(struct work_struct
*work
)
948 struct ntb_transport_qp
*qp
= container_of(work
,
949 struct ntb_transport_qp
,
951 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
952 struct ntb_transport_ctx
*nt
= qp
->transport
;
955 WARN_ON(!nt
->link_is_up
);
957 val
= ntb_spad_read(nt
->ndev
, QP_LINKS
);
959 ntb_peer_spad_write(nt
->ndev
, QP_LINKS
, val
| BIT(qp
->qp_num
));
961 /* query remote spad for qp ready bits */
962 dev_dbg_ratelimited(&pdev
->dev
, "Remote QP link status = %x\n", val
);
964 /* See if the remote side is up */
965 if (val
& BIT(qp
->qp_num
)) {
966 dev_info(&pdev
->dev
, "qp %d: Link Up\n", qp
->qp_num
);
967 qp
->link_is_up
= true;
970 if (qp
->event_handler
)
971 qp
->event_handler(qp
->cb_data
, qp
->link_is_up
);
974 tasklet_schedule(&qp
->rxc_db_work
);
975 } else if (nt
->link_is_up
)
976 schedule_delayed_work(&qp
->link_work
,
977 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
980 static int ntb_transport_init_queue(struct ntb_transport_ctx
*nt
,
983 struct ntb_transport_qp
*qp
;
985 resource_size_t mw_size
;
986 unsigned int num_qps_mw
, tx_size
;
987 unsigned int mw_num
, mw_count
, qp_count
;
990 mw_count
= nt
->mw_count
;
991 qp_count
= nt
->qp_count
;
993 mw_num
= QP_TO_MW(nt
, qp_num
);
995 qp
= &nt
->qp_vec
[qp_num
];
999 qp
->client_ready
= false;
1000 qp
->event_handler
= NULL
;
1001 ntb_qp_link_down_reset(qp
);
1003 if (qp_count
% mw_count
&& mw_num
+ 1 < qp_count
/ mw_count
)
1004 num_qps_mw
= qp_count
/ mw_count
+ 1;
1006 num_qps_mw
= qp_count
/ mw_count
;
1008 mw_base
= nt
->mw_vec
[mw_num
].phys_addr
;
1009 mw_size
= nt
->mw_vec
[mw_num
].phys_size
;
1011 tx_size
= (unsigned int)mw_size
/ num_qps_mw
;
1012 qp_offset
= tx_size
* (qp_num
/ mw_count
);
1014 qp
->tx_mw
= nt
->mw_vec
[mw_num
].vbase
+ qp_offset
;
1018 qp
->tx_mw_phys
= mw_base
+ qp_offset
;
1019 if (!qp
->tx_mw_phys
)
1022 tx_size
-= sizeof(struct ntb_rx_info
);
1023 qp
->rx_info
= qp
->tx_mw
+ tx_size
;
1025 /* Due to housekeeping, there must be atleast 2 buffs */
1026 qp
->tx_max_frame
= min(transport_mtu
, tx_size
/ 2);
1027 qp
->tx_max_entry
= tx_size
/ qp
->tx_max_frame
;
1029 if (nt
->debugfs_node_dir
) {
1030 char debugfs_name
[4];
1032 snprintf(debugfs_name
, 4, "qp%d", qp_num
);
1033 qp
->debugfs_dir
= debugfs_create_dir(debugfs_name
,
1034 nt
->debugfs_node_dir
);
1036 qp
->debugfs_stats
= debugfs_create_file("stats", S_IRUSR
,
1037 qp
->debugfs_dir
, qp
,
1038 &ntb_qp_debugfs_stats
);
1040 qp
->debugfs_dir
= NULL
;
1041 qp
->debugfs_stats
= NULL
;
1044 INIT_DELAYED_WORK(&qp
->link_work
, ntb_qp_link_work
);
1045 INIT_WORK(&qp
->link_cleanup
, ntb_qp_link_cleanup_work
);
1047 spin_lock_init(&qp
->ntb_rx_q_lock
);
1048 spin_lock_init(&qp
->ntb_tx_free_q_lock
);
1050 INIT_LIST_HEAD(&qp
->rx_post_q
);
1051 INIT_LIST_HEAD(&qp
->rx_pend_q
);
1052 INIT_LIST_HEAD(&qp
->rx_free_q
);
1053 INIT_LIST_HEAD(&qp
->tx_free_q
);
1055 tasklet_init(&qp
->rxc_db_work
, ntb_transport_rxc_db
,
1061 static int ntb_transport_probe(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1063 struct ntb_transport_ctx
*nt
;
1064 struct ntb_transport_mw
*mw
;
1065 unsigned int mw_count
, qp_count
, spad_count
, max_mw_count_for_spads
;
1070 mw_count
= ntb_mw_count(ndev
);
1072 if (ntb_db_is_unsafe(ndev
))
1074 "doorbell is unsafe, proceed anyway...\n");
1075 if (ntb_spad_is_unsafe(ndev
))
1077 "scratchpad is unsafe, proceed anyway...\n");
1079 node
= dev_to_node(&ndev
->dev
);
1081 nt
= kzalloc_node(sizeof(*nt
), GFP_KERNEL
, node
);
1086 spad_count
= ntb_spad_count(ndev
);
1088 /* Limit the MW's based on the availability of scratchpads */
1090 if (spad_count
< NTB_TRANSPORT_MIN_SPADS
) {
1096 max_mw_count_for_spads
= (spad_count
- MW0_SZ_HIGH
) / 2;
1097 nt
->mw_count
= min(mw_count
, max_mw_count_for_spads
);
1099 nt
->mw_vec
= kzalloc_node(mw_count
* sizeof(*nt
->mw_vec
),
1106 for (i
= 0; i
< mw_count
; i
++) {
1107 mw
= &nt
->mw_vec
[i
];
1109 rc
= ntb_mw_get_range(ndev
, i
, &mw
->phys_addr
, &mw
->phys_size
,
1110 &mw
->xlat_align
, &mw
->xlat_align_size
);
1114 mw
->vbase
= ioremap_wc(mw
->phys_addr
, mw
->phys_size
);
1122 mw
->virt_addr
= NULL
;
1126 qp_bitmap
= ntb_db_valid_mask(ndev
);
1128 qp_count
= ilog2(qp_bitmap
);
1129 if (max_num_clients
&& max_num_clients
< qp_count
)
1130 qp_count
= max_num_clients
;
1131 else if (mw_count
< qp_count
)
1132 qp_count
= mw_count
;
1134 qp_bitmap
&= BIT_ULL(qp_count
) - 1;
1136 nt
->qp_count
= qp_count
;
1137 nt
->qp_bitmap
= qp_bitmap
;
1138 nt
->qp_bitmap_free
= qp_bitmap
;
1140 nt
->qp_vec
= kzalloc_node(qp_count
* sizeof(*nt
->qp_vec
),
1147 if (nt_debugfs_dir
) {
1148 nt
->debugfs_node_dir
=
1149 debugfs_create_dir(pci_name(ndev
->pdev
),
1153 for (i
= 0; i
< qp_count
; i
++) {
1154 rc
= ntb_transport_init_queue(nt
, i
);
1159 INIT_DELAYED_WORK(&nt
->link_work
, ntb_transport_link_work
);
1160 INIT_WORK(&nt
->link_cleanup
, ntb_transport_link_cleanup_work
);
1162 rc
= ntb_set_ctx(ndev
, nt
, &ntb_transport_ops
);
1166 INIT_LIST_HEAD(&nt
->client_devs
);
1167 rc
= ntb_bus_init(nt
);
1171 nt
->link_is_up
= false;
1172 ntb_link_enable(ndev
, NTB_SPEED_AUTO
, NTB_WIDTH_AUTO
);
1173 ntb_link_event(ndev
);
1178 ntb_clear_ctx(ndev
);
1183 mw
= &nt
->mw_vec
[i
];
1192 static void ntb_transport_free(struct ntb_client
*self
, struct ntb_dev
*ndev
)
1194 struct ntb_transport_ctx
*nt
= ndev
->ctx
;
1195 struct ntb_transport_qp
*qp
;
1196 u64 qp_bitmap_alloc
;
1199 ntb_transport_link_cleanup(nt
);
1200 cancel_work_sync(&nt
->link_cleanup
);
1201 cancel_delayed_work_sync(&nt
->link_work
);
1203 qp_bitmap_alloc
= nt
->qp_bitmap
& ~nt
->qp_bitmap_free
;
1205 /* verify that all the qp's are freed */
1206 for (i
= 0; i
< nt
->qp_count
; i
++) {
1207 qp
= &nt
->qp_vec
[i
];
1208 if (qp_bitmap_alloc
& BIT_ULL(i
))
1209 ntb_transport_free_queue(qp
);
1210 debugfs_remove_recursive(qp
->debugfs_dir
);
1213 ntb_link_disable(ndev
);
1214 ntb_clear_ctx(ndev
);
1218 for (i
= nt
->mw_count
; i
--; ) {
1220 iounmap(nt
->mw_vec
[i
].vbase
);
1228 static void ntb_complete_rxc(struct ntb_transport_qp
*qp
)
1230 struct ntb_queue_entry
*entry
;
1233 unsigned long irqflags
;
1235 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1237 while (!list_empty(&qp
->rx_post_q
)) {
1238 entry
= list_first_entry(&qp
->rx_post_q
,
1239 struct ntb_queue_entry
, entry
);
1240 if (!(entry
->flags
& DESC_DONE_FLAG
))
1243 entry
->rx_hdr
->flags
= 0;
1244 iowrite32(entry
->rx_index
, &qp
->rx_info
->entry
);
1246 cb_data
= entry
->cb_data
;
1249 list_move_tail(&entry
->entry
, &qp
->rx_free_q
);
1251 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1253 if (qp
->rx_handler
&& qp
->client_ready
)
1254 qp
->rx_handler(qp
, qp
->cb_data
, cb_data
, len
);
1256 spin_lock_irqsave(&qp
->ntb_rx_q_lock
, irqflags
);
1259 spin_unlock_irqrestore(&qp
->ntb_rx_q_lock
, irqflags
);
1262 static void ntb_rx_copy_callback(void *data
,
1263 const struct dmaengine_result
*res
)
1265 struct ntb_queue_entry
*entry
= data
;
1267 /* we need to check DMA results if we are using DMA */
1269 enum dmaengine_tx_result dma_err
= res
->result
;
1272 case DMA_TRANS_READ_FAILED
:
1273 case DMA_TRANS_WRITE_FAILED
:
1275 case DMA_TRANS_ABORTED
:
1277 struct ntb_transport_qp
*qp
= entry
->qp
;
1278 void *offset
= qp
->rx_buff
+ qp
->rx_max_frame
*
1281 ntb_memcpy_rx(entry
, offset
);
1286 case DMA_TRANS_NOERROR
:
1292 entry
->flags
|= DESC_DONE_FLAG
;
1294 ntb_complete_rxc(entry
->qp
);
1297 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
)
1299 void *buf
= entry
->buf
;
1300 size_t len
= entry
->len
;
1302 memcpy(buf
, offset
, len
);
1304 /* Ensure that the data is fully copied out before clearing the flag */
1307 ntb_rx_copy_callback(entry
, NULL
);
1310 static int ntb_async_rx_submit(struct ntb_queue_entry
*entry
, void *offset
)
1312 struct dma_async_tx_descriptor
*txd
;
1313 struct ntb_transport_qp
*qp
= entry
->qp
;
1314 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1315 struct dma_device
*device
;
1316 size_t pay_off
, buff_off
, len
;
1317 struct dmaengine_unmap_data
*unmap
;
1318 dma_cookie_t cookie
;
1319 void *buf
= entry
->buf
;
1323 device
= chan
->device
;
1324 pay_off
= (size_t)offset
& ~PAGE_MASK
;
1325 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1327 if (!is_dma_copy_aligned(device
, pay_off
, buff_off
, len
))
1330 unmap
= dmaengine_get_unmap_data(device
->dev
, 2, GFP_NOWAIT
);
1335 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(offset
),
1336 pay_off
, len
, DMA_TO_DEVICE
);
1337 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1342 unmap
->addr
[1] = dma_map_page(device
->dev
, virt_to_page(buf
),
1343 buff_off
, len
, DMA_FROM_DEVICE
);
1344 if (dma_mapping_error(device
->dev
, unmap
->addr
[1]))
1347 unmap
->from_cnt
= 1;
1349 for (retries
= 0; retries
< DMA_RETRIES
; retries
++) {
1350 txd
= device
->device_prep_dma_memcpy(chan
,
1352 unmap
->addr
[0], len
,
1353 DMA_PREP_INTERRUPT
);
1357 set_current_state(TASK_INTERRUPTIBLE
);
1358 schedule_timeout(DMA_OUT_RESOURCE_TO
);
1362 qp
->dma_rx_prep_err
++;
1366 txd
->callback_result
= ntb_rx_copy_callback
;
1367 txd
->callback_param
= entry
;
1368 dma_set_unmap(txd
, unmap
);
1370 cookie
= dmaengine_submit(txd
);
1371 if (dma_submit_error(cookie
))
1374 dmaengine_unmap_put(unmap
);
1376 qp
->last_cookie
= cookie
;
1383 dmaengine_unmap_put(unmap
);
1385 dmaengine_unmap_put(unmap
);
1390 static void ntb_async_rx(struct ntb_queue_entry
*entry
, void *offset
)
1392 struct ntb_transport_qp
*qp
= entry
->qp
;
1393 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1399 if (entry
->len
< copy_bytes
)
1402 res
= ntb_async_rx_submit(entry
, offset
);
1406 if (!entry
->retries
)
1412 ntb_memcpy_rx(entry
, offset
);
1416 static int ntb_process_rxc(struct ntb_transport_qp
*qp
)
1418 struct ntb_payload_header
*hdr
;
1419 struct ntb_queue_entry
*entry
;
1422 offset
= qp
->rx_buff
+ qp
->rx_max_frame
* qp
->rx_index
;
1423 hdr
= offset
+ qp
->rx_max_frame
- sizeof(struct ntb_payload_header
);
1425 dev_dbg(&qp
->ndev
->pdev
->dev
, "qp %d: RX ver %u len %d flags %x\n",
1426 qp
->qp_num
, hdr
->ver
, hdr
->len
, hdr
->flags
);
1428 if (!(hdr
->flags
& DESC_DONE_FLAG
)) {
1429 dev_dbg(&qp
->ndev
->pdev
->dev
, "done flag not set\n");
1430 qp
->rx_ring_empty
++;
1434 if (hdr
->flags
& LINK_DOWN_FLAG
) {
1435 dev_dbg(&qp
->ndev
->pdev
->dev
, "link down flag set\n");
1436 ntb_qp_link_down(qp
);
1441 if (hdr
->ver
!= (u32
)qp
->rx_pkts
) {
1442 dev_dbg(&qp
->ndev
->pdev
->dev
,
1443 "version mismatch, expected %llu - got %u\n",
1444 qp
->rx_pkts
, hdr
->ver
);
1449 entry
= ntb_list_mv(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
, &qp
->rx_post_q
);
1451 dev_dbg(&qp
->ndev
->pdev
->dev
, "no receive buffer\n");
1452 qp
->rx_err_no_buf
++;
1456 entry
->rx_hdr
= hdr
;
1457 entry
->rx_index
= qp
->rx_index
;
1459 if (hdr
->len
> entry
->len
) {
1460 dev_dbg(&qp
->ndev
->pdev
->dev
,
1461 "receive buffer overflow! Wanted %d got %d\n",
1462 hdr
->len
, entry
->len
);
1466 entry
->flags
|= DESC_DONE_FLAG
;
1468 ntb_complete_rxc(qp
);
1470 dev_dbg(&qp
->ndev
->pdev
->dev
,
1471 "RX OK index %u ver %u size %d into buf size %d\n",
1472 qp
->rx_index
, hdr
->ver
, hdr
->len
, entry
->len
);
1474 qp
->rx_bytes
+= hdr
->len
;
1477 entry
->len
= hdr
->len
;
1479 ntb_async_rx(entry
, offset
);
1483 qp
->rx_index
%= qp
->rx_max_entry
;
1488 static void ntb_transport_rxc_db(unsigned long data
)
1490 struct ntb_transport_qp
*qp
= (void *)data
;
1493 dev_dbg(&qp
->ndev
->pdev
->dev
, "%s: doorbell %d received\n",
1494 __func__
, qp
->qp_num
);
1496 /* Limit the number of packets processed in a single interrupt to
1497 * provide fairness to others
1499 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
1500 rc
= ntb_process_rxc(qp
);
1505 if (i
&& qp
->rx_dma_chan
)
1506 dma_async_issue_pending(qp
->rx_dma_chan
);
1508 if (i
== qp
->rx_max_entry
) {
1509 /* there is more work to do */
1511 tasklet_schedule(&qp
->rxc_db_work
);
1512 } else if (ntb_db_read(qp
->ndev
) & BIT_ULL(qp
->qp_num
)) {
1513 /* the doorbell bit is set: clear it */
1514 ntb_db_clear(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1515 /* ntb_db_read ensures ntb_db_clear write is committed */
1516 ntb_db_read(qp
->ndev
);
1518 /* an interrupt may have arrived between finishing
1519 * ntb_process_rxc and clearing the doorbell bit:
1520 * there might be some more work to do.
1523 tasklet_schedule(&qp
->rxc_db_work
);
1527 static void ntb_tx_copy_callback(void *data
,
1528 const struct dmaengine_result
*res
)
1530 struct ntb_queue_entry
*entry
= data
;
1531 struct ntb_transport_qp
*qp
= entry
->qp
;
1532 struct ntb_payload_header __iomem
*hdr
= entry
->tx_hdr
;
1534 /* we need to check DMA results if we are using DMA */
1536 enum dmaengine_tx_result dma_err
= res
->result
;
1539 case DMA_TRANS_READ_FAILED
:
1540 case DMA_TRANS_WRITE_FAILED
:
1542 case DMA_TRANS_ABORTED
:
1544 void __iomem
*offset
=
1545 qp
->tx_mw
+ qp
->tx_max_frame
*
1548 /* resubmit via CPU */
1549 ntb_memcpy_tx(entry
, offset
);
1554 case DMA_TRANS_NOERROR
:
1560 iowrite32(entry
->flags
| DESC_DONE_FLAG
, &hdr
->flags
);
1562 ntb_peer_db_set(qp
->ndev
, BIT_ULL(qp
->qp_num
));
1564 /* The entry length can only be zero if the packet is intended to be a
1565 * "link down" or similar. Since no payload is being sent in these
1566 * cases, there is nothing to add to the completion queue.
1568 if (entry
->len
> 0) {
1569 qp
->tx_bytes
+= entry
->len
;
1572 qp
->tx_handler(qp
, qp
->cb_data
, entry
->cb_data
,
1576 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
, &qp
->tx_free_q
);
1579 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
)
1581 #ifdef ARCH_HAS_NOCACHE_UACCESS
1583 * Using non-temporal mov to improve performance on non-cached
1584 * writes, even though we aren't actually copying from user space.
1586 __copy_from_user_inatomic_nocache(offset
, entry
->buf
, entry
->len
);
1588 memcpy_toio(offset
, entry
->buf
, entry
->len
);
1591 /* Ensure that the data is fully copied out before setting the flags */
1594 ntb_tx_copy_callback(entry
, NULL
);
1597 static int ntb_async_tx_submit(struct ntb_transport_qp
*qp
,
1598 struct ntb_queue_entry
*entry
)
1600 struct dma_async_tx_descriptor
*txd
;
1601 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1602 struct dma_device
*device
;
1603 size_t len
= entry
->len
;
1604 void *buf
= entry
->buf
;
1605 size_t dest_off
, buff_off
;
1606 struct dmaengine_unmap_data
*unmap
;
1608 dma_cookie_t cookie
;
1611 device
= chan
->device
;
1612 dest
= qp
->tx_mw_phys
+ qp
->tx_max_frame
* entry
->tx_index
;
1613 buff_off
= (size_t)buf
& ~PAGE_MASK
;
1614 dest_off
= (size_t)dest
& ~PAGE_MASK
;
1616 if (!is_dma_copy_aligned(device
, buff_off
, dest_off
, len
))
1619 unmap
= dmaengine_get_unmap_data(device
->dev
, 1, GFP_NOWAIT
);
1624 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(buf
),
1625 buff_off
, len
, DMA_TO_DEVICE
);
1626 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1631 for (retries
= 0; retries
< DMA_RETRIES
; retries
++) {
1632 txd
= device
->device_prep_dma_memcpy(chan
, dest
,
1633 unmap
->addr
[0], len
,
1634 DMA_PREP_INTERRUPT
);
1638 set_current_state(TASK_INTERRUPTIBLE
);
1639 schedule_timeout(DMA_OUT_RESOURCE_TO
);
1643 qp
->dma_tx_prep_err
++;
1647 txd
->callback_result
= ntb_tx_copy_callback
;
1648 txd
->callback_param
= entry
;
1649 dma_set_unmap(txd
, unmap
);
1651 cookie
= dmaengine_submit(txd
);
1652 if (dma_submit_error(cookie
))
1655 dmaengine_unmap_put(unmap
);
1657 dma_async_issue_pending(chan
);
1661 dmaengine_unmap_put(unmap
);
1663 dmaengine_unmap_put(unmap
);
1668 static void ntb_async_tx(struct ntb_transport_qp
*qp
,
1669 struct ntb_queue_entry
*entry
)
1671 struct ntb_payload_header __iomem
*hdr
;
1672 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1673 void __iomem
*offset
;
1676 entry
->tx_index
= qp
->tx_index
;
1677 offset
= qp
->tx_mw
+ qp
->tx_max_frame
* entry
->tx_index
;
1678 hdr
= offset
+ qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1679 entry
->tx_hdr
= hdr
;
1681 iowrite32(entry
->len
, &hdr
->len
);
1682 iowrite32((u32
)qp
->tx_pkts
, &hdr
->ver
);
1687 if (entry
->len
< copy_bytes
)
1690 res
= ntb_async_tx_submit(qp
, entry
);
1694 if (!entry
->retries
)
1700 ntb_memcpy_tx(entry
, offset
);
1704 static int ntb_process_tx(struct ntb_transport_qp
*qp
,
1705 struct ntb_queue_entry
*entry
)
1707 if (qp
->tx_index
== qp
->remote_rx_info
->entry
) {
1712 if (entry
->len
> qp
->tx_max_frame
- sizeof(struct ntb_payload_header
)) {
1714 qp
->tx_handler(qp
, qp
->cb_data
, NULL
, -EIO
);
1716 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1721 ntb_async_tx(qp
, entry
);
1724 qp
->tx_index
%= qp
->tx_max_entry
;
1731 static void ntb_send_link_down(struct ntb_transport_qp
*qp
)
1733 struct pci_dev
*pdev
= qp
->ndev
->pdev
;
1734 struct ntb_queue_entry
*entry
;
1737 if (!qp
->link_is_up
)
1740 dev_info(&pdev
->dev
, "qp %d: Send Link Down\n", qp
->qp_num
);
1742 for (i
= 0; i
< NTB_LINK_DOWN_TIMEOUT
; i
++) {
1743 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1752 entry
->cb_data
= NULL
;
1755 entry
->flags
= LINK_DOWN_FLAG
;
1757 rc
= ntb_process_tx(qp
, entry
);
1759 dev_err(&pdev
->dev
, "ntb: QP%d unable to send linkdown msg\n",
1762 ntb_qp_link_down_reset(qp
);
1765 static bool ntb_dma_filter_fn(struct dma_chan
*chan
, void *node
)
1767 return dev_to_node(&chan
->dev
->device
) == (int)(unsigned long)node
;
1771 * ntb_transport_create_queue - Create a new NTB transport layer queue
1772 * @rx_handler: receive callback function
1773 * @tx_handler: transmit callback function
1774 * @event_handler: event callback function
1776 * Create a new NTB transport layer queue and provide the queue with a callback
1777 * routine for both transmit and receive. The receive callback routine will be
1778 * used to pass up data when the transport has received it on the queue. The
1779 * transmit callback routine will be called when the transport has completed the
1780 * transmission of the data on the queue and the data is ready to be freed.
1782 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1784 struct ntb_transport_qp
*
1785 ntb_transport_create_queue(void *data
, struct device
*client_dev
,
1786 const struct ntb_queue_handlers
*handlers
)
1788 struct ntb_dev
*ndev
;
1789 struct pci_dev
*pdev
;
1790 struct ntb_transport_ctx
*nt
;
1791 struct ntb_queue_entry
*entry
;
1792 struct ntb_transport_qp
*qp
;
1794 unsigned int free_queue
;
1795 dma_cap_mask_t dma_mask
;
1799 ndev
= dev_ntb(client_dev
->parent
);
1803 node
= dev_to_node(&ndev
->dev
);
1805 free_queue
= ffs(nt
->qp_bitmap
);
1809 /* decrement free_queue to make it zero based */
1812 qp
= &nt
->qp_vec
[free_queue
];
1813 qp_bit
= BIT_ULL(qp
->qp_num
);
1815 nt
->qp_bitmap_free
&= ~qp_bit
;
1818 qp
->rx_handler
= handlers
->rx_handler
;
1819 qp
->tx_handler
= handlers
->tx_handler
;
1820 qp
->event_handler
= handlers
->event_handler
;
1822 dma_cap_zero(dma_mask
);
1823 dma_cap_set(DMA_MEMCPY
, dma_mask
);
1827 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1828 (void *)(unsigned long)node
);
1829 if (!qp
->tx_dma_chan
)
1830 dev_info(&pdev
->dev
, "Unable to allocate TX DMA channel\n");
1833 dma_request_channel(dma_mask
, ntb_dma_filter_fn
,
1834 (void *)(unsigned long)node
);
1835 if (!qp
->rx_dma_chan
)
1836 dev_info(&pdev
->dev
, "Unable to allocate RX DMA channel\n");
1838 qp
->tx_dma_chan
= NULL
;
1839 qp
->rx_dma_chan
= NULL
;
1842 dev_dbg(&pdev
->dev
, "Using %s memcpy for TX\n",
1843 qp
->tx_dma_chan
? "DMA" : "CPU");
1845 dev_dbg(&pdev
->dev
, "Using %s memcpy for RX\n",
1846 qp
->rx_dma_chan
? "DMA" : "CPU");
1848 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1849 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1854 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
,
1857 qp
->rx_alloc_entry
= NTB_QP_DEF_NUM_ENTRIES
;
1859 for (i
= 0; i
< qp
->tx_max_entry
; i
++) {
1860 entry
= kzalloc_node(sizeof(*entry
), GFP_ATOMIC
, node
);
1865 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1869 ntb_db_clear(qp
->ndev
, qp_bit
);
1870 ntb_db_clear_mask(qp
->ndev
, qp_bit
);
1872 dev_info(&pdev
->dev
, "NTB Transport QP %d created\n", qp
->qp_num
);
1877 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1880 qp
->rx_alloc_entry
= 0;
1881 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1883 if (qp
->tx_dma_chan
)
1884 dma_release_channel(qp
->tx_dma_chan
);
1885 if (qp
->rx_dma_chan
)
1886 dma_release_channel(qp
->rx_dma_chan
);
1887 nt
->qp_bitmap_free
|= qp_bit
;
1891 EXPORT_SYMBOL_GPL(ntb_transport_create_queue
);
1894 * ntb_transport_free_queue - Frees NTB transport queue
1895 * @qp: NTB queue to be freed
1897 * Frees NTB transport queue
1899 void ntb_transport_free_queue(struct ntb_transport_qp
*qp
)
1901 struct pci_dev
*pdev
;
1902 struct ntb_queue_entry
*entry
;
1908 pdev
= qp
->ndev
->pdev
;
1912 if (qp
->tx_dma_chan
) {
1913 struct dma_chan
*chan
= qp
->tx_dma_chan
;
1914 /* Putting the dma_chan to NULL will force any new traffic to be
1915 * processed by the CPU instead of the DAM engine
1917 qp
->tx_dma_chan
= NULL
;
1919 /* Try to be nice and wait for any queued DMA engine
1920 * transactions to process before smashing it with a rock
1922 dma_sync_wait(chan
, qp
->last_cookie
);
1923 dmaengine_terminate_all(chan
);
1924 dma_release_channel(chan
);
1927 if (qp
->rx_dma_chan
) {
1928 struct dma_chan
*chan
= qp
->rx_dma_chan
;
1929 /* Putting the dma_chan to NULL will force any new traffic to be
1930 * processed by the CPU instead of the DAM engine
1932 qp
->rx_dma_chan
= NULL
;
1934 /* Try to be nice and wait for any queued DMA engine
1935 * transactions to process before smashing it with a rock
1937 dma_sync_wait(chan
, qp
->last_cookie
);
1938 dmaengine_terminate_all(chan
);
1939 dma_release_channel(chan
);
1942 qp_bit
= BIT_ULL(qp
->qp_num
);
1944 ntb_db_set_mask(qp
->ndev
, qp_bit
);
1945 tasklet_kill(&qp
->rxc_db_work
);
1947 cancel_delayed_work_sync(&qp
->link_work
);
1950 qp
->rx_handler
= NULL
;
1951 qp
->tx_handler
= NULL
;
1952 qp
->event_handler
= NULL
;
1954 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
)))
1957 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
))) {
1958 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_pend_q\n");
1962 while ((entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_post_q
))) {
1963 dev_warn(&pdev
->dev
, "Freeing item from non-empty rx_post_q\n");
1967 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1970 qp
->transport
->qp_bitmap_free
|= qp_bit
;
1972 dev_info(&pdev
->dev
, "NTB Transport QP %d freed\n", qp
->qp_num
);
1974 EXPORT_SYMBOL_GPL(ntb_transport_free_queue
);
1977 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1978 * @qp: NTB queue to be freed
1979 * @len: pointer to variable to write enqueued buffers length
1981 * Dequeues unused buffers from receive queue. Should only be used during
1984 * RETURNS: NULL error value on error, or void* for success.
1986 void *ntb_transport_rx_remove(struct ntb_transport_qp
*qp
, unsigned int *len
)
1988 struct ntb_queue_entry
*entry
;
1991 if (!qp
|| qp
->client_ready
)
1994 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_pend_q
);
1998 buf
= entry
->cb_data
;
2001 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
2005 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove
);
2008 * ntb_transport_rx_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 incoming packets will be copied into
2012 * @len: length of the data buffer
2014 * Enqueue a new receive buffer onto the transport queue into which a NTB
2015 * payload can be received into.
2017 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2019 int ntb_transport_rx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
2022 struct ntb_queue_entry
*entry
;
2027 entry
= ntb_list_rm(&qp
->ntb_rx_q_lock
, &qp
->rx_free_q
);
2031 entry
->cb_data
= cb
;
2037 entry
->rx_index
= 0;
2039 ntb_list_add(&qp
->ntb_rx_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
2042 tasklet_schedule(&qp
->rxc_db_work
);
2046 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue
);
2049 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2050 * @qp: NTB transport layer queue the entry is to be enqueued on
2051 * @cb: per buffer pointer for callback function to use
2052 * @data: pointer to data buffer that will be sent
2053 * @len: length of the data buffer
2055 * Enqueue a new transmit buffer onto the transport queue from which a NTB
2056 * payload will be transmitted. This assumes that a lock is being held to
2057 * serialize access to the qp.
2059 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2061 int ntb_transport_tx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
2064 struct ntb_queue_entry
*entry
;
2067 if (!qp
|| !qp
->link_is_up
|| !len
)
2070 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
2072 qp
->tx_err_no_buf
++;
2076 entry
->cb_data
= cb
;
2082 entry
->tx_index
= 0;
2084 rc
= ntb_process_tx(qp
, entry
);
2086 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
2091 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue
);
2094 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2095 * @qp: NTB transport layer queue to be enabled
2097 * Notify NTB transport layer of client readiness to use queue
2099 void ntb_transport_link_up(struct ntb_transport_qp
*qp
)
2104 qp
->client_ready
= true;
2106 if (qp
->transport
->link_is_up
)
2107 schedule_delayed_work(&qp
->link_work
, 0);
2109 EXPORT_SYMBOL_GPL(ntb_transport_link_up
);
2112 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2113 * @qp: NTB transport layer queue to be disabled
2115 * Notify NTB transport layer of client's desire to no longer receive data on
2116 * transport queue specified. It is the client's responsibility to ensure all
2117 * entries on queue are purged or otherwise handled appropriately.
2119 void ntb_transport_link_down(struct ntb_transport_qp
*qp
)
2126 qp
->client_ready
= false;
2128 val
= ntb_spad_read(qp
->ndev
, QP_LINKS
);
2130 ntb_peer_spad_write(qp
->ndev
, QP_LINKS
,
2131 val
& ~BIT(qp
->qp_num
));
2134 ntb_send_link_down(qp
);
2136 cancel_delayed_work_sync(&qp
->link_work
);
2138 EXPORT_SYMBOL_GPL(ntb_transport_link_down
);
2141 * ntb_transport_link_query - Query transport link state
2142 * @qp: NTB transport layer queue to be queried
2144 * Query connectivity to the remote system of the NTB transport queue
2146 * RETURNS: true for link up or false for link down
2148 bool ntb_transport_link_query(struct ntb_transport_qp
*qp
)
2153 return qp
->link_is_up
;
2155 EXPORT_SYMBOL_GPL(ntb_transport_link_query
);
2158 * ntb_transport_qp_num - Query the qp number
2159 * @qp: NTB transport layer queue to be queried
2161 * Query qp number of the NTB transport queue
2163 * RETURNS: a zero based number specifying the qp number
2165 unsigned char ntb_transport_qp_num(struct ntb_transport_qp
*qp
)
2172 EXPORT_SYMBOL_GPL(ntb_transport_qp_num
);
2175 * ntb_transport_max_size - Query the max payload size of a qp
2176 * @qp: NTB transport layer queue to be queried
2178 * Query the maximum payload size permissible on the given qp
2180 * RETURNS: the max payload size of a qp
2182 unsigned int ntb_transport_max_size(struct ntb_transport_qp
*qp
)
2184 unsigned int max_size
;
2185 unsigned int copy_align
;
2186 struct dma_chan
*rx_chan
, *tx_chan
;
2191 rx_chan
= qp
->rx_dma_chan
;
2192 tx_chan
= qp
->tx_dma_chan
;
2194 copy_align
= max(rx_chan
? rx_chan
->device
->copy_align
: 0,
2195 tx_chan
? tx_chan
->device
->copy_align
: 0);
2197 /* If DMA engine usage is possible, try to find the max size for that */
2198 max_size
= qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
2199 max_size
= round_down(max_size
, 1 << copy_align
);
2203 EXPORT_SYMBOL_GPL(ntb_transport_max_size
);
2205 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp
*qp
)
2207 unsigned int head
= qp
->tx_index
;
2208 unsigned int tail
= qp
->remote_rx_info
->entry
;
2210 return tail
> head
? tail
- head
: qp
->tx_max_entry
+ tail
- head
;
2212 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry
);
2214 static void ntb_transport_doorbell_callback(void *data
, int vector
)
2216 struct ntb_transport_ctx
*nt
= data
;
2217 struct ntb_transport_qp
*qp
;
2219 unsigned int qp_num
;
2221 db_bits
= (nt
->qp_bitmap
& ~nt
->qp_bitmap_free
&
2222 ntb_db_vector_mask(nt
->ndev
, vector
));
2225 qp_num
= __ffs(db_bits
);
2226 qp
= &nt
->qp_vec
[qp_num
];
2229 tasklet_schedule(&qp
->rxc_db_work
);
2231 db_bits
&= ~BIT_ULL(qp_num
);
2235 static const struct ntb_ctx_ops ntb_transport_ops
= {
2236 .link_event
= ntb_transport_event_callback
,
2237 .db_event
= ntb_transport_doorbell_callback
,
2240 static struct ntb_client ntb_transport_client
= {
2242 .probe
= ntb_transport_probe
,
2243 .remove
= ntb_transport_free
,
2247 static int __init
ntb_transport_init(void)
2251 pr_info("%s, version %s\n", NTB_TRANSPORT_DESC
, NTB_TRANSPORT_VER
);
2253 if (debugfs_initialized())
2254 nt_debugfs_dir
= debugfs_create_dir(KBUILD_MODNAME
, NULL
);
2256 rc
= bus_register(&ntb_transport_bus
);
2260 rc
= ntb_register_client(&ntb_transport_client
);
2267 bus_unregister(&ntb_transport_bus
);
2269 debugfs_remove_recursive(nt_debugfs_dir
);
2272 module_init(ntb_transport_init
);
2274 static void __exit
ntb_transport_exit(void)
2276 debugfs_remove_recursive(nt_debugfs_dir
);
2278 ntb_unregister_client(&ntb_transport_client
);
2279 bus_unregister(&ntb_transport_bus
);
2281 module_exit(ntb_transport_exit
);