xref: /linux/drivers/ntb/ntb_transport.c (revision ca55b2fef3a9373fcfc30f82fd26bc7fccbda732)
1 /*
2  * This file is provided under a dual BSD/GPLv2 license.  When using or
3  *   redistributing this file, you may do so under either license.
4  *
5  *   GPL LICENSE SUMMARY
6  *
7  *   Copyright(c) 2012 Intel Corporation. All rights reserved.
8  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
9  *
10  *   This program is free software; you can redistribute it and/or modify
11  *   it under the terms of version 2 of the GNU General Public License as
12  *   published by the Free Software Foundation.
13  *
14  *   BSD LICENSE
15  *
16  *   Copyright(c) 2012 Intel Corporation. All rights reserved.
17  *   Copyright (C) 2015 EMC Corporation. All Rights Reserved.
18  *
19  *   Redistribution and use in source and binary forms, with or without
20  *   modification, are permitted provided that the following conditions
21  *   are met:
22  *
23  *     * Redistributions of source code must retain the above copyright
24  *       notice, this list of conditions and the following disclaimer.
25  *     * Redistributions in binary form must reproduce the above copy
26  *       notice, this list of conditions and the following disclaimer in
27  *       the documentation and/or other materials provided with the
28  *       distribution.
29  *     * Neither the name of Intel Corporation nor the names of its
30  *       contributors may be used to endorse or promote products derived
31  *       from this software without specific prior written permission.
32  *
33  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
34  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
35  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
36  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
37  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
40  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
41  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
43  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  *
45  * PCIe NTB Transport Linux driver
46  *
47  * Contact Information:
48  * Jon Mason <jon.mason@intel.com>
49  */
50 #include <linux/debugfs.h>
51 #include <linux/delay.h>
52 #include <linux/dmaengine.h>
53 #include <linux/dma-mapping.h>
54 #include <linux/errno.h>
55 #include <linux/export.h>
56 #include <linux/interrupt.h>
57 #include <linux/module.h>
58 #include <linux/pci.h>
59 #include <linux/slab.h>
60 #include <linux/types.h>
61 #include <linux/uaccess.h>
62 #include "linux/ntb.h"
63 #include "linux/ntb_transport.h"
64 
65 #define NTB_TRANSPORT_VERSION	4
66 #define NTB_TRANSPORT_VER	"4"
67 #define NTB_TRANSPORT_NAME	"ntb_transport"
68 #define NTB_TRANSPORT_DESC	"Software Queue-Pair Transport over NTB"
69 
70 MODULE_DESCRIPTION(NTB_TRANSPORT_DESC);
71 MODULE_VERSION(NTB_TRANSPORT_VER);
72 MODULE_LICENSE("Dual BSD/GPL");
73 MODULE_AUTHOR("Intel Corporation");
74 
75 static unsigned long max_mw_size;
76 module_param(max_mw_size, ulong, 0644);
77 MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows");
78 
79 static unsigned int transport_mtu = 0x10000;
80 module_param(transport_mtu, uint, 0644);
81 MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets");
82 
83 static unsigned char max_num_clients;
84 module_param(max_num_clients, byte, 0644);
85 MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients");
86 
87 static unsigned int copy_bytes = 1024;
88 module_param(copy_bytes, uint, 0644);
89 MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA");
90 
91 static bool use_dma;
92 module_param(use_dma, bool, 0644);
93 MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy");
94 
95 static struct dentry *nt_debugfs_dir;
96 
97 struct ntb_queue_entry {
98 	/* ntb_queue list reference */
99 	struct list_head entry;
100 	/* pointers to data to be transferred */
101 	void *cb_data;
102 	void *buf;
103 	unsigned int len;
104 	unsigned int flags;
105 
106 	struct ntb_transport_qp *qp;
107 	union {
108 		struct ntb_payload_header __iomem *tx_hdr;
109 		struct ntb_payload_header *rx_hdr;
110 	};
111 	unsigned int index;
112 };
113 
114 struct ntb_rx_info {
115 	unsigned int entry;
116 };
117 
118 struct ntb_transport_qp {
119 	struct ntb_transport_ctx *transport;
120 	struct ntb_dev *ndev;
121 	void *cb_data;
122 	struct dma_chan *tx_dma_chan;
123 	struct dma_chan *rx_dma_chan;
124 
125 	bool client_ready;
126 	bool link_is_up;
127 
128 	u8 qp_num;	/* Only 64 QP's are allowed.  0-63 */
129 	u64 qp_bit;
130 
131 	struct ntb_rx_info __iomem *rx_info;
132 	struct ntb_rx_info *remote_rx_info;
133 
134 	void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
135 			   void *data, int len);
136 	struct list_head tx_free_q;
137 	spinlock_t ntb_tx_free_q_lock;
138 	void __iomem *tx_mw;
139 	dma_addr_t tx_mw_phys;
140 	unsigned int tx_index;
141 	unsigned int tx_max_entry;
142 	unsigned int tx_max_frame;
143 
144 	void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
145 			   void *data, int len);
146 	struct list_head rx_post_q;
147 	struct list_head rx_pend_q;
148 	struct list_head rx_free_q;
149 	/* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
150 	spinlock_t ntb_rx_q_lock;
151 	void *rx_buff;
152 	unsigned int rx_index;
153 	unsigned int rx_max_entry;
154 	unsigned int rx_max_frame;
155 	dma_cookie_t last_cookie;
156 	struct tasklet_struct rxc_db_work;
157 
158 	void (*event_handler)(void *data, int status);
159 	struct delayed_work link_work;
160 	struct work_struct link_cleanup;
161 
162 	struct dentry *debugfs_dir;
163 	struct dentry *debugfs_stats;
164 
165 	/* Stats */
166 	u64 rx_bytes;
167 	u64 rx_pkts;
168 	u64 rx_ring_empty;
169 	u64 rx_err_no_buf;
170 	u64 rx_err_oflow;
171 	u64 rx_err_ver;
172 	u64 rx_memcpy;
173 	u64 rx_async;
174 	u64 tx_bytes;
175 	u64 tx_pkts;
176 	u64 tx_ring_full;
177 	u64 tx_err_no_buf;
178 	u64 tx_memcpy;
179 	u64 tx_async;
180 };
181 
182 struct ntb_transport_mw {
183 	phys_addr_t phys_addr;
184 	resource_size_t phys_size;
185 	resource_size_t xlat_align;
186 	resource_size_t xlat_align_size;
187 	void __iomem *vbase;
188 	size_t xlat_size;
189 	size_t buff_size;
190 	void *virt_addr;
191 	dma_addr_t dma_addr;
192 };
193 
194 struct ntb_transport_client_dev {
195 	struct list_head entry;
196 	struct ntb_transport_ctx *nt;
197 	struct device dev;
198 };
199 
200 struct ntb_transport_ctx {
201 	struct list_head entry;
202 	struct list_head client_devs;
203 
204 	struct ntb_dev *ndev;
205 
206 	struct ntb_transport_mw *mw_vec;
207 	struct ntb_transport_qp *qp_vec;
208 	unsigned int mw_count;
209 	unsigned int qp_count;
210 	u64 qp_bitmap;
211 	u64 qp_bitmap_free;
212 
213 	bool link_is_up;
214 	struct delayed_work link_work;
215 	struct work_struct link_cleanup;
216 
217 	struct dentry *debugfs_node_dir;
218 };
219 
220 enum {
221 	DESC_DONE_FLAG = BIT(0),
222 	LINK_DOWN_FLAG = BIT(1),
223 };
224 
225 struct ntb_payload_header {
226 	unsigned int ver;
227 	unsigned int len;
228 	unsigned int flags;
229 };
230 
231 enum {
232 	VERSION = 0,
233 	QP_LINKS,
234 	NUM_QPS,
235 	NUM_MWS,
236 	MW0_SZ_HIGH,
237 	MW0_SZ_LOW,
238 	MW1_SZ_HIGH,
239 	MW1_SZ_LOW,
240 	MAX_SPAD,
241 };
242 
243 #define dev_client_dev(__dev) \
244 	container_of((__dev), struct ntb_transport_client_dev, dev)
245 
246 #define drv_client(__drv) \
247 	container_of((__drv), struct ntb_transport_client, driver)
248 
249 #define QP_TO_MW(nt, qp)	((qp) % nt->mw_count)
250 #define NTB_QP_DEF_NUM_ENTRIES	100
251 #define NTB_LINK_DOWN_TIMEOUT	10
252 
253 static void ntb_transport_rxc_db(unsigned long data);
254 static const struct ntb_ctx_ops ntb_transport_ops;
255 static struct ntb_client ntb_transport_client;
256 
257 static int ntb_transport_bus_match(struct device *dev,
258 				   struct device_driver *drv)
259 {
260 	return !strncmp(dev_name(dev), drv->name, strlen(drv->name));
261 }
262 
263 static int ntb_transport_bus_probe(struct device *dev)
264 {
265 	const struct ntb_transport_client *client;
266 	int rc = -EINVAL;
267 
268 	get_device(dev);
269 
270 	client = drv_client(dev->driver);
271 	rc = client->probe(dev);
272 	if (rc)
273 		put_device(dev);
274 
275 	return rc;
276 }
277 
278 static int ntb_transport_bus_remove(struct device *dev)
279 {
280 	const struct ntb_transport_client *client;
281 
282 	client = drv_client(dev->driver);
283 	client->remove(dev);
284 
285 	put_device(dev);
286 
287 	return 0;
288 }
289 
290 static struct bus_type ntb_transport_bus = {
291 	.name = "ntb_transport",
292 	.match = ntb_transport_bus_match,
293 	.probe = ntb_transport_bus_probe,
294 	.remove = ntb_transport_bus_remove,
295 };
296 
297 static LIST_HEAD(ntb_transport_list);
298 
299 static int ntb_bus_init(struct ntb_transport_ctx *nt)
300 {
301 	list_add_tail(&nt->entry, &ntb_transport_list);
302 	return 0;
303 }
304 
305 static void ntb_bus_remove(struct ntb_transport_ctx *nt)
306 {
307 	struct ntb_transport_client_dev *client_dev, *cd;
308 
309 	list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) {
310 		dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n",
311 			dev_name(&client_dev->dev));
312 		list_del(&client_dev->entry);
313 		device_unregister(&client_dev->dev);
314 	}
315 
316 	list_del(&nt->entry);
317 }
318 
319 static void ntb_transport_client_release(struct device *dev)
320 {
321 	struct ntb_transport_client_dev *client_dev;
322 
323 	client_dev = dev_client_dev(dev);
324 	kfree(client_dev);
325 }
326 
327 /**
328  * ntb_transport_unregister_client_dev - Unregister NTB client device
329  * @device_name: Name of NTB client device
330  *
331  * Unregister an NTB client device with the NTB transport layer
332  */
333 void ntb_transport_unregister_client_dev(char *device_name)
334 {
335 	struct ntb_transport_client_dev *client, *cd;
336 	struct ntb_transport_ctx *nt;
337 
338 	list_for_each_entry(nt, &ntb_transport_list, entry)
339 		list_for_each_entry_safe(client, cd, &nt->client_devs, entry)
340 			if (!strncmp(dev_name(&client->dev), device_name,
341 				     strlen(device_name))) {
342 				list_del(&client->entry);
343 				device_unregister(&client->dev);
344 			}
345 }
346 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev);
347 
348 /**
349  * ntb_transport_register_client_dev - Register NTB client device
350  * @device_name: Name of NTB client device
351  *
352  * Register an NTB client device with the NTB transport layer
353  */
354 int ntb_transport_register_client_dev(char *device_name)
355 {
356 	struct ntb_transport_client_dev *client_dev;
357 	struct ntb_transport_ctx *nt;
358 	int node;
359 	int rc, i = 0;
360 
361 	if (list_empty(&ntb_transport_list))
362 		return -ENODEV;
363 
364 	list_for_each_entry(nt, &ntb_transport_list, entry) {
365 		struct device *dev;
366 
367 		node = dev_to_node(&nt->ndev->dev);
368 
369 		client_dev = kzalloc_node(sizeof(*client_dev),
370 					  GFP_KERNEL, node);
371 		if (!client_dev) {
372 			rc = -ENOMEM;
373 			goto err;
374 		}
375 
376 		dev = &client_dev->dev;
377 
378 		/* setup and register client devices */
379 		dev_set_name(dev, "%s%d", device_name, i);
380 		dev->bus = &ntb_transport_bus;
381 		dev->release = ntb_transport_client_release;
382 		dev->parent = &nt->ndev->dev;
383 
384 		rc = device_register(dev);
385 		if (rc) {
386 			kfree(client_dev);
387 			goto err;
388 		}
389 
390 		list_add_tail(&client_dev->entry, &nt->client_devs);
391 		i++;
392 	}
393 
394 	return 0;
395 
396 err:
397 	ntb_transport_unregister_client_dev(device_name);
398 
399 	return rc;
400 }
401 EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev);
402 
403 /**
404  * ntb_transport_register_client - Register NTB client driver
405  * @drv: NTB client driver to be registered
406  *
407  * Register an NTB client driver with the NTB transport layer
408  *
409  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
410  */
411 int ntb_transport_register_client(struct ntb_transport_client *drv)
412 {
413 	drv->driver.bus = &ntb_transport_bus;
414 
415 	if (list_empty(&ntb_transport_list))
416 		return -ENODEV;
417 
418 	return driver_register(&drv->driver);
419 }
420 EXPORT_SYMBOL_GPL(ntb_transport_register_client);
421 
422 /**
423  * ntb_transport_unregister_client - Unregister NTB client driver
424  * @drv: NTB client driver to be unregistered
425  *
426  * Unregister an NTB client driver with the NTB transport layer
427  *
428  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
429  */
430 void ntb_transport_unregister_client(struct ntb_transport_client *drv)
431 {
432 	driver_unregister(&drv->driver);
433 }
434 EXPORT_SYMBOL_GPL(ntb_transport_unregister_client);
435 
436 static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count,
437 			    loff_t *offp)
438 {
439 	struct ntb_transport_qp *qp;
440 	char *buf;
441 	ssize_t ret, out_offset, out_count;
442 
443 	qp = filp->private_data;
444 
445 	if (!qp || !qp->link_is_up)
446 		return 0;
447 
448 	out_count = 1000;
449 
450 	buf = kmalloc(out_count, GFP_KERNEL);
451 	if (!buf)
452 		return -ENOMEM;
453 
454 	out_offset = 0;
455 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
456 			       "\nNTB QP stats:\n\n");
457 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
458 			       "rx_bytes - \t%llu\n", qp->rx_bytes);
459 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
460 			       "rx_pkts - \t%llu\n", qp->rx_pkts);
461 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
462 			       "rx_memcpy - \t%llu\n", qp->rx_memcpy);
463 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
464 			       "rx_async - \t%llu\n", qp->rx_async);
465 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
466 			       "rx_ring_empty - %llu\n", qp->rx_ring_empty);
467 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
468 			       "rx_err_no_buf - %llu\n", qp->rx_err_no_buf);
469 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
470 			       "rx_err_oflow - \t%llu\n", qp->rx_err_oflow);
471 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
472 			       "rx_err_ver - \t%llu\n", qp->rx_err_ver);
473 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
474 			       "rx_buff - \t0x%p\n", qp->rx_buff);
475 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
476 			       "rx_index - \t%u\n", qp->rx_index);
477 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
478 			       "rx_max_entry - \t%u\n\n", qp->rx_max_entry);
479 
480 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
481 			       "tx_bytes - \t%llu\n", qp->tx_bytes);
482 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
483 			       "tx_pkts - \t%llu\n", qp->tx_pkts);
484 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
485 			       "tx_memcpy - \t%llu\n", qp->tx_memcpy);
486 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
487 			       "tx_async - \t%llu\n", qp->tx_async);
488 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
489 			       "tx_ring_full - \t%llu\n", qp->tx_ring_full);
490 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
491 			       "tx_err_no_buf - %llu\n", qp->tx_err_no_buf);
492 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
493 			       "tx_mw - \t0x%p\n", qp->tx_mw);
494 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
495 			       "tx_index (H) - \t%u\n", qp->tx_index);
496 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
497 			       "RRI (T) - \t%u\n",
498 			       qp->remote_rx_info->entry);
499 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
500 			       "tx_max_entry - \t%u\n", qp->tx_max_entry);
501 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
502 			       "free tx - \t%u\n",
503 			       ntb_transport_tx_free_entry(qp));
504 
505 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
506 			       "\n");
507 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
508 			       "Using TX DMA - \t%s\n",
509 			       qp->tx_dma_chan ? "Yes" : "No");
510 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
511 			       "Using RX DMA - \t%s\n",
512 			       qp->rx_dma_chan ? "Yes" : "No");
513 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
514 			       "QP Link - \t%s\n",
515 			       qp->link_is_up ? "Up" : "Down");
516 	out_offset += snprintf(buf + out_offset, out_count - out_offset,
517 			       "\n");
518 
519 	if (out_offset > out_count)
520 		out_offset = out_count;
521 
522 	ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
523 	kfree(buf);
524 	return ret;
525 }
526 
527 static const struct file_operations ntb_qp_debugfs_stats = {
528 	.owner = THIS_MODULE,
529 	.open = simple_open,
530 	.read = debugfs_read,
531 };
532 
533 static void ntb_list_add(spinlock_t *lock, struct list_head *entry,
534 			 struct list_head *list)
535 {
536 	unsigned long flags;
537 
538 	spin_lock_irqsave(lock, flags);
539 	list_add_tail(entry, list);
540 	spin_unlock_irqrestore(lock, flags);
541 }
542 
543 static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock,
544 					   struct list_head *list)
545 {
546 	struct ntb_queue_entry *entry;
547 	unsigned long flags;
548 
549 	spin_lock_irqsave(lock, flags);
550 	if (list_empty(list)) {
551 		entry = NULL;
552 		goto out;
553 	}
554 	entry = list_first_entry(list, struct ntb_queue_entry, entry);
555 	list_del(&entry->entry);
556 
557 out:
558 	spin_unlock_irqrestore(lock, flags);
559 
560 	return entry;
561 }
562 
563 static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock,
564 					   struct list_head *list,
565 					   struct list_head *to_list)
566 {
567 	struct ntb_queue_entry *entry;
568 	unsigned long flags;
569 
570 	spin_lock_irqsave(lock, flags);
571 
572 	if (list_empty(list)) {
573 		entry = NULL;
574 	} else {
575 		entry = list_first_entry(list, struct ntb_queue_entry, entry);
576 		list_move_tail(&entry->entry, to_list);
577 	}
578 
579 	spin_unlock_irqrestore(lock, flags);
580 
581 	return entry;
582 }
583 
584 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
585 				     unsigned int qp_num)
586 {
587 	struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
588 	struct ntb_transport_mw *mw;
589 	unsigned int rx_size, num_qps_mw;
590 	unsigned int mw_num, mw_count, qp_count;
591 	unsigned int i;
592 
593 	mw_count = nt->mw_count;
594 	qp_count = nt->qp_count;
595 
596 	mw_num = QP_TO_MW(nt, qp_num);
597 	mw = &nt->mw_vec[mw_num];
598 
599 	if (!mw->virt_addr)
600 		return -ENOMEM;
601 
602 	if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
603 		num_qps_mw = qp_count / mw_count + 1;
604 	else
605 		num_qps_mw = qp_count / mw_count;
606 
607 	rx_size = (unsigned int)mw->xlat_size / num_qps_mw;
608 	qp->rx_buff = mw->virt_addr + rx_size * qp_num / mw_count;
609 	rx_size -= sizeof(struct ntb_rx_info);
610 
611 	qp->remote_rx_info = qp->rx_buff + rx_size;
612 
613 	/* Due to housekeeping, there must be atleast 2 buffs */
614 	qp->rx_max_frame = min(transport_mtu, rx_size / 2);
615 	qp->rx_max_entry = rx_size / qp->rx_max_frame;
616 	qp->rx_index = 0;
617 
618 	qp->remote_rx_info->entry = qp->rx_max_entry - 1;
619 
620 	/* setup the hdr offsets with 0's */
621 	for (i = 0; i < qp->rx_max_entry; i++) {
622 		void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) -
623 				sizeof(struct ntb_payload_header));
624 		memset(offset, 0, sizeof(struct ntb_payload_header));
625 	}
626 
627 	qp->rx_pkts = 0;
628 	qp->tx_pkts = 0;
629 	qp->tx_index = 0;
630 
631 	return 0;
632 }
633 
634 static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
635 {
636 	struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
637 	struct pci_dev *pdev = nt->ndev->pdev;
638 
639 	if (!mw->virt_addr)
640 		return;
641 
642 	ntb_mw_clear_trans(nt->ndev, num_mw);
643 	dma_free_coherent(&pdev->dev, mw->buff_size,
644 			  mw->virt_addr, mw->dma_addr);
645 	mw->xlat_size = 0;
646 	mw->buff_size = 0;
647 	mw->virt_addr = NULL;
648 }
649 
650 static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw,
651 		      resource_size_t size)
652 {
653 	struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
654 	struct pci_dev *pdev = nt->ndev->pdev;
655 	size_t xlat_size, buff_size;
656 	int rc;
657 
658 	if (!size)
659 		return -EINVAL;
660 
661 	xlat_size = round_up(size, mw->xlat_align_size);
662 	buff_size = round_up(size, mw->xlat_align);
663 
664 	/* No need to re-setup */
665 	if (mw->xlat_size == xlat_size)
666 		return 0;
667 
668 	if (mw->buff_size)
669 		ntb_free_mw(nt, num_mw);
670 
671 	/* Alloc memory for receiving data.  Must be aligned */
672 	mw->xlat_size = xlat_size;
673 	mw->buff_size = buff_size;
674 
675 	mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size,
676 					   &mw->dma_addr, GFP_KERNEL);
677 	if (!mw->virt_addr) {
678 		mw->xlat_size = 0;
679 		mw->buff_size = 0;
680 		dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n",
681 			buff_size);
682 		return -ENOMEM;
683 	}
684 
685 	/*
686 	 * we must ensure that the memory address allocated is BAR size
687 	 * aligned in order for the XLAT register to take the value. This
688 	 * is a requirement of the hardware. It is recommended to setup CMA
689 	 * for BAR sizes equal or greater than 4MB.
690 	 */
691 	if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) {
692 		dev_err(&pdev->dev, "DMA memory %pad is not aligned\n",
693 			&mw->dma_addr);
694 		ntb_free_mw(nt, num_mw);
695 		return -ENOMEM;
696 	}
697 
698 	/* Notify HW the memory location of the receive buffer */
699 	rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size);
700 	if (rc) {
701 		dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw);
702 		ntb_free_mw(nt, num_mw);
703 		return -EIO;
704 	}
705 
706 	return 0;
707 }
708 
709 static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
710 {
711 	qp->link_is_up = false;
712 
713 	qp->tx_index = 0;
714 	qp->rx_index = 0;
715 	qp->rx_bytes = 0;
716 	qp->rx_pkts = 0;
717 	qp->rx_ring_empty = 0;
718 	qp->rx_err_no_buf = 0;
719 	qp->rx_err_oflow = 0;
720 	qp->rx_err_ver = 0;
721 	qp->rx_memcpy = 0;
722 	qp->rx_async = 0;
723 	qp->tx_bytes = 0;
724 	qp->tx_pkts = 0;
725 	qp->tx_ring_full = 0;
726 	qp->tx_err_no_buf = 0;
727 	qp->tx_memcpy = 0;
728 	qp->tx_async = 0;
729 }
730 
731 static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
732 {
733 	struct ntb_transport_ctx *nt = qp->transport;
734 	struct pci_dev *pdev = nt->ndev->pdev;
735 
736 	dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num);
737 
738 	cancel_delayed_work_sync(&qp->link_work);
739 	ntb_qp_link_down_reset(qp);
740 
741 	if (qp->event_handler)
742 		qp->event_handler(qp->cb_data, qp->link_is_up);
743 }
744 
745 static void ntb_qp_link_cleanup_work(struct work_struct *work)
746 {
747 	struct ntb_transport_qp *qp = container_of(work,
748 						   struct ntb_transport_qp,
749 						   link_cleanup);
750 	struct ntb_transport_ctx *nt = qp->transport;
751 
752 	ntb_qp_link_cleanup(qp);
753 
754 	if (nt->link_is_up)
755 		schedule_delayed_work(&qp->link_work,
756 				      msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
757 }
758 
759 static void ntb_qp_link_down(struct ntb_transport_qp *qp)
760 {
761 	schedule_work(&qp->link_cleanup);
762 }
763 
764 static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
765 {
766 	struct ntb_transport_qp *qp;
767 	u64 qp_bitmap_alloc;
768 	int i;
769 
770 	qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
771 
772 	/* Pass along the info to any clients */
773 	for (i = 0; i < nt->qp_count; i++)
774 		if (qp_bitmap_alloc & BIT_ULL(i)) {
775 			qp = &nt->qp_vec[i];
776 			ntb_qp_link_cleanup(qp);
777 			cancel_work_sync(&qp->link_cleanup);
778 			cancel_delayed_work_sync(&qp->link_work);
779 		}
780 
781 	if (!nt->link_is_up)
782 		cancel_delayed_work_sync(&nt->link_work);
783 
784 	/* The scratchpad registers keep the values if the remote side
785 	 * goes down, blast them now to give them a sane value the next
786 	 * time they are accessed
787 	 */
788 	for (i = 0; i < MAX_SPAD; i++)
789 		ntb_spad_write(nt->ndev, i, 0);
790 }
791 
792 static void ntb_transport_link_cleanup_work(struct work_struct *work)
793 {
794 	struct ntb_transport_ctx *nt =
795 		container_of(work, struct ntb_transport_ctx, link_cleanup);
796 
797 	ntb_transport_link_cleanup(nt);
798 }
799 
800 static void ntb_transport_event_callback(void *data)
801 {
802 	struct ntb_transport_ctx *nt = data;
803 
804 	if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1)
805 		schedule_delayed_work(&nt->link_work, 0);
806 	else
807 		schedule_work(&nt->link_cleanup);
808 }
809 
810 static void ntb_transport_link_work(struct work_struct *work)
811 {
812 	struct ntb_transport_ctx *nt =
813 		container_of(work, struct ntb_transport_ctx, link_work.work);
814 	struct ntb_dev *ndev = nt->ndev;
815 	struct pci_dev *pdev = ndev->pdev;
816 	resource_size_t size;
817 	u32 val;
818 	int rc, i, spad;
819 
820 	/* send the local info, in the opposite order of the way we read it */
821 	for (i = 0; i < nt->mw_count; i++) {
822 		size = nt->mw_vec[i].phys_size;
823 
824 		if (max_mw_size && size > max_mw_size)
825 			size = max_mw_size;
826 
827 		spad = MW0_SZ_HIGH + (i * 2);
828 		ntb_peer_spad_write(ndev, spad, (u32)(size >> 32));
829 
830 		spad = MW0_SZ_LOW + (i * 2);
831 		ntb_peer_spad_write(ndev, spad, (u32)size);
832 	}
833 
834 	ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count);
835 
836 	ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count);
837 
838 	ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION);
839 
840 	/* Query the remote side for its info */
841 	val = ntb_spad_read(ndev, VERSION);
842 	dev_dbg(&pdev->dev, "Remote version = %d\n", val);
843 	if (val != NTB_TRANSPORT_VERSION)
844 		goto out;
845 
846 	val = ntb_spad_read(ndev, NUM_QPS);
847 	dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val);
848 	if (val != nt->qp_count)
849 		goto out;
850 
851 	val = ntb_spad_read(ndev, NUM_MWS);
852 	dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val);
853 	if (val != nt->mw_count)
854 		goto out;
855 
856 	for (i = 0; i < nt->mw_count; i++) {
857 		u64 val64;
858 
859 		val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2));
860 		val64 = (u64)val << 32;
861 
862 		val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2));
863 		val64 |= val;
864 
865 		dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64);
866 
867 		rc = ntb_set_mw(nt, i, val64);
868 		if (rc)
869 			goto out1;
870 	}
871 
872 	nt->link_is_up = true;
873 
874 	for (i = 0; i < nt->qp_count; i++) {
875 		struct ntb_transport_qp *qp = &nt->qp_vec[i];
876 
877 		ntb_transport_setup_qp_mw(nt, i);
878 
879 		if (qp->client_ready)
880 			schedule_delayed_work(&qp->link_work, 0);
881 	}
882 
883 	return;
884 
885 out1:
886 	for (i = 0; i < nt->mw_count; i++)
887 		ntb_free_mw(nt, i);
888 out:
889 	if (ntb_link_is_up(ndev, NULL, NULL) == 1)
890 		schedule_delayed_work(&nt->link_work,
891 				      msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
892 }
893 
894 static void ntb_qp_link_work(struct work_struct *work)
895 {
896 	struct ntb_transport_qp *qp = container_of(work,
897 						   struct ntb_transport_qp,
898 						   link_work.work);
899 	struct pci_dev *pdev = qp->ndev->pdev;
900 	struct ntb_transport_ctx *nt = qp->transport;
901 	int val;
902 
903 	WARN_ON(!nt->link_is_up);
904 
905 	val = ntb_spad_read(nt->ndev, QP_LINKS);
906 
907 	ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num));
908 
909 	/* query remote spad for qp ready bits */
910 	ntb_peer_spad_read(nt->ndev, QP_LINKS);
911 	dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val);
912 
913 	/* See if the remote side is up */
914 	if (val & BIT(qp->qp_num)) {
915 		dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num);
916 		qp->link_is_up = true;
917 
918 		if (qp->event_handler)
919 			qp->event_handler(qp->cb_data, qp->link_is_up);
920 
921 		tasklet_schedule(&qp->rxc_db_work);
922 	} else if (nt->link_is_up)
923 		schedule_delayed_work(&qp->link_work,
924 				      msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT));
925 }
926 
927 static int ntb_transport_init_queue(struct ntb_transport_ctx *nt,
928 				    unsigned int qp_num)
929 {
930 	struct ntb_transport_qp *qp;
931 	struct ntb_transport_mw *mw;
932 	phys_addr_t mw_base;
933 	resource_size_t mw_size;
934 	unsigned int num_qps_mw, tx_size;
935 	unsigned int mw_num, mw_count, qp_count;
936 	u64 qp_offset;
937 
938 	mw_count = nt->mw_count;
939 	qp_count = nt->qp_count;
940 
941 	mw_num = QP_TO_MW(nt, qp_num);
942 	mw = &nt->mw_vec[mw_num];
943 
944 	qp = &nt->qp_vec[qp_num];
945 	qp->qp_num = qp_num;
946 	qp->transport = nt;
947 	qp->ndev = nt->ndev;
948 	qp->client_ready = false;
949 	qp->event_handler = NULL;
950 	ntb_qp_link_down_reset(qp);
951 
952 	if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count)
953 		num_qps_mw = qp_count / mw_count + 1;
954 	else
955 		num_qps_mw = qp_count / mw_count;
956 
957 	mw_base = nt->mw_vec[mw_num].phys_addr;
958 	mw_size = nt->mw_vec[mw_num].phys_size;
959 
960 	tx_size = (unsigned int)mw_size / num_qps_mw;
961 	qp_offset = tx_size * qp_num / mw_count;
962 
963 	qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset;
964 	if (!qp->tx_mw)
965 		return -EINVAL;
966 
967 	qp->tx_mw_phys = mw_base + qp_offset;
968 	if (!qp->tx_mw_phys)
969 		return -EINVAL;
970 
971 	tx_size -= sizeof(struct ntb_rx_info);
972 	qp->rx_info = qp->tx_mw + tx_size;
973 
974 	/* Due to housekeeping, there must be atleast 2 buffs */
975 	qp->tx_max_frame = min(transport_mtu, tx_size / 2);
976 	qp->tx_max_entry = tx_size / qp->tx_max_frame;
977 
978 	if (nt->debugfs_node_dir) {
979 		char debugfs_name[4];
980 
981 		snprintf(debugfs_name, 4, "qp%d", qp_num);
982 		qp->debugfs_dir = debugfs_create_dir(debugfs_name,
983 						     nt->debugfs_node_dir);
984 
985 		qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR,
986 							qp->debugfs_dir, qp,
987 							&ntb_qp_debugfs_stats);
988 	} else {
989 		qp->debugfs_dir = NULL;
990 		qp->debugfs_stats = NULL;
991 	}
992 
993 	INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work);
994 	INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work);
995 
996 	spin_lock_init(&qp->ntb_rx_q_lock);
997 	spin_lock_init(&qp->ntb_tx_free_q_lock);
998 
999 	INIT_LIST_HEAD(&qp->rx_post_q);
1000 	INIT_LIST_HEAD(&qp->rx_pend_q);
1001 	INIT_LIST_HEAD(&qp->rx_free_q);
1002 	INIT_LIST_HEAD(&qp->tx_free_q);
1003 
1004 	tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db,
1005 		     (unsigned long)qp);
1006 
1007 	return 0;
1008 }
1009 
1010 static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev)
1011 {
1012 	struct ntb_transport_ctx *nt;
1013 	struct ntb_transport_mw *mw;
1014 	unsigned int mw_count, qp_count;
1015 	u64 qp_bitmap;
1016 	int node;
1017 	int rc, i;
1018 
1019 	if (ntb_db_is_unsafe(ndev))
1020 		dev_dbg(&ndev->dev,
1021 			"doorbell is unsafe, proceed anyway...\n");
1022 	if (ntb_spad_is_unsafe(ndev))
1023 		dev_dbg(&ndev->dev,
1024 			"scratchpad is unsafe, proceed anyway...\n");
1025 
1026 	node = dev_to_node(&ndev->dev);
1027 
1028 	nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node);
1029 	if (!nt)
1030 		return -ENOMEM;
1031 
1032 	nt->ndev = ndev;
1033 
1034 	mw_count = ntb_mw_count(ndev);
1035 
1036 	nt->mw_count = mw_count;
1037 
1038 	nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec),
1039 				  GFP_KERNEL, node);
1040 	if (!nt->mw_vec) {
1041 		rc = -ENOMEM;
1042 		goto err;
1043 	}
1044 
1045 	for (i = 0; i < mw_count; i++) {
1046 		mw = &nt->mw_vec[i];
1047 
1048 		rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size,
1049 				      &mw->xlat_align, &mw->xlat_align_size);
1050 		if (rc)
1051 			goto err1;
1052 
1053 		mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size);
1054 		if (!mw->vbase) {
1055 			rc = -ENOMEM;
1056 			goto err1;
1057 		}
1058 
1059 		mw->buff_size = 0;
1060 		mw->xlat_size = 0;
1061 		mw->virt_addr = NULL;
1062 		mw->dma_addr = 0;
1063 	}
1064 
1065 	qp_bitmap = ntb_db_valid_mask(ndev);
1066 
1067 	qp_count = ilog2(qp_bitmap);
1068 	if (max_num_clients && max_num_clients < qp_count)
1069 		qp_count = max_num_clients;
1070 	else if (mw_count < qp_count)
1071 		qp_count = mw_count;
1072 
1073 	qp_bitmap &= BIT_ULL(qp_count) - 1;
1074 
1075 	nt->qp_count = qp_count;
1076 	nt->qp_bitmap = qp_bitmap;
1077 	nt->qp_bitmap_free = qp_bitmap;
1078 
1079 	nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec),
1080 				  GFP_KERNEL, node);
1081 	if (!nt->qp_vec) {
1082 		rc = -ENOMEM;
1083 		goto err2;
1084 	}
1085 
1086 	if (nt_debugfs_dir) {
1087 		nt->debugfs_node_dir =
1088 			debugfs_create_dir(pci_name(ndev->pdev),
1089 					   nt_debugfs_dir);
1090 	}
1091 
1092 	for (i = 0; i < qp_count; i++) {
1093 		rc = ntb_transport_init_queue(nt, i);
1094 		if (rc)
1095 			goto err3;
1096 	}
1097 
1098 	INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work);
1099 	INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work);
1100 
1101 	rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops);
1102 	if (rc)
1103 		goto err3;
1104 
1105 	INIT_LIST_HEAD(&nt->client_devs);
1106 	rc = ntb_bus_init(nt);
1107 	if (rc)
1108 		goto err4;
1109 
1110 	nt->link_is_up = false;
1111 	ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
1112 	ntb_link_event(ndev);
1113 
1114 	return 0;
1115 
1116 err4:
1117 	ntb_clear_ctx(ndev);
1118 err3:
1119 	kfree(nt->qp_vec);
1120 err2:
1121 	kfree(nt->mw_vec);
1122 err1:
1123 	while (i--) {
1124 		mw = &nt->mw_vec[i];
1125 		iounmap(mw->vbase);
1126 	}
1127 err:
1128 	kfree(nt);
1129 	return rc;
1130 }
1131 
1132 static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev)
1133 {
1134 	struct ntb_transport_ctx *nt = ndev->ctx;
1135 	struct ntb_transport_qp *qp;
1136 	u64 qp_bitmap_alloc;
1137 	int i;
1138 
1139 	ntb_transport_link_cleanup(nt);
1140 	cancel_work_sync(&nt->link_cleanup);
1141 	cancel_delayed_work_sync(&nt->link_work);
1142 
1143 	qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free;
1144 
1145 	/* verify that all the qp's are freed */
1146 	for (i = 0; i < nt->qp_count; i++) {
1147 		qp = &nt->qp_vec[i];
1148 		if (qp_bitmap_alloc & BIT_ULL(i))
1149 			ntb_transport_free_queue(qp);
1150 		debugfs_remove_recursive(qp->debugfs_dir);
1151 	}
1152 
1153 	ntb_link_disable(ndev);
1154 	ntb_clear_ctx(ndev);
1155 
1156 	ntb_bus_remove(nt);
1157 
1158 	for (i = nt->mw_count; i--; ) {
1159 		ntb_free_mw(nt, i);
1160 		iounmap(nt->mw_vec[i].vbase);
1161 	}
1162 
1163 	kfree(nt->qp_vec);
1164 	kfree(nt->mw_vec);
1165 	kfree(nt);
1166 }
1167 
1168 static void ntb_complete_rxc(struct ntb_transport_qp *qp)
1169 {
1170 	struct ntb_queue_entry *entry;
1171 	void *cb_data;
1172 	unsigned int len;
1173 	unsigned long irqflags;
1174 
1175 	spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1176 
1177 	while (!list_empty(&qp->rx_post_q)) {
1178 		entry = list_first_entry(&qp->rx_post_q,
1179 					 struct ntb_queue_entry, entry);
1180 		if (!(entry->flags & DESC_DONE_FLAG))
1181 			break;
1182 
1183 		entry->rx_hdr->flags = 0;
1184 		iowrite32(entry->index, &qp->rx_info->entry);
1185 
1186 		cb_data = entry->cb_data;
1187 		len = entry->len;
1188 
1189 		list_move_tail(&entry->entry, &qp->rx_free_q);
1190 
1191 		spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1192 
1193 		if (qp->rx_handler && qp->client_ready)
1194 			qp->rx_handler(qp, qp->cb_data, cb_data, len);
1195 
1196 		spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags);
1197 	}
1198 
1199 	spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags);
1200 }
1201 
1202 static void ntb_rx_copy_callback(void *data)
1203 {
1204 	struct ntb_queue_entry *entry = data;
1205 
1206 	entry->flags |= DESC_DONE_FLAG;
1207 
1208 	ntb_complete_rxc(entry->qp);
1209 }
1210 
1211 static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset)
1212 {
1213 	void *buf = entry->buf;
1214 	size_t len = entry->len;
1215 
1216 	memcpy(buf, offset, len);
1217 
1218 	/* Ensure that the data is fully copied out before clearing the flag */
1219 	wmb();
1220 
1221 	ntb_rx_copy_callback(entry);
1222 }
1223 
1224 static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset)
1225 {
1226 	struct dma_async_tx_descriptor *txd;
1227 	struct ntb_transport_qp *qp = entry->qp;
1228 	struct dma_chan *chan = qp->rx_dma_chan;
1229 	struct dma_device *device;
1230 	size_t pay_off, buff_off, len;
1231 	struct dmaengine_unmap_data *unmap;
1232 	dma_cookie_t cookie;
1233 	void *buf = entry->buf;
1234 
1235 	len = entry->len;
1236 
1237 	if (!chan)
1238 		goto err;
1239 
1240 	if (len < copy_bytes)
1241 		goto err;
1242 
1243 	device = chan->device;
1244 	pay_off = (size_t)offset & ~PAGE_MASK;
1245 	buff_off = (size_t)buf & ~PAGE_MASK;
1246 
1247 	if (!is_dma_copy_aligned(device, pay_off, buff_off, len))
1248 		goto err;
1249 
1250 	unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT);
1251 	if (!unmap)
1252 		goto err;
1253 
1254 	unmap->len = len;
1255 	unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset),
1256 				      pay_off, len, DMA_TO_DEVICE);
1257 	if (dma_mapping_error(device->dev, unmap->addr[0]))
1258 		goto err_get_unmap;
1259 
1260 	unmap->to_cnt = 1;
1261 
1262 	unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf),
1263 				      buff_off, len, DMA_FROM_DEVICE);
1264 	if (dma_mapping_error(device->dev, unmap->addr[1]))
1265 		goto err_get_unmap;
1266 
1267 	unmap->from_cnt = 1;
1268 
1269 	txd = device->device_prep_dma_memcpy(chan, unmap->addr[1],
1270 					     unmap->addr[0], len,
1271 					     DMA_PREP_INTERRUPT);
1272 	if (!txd)
1273 		goto err_get_unmap;
1274 
1275 	txd->callback = ntb_rx_copy_callback;
1276 	txd->callback_param = entry;
1277 	dma_set_unmap(txd, unmap);
1278 
1279 	cookie = dmaengine_submit(txd);
1280 	if (dma_submit_error(cookie))
1281 		goto err_set_unmap;
1282 
1283 	dmaengine_unmap_put(unmap);
1284 
1285 	qp->last_cookie = cookie;
1286 
1287 	qp->rx_async++;
1288 
1289 	return;
1290 
1291 err_set_unmap:
1292 	dmaengine_unmap_put(unmap);
1293 err_get_unmap:
1294 	dmaengine_unmap_put(unmap);
1295 err:
1296 	ntb_memcpy_rx(entry, offset);
1297 	qp->rx_memcpy++;
1298 }
1299 
1300 static int ntb_process_rxc(struct ntb_transport_qp *qp)
1301 {
1302 	struct ntb_payload_header *hdr;
1303 	struct ntb_queue_entry *entry;
1304 	void *offset;
1305 
1306 	offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1307 	hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header);
1308 
1309 	dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n",
1310 		qp->qp_num, hdr->ver, hdr->len, hdr->flags);
1311 
1312 	if (!(hdr->flags & DESC_DONE_FLAG)) {
1313 		dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n");
1314 		qp->rx_ring_empty++;
1315 		return -EAGAIN;
1316 	}
1317 
1318 	if (hdr->flags & LINK_DOWN_FLAG) {
1319 		dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n");
1320 		ntb_qp_link_down(qp);
1321 		hdr->flags = 0;
1322 		return -EAGAIN;
1323 	}
1324 
1325 	if (hdr->ver != (u32)qp->rx_pkts) {
1326 		dev_dbg(&qp->ndev->pdev->dev,
1327 			"version mismatch, expected %llu - got %u\n",
1328 			qp->rx_pkts, hdr->ver);
1329 		qp->rx_err_ver++;
1330 		return -EIO;
1331 	}
1332 
1333 	entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
1334 	if (!entry) {
1335 		dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n");
1336 		qp->rx_err_no_buf++;
1337 		return -EAGAIN;
1338 	}
1339 
1340 	entry->rx_hdr = hdr;
1341 	entry->index = qp->rx_index;
1342 
1343 	if (hdr->len > entry->len) {
1344 		dev_dbg(&qp->ndev->pdev->dev,
1345 			"receive buffer overflow! Wanted %d got %d\n",
1346 			hdr->len, entry->len);
1347 		qp->rx_err_oflow++;
1348 
1349 		entry->len = -EIO;
1350 		entry->flags |= DESC_DONE_FLAG;
1351 
1352 		ntb_complete_rxc(qp);
1353 	} else {
1354 		dev_dbg(&qp->ndev->pdev->dev,
1355 			"RX OK index %u ver %u size %d into buf size %d\n",
1356 			qp->rx_index, hdr->ver, hdr->len, entry->len);
1357 
1358 		qp->rx_bytes += hdr->len;
1359 		qp->rx_pkts++;
1360 
1361 		entry->len = hdr->len;
1362 
1363 		ntb_async_rx(entry, offset);
1364 	}
1365 
1366 	qp->rx_index++;
1367 	qp->rx_index %= qp->rx_max_entry;
1368 
1369 	return 0;
1370 }
1371 
1372 static void ntb_transport_rxc_db(unsigned long data)
1373 {
1374 	struct ntb_transport_qp *qp = (void *)data;
1375 	int rc, i;
1376 
1377 	dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n",
1378 		__func__, qp->qp_num);
1379 
1380 	/* Limit the number of packets processed in a single interrupt to
1381 	 * provide fairness to others
1382 	 */
1383 	for (i = 0; i < qp->rx_max_entry; i++) {
1384 		rc = ntb_process_rxc(qp);
1385 		if (rc)
1386 			break;
1387 	}
1388 
1389 	if (i && qp->rx_dma_chan)
1390 		dma_async_issue_pending(qp->rx_dma_chan);
1391 
1392 	if (i == qp->rx_max_entry) {
1393 		/* there is more work to do */
1394 		tasklet_schedule(&qp->rxc_db_work);
1395 	} else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) {
1396 		/* the doorbell bit is set: clear it */
1397 		ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num));
1398 		/* ntb_db_read ensures ntb_db_clear write is committed */
1399 		ntb_db_read(qp->ndev);
1400 
1401 		/* an interrupt may have arrived between finishing
1402 		 * ntb_process_rxc and clearing the doorbell bit:
1403 		 * there might be some more work to do.
1404 		 */
1405 		tasklet_schedule(&qp->rxc_db_work);
1406 	}
1407 }
1408 
1409 static void ntb_tx_copy_callback(void *data)
1410 {
1411 	struct ntb_queue_entry *entry = data;
1412 	struct ntb_transport_qp *qp = entry->qp;
1413 	struct ntb_payload_header __iomem *hdr = entry->tx_hdr;
1414 
1415 	iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags);
1416 
1417 	ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num));
1418 
1419 	/* The entry length can only be zero if the packet is intended to be a
1420 	 * "link down" or similar.  Since no payload is being sent in these
1421 	 * cases, there is nothing to add to the completion queue.
1422 	 */
1423 	if (entry->len > 0) {
1424 		qp->tx_bytes += entry->len;
1425 
1426 		if (qp->tx_handler)
1427 			qp->tx_handler(qp, qp->cb_data, entry->cb_data,
1428 				       entry->len);
1429 	}
1430 
1431 	ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q);
1432 }
1433 
1434 static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset)
1435 {
1436 #ifdef ARCH_HAS_NOCACHE_UACCESS
1437 	/*
1438 	 * Using non-temporal mov to improve performance on non-cached
1439 	 * writes, even though we aren't actually copying from user space.
1440 	 */
1441 	__copy_from_user_inatomic_nocache(offset, entry->buf, entry->len);
1442 #else
1443 	memcpy_toio(offset, entry->buf, entry->len);
1444 #endif
1445 
1446 	/* Ensure that the data is fully copied out before setting the flags */
1447 	wmb();
1448 
1449 	ntb_tx_copy_callback(entry);
1450 }
1451 
1452 static void ntb_async_tx(struct ntb_transport_qp *qp,
1453 			 struct ntb_queue_entry *entry)
1454 {
1455 	struct ntb_payload_header __iomem *hdr;
1456 	struct dma_async_tx_descriptor *txd;
1457 	struct dma_chan *chan = qp->tx_dma_chan;
1458 	struct dma_device *device;
1459 	size_t dest_off, buff_off;
1460 	struct dmaengine_unmap_data *unmap;
1461 	dma_addr_t dest;
1462 	dma_cookie_t cookie;
1463 	void __iomem *offset;
1464 	size_t len = entry->len;
1465 	void *buf = entry->buf;
1466 
1467 	offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
1468 	hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header);
1469 	entry->tx_hdr = hdr;
1470 
1471 	iowrite32(entry->len, &hdr->len);
1472 	iowrite32((u32)qp->tx_pkts, &hdr->ver);
1473 
1474 	if (!chan)
1475 		goto err;
1476 
1477 	if (len < copy_bytes)
1478 		goto err;
1479 
1480 	device = chan->device;
1481 	dest = qp->tx_mw_phys + qp->tx_max_frame * qp->tx_index;
1482 	buff_off = (size_t)buf & ~PAGE_MASK;
1483 	dest_off = (size_t)dest & ~PAGE_MASK;
1484 
1485 	if (!is_dma_copy_aligned(device, buff_off, dest_off, len))
1486 		goto err;
1487 
1488 	unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT);
1489 	if (!unmap)
1490 		goto err;
1491 
1492 	unmap->len = len;
1493 	unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf),
1494 				      buff_off, len, DMA_TO_DEVICE);
1495 	if (dma_mapping_error(device->dev, unmap->addr[0]))
1496 		goto err_get_unmap;
1497 
1498 	unmap->to_cnt = 1;
1499 
1500 	txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], len,
1501 					     DMA_PREP_INTERRUPT);
1502 	if (!txd)
1503 		goto err_get_unmap;
1504 
1505 	txd->callback = ntb_tx_copy_callback;
1506 	txd->callback_param = entry;
1507 	dma_set_unmap(txd, unmap);
1508 
1509 	cookie = dmaengine_submit(txd);
1510 	if (dma_submit_error(cookie))
1511 		goto err_set_unmap;
1512 
1513 	dmaengine_unmap_put(unmap);
1514 
1515 	dma_async_issue_pending(chan);
1516 	qp->tx_async++;
1517 
1518 	return;
1519 err_set_unmap:
1520 	dmaengine_unmap_put(unmap);
1521 err_get_unmap:
1522 	dmaengine_unmap_put(unmap);
1523 err:
1524 	ntb_memcpy_tx(entry, offset);
1525 	qp->tx_memcpy++;
1526 }
1527 
1528 static int ntb_process_tx(struct ntb_transport_qp *qp,
1529 			  struct ntb_queue_entry *entry)
1530 {
1531 	if (qp->tx_index == qp->remote_rx_info->entry) {
1532 		qp->tx_ring_full++;
1533 		return -EAGAIN;
1534 	}
1535 
1536 	if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
1537 		if (qp->tx_handler)
1538 			qp->tx_handler(qp->cb_data, qp, NULL, -EIO);
1539 
1540 		ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1541 			     &qp->tx_free_q);
1542 		return 0;
1543 	}
1544 
1545 	ntb_async_tx(qp, entry);
1546 
1547 	qp->tx_index++;
1548 	qp->tx_index %= qp->tx_max_entry;
1549 
1550 	qp->tx_pkts++;
1551 
1552 	return 0;
1553 }
1554 
1555 static void ntb_send_link_down(struct ntb_transport_qp *qp)
1556 {
1557 	struct pci_dev *pdev = qp->ndev->pdev;
1558 	struct ntb_queue_entry *entry;
1559 	int i, rc;
1560 
1561 	if (!qp->link_is_up)
1562 		return;
1563 
1564 	dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num);
1565 
1566 	for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1567 		entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1568 		if (entry)
1569 			break;
1570 		msleep(100);
1571 	}
1572 
1573 	if (!entry)
1574 		return;
1575 
1576 	entry->cb_data = NULL;
1577 	entry->buf = NULL;
1578 	entry->len = 0;
1579 	entry->flags = LINK_DOWN_FLAG;
1580 
1581 	rc = ntb_process_tx(qp, entry);
1582 	if (rc)
1583 		dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n",
1584 			qp->qp_num);
1585 
1586 	ntb_qp_link_down_reset(qp);
1587 }
1588 
1589 static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node)
1590 {
1591 	return dev_to_node(&chan->dev->device) == (int)(unsigned long)node;
1592 }
1593 
1594 /**
1595  * ntb_transport_create_queue - Create a new NTB transport layer queue
1596  * @rx_handler: receive callback function
1597  * @tx_handler: transmit callback function
1598  * @event_handler: event callback function
1599  *
1600  * Create a new NTB transport layer queue and provide the queue with a callback
1601  * routine for both transmit and receive.  The receive callback routine will be
1602  * used to pass up data when the transport has received it on the queue.   The
1603  * transmit callback routine will be called when the transport has completed the
1604  * transmission of the data on the queue and the data is ready to be freed.
1605  *
1606  * RETURNS: pointer to newly created ntb_queue, NULL on error.
1607  */
1608 struct ntb_transport_qp *
1609 ntb_transport_create_queue(void *data, struct device *client_dev,
1610 			   const struct ntb_queue_handlers *handlers)
1611 {
1612 	struct ntb_dev *ndev;
1613 	struct pci_dev *pdev;
1614 	struct ntb_transport_ctx *nt;
1615 	struct ntb_queue_entry *entry;
1616 	struct ntb_transport_qp *qp;
1617 	u64 qp_bit;
1618 	unsigned int free_queue;
1619 	dma_cap_mask_t dma_mask;
1620 	int node;
1621 	int i;
1622 
1623 	ndev = dev_ntb(client_dev->parent);
1624 	pdev = ndev->pdev;
1625 	nt = ndev->ctx;
1626 
1627 	node = dev_to_node(&ndev->dev);
1628 
1629 	free_queue = ffs(nt->qp_bitmap);
1630 	if (!free_queue)
1631 		goto err;
1632 
1633 	/* decrement free_queue to make it zero based */
1634 	free_queue--;
1635 
1636 	qp = &nt->qp_vec[free_queue];
1637 	qp_bit = BIT_ULL(qp->qp_num);
1638 
1639 	nt->qp_bitmap_free &= ~qp_bit;
1640 
1641 	qp->cb_data = data;
1642 	qp->rx_handler = handlers->rx_handler;
1643 	qp->tx_handler = handlers->tx_handler;
1644 	qp->event_handler = handlers->event_handler;
1645 
1646 	dma_cap_zero(dma_mask);
1647 	dma_cap_set(DMA_MEMCPY, dma_mask);
1648 
1649 	if (use_dma) {
1650 		qp->tx_dma_chan =
1651 			dma_request_channel(dma_mask, ntb_dma_filter_fn,
1652 					    (void *)(unsigned long)node);
1653 		if (!qp->tx_dma_chan)
1654 			dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n");
1655 
1656 		qp->rx_dma_chan =
1657 			dma_request_channel(dma_mask, ntb_dma_filter_fn,
1658 					    (void *)(unsigned long)node);
1659 		if (!qp->rx_dma_chan)
1660 			dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n");
1661 	} else {
1662 		qp->tx_dma_chan = NULL;
1663 		qp->rx_dma_chan = NULL;
1664 	}
1665 
1666 	dev_dbg(&pdev->dev, "Using %s memcpy for TX\n",
1667 		qp->tx_dma_chan ? "DMA" : "CPU");
1668 
1669 	dev_dbg(&pdev->dev, "Using %s memcpy for RX\n",
1670 		qp->rx_dma_chan ? "DMA" : "CPU");
1671 
1672 	for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1673 		entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
1674 		if (!entry)
1675 			goto err1;
1676 
1677 		entry->qp = qp;
1678 		ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry,
1679 			     &qp->rx_free_q);
1680 	}
1681 
1682 	for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
1683 		entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node);
1684 		if (!entry)
1685 			goto err2;
1686 
1687 		entry->qp = qp;
1688 		ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1689 			     &qp->tx_free_q);
1690 	}
1691 
1692 	ntb_db_clear(qp->ndev, qp_bit);
1693 	ntb_db_clear_mask(qp->ndev, qp_bit);
1694 
1695 	dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num);
1696 
1697 	return qp;
1698 
1699 err2:
1700 	while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1701 		kfree(entry);
1702 err1:
1703 	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
1704 		kfree(entry);
1705 	if (qp->tx_dma_chan)
1706 		dma_release_channel(qp->tx_dma_chan);
1707 	if (qp->rx_dma_chan)
1708 		dma_release_channel(qp->rx_dma_chan);
1709 	nt->qp_bitmap_free |= qp_bit;
1710 err:
1711 	return NULL;
1712 }
1713 EXPORT_SYMBOL_GPL(ntb_transport_create_queue);
1714 
1715 /**
1716  * ntb_transport_free_queue - Frees NTB transport queue
1717  * @qp: NTB queue to be freed
1718  *
1719  * Frees NTB transport queue
1720  */
1721 void ntb_transport_free_queue(struct ntb_transport_qp *qp)
1722 {
1723 	struct pci_dev *pdev;
1724 	struct ntb_queue_entry *entry;
1725 	u64 qp_bit;
1726 
1727 	if (!qp)
1728 		return;
1729 
1730 	pdev = qp->ndev->pdev;
1731 
1732 	if (qp->tx_dma_chan) {
1733 		struct dma_chan *chan = qp->tx_dma_chan;
1734 		/* Putting the dma_chan to NULL will force any new traffic to be
1735 		 * processed by the CPU instead of the DAM engine
1736 		 */
1737 		qp->tx_dma_chan = NULL;
1738 
1739 		/* Try to be nice and wait for any queued DMA engine
1740 		 * transactions to process before smashing it with a rock
1741 		 */
1742 		dma_sync_wait(chan, qp->last_cookie);
1743 		dmaengine_terminate_all(chan);
1744 		dma_release_channel(chan);
1745 	}
1746 
1747 	if (qp->rx_dma_chan) {
1748 		struct dma_chan *chan = qp->rx_dma_chan;
1749 		/* Putting the dma_chan to NULL will force any new traffic to be
1750 		 * processed by the CPU instead of the DAM engine
1751 		 */
1752 		qp->rx_dma_chan = NULL;
1753 
1754 		/* Try to be nice and wait for any queued DMA engine
1755 		 * transactions to process before smashing it with a rock
1756 		 */
1757 		dma_sync_wait(chan, qp->last_cookie);
1758 		dmaengine_terminate_all(chan);
1759 		dma_release_channel(chan);
1760 	}
1761 
1762 	qp_bit = BIT_ULL(qp->qp_num);
1763 
1764 	ntb_db_set_mask(qp->ndev, qp_bit);
1765 	tasklet_disable(&qp->rxc_db_work);
1766 
1767 	cancel_delayed_work_sync(&qp->link_work);
1768 
1769 	qp->cb_data = NULL;
1770 	qp->rx_handler = NULL;
1771 	qp->tx_handler = NULL;
1772 	qp->event_handler = NULL;
1773 
1774 	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q)))
1775 		kfree(entry);
1776 
1777 	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) {
1778 		dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n");
1779 		kfree(entry);
1780 	}
1781 
1782 	while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) {
1783 		dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n");
1784 		kfree(entry);
1785 	}
1786 
1787 	while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
1788 		kfree(entry);
1789 
1790 	qp->transport->qp_bitmap_free |= qp_bit;
1791 
1792 	dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num);
1793 }
1794 EXPORT_SYMBOL_GPL(ntb_transport_free_queue);
1795 
1796 /**
1797  * ntb_transport_rx_remove - Dequeues enqueued rx packet
1798  * @qp: NTB queue to be freed
1799  * @len: pointer to variable to write enqueued buffers length
1800  *
1801  * Dequeues unused buffers from receive queue.  Should only be used during
1802  * shutdown of qp.
1803  *
1804  * RETURNS: NULL error value on error, or void* for success.
1805  */
1806 void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len)
1807 {
1808 	struct ntb_queue_entry *entry;
1809 	void *buf;
1810 
1811 	if (!qp || qp->client_ready)
1812 		return NULL;
1813 
1814 	entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q);
1815 	if (!entry)
1816 		return NULL;
1817 
1818 	buf = entry->cb_data;
1819 	*len = entry->len;
1820 
1821 	ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q);
1822 
1823 	return buf;
1824 }
1825 EXPORT_SYMBOL_GPL(ntb_transport_rx_remove);
1826 
1827 /**
1828  * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry
1829  * @qp: NTB transport layer queue the entry is to be enqueued on
1830  * @cb: per buffer pointer for callback function to use
1831  * @data: pointer to data buffer that incoming packets will be copied into
1832  * @len: length of the data buffer
1833  *
1834  * Enqueue a new receive buffer onto the transport queue into which a NTB
1835  * payload can be received into.
1836  *
1837  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1838  */
1839 int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1840 			     unsigned int len)
1841 {
1842 	struct ntb_queue_entry *entry;
1843 
1844 	if (!qp)
1845 		return -EINVAL;
1846 
1847 	entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q);
1848 	if (!entry)
1849 		return -ENOMEM;
1850 
1851 	entry->cb_data = cb;
1852 	entry->buf = data;
1853 	entry->len = len;
1854 	entry->flags = 0;
1855 
1856 	ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q);
1857 
1858 	tasklet_schedule(&qp->rxc_db_work);
1859 
1860 	return 0;
1861 }
1862 EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue);
1863 
1864 /**
1865  * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
1866  * @qp: NTB transport layer queue the entry is to be enqueued on
1867  * @cb: per buffer pointer for callback function to use
1868  * @data: pointer to data buffer that will be sent
1869  * @len: length of the data buffer
1870  *
1871  * Enqueue a new transmit buffer onto the transport queue from which a NTB
1872  * payload will be transmitted.  This assumes that a lock is being held to
1873  * serialize access to the qp.
1874  *
1875  * RETURNS: An appropriate -ERRNO error value on error, or zero for success.
1876  */
1877 int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
1878 			     unsigned int len)
1879 {
1880 	struct ntb_queue_entry *entry;
1881 	int rc;
1882 
1883 	if (!qp || !qp->link_is_up || !len)
1884 		return -EINVAL;
1885 
1886 	entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1887 	if (!entry) {
1888 		qp->tx_err_no_buf++;
1889 		return -EBUSY;
1890 	}
1891 
1892 	entry->cb_data = cb;
1893 	entry->buf = data;
1894 	entry->len = len;
1895 	entry->flags = 0;
1896 
1897 	rc = ntb_process_tx(qp, entry);
1898 	if (rc)
1899 		ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry,
1900 			     &qp->tx_free_q);
1901 
1902 	return rc;
1903 }
1904 EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue);
1905 
1906 /**
1907  * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
1908  * @qp: NTB transport layer queue to be enabled
1909  *
1910  * Notify NTB transport layer of client readiness to use queue
1911  */
1912 void ntb_transport_link_up(struct ntb_transport_qp *qp)
1913 {
1914 	if (!qp)
1915 		return;
1916 
1917 	qp->client_ready = true;
1918 
1919 	if (qp->transport->link_is_up)
1920 		schedule_delayed_work(&qp->link_work, 0);
1921 }
1922 EXPORT_SYMBOL_GPL(ntb_transport_link_up);
1923 
1924 /**
1925  * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1926  * @qp: NTB transport layer queue to be disabled
1927  *
1928  * Notify NTB transport layer of client's desire to no longer receive data on
1929  * transport queue specified.  It is the client's responsibility to ensure all
1930  * entries on queue are purged or otherwise handled appropriately.
1931  */
1932 void ntb_transport_link_down(struct ntb_transport_qp *qp)
1933 {
1934 	struct pci_dev *pdev;
1935 	int val;
1936 
1937 	if (!qp)
1938 		return;
1939 
1940 	pdev = qp->ndev->pdev;
1941 	qp->client_ready = false;
1942 
1943 	val = ntb_spad_read(qp->ndev, QP_LINKS);
1944 
1945 	ntb_peer_spad_write(qp->ndev, QP_LINKS,
1946 			    val & ~BIT(qp->qp_num));
1947 
1948 	if (qp->link_is_up)
1949 		ntb_send_link_down(qp);
1950 	else
1951 		cancel_delayed_work_sync(&qp->link_work);
1952 }
1953 EXPORT_SYMBOL_GPL(ntb_transport_link_down);
1954 
1955 /**
1956  * ntb_transport_link_query - Query transport link state
1957  * @qp: NTB transport layer queue to be queried
1958  *
1959  * Query connectivity to the remote system of the NTB transport queue
1960  *
1961  * RETURNS: true for link up or false for link down
1962  */
1963 bool ntb_transport_link_query(struct ntb_transport_qp *qp)
1964 {
1965 	if (!qp)
1966 		return false;
1967 
1968 	return qp->link_is_up;
1969 }
1970 EXPORT_SYMBOL_GPL(ntb_transport_link_query);
1971 
1972 /**
1973  * ntb_transport_qp_num - Query the qp number
1974  * @qp: NTB transport layer queue to be queried
1975  *
1976  * Query qp number of the NTB transport queue
1977  *
1978  * RETURNS: a zero based number specifying the qp number
1979  */
1980 unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp)
1981 {
1982 	if (!qp)
1983 		return 0;
1984 
1985 	return qp->qp_num;
1986 }
1987 EXPORT_SYMBOL_GPL(ntb_transport_qp_num);
1988 
1989 /**
1990  * ntb_transport_max_size - Query the max payload size of a qp
1991  * @qp: NTB transport layer queue to be queried
1992  *
1993  * Query the maximum payload size permissible on the given qp
1994  *
1995  * RETURNS: the max payload size of a qp
1996  */
1997 unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp)
1998 {
1999 	unsigned int max;
2000 	unsigned int copy_align;
2001 
2002 	if (!qp)
2003 		return 0;
2004 
2005 	if (!qp->tx_dma_chan && !qp->rx_dma_chan)
2006 		return qp->tx_max_frame - sizeof(struct ntb_payload_header);
2007 
2008 	copy_align = max(qp->tx_dma_chan->device->copy_align,
2009 			 qp->rx_dma_chan->device->copy_align);
2010 
2011 	/* If DMA engine usage is possible, try to find the max size for that */
2012 	max = qp->tx_max_frame - sizeof(struct ntb_payload_header);
2013 	max -= max % (1 << copy_align);
2014 
2015 	return max;
2016 }
2017 EXPORT_SYMBOL_GPL(ntb_transport_max_size);
2018 
2019 unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp)
2020 {
2021 	unsigned int head = qp->tx_index;
2022 	unsigned int tail = qp->remote_rx_info->entry;
2023 
2024 	return tail > head ? tail - head : qp->tx_max_entry + tail - head;
2025 }
2026 EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry);
2027 
2028 static void ntb_transport_doorbell_callback(void *data, int vector)
2029 {
2030 	struct ntb_transport_ctx *nt = data;
2031 	struct ntb_transport_qp *qp;
2032 	u64 db_bits;
2033 	unsigned int qp_num;
2034 
2035 	db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free &
2036 		   ntb_db_vector_mask(nt->ndev, vector));
2037 
2038 	while (db_bits) {
2039 		qp_num = __ffs(db_bits);
2040 		qp = &nt->qp_vec[qp_num];
2041 
2042 		tasklet_schedule(&qp->rxc_db_work);
2043 
2044 		db_bits &= ~BIT_ULL(qp_num);
2045 	}
2046 }
2047 
2048 static const struct ntb_ctx_ops ntb_transport_ops = {
2049 	.link_event = ntb_transport_event_callback,
2050 	.db_event = ntb_transport_doorbell_callback,
2051 };
2052 
2053 static struct ntb_client ntb_transport_client = {
2054 	.ops = {
2055 		.probe = ntb_transport_probe,
2056 		.remove = ntb_transport_free,
2057 	},
2058 };
2059 
2060 static int __init ntb_transport_init(void)
2061 {
2062 	int rc;
2063 
2064 	pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER);
2065 
2066 	if (debugfs_initialized())
2067 		nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL);
2068 
2069 	rc = bus_register(&ntb_transport_bus);
2070 	if (rc)
2071 		goto err_bus;
2072 
2073 	rc = ntb_register_client(&ntb_transport_client);
2074 	if (rc)
2075 		goto err_client;
2076 
2077 	return 0;
2078 
2079 err_client:
2080 	bus_unregister(&ntb_transport_bus);
2081 err_bus:
2082 	debugfs_remove_recursive(nt_debugfs_dir);
2083 	return rc;
2084 }
2085 module_init(ntb_transport_init);
2086 
2087 static void __exit ntb_transport_exit(void)
2088 {
2089 	debugfs_remove_recursive(nt_debugfs_dir);
2090 
2091 	ntb_unregister_client(&ntb_transport_client);
2092 	bus_unregister(&ntb_transport_bus);
2093 }
2094 module_exit(ntb_transport_exit);
2095