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