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>
59 #include <linux/ntb.h>
62 #define NTB_TRANSPORT_VERSION 3
64 static unsigned int transport_mtu
= 0x401E;
65 module_param(transport_mtu
, uint
, 0644);
66 MODULE_PARM_DESC(transport_mtu
, "Maximum size of NTB transport packets");
68 static unsigned char max_num_clients
;
69 module_param(max_num_clients
, byte
, 0644);
70 MODULE_PARM_DESC(max_num_clients
, "Maximum number of NTB transport clients");
72 static unsigned int copy_bytes
= 1024;
73 module_param(copy_bytes
, uint
, 0644);
74 MODULE_PARM_DESC(copy_bytes
, "Threshold under which NTB will use the CPU to copy instead of DMA");
76 struct ntb_queue_entry
{
77 /* ntb_queue list reference */
78 struct list_head entry
;
79 /* pointers to data to be transfered */
85 struct ntb_transport_qp
*qp
;
87 struct ntb_payload_header __iomem
*tx_hdr
;
88 struct ntb_payload_header
*rx_hdr
;
97 struct ntb_transport_qp
{
98 struct ntb_transport
*transport
;
99 struct ntb_device
*ndev
;
101 struct dma_chan
*dma_chan
;
105 u8 qp_num
; /* Only 64 QP's are allowed. 0-63 */
107 struct ntb_rx_info __iomem
*rx_info
;
108 struct ntb_rx_info
*remote_rx_info
;
110 void (*tx_handler
) (struct ntb_transport_qp
*qp
, void *qp_data
,
111 void *data
, int len
);
112 struct list_head tx_free_q
;
113 spinlock_t ntb_tx_free_q_lock
;
115 dma_addr_t tx_mw_phys
;
116 unsigned int tx_index
;
117 unsigned int tx_max_entry
;
118 unsigned int tx_max_frame
;
120 void (*rx_handler
) (struct ntb_transport_qp
*qp
, void *qp_data
,
121 void *data
, int len
);
122 struct tasklet_struct rx_work
;
123 struct list_head rx_pend_q
;
124 struct list_head rx_free_q
;
125 spinlock_t ntb_rx_pend_q_lock
;
126 spinlock_t ntb_rx_free_q_lock
;
128 unsigned int rx_index
;
129 unsigned int rx_max_entry
;
130 unsigned int rx_max_frame
;
131 dma_cookie_t last_cookie
;
133 void (*event_handler
) (void *data
, int status
);
134 struct delayed_work link_work
;
135 struct work_struct link_cleanup
;
137 struct dentry
*debugfs_dir
;
138 struct dentry
*debugfs_stats
;
157 struct ntb_transport_mw
{
163 struct ntb_transport_client_dev
{
164 struct list_head entry
;
168 struct ntb_transport
{
169 struct list_head entry
;
170 struct list_head client_devs
;
172 struct ntb_device
*ndev
;
173 struct ntb_transport_mw
*mw
;
174 struct ntb_transport_qp
*qps
;
175 unsigned int max_qps
;
176 unsigned long qp_bitmap
;
178 struct delayed_work link_work
;
179 struct work_struct link_cleanup
;
183 DESC_DONE_FLAG
= 1 << 0,
184 LINK_DOWN_FLAG
= 1 << 1,
187 struct ntb_payload_header
{
205 #define QP_TO_MW(ndev, qp) ((qp) % ntb_max_mw(ndev))
206 #define NTB_QP_DEF_NUM_ENTRIES 100
207 #define NTB_LINK_DOWN_TIMEOUT 10
209 static int ntb_match_bus(struct device
*dev
, struct device_driver
*drv
)
211 return !strncmp(dev_name(dev
), drv
->name
, strlen(drv
->name
));
214 static int ntb_client_probe(struct device
*dev
)
216 const struct ntb_client
*drv
= container_of(dev
->driver
,
217 struct ntb_client
, driver
);
218 struct pci_dev
*pdev
= container_of(dev
->parent
, struct pci_dev
, dev
);
222 if (drv
&& drv
->probe
)
223 rc
= drv
->probe(pdev
);
230 static int ntb_client_remove(struct device
*dev
)
232 const struct ntb_client
*drv
= container_of(dev
->driver
,
233 struct ntb_client
, driver
);
234 struct pci_dev
*pdev
= container_of(dev
->parent
, struct pci_dev
, dev
);
236 if (drv
&& drv
->remove
)
244 static struct bus_type ntb_bus_type
= {
246 .match
= ntb_match_bus
,
247 .probe
= ntb_client_probe
,
248 .remove
= ntb_client_remove
,
251 static LIST_HEAD(ntb_transport_list
);
253 static int ntb_bus_init(struct ntb_transport
*nt
)
255 if (list_empty(&ntb_transport_list
)) {
256 int rc
= bus_register(&ntb_bus_type
);
261 list_add(&nt
->entry
, &ntb_transport_list
);
266 static void ntb_bus_remove(struct ntb_transport
*nt
)
268 struct ntb_transport_client_dev
*client_dev
, *cd
;
270 list_for_each_entry_safe(client_dev
, cd
, &nt
->client_devs
, entry
) {
271 dev_err(client_dev
->dev
.parent
, "%s still attached to bus, removing\n",
272 dev_name(&client_dev
->dev
));
273 list_del(&client_dev
->entry
);
274 device_unregister(&client_dev
->dev
);
277 list_del(&nt
->entry
);
279 if (list_empty(&ntb_transport_list
))
280 bus_unregister(&ntb_bus_type
);
283 static void ntb_client_release(struct device
*dev
)
285 struct ntb_transport_client_dev
*client_dev
;
286 client_dev
= container_of(dev
, struct ntb_transport_client_dev
, dev
);
292 * ntb_unregister_client_dev - Unregister NTB client device
293 * @device_name: Name of NTB client device
295 * Unregister an NTB client device with the NTB transport layer
297 void ntb_unregister_client_dev(char *device_name
)
299 struct ntb_transport_client_dev
*client
, *cd
;
300 struct ntb_transport
*nt
;
302 list_for_each_entry(nt
, &ntb_transport_list
, entry
)
303 list_for_each_entry_safe(client
, cd
, &nt
->client_devs
, entry
)
304 if (!strncmp(dev_name(&client
->dev
), device_name
,
305 strlen(device_name
))) {
306 list_del(&client
->entry
);
307 device_unregister(&client
->dev
);
310 EXPORT_SYMBOL_GPL(ntb_unregister_client_dev
);
313 * ntb_register_client_dev - Register NTB client device
314 * @device_name: Name of NTB client device
316 * Register an NTB client device with the NTB transport layer
318 int ntb_register_client_dev(char *device_name
)
320 struct ntb_transport_client_dev
*client_dev
;
321 struct ntb_transport
*nt
;
324 if (list_empty(&ntb_transport_list
))
327 list_for_each_entry(nt
, &ntb_transport_list
, entry
) {
330 client_dev
= kzalloc(sizeof(struct ntb_transport_client_dev
),
337 dev
= &client_dev
->dev
;
339 /* setup and register client devices */
340 dev_set_name(dev
, "%s%d", device_name
, i
);
341 dev
->bus
= &ntb_bus_type
;
342 dev
->release
= ntb_client_release
;
343 dev
->parent
= &ntb_query_pdev(nt
->ndev
)->dev
;
345 rc
= device_register(dev
);
351 list_add_tail(&client_dev
->entry
, &nt
->client_devs
);
358 ntb_unregister_client_dev(device_name
);
362 EXPORT_SYMBOL_GPL(ntb_register_client_dev
);
365 * ntb_register_client - Register NTB client driver
366 * @drv: NTB client driver to be registered
368 * Register an NTB client driver with the NTB transport layer
370 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
372 int ntb_register_client(struct ntb_client
*drv
)
374 drv
->driver
.bus
= &ntb_bus_type
;
376 if (list_empty(&ntb_transport_list
))
379 return driver_register(&drv
->driver
);
381 EXPORT_SYMBOL_GPL(ntb_register_client
);
384 * ntb_unregister_client - Unregister NTB client driver
385 * @drv: NTB client driver to be unregistered
387 * Unregister an NTB client driver with the NTB transport layer
389 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
391 void ntb_unregister_client(struct ntb_client
*drv
)
393 driver_unregister(&drv
->driver
);
395 EXPORT_SYMBOL_GPL(ntb_unregister_client
);
397 static ssize_t
debugfs_read(struct file
*filp
, char __user
*ubuf
, size_t count
,
400 struct ntb_transport_qp
*qp
;
402 ssize_t ret
, out_offset
, out_count
;
406 buf
= kmalloc(out_count
, GFP_KERNEL
);
410 qp
= filp
->private_data
;
412 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
414 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
415 "rx_bytes - \t%llu\n", qp
->rx_bytes
);
416 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
417 "rx_pkts - \t%llu\n", qp
->rx_pkts
);
418 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
419 "rx_memcpy - \t%llu\n", qp
->rx_memcpy
);
420 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
421 "rx_async - \t%llu\n", qp
->rx_async
);
422 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
423 "rx_ring_empty - %llu\n", qp
->rx_ring_empty
);
424 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
425 "rx_err_no_buf - %llu\n", qp
->rx_err_no_buf
);
426 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
427 "rx_err_oflow - \t%llu\n", qp
->rx_err_oflow
);
428 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
429 "rx_err_ver - \t%llu\n", qp
->rx_err_ver
);
430 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
431 "rx_buff - \t%p\n", qp
->rx_buff
);
432 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
433 "rx_index - \t%u\n", qp
->rx_index
);
434 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
435 "rx_max_entry - \t%u\n", qp
->rx_max_entry
);
437 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
438 "tx_bytes - \t%llu\n", qp
->tx_bytes
);
439 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
440 "tx_pkts - \t%llu\n", qp
->tx_pkts
);
441 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
442 "tx_memcpy - \t%llu\n", qp
->tx_memcpy
);
443 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
444 "tx_async - \t%llu\n", qp
->tx_async
);
445 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
446 "tx_ring_full - \t%llu\n", qp
->tx_ring_full
);
447 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
448 "tx_err_no_buf - %llu\n", qp
->tx_err_no_buf
);
449 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
450 "tx_mw - \t%p\n", qp
->tx_mw
);
451 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
452 "tx_index - \t%u\n", qp
->tx_index
);
453 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
454 "tx_max_entry - \t%u\n", qp
->tx_max_entry
);
456 out_offset
+= snprintf(buf
+ out_offset
, out_count
- out_offset
,
457 "\nQP Link %s\n", (qp
->qp_link
== NTB_LINK_UP
) ?
459 if (out_offset
> out_count
)
460 out_offset
= out_count
;
462 ret
= simple_read_from_buffer(ubuf
, count
, offp
, buf
, out_offset
);
467 static const struct file_operations ntb_qp_debugfs_stats
= {
468 .owner
= THIS_MODULE
,
470 .read
= debugfs_read
,
473 static void ntb_list_add(spinlock_t
*lock
, struct list_head
*entry
,
474 struct list_head
*list
)
478 spin_lock_irqsave(lock
, flags
);
479 list_add_tail(entry
, list
);
480 spin_unlock_irqrestore(lock
, flags
);
483 static struct ntb_queue_entry
*ntb_list_rm(spinlock_t
*lock
,
484 struct list_head
*list
)
486 struct ntb_queue_entry
*entry
;
489 spin_lock_irqsave(lock
, flags
);
490 if (list_empty(list
)) {
494 entry
= list_first_entry(list
, struct ntb_queue_entry
, entry
);
495 list_del(&entry
->entry
);
497 spin_unlock_irqrestore(lock
, flags
);
502 static void ntb_transport_setup_qp_mw(struct ntb_transport
*nt
,
505 struct ntb_transport_qp
*qp
= &nt
->qps
[qp_num
];
506 unsigned int rx_size
, num_qps_mw
;
510 mw_max
= ntb_max_mw(nt
->ndev
);
511 mw_num
= QP_TO_MW(nt
->ndev
, qp_num
);
513 WARN_ON(nt
->mw
[mw_num
].virt_addr
== NULL
);
515 if (nt
->max_qps
% mw_max
&& mw_num
< nt
->max_qps
% mw_max
)
516 num_qps_mw
= nt
->max_qps
/ mw_max
+ 1;
518 num_qps_mw
= nt
->max_qps
/ mw_max
;
520 rx_size
= (unsigned int) nt
->mw
[mw_num
].size
/ num_qps_mw
;
521 qp
->rx_buff
= nt
->mw
[mw_num
].virt_addr
+ qp_num
/ mw_max
* rx_size
;
522 rx_size
-= sizeof(struct ntb_rx_info
);
524 qp
->remote_rx_info
= qp
->rx_buff
+ rx_size
;
526 /* Due to housekeeping, there must be atleast 2 buffs */
527 qp
->rx_max_frame
= min(transport_mtu
, rx_size
/ 2);
528 qp
->rx_max_entry
= rx_size
/ qp
->rx_max_frame
;
531 qp
->remote_rx_info
->entry
= qp
->rx_max_entry
- 1;
533 /* setup the hdr offsets with 0's */
534 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
535 void *offset
= qp
->rx_buff
+ qp
->rx_max_frame
* (i
+ 1) -
536 sizeof(struct ntb_payload_header
);
537 memset(offset
, 0, sizeof(struct ntb_payload_header
));
545 static void ntb_free_mw(struct ntb_transport
*nt
, int num_mw
)
547 struct ntb_transport_mw
*mw
= &nt
->mw
[num_mw
];
548 struct pci_dev
*pdev
= ntb_query_pdev(nt
->ndev
);
553 dma_free_coherent(&pdev
->dev
, mw
->size
, mw
->virt_addr
, mw
->dma_addr
);
554 mw
->virt_addr
= NULL
;
557 static int ntb_set_mw(struct ntb_transport
*nt
, int num_mw
, unsigned int size
)
559 struct ntb_transport_mw
*mw
= &nt
->mw
[num_mw
];
560 struct pci_dev
*pdev
= ntb_query_pdev(nt
->ndev
);
562 /* No need to re-setup */
563 if (mw
->size
== ALIGN(size
, 4096))
567 ntb_free_mw(nt
, num_mw
);
569 /* Alloc memory for receiving data. Must be 4k aligned */
570 mw
->size
= ALIGN(size
, 4096);
572 mw
->virt_addr
= dma_alloc_coherent(&pdev
->dev
, mw
->size
, &mw
->dma_addr
,
574 if (!mw
->virt_addr
) {
576 dev_err(&pdev
->dev
, "Unable to allocate MW buffer of size %d\n",
581 /* Notify HW the memory location of the receive buffer */
582 ntb_set_mw_addr(nt
->ndev
, num_mw
, mw
->dma_addr
);
587 static void ntb_qp_link_cleanup(struct work_struct
*work
)
589 struct ntb_transport_qp
*qp
= container_of(work
,
590 struct ntb_transport_qp
,
592 struct ntb_transport
*nt
= qp
->transport
;
593 struct pci_dev
*pdev
= ntb_query_pdev(nt
->ndev
);
595 if (qp
->qp_link
== NTB_LINK_DOWN
) {
596 cancel_delayed_work_sync(&qp
->link_work
);
600 if (qp
->event_handler
)
601 qp
->event_handler(qp
->cb_data
, NTB_LINK_DOWN
);
603 dev_info(&pdev
->dev
, "qp %d: Link Down\n", qp
->qp_num
);
604 qp
->qp_link
= NTB_LINK_DOWN
;
606 if (nt
->transport_link
== NTB_LINK_UP
)
607 schedule_delayed_work(&qp
->link_work
,
608 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
611 static void ntb_qp_link_down(struct ntb_transport_qp
*qp
)
613 schedule_work(&qp
->link_cleanup
);
616 static void ntb_transport_link_cleanup(struct work_struct
*work
)
618 struct ntb_transport
*nt
= container_of(work
, struct ntb_transport
,
622 if (nt
->transport_link
== NTB_LINK_DOWN
)
623 cancel_delayed_work_sync(&nt
->link_work
);
625 nt
->transport_link
= NTB_LINK_DOWN
;
627 /* Pass along the info to any clients */
628 for (i
= 0; i
< nt
->max_qps
; i
++)
629 if (!test_bit(i
, &nt
->qp_bitmap
))
630 ntb_qp_link_down(&nt
->qps
[i
]);
632 /* The scratchpad registers keep the values if the remote side
633 * goes down, blast them now to give them a sane value the next
634 * time they are accessed
636 for (i
= 0; i
< MAX_SPAD
; i
++)
637 ntb_write_local_spad(nt
->ndev
, i
, 0);
640 static void ntb_transport_event_callback(void *data
, enum ntb_hw_event event
)
642 struct ntb_transport
*nt
= data
;
645 case NTB_EVENT_HW_LINK_UP
:
646 schedule_delayed_work(&nt
->link_work
, 0);
648 case NTB_EVENT_HW_LINK_DOWN
:
649 schedule_work(&nt
->link_cleanup
);
656 static void ntb_transport_link_work(struct work_struct
*work
)
658 struct ntb_transport
*nt
= container_of(work
, struct ntb_transport
,
660 struct ntb_device
*ndev
= nt
->ndev
;
661 struct pci_dev
*pdev
= ntb_query_pdev(ndev
);
665 /* send the local info, in the opposite order of the way we read it */
666 for (i
= 0; i
< ntb_max_mw(ndev
); i
++) {
667 rc
= ntb_write_remote_spad(ndev
, MW0_SZ_HIGH
+ (i
* 2),
668 ntb_get_mw_size(ndev
, i
) >> 32);
670 dev_err(&pdev
->dev
, "Error writing %u to remote spad %d\n",
671 (u32
)(ntb_get_mw_size(ndev
, i
) >> 32),
672 MW0_SZ_HIGH
+ (i
* 2));
676 rc
= ntb_write_remote_spad(ndev
, MW0_SZ_LOW
+ (i
* 2),
677 (u32
) ntb_get_mw_size(ndev
, i
));
679 dev_err(&pdev
->dev
, "Error writing %u to remote spad %d\n",
680 (u32
) ntb_get_mw_size(ndev
, i
),
681 MW0_SZ_LOW
+ (i
* 2));
686 rc
= ntb_write_remote_spad(ndev
, NUM_MWS
, ntb_max_mw(ndev
));
688 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
689 ntb_max_mw(ndev
), NUM_MWS
);
693 rc
= ntb_write_remote_spad(ndev
, NUM_QPS
, nt
->max_qps
);
695 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
696 nt
->max_qps
, NUM_QPS
);
700 rc
= ntb_write_remote_spad(ndev
, VERSION
, NTB_TRANSPORT_VERSION
);
702 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
703 NTB_TRANSPORT_VERSION
, VERSION
);
707 /* Query the remote side for its info */
708 rc
= ntb_read_remote_spad(ndev
, VERSION
, &val
);
710 dev_err(&pdev
->dev
, "Error reading remote spad %d\n", VERSION
);
714 if (val
!= NTB_TRANSPORT_VERSION
)
716 dev_dbg(&pdev
->dev
, "Remote version = %d\n", val
);
718 rc
= ntb_read_remote_spad(ndev
, NUM_QPS
, &val
);
720 dev_err(&pdev
->dev
, "Error reading remote spad %d\n", NUM_QPS
);
724 if (val
!= nt
->max_qps
)
726 dev_dbg(&pdev
->dev
, "Remote max number of qps = %d\n", val
);
728 rc
= ntb_read_remote_spad(ndev
, NUM_MWS
, &val
);
730 dev_err(&pdev
->dev
, "Error reading remote spad %d\n", NUM_MWS
);
734 if (val
!= ntb_max_mw(ndev
))
736 dev_dbg(&pdev
->dev
, "Remote number of mws = %d\n", val
);
738 for (i
= 0; i
< ntb_max_mw(ndev
); i
++) {
741 rc
= ntb_read_remote_spad(ndev
, MW0_SZ_HIGH
+ (i
* 2), &val
);
743 dev_err(&pdev
->dev
, "Error reading remote spad %d\n",
744 MW0_SZ_HIGH
+ (i
* 2));
748 val64
= (u64
) val
<< 32;
750 rc
= ntb_read_remote_spad(ndev
, MW0_SZ_LOW
+ (i
* 2), &val
);
752 dev_err(&pdev
->dev
, "Error reading remote spad %d\n",
753 MW0_SZ_LOW
+ (i
* 2));
759 dev_dbg(&pdev
->dev
, "Remote MW%d size = %llu\n", i
, val64
);
761 rc
= ntb_set_mw(nt
, i
, val64
);
766 nt
->transport_link
= NTB_LINK_UP
;
768 for (i
= 0; i
< nt
->max_qps
; i
++) {
769 struct ntb_transport_qp
*qp
= &nt
->qps
[i
];
771 ntb_transport_setup_qp_mw(nt
, i
);
773 if (qp
->client_ready
== NTB_LINK_UP
)
774 schedule_delayed_work(&qp
->link_work
, 0);
780 for (i
= 0; i
< ntb_max_mw(ndev
); i
++)
783 if (ntb_hw_link_status(ndev
))
784 schedule_delayed_work(&nt
->link_work
,
785 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
788 static void ntb_qp_link_work(struct work_struct
*work
)
790 struct ntb_transport_qp
*qp
= container_of(work
,
791 struct ntb_transport_qp
,
793 struct pci_dev
*pdev
= ntb_query_pdev(qp
->ndev
);
794 struct ntb_transport
*nt
= qp
->transport
;
797 WARN_ON(nt
->transport_link
!= NTB_LINK_UP
);
799 rc
= ntb_read_local_spad(nt
->ndev
, QP_LINKS
, &val
);
801 dev_err(&pdev
->dev
, "Error reading spad %d\n", QP_LINKS
);
805 rc
= ntb_write_remote_spad(nt
->ndev
, QP_LINKS
, val
| 1 << qp
->qp_num
);
807 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
808 val
| 1 << qp
->qp_num
, QP_LINKS
);
810 /* query remote spad for qp ready bits */
811 rc
= ntb_read_remote_spad(nt
->ndev
, QP_LINKS
, &val
);
813 dev_err(&pdev
->dev
, "Error reading remote spad %d\n", QP_LINKS
);
815 dev_dbg(&pdev
->dev
, "Remote QP link status = %x\n", val
);
817 /* See if the remote side is up */
818 if (1 << qp
->qp_num
& val
) {
819 qp
->qp_link
= NTB_LINK_UP
;
821 dev_info(&pdev
->dev
, "qp %d: Link Up\n", qp
->qp_num
);
822 if (qp
->event_handler
)
823 qp
->event_handler(qp
->cb_data
, NTB_LINK_UP
);
824 } else if (nt
->transport_link
== NTB_LINK_UP
)
825 schedule_delayed_work(&qp
->link_work
,
826 msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT
));
829 static int ntb_transport_init_queue(struct ntb_transport
*nt
,
832 struct ntb_transport_qp
*qp
;
833 unsigned int num_qps_mw
, tx_size
;
837 mw_max
= ntb_max_mw(nt
->ndev
);
838 mw_num
= QP_TO_MW(nt
->ndev
, qp_num
);
840 qp
= &nt
->qps
[qp_num
];
844 qp
->qp_link
= NTB_LINK_DOWN
;
845 qp
->client_ready
= NTB_LINK_DOWN
;
846 qp
->event_handler
= NULL
;
848 if (nt
->max_qps
% mw_max
&& mw_num
< nt
->max_qps
% mw_max
)
849 num_qps_mw
= nt
->max_qps
/ mw_max
+ 1;
851 num_qps_mw
= nt
->max_qps
/ mw_max
;
853 tx_size
= (unsigned int) ntb_get_mw_size(qp
->ndev
, mw_num
) / num_qps_mw
;
854 qp_offset
= qp_num
/ mw_max
* tx_size
;
855 qp
->tx_mw
= ntb_get_mw_vbase(nt
->ndev
, mw_num
) + qp_offset
;
859 qp
->tx_mw_phys
= ntb_get_mw_base(qp
->ndev
, mw_num
) + qp_offset
;
863 tx_size
-= sizeof(struct ntb_rx_info
);
864 qp
->rx_info
= qp
->tx_mw
+ tx_size
;
866 /* Due to housekeeping, there must be atleast 2 buffs */
867 qp
->tx_max_frame
= min(transport_mtu
, tx_size
/ 2);
868 qp
->tx_max_entry
= tx_size
/ qp
->tx_max_frame
;
870 if (ntb_query_debugfs(nt
->ndev
)) {
871 char debugfs_name
[4];
873 snprintf(debugfs_name
, 4, "qp%d", qp_num
);
874 qp
->debugfs_dir
= debugfs_create_dir(debugfs_name
,
875 ntb_query_debugfs(nt
->ndev
));
877 qp
->debugfs_stats
= debugfs_create_file("stats", S_IRUSR
,
879 &ntb_qp_debugfs_stats
);
882 INIT_DELAYED_WORK(&qp
->link_work
, ntb_qp_link_work
);
883 INIT_WORK(&qp
->link_cleanup
, ntb_qp_link_cleanup
);
885 spin_lock_init(&qp
->ntb_rx_pend_q_lock
);
886 spin_lock_init(&qp
->ntb_rx_free_q_lock
);
887 spin_lock_init(&qp
->ntb_tx_free_q_lock
);
889 INIT_LIST_HEAD(&qp
->rx_pend_q
);
890 INIT_LIST_HEAD(&qp
->rx_free_q
);
891 INIT_LIST_HEAD(&qp
->tx_free_q
);
896 int ntb_transport_init(struct pci_dev
*pdev
)
898 struct ntb_transport
*nt
;
901 nt
= kzalloc(sizeof(struct ntb_transport
), GFP_KERNEL
);
905 nt
->ndev
= ntb_register_transport(pdev
, nt
);
911 nt
->mw
= kcalloc(ntb_max_mw(nt
->ndev
), sizeof(struct ntb_transport_mw
),
919 nt
->max_qps
= min(ntb_max_cbs(nt
->ndev
), max_num_clients
);
921 nt
->max_qps
= min(ntb_max_cbs(nt
->ndev
), ntb_max_mw(nt
->ndev
));
923 nt
->qps
= kcalloc(nt
->max_qps
, sizeof(struct ntb_transport_qp
),
930 nt
->qp_bitmap
= ((u64
) 1 << nt
->max_qps
) - 1;
932 for (i
= 0; i
< nt
->max_qps
; i
++) {
933 rc
= ntb_transport_init_queue(nt
, i
);
938 INIT_DELAYED_WORK(&nt
->link_work
, ntb_transport_link_work
);
939 INIT_WORK(&nt
->link_cleanup
, ntb_transport_link_cleanup
);
941 rc
= ntb_register_event_callback(nt
->ndev
,
942 ntb_transport_event_callback
);
946 INIT_LIST_HEAD(&nt
->client_devs
);
947 rc
= ntb_bus_init(nt
);
951 if (ntb_hw_link_status(nt
->ndev
))
952 schedule_delayed_work(&nt
->link_work
, 0);
957 ntb_unregister_event_callback(nt
->ndev
);
963 ntb_unregister_transport(nt
->ndev
);
969 void ntb_transport_free(void *transport
)
971 struct ntb_transport
*nt
= transport
;
972 struct ntb_device
*ndev
= nt
->ndev
;
975 nt
->transport_link
= NTB_LINK_DOWN
;
977 /* verify that all the qp's are freed */
978 for (i
= 0; i
< nt
->max_qps
; i
++) {
979 if (!test_bit(i
, &nt
->qp_bitmap
))
980 ntb_transport_free_queue(&nt
->qps
[i
]);
981 debugfs_remove_recursive(nt
->qps
[i
].debugfs_dir
);
986 cancel_delayed_work_sync(&nt
->link_work
);
988 ntb_unregister_event_callback(ndev
);
990 for (i
= 0; i
< ntb_max_mw(ndev
); i
++)
995 ntb_unregister_transport(ndev
);
999 static void ntb_rx_copy_callback(void *data
)
1001 struct ntb_queue_entry
*entry
= data
;
1002 struct ntb_transport_qp
*qp
= entry
->qp
;
1003 void *cb_data
= entry
->cb_data
;
1004 unsigned int len
= entry
->len
;
1005 struct ntb_payload_header
*hdr
= entry
->rx_hdr
;
1007 /* Ensure that the data is fully copied out before clearing the flag */
1011 iowrite32(entry
->index
, &qp
->rx_info
->entry
);
1013 ntb_list_add(&qp
->ntb_rx_free_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
1015 if (qp
->rx_handler
&& qp
->client_ready
== NTB_LINK_UP
)
1016 qp
->rx_handler(qp
, qp
->cb_data
, cb_data
, len
);
1019 static void ntb_memcpy_rx(struct ntb_queue_entry
*entry
, void *offset
)
1021 void *buf
= entry
->buf
;
1022 size_t len
= entry
->len
;
1024 memcpy(buf
, offset
, len
);
1026 ntb_rx_copy_callback(entry
);
1029 static void ntb_async_rx(struct ntb_queue_entry
*entry
, void *offset
,
1032 struct dma_async_tx_descriptor
*txd
;
1033 struct ntb_transport_qp
*qp
= entry
->qp
;
1034 struct dma_chan
*chan
= qp
->dma_chan
;
1035 struct dma_device
*device
;
1036 size_t pay_off
, buff_off
;
1037 dma_addr_t src
, dest
;
1038 dma_cookie_t cookie
;
1039 void *buf
= entry
->buf
;
1040 unsigned long flags
;
1047 if (len
< copy_bytes
)
1050 device
= chan
->device
;
1051 pay_off
= (size_t) offset
& ~PAGE_MASK
;
1052 buff_off
= (size_t) buf
& ~PAGE_MASK
;
1054 if (!is_dma_copy_aligned(device
, pay_off
, buff_off
, len
))
1057 dest
= dma_map_single(device
->dev
, buf
, len
, DMA_FROM_DEVICE
);
1058 if (dma_mapping_error(device
->dev
, dest
))
1061 src
= dma_map_single(device
->dev
, offset
, len
, DMA_TO_DEVICE
);
1062 if (dma_mapping_error(device
->dev
, src
))
1065 flags
= DMA_COMPL_DEST_UNMAP_SINGLE
| DMA_COMPL_SRC_UNMAP_SINGLE
|
1067 txd
= device
->device_prep_dma_memcpy(chan
, dest
, src
, len
, flags
);
1071 txd
->callback
= ntb_rx_copy_callback
;
1072 txd
->callback_param
= entry
;
1074 cookie
= dmaengine_submit(txd
);
1075 if (dma_submit_error(cookie
))
1078 qp
->last_cookie
= cookie
;
1085 dma_unmap_single(device
->dev
, src
, len
, DMA_TO_DEVICE
);
1087 dma_unmap_single(device
->dev
, dest
, len
, DMA_FROM_DEVICE
);
1089 /* If the callbacks come out of order, the writing of the index to the
1090 * last completed will be out of order. This may result in the
1091 * receive stalling forever.
1093 dma_sync_wait(chan
, qp
->last_cookie
);
1095 ntb_memcpy_rx(entry
, offset
);
1099 static int ntb_process_rxc(struct ntb_transport_qp
*qp
)
1101 struct ntb_payload_header
*hdr
;
1102 struct ntb_queue_entry
*entry
;
1105 offset
= qp
->rx_buff
+ qp
->rx_max_frame
* qp
->rx_index
;
1106 hdr
= offset
+ qp
->rx_max_frame
- sizeof(struct ntb_payload_header
);
1108 entry
= ntb_list_rm(&qp
->ntb_rx_pend_q_lock
, &qp
->rx_pend_q
);
1110 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
,
1111 "no buffer - HDR ver %u, len %d, flags %x\n",
1112 hdr
->ver
, hdr
->len
, hdr
->flags
);
1113 qp
->rx_err_no_buf
++;
1117 if (!(hdr
->flags
& DESC_DONE_FLAG
)) {
1118 ntb_list_add(&qp
->ntb_rx_pend_q_lock
, &entry
->entry
,
1120 qp
->rx_ring_empty
++;
1124 if (hdr
->ver
!= (u32
) qp
->rx_pkts
) {
1125 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
,
1126 "qp %d: version mismatch, expected %llu - got %u\n",
1127 qp
->qp_num
, qp
->rx_pkts
, hdr
->ver
);
1128 ntb_list_add(&qp
->ntb_rx_pend_q_lock
, &entry
->entry
,
1134 if (hdr
->flags
& LINK_DOWN_FLAG
) {
1135 ntb_qp_link_down(qp
);
1140 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
,
1141 "rx offset %u, ver %u - %d payload received, buf size %d\n",
1142 qp
->rx_index
, hdr
->ver
, hdr
->len
, entry
->len
);
1144 qp
->rx_bytes
+= hdr
->len
;
1147 if (hdr
->len
> entry
->len
) {
1149 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
,
1150 "RX overflow! Wanted %d got %d\n",
1151 hdr
->len
, entry
->len
);
1156 entry
->index
= qp
->rx_index
;
1157 entry
->rx_hdr
= hdr
;
1159 ntb_async_rx(entry
, offset
, hdr
->len
);
1163 qp
->rx_index
%= qp
->rx_max_entry
;
1168 ntb_list_add(&qp
->ntb_rx_pend_q_lock
, &entry
->entry
,
1170 /* Ensure that the data is fully copied out before clearing the flag */
1173 iowrite32(qp
->rx_index
, &qp
->rx_info
->entry
);
1178 static void ntb_transport_rx(unsigned long data
)
1180 struct ntb_transport_qp
*qp
= (struct ntb_transport_qp
*)data
;
1183 /* Limit the number of packets processed in a single interrupt to
1184 * provide fairness to others
1186 for (i
= 0; i
< qp
->rx_max_entry
; i
++) {
1187 rc
= ntb_process_rxc(qp
);
1193 dma_async_issue_pending(qp
->dma_chan
);
1196 static void ntb_transport_rxc_db(void *data
, int db_num
)
1198 struct ntb_transport_qp
*qp
= data
;
1200 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
, "%s: doorbell %d received\n",
1203 tasklet_schedule(&qp
->rx_work
);
1206 static void ntb_tx_copy_callback(void *data
)
1208 struct ntb_queue_entry
*entry
= data
;
1209 struct ntb_transport_qp
*qp
= entry
->qp
;
1210 struct ntb_payload_header __iomem
*hdr
= entry
->tx_hdr
;
1212 /* Ensure that the data is fully copied out before setting the flags */
1214 iowrite32(entry
->flags
| DESC_DONE_FLAG
, &hdr
->flags
);
1216 ntb_ring_doorbell(qp
->ndev
, qp
->qp_num
);
1218 /* The entry length can only be zero if the packet is intended to be a
1219 * "link down" or similar. Since no payload is being sent in these
1220 * cases, there is nothing to add to the completion queue.
1222 if (entry
->len
> 0) {
1223 qp
->tx_bytes
+= entry
->len
;
1226 qp
->tx_handler(qp
, qp
->cb_data
, entry
->cb_data
,
1230 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
, &qp
->tx_free_q
);
1233 static void ntb_memcpy_tx(struct ntb_queue_entry
*entry
, void __iomem
*offset
)
1235 memcpy_toio(offset
, entry
->buf
, entry
->len
);
1237 ntb_tx_copy_callback(entry
);
1240 static void ntb_async_tx(struct ntb_transport_qp
*qp
,
1241 struct ntb_queue_entry
*entry
)
1243 struct ntb_payload_header __iomem
*hdr
;
1244 struct dma_async_tx_descriptor
*txd
;
1245 struct dma_chan
*chan
= qp
->dma_chan
;
1246 struct dma_device
*device
;
1247 size_t dest_off
, buff_off
;
1248 dma_addr_t src
, dest
;
1249 dma_cookie_t cookie
;
1250 void __iomem
*offset
;
1251 size_t len
= entry
->len
;
1252 void *buf
= entry
->buf
;
1253 unsigned long flags
;
1255 offset
= qp
->tx_mw
+ qp
->tx_max_frame
* qp
->tx_index
;
1256 hdr
= offset
+ qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1257 entry
->tx_hdr
= hdr
;
1259 iowrite32(entry
->len
, &hdr
->len
);
1260 iowrite32((u32
) qp
->tx_pkts
, &hdr
->ver
);
1265 if (len
< copy_bytes
)
1268 device
= chan
->device
;
1269 dest
= qp
->tx_mw_phys
+ qp
->tx_max_frame
* qp
->tx_index
;
1270 buff_off
= (size_t) buf
& ~PAGE_MASK
;
1271 dest_off
= (size_t) dest
& ~PAGE_MASK
;
1273 if (!is_dma_copy_aligned(device
, buff_off
, dest_off
, len
))
1276 src
= dma_map_single(device
->dev
, buf
, len
, DMA_TO_DEVICE
);
1277 if (dma_mapping_error(device
->dev
, src
))
1280 flags
= DMA_COMPL_SRC_UNMAP_SINGLE
| DMA_PREP_INTERRUPT
;
1281 txd
= device
->device_prep_dma_memcpy(chan
, dest
, src
, len
, flags
);
1285 txd
->callback
= ntb_tx_copy_callback
;
1286 txd
->callback_param
= entry
;
1288 cookie
= dmaengine_submit(txd
);
1289 if (dma_submit_error(cookie
))
1292 dma_async_issue_pending(chan
);
1297 dma_unmap_single(device
->dev
, src
, len
, DMA_TO_DEVICE
);
1299 ntb_memcpy_tx(entry
, offset
);
1303 static int ntb_process_tx(struct ntb_transport_qp
*qp
,
1304 struct ntb_queue_entry
*entry
)
1306 dev_dbg(&ntb_query_pdev(qp
->ndev
)->dev
, "%lld - tx %u, entry len %d flags %x buff %p\n",
1307 qp
->tx_pkts
, qp
->tx_index
, entry
->len
, entry
->flags
,
1309 if (qp
->tx_index
== qp
->remote_rx_info
->entry
) {
1314 if (entry
->len
> qp
->tx_max_frame
- sizeof(struct ntb_payload_header
)) {
1316 qp
->tx_handler(qp
->cb_data
, qp
, NULL
, -EIO
);
1318 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1323 ntb_async_tx(qp
, entry
);
1326 qp
->tx_index
%= qp
->tx_max_entry
;
1333 static void ntb_send_link_down(struct ntb_transport_qp
*qp
)
1335 struct pci_dev
*pdev
= ntb_query_pdev(qp
->ndev
);
1336 struct ntb_queue_entry
*entry
;
1339 if (qp
->qp_link
== NTB_LINK_DOWN
)
1342 qp
->qp_link
= NTB_LINK_DOWN
;
1343 dev_info(&pdev
->dev
, "qp %d: Link Down\n", qp
->qp_num
);
1345 for (i
= 0; i
< NTB_LINK_DOWN_TIMEOUT
; i
++) {
1346 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1355 entry
->cb_data
= NULL
;
1358 entry
->flags
= LINK_DOWN_FLAG
;
1360 rc
= ntb_process_tx(qp
, entry
);
1362 dev_err(&pdev
->dev
, "ntb: QP%d unable to send linkdown msg\n",
1367 * ntb_transport_create_queue - Create a new NTB transport layer queue
1368 * @rx_handler: receive callback function
1369 * @tx_handler: transmit callback function
1370 * @event_handler: event callback function
1372 * Create a new NTB transport layer queue and provide the queue with a callback
1373 * routine for both transmit and receive. The receive callback routine will be
1374 * used to pass up data when the transport has received it on the queue. The
1375 * transmit callback routine will be called when the transport has completed the
1376 * transmission of the data on the queue and the data is ready to be freed.
1378 * RETURNS: pointer to newly created ntb_queue, NULL on error.
1380 struct ntb_transport_qp
*
1381 ntb_transport_create_queue(void *data
, struct pci_dev
*pdev
,
1382 const struct ntb_queue_handlers
*handlers
)
1384 struct ntb_queue_entry
*entry
;
1385 struct ntb_transport_qp
*qp
;
1386 struct ntb_transport
*nt
;
1387 unsigned int free_queue
;
1390 nt
= ntb_find_transport(pdev
);
1394 free_queue
= ffs(nt
->qp_bitmap
);
1398 /* decrement free_queue to make it zero based */
1401 clear_bit(free_queue
, &nt
->qp_bitmap
);
1403 qp
= &nt
->qps
[free_queue
];
1405 qp
->rx_handler
= handlers
->rx_handler
;
1406 qp
->tx_handler
= handlers
->tx_handler
;
1407 qp
->event_handler
= handlers
->event_handler
;
1409 qp
->dma_chan
= dma_find_channel(DMA_MEMCPY
);
1411 dev_info(&pdev
->dev
, "Unable to allocate DMA channel, using CPU instead\n");
1415 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1416 entry
= kzalloc(sizeof(struct ntb_queue_entry
), GFP_ATOMIC
);
1421 ntb_list_add(&qp
->ntb_rx_free_q_lock
, &entry
->entry
,
1425 for (i
= 0; i
< NTB_QP_DEF_NUM_ENTRIES
; i
++) {
1426 entry
= kzalloc(sizeof(struct ntb_queue_entry
), GFP_ATOMIC
);
1431 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1435 tasklet_init(&qp
->rx_work
, ntb_transport_rx
, (unsigned long) qp
);
1437 rc
= ntb_register_db_callback(qp
->ndev
, free_queue
, qp
,
1438 ntb_transport_rxc_db
);
1442 dev_info(&pdev
->dev
, "NTB Transport QP %d created\n", qp
->qp_num
);
1447 tasklet_disable(&qp
->rx_work
);
1449 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1452 while ((entry
= ntb_list_rm(&qp
->ntb_rx_free_q_lock
, &qp
->rx_free_q
)))
1454 set_bit(free_queue
, &nt
->qp_bitmap
);
1458 EXPORT_SYMBOL_GPL(ntb_transport_create_queue
);
1461 * ntb_transport_free_queue - Frees NTB transport queue
1462 * @qp: NTB queue to be freed
1464 * Frees NTB transport queue
1466 void ntb_transport_free_queue(struct ntb_transport_qp
*qp
)
1468 struct pci_dev
*pdev
;
1469 struct ntb_queue_entry
*entry
;
1474 pdev
= ntb_query_pdev(qp
->ndev
);
1477 struct dma_chan
*chan
= qp
->dma_chan
;
1478 /* Putting the dma_chan to NULL will force any new traffic to be
1479 * processed by the CPU instead of the DAM engine
1481 qp
->dma_chan
= NULL
;
1483 /* Try to be nice and wait for any queued DMA engine
1484 * transactions to process before smashing it with a rock
1486 dma_sync_wait(chan
, qp
->last_cookie
);
1487 dmaengine_terminate_all(chan
);
1491 ntb_unregister_db_callback(qp
->ndev
, qp
->qp_num
);
1492 tasklet_disable(&qp
->rx_work
);
1494 cancel_delayed_work_sync(&qp
->link_work
);
1496 while ((entry
= ntb_list_rm(&qp
->ntb_rx_free_q_lock
, &qp
->rx_free_q
)))
1499 while ((entry
= ntb_list_rm(&qp
->ntb_rx_pend_q_lock
, &qp
->rx_pend_q
))) {
1500 dev_warn(&pdev
->dev
, "Freeing item from a non-empty queue\n");
1504 while ((entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
)))
1507 set_bit(qp
->qp_num
, &qp
->transport
->qp_bitmap
);
1509 dev_info(&pdev
->dev
, "NTB Transport QP %d freed\n", qp
->qp_num
);
1511 EXPORT_SYMBOL_GPL(ntb_transport_free_queue
);
1514 * ntb_transport_rx_remove - Dequeues enqueued rx packet
1515 * @qp: NTB queue to be freed
1516 * @len: pointer to variable to write enqueued buffers length
1518 * Dequeues unused buffers from receive queue. Should only be used during
1521 * RETURNS: NULL error value on error, or void* for success.
1523 void *ntb_transport_rx_remove(struct ntb_transport_qp
*qp
, unsigned int *len
)
1525 struct ntb_queue_entry
*entry
;
1528 if (!qp
|| qp
->client_ready
== NTB_LINK_UP
)
1531 entry
= ntb_list_rm(&qp
->ntb_rx_pend_q_lock
, &qp
->rx_pend_q
);
1535 buf
= entry
->cb_data
;
1538 ntb_list_add(&qp
->ntb_rx_free_q_lock
, &entry
->entry
, &qp
->rx_free_q
);
1542 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove
);
1545 * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1546 * @qp: NTB transport layer queue the entry is to be enqueued on
1547 * @cb: per buffer pointer for callback function to use
1548 * @data: pointer to data buffer that incoming packets will be copied into
1549 * @len: length of the data buffer
1551 * Enqueue a new receive buffer onto the transport queue into which a NTB
1552 * payload can be received into.
1554 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1556 int ntb_transport_rx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1559 struct ntb_queue_entry
*entry
;
1564 entry
= ntb_list_rm(&qp
->ntb_rx_free_q_lock
, &qp
->rx_free_q
);
1568 entry
->cb_data
= cb
;
1572 ntb_list_add(&qp
->ntb_rx_pend_q_lock
, &entry
->entry
, &qp
->rx_pend_q
);
1576 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue
);
1579 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1580 * @qp: NTB transport layer queue the entry is to be enqueued on
1581 * @cb: per buffer pointer for callback function to use
1582 * @data: pointer to data buffer that will be sent
1583 * @len: length of the data buffer
1585 * Enqueue a new transmit buffer onto the transport queue from which a NTB
1586 * payload will be transmitted. This assumes that a lock is being held to
1587 * serialize access to the qp.
1589 * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1591 int ntb_transport_tx_enqueue(struct ntb_transport_qp
*qp
, void *cb
, void *data
,
1594 struct ntb_queue_entry
*entry
;
1597 if (!qp
|| qp
->qp_link
!= NTB_LINK_UP
|| !len
)
1600 entry
= ntb_list_rm(&qp
->ntb_tx_free_q_lock
, &qp
->tx_free_q
);
1602 qp
->tx_err_no_buf
++;
1606 entry
->cb_data
= cb
;
1611 rc
= ntb_process_tx(qp
, entry
);
1613 ntb_list_add(&qp
->ntb_tx_free_q_lock
, &entry
->entry
,
1618 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue
);
1621 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1622 * @qp: NTB transport layer queue to be enabled
1624 * Notify NTB transport layer of client readiness to use queue
1626 void ntb_transport_link_up(struct ntb_transport_qp
*qp
)
1631 qp
->client_ready
= NTB_LINK_UP
;
1633 if (qp
->transport
->transport_link
== NTB_LINK_UP
)
1634 schedule_delayed_work(&qp
->link_work
, 0);
1636 EXPORT_SYMBOL_GPL(ntb_transport_link_up
);
1639 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1640 * @qp: NTB transport layer queue to be disabled
1642 * Notify NTB transport layer of client's desire to no longer receive data on
1643 * transport queue specified. It is the client's responsibility to ensure all
1644 * entries on queue are purged or otherwise handled appropriately.
1646 void ntb_transport_link_down(struct ntb_transport_qp
*qp
)
1648 struct pci_dev
*pdev
;
1654 pdev
= ntb_query_pdev(qp
->ndev
);
1655 qp
->client_ready
= NTB_LINK_DOWN
;
1657 rc
= ntb_read_local_spad(qp
->ndev
, QP_LINKS
, &val
);
1659 dev_err(&pdev
->dev
, "Error reading spad %d\n", QP_LINKS
);
1663 rc
= ntb_write_remote_spad(qp
->ndev
, QP_LINKS
,
1664 val
& ~(1 << qp
->qp_num
));
1666 dev_err(&pdev
->dev
, "Error writing %x to remote spad %d\n",
1667 val
& ~(1 << qp
->qp_num
), QP_LINKS
);
1669 if (qp
->qp_link
== NTB_LINK_UP
)
1670 ntb_send_link_down(qp
);
1672 cancel_delayed_work_sync(&qp
->link_work
);
1674 EXPORT_SYMBOL_GPL(ntb_transport_link_down
);
1677 * ntb_transport_link_query - Query transport link state
1678 * @qp: NTB transport layer queue to be queried
1680 * Query connectivity to the remote system of the NTB transport queue
1682 * RETURNS: true for link up or false for link down
1684 bool ntb_transport_link_query(struct ntb_transport_qp
*qp
)
1689 return qp
->qp_link
== NTB_LINK_UP
;
1691 EXPORT_SYMBOL_GPL(ntb_transport_link_query
);
1694 * ntb_transport_qp_num - Query the qp number
1695 * @qp: NTB transport layer queue to be queried
1697 * Query qp number of the NTB transport queue
1699 * RETURNS: a zero based number specifying the qp number
1701 unsigned char ntb_transport_qp_num(struct ntb_transport_qp
*qp
)
1708 EXPORT_SYMBOL_GPL(ntb_transport_qp_num
);
1711 * ntb_transport_max_size - Query the max payload size of a qp
1712 * @qp: NTB transport layer queue to be queried
1714 * Query the maximum payload size permissible on the given qp
1716 * RETURNS: the max payload size of a qp
1718 unsigned int ntb_transport_max_size(struct ntb_transport_qp
*qp
)
1726 return qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1728 /* If DMA engine usage is possible, try to find the max size for that */
1729 max
= qp
->tx_max_frame
- sizeof(struct ntb_payload_header
);
1730 max
-= max
% (1 << qp
->dma_chan
->device
->copy_align
);
1734 EXPORT_SYMBOL_GPL(ntb_transport_max_size
);