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