xref: /openbmc/linux/drivers/ntb/ntb_transport.c (revision 5a7693e6bbf19b22fd6c1d2c4b7beb0a03969e2c)
1fce8a7bbSJon Mason /*
2fce8a7bbSJon Mason  * This file is provided under a dual BSD/GPLv2 license.  When using or
3fce8a7bbSJon Mason  *   redistributing this file, you may do so under either license.
4fce8a7bbSJon Mason  *
5fce8a7bbSJon Mason  *   GPL LICENSE SUMMARY
6fce8a7bbSJon Mason  *
7fce8a7bbSJon Mason  *   Copyright(c) 2012 Intel Corporation. All rights reserved.
8e26a5843SAllen Hubbe  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
9fce8a7bbSJon Mason  *
10fce8a7bbSJon Mason  *   This program is free software; you can redistribute it and/or modify
11fce8a7bbSJon Mason  *   it under the terms of version 2 of the GNU General Public License as
12fce8a7bbSJon Mason  *   published by the Free Software Foundation.
13fce8a7bbSJon Mason  *
14fce8a7bbSJon Mason  *   BSD LICENSE
15fce8a7bbSJon Mason  *
16fce8a7bbSJon Mason  *   Copyright(c) 2012 Intel Corporation. All rights reserved.
17e26a5843SAllen Hubbe  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
18fce8a7bbSJon Mason  *
19fce8a7bbSJon Mason  *   Redistribution and use in source and binary forms, with or without
20fce8a7bbSJon Mason  *   modification, are permitted provided that the following conditions
21fce8a7bbSJon Mason  *   are met:
22fce8a7bbSJon Mason  *
23fce8a7bbSJon Mason  *     * Redistributions of source code must retain the above copyright
24fce8a7bbSJon Mason  *       notice, this list of conditions and the following disclaimer.
25fce8a7bbSJon Mason  *     * Redistributions in binary form must reproduce the above copy
26fce8a7bbSJon Mason  *       notice, this list of conditions and the following disclaimer in
27fce8a7bbSJon Mason  *       the documentation and/or other materials provided with the
28fce8a7bbSJon Mason  *       distribution.
29fce8a7bbSJon Mason  *     * Neither the name of Intel Corporation nor the names of its
30fce8a7bbSJon Mason  *       contributors may be used to endorse or promote products derived
31fce8a7bbSJon Mason  *       from this software without specific prior written permission.
32fce8a7bbSJon Mason  *
33fce8a7bbSJon Mason  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34fce8a7bbSJon Mason  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35fce8a7bbSJon Mason  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36fce8a7bbSJon Mason  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37fce8a7bbSJon Mason  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38fce8a7bbSJon Mason  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39fce8a7bbSJon Mason  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40fce8a7bbSJon Mason  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41fce8a7bbSJon Mason  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42fce8a7bbSJon Mason  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43fce8a7bbSJon Mason  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44fce8a7bbSJon Mason  *
45e26a5843SAllen Hubbe  * PCIe NTB Transport Linux driver
46fce8a7bbSJon Mason  *
47fce8a7bbSJon Mason  * Contact Information:
48fce8a7bbSJon Mason  * Jon Mason <jon.mason@intel.com>
49fce8a7bbSJon Mason  */
50fce8a7bbSJon Mason #include <linux/debugfs.h>
51fce8a7bbSJon Mason #include <linux/delay.h>
52282a2feeSJon Mason #include <linux/dmaengine.h>
53fce8a7bbSJon Mason #include <linux/dma-mapping.h>
54fce8a7bbSJon Mason #include <linux/errno.h>
55fce8a7bbSJon Mason #include <linux/export.h>
56fce8a7bbSJon Mason #include <linux/interrupt.h>
57fce8a7bbSJon Mason #include <linux/module.h>
58fce8a7bbSJon Mason #include <linux/pci.h>
59fce8a7bbSJon Mason #include <linux/slab.h>
60fce8a7bbSJon Mason #include <linux/types.h>
6106917f75SDave Jiang #include <linux/uaccess.h>
62e26a5843SAllen Hubbe #include "linux/ntb.h"
63e26a5843SAllen Hubbe #include "linux/ntb_transport.h"
64fce8a7bbSJon Mason 
65e26a5843SAllen Hubbe #define NTB_TRANSPORT_VERSION	4
66e26a5843SAllen Hubbe #define NTB_TRANSPORT_VER	"4"
67e26a5843SAllen Hubbe #define NTB_TRANSPORT_NAME	"ntb_transport"
68e26a5843SAllen Hubbe #define NTB_TRANSPORT_DESC	"Software Queue-Pair Transport over NTB"
69b17faba0SShyam Sundar S K #define NTB_TRANSPORT_MIN_SPADS (MW0_SZ_HIGH + 2)
70e26a5843SAllen Hubbe 
71e26a5843SAllen Hubbe MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
72e26a5843SAllen Hubbe MODULE_VERSION(NTB_TRANSPORT_VER);
73e26a5843SAllen Hubbe MODULE_LICENSE("Dual BSD/GPL");
74e26a5843SAllen Hubbe MODULE_AUTHOR("Intel Corporation");
75e26a5843SAllen Hubbe 
76e26a5843SAllen Hubbe static unsigned long max_mw_size;
77e26a5843SAllen Hubbe module_param(max_mw_size, ulong, 0644);
78e26a5843SAllen Hubbe MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
79fce8a7bbSJon Mason 
809891417dSDave Jiang static unsigned int transport_mtu = 0x10000;
81fce8a7bbSJon Mason module_param(transport_mtu, uint, 0644);
82fce8a7bbSJon Mason MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
83fce8a7bbSJon Mason 
84948d3a65SJon Mason static unsigned char max_num_clients;
85fce8a7bbSJon Mason module_param(max_num_clients, byte, 0644);
86fce8a7bbSJon Mason MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
87fce8a7bbSJon Mason 
88282a2feeSJon Mason static unsigned int copy_bytes = 1024;
89282a2feeSJon Mason module_param(copy_bytes, uint, 0644);
90282a2feeSJon Mason MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
91282a2feeSJon Mason 
92a41ef053SDave Jiang static bool use_dma;
93a41ef053SDave Jiang module_param(use_dma, bool, 0644);
94a41ef053SDave Jiang MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
95a41ef053SDave Jiang 
962b0569b3SLogan Gunthorpe static bool use_msi;
972b0569b3SLogan Gunthorpe #ifdef CONFIG_NTB_MSI
982b0569b3SLogan Gunthorpe module_param(use_msi, bool, 0644);
992b0569b3SLogan Gunthorpe MODULE_PARM_DESC(use_msi, "Use MSI interrupts instead of doorbells");
1002b0569b3SLogan Gunthorpe #endif
1012b0569b3SLogan Gunthorpe 
102e26a5843SAllen Hubbe static struct dentry *nt_debugfs_dir;
103e26a5843SAllen Hubbe 
1041e530119SSerge Semin /* Only two-ports NTB devices are supported */
1051e530119SSerge Semin #define PIDX		NTB_DEF_PEER_IDX
1061e530119SSerge Semin 
107fce8a7bbSJon Mason struct ntb_queue_entry {
108fce8a7bbSJon Mason 	/* ntb_queue list reference */
109fce8a7bbSJon Mason 	struct list_head entry;
110e26a5843SAllen Hubbe 	/* pointers to data to be transferred */
111fce8a7bbSJon Mason 	void *cb_data;
112fce8a7bbSJon Mason 	void *buf;
113fce8a7bbSJon Mason 	unsigned int len;
114fce8a7bbSJon Mason 	unsigned int flags;
1159cabc269SDave Jiang 	int retries;
1169cabc269SDave Jiang 	int errors;
1179cabc269SDave Jiang 	unsigned int tx_index;
11872203572SDave Jiang 	unsigned int rx_index;
119282a2feeSJon Mason 
120282a2feeSJon Mason 	struct ntb_transport_qp *qp;
121282a2feeSJon Mason 	union {
122282a2feeSJon Mason 		struct ntb_payload_header __iomem *tx_hdr;
123282a2feeSJon Mason 		struct ntb_payload_header *rx_hdr;
124282a2feeSJon Mason 	};
125fce8a7bbSJon Mason };
126fce8a7bbSJon Mason 
127793c20e9SJon Mason struct ntb_rx_info {
128793c20e9SJon Mason 	unsigned int entry;
129793c20e9SJon Mason };
130793c20e9SJon Mason 
131fce8a7bbSJon Mason struct ntb_transport_qp {
132e26a5843SAllen Hubbe 	struct ntb_transport_ctx *transport;
133e26a5843SAllen Hubbe 	struct ntb_dev *ndev;
134fce8a7bbSJon Mason 	void *cb_data;
135569410caSDave Jiang 	struct dma_chan *tx_dma_chan;
136569410caSDave Jiang 	struct dma_chan *rx_dma_chan;
137fce8a7bbSJon Mason 
138fce8a7bbSJon Mason 	bool client_ready;
139e26a5843SAllen Hubbe 	bool link_is_up;
140e9021331SDave Jiang 	bool active;
141e26a5843SAllen Hubbe 
142fce8a7bbSJon Mason 	u8 qp_num;	/* Only 64 QP's are allowed.  0-63 */
143e26a5843SAllen Hubbe 	u64 qp_bit;
144fce8a7bbSJon Mason 
14574465645SJon Mason 	struct ntb_rx_info __iomem *rx_info;
146793c20e9SJon Mason 	struct ntb_rx_info *remote_rx_info;
147793c20e9SJon Mason 
148fce8a7bbSJon Mason 	void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
149fce8a7bbSJon Mason 			   void *data, int len);
150fce8a7bbSJon Mason 	struct list_head tx_free_q;
151fce8a7bbSJon Mason 	spinlock_t ntb_tx_free_q_lock;
15274465645SJon Mason 	void __iomem *tx_mw;
153c59666bbSLogan Gunthorpe 	phys_addr_t tx_mw_phys;
154c59666bbSLogan Gunthorpe 	size_t tx_mw_size;
155c59666bbSLogan Gunthorpe 	dma_addr_t tx_mw_dma_addr;
156793c20e9SJon Mason 	unsigned int tx_index;
157793c20e9SJon Mason 	unsigned int tx_max_entry;
158ef114ed5SJon Mason 	unsigned int tx_max_frame;
159fce8a7bbSJon Mason 
160fce8a7bbSJon Mason 	void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
161fce8a7bbSJon Mason 			   void *data, int len);
162da2e5ae5SAllen Hubbe 	struct list_head rx_post_q;
163fce8a7bbSJon Mason 	struct list_head rx_pend_q;
164fce8a7bbSJon Mason 	struct list_head rx_free_q;
165da2e5ae5SAllen Hubbe 	/* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
166da2e5ae5SAllen Hubbe 	spinlock_t ntb_rx_q_lock;
167793c20e9SJon Mason 	void *rx_buff;
168793c20e9SJon Mason 	unsigned int rx_index;
169793c20e9SJon Mason 	unsigned int rx_max_entry;
170ef114ed5SJon Mason 	unsigned int rx_max_frame;
171a754a8fcSDave Jiang 	unsigned int rx_alloc_entry;
172282a2feeSJon Mason 	dma_cookie_t last_cookie;
173e26a5843SAllen Hubbe 	struct tasklet_struct rxc_db_work;
174fce8a7bbSJon Mason 
175fce8a7bbSJon Mason 	void (*event_handler)(void *data, int status);
176fce8a7bbSJon Mason 	struct delayed_work link_work;
1777b4f2d3cSJon Mason 	struct work_struct link_cleanup;
178fce8a7bbSJon Mason 
179fce8a7bbSJon Mason 	struct dentry *debugfs_dir;
180fce8a7bbSJon Mason 	struct dentry *debugfs_stats;
181fce8a7bbSJon Mason 
182fce8a7bbSJon Mason 	/* Stats */
183fce8a7bbSJon Mason 	u64 rx_bytes;
184fce8a7bbSJon Mason 	u64 rx_pkts;
185fce8a7bbSJon Mason 	u64 rx_ring_empty;
186fce8a7bbSJon Mason 	u64 rx_err_no_buf;
187fce8a7bbSJon Mason 	u64 rx_err_oflow;
188fce8a7bbSJon Mason 	u64 rx_err_ver;
189282a2feeSJon Mason 	u64 rx_memcpy;
190282a2feeSJon Mason 	u64 rx_async;
191fce8a7bbSJon Mason 	u64 tx_bytes;
192fce8a7bbSJon Mason 	u64 tx_pkts;
193fce8a7bbSJon Mason 	u64 tx_ring_full;
194282a2feeSJon Mason 	u64 tx_err_no_buf;
195282a2feeSJon Mason 	u64 tx_memcpy;
196282a2feeSJon Mason 	u64 tx_async;
1972b0569b3SLogan Gunthorpe 
1982b0569b3SLogan Gunthorpe 	bool use_msi;
1992b0569b3SLogan Gunthorpe 	int msi_irq;
2002b0569b3SLogan Gunthorpe 	struct ntb_msi_desc msi_desc;
2012b0569b3SLogan Gunthorpe 	struct ntb_msi_desc peer_msi_desc;
202fce8a7bbSJon Mason };
203fce8a7bbSJon Mason 
204fce8a7bbSJon Mason struct ntb_transport_mw {
205e26a5843SAllen Hubbe 	phys_addr_t phys_addr;
206e26a5843SAllen Hubbe 	resource_size_t phys_size;
207e26a5843SAllen Hubbe 	void __iomem *vbase;
208e26a5843SAllen Hubbe 	size_t xlat_size;
209e26a5843SAllen Hubbe 	size_t buff_size;
210fc5d1829SAaron Sierra 	size_t alloc_size;
211fc5d1829SAaron Sierra 	void *alloc_addr;
212fce8a7bbSJon Mason 	void *virt_addr;
213fce8a7bbSJon Mason 	dma_addr_t dma_addr;
214fce8a7bbSJon Mason };
215fce8a7bbSJon Mason 
216fce8a7bbSJon Mason struct ntb_transport_client_dev {
217fce8a7bbSJon Mason 	struct list_head entry;
218e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt;
219fce8a7bbSJon Mason 	struct device dev;
220fce8a7bbSJon Mason };
221fce8a7bbSJon Mason 
222e26a5843SAllen Hubbe struct ntb_transport_ctx {
223fce8a7bbSJon Mason 	struct list_head entry;
224fce8a7bbSJon Mason 	struct list_head client_devs;
225fce8a7bbSJon Mason 
226e26a5843SAllen Hubbe 	struct ntb_dev *ndev;
227e26a5843SAllen Hubbe 
228e26a5843SAllen Hubbe 	struct ntb_transport_mw *mw_vec;
229e26a5843SAllen Hubbe 	struct ntb_transport_qp *qp_vec;
230e26a5843SAllen Hubbe 	unsigned int mw_count;
231e26a5843SAllen Hubbe 	unsigned int qp_count;
232e26a5843SAllen Hubbe 	u64 qp_bitmap;
233e26a5843SAllen Hubbe 	u64 qp_bitmap_free;
234e26a5843SAllen Hubbe 
2352b0569b3SLogan Gunthorpe 	bool use_msi;
2362b0569b3SLogan Gunthorpe 	unsigned int msi_spad_offset;
2372b0569b3SLogan Gunthorpe 	u64 msi_db_mask;
2382b0569b3SLogan Gunthorpe 
239e26a5843SAllen Hubbe 	bool link_is_up;
240fce8a7bbSJon Mason 	struct delayed_work link_work;
2417b4f2d3cSJon Mason 	struct work_struct link_cleanup;
242c8650fd0SDave Jiang 
243c8650fd0SDave Jiang 	struct dentry *debugfs_node_dir;
244fce8a7bbSJon Mason };
245fce8a7bbSJon Mason 
246fce8a7bbSJon Mason enum {
247e26a5843SAllen Hubbe 	DESC_DONE_FLAG = BIT(0),
248e26a5843SAllen Hubbe 	LINK_DOWN_FLAG = BIT(1),
249fce8a7bbSJon Mason };
250fce8a7bbSJon Mason 
251fce8a7bbSJon Mason struct ntb_payload_header {
25274465645SJon Mason 	unsigned int ver;
253fce8a7bbSJon Mason 	unsigned int len;
254fce8a7bbSJon Mason 	unsigned int flags;
255fce8a7bbSJon Mason };
256fce8a7bbSJon Mason 
257fce8a7bbSJon Mason enum {
258fce8a7bbSJon Mason 	VERSION = 0,
259fce8a7bbSJon Mason 	QP_LINKS,
260113fc505SJon Mason 	NUM_QPS,
261113fc505SJon Mason 	NUM_MWS,
262113fc505SJon Mason 	MW0_SZ_HIGH,
263113fc505SJon Mason 	MW0_SZ_LOW,
264fce8a7bbSJon Mason };
265fce8a7bbSJon Mason 
266e26a5843SAllen Hubbe #define dev_client_dev(__dev) \
267e26a5843SAllen Hubbe 	container_of((__dev), struct ntb_transport_client_dev, dev)
268e26a5843SAllen Hubbe 
269e26a5843SAllen Hubbe #define drv_client(__drv) \
270e26a5843SAllen Hubbe 	container_of((__drv), struct ntb_transport_client, driver)
271e26a5843SAllen Hubbe 
272e26a5843SAllen Hubbe #define QP_TO_MW(nt, qp)	((qp) % nt->mw_count)
273fce8a7bbSJon Mason #define NTB_QP_DEF_NUM_ENTRIES	100
274fce8a7bbSJon Mason #define NTB_LINK_DOWN_TIMEOUT	10
275fce8a7bbSJon Mason 
276e26a5843SAllen Hubbe static void ntb_transport_rxc_db(unsigned long data);
277e26a5843SAllen Hubbe static const struct ntb_ctx_ops ntb_transport_ops;
278e26a5843SAllen Hubbe static struct ntb_client ntb_transport_client;
2799cabc269SDave Jiang static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
2809cabc269SDave Jiang 			       struct ntb_queue_entry *entry);
2819cabc269SDave Jiang static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset);
28272203572SDave Jiang static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset);
28372203572SDave Jiang static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset);
28472203572SDave Jiang 
285e26a5843SAllen Hubbe 
286e26a5843SAllen Hubbe static int ntb_transport_bus_match(struct device *dev,
287e26a5843SAllen Hubbe 				   struct device_driver *drv)
288fce8a7bbSJon Mason {
289fce8a7bbSJon Mason 	return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
290fce8a7bbSJon Mason }
291fce8a7bbSJon Mason 
292e26a5843SAllen Hubbe static int ntb_transport_bus_probe(struct device *dev)
293fce8a7bbSJon Mason {
294e26a5843SAllen Hubbe 	const struct ntb_transport_client *client;
2955e2cbf13SColin Ian King 	int rc;
296fce8a7bbSJon Mason 
297fce8a7bbSJon Mason 	get_device(dev);
298e26a5843SAllen Hubbe 
299e26a5843SAllen Hubbe 	client = drv_client(dev->driver);
300e26a5843SAllen Hubbe 	rc = client->probe(dev);
301fce8a7bbSJon Mason 	if (rc)
302fce8a7bbSJon Mason 		put_device(dev);
303fce8a7bbSJon Mason 
304fce8a7bbSJon Mason 	return rc;
305fce8a7bbSJon Mason }
306fce8a7bbSJon Mason 
307fc7a6209SUwe Kleine-König static void ntb_transport_bus_remove(struct device *dev)
308fce8a7bbSJon Mason {
309e26a5843SAllen Hubbe 	const struct ntb_transport_client *client;
310fce8a7bbSJon Mason 
311e26a5843SAllen Hubbe 	client = drv_client(dev->driver);
312e26a5843SAllen Hubbe 	client->remove(dev);
313fce8a7bbSJon Mason 
314fce8a7bbSJon Mason 	put_device(dev);
315fce8a7bbSJon Mason }
316fce8a7bbSJon Mason 
317e26a5843SAllen Hubbe static struct bus_type ntb_transport_bus = {
318e26a5843SAllen Hubbe 	.name = "ntb_transport",
319e26a5843SAllen Hubbe 	.match = ntb_transport_bus_match,
320e26a5843SAllen Hubbe 	.probe = ntb_transport_bus_probe,
321e26a5843SAllen Hubbe 	.remove = ntb_transport_bus_remove,
322fce8a7bbSJon Mason };
323fce8a7bbSJon Mason 
324fce8a7bbSJon Mason static LIST_HEAD(ntb_transport_list);
325fce8a7bbSJon Mason 
326e26a5843SAllen Hubbe static int ntb_bus_init(struct ntb_transport_ctx *nt)
327fce8a7bbSJon Mason {
32831510000SDave Jiang 	list_add_tail(&nt->entry, &ntb_transport_list);
329fce8a7bbSJon Mason 	return 0;
330fce8a7bbSJon Mason }
331fce8a7bbSJon Mason 
332e26a5843SAllen Hubbe static void ntb_bus_remove(struct ntb_transport_ctx *nt)
333fce8a7bbSJon Mason {
334fce8a7bbSJon Mason 	struct ntb_transport_client_dev *client_dev, *cd;
335fce8a7bbSJon Mason 
336fce8a7bbSJon Mason 	list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
337fce8a7bbSJon Mason 		dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
338fce8a7bbSJon Mason 			dev_name(&client_dev->dev));
339fce8a7bbSJon Mason 		list_del(&client_dev->entry);
340fce8a7bbSJon Mason 		device_unregister(&client_dev->dev);
341fce8a7bbSJon Mason 	}
342fce8a7bbSJon Mason 
343fce8a7bbSJon Mason 	list_del(&nt->entry);
344fce8a7bbSJon Mason }
345fce8a7bbSJon Mason 
346e26a5843SAllen Hubbe static void ntb_transport_client_release(struct device *dev)
347fce8a7bbSJon Mason {
348fce8a7bbSJon Mason 	struct ntb_transport_client_dev *client_dev;
349fce8a7bbSJon Mason 
350e26a5843SAllen Hubbe 	client_dev = dev_client_dev(dev);
351fce8a7bbSJon Mason 	kfree(client_dev);
352fce8a7bbSJon Mason }
353fce8a7bbSJon Mason 
354fce8a7bbSJon Mason /**
355e26a5843SAllen Hubbe  * ntb_transport_unregister_client_dev - Unregister NTB client device
356fce8a7bbSJon Mason  * @device_name: Name of NTB client device
357fce8a7bbSJon Mason  *
358fce8a7bbSJon Mason  * Unregister an NTB client device with the NTB transport layer
359fce8a7bbSJon Mason  */
360e26a5843SAllen Hubbe void ntb_transport_unregister_client_dev(char *device_name)
361fce8a7bbSJon Mason {
362fce8a7bbSJon Mason 	struct ntb_transport_client_dev *client, *cd;
363e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt;
364fce8a7bbSJon Mason 
365fce8a7bbSJon Mason 	list_for_each_entry(nt, &ntb_transport_list, entry)
366fce8a7bbSJon Mason 		list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
367fce8a7bbSJon Mason 			if (!strncmp(dev_name(&client->dev), device_name,
368fce8a7bbSJon Mason 				     strlen(device_name))) {
369fce8a7bbSJon Mason 				list_del(&client->entry);
370fce8a7bbSJon Mason 				device_unregister(&client->dev);
371fce8a7bbSJon Mason 			}
372fce8a7bbSJon Mason }
373e26a5843SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
374fce8a7bbSJon Mason 
375fce8a7bbSJon Mason /**
376e26a5843SAllen Hubbe  * ntb_transport_register_client_dev - Register NTB client device
377fce8a7bbSJon Mason  * @device_name: Name of NTB client device
378fce8a7bbSJon Mason  *
379fce8a7bbSJon Mason  * Register an NTB client device with the NTB transport layer
380fce8a7bbSJon Mason  */
381e26a5843SAllen Hubbe int ntb_transport_register_client_dev(char *device_name)
382fce8a7bbSJon Mason {
383fce8a7bbSJon Mason 	struct ntb_transport_client_dev *client_dev;
384e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt;
3851199aa61SAllen Hubbe 	int node;
3868b19d450SJon Mason 	int rc, i = 0;
387fce8a7bbSJon Mason 
3888222b402SJon Mason 	if (list_empty(&ntb_transport_list))
3898222b402SJon Mason 		return -ENODEV;
3908222b402SJon Mason 
391fce8a7bbSJon Mason 	list_for_each_entry(nt, &ntb_transport_list, entry) {
392fce8a7bbSJon Mason 		struct device *dev;
393fce8a7bbSJon Mason 
3941199aa61SAllen Hubbe 		node = dev_to_node(&nt->ndev->dev);
3951199aa61SAllen Hubbe 
3961199aa61SAllen Hubbe 		client_dev = kzalloc_node(sizeof(*client_dev),
3971199aa61SAllen Hubbe 					  GFP_KERNEL, node);
398fce8a7bbSJon Mason 		if (!client_dev) {
399fce8a7bbSJon Mason 			rc = -ENOMEM;
400fce8a7bbSJon Mason 			goto err;
401fce8a7bbSJon Mason 		}
402fce8a7bbSJon Mason 
403fce8a7bbSJon Mason 		dev = &client_dev->dev;
404fce8a7bbSJon Mason 
405fce8a7bbSJon Mason 		/* setup and register client devices */
4068b19d450SJon Mason 		dev_set_name(dev, "%s%d", device_name, i);
407e26a5843SAllen Hubbe 		dev->bus = &ntb_transport_bus;
408e26a5843SAllen Hubbe 		dev->release = ntb_transport_client_release;
409e26a5843SAllen Hubbe 		dev->parent = &nt->ndev->dev;
410fce8a7bbSJon Mason 
411fce8a7bbSJon Mason 		rc = device_register(dev);
412fce8a7bbSJon Mason 		if (rc) {
4138623ccbfSYang Yingliang 			put_device(dev);
414fce8a7bbSJon Mason 			goto err;
415fce8a7bbSJon Mason 		}
416fce8a7bbSJon Mason 
417fce8a7bbSJon Mason 		list_add_tail(&client_dev->entry, &nt->client_devs);
4188b19d450SJon Mason 		i++;
419fce8a7bbSJon Mason 	}
420fce8a7bbSJon Mason 
421fce8a7bbSJon Mason 	return 0;
422fce8a7bbSJon Mason 
423fce8a7bbSJon Mason err:
424e26a5843SAllen Hubbe 	ntb_transport_unregister_client_dev(device_name);
425fce8a7bbSJon Mason 
426fce8a7bbSJon Mason 	return rc;
427fce8a7bbSJon Mason }
428e26a5843SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
429fce8a7bbSJon Mason 
430fce8a7bbSJon Mason /**
431ec110bc7SAllen Hubbe  * ntb_transport_register_client - Register NTB client driver
432fce8a7bbSJon Mason  * @drv: NTB client driver to be registered
433fce8a7bbSJon Mason  *
434fce8a7bbSJon Mason  * Register an NTB client driver with the NTB transport layer
435fce8a7bbSJon Mason  *
436fce8a7bbSJon Mason  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
437fce8a7bbSJon Mason  */
438e26a5843SAllen Hubbe int ntb_transport_register_client(struct ntb_transport_client *drv)
439fce8a7bbSJon Mason {
440e26a5843SAllen Hubbe 	drv->driver.bus = &ntb_transport_bus;
441fce8a7bbSJon Mason 
4428222b402SJon Mason 	if (list_empty(&ntb_transport_list))
4438222b402SJon Mason 		return -ENODEV;
4448222b402SJon Mason 
445fce8a7bbSJon Mason 	return driver_register(&drv->driver);
446fce8a7bbSJon Mason }
447ec110bc7SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_register_client);
448fce8a7bbSJon Mason 
449fce8a7bbSJon Mason /**
450ec110bc7SAllen Hubbe  * ntb_transport_unregister_client - Unregister NTB client driver
451fce8a7bbSJon Mason  * @drv: NTB client driver to be unregistered
452fce8a7bbSJon Mason  *
453fce8a7bbSJon Mason  * Unregister an NTB client driver with the NTB transport layer
454fce8a7bbSJon Mason  *
455fce8a7bbSJon Mason  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
456fce8a7bbSJon Mason  */
457e26a5843SAllen Hubbe void ntb_transport_unregister_client(struct ntb_transport_client *drv)
458fce8a7bbSJon Mason {
459fce8a7bbSJon Mason 	driver_unregister(&drv->driver);
460fce8a7bbSJon Mason }
461ec110bc7SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
462fce8a7bbSJon Mason 
463fce8a7bbSJon Mason static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
464fce8a7bbSJon Mason 			    loff_t *offp)
465fce8a7bbSJon Mason {
466fce8a7bbSJon Mason 	struct ntb_transport_qp *qp;
467d7237e22SJon Mason 	char *buf;
468fce8a7bbSJon Mason 	ssize_t ret, out_offset, out_count;
469fce8a7bbSJon Mason 
470260bee94SDave Jiang 	qp = filp->private_data;
471260bee94SDave Jiang 
472260bee94SDave Jiang 	if (!qp || !qp->link_is_up)
473260bee94SDave Jiang 		return 0;
474260bee94SDave Jiang 
475282a2feeSJon Mason 	out_count = 1000;
476d7237e22SJon Mason 
477d7237e22SJon Mason 	buf = kmalloc(out_count, GFP_KERNEL);
478d7237e22SJon Mason 	if (!buf)
479d7237e22SJon Mason 		return -ENOMEM;
480fce8a7bbSJon Mason 
481fce8a7bbSJon Mason 	out_offset = 0;
4827f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
483d98ef99eSDave Jiang 			       "\nNTB QP stats:\n\n");
4847f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
485fce8a7bbSJon Mason 			       "rx_bytes - \t%llu\n", qp->rx_bytes);
4867f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
487fce8a7bbSJon Mason 			       "rx_pkts - \t%llu\n", qp->rx_pkts);
4887f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
489282a2feeSJon Mason 			       "rx_memcpy - \t%llu\n", qp->rx_memcpy);
4907f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
491282a2feeSJon Mason 			       "rx_async - \t%llu\n", qp->rx_async);
4927f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
493fce8a7bbSJon Mason 			       "rx_ring_empty - %llu\n", qp->rx_ring_empty);
4947f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
495fce8a7bbSJon Mason 			       "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
4967f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
497fce8a7bbSJon Mason 			       "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
4987f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
499fce8a7bbSJon Mason 			       "rx_err_ver - \t%llu\n", qp->rx_err_ver);
5007f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
501d98ef99eSDave Jiang 			       "rx_buff - \t0x%p\n", qp->rx_buff);
5027f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
503793c20e9SJon Mason 			       "rx_index - \t%u\n", qp->rx_index);
5047f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
505a754a8fcSDave Jiang 			       "rx_max_entry - \t%u\n", qp->rx_max_entry);
5067f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
507a754a8fcSDave Jiang 			       "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry);
508fce8a7bbSJon Mason 
5097f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
510fce8a7bbSJon Mason 			       "tx_bytes - \t%llu\n", qp->tx_bytes);
5117f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
512fce8a7bbSJon Mason 			       "tx_pkts - \t%llu\n", qp->tx_pkts);
5137f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
514282a2feeSJon Mason 			       "tx_memcpy - \t%llu\n", qp->tx_memcpy);
5157f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
516282a2feeSJon Mason 			       "tx_async - \t%llu\n", qp->tx_async);
5177f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
518fce8a7bbSJon Mason 			       "tx_ring_full - \t%llu\n", qp->tx_ring_full);
5197f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
520282a2feeSJon Mason 			       "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
5217f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
522d98ef99eSDave Jiang 			       "tx_mw - \t0x%p\n", qp->tx_mw);
5237f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
524d98ef99eSDave Jiang 			       "tx_index (H) - \t%u\n", qp->tx_index);
5257f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
526d98ef99eSDave Jiang 			       "RRI (T) - \t%u\n",
527d98ef99eSDave Jiang 			       qp->remote_rx_info->entry);
5287f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
529793c20e9SJon Mason 			       "tx_max_entry - \t%u\n", qp->tx_max_entry);
5307f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
531e74bfeedSDave Jiang 			       "free tx - \t%u\n",
532e74bfeedSDave Jiang 			       ntb_transport_tx_free_entry(qp));
533fce8a7bbSJon Mason 
5347f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
535d98ef99eSDave Jiang 			       "\n");
5367f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
537569410caSDave Jiang 			       "Using TX DMA - \t%s\n",
538569410caSDave Jiang 			       qp->tx_dma_chan ? "Yes" : "No");
5397f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
540569410caSDave Jiang 			       "Using RX DMA - \t%s\n",
541569410caSDave Jiang 			       qp->rx_dma_chan ? "Yes" : "No");
5427f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
543d98ef99eSDave Jiang 			       "QP Link - \t%s\n",
544e26a5843SAllen Hubbe 			       qp->link_is_up ? "Up" : "Down");
5457f78c68aSTakashi Iwai 	out_offset += scnprintf(buf + out_offset, out_count - out_offset,
546d98ef99eSDave Jiang 			       "\n");
547d98ef99eSDave Jiang 
548d7237e22SJon Mason 	if (out_offset > out_count)
549d7237e22SJon Mason 		out_offset = out_count;
550fce8a7bbSJon Mason 
551fce8a7bbSJon Mason 	ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
552d7237e22SJon Mason 	kfree(buf);
553fce8a7bbSJon Mason 	return ret;
554fce8a7bbSJon Mason }
555fce8a7bbSJon Mason 
556fce8a7bbSJon Mason static const struct file_operations ntb_qp_debugfs_stats = {
557fce8a7bbSJon Mason 	.owner = THIS_MODULE,
558d66d7ac2SJon Mason 	.open = simple_open,
559fce8a7bbSJon Mason 	.read = debugfs_read,
560fce8a7bbSJon Mason };
561fce8a7bbSJon Mason 
562fce8a7bbSJon Mason static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
563fce8a7bbSJon Mason 			 struct list_head *list)
564fce8a7bbSJon Mason {
565fce8a7bbSJon Mason 	unsigned long flags;
566fce8a7bbSJon Mason 
567fce8a7bbSJon Mason 	spin_lock_irqsave(lock, flags);
568fce8a7bbSJon Mason 	list_add_tail(entry, list);
569fce8a7bbSJon Mason 	spin_unlock_irqrestore(lock, flags);
570fce8a7bbSJon Mason }
571fce8a7bbSJon Mason 
572fce8a7bbSJon Mason static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
573fce8a7bbSJon Mason 					   struct list_head *list)
574fce8a7bbSJon Mason {
575fce8a7bbSJon Mason 	struct ntb_queue_entry *entry;
576fce8a7bbSJon Mason 	unsigned long flags;
577fce8a7bbSJon Mason 
578fce8a7bbSJon Mason 	spin_lock_irqsave(lock, flags);
579fce8a7bbSJon Mason 	if (list_empty(list)) {
580fce8a7bbSJon Mason 		entry = NULL;
581fce8a7bbSJon Mason 		goto out;
582fce8a7bbSJon Mason 	}
583fce8a7bbSJon Mason 	entry = list_first_entry(list, struct ntb_queue_entry, entry);
584fce8a7bbSJon Mason 	list_del(&entry->entry);
585e74bfeedSDave Jiang 
586fce8a7bbSJon Mason out:
587fce8a7bbSJon Mason 	spin_unlock_irqrestore(lock, flags);
588fce8a7bbSJon Mason 
589fce8a7bbSJon Mason 	return entry;
590fce8a7bbSJon Mason }
591fce8a7bbSJon Mason 
592da2e5ae5SAllen Hubbe static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
593da2e5ae5SAllen Hubbe 					   struct list_head *list,
594da2e5ae5SAllen Hubbe 					   struct list_head *to_list)
595da2e5ae5SAllen Hubbe {
596da2e5ae5SAllen Hubbe 	struct ntb_queue_entry *entry;
597da2e5ae5SAllen Hubbe 	unsigned long flags;
598da2e5ae5SAllen Hubbe 
599da2e5ae5SAllen Hubbe 	spin_lock_irqsave(lock, flags);
600da2e5ae5SAllen Hubbe 
601da2e5ae5SAllen Hubbe 	if (list_empty(list)) {
602da2e5ae5SAllen Hubbe 		entry = NULL;
603da2e5ae5SAllen Hubbe 	} else {
604da2e5ae5SAllen Hubbe 		entry = list_first_entry(list, struct ntb_queue_entry, entry);
605da2e5ae5SAllen Hubbe 		list_move_tail(&entry->entry, to_list);
606da2e5ae5SAllen Hubbe 	}
607da2e5ae5SAllen Hubbe 
608da2e5ae5SAllen Hubbe 	spin_unlock_irqrestore(lock, flags);
609da2e5ae5SAllen Hubbe 
610da2e5ae5SAllen Hubbe 	return entry;
611da2e5ae5SAllen Hubbe }
612da2e5ae5SAllen Hubbe 
613e26a5843SAllen Hubbe static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
614fce8a7bbSJon Mason 				     unsigned int qp_num)
615fce8a7bbSJon Mason {
616e26a5843SAllen Hubbe 	struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
617e26a5843SAllen Hubbe 	struct ntb_transport_mw *mw;
618a754a8fcSDave Jiang 	struct ntb_dev *ndev = nt->ndev;
619a754a8fcSDave Jiang 	struct ntb_queue_entry *entry;
620ef114ed5SJon Mason 	unsigned int rx_size, num_qps_mw;
621e26a5843SAllen Hubbe 	unsigned int mw_num, mw_count, qp_count;
622793c20e9SJon Mason 	unsigned int i;
623a754a8fcSDave Jiang 	int node;
624fce8a7bbSJon Mason 
625e26a5843SAllen Hubbe 	mw_count = nt->mw_count;
626e26a5843SAllen Hubbe 	qp_count = nt->qp_count;
627948d3a65SJon Mason 
628e26a5843SAllen Hubbe 	mw_num = QP_TO_MW(nt, qp_num);
629e26a5843SAllen Hubbe 	mw = &nt->mw_vec[mw_num];
630fce8a7bbSJon Mason 
631e26a5843SAllen Hubbe 	if (!mw->virt_addr)
632e26a5843SAllen Hubbe 		return -ENOMEM;
633e26a5843SAllen Hubbe 
6348e8496e0SLogan Gunthorpe 	if (mw_num < qp_count % mw_count)
635e26a5843SAllen Hubbe 		num_qps_mw = qp_count / mw_count + 1;
636fce8a7bbSJon Mason 	else
637e26a5843SAllen Hubbe 		num_qps_mw = qp_count / mw_count;
638fce8a7bbSJon Mason 
639e26a5843SAllen Hubbe 	rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
640c92ba3c5SJon Mason 	qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
641793c20e9SJon Mason 	rx_size -= sizeof(struct ntb_rx_info);
642793c20e9SJon Mason 
643282a2feeSJon Mason 	qp->remote_rx_info = qp->rx_buff + rx_size;
644282a2feeSJon Mason 
645c9d534c8SJon Mason 	/* Due to housekeeping, there must be atleast 2 buffs */
646c9d534c8SJon Mason 	qp->rx_max_frame = min(transport_mtu, rx_size / 2);
647793c20e9SJon Mason 	qp->rx_max_entry = rx_size / qp->rx_max_frame;
648793c20e9SJon Mason 	qp->rx_index = 0;
649793c20e9SJon Mason 
650a754a8fcSDave Jiang 	/*
651a754a8fcSDave Jiang 	 * Checking to see if we have more entries than the default.
652a754a8fcSDave Jiang 	 * We should add additional entries if that is the case so we
653a754a8fcSDave Jiang 	 * can be in sync with the transport frames.
654a754a8fcSDave Jiang 	 */
655a754a8fcSDave Jiang 	node = dev_to_node(&ndev->dev);
656a754a8fcSDave Jiang 	for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) {
65782edcc75SJia-Ju Bai 		entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
658a754a8fcSDave Jiang 		if (!entry)
659a754a8fcSDave Jiang 			return -ENOMEM;
660a754a8fcSDave Jiang 
661a754a8fcSDave Jiang 		entry->qp = qp;
662a754a8fcSDave Jiang 		ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
663a754a8fcSDave Jiang 			     &qp->rx_free_q);
664a754a8fcSDave Jiang 		qp->rx_alloc_entry++;
665a754a8fcSDave Jiang 	}
666a754a8fcSDave Jiang 
667c9d534c8SJon Mason 	qp->remote_rx_info->entry = qp->rx_max_entry - 1;
668fce8a7bbSJon Mason 
669ef114ed5SJon Mason 	/* setup the hdr offsets with 0's */
670793c20e9SJon Mason 	for (i = 0; i < qp->rx_max_entry; i++) {
671e26a5843SAllen Hubbe 		void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
672e26a5843SAllen Hubbe 				sizeof(struct ntb_payload_header));
673ef114ed5SJon Mason 		memset(offset, 0, sizeof(struct ntb_payload_header));
674793c20e9SJon Mason 	}
675fce8a7bbSJon Mason 
676fce8a7bbSJon Mason 	qp->rx_pkts = 0;
677fce8a7bbSJon Mason 	qp->tx_pkts = 0;
67890f9e934SJon Mason 	qp->tx_index = 0;
679e26a5843SAllen Hubbe 
680e26a5843SAllen Hubbe 	return 0;
681fce8a7bbSJon Mason }
682fce8a7bbSJon Mason 
6832b0569b3SLogan Gunthorpe static irqreturn_t ntb_transport_isr(int irq, void *dev)
6842b0569b3SLogan Gunthorpe {
6852b0569b3SLogan Gunthorpe 	struct ntb_transport_qp *qp = dev;
6862b0569b3SLogan Gunthorpe 
6872b0569b3SLogan Gunthorpe 	tasklet_schedule(&qp->rxc_db_work);
6882b0569b3SLogan Gunthorpe 
6892b0569b3SLogan Gunthorpe 	return IRQ_HANDLED;
6902b0569b3SLogan Gunthorpe }
6912b0569b3SLogan Gunthorpe 
6922b0569b3SLogan Gunthorpe static void ntb_transport_setup_qp_peer_msi(struct ntb_transport_ctx *nt,
6932b0569b3SLogan Gunthorpe 					    unsigned int qp_num)
6942b0569b3SLogan Gunthorpe {
6952b0569b3SLogan Gunthorpe 	struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
6962b0569b3SLogan Gunthorpe 	int spad = qp_num * 2 + nt->msi_spad_offset;
6972b0569b3SLogan Gunthorpe 
6982b0569b3SLogan Gunthorpe 	if (!nt->use_msi)
6992b0569b3SLogan Gunthorpe 		return;
7002b0569b3SLogan Gunthorpe 
7012b0569b3SLogan Gunthorpe 	if (spad >= ntb_spad_count(nt->ndev))
7022b0569b3SLogan Gunthorpe 		return;
7032b0569b3SLogan Gunthorpe 
7042b0569b3SLogan Gunthorpe 	qp->peer_msi_desc.addr_offset =
7052b0569b3SLogan Gunthorpe 		ntb_peer_spad_read(qp->ndev, PIDX, spad);
7062b0569b3SLogan Gunthorpe 	qp->peer_msi_desc.data =
7072b0569b3SLogan Gunthorpe 		ntb_peer_spad_read(qp->ndev, PIDX, spad + 1);
7082b0569b3SLogan Gunthorpe 
7092b0569b3SLogan Gunthorpe 	dev_dbg(&qp->ndev->pdev->dev, "QP%d Peer MSI addr=%x data=%x\n",
7102b0569b3SLogan Gunthorpe 		qp_num, qp->peer_msi_desc.addr_offset, qp->peer_msi_desc.data);
7112b0569b3SLogan Gunthorpe 
7122b0569b3SLogan Gunthorpe 	if (qp->peer_msi_desc.addr_offset) {
7132b0569b3SLogan Gunthorpe 		qp->use_msi = true;
7142b0569b3SLogan Gunthorpe 		dev_info(&qp->ndev->pdev->dev,
7152b0569b3SLogan Gunthorpe 			 "Using MSI interrupts for QP%d\n", qp_num);
7162b0569b3SLogan Gunthorpe 	}
7172b0569b3SLogan Gunthorpe }
7182b0569b3SLogan Gunthorpe 
7192b0569b3SLogan Gunthorpe static void ntb_transport_setup_qp_msi(struct ntb_transport_ctx *nt,
7202b0569b3SLogan Gunthorpe 				       unsigned int qp_num)
7212b0569b3SLogan Gunthorpe {
7222b0569b3SLogan Gunthorpe 	struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
7232b0569b3SLogan Gunthorpe 	int spad = qp_num * 2 + nt->msi_spad_offset;
7242b0569b3SLogan Gunthorpe 	int rc;
7252b0569b3SLogan Gunthorpe 
7262b0569b3SLogan Gunthorpe 	if (!nt->use_msi)
7272b0569b3SLogan Gunthorpe 		return;
7282b0569b3SLogan Gunthorpe 
7292b0569b3SLogan Gunthorpe 	if (spad >= ntb_spad_count(nt->ndev)) {
7302b0569b3SLogan Gunthorpe 		dev_warn_once(&qp->ndev->pdev->dev,
7312b0569b3SLogan Gunthorpe 			      "Not enough SPADS to use MSI interrupts\n");
7322b0569b3SLogan Gunthorpe 		return;
7332b0569b3SLogan Gunthorpe 	}
7342b0569b3SLogan Gunthorpe 
7352b0569b3SLogan Gunthorpe 	ntb_spad_write(qp->ndev, spad, 0);
7362b0569b3SLogan Gunthorpe 	ntb_spad_write(qp->ndev, spad + 1, 0);
7372b0569b3SLogan Gunthorpe 
7382b0569b3SLogan Gunthorpe 	if (!qp->msi_irq) {
7392b0569b3SLogan Gunthorpe 		qp->msi_irq = ntbm_msi_request_irq(qp->ndev, ntb_transport_isr,
7402b0569b3SLogan Gunthorpe 						   KBUILD_MODNAME, qp,
7412b0569b3SLogan Gunthorpe 						   &qp->msi_desc);
7422b0569b3SLogan Gunthorpe 		if (qp->msi_irq < 0) {
7432b0569b3SLogan Gunthorpe 			dev_warn(&qp->ndev->pdev->dev,
7442b0569b3SLogan Gunthorpe 				 "Unable to allocate MSI interrupt for qp%d\n",
7452b0569b3SLogan Gunthorpe 				 qp_num);
7462b0569b3SLogan Gunthorpe 			return;
7472b0569b3SLogan Gunthorpe 		}
7482b0569b3SLogan Gunthorpe 	}
7492b0569b3SLogan Gunthorpe 
7502b0569b3SLogan Gunthorpe 	rc = ntb_spad_write(qp->ndev, spad, qp->msi_desc.addr_offset);
7512b0569b3SLogan Gunthorpe 	if (rc)
7522b0569b3SLogan Gunthorpe 		goto err_free_interrupt;
7532b0569b3SLogan Gunthorpe 
7542b0569b3SLogan Gunthorpe 	rc = ntb_spad_write(qp->ndev, spad + 1, qp->msi_desc.data);
7552b0569b3SLogan Gunthorpe 	if (rc)
7562b0569b3SLogan Gunthorpe 		goto err_free_interrupt;
7572b0569b3SLogan Gunthorpe 
7582b0569b3SLogan Gunthorpe 	dev_dbg(&qp->ndev->pdev->dev, "QP%d MSI %d addr=%x data=%x\n",
7592b0569b3SLogan Gunthorpe 		qp_num, qp->msi_irq, qp->msi_desc.addr_offset,
7602b0569b3SLogan Gunthorpe 		qp->msi_desc.data);
7612b0569b3SLogan Gunthorpe 
7622b0569b3SLogan Gunthorpe 	return;
7632b0569b3SLogan Gunthorpe 
7642b0569b3SLogan Gunthorpe err_free_interrupt:
7652b0569b3SLogan Gunthorpe 	devm_free_irq(&nt->ndev->dev, qp->msi_irq, qp);
7662b0569b3SLogan Gunthorpe }
7672b0569b3SLogan Gunthorpe 
7682b0569b3SLogan Gunthorpe static void ntb_transport_msi_peer_desc_changed(struct ntb_transport_ctx *nt)
7692b0569b3SLogan Gunthorpe {
7702b0569b3SLogan Gunthorpe 	int i;
7712b0569b3SLogan Gunthorpe 
7722b0569b3SLogan Gunthorpe 	dev_dbg(&nt->ndev->pdev->dev, "Peer MSI descriptors changed");
7732b0569b3SLogan Gunthorpe 
7742b0569b3SLogan Gunthorpe 	for (i = 0; i < nt->qp_count; i++)
7752b0569b3SLogan Gunthorpe 		ntb_transport_setup_qp_peer_msi(nt, i);
7762b0569b3SLogan Gunthorpe }
7772b0569b3SLogan Gunthorpe 
7782b0569b3SLogan Gunthorpe static void ntb_transport_msi_desc_changed(void *data)
7792b0569b3SLogan Gunthorpe {
7802b0569b3SLogan Gunthorpe 	struct ntb_transport_ctx *nt = data;
7812b0569b3SLogan Gunthorpe 	int i;
7822b0569b3SLogan Gunthorpe 
7832b0569b3SLogan Gunthorpe 	dev_dbg(&nt->ndev->pdev->dev, "MSI descriptors changed");
7842b0569b3SLogan Gunthorpe 
7852b0569b3SLogan Gunthorpe 	for (i = 0; i < nt->qp_count; i++)
7862b0569b3SLogan Gunthorpe 		ntb_transport_setup_qp_msi(nt, i);
7872b0569b3SLogan Gunthorpe 
7882b0569b3SLogan Gunthorpe 	ntb_peer_db_set(nt->ndev, nt->msi_db_mask);
7892b0569b3SLogan Gunthorpe }
7902b0569b3SLogan Gunthorpe 
791e26a5843SAllen Hubbe static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
792113fc505SJon Mason {
793e26a5843SAllen Hubbe 	struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
794e26a5843SAllen Hubbe 	struct pci_dev *pdev = nt->ndev->pdev;
795113fc505SJon Mason 
796113fc505SJon Mason 	if (!mw->virt_addr)
797113fc505SJon Mason 		return;
798113fc505SJon Mason 
799443b9a14SSerge Semin 	ntb_mw_clear_trans(nt->ndev, PIDX, num_mw);
800fc5d1829SAaron Sierra 	dma_free_coherent(&pdev->dev, mw->alloc_size,
801fc5d1829SAaron Sierra 			  mw->alloc_addr, mw->dma_addr);
802e26a5843SAllen Hubbe 	mw->xlat_size = 0;
803e26a5843SAllen Hubbe 	mw->buff_size = 0;
804fc5d1829SAaron Sierra 	mw->alloc_size = 0;
805fc5d1829SAaron Sierra 	mw->alloc_addr = NULL;
806113fc505SJon Mason 	mw->virt_addr = NULL;
807113fc505SJon Mason }
808113fc505SJon Mason 
809fc5d1829SAaron Sierra static int ntb_alloc_mw_buffer(struct ntb_transport_mw *mw,
810fc5d1829SAaron Sierra 			       struct device *dma_dev, size_t align)
811fc5d1829SAaron Sierra {
812fc5d1829SAaron Sierra 	dma_addr_t dma_addr;
813fc5d1829SAaron Sierra 	void *alloc_addr, *virt_addr;
814fc5d1829SAaron Sierra 	int rc;
815fc5d1829SAaron Sierra 
816fc5d1829SAaron Sierra 	alloc_addr = dma_alloc_coherent(dma_dev, mw->alloc_size,
817fc5d1829SAaron Sierra 					&dma_addr, GFP_KERNEL);
818fc5d1829SAaron Sierra 	if (!alloc_addr) {
819fc5d1829SAaron Sierra 		dev_err(dma_dev, "Unable to alloc MW buff of size %zu\n",
820fc5d1829SAaron Sierra 			mw->alloc_size);
821fc5d1829SAaron Sierra 		return -ENOMEM;
822fc5d1829SAaron Sierra 	}
823fc5d1829SAaron Sierra 	virt_addr = alloc_addr;
824fc5d1829SAaron Sierra 
825fc5d1829SAaron Sierra 	/*
826fc5d1829SAaron Sierra 	 * we must ensure that the memory address allocated is BAR size
827fc5d1829SAaron Sierra 	 * aligned in order for the XLAT register to take the value. This
828fc5d1829SAaron Sierra 	 * is a requirement of the hardware. It is recommended to setup CMA
829fc5d1829SAaron Sierra 	 * for BAR sizes equal or greater than 4MB.
830fc5d1829SAaron Sierra 	 */
831fc5d1829SAaron Sierra 	if (!IS_ALIGNED(dma_addr, align)) {
832fc5d1829SAaron Sierra 		if (mw->alloc_size > mw->buff_size) {
833fc5d1829SAaron Sierra 			virt_addr = PTR_ALIGN(alloc_addr, align);
834fc5d1829SAaron Sierra 			dma_addr = ALIGN(dma_addr, align);
835fc5d1829SAaron Sierra 		} else {
836fc5d1829SAaron Sierra 			rc = -ENOMEM;
837fc5d1829SAaron Sierra 			goto err;
838fc5d1829SAaron Sierra 		}
839fc5d1829SAaron Sierra 	}
840fc5d1829SAaron Sierra 
841fc5d1829SAaron Sierra 	mw->alloc_addr = alloc_addr;
842fc5d1829SAaron Sierra 	mw->virt_addr = virt_addr;
843fc5d1829SAaron Sierra 	mw->dma_addr = dma_addr;
844fc5d1829SAaron Sierra 
845fc5d1829SAaron Sierra 	return 0;
846fc5d1829SAaron Sierra 
847fc5d1829SAaron Sierra err:
848fc5d1829SAaron Sierra 	dma_free_coherent(dma_dev, mw->alloc_size, alloc_addr, dma_addr);
849fc5d1829SAaron Sierra 
850fc5d1829SAaron Sierra 	return rc;
851fc5d1829SAaron Sierra }
852fc5d1829SAaron Sierra 
853e26a5843SAllen Hubbe static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
8548c9edf63SAllen Hubbe 		      resource_size_t size)
855b77b2637SJon Mason {
856e26a5843SAllen Hubbe 	struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
857e26a5843SAllen Hubbe 	struct pci_dev *pdev = nt->ndev->pdev;
8588c9edf63SAllen Hubbe 	size_t xlat_size, buff_size;
859980c41c8SLogan Gunthorpe 	resource_size_t xlat_align;
860980c41c8SLogan Gunthorpe 	resource_size_t xlat_align_size;
861e26a5843SAllen Hubbe 	int rc;
862e26a5843SAllen Hubbe 
8638c9edf63SAllen Hubbe 	if (!size)
8648c9edf63SAllen Hubbe 		return -EINVAL;
8658c9edf63SAllen Hubbe 
866980c41c8SLogan Gunthorpe 	rc = ntb_mw_get_align(nt->ndev, PIDX, num_mw, &xlat_align,
867980c41c8SLogan Gunthorpe 			      &xlat_align_size, NULL);
868980c41c8SLogan Gunthorpe 	if (rc)
869980c41c8SLogan Gunthorpe 		return rc;
870980c41c8SLogan Gunthorpe 
871980c41c8SLogan Gunthorpe 	xlat_size = round_up(size, xlat_align_size);
872980c41c8SLogan Gunthorpe 	buff_size = round_up(size, xlat_align);
873b77b2637SJon Mason 
874b77b2637SJon Mason 	/* No need to re-setup */
875e26a5843SAllen Hubbe 	if (mw->xlat_size == xlat_size)
876b77b2637SJon Mason 		return 0;
877b77b2637SJon Mason 
878e26a5843SAllen Hubbe 	if (mw->buff_size)
879b77b2637SJon Mason 		ntb_free_mw(nt, num_mw);
880b77b2637SJon Mason 
881e26a5843SAllen Hubbe 	/* Alloc memory for receiving data.  Must be aligned */
882e26a5843SAllen Hubbe 	mw->xlat_size = xlat_size;
883e26a5843SAllen Hubbe 	mw->buff_size = buff_size;
884fc5d1829SAaron Sierra 	mw->alloc_size = buff_size;
885b77b2637SJon Mason 
886fc5d1829SAaron Sierra 	rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
887fc5d1829SAaron Sierra 	if (rc) {
888fc5d1829SAaron Sierra 		mw->alloc_size *= 2;
889fc5d1829SAaron Sierra 		rc = ntb_alloc_mw_buffer(mw, &pdev->dev, xlat_align);
890fc5d1829SAaron Sierra 		if (rc) {
891fc5d1829SAaron Sierra 			dev_err(&pdev->dev,
892fc5d1829SAaron Sierra 				"Unable to alloc aligned MW buff\n");
893e26a5843SAllen Hubbe 			mw->xlat_size = 0;
894e26a5843SAllen Hubbe 			mw->buff_size = 0;
895fc5d1829SAaron Sierra 			mw->alloc_size = 0;
896fc5d1829SAaron Sierra 			return rc;
897b77b2637SJon Mason 		}
8983cc5ba19SDave Jiang 	}
8993cc5ba19SDave Jiang 
900b77b2637SJon Mason 	/* Notify HW the memory location of the receive buffer */
901443b9a14SSerge Semin 	rc = ntb_mw_set_trans(nt->ndev, PIDX, num_mw, mw->dma_addr,
902443b9a14SSerge Semin 			      mw->xlat_size);
903e26a5843SAllen Hubbe 	if (rc) {
904e26a5843SAllen Hubbe 		dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
905e26a5843SAllen Hubbe 		ntb_free_mw(nt, num_mw);
906e26a5843SAllen Hubbe 		return -EIO;
907e26a5843SAllen Hubbe 	}
908b77b2637SJon Mason 
909b77b2637SJon Mason 	return 0;
910b77b2637SJon Mason }
911b77b2637SJon Mason 
912cc79bd27SDave Jiang static void ntb_qp_link_context_reset(struct ntb_transport_qp *qp)
9132849b5d7SAllen Hubbe {
9142849b5d7SAllen Hubbe 	qp->link_is_up = false;
915e9021331SDave Jiang 	qp->active = false;
9162849b5d7SAllen Hubbe 
9172849b5d7SAllen Hubbe 	qp->tx_index = 0;
9182849b5d7SAllen Hubbe 	qp->rx_index = 0;
9192849b5d7SAllen Hubbe 	qp->rx_bytes = 0;
9202849b5d7SAllen Hubbe 	qp->rx_pkts = 0;
9212849b5d7SAllen Hubbe 	qp->rx_ring_empty = 0;
9222849b5d7SAllen Hubbe 	qp->rx_err_no_buf = 0;
9232849b5d7SAllen Hubbe 	qp->rx_err_oflow = 0;
9242849b5d7SAllen Hubbe 	qp->rx_err_ver = 0;
9252849b5d7SAllen Hubbe 	qp->rx_memcpy = 0;
9262849b5d7SAllen Hubbe 	qp->rx_async = 0;
9272849b5d7SAllen Hubbe 	qp->tx_bytes = 0;
9282849b5d7SAllen Hubbe 	qp->tx_pkts = 0;
9292849b5d7SAllen Hubbe 	qp->tx_ring_full = 0;
9302849b5d7SAllen Hubbe 	qp->tx_err_no_buf = 0;
9312849b5d7SAllen Hubbe 	qp->tx_memcpy = 0;
9322849b5d7SAllen Hubbe 	qp->tx_async = 0;
9332849b5d7SAllen Hubbe }
9342849b5d7SAllen Hubbe 
935cc79bd27SDave Jiang static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
936cc79bd27SDave Jiang {
937cc79bd27SDave Jiang 	ntb_qp_link_context_reset(qp);
938cc79bd27SDave Jiang 	if (qp->remote_rx_info)
939cc79bd27SDave Jiang 		qp->remote_rx_info->entry = qp->rx_max_entry - 1;
940cc79bd27SDave Jiang }
941cc79bd27SDave Jiang 
942fca4d518SJon Mason static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
943fce8a7bbSJon Mason {
944e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt = qp->transport;
945e26a5843SAllen Hubbe 	struct pci_dev *pdev = nt->ndev->pdev;
946fce8a7bbSJon Mason 
947e22e0b9dSAllen Hubbe 	dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
9482849b5d7SAllen Hubbe 
9492849b5d7SAllen Hubbe 	cancel_delayed_work_sync(&qp->link_work);
9502849b5d7SAllen Hubbe 	ntb_qp_link_down_reset(qp);
951e26a5843SAllen Hubbe 
952e26a5843SAllen Hubbe 	if (qp->event_handler)
953e26a5843SAllen Hubbe 		qp->event_handler(qp->cb_data, qp->link_is_up);
954fca4d518SJon Mason }
955fca4d518SJon Mason 
956fca4d518SJon Mason static void ntb_qp_link_cleanup_work(struct work_struct *work)
957fca4d518SJon Mason {
958fca4d518SJon Mason 	struct ntb_transport_qp *qp = container_of(work,
959fca4d518SJon Mason 						   struct ntb_transport_qp,
960fca4d518SJon Mason 						   link_cleanup);
961e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt = qp->transport;
962fca4d518SJon Mason 
963fca4d518SJon Mason 	ntb_qp_link_cleanup(qp);
964fce8a7bbSJon Mason 
965e26a5843SAllen Hubbe 	if (nt->link_is_up)
966fce8a7bbSJon Mason 		schedule_delayed_work(&qp->link_work,
967fce8a7bbSJon Mason 				      msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
968fce8a7bbSJon Mason }
969fce8a7bbSJon Mason 
9707b4f2d3cSJon Mason static void ntb_qp_link_down(struct ntb_transport_qp *qp)
971fce8a7bbSJon Mason {
9727b4f2d3cSJon Mason 	schedule_work(&qp->link_cleanup);
9737b4f2d3cSJon Mason }
9747b4f2d3cSJon Mason 
975e26a5843SAllen Hubbe static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
9767b4f2d3cSJon Mason {
977e26a5843SAllen Hubbe 	struct ntb_transport_qp *qp;
978e26a5843SAllen Hubbe 	u64 qp_bitmap_alloc;
979b17faba0SShyam Sundar S K 	unsigned int i, count;
980fce8a7bbSJon Mason 
981e26a5843SAllen Hubbe 	qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
982fca4d518SJon Mason 
983e26a5843SAllen Hubbe 	/* Pass along the info to any clients */
984e26a5843SAllen Hubbe 	for (i = 0; i < nt->qp_count; i++)
985e26a5843SAllen Hubbe 		if (qp_bitmap_alloc & BIT_ULL(i)) {
986e26a5843SAllen Hubbe 			qp = &nt->qp_vec[i];
987e26a5843SAllen Hubbe 			ntb_qp_link_cleanup(qp);
988e26a5843SAllen Hubbe 			cancel_work_sync(&qp->link_cleanup);
989e26a5843SAllen Hubbe 			cancel_delayed_work_sync(&qp->link_work);
990e26a5843SAllen Hubbe 		}
991e26a5843SAllen Hubbe 
992e26a5843SAllen Hubbe 	if (!nt->link_is_up)
993fce8a7bbSJon Mason 		cancel_delayed_work_sync(&nt->link_work);
994fce8a7bbSJon Mason 
9959143595aSJoey Zhang 	for (i = 0; i < nt->mw_count; i++)
9969143595aSJoey Zhang 		ntb_free_mw(nt, i);
9979143595aSJoey Zhang 
998fce8a7bbSJon Mason 	/* The scratchpad registers keep the values if the remote side
999fce8a7bbSJon Mason 	 * goes down, blast them now to give them a sane value the next
1000fce8a7bbSJon Mason 	 * time they are accessed
1001fce8a7bbSJon Mason 	 */
1002b17faba0SShyam Sundar S K 	count = ntb_spad_count(nt->ndev);
1003b17faba0SShyam Sundar S K 	for (i = 0; i < count; i++)
1004e26a5843SAllen Hubbe 		ntb_spad_write(nt->ndev, i, 0);
1005fce8a7bbSJon Mason }
1006fce8a7bbSJon Mason 
1007fca4d518SJon Mason static void ntb_transport_link_cleanup_work(struct work_struct *work)
1008fca4d518SJon Mason {
1009e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt =
1010e26a5843SAllen Hubbe 		container_of(work, struct ntb_transport_ctx, link_cleanup);
1011fca4d518SJon Mason 
1012fca4d518SJon Mason 	ntb_transport_link_cleanup(nt);
1013fca4d518SJon Mason }
1014fca4d518SJon Mason 
1015e26a5843SAllen Hubbe static void ntb_transport_event_callback(void *data)
1016fce8a7bbSJon Mason {
1017e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt = data;
1018fce8a7bbSJon Mason 
1019e26a5843SAllen Hubbe 	if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
1020fce8a7bbSJon Mason 		schedule_delayed_work(&nt->link_work, 0);
1021e26a5843SAllen Hubbe 	else
10227b4f2d3cSJon Mason 		schedule_work(&nt->link_cleanup);
1023fce8a7bbSJon Mason }
1024fce8a7bbSJon Mason 
1025fce8a7bbSJon Mason static void ntb_transport_link_work(struct work_struct *work)
1026fce8a7bbSJon Mason {
1027e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt =
1028e26a5843SAllen Hubbe 		container_of(work, struct ntb_transport_ctx, link_work.work);
1029e26a5843SAllen Hubbe 	struct ntb_dev *ndev = nt->ndev;
1030e26a5843SAllen Hubbe 	struct pci_dev *pdev = ndev->pdev;
1031e26a5843SAllen Hubbe 	resource_size_t size;
1032fce8a7bbSJon Mason 	u32 val;
103384f76685SDave Jiang 	int rc = 0, i, spad;
1034fce8a7bbSJon Mason 
1035113fc505SJon Mason 	/* send the local info, in the opposite order of the way we read it */
10362b0569b3SLogan Gunthorpe 
10372b0569b3SLogan Gunthorpe 	if (nt->use_msi) {
10382b0569b3SLogan Gunthorpe 		rc = ntb_msi_setup_mws(ndev);
10392b0569b3SLogan Gunthorpe 		if (rc) {
10402b0569b3SLogan Gunthorpe 			dev_warn(&pdev->dev,
10412b0569b3SLogan Gunthorpe 				 "Failed to register MSI memory window: %d\n",
10422b0569b3SLogan Gunthorpe 				 rc);
10432b0569b3SLogan Gunthorpe 			nt->use_msi = false;
10442b0569b3SLogan Gunthorpe 		}
10452b0569b3SLogan Gunthorpe 	}
10462b0569b3SLogan Gunthorpe 
10472b0569b3SLogan Gunthorpe 	for (i = 0; i < nt->qp_count; i++)
10482b0569b3SLogan Gunthorpe 		ntb_transport_setup_qp_msi(nt, i);
10492b0569b3SLogan Gunthorpe 
1050e26a5843SAllen Hubbe 	for (i = 0; i < nt->mw_count; i++) {
1051e26a5843SAllen Hubbe 		size = nt->mw_vec[i].phys_size;
1052e26a5843SAllen Hubbe 
1053e26a5843SAllen Hubbe 		if (max_mw_size && size > max_mw_size)
1054e26a5843SAllen Hubbe 			size = max_mw_size;
1055e26a5843SAllen Hubbe 
1056e26a5843SAllen Hubbe 		spad = MW0_SZ_HIGH + (i * 2);
1057d67288a3SSerge Semin 		ntb_peer_spad_write(ndev, PIDX, spad, upper_32_bits(size));
1058e26a5843SAllen Hubbe 
1059e26a5843SAllen Hubbe 		spad = MW0_SZ_LOW + (i * 2);
1060d67288a3SSerge Semin 		ntb_peer_spad_write(ndev, PIDX, spad, lower_32_bits(size));
1061fce8a7bbSJon Mason 	}
1062fce8a7bbSJon Mason 
1063d67288a3SSerge Semin 	ntb_peer_spad_write(ndev, PIDX, NUM_MWS, nt->mw_count);
1064fce8a7bbSJon Mason 
1065d67288a3SSerge Semin 	ntb_peer_spad_write(ndev, PIDX, NUM_QPS, nt->qp_count);
1066fce8a7bbSJon Mason 
1067d67288a3SSerge Semin 	ntb_peer_spad_write(ndev, PIDX, VERSION, NTB_TRANSPORT_VERSION);
1068fce8a7bbSJon Mason 
1069fce8a7bbSJon Mason 	/* Query the remote side for its info */
10700f69a7dfSDave Jiang 	val = ntb_spad_read(ndev, VERSION);
1071e26a5843SAllen Hubbe 	dev_dbg(&pdev->dev, "Remote version = %d\n", val);
1072fce8a7bbSJon Mason 	if (val != NTB_TRANSPORT_VERSION)
1073fce8a7bbSJon Mason 		goto out;
1074fce8a7bbSJon Mason 
10750f69a7dfSDave Jiang 	val = ntb_spad_read(ndev, NUM_QPS);
1076fce8a7bbSJon Mason 	dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
1077e26a5843SAllen Hubbe 	if (val != nt->qp_count)
1078fce8a7bbSJon Mason 		goto out;
1079fce8a7bbSJon Mason 
10800f69a7dfSDave Jiang 	val = ntb_spad_read(ndev, NUM_MWS);
1081113fc505SJon Mason 	dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
1082e26a5843SAllen Hubbe 	if (val != nt->mw_count)
1083e26a5843SAllen Hubbe 		goto out;
1084fce8a7bbSJon Mason 
1085e26a5843SAllen Hubbe 	for (i = 0; i < nt->mw_count; i++) {
1086113fc505SJon Mason 		u64 val64;
1087fce8a7bbSJon Mason 
10880f69a7dfSDave Jiang 		val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
1089113fc505SJon Mason 		val64 = (u64)val << 32;
1090fce8a7bbSJon Mason 
10910f69a7dfSDave Jiang 		val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
1092113fc505SJon Mason 		val64 |= val;
1093113fc505SJon Mason 
1094e26a5843SAllen Hubbe 		dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
1095113fc505SJon Mason 
1096113fc505SJon Mason 		rc = ntb_set_mw(nt, i, val64);
1097fce8a7bbSJon Mason 		if (rc)
1098113fc505SJon Mason 			goto out1;
1099113fc505SJon Mason 	}
1100fce8a7bbSJon Mason 
1101e26a5843SAllen Hubbe 	nt->link_is_up = true;
1102fce8a7bbSJon Mason 
1103e26a5843SAllen Hubbe 	for (i = 0; i < nt->qp_count; i++) {
1104e26a5843SAllen Hubbe 		struct ntb_transport_qp *qp = &nt->qp_vec[i];
1105fce8a7bbSJon Mason 
1106fce8a7bbSJon Mason 		ntb_transport_setup_qp_mw(nt, i);
11072b0569b3SLogan Gunthorpe 		ntb_transport_setup_qp_peer_msi(nt, i);
1108fce8a7bbSJon Mason 
1109e26a5843SAllen Hubbe 		if (qp->client_ready)
1110fce8a7bbSJon Mason 			schedule_delayed_work(&qp->link_work, 0);
1111fce8a7bbSJon Mason 	}
1112fce8a7bbSJon Mason 
1113fce8a7bbSJon Mason 	return;
1114fce8a7bbSJon Mason 
1115113fc505SJon Mason out1:
1116e26a5843SAllen Hubbe 	for (i = 0; i < nt->mw_count; i++)
1117113fc505SJon Mason 		ntb_free_mw(nt, i);
111884f76685SDave Jiang 
111984f76685SDave Jiang 	/* if there's an actual failure, we should just bail */
1120f3fd2afeSDave Jiang 	if (rc < 0)
112184f76685SDave Jiang 		return;
112284f76685SDave Jiang 
1123fce8a7bbSJon Mason out:
1124e26a5843SAllen Hubbe 	if (ntb_link_is_up(ndev, NULL, NULL) == 1)
1125fce8a7bbSJon Mason 		schedule_delayed_work(&nt->link_work,
1126fce8a7bbSJon Mason 				      msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
1127fce8a7bbSJon Mason }
1128fce8a7bbSJon Mason 
1129fce8a7bbSJon Mason static void ntb_qp_link_work(struct work_struct *work)
1130fce8a7bbSJon Mason {
1131fce8a7bbSJon Mason 	struct ntb_transport_qp *qp = container_of(work,
1132fce8a7bbSJon Mason 						   struct ntb_transport_qp,
1133fce8a7bbSJon Mason 						   link_work.work);
1134e26a5843SAllen Hubbe 	struct pci_dev *pdev = qp->ndev->pdev;
1135e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt = qp->transport;
1136e26a5843SAllen Hubbe 	int val;
1137fce8a7bbSJon Mason 
1138e26a5843SAllen Hubbe 	WARN_ON(!nt->link_is_up);
1139fce8a7bbSJon Mason 
1140e26a5843SAllen Hubbe 	val = ntb_spad_read(nt->ndev, QP_LINKS);
1141fce8a7bbSJon Mason 
1142d67288a3SSerge Semin 	ntb_peer_spad_write(nt->ndev, PIDX, QP_LINKS, val | BIT(qp->qp_num));
1143fce8a7bbSJon Mason 
1144fce8a7bbSJon Mason 	/* query remote spad for qp ready bits */
114528762289SAllen Hubbe 	dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
1146fce8a7bbSJon Mason 
1147fce8a7bbSJon Mason 	/* See if the remote side is up */
1148e26a5843SAllen Hubbe 	if (val & BIT(qp->qp_num)) {
1149fce8a7bbSJon Mason 		dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
1150e26a5843SAllen Hubbe 		qp->link_is_up = true;
1151e9021331SDave Jiang 		qp->active = true;
1152e26a5843SAllen Hubbe 
1153fce8a7bbSJon Mason 		if (qp->event_handler)
1154e26a5843SAllen Hubbe 			qp->event_handler(qp->cb_data, qp->link_is_up);
11558b5a22d8SAllen Hubbe 
1156e9021331SDave Jiang 		if (qp->active)
11578b5a22d8SAllen Hubbe 			tasklet_schedule(&qp->rxc_db_work);
1158e26a5843SAllen Hubbe 	} else if (nt->link_is_up)
1159fce8a7bbSJon Mason 		schedule_delayed_work(&qp->link_work,
1160fce8a7bbSJon Mason 				      msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
1161fce8a7bbSJon Mason }
1162fce8a7bbSJon Mason 
1163e26a5843SAllen Hubbe static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
1164fce8a7bbSJon Mason 				    unsigned int qp_num)
1165fce8a7bbSJon Mason {
1166fce8a7bbSJon Mason 	struct ntb_transport_qp *qp;
1167e26a5843SAllen Hubbe 	phys_addr_t mw_base;
1168e26a5843SAllen Hubbe 	resource_size_t mw_size;
1169ef114ed5SJon Mason 	unsigned int num_qps_mw, tx_size;
1170e26a5843SAllen Hubbe 	unsigned int mw_num, mw_count, qp_count;
1171282a2feeSJon Mason 	u64 qp_offset;
1172948d3a65SJon Mason 
1173e26a5843SAllen Hubbe 	mw_count = nt->mw_count;
1174e26a5843SAllen Hubbe 	qp_count = nt->qp_count;
1175fce8a7bbSJon Mason 
1176e26a5843SAllen Hubbe 	mw_num = QP_TO_MW(nt, qp_num);
1177e26a5843SAllen Hubbe 
1178e26a5843SAllen Hubbe 	qp = &nt->qp_vec[qp_num];
1179fce8a7bbSJon Mason 	qp->qp_num = qp_num;
1180fce8a7bbSJon Mason 	qp->transport = nt;
1181fce8a7bbSJon Mason 	qp->ndev = nt->ndev;
1182e26a5843SAllen Hubbe 	qp->client_ready = false;
1183fce8a7bbSJon Mason 	qp->event_handler = NULL;
1184cc79bd27SDave Jiang 	ntb_qp_link_context_reset(qp);
1185fce8a7bbSJon Mason 
11868e8496e0SLogan Gunthorpe 	if (mw_num < qp_count % mw_count)
1187e26a5843SAllen Hubbe 		num_qps_mw = qp_count / mw_count + 1;
1188ef114ed5SJon Mason 	else
1189e26a5843SAllen Hubbe 		num_qps_mw = qp_count / mw_count;
1190ef114ed5SJon Mason 
1191e26a5843SAllen Hubbe 	mw_base = nt->mw_vec[mw_num].phys_addr;
1192e26a5843SAllen Hubbe 	mw_size = nt->mw_vec[mw_num].phys_size;
1193e26a5843SAllen Hubbe 
1194cbd27448SLogan Gunthorpe 	if (max_mw_size && mw_size > max_mw_size)
1195cbd27448SLogan Gunthorpe 		mw_size = max_mw_size;
1196cbd27448SLogan Gunthorpe 
1197e26a5843SAllen Hubbe 	tx_size = (unsigned int)mw_size / num_qps_mw;
1198c92ba3c5SJon Mason 	qp_offset = tx_size * (qp_num / mw_count);
1199e26a5843SAllen Hubbe 
1200c59666bbSLogan Gunthorpe 	qp->tx_mw_size = tx_size;
1201e26a5843SAllen Hubbe 	qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
1202282a2feeSJon Mason 	if (!qp->tx_mw)
1203282a2feeSJon Mason 		return -EINVAL;
1204793c20e9SJon Mason 
1205e26a5843SAllen Hubbe 	qp->tx_mw_phys = mw_base + qp_offset;
1206282a2feeSJon Mason 	if (!qp->tx_mw_phys)
1207282a2feeSJon Mason 		return -EINVAL;
1208282a2feeSJon Mason 
1209282a2feeSJon Mason 	tx_size -= sizeof(struct ntb_rx_info);
1210282a2feeSJon Mason 	qp->rx_info = qp->tx_mw + tx_size;
1211282a2feeSJon Mason 
1212c9d534c8SJon Mason 	/* Due to housekeeping, there must be atleast 2 buffs */
1213c9d534c8SJon Mason 	qp->tx_max_frame = min(transport_mtu, tx_size / 2);
1214793c20e9SJon Mason 	qp->tx_max_entry = tx_size / qp->tx_max_frame;
1215ef114ed5SJon Mason 
1216c8650fd0SDave Jiang 	if (nt->debugfs_node_dir) {
1217fce8a7bbSJon Mason 		char debugfs_name[4];
1218fce8a7bbSJon Mason 
1219fce8a7bbSJon Mason 		snprintf(debugfs_name, 4, "qp%d", qp_num);
1220fce8a7bbSJon Mason 		qp->debugfs_dir = debugfs_create_dir(debugfs_name,
1221c8650fd0SDave Jiang 						     nt->debugfs_node_dir);
1222fce8a7bbSJon Mason 
1223fce8a7bbSJon Mason 		qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
1224fce8a7bbSJon Mason 							qp->debugfs_dir, qp,
1225fce8a7bbSJon Mason 							&ntb_qp_debugfs_stats);
1226e26a5843SAllen Hubbe 	} else {
1227e26a5843SAllen Hubbe 		qp->debugfs_dir = NULL;
1228e26a5843SAllen Hubbe 		qp->debugfs_stats = NULL;
1229fce8a7bbSJon Mason 	}
1230fce8a7bbSJon Mason 
1231fce8a7bbSJon Mason 	INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
1232fca4d518SJon Mason 	INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
1233fce8a7bbSJon Mason 
1234da2e5ae5SAllen Hubbe 	spin_lock_init(&qp->ntb_rx_q_lock);
1235fce8a7bbSJon Mason 	spin_lock_init(&qp->ntb_tx_free_q_lock);
1236fce8a7bbSJon Mason 
1237da2e5ae5SAllen Hubbe 	INIT_LIST_HEAD(&qp->rx_post_q);
1238fce8a7bbSJon Mason 	INIT_LIST_HEAD(&qp->rx_pend_q);
1239fce8a7bbSJon Mason 	INIT_LIST_HEAD(&qp->rx_free_q);
1240fce8a7bbSJon Mason 	INIT_LIST_HEAD(&qp->tx_free_q);
1241282a2feeSJon Mason 
1242e26a5843SAllen Hubbe 	tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1243e26a5843SAllen Hubbe 		     (unsigned long)qp);
1244e26a5843SAllen Hubbe 
1245282a2feeSJon Mason 	return 0;
1246fce8a7bbSJon Mason }
1247fce8a7bbSJon Mason 
1248e26a5843SAllen Hubbe static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
1249fce8a7bbSJon Mason {
1250e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt;
1251e26a5843SAllen Hubbe 	struct ntb_transport_mw *mw;
1252b17faba0SShyam Sundar S K 	unsigned int mw_count, qp_count, spad_count, max_mw_count_for_spads;
1253e26a5843SAllen Hubbe 	u64 qp_bitmap;
12541199aa61SAllen Hubbe 	int node;
1255fce8a7bbSJon Mason 	int rc, i;
1256fce8a7bbSJon Mason 
1257bc240eecSLogan Gunthorpe 	mw_count = ntb_peer_mw_count(ndev);
1258443b9a14SSerge Semin 
1259443b9a14SSerge Semin 	if (!ndev->ops->mw_set_trans) {
1260443b9a14SSerge Semin 		dev_err(&ndev->dev, "Inbound MW based NTB API is required\n");
1261443b9a14SSerge Semin 		return -EINVAL;
1262443b9a14SSerge Semin 	}
126319645a07SLogan Gunthorpe 
1264e26a5843SAllen Hubbe 	if (ntb_db_is_unsafe(ndev))
1265e26a5843SAllen Hubbe 		dev_dbg(&ndev->dev,
1266e26a5843SAllen Hubbe 			"doorbell is unsafe, proceed anyway...\n");
1267e26a5843SAllen Hubbe 	if (ntb_spad_is_unsafe(ndev))
1268e26a5843SAllen Hubbe 		dev_dbg(&ndev->dev,
1269e26a5843SAllen Hubbe 			"scratchpad is unsafe, proceed anyway...\n");
1270e26a5843SAllen Hubbe 
12711e530119SSerge Semin 	if (ntb_peer_port_count(ndev) != NTB_DEF_PEER_CNT)
12721e530119SSerge Semin 		dev_warn(&ndev->dev, "Multi-port NTB devices unsupported\n");
12731e530119SSerge Semin 
12741199aa61SAllen Hubbe 	node = dev_to_node(&ndev->dev);
12751199aa61SAllen Hubbe 
12761199aa61SAllen Hubbe 	nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
1277fce8a7bbSJon Mason 	if (!nt)
1278fce8a7bbSJon Mason 		return -ENOMEM;
1279fce8a7bbSJon Mason 
1280e26a5843SAllen Hubbe 	nt->ndev = ndev;
12812b0569b3SLogan Gunthorpe 
12822b0569b3SLogan Gunthorpe 	/*
12832b0569b3SLogan Gunthorpe 	 * If we are using MSI, and have at least one extra memory window,
12842b0569b3SLogan Gunthorpe 	 * we will reserve the last MW for the MSI window.
12852b0569b3SLogan Gunthorpe 	 */
12862b0569b3SLogan Gunthorpe 	if (use_msi && mw_count > 1) {
12872b0569b3SLogan Gunthorpe 		rc = ntb_msi_init(ndev, ntb_transport_msi_desc_changed);
12882b0569b3SLogan Gunthorpe 		if (!rc) {
12892b0569b3SLogan Gunthorpe 			mw_count -= 1;
12902b0569b3SLogan Gunthorpe 			nt->use_msi = true;
12912b0569b3SLogan Gunthorpe 		}
12922b0569b3SLogan Gunthorpe 	}
12932b0569b3SLogan Gunthorpe 
1294b17faba0SShyam Sundar S K 	spad_count = ntb_spad_count(ndev);
1295e26a5843SAllen Hubbe 
1296b17faba0SShyam Sundar S K 	/* Limit the MW's based on the availability of scratchpads */
1297b17faba0SShyam Sundar S K 
1298b17faba0SShyam Sundar S K 	if (spad_count < NTB_TRANSPORT_MIN_SPADS) {
1299b17faba0SShyam Sundar S K 		nt->mw_count = 0;
1300b17faba0SShyam Sundar S K 		rc = -EINVAL;
1301b17faba0SShyam Sundar S K 		goto err;
1302b17faba0SShyam Sundar S K 	}
1303b17faba0SShyam Sundar S K 
1304b17faba0SShyam Sundar S K 	max_mw_count_for_spads = (spad_count - MW0_SZ_HIGH) / 2;
1305b17faba0SShyam Sundar S K 	nt->mw_count = min(mw_count, max_mw_count_for_spads);
1306e26a5843SAllen Hubbe 
13072b0569b3SLogan Gunthorpe 	nt->msi_spad_offset = nt->mw_count * 2 + MW0_SZ_HIGH;
13082b0569b3SLogan Gunthorpe 
1309590b5b7dSKees Cook 	nt->mw_vec = kcalloc_node(mw_count, sizeof(*nt->mw_vec),
13101199aa61SAllen Hubbe 				  GFP_KERNEL, node);
1311e26a5843SAllen Hubbe 	if (!nt->mw_vec) {
1312e26a5843SAllen Hubbe 		rc = -ENOMEM;
1313fce8a7bbSJon Mason 		goto err;
1314fce8a7bbSJon Mason 	}
1315fce8a7bbSJon Mason 
1316e26a5843SAllen Hubbe 	for (i = 0; i < mw_count; i++) {
1317e26a5843SAllen Hubbe 		mw = &nt->mw_vec[i];
1318e26a5843SAllen Hubbe 
1319443b9a14SSerge Semin 		rc = ntb_peer_mw_get_addr(ndev, i, &mw->phys_addr,
1320443b9a14SSerge Semin 					  &mw->phys_size);
1321e26a5843SAllen Hubbe 		if (rc)
1322e26a5843SAllen Hubbe 			goto err1;
1323e26a5843SAllen Hubbe 
132406917f75SDave Jiang 		mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
1325e26a5843SAllen Hubbe 		if (!mw->vbase) {
1326948d3a65SJon Mason 			rc = -ENOMEM;
1327948d3a65SJon Mason 			goto err1;
1328948d3a65SJon Mason 		}
1329948d3a65SJon Mason 
1330e26a5843SAllen Hubbe 		mw->buff_size = 0;
1331e26a5843SAllen Hubbe 		mw->xlat_size = 0;
1332e26a5843SAllen Hubbe 		mw->virt_addr = NULL;
1333e26a5843SAllen Hubbe 		mw->dma_addr = 0;
1334e26a5843SAllen Hubbe 	}
1335fce8a7bbSJon Mason 
1336e26a5843SAllen Hubbe 	qp_bitmap = ntb_db_valid_mask(ndev);
1337e26a5843SAllen Hubbe 
1338e26a5843SAllen Hubbe 	qp_count = ilog2(qp_bitmap);
13392b0569b3SLogan Gunthorpe 	if (nt->use_msi) {
13402b0569b3SLogan Gunthorpe 		qp_count -= 1;
13412b0569b3SLogan Gunthorpe 		nt->msi_db_mask = 1 << qp_count;
13422b0569b3SLogan Gunthorpe 		ntb_db_clear_mask(ndev, nt->msi_db_mask);
13432b0569b3SLogan Gunthorpe 	}
13442b0569b3SLogan Gunthorpe 
1345e26a5843SAllen Hubbe 	if (max_num_clients && max_num_clients < qp_count)
1346e26a5843SAllen Hubbe 		qp_count = max_num_clients;
1347cb827ee6SLogan Gunthorpe 	else if (nt->mw_count < qp_count)
1348cb827ee6SLogan Gunthorpe 		qp_count = nt->mw_count;
1349e26a5843SAllen Hubbe 
1350e26a5843SAllen Hubbe 	qp_bitmap &= BIT_ULL(qp_count) - 1;
1351e26a5843SAllen Hubbe 
1352e26a5843SAllen Hubbe 	nt->qp_count = qp_count;
1353e26a5843SAllen Hubbe 	nt->qp_bitmap = qp_bitmap;
1354e26a5843SAllen Hubbe 	nt->qp_bitmap_free = qp_bitmap;
1355e26a5843SAllen Hubbe 
1356590b5b7dSKees Cook 	nt->qp_vec = kcalloc_node(qp_count, sizeof(*nt->qp_vec),
13571199aa61SAllen Hubbe 				  GFP_KERNEL, node);
1358e26a5843SAllen Hubbe 	if (!nt->qp_vec) {
1359fce8a7bbSJon Mason 		rc = -ENOMEM;
1360d4adee09SSudip Mukherjee 		goto err1;
1361fce8a7bbSJon Mason 	}
1362fce8a7bbSJon Mason 
1363c8650fd0SDave Jiang 	if (nt_debugfs_dir) {
1364c8650fd0SDave Jiang 		nt->debugfs_node_dir =
1365c8650fd0SDave Jiang 			debugfs_create_dir(pci_name(ndev->pdev),
1366c8650fd0SDave Jiang 					   nt_debugfs_dir);
1367c8650fd0SDave Jiang 	}
1368c8650fd0SDave Jiang 
1369e26a5843SAllen Hubbe 	for (i = 0; i < qp_count; i++) {
1370282a2feeSJon Mason 		rc = ntb_transport_init_queue(nt, i);
1371282a2feeSJon Mason 		if (rc)
1372d4adee09SSudip Mukherjee 			goto err2;
1373282a2feeSJon Mason 	}
1374fce8a7bbSJon Mason 
1375fce8a7bbSJon Mason 	INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
1376fca4d518SJon Mason 	INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
1377fce8a7bbSJon Mason 
1378e26a5843SAllen Hubbe 	rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
1379fce8a7bbSJon Mason 	if (rc)
1380d4adee09SSudip Mukherjee 		goto err2;
1381fce8a7bbSJon Mason 
1382fce8a7bbSJon Mason 	INIT_LIST_HEAD(&nt->client_devs);
1383fce8a7bbSJon Mason 	rc = ntb_bus_init(nt);
1384fce8a7bbSJon Mason 	if (rc)
1385d4adee09SSudip Mukherjee 		goto err3;
1386fce8a7bbSJon Mason 
1387e26a5843SAllen Hubbe 	nt->link_is_up = false;
1388e26a5843SAllen Hubbe 	ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1389e26a5843SAllen Hubbe 	ntb_link_event(ndev);
1390fce8a7bbSJon Mason 
1391fce8a7bbSJon Mason 	return 0;
1392fce8a7bbSJon Mason 
1393948d3a65SJon Mason err3:
1394d4adee09SSudip Mukherjee 	ntb_clear_ctx(ndev);
1395948d3a65SJon Mason err2:
1396d4adee09SSudip Mukherjee 	kfree(nt->qp_vec);
1397fce8a7bbSJon Mason err1:
1398e26a5843SAllen Hubbe 	while (i--) {
1399e26a5843SAllen Hubbe 		mw = &nt->mw_vec[i];
1400e26a5843SAllen Hubbe 		iounmap(mw->vbase);
1401e26a5843SAllen Hubbe 	}
1402d4adee09SSudip Mukherjee 	kfree(nt->mw_vec);
1403fce8a7bbSJon Mason err:
1404fce8a7bbSJon Mason 	kfree(nt);
1405fce8a7bbSJon Mason 	return rc;
1406fce8a7bbSJon Mason }
1407fce8a7bbSJon Mason 
1408e26a5843SAllen Hubbe static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
1409fce8a7bbSJon Mason {
1410e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt = ndev->ctx;
1411e26a5843SAllen Hubbe 	struct ntb_transport_qp *qp;
1412e26a5843SAllen Hubbe 	u64 qp_bitmap_alloc;
1413fce8a7bbSJon Mason 	int i;
1414fce8a7bbSJon Mason 
1415fca4d518SJon Mason 	ntb_transport_link_cleanup(nt);
1416e26a5843SAllen Hubbe 	cancel_work_sync(&nt->link_cleanup);
1417e26a5843SAllen Hubbe 	cancel_delayed_work_sync(&nt->link_work);
1418e26a5843SAllen Hubbe 
1419e26a5843SAllen Hubbe 	qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
1420fce8a7bbSJon Mason 
1421fce8a7bbSJon Mason 	/* verify that all the qp's are freed */
1422e26a5843SAllen Hubbe 	for (i = 0; i < nt->qp_count; i++) {
1423e26a5843SAllen Hubbe 		qp = &nt->qp_vec[i];
1424e26a5843SAllen Hubbe 		if (qp_bitmap_alloc & BIT_ULL(i))
1425e26a5843SAllen Hubbe 			ntb_transport_free_queue(qp);
1426e26a5843SAllen Hubbe 		debugfs_remove_recursive(qp->debugfs_dir);
14271517a3f2SJon Mason 	}
1428fce8a7bbSJon Mason 
1429e26a5843SAllen Hubbe 	ntb_link_disable(ndev);
1430e26a5843SAllen Hubbe 	ntb_clear_ctx(ndev);
1431e26a5843SAllen Hubbe 
1432fce8a7bbSJon Mason 	ntb_bus_remove(nt);
1433fce8a7bbSJon Mason 
1434e26a5843SAllen Hubbe 	for (i = nt->mw_count; i--; ) {
1435113fc505SJon Mason 		ntb_free_mw(nt, i);
1436e26a5843SAllen Hubbe 		iounmap(nt->mw_vec[i].vbase);
1437e26a5843SAllen Hubbe 	}
1438fce8a7bbSJon Mason 
1439e26a5843SAllen Hubbe 	kfree(nt->qp_vec);
1440e26a5843SAllen Hubbe 	kfree(nt->mw_vec);
1441fce8a7bbSJon Mason 	kfree(nt);
1442fce8a7bbSJon Mason }
1443fce8a7bbSJon Mason 
1444da2e5ae5SAllen Hubbe static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1445fce8a7bbSJon Mason {
1446da2e5ae5SAllen Hubbe 	struct ntb_queue_entry *entry;
1447da2e5ae5SAllen Hubbe 	void *cb_data;
1448da2e5ae5SAllen Hubbe 	unsigned int len;
1449da2e5ae5SAllen Hubbe 	unsigned long irqflags;
1450448c6fb3SJon Mason 
1451da2e5ae5SAllen Hubbe 	spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1452282a2feeSJon Mason 
1453da2e5ae5SAllen Hubbe 	while (!list_empty(&qp->rx_post_q)) {
1454da2e5ae5SAllen Hubbe 		entry = list_first_entry(&qp->rx_post_q,
1455da2e5ae5SAllen Hubbe 					 struct ntb_queue_entry, entry);
1456da2e5ae5SAllen Hubbe 		if (!(entry->flags & DESC_DONE_FLAG))
1457da2e5ae5SAllen Hubbe 			break;
1458da2e5ae5SAllen Hubbe 
1459da2e5ae5SAllen Hubbe 		entry->rx_hdr->flags = 0;
146072203572SDave Jiang 		iowrite32(entry->rx_index, &qp->rx_info->entry);
1461fce8a7bbSJon Mason 
1462da2e5ae5SAllen Hubbe 		cb_data = entry->cb_data;
1463da2e5ae5SAllen Hubbe 		len = entry->len;
1464da2e5ae5SAllen Hubbe 
1465da2e5ae5SAllen Hubbe 		list_move_tail(&entry->entry, &qp->rx_free_q);
1466da2e5ae5SAllen Hubbe 
1467da2e5ae5SAllen Hubbe 		spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1468448c6fb3SJon Mason 
1469e26a5843SAllen Hubbe 		if (qp->rx_handler && qp->client_ready)
1470448c6fb3SJon Mason 			qp->rx_handler(qp, qp->cb_data, cb_data, len);
1471da2e5ae5SAllen Hubbe 
1472da2e5ae5SAllen Hubbe 		spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1473da2e5ae5SAllen Hubbe 	}
1474da2e5ae5SAllen Hubbe 
1475da2e5ae5SAllen Hubbe 	spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1476da2e5ae5SAllen Hubbe }
1477da2e5ae5SAllen Hubbe 
147872203572SDave Jiang static void ntb_rx_copy_callback(void *data,
147972203572SDave Jiang 				 const struct dmaengine_result *res)
1480da2e5ae5SAllen Hubbe {
1481da2e5ae5SAllen Hubbe 	struct ntb_queue_entry *entry = data;
1482da2e5ae5SAllen Hubbe 
148372203572SDave Jiang 	/* we need to check DMA results if we are using DMA */
148472203572SDave Jiang 	if (res) {
148572203572SDave Jiang 		enum dmaengine_tx_result dma_err = res->result;
148672203572SDave Jiang 
148772203572SDave Jiang 		switch (dma_err) {
148872203572SDave Jiang 		case DMA_TRANS_READ_FAILED:
148972203572SDave Jiang 		case DMA_TRANS_WRITE_FAILED:
149072203572SDave Jiang 			entry->errors++;
1491df561f66SGustavo A. R. Silva 			fallthrough;
149272203572SDave Jiang 		case DMA_TRANS_ABORTED:
149372203572SDave Jiang 		{
149472203572SDave Jiang 			struct ntb_transport_qp *qp = entry->qp;
149572203572SDave Jiang 			void *offset = qp->rx_buff + qp->rx_max_frame *
149672203572SDave Jiang 					qp->rx_index;
149772203572SDave Jiang 
149872203572SDave Jiang 			ntb_memcpy_rx(entry, offset);
149972203572SDave Jiang 			qp->rx_memcpy++;
150072203572SDave Jiang 			return;
150172203572SDave Jiang 		}
150272203572SDave Jiang 
150372203572SDave Jiang 		case DMA_TRANS_NOERROR:
150472203572SDave Jiang 		default:
150572203572SDave Jiang 			break;
150672203572SDave Jiang 		}
150772203572SDave Jiang 	}
150872203572SDave Jiang 
1509da2e5ae5SAllen Hubbe 	entry->flags |= DESC_DONE_FLAG;
1510da2e5ae5SAllen Hubbe 
1511da2e5ae5SAllen Hubbe 	ntb_complete_rxc(entry->qp);
1512fce8a7bbSJon Mason }
1513fce8a7bbSJon Mason 
1514282a2feeSJon Mason static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1515282a2feeSJon Mason {
1516282a2feeSJon Mason 	void *buf = entry->buf;
1517282a2feeSJon Mason 	size_t len = entry->len;
1518282a2feeSJon Mason 
1519282a2feeSJon Mason 	memcpy(buf, offset, len);
1520282a2feeSJon Mason 
1521e26a5843SAllen Hubbe 	/* Ensure that the data is fully copied out before clearing the flag */
1522e26a5843SAllen Hubbe 	wmb();
1523e26a5843SAllen Hubbe 
152472203572SDave Jiang 	ntb_rx_copy_callback(entry, NULL);
1525282a2feeSJon Mason }
1526282a2feeSJon Mason 
152772203572SDave Jiang static int ntb_async_rx_submit(struct ntb_queue_entry *entry, void *offset)
1528282a2feeSJon Mason {
1529282a2feeSJon Mason 	struct dma_async_tx_descriptor *txd;
1530282a2feeSJon Mason 	struct ntb_transport_qp *qp = entry->qp;
1531569410caSDave Jiang 	struct dma_chan *chan = qp->rx_dma_chan;
1532282a2feeSJon Mason 	struct dma_device *device;
1533da2e5ae5SAllen Hubbe 	size_t pay_off, buff_off, len;
15346f57fd05SBartlomiej Zolnierkiewicz 	struct dmaengine_unmap_data *unmap;
1535282a2feeSJon Mason 	dma_cookie_t cookie;
1536282a2feeSJon Mason 	void *buf = entry->buf;
1537282a2feeSJon Mason 
1538da2e5ae5SAllen Hubbe 	len = entry->len;
1539282a2feeSJon Mason 	device = chan->device;
1540282a2feeSJon Mason 	pay_off = (size_t)offset & ~PAGE_MASK;
1541282a2feeSJon Mason 	buff_off = (size_t)buf & ~PAGE_MASK;
1542282a2feeSJon Mason 
1543282a2feeSJon Mason 	if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
1544905921e7SAllen Hubbe 		goto err;
1545282a2feeSJon Mason 
15466f57fd05SBartlomiej Zolnierkiewicz 	unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
15476f57fd05SBartlomiej Zolnierkiewicz 	if (!unmap)
1548905921e7SAllen Hubbe 		goto err;
1549282a2feeSJon Mason 
15506f57fd05SBartlomiej Zolnierkiewicz 	unmap->len = len;
15516f57fd05SBartlomiej Zolnierkiewicz 	unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
15526f57fd05SBartlomiej Zolnierkiewicz 				      pay_off, len, DMA_TO_DEVICE);
15536f57fd05SBartlomiej Zolnierkiewicz 	if (dma_mapping_error(device->dev, unmap->addr[0]))
15546f57fd05SBartlomiej Zolnierkiewicz 		goto err_get_unmap;
1555282a2feeSJon Mason 
15566f57fd05SBartlomiej Zolnierkiewicz 	unmap->to_cnt = 1;
15576f57fd05SBartlomiej Zolnierkiewicz 
15586f57fd05SBartlomiej Zolnierkiewicz 	unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
15596f57fd05SBartlomiej Zolnierkiewicz 				      buff_off, len, DMA_FROM_DEVICE);
15606f57fd05SBartlomiej Zolnierkiewicz 	if (dma_mapping_error(device->dev, unmap->addr[1]))
15616f57fd05SBartlomiej Zolnierkiewicz 		goto err_get_unmap;
15626f57fd05SBartlomiej Zolnierkiewicz 
15636f57fd05SBartlomiej Zolnierkiewicz 	unmap->from_cnt = 1;
15646f57fd05SBartlomiej Zolnierkiewicz 
156588931ec3SAllen Hubbe 	txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
15660776ae7bSBartlomiej Zolnierkiewicz 					     unmap->addr[0], len,
15670776ae7bSBartlomiej Zolnierkiewicz 					     DMA_PREP_INTERRUPT);
156888931ec3SAllen Hubbe 	if (!txd)
15696f57fd05SBartlomiej Zolnierkiewicz 		goto err_get_unmap;
1570282a2feeSJon Mason 
157172203572SDave Jiang 	txd->callback_result = ntb_rx_copy_callback;
1572282a2feeSJon Mason 	txd->callback_param = entry;
15736f57fd05SBartlomiej Zolnierkiewicz 	dma_set_unmap(txd, unmap);
1574282a2feeSJon Mason 
1575282a2feeSJon Mason 	cookie = dmaengine_submit(txd);
1576282a2feeSJon Mason 	if (dma_submit_error(cookie))
15776f57fd05SBartlomiej Zolnierkiewicz 		goto err_set_unmap;
15786f57fd05SBartlomiej Zolnierkiewicz 
15796f57fd05SBartlomiej Zolnierkiewicz 	dmaengine_unmap_put(unmap);
1580282a2feeSJon Mason 
1581282a2feeSJon Mason 	qp->last_cookie = cookie;
1582282a2feeSJon Mason 
1583282a2feeSJon Mason 	qp->rx_async++;
1584282a2feeSJon Mason 
158572203572SDave Jiang 	return 0;
1586282a2feeSJon Mason 
15876f57fd05SBartlomiej Zolnierkiewicz err_set_unmap:
15886f57fd05SBartlomiej Zolnierkiewicz 	dmaengine_unmap_put(unmap);
15896f57fd05SBartlomiej Zolnierkiewicz err_get_unmap:
15906f57fd05SBartlomiej Zolnierkiewicz 	dmaengine_unmap_put(unmap);
1591282a2feeSJon Mason err:
159272203572SDave Jiang 	return -ENXIO;
159372203572SDave Jiang }
159472203572SDave Jiang 
159572203572SDave Jiang static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
159672203572SDave Jiang {
159772203572SDave Jiang 	struct ntb_transport_qp *qp = entry->qp;
159872203572SDave Jiang 	struct dma_chan *chan = qp->rx_dma_chan;
159972203572SDave Jiang 	int res;
160072203572SDave Jiang 
160172203572SDave Jiang 	if (!chan)
160272203572SDave Jiang 		goto err;
160372203572SDave Jiang 
160472203572SDave Jiang 	if (entry->len < copy_bytes)
160572203572SDave Jiang 		goto err;
160672203572SDave Jiang 
160772203572SDave Jiang 	res = ntb_async_rx_submit(entry, offset);
160872203572SDave Jiang 	if (res < 0)
160972203572SDave Jiang 		goto err;
161072203572SDave Jiang 
161172203572SDave Jiang 	if (!entry->retries)
161272203572SDave Jiang 		qp->rx_async++;
161372203572SDave Jiang 
161472203572SDave Jiang 	return;
161572203572SDave Jiang 
161672203572SDave Jiang err:
1617282a2feeSJon Mason 	ntb_memcpy_rx(entry, offset);
1618282a2feeSJon Mason 	qp->rx_memcpy++;
1619282a2feeSJon Mason }
1620282a2feeSJon Mason 
1621fce8a7bbSJon Mason static int ntb_process_rxc(struct ntb_transport_qp *qp)
1622fce8a7bbSJon Mason {
1623fce8a7bbSJon Mason 	struct ntb_payload_header *hdr;
1624fce8a7bbSJon Mason 	struct ntb_queue_entry *entry;
1625fce8a7bbSJon Mason 	void *offset;
1626fce8a7bbSJon Mason 
1627793c20e9SJon Mason 	offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1628793c20e9SJon Mason 	hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1629793c20e9SJon Mason 
1630e26a5843SAllen Hubbe 	dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1631e26a5843SAllen Hubbe 		qp->qp_num, hdr->ver, hdr->len, hdr->flags);
1632fce8a7bbSJon Mason 
1633fce8a7bbSJon Mason 	if (!(hdr->flags & DESC_DONE_FLAG)) {
1634e26a5843SAllen Hubbe 		dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
1635fce8a7bbSJon Mason 		qp->rx_ring_empty++;
1636fce8a7bbSJon Mason 		return -EAGAIN;
1637fce8a7bbSJon Mason 	}
1638fce8a7bbSJon Mason 
1639e26a5843SAllen Hubbe 	if (hdr->flags & LINK_DOWN_FLAG) {
1640e26a5843SAllen Hubbe 		dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1641e26a5843SAllen Hubbe 		ntb_qp_link_down(qp);
1642e26a5843SAllen Hubbe 		hdr->flags = 0;
1643c0900b33SAllen Hubbe 		return -EAGAIN;
1644e26a5843SAllen Hubbe 	}
1645e26a5843SAllen Hubbe 
164674465645SJon Mason 	if (hdr->ver != (u32)qp->rx_pkts) {
1647e26a5843SAllen Hubbe 		dev_dbg(&qp->ndev->pdev->dev,
1648e26a5843SAllen Hubbe 			"version mismatch, expected %llu - got %u\n",
1649e26a5843SAllen Hubbe 			qp->rx_pkts, hdr->ver);
1650fce8a7bbSJon Mason 		qp->rx_err_ver++;
1651fce8a7bbSJon Mason 		return -EIO;
1652fce8a7bbSJon Mason 	}
1653fce8a7bbSJon Mason 
1654da2e5ae5SAllen Hubbe 	entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
1655e26a5843SAllen Hubbe 	if (!entry) {
1656e26a5843SAllen Hubbe 		dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1657e26a5843SAllen Hubbe 		qp->rx_err_no_buf++;
1658da2e5ae5SAllen Hubbe 		return -EAGAIN;
1659fce8a7bbSJon Mason 	}
1660fce8a7bbSJon Mason 
1661da2e5ae5SAllen Hubbe 	entry->rx_hdr = hdr;
166272203572SDave Jiang 	entry->rx_index = qp->rx_index;
1663da2e5ae5SAllen Hubbe 
1664e26a5843SAllen Hubbe 	if (hdr->len > entry->len) {
1665e26a5843SAllen Hubbe 		dev_dbg(&qp->ndev->pdev->dev,
1666e26a5843SAllen Hubbe 			"receive buffer overflow! Wanted %d got %d\n",
1667e26a5843SAllen Hubbe 			hdr->len, entry->len);
1668e26a5843SAllen Hubbe 		qp->rx_err_oflow++;
1669e26a5843SAllen Hubbe 
1670da2e5ae5SAllen Hubbe 		entry->len = -EIO;
1671da2e5ae5SAllen Hubbe 		entry->flags |= DESC_DONE_FLAG;
1672e26a5843SAllen Hubbe 
1673da2e5ae5SAllen Hubbe 		ntb_complete_rxc(qp);
1674da2e5ae5SAllen Hubbe 	} else {
1675e26a5843SAllen Hubbe 		dev_dbg(&qp->ndev->pdev->dev,
1676e26a5843SAllen Hubbe 			"RX OK index %u ver %u size %d into buf size %d\n",
1677793c20e9SJon Mason 			qp->rx_index, hdr->ver, hdr->len, entry->len);
1678fce8a7bbSJon Mason 
1679282a2feeSJon Mason 		qp->rx_bytes += hdr->len;
1680282a2feeSJon Mason 		qp->rx_pkts++;
1681fce8a7bbSJon Mason 
1682da2e5ae5SAllen Hubbe 		entry->len = hdr->len;
1683282a2feeSJon Mason 
1684da2e5ae5SAllen Hubbe 		ntb_async_rx(entry, offset);
1685da2e5ae5SAllen Hubbe 	}
1686fce8a7bbSJon Mason 
1687282a2feeSJon Mason 	qp->rx_index++;
1688282a2feeSJon Mason 	qp->rx_index %= qp->rx_max_entry;
1689282a2feeSJon Mason 
1690282a2feeSJon Mason 	return 0;
1691fce8a7bbSJon Mason }
1692fce8a7bbSJon Mason 
1693e26a5843SAllen Hubbe static void ntb_transport_rxc_db(unsigned long data)
1694fce8a7bbSJon Mason {
1695e26a5843SAllen Hubbe 	struct ntb_transport_qp *qp = (void *)data;
1696c336acd3SJon Mason 	int rc, i;
1697fce8a7bbSJon Mason 
1698e26a5843SAllen Hubbe 	dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1699e26a5843SAllen Hubbe 		__func__, qp->qp_num);
1700e8aeb60cSJon Mason 
1701c336acd3SJon Mason 	/* Limit the number of packets processed in a single interrupt to
1702c336acd3SJon Mason 	 * provide fairness to others
1703c336acd3SJon Mason 	 */
1704c336acd3SJon Mason 	for (i = 0; i < qp->rx_max_entry; i++) {
1705fce8a7bbSJon Mason 		rc = ntb_process_rxc(qp);
1706c336acd3SJon Mason 		if (rc)
1707c336acd3SJon Mason 			break;
1708c336acd3SJon Mason 	}
1709282a2feeSJon Mason 
1710569410caSDave Jiang 	if (i && qp->rx_dma_chan)
1711569410caSDave Jiang 		dma_async_issue_pending(qp->rx_dma_chan);
1712fce8a7bbSJon Mason 
1713e26a5843SAllen Hubbe 	if (i == qp->rx_max_entry) {
1714e26a5843SAllen Hubbe 		/* there is more work to do */
1715e9021331SDave Jiang 		if (qp->active)
1716e26a5843SAllen Hubbe 			tasklet_schedule(&qp->rxc_db_work);
1717e26a5843SAllen Hubbe 	} else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1718e26a5843SAllen Hubbe 		/* the doorbell bit is set: clear it */
1719e26a5843SAllen Hubbe 		ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1720e26a5843SAllen Hubbe 		/* ntb_db_read ensures ntb_db_clear write is committed */
1721e26a5843SAllen Hubbe 		ntb_db_read(qp->ndev);
1722e26a5843SAllen Hubbe 
1723e26a5843SAllen Hubbe 		/* an interrupt may have arrived between finishing
1724e26a5843SAllen Hubbe 		 * ntb_process_rxc and clearing the doorbell bit:
1725e26a5843SAllen Hubbe 		 * there might be some more work to do.
1726e26a5843SAllen Hubbe 		 */
1727e9021331SDave Jiang 		if (qp->active)
1728e26a5843SAllen Hubbe 			tasklet_schedule(&qp->rxc_db_work);
1729e26a5843SAllen Hubbe 	}
1730fce8a7bbSJon Mason }
1731fce8a7bbSJon Mason 
17329cabc269SDave Jiang static void ntb_tx_copy_callback(void *data,
17339cabc269SDave Jiang 				 const struct dmaengine_result *res)
1734fce8a7bbSJon Mason {
1735282a2feeSJon Mason 	struct ntb_queue_entry *entry = data;
1736282a2feeSJon Mason 	struct ntb_transport_qp *qp = entry->qp;
1737282a2feeSJon Mason 	struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
1738fce8a7bbSJon Mason 
17399cabc269SDave Jiang 	/* we need to check DMA results if we are using DMA */
17409cabc269SDave Jiang 	if (res) {
17419cabc269SDave Jiang 		enum dmaengine_tx_result dma_err = res->result;
17429cabc269SDave Jiang 
17439cabc269SDave Jiang 		switch (dma_err) {
17449cabc269SDave Jiang 		case DMA_TRANS_READ_FAILED:
17459cabc269SDave Jiang 		case DMA_TRANS_WRITE_FAILED:
17469cabc269SDave Jiang 			entry->errors++;
1747df561f66SGustavo A. R. Silva 			fallthrough;
17489cabc269SDave Jiang 		case DMA_TRANS_ABORTED:
17499cabc269SDave Jiang 		{
17509cabc269SDave Jiang 			void __iomem *offset =
17519cabc269SDave Jiang 				qp->tx_mw + qp->tx_max_frame *
17529cabc269SDave Jiang 				entry->tx_index;
17539cabc269SDave Jiang 
17549cabc269SDave Jiang 			/* resubmit via CPU */
17559cabc269SDave Jiang 			ntb_memcpy_tx(entry, offset);
17569cabc269SDave Jiang 			qp->tx_memcpy++;
17579cabc269SDave Jiang 			return;
17589cabc269SDave Jiang 		}
17599cabc269SDave Jiang 
17609cabc269SDave Jiang 		case DMA_TRANS_NOERROR:
17619cabc269SDave Jiang 		default:
17629cabc269SDave Jiang 			break;
17639cabc269SDave Jiang 		}
17649cabc269SDave Jiang 	}
17659cabc269SDave Jiang 
176674465645SJon Mason 	iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
1767fce8a7bbSJon Mason 
17682b0569b3SLogan Gunthorpe 	if (qp->use_msi)
17692b0569b3SLogan Gunthorpe 		ntb_msi_peer_trigger(qp->ndev, PIDX, &qp->peer_msi_desc);
17702b0569b3SLogan Gunthorpe 	else
1771e26a5843SAllen Hubbe 		ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
1772fce8a7bbSJon Mason 
1773fce8a7bbSJon Mason 	/* The entry length can only be zero if the packet is intended to be a
1774fce8a7bbSJon Mason 	 * "link down" or similar.  Since no payload is being sent in these
1775fce8a7bbSJon Mason 	 * cases, there is nothing to add to the completion queue.
1776fce8a7bbSJon Mason 	 */
1777fce8a7bbSJon Mason 	if (entry->len > 0) {
1778fce8a7bbSJon Mason 		qp->tx_bytes += entry->len;
1779fce8a7bbSJon Mason 
1780fce8a7bbSJon Mason 		if (qp->tx_handler)
1781fce8a7bbSJon Mason 			qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1782fce8a7bbSJon Mason 				       entry->len);
1783fce8a7bbSJon Mason 	}
1784fce8a7bbSJon Mason 
1785fce8a7bbSJon Mason 	ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1786fce8a7bbSJon Mason }
1787fce8a7bbSJon Mason 
1788282a2feeSJon Mason static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1789282a2feeSJon Mason {
179006917f75SDave Jiang #ifdef ARCH_HAS_NOCACHE_UACCESS
179106917f75SDave Jiang 	/*
179206917f75SDave Jiang 	 * Using non-temporal mov to improve performance on non-cached
179306917f75SDave Jiang 	 * writes, even though we aren't actually copying from user space.
179406917f75SDave Jiang 	 */
179506917f75SDave Jiang 	__copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
179606917f75SDave Jiang #else
1797282a2feeSJon Mason 	memcpy_toio(offset, entry->buf, entry->len);
179806917f75SDave Jiang #endif
1799282a2feeSJon Mason 
1800e26a5843SAllen Hubbe 	/* Ensure that the data is fully copied out before setting the flags */
1801e26a5843SAllen Hubbe 	wmb();
1802e26a5843SAllen Hubbe 
18039cabc269SDave Jiang 	ntb_tx_copy_callback(entry, NULL);
1804282a2feeSJon Mason }
1805282a2feeSJon Mason 
18069cabc269SDave Jiang static int ntb_async_tx_submit(struct ntb_transport_qp *qp,
1807282a2feeSJon Mason 			       struct ntb_queue_entry *entry)
1808282a2feeSJon Mason {
1809282a2feeSJon Mason 	struct dma_async_tx_descriptor *txd;
1810569410caSDave Jiang 	struct dma_chan *chan = qp->tx_dma_chan;
1811282a2feeSJon Mason 	struct dma_device *device;
18129cabc269SDave Jiang 	size_t len = entry->len;
18139cabc269SDave Jiang 	void *buf = entry->buf;
1814282a2feeSJon Mason 	size_t dest_off, buff_off;
18156f57fd05SBartlomiej Zolnierkiewicz 	struct dmaengine_unmap_data *unmap;
18166f57fd05SBartlomiej Zolnierkiewicz 	dma_addr_t dest;
1817282a2feeSJon Mason 	dma_cookie_t cookie;
1818282a2feeSJon Mason 
1819282a2feeSJon Mason 	device = chan->device;
1820c59666bbSLogan Gunthorpe 	dest = qp->tx_mw_dma_addr + qp->tx_max_frame * entry->tx_index;
1821282a2feeSJon Mason 	buff_off = (size_t)buf & ~PAGE_MASK;
1822282a2feeSJon Mason 	dest_off = (size_t)dest & ~PAGE_MASK;
1823282a2feeSJon Mason 
1824282a2feeSJon Mason 	if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1825282a2feeSJon Mason 		goto err;
1826282a2feeSJon Mason 
18276f57fd05SBartlomiej Zolnierkiewicz 	unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
18286f57fd05SBartlomiej Zolnierkiewicz 	if (!unmap)
1829282a2feeSJon Mason 		goto err;
1830282a2feeSJon Mason 
18316f57fd05SBartlomiej Zolnierkiewicz 	unmap->len = len;
18326f57fd05SBartlomiej Zolnierkiewicz 	unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
18336f57fd05SBartlomiej Zolnierkiewicz 				      buff_off, len, DMA_TO_DEVICE);
18346f57fd05SBartlomiej Zolnierkiewicz 	if (dma_mapping_error(device->dev, unmap->addr[0]))
18356f57fd05SBartlomiej Zolnierkiewicz 		goto err_get_unmap;
18366f57fd05SBartlomiej Zolnierkiewicz 
18376f57fd05SBartlomiej Zolnierkiewicz 	unmap->to_cnt = 1;
18386f57fd05SBartlomiej Zolnierkiewicz 
183988931ec3SAllen Hubbe 	txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
18409cabc269SDave Jiang 					     DMA_PREP_INTERRUPT);
184188931ec3SAllen Hubbe 	if (!txd)
18426f57fd05SBartlomiej Zolnierkiewicz 		goto err_get_unmap;
1843282a2feeSJon Mason 
18449cabc269SDave Jiang 	txd->callback_result = ntb_tx_copy_callback;
1845282a2feeSJon Mason 	txd->callback_param = entry;
18466f57fd05SBartlomiej Zolnierkiewicz 	dma_set_unmap(txd, unmap);
1847282a2feeSJon Mason 
1848282a2feeSJon Mason 	cookie = dmaengine_submit(txd);
1849282a2feeSJon Mason 	if (dma_submit_error(cookie))
18506f57fd05SBartlomiej Zolnierkiewicz 		goto err_set_unmap;
18516f57fd05SBartlomiej Zolnierkiewicz 
18526f57fd05SBartlomiej Zolnierkiewicz 	dmaengine_unmap_put(unmap);
1853282a2feeSJon Mason 
1854282a2feeSJon Mason 	dma_async_issue_pending(chan);
1855282a2feeSJon Mason 
18569cabc269SDave Jiang 	return 0;
18576f57fd05SBartlomiej Zolnierkiewicz err_set_unmap:
18586f57fd05SBartlomiej Zolnierkiewicz 	dmaengine_unmap_put(unmap);
18596f57fd05SBartlomiej Zolnierkiewicz err_get_unmap:
18606f57fd05SBartlomiej Zolnierkiewicz 	dmaengine_unmap_put(unmap);
1861282a2feeSJon Mason err:
18629cabc269SDave Jiang 	return -ENXIO;
18639cabc269SDave Jiang }
18649cabc269SDave Jiang 
18659cabc269SDave Jiang static void ntb_async_tx(struct ntb_transport_qp *qp,
18669cabc269SDave Jiang 			 struct ntb_queue_entry *entry)
18679cabc269SDave Jiang {
18689cabc269SDave Jiang 	struct ntb_payload_header __iomem *hdr;
18699cabc269SDave Jiang 	struct dma_chan *chan = qp->tx_dma_chan;
18709cabc269SDave Jiang 	void __iomem *offset;
18719cabc269SDave Jiang 	int res;
18729cabc269SDave Jiang 
18739cabc269SDave Jiang 	entry->tx_index = qp->tx_index;
18749cabc269SDave Jiang 	offset = qp->tx_mw + qp->tx_max_frame * entry->tx_index;
18759cabc269SDave Jiang 	hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
18769cabc269SDave Jiang 	entry->tx_hdr = hdr;
18779cabc269SDave Jiang 
18789cabc269SDave Jiang 	iowrite32(entry->len, &hdr->len);
18799cabc269SDave Jiang 	iowrite32((u32)qp->tx_pkts, &hdr->ver);
18809cabc269SDave Jiang 
18819cabc269SDave Jiang 	if (!chan)
18829cabc269SDave Jiang 		goto err;
18839cabc269SDave Jiang 
18849cabc269SDave Jiang 	if (entry->len < copy_bytes)
18859cabc269SDave Jiang 		goto err;
18869cabc269SDave Jiang 
18879cabc269SDave Jiang 	res = ntb_async_tx_submit(qp, entry);
18889cabc269SDave Jiang 	if (res < 0)
18899cabc269SDave Jiang 		goto err;
18909cabc269SDave Jiang 
18919cabc269SDave Jiang 	if (!entry->retries)
18929cabc269SDave Jiang 		qp->tx_async++;
18939cabc269SDave Jiang 
18949cabc269SDave Jiang 	return;
18959cabc269SDave Jiang 
18969cabc269SDave Jiang err:
1897282a2feeSJon Mason 	ntb_memcpy_tx(entry, offset);
1898282a2feeSJon Mason 	qp->tx_memcpy++;
1899282a2feeSJon Mason }
1900282a2feeSJon Mason 
1901fce8a7bbSJon Mason static int ntb_process_tx(struct ntb_transport_qp *qp,
1902fce8a7bbSJon Mason 			  struct ntb_queue_entry *entry)
1903fce8a7bbSJon Mason {
1904793c20e9SJon Mason 	if (qp->tx_index == qp->remote_rx_info->entry) {
1905fce8a7bbSJon Mason 		qp->tx_ring_full++;
1906fce8a7bbSJon Mason 		return -EAGAIN;
1907fce8a7bbSJon Mason 	}
1908fce8a7bbSJon Mason 
1909ef114ed5SJon Mason 	if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
1910fce8a7bbSJon Mason 		if (qp->tx_handler)
1911179f912aSJon Mason 			qp->tx_handler(qp, qp->cb_data, NULL, -EIO);
1912fce8a7bbSJon Mason 
1913fce8a7bbSJon Mason 		ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1914fce8a7bbSJon Mason 			     &qp->tx_free_q);
1915fce8a7bbSJon Mason 		return 0;
1916fce8a7bbSJon Mason 	}
1917fce8a7bbSJon Mason 
1918282a2feeSJon Mason 	ntb_async_tx(qp, entry);
1919fce8a7bbSJon Mason 
1920793c20e9SJon Mason 	qp->tx_index++;
1921793c20e9SJon Mason 	qp->tx_index %= qp->tx_max_entry;
1922fce8a7bbSJon Mason 
1923fce8a7bbSJon Mason 	qp->tx_pkts++;
1924fce8a7bbSJon Mason 
1925fce8a7bbSJon Mason 	return 0;
1926fce8a7bbSJon Mason }
1927fce8a7bbSJon Mason 
1928fce8a7bbSJon Mason static void ntb_send_link_down(struct ntb_transport_qp *qp)
1929fce8a7bbSJon Mason {
1930e26a5843SAllen Hubbe 	struct pci_dev *pdev = qp->ndev->pdev;
1931fce8a7bbSJon Mason 	struct ntb_queue_entry *entry;
1932fce8a7bbSJon Mason 	int i, rc;
1933fce8a7bbSJon Mason 
1934e26a5843SAllen Hubbe 	if (!qp->link_is_up)
1935fce8a7bbSJon Mason 		return;
1936fce8a7bbSJon Mason 
1937e22e0b9dSAllen Hubbe 	dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
1938fce8a7bbSJon Mason 
1939fce8a7bbSJon Mason 	for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1940f766755cSJon Mason 		entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1941fce8a7bbSJon Mason 		if (entry)
1942fce8a7bbSJon Mason 			break;
1943fce8a7bbSJon Mason 		msleep(100);
1944fce8a7bbSJon Mason 	}
1945fce8a7bbSJon Mason 
1946fce8a7bbSJon Mason 	if (!entry)
1947fce8a7bbSJon Mason 		return;
1948fce8a7bbSJon Mason 
1949fce8a7bbSJon Mason 	entry->cb_data = NULL;
1950fce8a7bbSJon Mason 	entry->buf = NULL;
1951fce8a7bbSJon Mason 	entry->len = 0;
1952fce8a7bbSJon Mason 	entry->flags = LINK_DOWN_FLAG;
1953fce8a7bbSJon Mason 
1954fce8a7bbSJon Mason 	rc = ntb_process_tx(qp, entry);
1955fce8a7bbSJon Mason 	if (rc)
1956fce8a7bbSJon Mason 		dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1957fce8a7bbSJon Mason 			qp->qp_num);
19582849b5d7SAllen Hubbe 
19592849b5d7SAllen Hubbe 	ntb_qp_link_down_reset(qp);
1960fce8a7bbSJon Mason }
1961fce8a7bbSJon Mason 
19621199aa61SAllen Hubbe static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
19631199aa61SAllen Hubbe {
19641199aa61SAllen Hubbe 	return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
19651199aa61SAllen Hubbe }
19661199aa61SAllen Hubbe 
1967fce8a7bbSJon Mason /**
1968fce8a7bbSJon Mason  * ntb_transport_create_queue - Create a new NTB transport layer queue
1969fce8a7bbSJon Mason  * @rx_handler: receive callback function
1970fce8a7bbSJon Mason  * @tx_handler: transmit callback function
1971fce8a7bbSJon Mason  * @event_handler: event callback function
1972fce8a7bbSJon Mason  *
1973fce8a7bbSJon Mason  * Create a new NTB transport layer queue and provide the queue with a callback
1974fce8a7bbSJon Mason  * routine for both transmit and receive.  The receive callback routine will be
1975fce8a7bbSJon Mason  * used to pass up data when the transport has received it on the queue.   The
1976fce8a7bbSJon Mason  * transmit callback routine will be called when the transport has completed the
1977fce8a7bbSJon Mason  * transmission of the data on the queue and the data is ready to be freed.
1978fce8a7bbSJon Mason  *
1979fce8a7bbSJon Mason  * RETURNS: pointer to newly created ntb_queue, NULL on error.
1980fce8a7bbSJon Mason  */
1981fce8a7bbSJon Mason struct ntb_transport_qp *
1982e26a5843SAllen Hubbe ntb_transport_create_queue(void *data, struct device *client_dev,
1983fce8a7bbSJon Mason 			   const struct ntb_queue_handlers *handlers)
1984fce8a7bbSJon Mason {
1985e26a5843SAllen Hubbe 	struct ntb_dev *ndev;
1986e26a5843SAllen Hubbe 	struct pci_dev *pdev;
1987e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt;
1988fce8a7bbSJon Mason 	struct ntb_queue_entry *entry;
1989fce8a7bbSJon Mason 	struct ntb_transport_qp *qp;
1990e26a5843SAllen Hubbe 	u64 qp_bit;
1991fce8a7bbSJon Mason 	unsigned int free_queue;
19921199aa61SAllen Hubbe 	dma_cap_mask_t dma_mask;
19931199aa61SAllen Hubbe 	int node;
1994e26a5843SAllen Hubbe 	int i;
1995fce8a7bbSJon Mason 
1996e26a5843SAllen Hubbe 	ndev = dev_ntb(client_dev->parent);
1997e26a5843SAllen Hubbe 	pdev = ndev->pdev;
1998e26a5843SAllen Hubbe 	nt = ndev->ctx;
1999fce8a7bbSJon Mason 
20001199aa61SAllen Hubbe 	node = dev_to_node(&ndev->dev);
20011199aa61SAllen Hubbe 
20028fcd0950SThomas VanSelus 	free_queue = ffs(nt->qp_bitmap_free);
2003fce8a7bbSJon Mason 	if (!free_queue)
2004fce8a7bbSJon Mason 		goto err;
2005fce8a7bbSJon Mason 
2006fce8a7bbSJon Mason 	/* decrement free_queue to make it zero based */
2007fce8a7bbSJon Mason 	free_queue--;
2008fce8a7bbSJon Mason 
2009e26a5843SAllen Hubbe 	qp = &nt->qp_vec[free_queue];
2010e26a5843SAllen Hubbe 	qp_bit = BIT_ULL(qp->qp_num);
2011fce8a7bbSJon Mason 
2012e26a5843SAllen Hubbe 	nt->qp_bitmap_free &= ~qp_bit;
2013e26a5843SAllen Hubbe 
2014fce8a7bbSJon Mason 	qp->cb_data = data;
2015fce8a7bbSJon Mason 	qp->rx_handler = handlers->rx_handler;
2016fce8a7bbSJon Mason 	qp->tx_handler = handlers->tx_handler;
2017fce8a7bbSJon Mason 	qp->event_handler = handlers->event_handler;
2018fce8a7bbSJon Mason 
20191199aa61SAllen Hubbe 	dma_cap_zero(dma_mask);
20201199aa61SAllen Hubbe 	dma_cap_set(DMA_MEMCPY, dma_mask);
20211199aa61SAllen Hubbe 
2022a41ef053SDave Jiang 	if (use_dma) {
2023569410caSDave Jiang 		qp->tx_dma_chan =
2024569410caSDave Jiang 			dma_request_channel(dma_mask, ntb_dma_filter_fn,
20251199aa61SAllen Hubbe 					    (void *)(unsigned long)node);
2026569410caSDave Jiang 		if (!qp->tx_dma_chan)
2027569410caSDave Jiang 			dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
2028569410caSDave Jiang 
2029569410caSDave Jiang 		qp->rx_dma_chan =
2030569410caSDave Jiang 			dma_request_channel(dma_mask, ntb_dma_filter_fn,
2031569410caSDave Jiang 					    (void *)(unsigned long)node);
2032569410caSDave Jiang 		if (!qp->rx_dma_chan)
2033569410caSDave Jiang 			dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
2034a41ef053SDave Jiang 	} else {
2035569410caSDave Jiang 		qp->tx_dma_chan = NULL;
2036569410caSDave Jiang 		qp->rx_dma_chan = NULL;
2037a41ef053SDave Jiang 	}
2038569410caSDave Jiang 
203951cb8dbfSLogan Gunthorpe 	qp->tx_mw_dma_addr = 0;
2040c59666bbSLogan Gunthorpe 	if (qp->tx_dma_chan) {
2041c59666bbSLogan Gunthorpe 		qp->tx_mw_dma_addr =
2042c59666bbSLogan Gunthorpe 			dma_map_resource(qp->tx_dma_chan->device->dev,
2043c59666bbSLogan Gunthorpe 					 qp->tx_mw_phys, qp->tx_mw_size,
2044c59666bbSLogan Gunthorpe 					 DMA_FROM_DEVICE, 0);
2045c59666bbSLogan Gunthorpe 		if (dma_mapping_error(qp->tx_dma_chan->device->dev,
2046c59666bbSLogan Gunthorpe 				      qp->tx_mw_dma_addr)) {
2047c59666bbSLogan Gunthorpe 			qp->tx_mw_dma_addr = 0;
2048c59666bbSLogan Gunthorpe 			goto err1;
2049c59666bbSLogan Gunthorpe 		}
2050c59666bbSLogan Gunthorpe 	}
2051c59666bbSLogan Gunthorpe 
2052569410caSDave Jiang 	dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
2053569410caSDave Jiang 		qp->tx_dma_chan ? "DMA" : "CPU");
2054569410caSDave Jiang 
2055569410caSDave Jiang 	dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
2056569410caSDave Jiang 		qp->rx_dma_chan ? "DMA" : "CPU");
2057282a2feeSJon Mason 
2058fce8a7bbSJon Mason 	for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
2059c9160b69SJia-Ju Bai 		entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
2060fce8a7bbSJon Mason 		if (!entry)
2061fce8a7bbSJon Mason 			goto err1;
2062fce8a7bbSJon Mason 
2063282a2feeSJon Mason 		entry->qp = qp;
2064da2e5ae5SAllen Hubbe 		ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
2065fce8a7bbSJon Mason 			     &qp->rx_free_q);
2066fce8a7bbSJon Mason 	}
2067a754a8fcSDave Jiang 	qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES;
2068fce8a7bbSJon Mason 
2069a754a8fcSDave Jiang 	for (i = 0; i < qp->tx_max_entry; i++) {
2070c9160b69SJia-Ju Bai 		entry = kzalloc_node(sizeof(*entry), GFP_KERNEL, node);
2071fce8a7bbSJon Mason 		if (!entry)
2072fce8a7bbSJon Mason 			goto err2;
2073fce8a7bbSJon Mason 
2074282a2feeSJon Mason 		entry->qp = qp;
2075fce8a7bbSJon Mason 		ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2076fce8a7bbSJon Mason 			     &qp->tx_free_q);
2077fce8a7bbSJon Mason 	}
2078fce8a7bbSJon Mason 
2079e26a5843SAllen Hubbe 	ntb_db_clear(qp->ndev, qp_bit);
2080e26a5843SAllen Hubbe 	ntb_db_clear_mask(qp->ndev, qp_bit);
2081fce8a7bbSJon Mason 
2082fce8a7bbSJon Mason 	dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
2083fce8a7bbSJon Mason 
2084fce8a7bbSJon Mason 	return qp;
2085fce8a7bbSJon Mason 
2086fce8a7bbSJon Mason err2:
2087f766755cSJon Mason 	while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
2088fce8a7bbSJon Mason 		kfree(entry);
2089fce8a7bbSJon Mason err1:
2090a754a8fcSDave Jiang 	qp->rx_alloc_entry = 0;
2091da2e5ae5SAllen Hubbe 	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
2092fce8a7bbSJon Mason 		kfree(entry);
2093c59666bbSLogan Gunthorpe 	if (qp->tx_mw_dma_addr)
2094c59666bbSLogan Gunthorpe 		dma_unmap_resource(qp->tx_dma_chan->device->dev,
2095c59666bbSLogan Gunthorpe 				   qp->tx_mw_dma_addr, qp->tx_mw_size,
2096c59666bbSLogan Gunthorpe 				   DMA_FROM_DEVICE, 0);
2097569410caSDave Jiang 	if (qp->tx_dma_chan)
2098569410caSDave Jiang 		dma_release_channel(qp->tx_dma_chan);
2099569410caSDave Jiang 	if (qp->rx_dma_chan)
2100569410caSDave Jiang 		dma_release_channel(qp->rx_dma_chan);
2101e26a5843SAllen Hubbe 	nt->qp_bitmap_free |= qp_bit;
2102fce8a7bbSJon Mason err:
2103fce8a7bbSJon Mason 	return NULL;
2104fce8a7bbSJon Mason }
2105fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
2106fce8a7bbSJon Mason 
2107fce8a7bbSJon Mason /**
2108fce8a7bbSJon Mason  * ntb_transport_free_queue - Frees NTB transport queue
2109fce8a7bbSJon Mason  * @qp: NTB queue to be freed
2110fce8a7bbSJon Mason  *
2111fce8a7bbSJon Mason  * Frees NTB transport queue
2112fce8a7bbSJon Mason  */
2113fce8a7bbSJon Mason void ntb_transport_free_queue(struct ntb_transport_qp *qp)
2114fce8a7bbSJon Mason {
2115186f27ffSJon Mason 	struct pci_dev *pdev;
2116fce8a7bbSJon Mason 	struct ntb_queue_entry *entry;
2117e26a5843SAllen Hubbe 	u64 qp_bit;
2118fce8a7bbSJon Mason 
2119fce8a7bbSJon Mason 	if (!qp)
2120fce8a7bbSJon Mason 		return;
2121fce8a7bbSJon Mason 
2122e26a5843SAllen Hubbe 	pdev = qp->ndev->pdev;
2123186f27ffSJon Mason 
2124e9021331SDave Jiang 	qp->active = false;
2125e9021331SDave Jiang 
2126569410caSDave Jiang 	if (qp->tx_dma_chan) {
2127569410caSDave Jiang 		struct dma_chan *chan = qp->tx_dma_chan;
2128282a2feeSJon Mason 		/* Putting the dma_chan to NULL will force any new traffic to be
2129282a2feeSJon Mason 		 * processed by the CPU instead of the DAM engine
2130282a2feeSJon Mason 		 */
2131569410caSDave Jiang 		qp->tx_dma_chan = NULL;
2132569410caSDave Jiang 
2133569410caSDave Jiang 		/* Try to be nice and wait for any queued DMA engine
2134569410caSDave Jiang 		 * transactions to process before smashing it with a rock
2135569410caSDave Jiang 		 */
2136569410caSDave Jiang 		dma_sync_wait(chan, qp->last_cookie);
2137569410caSDave Jiang 		dmaengine_terminate_all(chan);
2138c59666bbSLogan Gunthorpe 
2139c59666bbSLogan Gunthorpe 		dma_unmap_resource(chan->device->dev,
2140c59666bbSLogan Gunthorpe 				   qp->tx_mw_dma_addr, qp->tx_mw_size,
2141c59666bbSLogan Gunthorpe 				   DMA_FROM_DEVICE, 0);
2142c59666bbSLogan Gunthorpe 
2143569410caSDave Jiang 		dma_release_channel(chan);
2144569410caSDave Jiang 	}
2145569410caSDave Jiang 
2146569410caSDave Jiang 	if (qp->rx_dma_chan) {
2147569410caSDave Jiang 		struct dma_chan *chan = qp->rx_dma_chan;
2148569410caSDave Jiang 		/* Putting the dma_chan to NULL will force any new traffic to be
2149569410caSDave Jiang 		 * processed by the CPU instead of the DAM engine
2150569410caSDave Jiang 		 */
2151569410caSDave Jiang 		qp->rx_dma_chan = NULL;
2152282a2feeSJon Mason 
2153282a2feeSJon Mason 		/* Try to be nice and wait for any queued DMA engine
2154282a2feeSJon Mason 		 * transactions to process before smashing it with a rock
2155282a2feeSJon Mason 		 */
2156282a2feeSJon Mason 		dma_sync_wait(chan, qp->last_cookie);
2157282a2feeSJon Mason 		dmaengine_terminate_all(chan);
21581199aa61SAllen Hubbe 		dma_release_channel(chan);
2159282a2feeSJon Mason 	}
2160fce8a7bbSJon Mason 
2161e26a5843SAllen Hubbe 	qp_bit = BIT_ULL(qp->qp_num);
2162e26a5843SAllen Hubbe 
2163e26a5843SAllen Hubbe 	ntb_db_set_mask(qp->ndev, qp_bit);
2164e9021331SDave Jiang 	tasklet_kill(&qp->rxc_db_work);
2165fce8a7bbSJon Mason 
2166282a2feeSJon Mason 	cancel_delayed_work_sync(&qp->link_work);
2167282a2feeSJon Mason 
2168e26a5843SAllen Hubbe 	qp->cb_data = NULL;
2169e26a5843SAllen Hubbe 	qp->rx_handler = NULL;
2170e26a5843SAllen Hubbe 	qp->tx_handler = NULL;
2171e26a5843SAllen Hubbe 	qp->event_handler = NULL;
2172e26a5843SAllen Hubbe 
2173da2e5ae5SAllen Hubbe 	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
2174fce8a7bbSJon Mason 		kfree(entry);
2175fce8a7bbSJon Mason 
2176da2e5ae5SAllen Hubbe 	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
2177da2e5ae5SAllen Hubbe 		dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
2178da2e5ae5SAllen Hubbe 		kfree(entry);
2179da2e5ae5SAllen Hubbe 	}
2180da2e5ae5SAllen Hubbe 
2181da2e5ae5SAllen Hubbe 	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
2182da2e5ae5SAllen Hubbe 		dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
2183fce8a7bbSJon Mason 		kfree(entry);
2184fce8a7bbSJon Mason 	}
2185fce8a7bbSJon Mason 
2186f766755cSJon Mason 	while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
2187fce8a7bbSJon Mason 		kfree(entry);
2188fce8a7bbSJon Mason 
218930a4bb1eSAllen Hubbe 	qp->transport->qp_bitmap_free |= qp_bit;
2190fce8a7bbSJon Mason 
2191fce8a7bbSJon Mason 	dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
2192fce8a7bbSJon Mason }
2193fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
2194fce8a7bbSJon Mason 
2195fce8a7bbSJon Mason /**
2196fce8a7bbSJon Mason  * ntb_transport_rx_remove - Dequeues enqueued rx packet
2197fce8a7bbSJon Mason  * @qp: NTB queue to be freed
2198fce8a7bbSJon Mason  * @len: pointer to variable to write enqueued buffers length
2199fce8a7bbSJon Mason  *
2200fce8a7bbSJon Mason  * Dequeues unused buffers from receive queue.  Should only be used during
2201fce8a7bbSJon Mason  * shutdown of qp.
2202fce8a7bbSJon Mason  *
2203fce8a7bbSJon Mason  * RETURNS: NULL error value on error, or void* for success.
2204fce8a7bbSJon Mason  */
2205fce8a7bbSJon Mason void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
2206fce8a7bbSJon Mason {
2207fce8a7bbSJon Mason 	struct ntb_queue_entry *entry;
2208fce8a7bbSJon Mason 	void *buf;
2209fce8a7bbSJon Mason 
2210e26a5843SAllen Hubbe 	if (!qp || qp->client_ready)
2211fce8a7bbSJon Mason 		return NULL;
2212fce8a7bbSJon Mason 
2213da2e5ae5SAllen Hubbe 	entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
2214fce8a7bbSJon Mason 	if (!entry)
2215fce8a7bbSJon Mason 		return NULL;
2216fce8a7bbSJon Mason 
2217fce8a7bbSJon Mason 	buf = entry->cb_data;
2218fce8a7bbSJon Mason 	*len = entry->len;
2219fce8a7bbSJon Mason 
2220da2e5ae5SAllen Hubbe 	ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
2221fce8a7bbSJon Mason 
2222fce8a7bbSJon Mason 	return buf;
2223fce8a7bbSJon Mason }
2224fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
2225fce8a7bbSJon Mason 
2226fce8a7bbSJon Mason /**
2227fce8a7bbSJon Mason  * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
2228fce8a7bbSJon Mason  * @qp: NTB transport layer queue the entry is to be enqueued on
2229fce8a7bbSJon Mason  * @cb: per buffer pointer for callback function to use
2230fce8a7bbSJon Mason  * @data: pointer to data buffer that incoming packets will be copied into
2231fce8a7bbSJon Mason  * @len: length of the data buffer
2232fce8a7bbSJon Mason  *
2233fce8a7bbSJon Mason  * Enqueue a new receive buffer onto the transport queue into which a NTB
2234fce8a7bbSJon Mason  * payload can be received into.
2235fce8a7bbSJon Mason  *
2236fce8a7bbSJon Mason  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2237fce8a7bbSJon Mason  */
2238fce8a7bbSJon Mason int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2239fce8a7bbSJon Mason 			     unsigned int len)
2240fce8a7bbSJon Mason {
2241fce8a7bbSJon Mason 	struct ntb_queue_entry *entry;
2242fce8a7bbSJon Mason 
2243fce8a7bbSJon Mason 	if (!qp)
2244fce8a7bbSJon Mason 		return -EINVAL;
2245fce8a7bbSJon Mason 
2246da2e5ae5SAllen Hubbe 	entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
2247fce8a7bbSJon Mason 	if (!entry)
2248fce8a7bbSJon Mason 		return -ENOMEM;
2249fce8a7bbSJon Mason 
2250fce8a7bbSJon Mason 	entry->cb_data = cb;
2251fce8a7bbSJon Mason 	entry->buf = data;
2252fce8a7bbSJon Mason 	entry->len = len;
2253da2e5ae5SAllen Hubbe 	entry->flags = 0;
225472203572SDave Jiang 	entry->retries = 0;
225572203572SDave Jiang 	entry->errors = 0;
225672203572SDave Jiang 	entry->rx_index = 0;
2257fce8a7bbSJon Mason 
2258da2e5ae5SAllen Hubbe 	ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
2259da2e5ae5SAllen Hubbe 
2260e9021331SDave Jiang 	if (qp->active)
2261da2e5ae5SAllen Hubbe 		tasklet_schedule(&qp->rxc_db_work);
2262fce8a7bbSJon Mason 
2263fce8a7bbSJon Mason 	return 0;
2264fce8a7bbSJon Mason }
2265fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
2266fce8a7bbSJon Mason 
2267fce8a7bbSJon Mason /**
2268fce8a7bbSJon Mason  * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
2269fce8a7bbSJon Mason  * @qp: NTB transport layer queue the entry is to be enqueued on
2270fce8a7bbSJon Mason  * @cb: per buffer pointer for callback function to use
2271fce8a7bbSJon Mason  * @data: pointer to data buffer that will be sent
2272fce8a7bbSJon Mason  * @len: length of the data buffer
2273fce8a7bbSJon Mason  *
2274fce8a7bbSJon Mason  * Enqueue a new transmit buffer onto the transport queue from which a NTB
2275f9a2cf89SJon Mason  * payload will be transmitted.  This assumes that a lock is being held to
2276fce8a7bbSJon Mason  * serialize access to the qp.
2277fce8a7bbSJon Mason  *
2278fce8a7bbSJon Mason  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
2279fce8a7bbSJon Mason  */
2280fce8a7bbSJon Mason int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
2281fce8a7bbSJon Mason 			     unsigned int len)
2282fce8a7bbSJon Mason {
2283fce8a7bbSJon Mason 	struct ntb_queue_entry *entry;
2284fce8a7bbSJon Mason 	int rc;
2285fce8a7bbSJon Mason 
2286f195a1a6SDave Jiang 	if (!qp || !len)
2287fce8a7bbSJon Mason 		return -EINVAL;
2288fce8a7bbSJon Mason 
2289f195a1a6SDave Jiang 	/* If the qp link is down already, just ignore. */
2290f195a1a6SDave Jiang 	if (!qp->link_is_up)
2291f195a1a6SDave Jiang 		return 0;
2292f195a1a6SDave Jiang 
2293fce8a7bbSJon Mason 	entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
2294282a2feeSJon Mason 	if (!entry) {
2295282a2feeSJon Mason 		qp->tx_err_no_buf++;
2296e74bfeedSDave Jiang 		return -EBUSY;
2297282a2feeSJon Mason 	}
2298fce8a7bbSJon Mason 
2299fce8a7bbSJon Mason 	entry->cb_data = cb;
2300fce8a7bbSJon Mason 	entry->buf = data;
2301fce8a7bbSJon Mason 	entry->len = len;
2302fce8a7bbSJon Mason 	entry->flags = 0;
23039cabc269SDave Jiang 	entry->errors = 0;
23049cabc269SDave Jiang 	entry->retries = 0;
23059cabc269SDave Jiang 	entry->tx_index = 0;
2306fce8a7bbSJon Mason 
2307fce8a7bbSJon Mason 	rc = ntb_process_tx(qp, entry);
2308fce8a7bbSJon Mason 	if (rc)
2309fce8a7bbSJon Mason 		ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
2310fce8a7bbSJon Mason 			     &qp->tx_free_q);
2311fce8a7bbSJon Mason 
2312fce8a7bbSJon Mason 	return rc;
2313fce8a7bbSJon Mason }
2314fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
2315fce8a7bbSJon Mason 
2316fce8a7bbSJon Mason /**
2317fce8a7bbSJon Mason  * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
2318fce8a7bbSJon Mason  * @qp: NTB transport layer queue to be enabled
2319fce8a7bbSJon Mason  *
2320fce8a7bbSJon Mason  * Notify NTB transport layer of client readiness to use queue
2321fce8a7bbSJon Mason  */
2322fce8a7bbSJon Mason void ntb_transport_link_up(struct ntb_transport_qp *qp)
2323fce8a7bbSJon Mason {
2324fce8a7bbSJon Mason 	if (!qp)
2325fce8a7bbSJon Mason 		return;
2326fce8a7bbSJon Mason 
2327e26a5843SAllen Hubbe 	qp->client_ready = true;
2328fce8a7bbSJon Mason 
2329e26a5843SAllen Hubbe 	if (qp->transport->link_is_up)
2330fce8a7bbSJon Mason 		schedule_delayed_work(&qp->link_work, 0);
2331fce8a7bbSJon Mason }
2332fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_link_up);
2333fce8a7bbSJon Mason 
2334fce8a7bbSJon Mason /**
2335fce8a7bbSJon Mason  * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
2336fce8a7bbSJon Mason  * @qp: NTB transport layer queue to be disabled
2337fce8a7bbSJon Mason  *
2338fce8a7bbSJon Mason  * Notify NTB transport layer of client's desire to no longer receive data on
2339fce8a7bbSJon Mason  * transport queue specified.  It is the client's responsibility to ensure all
2340f9a2cf89SJon Mason  * entries on queue are purged or otherwise handled appropriately.
2341fce8a7bbSJon Mason  */
2342fce8a7bbSJon Mason void ntb_transport_link_down(struct ntb_transport_qp *qp)
2343fce8a7bbSJon Mason {
2344e26a5843SAllen Hubbe 	int val;
2345fce8a7bbSJon Mason 
2346fce8a7bbSJon Mason 	if (!qp)
2347fce8a7bbSJon Mason 		return;
2348fce8a7bbSJon Mason 
2349e26a5843SAllen Hubbe 	qp->client_ready = false;
2350fce8a7bbSJon Mason 
2351e26a5843SAllen Hubbe 	val = ntb_spad_read(qp->ndev, QP_LINKS);
2352fce8a7bbSJon Mason 
2353d67288a3SSerge Semin 	ntb_peer_spad_write(qp->ndev, PIDX, QP_LINKS, val & ~BIT(qp->qp_num));
2354fce8a7bbSJon Mason 
2355e26a5843SAllen Hubbe 	if (qp->link_is_up)
2356fce8a7bbSJon Mason 		ntb_send_link_down(qp);
2357fce8a7bbSJon Mason 	else
2358fce8a7bbSJon Mason 		cancel_delayed_work_sync(&qp->link_work);
2359fce8a7bbSJon Mason }
2360fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_link_down);
2361fce8a7bbSJon Mason 
2362fce8a7bbSJon Mason /**
2363fce8a7bbSJon Mason  * ntb_transport_link_query - Query transport link state
2364fce8a7bbSJon Mason  * @qp: NTB transport layer queue to be queried
2365fce8a7bbSJon Mason  *
2366fce8a7bbSJon Mason  * Query connectivity to the remote system of the NTB transport queue
2367fce8a7bbSJon Mason  *
2368fce8a7bbSJon Mason  * RETURNS: true for link up or false for link down
2369fce8a7bbSJon Mason  */
2370fce8a7bbSJon Mason bool ntb_transport_link_query(struct ntb_transport_qp *qp)
2371fce8a7bbSJon Mason {
2372186f27ffSJon Mason 	if (!qp)
2373186f27ffSJon Mason 		return false;
2374186f27ffSJon Mason 
2375e26a5843SAllen Hubbe 	return qp->link_is_up;
2376fce8a7bbSJon Mason }
2377fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_link_query);
2378fce8a7bbSJon Mason 
2379fce8a7bbSJon Mason /**
2380fce8a7bbSJon Mason  * ntb_transport_qp_num - Query the qp number
2381fce8a7bbSJon Mason  * @qp: NTB transport layer queue to be queried
2382fce8a7bbSJon Mason  *
2383fce8a7bbSJon Mason  * Query qp number of the NTB transport queue
2384fce8a7bbSJon Mason  *
2385fce8a7bbSJon Mason  * RETURNS: a zero based number specifying the qp number
2386fce8a7bbSJon Mason  */
2387fce8a7bbSJon Mason unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
2388fce8a7bbSJon Mason {
2389186f27ffSJon Mason 	if (!qp)
2390186f27ffSJon Mason 		return 0;
2391186f27ffSJon Mason 
2392fce8a7bbSJon Mason 	return qp->qp_num;
2393fce8a7bbSJon Mason }
2394fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
2395fce8a7bbSJon Mason 
2396fce8a7bbSJon Mason /**
2397fce8a7bbSJon Mason  * ntb_transport_max_size - Query the max payload size of a qp
2398fce8a7bbSJon Mason  * @qp: NTB transport layer queue to be queried
2399fce8a7bbSJon Mason  *
2400fce8a7bbSJon Mason  * Query the maximum payload size permissible on the given qp
2401fce8a7bbSJon Mason  *
2402fce8a7bbSJon Mason  * RETURNS: the max payload size of a qp
2403fce8a7bbSJon Mason  */
2404ef114ed5SJon Mason unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
2405fce8a7bbSJon Mason {
240604afde45SDave Jiang 	unsigned int max_size;
2407569410caSDave Jiang 	unsigned int copy_align;
240804afde45SDave Jiang 	struct dma_chan *rx_chan, *tx_chan;
2409282a2feeSJon Mason 
2410186f27ffSJon Mason 	if (!qp)
2411186f27ffSJon Mason 		return 0;
2412186f27ffSJon Mason 
241304afde45SDave Jiang 	rx_chan = qp->rx_dma_chan;
241404afde45SDave Jiang 	tx_chan = qp->tx_dma_chan;
2415282a2feeSJon Mason 
241604afde45SDave Jiang 	copy_align = max(rx_chan ? rx_chan->device->copy_align : 0,
241704afde45SDave Jiang 			 tx_chan ? tx_chan->device->copy_align : 0);
2418569410caSDave Jiang 
2419282a2feeSJon Mason 	/* If DMA engine usage is possible, try to find the max size for that */
242004afde45SDave Jiang 	max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header);
242104afde45SDave Jiang 	max_size = round_down(max_size, 1 << copy_align);
2422282a2feeSJon Mason 
242304afde45SDave Jiang 	return max_size;
2424fce8a7bbSJon Mason }
2425fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_max_size);
2426e26a5843SAllen Hubbe 
2427e74bfeedSDave Jiang unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2428e74bfeedSDave Jiang {
2429e74bfeedSDave Jiang 	unsigned int head = qp->tx_index;
2430e74bfeedSDave Jiang 	unsigned int tail = qp->remote_rx_info->entry;
2431e74bfeedSDave Jiang 
2432*5a7693e6SDave Jiang 	return tail >= head ? tail - head : qp->tx_max_entry + tail - head;
2433e74bfeedSDave Jiang }
2434e74bfeedSDave Jiang EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2435e74bfeedSDave Jiang 
2436e26a5843SAllen Hubbe static void ntb_transport_doorbell_callback(void *data, int vector)
2437e26a5843SAllen Hubbe {
2438e26a5843SAllen Hubbe 	struct ntb_transport_ctx *nt = data;
2439e26a5843SAllen Hubbe 	struct ntb_transport_qp *qp;
2440e26a5843SAllen Hubbe 	u64 db_bits;
2441e26a5843SAllen Hubbe 	unsigned int qp_num;
2442e26a5843SAllen Hubbe 
24432b0569b3SLogan Gunthorpe 	if (ntb_db_read(nt->ndev) & nt->msi_db_mask) {
24442b0569b3SLogan Gunthorpe 		ntb_transport_msi_peer_desc_changed(nt);
24452b0569b3SLogan Gunthorpe 		ntb_db_clear(nt->ndev, nt->msi_db_mask);
24462b0569b3SLogan Gunthorpe 	}
24472b0569b3SLogan Gunthorpe 
2448e26a5843SAllen Hubbe 	db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2449e26a5843SAllen Hubbe 		   ntb_db_vector_mask(nt->ndev, vector));
2450e26a5843SAllen Hubbe 
2451e26a5843SAllen Hubbe 	while (db_bits) {
2452e26a5843SAllen Hubbe 		qp_num = __ffs(db_bits);
2453e26a5843SAllen Hubbe 		qp = &nt->qp_vec[qp_num];
2454e26a5843SAllen Hubbe 
2455e9021331SDave Jiang 		if (qp->active)
2456e26a5843SAllen Hubbe 			tasklet_schedule(&qp->rxc_db_work);
2457e26a5843SAllen Hubbe 
2458e26a5843SAllen Hubbe 		db_bits &= ~BIT_ULL(qp_num);
2459e26a5843SAllen Hubbe 	}
2460e26a5843SAllen Hubbe }
2461e26a5843SAllen Hubbe 
2462e26a5843SAllen Hubbe static const struct ntb_ctx_ops ntb_transport_ops = {
2463e26a5843SAllen Hubbe 	.link_event = ntb_transport_event_callback,
2464e26a5843SAllen Hubbe 	.db_event = ntb_transport_doorbell_callback,
2465e26a5843SAllen Hubbe };
2466e26a5843SAllen Hubbe 
2467e26a5843SAllen Hubbe static struct ntb_client ntb_transport_client = {
2468e26a5843SAllen Hubbe 	.ops = {
2469e26a5843SAllen Hubbe 		.probe = ntb_transport_probe,
2470e26a5843SAllen Hubbe 		.remove = ntb_transport_free,
2471e26a5843SAllen Hubbe 	},
2472e26a5843SAllen Hubbe };
2473e26a5843SAllen Hubbe 
2474e26a5843SAllen Hubbe static int __init ntb_transport_init(void)
2475e26a5843SAllen Hubbe {
2476e26a5843SAllen Hubbe 	int rc;
2477e26a5843SAllen Hubbe 
24787eb38781SDave Jiang 	pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
24797eb38781SDave Jiang 
2480e26a5843SAllen Hubbe 	if (debugfs_initialized())
2481e26a5843SAllen Hubbe 		nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2482e26a5843SAllen Hubbe 
2483e26a5843SAllen Hubbe 	rc = bus_register(&ntb_transport_bus);
2484e26a5843SAllen Hubbe 	if (rc)
2485e26a5843SAllen Hubbe 		goto err_bus;
2486e26a5843SAllen Hubbe 
2487e26a5843SAllen Hubbe 	rc = ntb_register_client(&ntb_transport_client);
2488e26a5843SAllen Hubbe 	if (rc)
2489e26a5843SAllen Hubbe 		goto err_client;
2490e26a5843SAllen Hubbe 
2491e26a5843SAllen Hubbe 	return 0;
2492e26a5843SAllen Hubbe 
2493e26a5843SAllen Hubbe err_client:
2494e26a5843SAllen Hubbe 	bus_unregister(&ntb_transport_bus);
2495e26a5843SAllen Hubbe err_bus:
2496e26a5843SAllen Hubbe 	debugfs_remove_recursive(nt_debugfs_dir);
2497e26a5843SAllen Hubbe 	return rc;
2498e26a5843SAllen Hubbe }
2499e26a5843SAllen Hubbe module_init(ntb_transport_init);
2500e26a5843SAllen Hubbe 
2501e26a5843SAllen Hubbe static void __exit ntb_transport_exit(void)
2502e26a5843SAllen Hubbe {
2503e26a5843SAllen Hubbe 	ntb_unregister_client(&ntb_transport_client);
2504e26a5843SAllen Hubbe 	bus_unregister(&ntb_transport_bus);
2505dd62245eSAllen Hubbe 	debugfs_remove_recursive(nt_debugfs_dir);
2506e26a5843SAllen Hubbe }
2507e26a5843SAllen Hubbe module_exit(ntb_transport_exit);
2508