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