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