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