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.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
15 * Copyright(c) 2012 Intel Corporation. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
21 * * Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * * Redistributions in binary form must reproduce the above copy
24 * notice, this list of conditions and the following disclaimer in
25 * the documentation and/or other materials provided with the
27 * * Neither the name of Intel Corporation nor the names of its
28 * contributors may be used to endorse or promote products derived
29 * from this software without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * Intel PCIe NTB Linux driver
45 * Contact Information:
46 * Jon Mason <jon.mason@intel.com>
48 #include <linux/debugfs.h>
49 #include <linux/delay.h>
50 #include <linux/dmaengine.h>
51 #include <linux/dma-mapping.h>
52 #include <linux/errno.h>
53 #include <linux/export.h>
54 #include <linux/interrupt.h>
55 #include <linux/module.h>
56 #include <linux/pci.h>
57 #include <linux/slab.h>
58 #include <linux/types.h>
61 #define NTB_TRANSPORT_VERSION 3
63 static unsigned int transport_mtu
= 0x401E;
64 module_param(transport_mtu
, uint
, 0644);
65 MODULE_PARM_DESC(transport_mtu
, "Maximum size of NTB transport packets");
67 static unsigned char max_num_clients
;
68 module_param(max_num_clients
, byte
, 0644);
69 MODULE_PARM_DESC(max_num_clients
, "Maximum number of NTB transport clients");
71 static unsigned int copy_bytes
= 1024;
72 module_param(copy_bytes
, uint
, 0644);
73 MODULE_PARM_DESC(copy_bytes
, "Threshold under which NTB will use the CPU to copy instead of DMA");
75 struct ntb_queue_entry
{
76 /* ntb_queue list reference */
77 struct list_head entry
;
78 /* pointers to data to be transfered */
84 struct ntb_transport_qp
*qp
;
86 struct ntb_payload_header __iomem
*tx_hdr
;
87 struct ntb_payload_header
*rx_hdr
;
96 struct ntb_transport_qp
{
97 struct ntb_transport
*transport
;
98 struct ntb_device
*ndev
;
100 struct dma_chan
*dma_chan
;
104 u8 qp_num
; /* Only 64 QP's are allowed. 0-63 */
106 struct ntb_rx_info __iomem
*rx_info
;
107 struct ntb_rx_info
*remote_rx_info
;
109 void (*tx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
110 void *data
, int len
);
111 struct list_head tx_free_q
;
112 spinlock_t ntb_tx_free_q_lock
;
114 dma_addr_t tx_mw_phys
;
115 unsigned int tx_index
;
116 unsigned int tx_max_entry
;
117 unsigned int tx_max_frame
;
119 void (*rx_handler
)(struct ntb_transport_qp
*qp
, void *qp_data
,
120 void *data
, int len
);
121 struct list_head rx_pend_q
;
122 struct list_head rx_free_q
;
123 spinlock_t ntb_rx_pend_q_lock
;
124 spinlock_t ntb_rx_free_q_lock
;
126 unsigned int rx_index
;
127 unsigned int rx_max_entry
;
128 unsigned int rx_max_frame
;
129 dma_cookie_t last_cookie
;
131 void (*event_handler
)(void *data
, int status
);
132 struct delayed_work link_work
;
133 struct work_struct link_cleanup
;
135 struct dentry
*debugfs_dir
;
136 struct dentry
*debugfs_stats
;
155 struct ntb_transport_mw
{
161 struct ntb_transport_client_dev
{
162 struct list_head entry
;
166 struct ntb_transport
{
167 struct list_head entry
;
168 struct list_head client_devs
;
170 struct ntb_device
*ndev
;
171 struct ntb_transport_mw
*mw
;
172 struct ntb_transport_qp
*qps
;
173 unsigned int max_qps
;
174 unsigned long qp_bitmap
;
176 struct delayed_work link_work
;
177 struct work_struct link_cleanup
;
181 DESC_DONE_FLAG
= 1 << 0,
182 LINK_DOWN_FLAG
= 1 << 1,
185 struct ntb_payload_header
{
203 #define QP_TO_MW(ndev, qp) ((qp) % ntb_max_mw(ndev))
204 #define NTB_QP_DEF_NUM_ENTRIES 100
205 #define NTB_LINK_DOWN_TIMEOUT 10
207 static int ntb_match_bus(struct device
*dev
, struct device_driver
*drv
)
209 return !strncmp(dev_name(dev
), drv
->name
, strlen(drv
->name
));
212 static int ntb_client_probe(struct device
*dev
)
214 const struct ntb_client
*drv
= container_of(dev
->driver
,
215 struct ntb_client
, driver
);
216 struct pci_dev
*pdev
= container_of(dev
->parent
, struct pci_dev
, dev
);
220 if (drv
&& drv
->probe
)
221 rc
= drv
->probe(pdev
);
228 static int ntb_client_remove(struct device
*dev
)
230 const struct ntb_client
*drv
= container_of(dev
->driver
,
231 struct ntb_client
, driver
);
232 struct pci_dev
*pdev
= container_of(dev
->parent
, struct pci_dev
, dev
);
234 if (drv
&& drv
->remove
)
242 static struct bus_type ntb_bus_type
= {
244 .match
= ntb_match_bus
,
245 .probe
= ntb_client_probe
,
246 .remove
= ntb_client_remove
,
249 static LIST_HEAD(ntb_transport_list
);
251 static int ntb_bus_init(struct ntb_transport
*nt
)
253 if (list_empty(&ntb_transport_list
)) {
254 int rc
= bus_register(&ntb_bus_type
);
259 list_add(&nt
->entry
, &ntb_transport_list
);
264 static void ntb_bus_remove(struct ntb_transport
*nt
)
266 struct ntb_transport_client_dev
*client_dev
, *cd
;
268 list_for_each_entry_safe(client_dev
, cd
, &nt
->client_devs
, entry
) {
269 dev_err(client_dev
->dev
.parent
, "%s still attached to bus, removing\n",
270 dev_name(&client_dev
->dev
));
271 list_del(&client_dev
->entry
);
272 device_unregister(&client_dev
->dev
);
275 list_del(&nt
->entry
);
277 if (list_empty(&ntb_transport_list
))
278 bus_unregister(&ntb_bus_type
);
281 static void ntb_client_release(struct device
*dev
)
283 struct ntb_transport_client_dev
*client_dev
;
284 client_dev
= container_of(dev
, struct ntb_transport_client_dev
, dev
);
290 * ntb_unregister_client_dev - Unregister NTB client device
291 * @device_name: Name of NTB client device
293 * Unregister an NTB client device with the NTB transport layer
295 void ntb_unregister_client_dev(char *device_name
)
297 struct ntb_transport_client_dev
*client
, *cd
;
298 struct ntb_transport
*nt
;
300 list_for_each_entry(nt
, &ntb_transport_list
, entry
)
301 list_for_each_entry_safe(client
, cd
, &nt
->client_devs
, entry
)
302 if (!strncmp(dev_name(&client
->dev
), device_name
,
303 strlen(device_name
))) {
304 list_del(&client
->entry
);
305 device_unregister(&client
->dev
);
308 EXPORT_SYMBOL_GPL(ntb_unregister_client_dev
);
311 * ntb_register_client_dev - Register NTB client device
312 * @device_name: Name of NTB client device
314 * Register an NTB client device with the NTB transport layer
316 int ntb_register_client_dev(char *device_name
)
318 struct ntb_transport_client_dev
*client_dev
;
319 struct ntb_transport
*nt
;
322 if (list_empty(&ntb_transport_list
))
325 list_for_each_entry(nt
, &ntb_transport_list
, entry
) {
328 client_dev
= kzalloc(sizeof(struct ntb_transport_client_dev
),
335 dev
= &client_dev
->dev
;
337 /* setup and register client devices */
338 dev_set_name(dev
, "%s%d", device_name
, i
);
339 dev
->bus
= &ntb_bus_type
;
340 dev
->release
= ntb_client_release
;
341 dev
->parent
= &ntb_query_pdev(nt
->ndev
)->dev
;
343 rc
= device_register(dev
);
349 list_add_tail(&client_dev
->entry
, &nt
->client_devs
);
356 ntb_unregister_client_dev(device_name
);
360 EXPORT_SYMBOL_GPL(ntb_register_client_dev
);
363 * ntb_register_client - Register NTB client driver
364 * @drv: NTB client driver to be registered
366 * Register an NTB client driver with the NTB transport layer
368 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
370 int ntb_register_client(struct ntb_client
*drv
)
372 drv
->driver
.bus
= &ntb_bus_type
;
374 if (list_empty(&ntb_transport_list
))
377 return driver_register(&drv
->driver
);
379 EXPORT_SYMBOL_GPL(ntb_register_client
);
382 * ntb_unregister_client - Unregister NTB client driver
383 * @drv: NTB client driver to be unregistered
385 * Unregister an NTB client driver with the NTB transport layer
387 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
389 void ntb_unregister_client(struct ntb_client
*drv
)
391 driver_unregister(&drv
->driver
);
393 EXPORT_SYMBOL_GPL(ntb_unregister_client
);
395 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
398 struct ntb_transport_qp
*qp
;
400 ssize_t ret
, out_offset
, out_count
;
404 buf
= kmalloc(out_count
, GFP_KERNEL
);
408 qp
= filp
->private_data
;
410 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
412 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
413 "rx_bytes - \t%llu\n", qp
->rx_bytes
);
414 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
415 "rx_pkts - \t%llu\n", qp
->rx_pkts
);
416 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
417 "rx_memcpy - \t%llu\n", qp
->rx_memcpy
);
418 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
419 "rx_async - \t%llu\n", qp
->rx_async
);
420 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
421 "rx_ring_empty - %llu\n", qp
->rx_ring_empty
);
422 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
423 "rx_err_no_buf - %llu\n", qp
->rx_err_no_buf
);
424 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
425 "rx_err_oflow - \t%llu\n", qp
->rx_err_oflow
);
426 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
427 "rx_err_ver - \t%llu\n", qp
->rx_err_ver
);
428 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
429 "rx_buff - \t%p\n", qp
->rx_buff
);
430 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
431 "rx_index - \t%u\n", qp
->rx_index
);
432 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
433 "rx_max_entry - \t%u\n", qp
->rx_max_entry
);
435 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
436 "tx_bytes - \t%llu\n", qp
->tx_bytes
);
437 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
438 "tx_pkts - \t%llu\n", qp
->tx_pkts
);
439 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
440 "tx_memcpy - \t%llu\n", qp
->tx_memcpy
);
441 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
442 "tx_async - \t%llu\n", qp
->tx_async
);
443 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
444 "tx_ring_full - \t%llu\n", qp
->tx_ring_full
);
445 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
446 "tx_err_no_buf - %llu\n", qp
->tx_err_no_buf
);
447 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
448 "tx_mw - \t%p\n", qp
->tx_mw
);
449 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
450 "tx_index - \t%u\n", qp
->tx_index
);
451 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
452 "tx_max_entry - \t%u\n", qp
->tx_max_entry
);
454 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
455 "\nQP Link %s\n", (qp
->qp_link
== NTB_LINK_UP
) ?
457 if (out_offset
> out_count
)
458 out_offset
= out_count
;
460 ret
= simple_read_from_buffer(ubuf
, count
, offp
, buf
, out_offset
);
465 static const struct file_operations ntb_qp_debugfs_stats
= {
466 .owner
= THIS_MODULE
,
468 .read
= debugfs_read
,
471 static void ntb_list_add(spinlock_t
*lock
, struct list_head
*entry
,
472 struct list_head
*list
)
476 spin_lock_irqsave(lock
, flags
);
477 list_add_tail(entry
, list
);
478 spin_unlock_irqrestore(lock
, flags
);
481 static struct ntb_queue_entry
*ntb_list_rm(spinlock_t
*lock
,
482 struct list_head
*list
)
484 struct ntb_queue_entry
*entry
;
487 spin_lock_irqsave(lock
, flags
);
488 if (list_empty(list
)) {
492 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
493 list_del(&entry
->entry
);
495 spin_unlock_irqrestore(lock
, flags
);
500 static void ntb_transport_setup_qp_mw(struct ntb_transport
*nt
,
503 struct ntb_transport_qp
*qp
= &nt
->qps
[qp_num
];
504 unsigned int rx_size
, num_qps_mw
;
508 mw_max
= ntb_max_mw(nt
->ndev
);
509 mw_num
= QP_TO_MW(nt
->ndev
, qp_num
);
511 WARN_ON(nt
->mw
[mw_num
].virt_addr
== NULL
);
513 if (nt
->max_qps
% mw_max
&& mw_num
+ 1 < nt
->max_qps
/ mw_max
)
514 num_qps_mw
= nt
->max_qps
/ mw_max
+ 1;
516 num_qps_mw
= nt
->max_qps
/ mw_max
;
518 rx_size
= (unsigned int) nt
->mw
[mw_num
].size
/ num_qps_mw
;
519 qp
->rx_buff
= nt
->mw
[mw_num
].virt_addr
+ qp_num
/ mw_max
* rx_size
;
520 rx_size
-= sizeof(struct ntb_rx_info
);
522 qp
->remote_rx_info
= qp
->rx_buff
+ rx_size
;
524 /* Due to housekeeping, there must be atleast 2 buffs */
525 qp
->rx_max_frame
= min(transport_mtu
, rx_size
/ 2);
526 qp
->rx_max_entry
= rx_size
/ qp
->rx_max_frame
;
529 qp
->remote_rx_info
->entry
= qp
->rx_max_entry
- 1;
531 /* setup the hdr offsets with 0's */
532 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
533 void *offset
= qp
->rx_buff
+ qp
->rx_max_frame
* (i
+ 1) -
534 sizeof(struct ntb_payload_header
);
535 memset(offset
, 0, sizeof(struct ntb_payload_header
));
543 static void ntb_free_mw(struct ntb_transport
*nt
, int num_mw
)
545 struct ntb_transport_mw
*mw
= &nt
->mw
[num_mw
];
546 struct pci_dev
*pdev
= ntb_query_pdev(nt
->ndev
);
551 dma_free_coherent(&pdev
->dev
, mw
->size
, mw
->virt_addr
, mw
->dma_addr
);
552 mw
->virt_addr
= NULL
;
555 static int ntb_set_mw(struct ntb_transport
*nt
, int num_mw
, unsigned int size
)
557 struct ntb_transport_mw
*mw
= &nt
->mw
[num_mw
];
558 struct pci_dev
*pdev
= ntb_query_pdev(nt
->ndev
);
560 /* No need to re-setup */
561 if (mw
->size
== ALIGN(size
, 4096))
565 ntb_free_mw(nt
, num_mw
);
567 /* Alloc memory for receiving data. Must be 4k aligned */
568 mw
->size
= ALIGN(size
, 4096);
570 mw
->virt_addr
= dma_alloc_coherent(&pdev
->dev
, mw
->size
, &mw
->dma_addr
,
572 if (!mw
->virt_addr
) {
574 dev_err(&pdev
->dev
, "Unable to allocate MW buffer of size %d\n",
580 * we must ensure that the memory address allocated is BAR size
581 * aligned in order for the XLAT register to take the value. This
582 * is a requirement of the hardware. It is recommended to setup CMA
583 * for BAR sizes equal or greater than 4MB.
585 if (!IS_ALIGNED(mw
->dma_addr
, mw
->size
)) {
586 dev_err(&pdev
->dev
, "DMA memory %pad not aligned to BAR size\n",
588 ntb_free_mw(nt
, num_mw
);
592 /* Notify HW the memory location of the receive buffer */
593 ntb_set_mw_addr(nt
->ndev
, num_mw
, mw
->dma_addr
);
598 static void ntb_qp_link_cleanup(struct ntb_transport_qp
*qp
)
600 struct ntb_transport
*nt
= qp
->transport
;
601 struct pci_dev
*pdev
= ntb_query_pdev(nt
->ndev
);
603 if (qp
->qp_link
== NTB_LINK_DOWN
) {
604 cancel_delayed_work_sync(&qp
->link_work
);
608 if (qp
->event_handler
)
609 qp
->event_handler(qp
->cb_data
, NTB_LINK_DOWN
);
611 dev_info(&pdev
->dev
, "qp %d: Link Down\n", qp
->qp_num
);
612 qp
->qp_link
= NTB_LINK_DOWN
;
615 static void ntb_qp_link_cleanup_work(struct work_struct
*work
)
617 struct ntb_transport_qp
*qp
= container_of(work
,
618 struct ntb_transport_qp
,
620 struct ntb_transport
*nt
= qp
->transport
;
622 ntb_qp_link_cleanup(qp
);
624 if (nt
->transport_link
== NTB_LINK_UP
)
625 schedule_delayed_work(&qp
->link_work
,
626 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
629 static void ntb_qp_link_down(struct ntb_transport_qp
*qp
)
631 schedule_work(&qp
->link_cleanup
);
634 static void ntb_transport_link_cleanup(struct ntb_transport
*nt
)
638 /* Pass along the info to any clients */
639 for (i
= 0; i
< nt
->max_qps
; i
++)
640 if (!test_bit(i
, &nt
->qp_bitmap
))
641 ntb_qp_link_cleanup(&nt
->qps
[i
]);
643 if (nt
->transport_link
== NTB_LINK_DOWN
)
644 cancel_delayed_work_sync(&nt
->link_work
);
646 nt
->transport_link
= NTB_LINK_DOWN
;
648 /* The scratchpad registers keep the values if the remote side
649 * goes down, blast them now to give them a sane value the next
650 * time they are accessed
652 for (i
= 0; i
< MAX_SPAD
; i
++)
653 ntb_write_local_spad(nt
->ndev
, i
, 0);
656 static void ntb_transport_link_cleanup_work(struct work_struct
*work
)
658 struct ntb_transport
*nt
= container_of(work
, struct ntb_transport
,
661 ntb_transport_link_cleanup(nt
);
664 static void ntb_transport_event_callback(void *data
, enum ntb_hw_event event
)
666 struct ntb_transport
*nt
= data
;
669 case NTB_EVENT_HW_LINK_UP
:
670 schedule_delayed_work(&nt
->link_work
, 0);
672 case NTB_EVENT_HW_LINK_DOWN
:
673 schedule_work(&nt
->link_cleanup
);
680 static void ntb_transport_link_work(struct work_struct
*work
)
682 struct ntb_transport
*nt
= container_of(work
, struct ntb_transport
,
684 struct ntb_device
*ndev
= nt
->ndev
;
685 struct pci_dev
*pdev
= ntb_query_pdev(ndev
);
689 /* send the local info, in the opposite order of the way we read it */
690 for (i
= 0; i
< ntb_max_mw(ndev
); i
++) {
691 rc
= ntb_write_remote_spad(ndev
, MW0_SZ_HIGH
+ (i
* 2),
692 ntb_get_mw_size(ndev
, i
) >> 32);
694 dev_err(&pdev
->dev
, "Error writing %u to remote spad %d\n",
695 (u32
)(ntb_get_mw_size(ndev
, i
) >> 32),
696 MW0_SZ_HIGH
+ (i
* 2));
700 rc
= ntb_write_remote_spad(ndev
, MW0_SZ_LOW
+ (i
* 2),
701 (u32
) ntb_get_mw_size(ndev
, i
));
703 dev_err(&pdev
->dev
, "Error writing %u to remote spad %d\n",
704 (u32
) ntb_get_mw_size(ndev
, i
),
705 MW0_SZ_LOW
+ (i
* 2));
710 rc
= ntb_write_remote_spad(ndev
, NUM_MWS
, ntb_max_mw(ndev
));
712 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
713 ntb_max_mw(ndev
), NUM_MWS
);
717 rc
= ntb_write_remote_spad(ndev
, NUM_QPS
, nt
->max_qps
);
719 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
720 nt
->max_qps
, NUM_QPS
);
724 rc
= ntb_write_remote_spad(ndev
, VERSION
, NTB_TRANSPORT_VERSION
);
726 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
727 NTB_TRANSPORT_VERSION
, VERSION
);
731 /* Query the remote side for its info */
732 rc
= ntb_read_remote_spad(ndev
, VERSION
, &val
);
734 dev_err(&pdev
->dev
, "Error reading remote spad %d\n", VERSION
);
738 if (val
!= NTB_TRANSPORT_VERSION
)
740 dev_dbg(&pdev
->dev
, "Remote version = %d\n", val
);
742 rc
= ntb_read_remote_spad(ndev
, NUM_QPS
, &val
);
744 dev_err(&pdev
->dev
, "Error reading remote spad %d\n", NUM_QPS
);
748 if (val
!= nt
->max_qps
)
750 dev_dbg(&pdev
->dev
, "Remote max number of qps = %d\n", val
);
752 rc
= ntb_read_remote_spad(ndev
, NUM_MWS
, &val
);
754 dev_err(&pdev
->dev
, "Error reading remote spad %d\n", NUM_MWS
);
758 if (val
!= ntb_max_mw(ndev
))
760 dev_dbg(&pdev
->dev
, "Remote number of mws = %d\n", val
);
762 for (i
= 0; i
< ntb_max_mw(ndev
); i
++) {
765 rc
= ntb_read_remote_spad(ndev
, MW0_SZ_HIGH
+ (i
* 2), &val
);
767 dev_err(&pdev
->dev
, "Error reading remote spad %d\n",
768 MW0_SZ_HIGH
+ (i
* 2));
772 val64
= (u64
) val
<< 32;
774 rc
= ntb_read_remote_spad(ndev
, MW0_SZ_LOW
+ (i
* 2), &val
);
776 dev_err(&pdev
->dev
, "Error reading remote spad %d\n",
777 MW0_SZ_LOW
+ (i
* 2));
783 dev_dbg(&pdev
->dev
, "Remote MW%d size = %llu\n", i
, val64
);
785 rc
= ntb_set_mw(nt
, i
, val64
);
790 nt
->transport_link
= NTB_LINK_UP
;
792 for (i
= 0; i
< nt
->max_qps
; i
++) {
793 struct ntb_transport_qp
*qp
= &nt
->qps
[i
];
795 ntb_transport_setup_qp_mw(nt
, i
);
797 if (qp
->client_ready
== NTB_LINK_UP
)
798 schedule_delayed_work(&qp
->link_work
, 0);
804 for (i
= 0; i
< ntb_max_mw(ndev
); i
++)
807 if (ntb_hw_link_status(ndev
))
808 schedule_delayed_work(&nt
->link_work
,
809 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
812 static void ntb_qp_link_work(struct work_struct
*work
)
814 struct ntb_transport_qp
*qp
= container_of(work
,
815 struct ntb_transport_qp
,
817 struct pci_dev
*pdev
= ntb_query_pdev(qp
->ndev
);
818 struct ntb_transport
*nt
= qp
->transport
;
821 WARN_ON(nt
->transport_link
!= NTB_LINK_UP
);
823 rc
= ntb_read_local_spad(nt
->ndev
, QP_LINKS
, &val
);
825 dev_err(&pdev
->dev
, "Error reading spad %d\n", QP_LINKS
);
829 rc
= ntb_write_remote_spad(nt
->ndev
, QP_LINKS
, val
| 1 << qp
->qp_num
);
831 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
832 val
| 1 << qp
->qp_num
, QP_LINKS
);
834 /* query remote spad for qp ready bits */
835 rc
= ntb_read_remote_spad(nt
->ndev
, QP_LINKS
, &val
);
837 dev_err(&pdev
->dev
, "Error reading remote spad %d\n", QP_LINKS
);
839 dev_dbg(&pdev
->dev
, "Remote QP link status = %x\n", val
);
841 /* See if the remote side is up */
842 if (1 << qp
->qp_num
& val
) {
843 qp
->qp_link
= NTB_LINK_UP
;
845 dev_info(&pdev
->dev
, "qp %d: Link Up\n", qp
->qp_num
);
846 if (qp
->event_handler
)
847 qp
->event_handler(qp
->cb_data
, NTB_LINK_UP
);
848 } else if (nt
->transport_link
== NTB_LINK_UP
)
849 schedule_delayed_work(&qp
->link_work
,
850 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
853 static int ntb_transport_init_queue(struct ntb_transport
*nt
,
856 struct ntb_transport_qp
*qp
;
857 unsigned int num_qps_mw
, tx_size
;
861 mw_max
= ntb_max_mw(nt
->ndev
);
862 mw_num
= QP_TO_MW(nt
->ndev
, qp_num
);
864 qp
= &nt
->qps
[qp_num
];
868 qp
->qp_link
= NTB_LINK_DOWN
;
869 qp
->client_ready
= NTB_LINK_DOWN
;
870 qp
->event_handler
= NULL
;
872 if (nt
->max_qps
% mw_max
&& mw_num
+ 1 < nt
->max_qps
/ mw_max
)
873 num_qps_mw
= nt
->max_qps
/ mw_max
+ 1;
875 num_qps_mw
= nt
->max_qps
/ mw_max
;
877 tx_size
= (unsigned int) ntb_get_mw_size(qp
->ndev
, mw_num
) / num_qps_mw
;
878 qp_offset
= qp_num
/ mw_max
* tx_size
;
879 qp
->tx_mw
= ntb_get_mw_vbase(nt
->ndev
, mw_num
) + qp_offset
;
883 qp
->tx_mw_phys
= ntb_get_mw_base(qp
->ndev
, mw_num
) + qp_offset
;
887 tx_size
-= sizeof(struct ntb_rx_info
);
888 qp
->rx_info
= qp
->tx_mw
+ tx_size
;
890 /* Due to housekeeping, there must be atleast 2 buffs */
891 qp
->tx_max_frame
= min(transport_mtu
, tx_size
/ 2);
892 qp
->tx_max_entry
= tx_size
/ qp
->tx_max_frame
;
894 if (ntb_query_debugfs(nt
->ndev
)) {
895 char debugfs_name
[4];
897 snprintf(debugfs_name
, 4, "qp%d", qp_num
);
898 qp
->debugfs_dir
= debugfs_create_dir(debugfs_name
,
899 ntb_query_debugfs(nt
->ndev
));
901 qp
->debugfs_stats
= debugfs_create_file("stats", S_IRUSR
,
903 &ntb_qp_debugfs_stats
);
906 INIT_DELAYED_WORK(&qp
->link_work
, ntb_qp_link_work
);
907 INIT_WORK(&qp
->link_cleanup
, ntb_qp_link_cleanup_work
);
909 spin_lock_init(&qp
->ntb_rx_pend_q_lock
);
910 spin_lock_init(&qp
->ntb_rx_free_q_lock
);
911 spin_lock_init(&qp
->ntb_tx_free_q_lock
);
913 INIT_LIST_HEAD(&qp
->rx_pend_q
);
914 INIT_LIST_HEAD(&qp
->rx_free_q
);
915 INIT_LIST_HEAD(&qp
->tx_free_q
);
920 int ntb_transport_init(struct pci_dev
*pdev
)
922 struct ntb_transport
*nt
;
925 nt
= kzalloc(sizeof(struct ntb_transport
), GFP_KERNEL
);
929 nt
->ndev
= ntb_register_transport(pdev
, nt
);
935 nt
->mw
= kcalloc(ntb_max_mw(nt
->ndev
), sizeof(struct ntb_transport_mw
),
943 nt
->max_qps
= min(ntb_max_cbs(nt
->ndev
), max_num_clients
);
945 nt
->max_qps
= min(ntb_max_cbs(nt
->ndev
), ntb_max_mw(nt
->ndev
));
947 nt
->qps
= kcalloc(nt
->max_qps
, sizeof(struct ntb_transport_qp
),
954 nt
->qp_bitmap
= ((u64
) 1 << nt
->max_qps
) - 1;
956 for (i
= 0; i
< nt
->max_qps
; i
++) {
957 rc
= ntb_transport_init_queue(nt
, i
);
962 INIT_DELAYED_WORK(&nt
->link_work
, ntb_transport_link_work
);
963 INIT_WORK(&nt
->link_cleanup
, ntb_transport_link_cleanup_work
);
965 rc
= ntb_register_event_callback(nt
->ndev
,
966 ntb_transport_event_callback
);
970 INIT_LIST_HEAD(&nt
->client_devs
);
971 rc
= ntb_bus_init(nt
);
975 if (ntb_hw_link_status(nt
->ndev
))
976 schedule_delayed_work(&nt
->link_work
, 0);
981 ntb_unregister_event_callback(nt
->ndev
);
987 ntb_unregister_transport(nt
->ndev
);
993 void ntb_transport_free(void *transport
)
995 struct ntb_transport
*nt
= transport
;
996 struct ntb_device
*ndev
= nt
->ndev
;
999 ntb_transport_link_cleanup(nt
);
1001 /* verify that all the qp's are freed */
1002 for (i
= 0; i
< nt
->max_qps
; i
++) {
1003 if (!test_bit(i
, &nt
->qp_bitmap
))
1004 ntb_transport_free_queue(&nt
->qps
[i
]);
1005 debugfs_remove_recursive(nt
->qps
[i
].debugfs_dir
);
1010 cancel_delayed_work_sync(&nt
->link_work
);
1012 ntb_unregister_event_callback(ndev
);
1014 for (i
= 0; i
< ntb_max_mw(ndev
); i
++)
1019 ntb_unregister_transport(ndev
);
1023 static void ntb_rx_copy_callback(void *data
)
1025 struct ntb_queue_entry
*entry
= data
;
1026 struct ntb_transport_qp
*qp
= entry
->qp
;
1027 void *cb_data
= entry
->cb_data
;
1028 unsigned int len
= entry
->len
;
1029 struct ntb_payload_header
*hdr
= entry
->rx_hdr
;
1031 /* Ensure that the data is fully copied out before clearing the flag */
1035 iowrite32(entry
->index
, &qp
->rx_info
->entry
);
1037 ntb_list_add(&qp
->ntb_rx_free_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
1039 if (qp
->rx_handler
&& qp
->client_ready
== NTB_LINK_UP
)
1040 qp
->rx_handler(qp
, qp
->cb_data
, cb_data
, len
);
1043 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
)
1045 void *buf
= entry
->buf
;
1046 size_t len
= entry
->len
;
1048 memcpy(buf
, offset
, len
);
1050 ntb_rx_copy_callback(entry
);
1053 static void ntb_async_rx(struct ntb_queue_entry
*entry
, void *offset
,
1056 struct dma_async_tx_descriptor
*txd
;
1057 struct ntb_transport_qp
*qp
= entry
->qp
;
1058 struct dma_chan
*chan
= qp
->dma_chan
;
1059 struct dma_device
*device
;
1060 size_t pay_off
, buff_off
;
1061 struct dmaengine_unmap_data
*unmap
;
1062 dma_cookie_t cookie
;
1063 void *buf
= entry
->buf
;
1070 if (len
< copy_bytes
)
1073 device
= chan
->device
;
1074 pay_off
= (size_t) offset
& ~PAGE_MASK
;
1075 buff_off
= (size_t) buf
& ~PAGE_MASK
;
1077 if (!is_dma_copy_aligned(device
, pay_off
, buff_off
, len
))
1080 unmap
= dmaengine_get_unmap_data(device
->dev
, 2, GFP_NOWAIT
);
1085 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(offset
),
1086 pay_off
, len
, DMA_TO_DEVICE
);
1087 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1092 unmap
->addr
[1] = dma_map_page(device
->dev
, virt_to_page(buf
),
1093 buff_off
, len
, DMA_FROM_DEVICE
);
1094 if (dma_mapping_error(device
->dev
, unmap
->addr
[1]))
1097 unmap
->from_cnt
= 1;
1099 txd
= device
->device_prep_dma_memcpy(chan
, unmap
->addr
[1],
1100 unmap
->addr
[0], len
,
1101 DMA_PREP_INTERRUPT
);
1105 txd
->callback
= ntb_rx_copy_callback
;
1106 txd
->callback_param
= entry
;
1107 dma_set_unmap(txd
, unmap
);
1109 cookie
= dmaengine_submit(txd
);
1110 if (dma_submit_error(cookie
))
1113 dmaengine_unmap_put(unmap
);
1115 qp
->last_cookie
= cookie
;
1122 dmaengine_unmap_put(unmap
);
1124 dmaengine_unmap_put(unmap
);
1126 /* If the callbacks come out of order, the writing of the index to the
1127 * last completed will be out of order. This may result in the
1128 * receive stalling forever.
1130 dma_sync_wait(chan
, qp
->last_cookie
);
1132 ntb_memcpy_rx(entry
, offset
);
1136 static int ntb_process_rxc(struct ntb_transport_qp
*qp
)
1138 struct ntb_payload_header
*hdr
;
1139 struct ntb_queue_entry
*entry
;
1142 offset
= qp
->rx_buff
+ qp
->rx_max_frame
* qp
->rx_index
;
1143 hdr
= offset
+ qp
->rx_max_frame
- sizeof(struct ntb_payload_header
);
1145 entry
= ntb_list_rm(&qp
->ntb_rx_pend_q_lock
, &qp
->rx_pend_q
);
1147 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
,
1148 "no buffer - HDR ver %u, len %d, flags %x\n",
1149 hdr
->ver
, hdr
->len
, hdr
->flags
);
1150 qp
->rx_err_no_buf
++;
1154 if (!(hdr
->flags
& DESC_DONE_FLAG
)) {
1155 ntb_list_add(&qp
->ntb_rx_pend_q_lock
, &entry
->entry
,
1157 qp
->rx_ring_empty
++;
1161 if (hdr
->ver
!= (u32
) qp
->rx_pkts
) {
1162 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
,
1163 "qp %d: version mismatch, expected %llu - got %u\n",
1164 qp
->qp_num
, qp
->rx_pkts
, hdr
->ver
);
1165 ntb_list_add(&qp
->ntb_rx_pend_q_lock
, &entry
->entry
,
1171 if (hdr
->flags
& LINK_DOWN_FLAG
) {
1172 ntb_qp_link_down(qp
);
1177 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
,
1178 "rx offset %u, ver %u - %d payload received, buf size %d\n",
1179 qp
->rx_index
, hdr
->ver
, hdr
->len
, entry
->len
);
1181 qp
->rx_bytes
+= hdr
->len
;
1184 if (hdr
->len
> entry
->len
) {
1186 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
,
1187 "RX overflow! Wanted %d got %d\n",
1188 hdr
->len
, entry
->len
);
1193 entry
->index
= qp
->rx_index
;
1194 entry
->rx_hdr
= hdr
;
1196 ntb_async_rx(entry
, offset
, hdr
->len
);
1200 qp
->rx_index
%= qp
->rx_max_entry
;
1205 ntb_list_add(&qp
->ntb_rx_pend_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
1206 /* Ensure that the data is fully copied out before clearing the flag */
1209 iowrite32(qp
->rx_index
, &qp
->rx_info
->entry
);
1214 static int ntb_transport_rxc_db(void *data
, int db_num
)
1216 struct ntb_transport_qp
*qp
= data
;
1219 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
, "%s: doorbell %d received\n",
1222 /* Limit the number of packets processed in a single interrupt to
1223 * provide fairness to others
1225 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
1226 rc
= ntb_process_rxc(qp
);
1232 dma_async_issue_pending(qp
->dma_chan
);
1237 static void ntb_tx_copy_callback(void *data
)
1239 struct ntb_queue_entry
*entry
= data
;
1240 struct ntb_transport_qp
*qp
= entry
->qp
;
1241 struct ntb_payload_header __iomem
*hdr
= entry
->tx_hdr
;
1243 /* Ensure that the data is fully copied out before setting the flags */
1245 iowrite32(entry
->flags
| DESC_DONE_FLAG
, &hdr
->flags
);
1247 ntb_ring_doorbell(qp
->ndev
, qp
->qp_num
);
1249 /* The entry length can only be zero if the packet is intended to be a
1250 * "link down" or similar. Since no payload is being sent in these
1251 * cases, there is nothing to add to the completion queue.
1253 if (entry
->len
> 0) {
1254 qp
->tx_bytes
+= entry
->len
;
1257 qp
->tx_handler(qp
, qp
->cb_data
, entry
->cb_data
,
1261 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
, &qp
->tx_free_q
);
1264 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
)
1266 memcpy_toio(offset
, entry
->buf
, entry
->len
);
1268 ntb_tx_copy_callback(entry
);
1271 static void ntb_async_tx(struct ntb_transport_qp
*qp
,
1272 struct ntb_queue_entry
*entry
)
1274 struct ntb_payload_header __iomem
*hdr
;
1275 struct dma_async_tx_descriptor
*txd
;
1276 struct dma_chan
*chan
= qp
->dma_chan
;
1277 struct dma_device
*device
;
1278 size_t dest_off
, buff_off
;
1279 struct dmaengine_unmap_data
*unmap
;
1281 dma_cookie_t cookie
;
1282 void __iomem
*offset
;
1283 size_t len
= entry
->len
;
1284 void *buf
= entry
->buf
;
1286 offset
= qp
->tx_mw
+ qp
->tx_max_frame
* qp
->tx_index
;
1287 hdr
= offset
+ qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1288 entry
->tx_hdr
= hdr
;
1290 iowrite32(entry
->len
, &hdr
->len
);
1291 iowrite32((u32
) qp
->tx_pkts
, &hdr
->ver
);
1296 if (len
< copy_bytes
)
1299 device
= chan
->device
;
1300 dest
= qp
->tx_mw_phys
+ qp
->tx_max_frame
* qp
->tx_index
;
1301 buff_off
= (size_t) buf
& ~PAGE_MASK
;
1302 dest_off
= (size_t) dest
& ~PAGE_MASK
;
1304 if (!is_dma_copy_aligned(device
, buff_off
, dest_off
, len
))
1307 unmap
= dmaengine_get_unmap_data(device
->dev
, 1, GFP_NOWAIT
);
1312 unmap
->addr
[0] = dma_map_page(device
->dev
, virt_to_page(buf
),
1313 buff_off
, len
, DMA_TO_DEVICE
);
1314 if (dma_mapping_error(device
->dev
, unmap
->addr
[0]))
1319 txd
= device
->device_prep_dma_memcpy(chan
, dest
, unmap
->addr
[0], len
,
1320 DMA_PREP_INTERRUPT
);
1324 txd
->callback
= ntb_tx_copy_callback
;
1325 txd
->callback_param
= entry
;
1326 dma_set_unmap(txd
, unmap
);
1328 cookie
= dmaengine_submit(txd
);
1329 if (dma_submit_error(cookie
))
1332 dmaengine_unmap_put(unmap
);
1334 dma_async_issue_pending(chan
);
1339 dmaengine_unmap_put(unmap
);
1341 dmaengine_unmap_put(unmap
);
1343 ntb_memcpy_tx(entry
, offset
);
1347 static int ntb_process_tx(struct ntb_transport_qp
*qp
,
1348 struct ntb_queue_entry
*entry
)
1350 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
, "%lld - tx %u, entry len %d flags %x buff %p\n",
1351 qp
->tx_pkts
, qp
->tx_index
, entry
->len
, entry
->flags
,
1353 if (qp
->tx_index
== qp
->remote_rx_info
->entry
) {
1358 if (entry
->len
> qp
->tx_max_frame
- sizeof(struct ntb_payload_header
)) {
1360 qp
->tx_handler(qp
->cb_data
, qp
, NULL
, -EIO
);
1362 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1367 ntb_async_tx(qp
, entry
);
1370 qp
->tx_index
%= qp
->tx_max_entry
;
1377 static void ntb_send_link_down(struct ntb_transport_qp
*qp
)
1379 struct pci_dev
*pdev
= ntb_query_pdev(qp
->ndev
);
1380 struct ntb_queue_entry
*entry
;
1383 if (qp
->qp_link
== NTB_LINK_DOWN
)
1386 qp
->qp_link
= NTB_LINK_DOWN
;
1387 dev_info(&pdev
->dev
, "qp %d: Link Down\n", qp
->qp_num
);
1389 for (i
= 0; i
< NTB_LINK_DOWN_TIMEOUT
; i
++) {
1390 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1399 entry
->cb_data
= NULL
;
1402 entry
->flags
= LINK_DOWN_FLAG
;
1404 rc
= ntb_process_tx(qp
, entry
);
1406 dev_err(&pdev
->dev
, "ntb: QP%d unable to send linkdown msg\n",
1411 * ntb_transport_create_queue - Create a new NTB transport layer queue
1412 * @rx_handler: receive callback function
1413 * @tx_handler: transmit callback function
1414 * @event_handler: event callback function
1416 * Create a new NTB transport layer queue and provide the queue with a callback
1417 * routine for both transmit and receive. The receive callback routine will be
1418 * used to pass up data when the transport has received it on the queue. The
1419 * transmit callback routine will be called when the transport has completed the
1420 * transmission of the data on the queue and the data is ready to be freed.
1422 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1424 struct ntb_transport_qp
*
1425 ntb_transport_create_queue(void *data
, struct pci_dev
*pdev
,
1426 const struct ntb_queue_handlers
*handlers
)
1428 struct ntb_queue_entry
*entry
;
1429 struct ntb_transport_qp
*qp
;
1430 struct ntb_transport
*nt
;
1431 unsigned int free_queue
;
1434 nt
= ntb_find_transport(pdev
);
1438 free_queue
= ffs(nt
->qp_bitmap
);
1442 /* decrement free_queue to make it zero based */
1445 clear_bit(free_queue
, &nt
->qp_bitmap
);
1447 qp
= &nt
->qps
[free_queue
];
1449 qp
->rx_handler
= handlers
->rx_handler
;
1450 qp
->tx_handler
= handlers
->tx_handler
;
1451 qp
->event_handler
= handlers
->event_handler
;
1454 qp
->dma_chan
= dma_find_channel(DMA_MEMCPY
);
1455 if (!qp
->dma_chan
) {
1457 dev_info(&pdev
->dev
, "Unable to allocate DMA channel, using CPU instead\n");
1460 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1461 entry
= kzalloc(sizeof(struct ntb_queue_entry
), GFP_ATOMIC
);
1466 ntb_list_add(&qp
->ntb_rx_free_q_lock
, &entry
->entry
,
1470 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1471 entry
= kzalloc(sizeof(struct ntb_queue_entry
), GFP_ATOMIC
);
1476 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1480 rc
= ntb_register_db_callback(qp
->ndev
, free_queue
, qp
,
1481 ntb_transport_rxc_db
);
1485 dev_info(&pdev
->dev
, "NTB Transport QP %d created\n", qp
->qp_num
);
1490 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1493 while ((entry
= ntb_list_rm(&qp
->ntb_rx_free_q_lock
, &qp
->rx_free_q
)))
1497 set_bit(free_queue
, &nt
->qp_bitmap
);
1501 EXPORT_SYMBOL_GPL(ntb_transport_create_queue
);
1504 * ntb_transport_free_queue - Frees NTB transport queue
1505 * @qp: NTB queue to be freed
1507 * Frees NTB transport queue
1509 void ntb_transport_free_queue(struct ntb_transport_qp
*qp
)
1511 struct pci_dev
*pdev
;
1512 struct ntb_queue_entry
*entry
;
1517 pdev
= ntb_query_pdev(qp
->ndev
);
1520 struct dma_chan
*chan
= qp
->dma_chan
;
1521 /* Putting the dma_chan to NULL will force any new traffic to be
1522 * processed by the CPU instead of the DAM engine
1524 qp
->dma_chan
= NULL
;
1526 /* Try to be nice and wait for any queued DMA engine
1527 * transactions to process before smashing it with a rock
1529 dma_sync_wait(chan
, qp
->last_cookie
);
1530 dmaengine_terminate_all(chan
);
1534 ntb_unregister_db_callback(qp
->ndev
, qp
->qp_num
);
1536 cancel_delayed_work_sync(&qp
->link_work
);
1538 while ((entry
= ntb_list_rm(&qp
->ntb_rx_free_q_lock
, &qp
->rx_free_q
)))
1541 while ((entry
= ntb_list_rm(&qp
->ntb_rx_pend_q_lock
, &qp
->rx_pend_q
))) {
1542 dev_warn(&pdev
->dev
, "Freeing item from a non-empty queue\n");
1546 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1549 set_bit(qp
->qp_num
, &qp
->transport
->qp_bitmap
);
1551 dev_info(&pdev
->dev
, "NTB Transport QP %d freed\n", qp
->qp_num
);
1553 EXPORT_SYMBOL_GPL(ntb_transport_free_queue
);
1556 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1557 * @qp: NTB queue to be freed
1558 * @len: pointer to variable to write enqueued buffers length
1560 * Dequeues unused buffers from receive queue. Should only be used during
1563 * RETURNS: NULL error value on error, or void* for success.
1565 void *ntb_transport_rx_remove(struct ntb_transport_qp
*qp
, unsigned int *len
)
1567 struct ntb_queue_entry
*entry
;
1570 if (!qp
|| qp
->client_ready
== NTB_LINK_UP
)
1573 entry
= ntb_list_rm(&qp
->ntb_rx_pend_q_lock
, &qp
->rx_pend_q
);
1577 buf
= entry
->cb_data
;
1580 ntb_list_add(&qp
->ntb_rx_free_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
1584 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove
);
1587 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1588 * @qp: NTB transport layer queue the entry is to be enqueued on
1589 * @cb: per buffer pointer for callback function to use
1590 * @data: pointer to data buffer that incoming packets will be copied into
1591 * @len: length of the data buffer
1593 * Enqueue a new receive buffer onto the transport queue into which a NTB
1594 * payload can be received into.
1596 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1598 int ntb_transport_rx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1601 struct ntb_queue_entry
*entry
;
1606 entry
= ntb_list_rm(&qp
->ntb_rx_free_q_lock
, &qp
->rx_free_q
);
1610 entry
->cb_data
= cb
;
1614 ntb_list_add(&qp
->ntb_rx_pend_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
1618 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue
);
1621 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1622 * @qp: NTB transport layer queue the entry is to be enqueued on
1623 * @cb: per buffer pointer for callback function to use
1624 * @data: pointer to data buffer that will be sent
1625 * @len: length of the data buffer
1627 * Enqueue a new transmit buffer onto the transport queue from which a NTB
1628 * payload will be transmitted. This assumes that a lock is being held to
1629 * serialize access to the qp.
1631 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1633 int ntb_transport_tx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1636 struct ntb_queue_entry
*entry
;
1639 if (!qp
|| qp
->qp_link
!= NTB_LINK_UP
|| !len
)
1642 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1644 qp
->tx_err_no_buf
++;
1648 entry
->cb_data
= cb
;
1653 rc
= ntb_process_tx(qp
, entry
);
1655 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1660 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue
);
1663 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1664 * @qp: NTB transport layer queue to be enabled
1666 * Notify NTB transport layer of client readiness to use queue
1668 void ntb_transport_link_up(struct ntb_transport_qp
*qp
)
1673 qp
->client_ready
= NTB_LINK_UP
;
1675 if (qp
->transport
->transport_link
== NTB_LINK_UP
)
1676 schedule_delayed_work(&qp
->link_work
, 0);
1678 EXPORT_SYMBOL_GPL(ntb_transport_link_up
);
1681 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1682 * @qp: NTB transport layer queue to be disabled
1684 * Notify NTB transport layer of client's desire to no longer receive data on
1685 * transport queue specified. It is the client's responsibility to ensure all
1686 * entries on queue are purged or otherwise handled appropriately.
1688 void ntb_transport_link_down(struct ntb_transport_qp
*qp
)
1690 struct pci_dev
*pdev
;
1696 pdev
= ntb_query_pdev(qp
->ndev
);
1697 qp
->client_ready
= NTB_LINK_DOWN
;
1699 rc
= ntb_read_local_spad(qp
->ndev
, QP_LINKS
, &val
);
1701 dev_err(&pdev
->dev
, "Error reading spad %d\n", QP_LINKS
);
1705 rc
= ntb_write_remote_spad(qp
->ndev
, QP_LINKS
,
1706 val
& ~(1 << qp
->qp_num
));
1708 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
1709 val
& ~(1 << qp
->qp_num
), QP_LINKS
);
1711 if (qp
->qp_link
== NTB_LINK_UP
)
1712 ntb_send_link_down(qp
);
1714 cancel_delayed_work_sync(&qp
->link_work
);
1716 EXPORT_SYMBOL_GPL(ntb_transport_link_down
);
1719 * ntb_transport_link_query - Query transport link state
1720 * @qp: NTB transport layer queue to be queried
1722 * Query connectivity to the remote system of the NTB transport queue
1724 * RETURNS: true for link up or false for link down
1726 bool ntb_transport_link_query(struct ntb_transport_qp
*qp
)
1731 return qp
->qp_link
== NTB_LINK_UP
;
1733 EXPORT_SYMBOL_GPL(ntb_transport_link_query
);
1736 * ntb_transport_qp_num - Query the qp number
1737 * @qp: NTB transport layer queue to be queried
1739 * Query qp number of the NTB transport queue
1741 * RETURNS: a zero based number specifying the qp number
1743 unsigned char ntb_transport_qp_num(struct ntb_transport_qp
*qp
)
1750 EXPORT_SYMBOL_GPL(ntb_transport_qp_num
);
1753 * ntb_transport_max_size - Query the max payload size of a qp
1754 * @qp: NTB transport layer queue to be queried
1756 * Query the maximum payload size permissible on the given qp
1758 * RETURNS: the max payload size of a qp
1760 unsigned int ntb_transport_max_size(struct ntb_transport_qp
*qp
)
1768 return qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1770 /* If DMA engine usage is possible, try to find the max size for that */
1771 max
= qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1772 max
-= max
% (1 << qp
->dma_chan
->device
->copy_align
);
1776 EXPORT_SYMBOL_GPL(ntb_transport_max_size
);