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