1fce8a7bbSJon Mason /* 2fce8a7bbSJon Mason * This file is provided under a dual BSD/GPLv2 license. When using or 3fce8a7bbSJon Mason * redistributing this file, you may do so under either license. 4fce8a7bbSJon Mason * 5fce8a7bbSJon Mason * GPL LICENSE SUMMARY 6fce8a7bbSJon Mason * 7fce8a7bbSJon Mason * Copyright(c) 2012 Intel Corporation. All rights reserved. 8e26a5843SAllen Hubbe * Copyright (C) 2015 EMC Corporation. All Rights Reserved. 9fce8a7bbSJon Mason * 10fce8a7bbSJon Mason * This program is free software; you can redistribute it and/or modify 11fce8a7bbSJon Mason * it under the terms of version 2 of the GNU General Public License as 12fce8a7bbSJon Mason * published by the Free Software Foundation. 13fce8a7bbSJon Mason * 14fce8a7bbSJon Mason * BSD LICENSE 15fce8a7bbSJon Mason * 16fce8a7bbSJon Mason * Copyright(c) 2012 Intel Corporation. All rights reserved. 17e26a5843SAllen Hubbe * Copyright (C) 2015 EMC Corporation. All Rights Reserved. 18fce8a7bbSJon Mason * 19fce8a7bbSJon Mason * Redistribution and use in source and binary forms, with or without 20fce8a7bbSJon Mason * modification, are permitted provided that the following conditions 21fce8a7bbSJon Mason * are met: 22fce8a7bbSJon Mason * 23fce8a7bbSJon Mason * * Redistributions of source code must retain the above copyright 24fce8a7bbSJon Mason * notice, this list of conditions and the following disclaimer. 25fce8a7bbSJon Mason * * Redistributions in binary form must reproduce the above copy 26fce8a7bbSJon Mason * notice, this list of conditions and the following disclaimer in 27fce8a7bbSJon Mason * the documentation and/or other materials provided with the 28fce8a7bbSJon Mason * distribution. 29fce8a7bbSJon Mason * * Neither the name of Intel Corporation nor the names of its 30fce8a7bbSJon Mason * contributors may be used to endorse or promote products derived 31fce8a7bbSJon Mason * from this software without specific prior written permission. 32fce8a7bbSJon Mason * 33fce8a7bbSJon Mason * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 34fce8a7bbSJon Mason * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 35fce8a7bbSJon Mason * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 36fce8a7bbSJon Mason * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 37fce8a7bbSJon Mason * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 38fce8a7bbSJon Mason * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 39fce8a7bbSJon Mason * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 40fce8a7bbSJon Mason * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 41fce8a7bbSJon Mason * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 42fce8a7bbSJon Mason * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 43fce8a7bbSJon Mason * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 44fce8a7bbSJon Mason * 45e26a5843SAllen Hubbe * PCIe NTB Transport Linux driver 46fce8a7bbSJon Mason * 47fce8a7bbSJon Mason * Contact Information: 48fce8a7bbSJon Mason * Jon Mason <jon.mason@intel.com> 49fce8a7bbSJon Mason */ 50fce8a7bbSJon Mason #include <linux/debugfs.h> 51fce8a7bbSJon Mason #include <linux/delay.h> 52282a2feeSJon Mason #include <linux/dmaengine.h> 53fce8a7bbSJon Mason #include <linux/dma-mapping.h> 54fce8a7bbSJon Mason #include <linux/errno.h> 55fce8a7bbSJon Mason #include <linux/export.h> 56fce8a7bbSJon Mason #include <linux/interrupt.h> 57fce8a7bbSJon Mason #include <linux/module.h> 58fce8a7bbSJon Mason #include <linux/pci.h> 59fce8a7bbSJon Mason #include <linux/slab.h> 60fce8a7bbSJon Mason #include <linux/types.h> 6106917f75SDave Jiang #include <linux/uaccess.h> 62e26a5843SAllen Hubbe #include "linux/ntb.h" 63e26a5843SAllen Hubbe #include "linux/ntb_transport.h" 64fce8a7bbSJon Mason 65e26a5843SAllen Hubbe #define NTB_TRANSPORT_VERSION 4 66e26a5843SAllen Hubbe #define NTB_TRANSPORT_VER "4" 67e26a5843SAllen Hubbe #define NTB_TRANSPORT_NAME "ntb_transport" 68e26a5843SAllen Hubbe #define NTB_TRANSPORT_DESC "Software Queue-Pair Transport over NTB" 69e26a5843SAllen Hubbe 70e26a5843SAllen Hubbe MODULE_DESCRIPTION(NTB_TRANSPORT_DESC); 71e26a5843SAllen Hubbe MODULE_VERSION(NTB_TRANSPORT_VER); 72e26a5843SAllen Hubbe MODULE_LICENSE("Dual BSD/GPL"); 73e26a5843SAllen Hubbe MODULE_AUTHOR("Intel Corporation"); 74e26a5843SAllen Hubbe 75e26a5843SAllen Hubbe static unsigned long max_mw_size; 76e26a5843SAllen Hubbe module_param(max_mw_size, ulong, 0644); 77e26a5843SAllen Hubbe MODULE_PARM_DESC(max_mw_size, "Limit size of large memory windows"); 78fce8a7bbSJon Mason 799891417dSDave Jiang static unsigned int transport_mtu = 0x10000; 80fce8a7bbSJon Mason module_param(transport_mtu, uint, 0644); 81fce8a7bbSJon Mason MODULE_PARM_DESC(transport_mtu, "Maximum size of NTB transport packets"); 82fce8a7bbSJon Mason 83948d3a65SJon Mason static unsigned char max_num_clients; 84fce8a7bbSJon Mason module_param(max_num_clients, byte, 0644); 85fce8a7bbSJon Mason MODULE_PARM_DESC(max_num_clients, "Maximum number of NTB transport clients"); 86fce8a7bbSJon Mason 87282a2feeSJon Mason static unsigned int copy_bytes = 1024; 88282a2feeSJon Mason module_param(copy_bytes, uint, 0644); 89282a2feeSJon Mason MODULE_PARM_DESC(copy_bytes, "Threshold under which NTB will use the CPU to copy instead of DMA"); 90282a2feeSJon Mason 91a41ef053SDave Jiang static bool use_dma; 92a41ef053SDave Jiang module_param(use_dma, bool, 0644); 93a41ef053SDave Jiang MODULE_PARM_DESC(use_dma, "Use DMA engine to perform large data copy"); 94a41ef053SDave Jiang 95e26a5843SAllen Hubbe static struct dentry *nt_debugfs_dir; 96e26a5843SAllen Hubbe 97fce8a7bbSJon Mason struct ntb_queue_entry { 98fce8a7bbSJon Mason /* ntb_queue list reference */ 99fce8a7bbSJon Mason struct list_head entry; 100e26a5843SAllen Hubbe /* pointers to data to be transferred */ 101fce8a7bbSJon Mason void *cb_data; 102fce8a7bbSJon Mason void *buf; 103fce8a7bbSJon Mason unsigned int len; 104fce8a7bbSJon Mason unsigned int flags; 105282a2feeSJon Mason 106282a2feeSJon Mason struct ntb_transport_qp *qp; 107282a2feeSJon Mason union { 108282a2feeSJon Mason struct ntb_payload_header __iomem *tx_hdr; 109282a2feeSJon Mason struct ntb_payload_header *rx_hdr; 110282a2feeSJon Mason }; 111282a2feeSJon Mason unsigned int index; 112fce8a7bbSJon Mason }; 113fce8a7bbSJon Mason 114793c20e9SJon Mason struct ntb_rx_info { 115793c20e9SJon Mason unsigned int entry; 116793c20e9SJon Mason }; 117793c20e9SJon Mason 118fce8a7bbSJon Mason struct ntb_transport_qp { 119e26a5843SAllen Hubbe struct ntb_transport_ctx *transport; 120e26a5843SAllen Hubbe struct ntb_dev *ndev; 121fce8a7bbSJon Mason void *cb_data; 122569410caSDave Jiang struct dma_chan *tx_dma_chan; 123569410caSDave Jiang struct dma_chan *rx_dma_chan; 124fce8a7bbSJon Mason 125fce8a7bbSJon Mason bool client_ready; 126e26a5843SAllen Hubbe bool link_is_up; 127e9021331SDave Jiang bool active; 128e26a5843SAllen Hubbe 129fce8a7bbSJon Mason u8 qp_num; /* Only 64 QP's are allowed. 0-63 */ 130e26a5843SAllen Hubbe u64 qp_bit; 131fce8a7bbSJon Mason 13274465645SJon Mason struct ntb_rx_info __iomem *rx_info; 133793c20e9SJon Mason struct ntb_rx_info *remote_rx_info; 134793c20e9SJon Mason 135fce8a7bbSJon Mason void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data, 136fce8a7bbSJon Mason void *data, int len); 137fce8a7bbSJon Mason struct list_head tx_free_q; 138fce8a7bbSJon Mason spinlock_t ntb_tx_free_q_lock; 13974465645SJon Mason void __iomem *tx_mw; 140282a2feeSJon Mason dma_addr_t tx_mw_phys; 141793c20e9SJon Mason unsigned int tx_index; 142793c20e9SJon Mason unsigned int tx_max_entry; 143ef114ed5SJon Mason unsigned int tx_max_frame; 144fce8a7bbSJon Mason 145fce8a7bbSJon Mason void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data, 146fce8a7bbSJon Mason void *data, int len); 147da2e5ae5SAllen Hubbe struct list_head rx_post_q; 148fce8a7bbSJon Mason struct list_head rx_pend_q; 149fce8a7bbSJon Mason struct list_head rx_free_q; 150da2e5ae5SAllen Hubbe /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */ 151da2e5ae5SAllen Hubbe spinlock_t ntb_rx_q_lock; 152793c20e9SJon Mason void *rx_buff; 153793c20e9SJon Mason unsigned int rx_index; 154793c20e9SJon Mason unsigned int rx_max_entry; 155ef114ed5SJon Mason unsigned int rx_max_frame; 156*a754a8fcSDave Jiang unsigned int rx_alloc_entry; 157282a2feeSJon Mason dma_cookie_t last_cookie; 158e26a5843SAllen Hubbe struct tasklet_struct rxc_db_work; 159fce8a7bbSJon Mason 160fce8a7bbSJon Mason void (*event_handler)(void *data, int status); 161fce8a7bbSJon Mason struct delayed_work link_work; 1627b4f2d3cSJon Mason struct work_struct link_cleanup; 163fce8a7bbSJon Mason 164fce8a7bbSJon Mason struct dentry *debugfs_dir; 165fce8a7bbSJon Mason struct dentry *debugfs_stats; 166fce8a7bbSJon Mason 167fce8a7bbSJon Mason /* Stats */ 168fce8a7bbSJon Mason u64 rx_bytes; 169fce8a7bbSJon Mason u64 rx_pkts; 170fce8a7bbSJon Mason u64 rx_ring_empty; 171fce8a7bbSJon Mason u64 rx_err_no_buf; 172fce8a7bbSJon Mason u64 rx_err_oflow; 173fce8a7bbSJon Mason u64 rx_err_ver; 174282a2feeSJon Mason u64 rx_memcpy; 175282a2feeSJon Mason u64 rx_async; 1768c874cc1SDave Jiang u64 dma_rx_prep_err; 177fce8a7bbSJon Mason u64 tx_bytes; 178fce8a7bbSJon Mason u64 tx_pkts; 179fce8a7bbSJon Mason u64 tx_ring_full; 180282a2feeSJon Mason u64 tx_err_no_buf; 181282a2feeSJon Mason u64 tx_memcpy; 182282a2feeSJon Mason u64 tx_async; 1838c874cc1SDave Jiang u64 dma_tx_prep_err; 184fce8a7bbSJon Mason }; 185fce8a7bbSJon Mason 186fce8a7bbSJon Mason struct ntb_transport_mw { 187e26a5843SAllen Hubbe phys_addr_t phys_addr; 188e26a5843SAllen Hubbe resource_size_t phys_size; 189e26a5843SAllen Hubbe resource_size_t xlat_align; 190e26a5843SAllen Hubbe resource_size_t xlat_align_size; 191e26a5843SAllen Hubbe void __iomem *vbase; 192e26a5843SAllen Hubbe size_t xlat_size; 193e26a5843SAllen Hubbe size_t buff_size; 194fce8a7bbSJon Mason void *virt_addr; 195fce8a7bbSJon Mason dma_addr_t dma_addr; 196fce8a7bbSJon Mason }; 197fce8a7bbSJon Mason 198fce8a7bbSJon Mason struct ntb_transport_client_dev { 199fce8a7bbSJon Mason struct list_head entry; 200e26a5843SAllen Hubbe struct ntb_transport_ctx *nt; 201fce8a7bbSJon Mason struct device dev; 202fce8a7bbSJon Mason }; 203fce8a7bbSJon Mason 204e26a5843SAllen Hubbe struct ntb_transport_ctx { 205fce8a7bbSJon Mason struct list_head entry; 206fce8a7bbSJon Mason struct list_head client_devs; 207fce8a7bbSJon Mason 208e26a5843SAllen Hubbe struct ntb_dev *ndev; 209e26a5843SAllen Hubbe 210e26a5843SAllen Hubbe struct ntb_transport_mw *mw_vec; 211e26a5843SAllen Hubbe struct ntb_transport_qp *qp_vec; 212e26a5843SAllen Hubbe unsigned int mw_count; 213e26a5843SAllen Hubbe unsigned int qp_count; 214e26a5843SAllen Hubbe u64 qp_bitmap; 215e26a5843SAllen Hubbe u64 qp_bitmap_free; 216e26a5843SAllen Hubbe 217e26a5843SAllen Hubbe bool link_is_up; 218fce8a7bbSJon Mason struct delayed_work link_work; 2197b4f2d3cSJon Mason struct work_struct link_cleanup; 220c8650fd0SDave Jiang 221c8650fd0SDave Jiang struct dentry *debugfs_node_dir; 222fce8a7bbSJon Mason }; 223fce8a7bbSJon Mason 224fce8a7bbSJon Mason enum { 225e26a5843SAllen Hubbe DESC_DONE_FLAG = BIT(0), 226e26a5843SAllen Hubbe LINK_DOWN_FLAG = BIT(1), 227fce8a7bbSJon Mason }; 228fce8a7bbSJon Mason 229fce8a7bbSJon Mason struct ntb_payload_header { 23074465645SJon Mason unsigned int ver; 231fce8a7bbSJon Mason unsigned int len; 232fce8a7bbSJon Mason unsigned int flags; 233fce8a7bbSJon Mason }; 234fce8a7bbSJon Mason 235fce8a7bbSJon Mason enum { 236fce8a7bbSJon Mason VERSION = 0, 237fce8a7bbSJon Mason QP_LINKS, 238113fc505SJon Mason NUM_QPS, 239113fc505SJon Mason NUM_MWS, 240113fc505SJon Mason MW0_SZ_HIGH, 241113fc505SJon Mason MW0_SZ_LOW, 242113fc505SJon Mason MW1_SZ_HIGH, 243113fc505SJon Mason MW1_SZ_LOW, 244fce8a7bbSJon Mason MAX_SPAD, 245fce8a7bbSJon Mason }; 246fce8a7bbSJon Mason 247e26a5843SAllen Hubbe #define dev_client_dev(__dev) \ 248e26a5843SAllen Hubbe container_of((__dev), struct ntb_transport_client_dev, dev) 249e26a5843SAllen Hubbe 250e26a5843SAllen Hubbe #define drv_client(__drv) \ 251e26a5843SAllen Hubbe container_of((__drv), struct ntb_transport_client, driver) 252e26a5843SAllen Hubbe 253e26a5843SAllen Hubbe #define QP_TO_MW(nt, qp) ((qp) % nt->mw_count) 254fce8a7bbSJon Mason #define NTB_QP_DEF_NUM_ENTRIES 100 255fce8a7bbSJon Mason #define NTB_LINK_DOWN_TIMEOUT 10 2568c874cc1SDave Jiang #define DMA_RETRIES 20 2578c874cc1SDave Jiang #define DMA_OUT_RESOURCE_TO 50 258fce8a7bbSJon Mason 259e26a5843SAllen Hubbe static void ntb_transport_rxc_db(unsigned long data); 260e26a5843SAllen Hubbe static const struct ntb_ctx_ops ntb_transport_ops; 261e26a5843SAllen Hubbe static struct ntb_client ntb_transport_client; 262e26a5843SAllen Hubbe 263e26a5843SAllen Hubbe static int ntb_transport_bus_match(struct device *dev, 264e26a5843SAllen Hubbe struct device_driver *drv) 265fce8a7bbSJon Mason { 266fce8a7bbSJon Mason return !strncmp(dev_name(dev), drv->name, strlen(drv->name)); 267fce8a7bbSJon Mason } 268fce8a7bbSJon Mason 269e26a5843SAllen Hubbe static int ntb_transport_bus_probe(struct device *dev) 270fce8a7bbSJon Mason { 271e26a5843SAllen Hubbe const struct ntb_transport_client *client; 272fce8a7bbSJon Mason int rc = -EINVAL; 273fce8a7bbSJon Mason 274fce8a7bbSJon Mason get_device(dev); 275e26a5843SAllen Hubbe 276e26a5843SAllen Hubbe client = drv_client(dev->driver); 277e26a5843SAllen Hubbe rc = client->probe(dev); 278fce8a7bbSJon Mason if (rc) 279fce8a7bbSJon Mason put_device(dev); 280fce8a7bbSJon Mason 281fce8a7bbSJon Mason return rc; 282fce8a7bbSJon Mason } 283fce8a7bbSJon Mason 284e26a5843SAllen Hubbe static int ntb_transport_bus_remove(struct device *dev) 285fce8a7bbSJon Mason { 286e26a5843SAllen Hubbe const struct ntb_transport_client *client; 287fce8a7bbSJon Mason 288e26a5843SAllen Hubbe client = drv_client(dev->driver); 289e26a5843SAllen Hubbe client->remove(dev); 290fce8a7bbSJon Mason 291fce8a7bbSJon Mason put_device(dev); 292fce8a7bbSJon Mason 293fce8a7bbSJon Mason return 0; 294fce8a7bbSJon Mason } 295fce8a7bbSJon Mason 296e26a5843SAllen Hubbe static struct bus_type ntb_transport_bus = { 297e26a5843SAllen Hubbe .name = "ntb_transport", 298e26a5843SAllen Hubbe .match = ntb_transport_bus_match, 299e26a5843SAllen Hubbe .probe = ntb_transport_bus_probe, 300e26a5843SAllen Hubbe .remove = ntb_transport_bus_remove, 301fce8a7bbSJon Mason }; 302fce8a7bbSJon Mason 303fce8a7bbSJon Mason static LIST_HEAD(ntb_transport_list); 304fce8a7bbSJon Mason 305e26a5843SAllen Hubbe static int ntb_bus_init(struct ntb_transport_ctx *nt) 306fce8a7bbSJon Mason { 30731510000SDave Jiang list_add_tail(&nt->entry, &ntb_transport_list); 308fce8a7bbSJon Mason return 0; 309fce8a7bbSJon Mason } 310fce8a7bbSJon Mason 311e26a5843SAllen Hubbe static void ntb_bus_remove(struct ntb_transport_ctx *nt) 312fce8a7bbSJon Mason { 313fce8a7bbSJon Mason struct ntb_transport_client_dev *client_dev, *cd; 314fce8a7bbSJon Mason 315fce8a7bbSJon Mason list_for_each_entry_safe(client_dev, cd, &nt->client_devs, entry) { 316fce8a7bbSJon Mason dev_err(client_dev->dev.parent, "%s still attached to bus, removing\n", 317fce8a7bbSJon Mason dev_name(&client_dev->dev)); 318fce8a7bbSJon Mason list_del(&client_dev->entry); 319fce8a7bbSJon Mason device_unregister(&client_dev->dev); 320fce8a7bbSJon Mason } 321fce8a7bbSJon Mason 322fce8a7bbSJon Mason list_del(&nt->entry); 323fce8a7bbSJon Mason } 324fce8a7bbSJon Mason 325e26a5843SAllen Hubbe static void ntb_transport_client_release(struct device *dev) 326fce8a7bbSJon Mason { 327fce8a7bbSJon Mason struct ntb_transport_client_dev *client_dev; 328fce8a7bbSJon Mason 329e26a5843SAllen Hubbe client_dev = dev_client_dev(dev); 330fce8a7bbSJon Mason kfree(client_dev); 331fce8a7bbSJon Mason } 332fce8a7bbSJon Mason 333fce8a7bbSJon Mason /** 334e26a5843SAllen Hubbe * ntb_transport_unregister_client_dev - Unregister NTB client device 335fce8a7bbSJon Mason * @device_name: Name of NTB client device 336fce8a7bbSJon Mason * 337fce8a7bbSJon Mason * Unregister an NTB client device with the NTB transport layer 338fce8a7bbSJon Mason */ 339e26a5843SAllen Hubbe void ntb_transport_unregister_client_dev(char *device_name) 340fce8a7bbSJon Mason { 341fce8a7bbSJon Mason struct ntb_transport_client_dev *client, *cd; 342e26a5843SAllen Hubbe struct ntb_transport_ctx *nt; 343fce8a7bbSJon Mason 344fce8a7bbSJon Mason list_for_each_entry(nt, &ntb_transport_list, entry) 345fce8a7bbSJon Mason list_for_each_entry_safe(client, cd, &nt->client_devs, entry) 346fce8a7bbSJon Mason if (!strncmp(dev_name(&client->dev), device_name, 347fce8a7bbSJon Mason strlen(device_name))) { 348fce8a7bbSJon Mason list_del(&client->entry); 349fce8a7bbSJon Mason device_unregister(&client->dev); 350fce8a7bbSJon Mason } 351fce8a7bbSJon Mason } 352e26a5843SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_unregister_client_dev); 353fce8a7bbSJon Mason 354fce8a7bbSJon Mason /** 355e26a5843SAllen Hubbe * ntb_transport_register_client_dev - Register NTB client device 356fce8a7bbSJon Mason * @device_name: Name of NTB client device 357fce8a7bbSJon Mason * 358fce8a7bbSJon Mason * Register an NTB client device with the NTB transport layer 359fce8a7bbSJon Mason */ 360e26a5843SAllen Hubbe int ntb_transport_register_client_dev(char *device_name) 361fce8a7bbSJon Mason { 362fce8a7bbSJon Mason struct ntb_transport_client_dev *client_dev; 363e26a5843SAllen Hubbe struct ntb_transport_ctx *nt; 3641199aa61SAllen Hubbe int node; 3658b19d450SJon Mason int rc, i = 0; 366fce8a7bbSJon Mason 3678222b402SJon Mason if (list_empty(&ntb_transport_list)) 3688222b402SJon Mason return -ENODEV; 3698222b402SJon Mason 370fce8a7bbSJon Mason list_for_each_entry(nt, &ntb_transport_list, entry) { 371fce8a7bbSJon Mason struct device *dev; 372fce8a7bbSJon Mason 3731199aa61SAllen Hubbe node = dev_to_node(&nt->ndev->dev); 3741199aa61SAllen Hubbe 3751199aa61SAllen Hubbe client_dev = kzalloc_node(sizeof(*client_dev), 3761199aa61SAllen Hubbe GFP_KERNEL, node); 377fce8a7bbSJon Mason if (!client_dev) { 378fce8a7bbSJon Mason rc = -ENOMEM; 379fce8a7bbSJon Mason goto err; 380fce8a7bbSJon Mason } 381fce8a7bbSJon Mason 382fce8a7bbSJon Mason dev = &client_dev->dev; 383fce8a7bbSJon Mason 384fce8a7bbSJon Mason /* setup and register client devices */ 3858b19d450SJon Mason dev_set_name(dev, "%s%d", device_name, i); 386e26a5843SAllen Hubbe dev->bus = &ntb_transport_bus; 387e26a5843SAllen Hubbe dev->release = ntb_transport_client_release; 388e26a5843SAllen Hubbe dev->parent = &nt->ndev->dev; 389fce8a7bbSJon Mason 390fce8a7bbSJon Mason rc = device_register(dev); 391fce8a7bbSJon Mason if (rc) { 392fce8a7bbSJon Mason kfree(client_dev); 393fce8a7bbSJon Mason goto err; 394fce8a7bbSJon Mason } 395fce8a7bbSJon Mason 396fce8a7bbSJon Mason list_add_tail(&client_dev->entry, &nt->client_devs); 3978b19d450SJon Mason i++; 398fce8a7bbSJon Mason } 399fce8a7bbSJon Mason 400fce8a7bbSJon Mason return 0; 401fce8a7bbSJon Mason 402fce8a7bbSJon Mason err: 403e26a5843SAllen Hubbe ntb_transport_unregister_client_dev(device_name); 404fce8a7bbSJon Mason 405fce8a7bbSJon Mason return rc; 406fce8a7bbSJon Mason } 407e26a5843SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_register_client_dev); 408fce8a7bbSJon Mason 409fce8a7bbSJon Mason /** 410ec110bc7SAllen Hubbe * ntb_transport_register_client - Register NTB client driver 411fce8a7bbSJon Mason * @drv: NTB client driver to be registered 412fce8a7bbSJon Mason * 413fce8a7bbSJon Mason * Register an NTB client driver with the NTB transport layer 414fce8a7bbSJon Mason * 415fce8a7bbSJon Mason * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 416fce8a7bbSJon Mason */ 417e26a5843SAllen Hubbe int ntb_transport_register_client(struct ntb_transport_client *drv) 418fce8a7bbSJon Mason { 419e26a5843SAllen Hubbe drv->driver.bus = &ntb_transport_bus; 420fce8a7bbSJon Mason 4218222b402SJon Mason if (list_empty(&ntb_transport_list)) 4228222b402SJon Mason return -ENODEV; 4238222b402SJon Mason 424fce8a7bbSJon Mason return driver_register(&drv->driver); 425fce8a7bbSJon Mason } 426ec110bc7SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_register_client); 427fce8a7bbSJon Mason 428fce8a7bbSJon Mason /** 429ec110bc7SAllen Hubbe * ntb_transport_unregister_client - Unregister NTB client driver 430fce8a7bbSJon Mason * @drv: NTB client driver to be unregistered 431fce8a7bbSJon Mason * 432fce8a7bbSJon Mason * Unregister an NTB client driver with the NTB transport layer 433fce8a7bbSJon Mason * 434fce8a7bbSJon Mason * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 435fce8a7bbSJon Mason */ 436e26a5843SAllen Hubbe void ntb_transport_unregister_client(struct ntb_transport_client *drv) 437fce8a7bbSJon Mason { 438fce8a7bbSJon Mason driver_unregister(&drv->driver); 439fce8a7bbSJon Mason } 440ec110bc7SAllen Hubbe EXPORT_SYMBOL_GPL(ntb_transport_unregister_client); 441fce8a7bbSJon Mason 442fce8a7bbSJon Mason static ssize_t debugfs_read(struct file *filp, char __user *ubuf, size_t count, 443fce8a7bbSJon Mason loff_t *offp) 444fce8a7bbSJon Mason { 445fce8a7bbSJon Mason struct ntb_transport_qp *qp; 446d7237e22SJon Mason char *buf; 447fce8a7bbSJon Mason ssize_t ret, out_offset, out_count; 448fce8a7bbSJon Mason 449260bee94SDave Jiang qp = filp->private_data; 450260bee94SDave Jiang 451260bee94SDave Jiang if (!qp || !qp->link_is_up) 452260bee94SDave Jiang return 0; 453260bee94SDave Jiang 454282a2feeSJon Mason out_count = 1000; 455d7237e22SJon Mason 456d7237e22SJon Mason buf = kmalloc(out_count, GFP_KERNEL); 457d7237e22SJon Mason if (!buf) 458d7237e22SJon Mason return -ENOMEM; 459fce8a7bbSJon Mason 460fce8a7bbSJon Mason out_offset = 0; 461fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 462d98ef99eSDave Jiang "\nNTB QP stats:\n\n"); 463fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 464fce8a7bbSJon Mason "rx_bytes - \t%llu\n", qp->rx_bytes); 465fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 466fce8a7bbSJon Mason "rx_pkts - \t%llu\n", qp->rx_pkts); 467fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 468282a2feeSJon Mason "rx_memcpy - \t%llu\n", qp->rx_memcpy); 469282a2feeSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 470282a2feeSJon Mason "rx_async - \t%llu\n", qp->rx_async); 471282a2feeSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 472fce8a7bbSJon Mason "rx_ring_empty - %llu\n", qp->rx_ring_empty); 473fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 474fce8a7bbSJon Mason "rx_err_no_buf - %llu\n", qp->rx_err_no_buf); 475fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 476fce8a7bbSJon Mason "rx_err_oflow - \t%llu\n", qp->rx_err_oflow); 477fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 478fce8a7bbSJon Mason "rx_err_ver - \t%llu\n", qp->rx_err_ver); 479fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 480d98ef99eSDave Jiang "rx_buff - \t0x%p\n", qp->rx_buff); 481fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 482793c20e9SJon Mason "rx_index - \t%u\n", qp->rx_index); 483fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 484*a754a8fcSDave Jiang "rx_max_entry - \t%u\n", qp->rx_max_entry); 485*a754a8fcSDave Jiang out_offset += snprintf(buf + out_offset, out_count - out_offset, 486*a754a8fcSDave Jiang "rx_alloc_entry - \t%u\n\n", qp->rx_alloc_entry); 487fce8a7bbSJon Mason 488fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 489fce8a7bbSJon Mason "tx_bytes - \t%llu\n", qp->tx_bytes); 490fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 491fce8a7bbSJon Mason "tx_pkts - \t%llu\n", qp->tx_pkts); 492fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 493282a2feeSJon Mason "tx_memcpy - \t%llu\n", qp->tx_memcpy); 494282a2feeSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 495282a2feeSJon Mason "tx_async - \t%llu\n", qp->tx_async); 496282a2feeSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 497fce8a7bbSJon Mason "tx_ring_full - \t%llu\n", qp->tx_ring_full); 498fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 499282a2feeSJon Mason "tx_err_no_buf - %llu\n", qp->tx_err_no_buf); 500282a2feeSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 501d98ef99eSDave Jiang "tx_mw - \t0x%p\n", qp->tx_mw); 502fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 503d98ef99eSDave Jiang "tx_index (H) - \t%u\n", qp->tx_index); 504d98ef99eSDave Jiang out_offset += snprintf(buf + out_offset, out_count - out_offset, 505d98ef99eSDave Jiang "RRI (T) - \t%u\n", 506d98ef99eSDave Jiang qp->remote_rx_info->entry); 507fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 508793c20e9SJon Mason "tx_max_entry - \t%u\n", qp->tx_max_entry); 509e74bfeedSDave Jiang out_offset += snprintf(buf + out_offset, out_count - out_offset, 510e74bfeedSDave Jiang "free tx - \t%u\n", 511e74bfeedSDave Jiang ntb_transport_tx_free_entry(qp)); 5128c874cc1SDave Jiang out_offset += snprintf(buf + out_offset, out_count - out_offset, 5138c874cc1SDave Jiang "DMA tx prep err - \t%llu\n", 5148c874cc1SDave Jiang qp->dma_tx_prep_err); 5158c874cc1SDave Jiang out_offset += snprintf(buf + out_offset, out_count - out_offset, 5168c874cc1SDave Jiang "DMA rx prep err - \t%llu\n", 5178c874cc1SDave Jiang qp->dma_rx_prep_err); 518fce8a7bbSJon Mason 519fce8a7bbSJon Mason out_offset += snprintf(buf + out_offset, out_count - out_offset, 520d98ef99eSDave Jiang "\n"); 521d98ef99eSDave Jiang out_offset += snprintf(buf + out_offset, out_count - out_offset, 522569410caSDave Jiang "Using TX DMA - \t%s\n", 523569410caSDave Jiang qp->tx_dma_chan ? "Yes" : "No"); 524569410caSDave Jiang out_offset += snprintf(buf + out_offset, out_count - out_offset, 525569410caSDave Jiang "Using RX DMA - \t%s\n", 526569410caSDave Jiang qp->rx_dma_chan ? "Yes" : "No"); 527d98ef99eSDave Jiang out_offset += snprintf(buf + out_offset, out_count - out_offset, 528d98ef99eSDave Jiang "QP Link - \t%s\n", 529e26a5843SAllen Hubbe qp->link_is_up ? "Up" : "Down"); 530d98ef99eSDave Jiang out_offset += snprintf(buf + out_offset, out_count - out_offset, 531d98ef99eSDave Jiang "\n"); 532d98ef99eSDave Jiang 533d7237e22SJon Mason if (out_offset > out_count) 534d7237e22SJon Mason out_offset = out_count; 535fce8a7bbSJon Mason 536fce8a7bbSJon Mason ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset); 537d7237e22SJon Mason kfree(buf); 538fce8a7bbSJon Mason return ret; 539fce8a7bbSJon Mason } 540fce8a7bbSJon Mason 541fce8a7bbSJon Mason static const struct file_operations ntb_qp_debugfs_stats = { 542fce8a7bbSJon Mason .owner = THIS_MODULE, 543d66d7ac2SJon Mason .open = simple_open, 544fce8a7bbSJon Mason .read = debugfs_read, 545fce8a7bbSJon Mason }; 546fce8a7bbSJon Mason 547fce8a7bbSJon Mason static void ntb_list_add(spinlock_t *lock, struct list_head *entry, 548fce8a7bbSJon Mason struct list_head *list) 549fce8a7bbSJon Mason { 550fce8a7bbSJon Mason unsigned long flags; 551fce8a7bbSJon Mason 552fce8a7bbSJon Mason spin_lock_irqsave(lock, flags); 553fce8a7bbSJon Mason list_add_tail(entry, list); 554fce8a7bbSJon Mason spin_unlock_irqrestore(lock, flags); 555fce8a7bbSJon Mason } 556fce8a7bbSJon Mason 557fce8a7bbSJon Mason static struct ntb_queue_entry *ntb_list_rm(spinlock_t *lock, 558fce8a7bbSJon Mason struct list_head *list) 559fce8a7bbSJon Mason { 560fce8a7bbSJon Mason struct ntb_queue_entry *entry; 561fce8a7bbSJon Mason unsigned long flags; 562fce8a7bbSJon Mason 563fce8a7bbSJon Mason spin_lock_irqsave(lock, flags); 564fce8a7bbSJon Mason if (list_empty(list)) { 565fce8a7bbSJon Mason entry = NULL; 566fce8a7bbSJon Mason goto out; 567fce8a7bbSJon Mason } 568fce8a7bbSJon Mason entry = list_first_entry(list, struct ntb_queue_entry, entry); 569fce8a7bbSJon Mason list_del(&entry->entry); 570e74bfeedSDave Jiang 571fce8a7bbSJon Mason out: 572fce8a7bbSJon Mason spin_unlock_irqrestore(lock, flags); 573fce8a7bbSJon Mason 574fce8a7bbSJon Mason return entry; 575fce8a7bbSJon Mason } 576fce8a7bbSJon Mason 577da2e5ae5SAllen Hubbe static struct ntb_queue_entry *ntb_list_mv(spinlock_t *lock, 578da2e5ae5SAllen Hubbe struct list_head *list, 579da2e5ae5SAllen Hubbe struct list_head *to_list) 580da2e5ae5SAllen Hubbe { 581da2e5ae5SAllen Hubbe struct ntb_queue_entry *entry; 582da2e5ae5SAllen Hubbe unsigned long flags; 583da2e5ae5SAllen Hubbe 584da2e5ae5SAllen Hubbe spin_lock_irqsave(lock, flags); 585da2e5ae5SAllen Hubbe 586da2e5ae5SAllen Hubbe if (list_empty(list)) { 587da2e5ae5SAllen Hubbe entry = NULL; 588da2e5ae5SAllen Hubbe } else { 589da2e5ae5SAllen Hubbe entry = list_first_entry(list, struct ntb_queue_entry, entry); 590da2e5ae5SAllen Hubbe list_move_tail(&entry->entry, to_list); 591da2e5ae5SAllen Hubbe } 592da2e5ae5SAllen Hubbe 593da2e5ae5SAllen Hubbe spin_unlock_irqrestore(lock, flags); 594da2e5ae5SAllen Hubbe 595da2e5ae5SAllen Hubbe return entry; 596da2e5ae5SAllen Hubbe } 597da2e5ae5SAllen Hubbe 598e26a5843SAllen Hubbe static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, 599fce8a7bbSJon Mason unsigned int qp_num) 600fce8a7bbSJon Mason { 601e26a5843SAllen Hubbe struct ntb_transport_qp *qp = &nt->qp_vec[qp_num]; 602e26a5843SAllen Hubbe struct ntb_transport_mw *mw; 603*a754a8fcSDave Jiang struct ntb_dev *ndev = nt->ndev; 604*a754a8fcSDave Jiang struct ntb_queue_entry *entry; 605ef114ed5SJon Mason unsigned int rx_size, num_qps_mw; 606e26a5843SAllen Hubbe unsigned int mw_num, mw_count, qp_count; 607793c20e9SJon Mason unsigned int i; 608*a754a8fcSDave Jiang int node; 609fce8a7bbSJon Mason 610e26a5843SAllen Hubbe mw_count = nt->mw_count; 611e26a5843SAllen Hubbe qp_count = nt->qp_count; 612948d3a65SJon Mason 613e26a5843SAllen Hubbe mw_num = QP_TO_MW(nt, qp_num); 614e26a5843SAllen Hubbe mw = &nt->mw_vec[mw_num]; 615fce8a7bbSJon Mason 616e26a5843SAllen Hubbe if (!mw->virt_addr) 617e26a5843SAllen Hubbe return -ENOMEM; 618e26a5843SAllen Hubbe 619e26a5843SAllen Hubbe if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count) 620e26a5843SAllen Hubbe num_qps_mw = qp_count / mw_count + 1; 621fce8a7bbSJon Mason else 622e26a5843SAllen Hubbe num_qps_mw = qp_count / mw_count; 623fce8a7bbSJon Mason 624e26a5843SAllen Hubbe rx_size = (unsigned int)mw->xlat_size / num_qps_mw; 625c92ba3c5SJon Mason qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count); 626793c20e9SJon Mason rx_size -= sizeof(struct ntb_rx_info); 627793c20e9SJon Mason 628282a2feeSJon Mason qp->remote_rx_info = qp->rx_buff + rx_size; 629282a2feeSJon Mason 630c9d534c8SJon Mason /* Due to housekeeping, there must be atleast 2 buffs */ 631c9d534c8SJon Mason qp->rx_max_frame = min(transport_mtu, rx_size / 2); 632793c20e9SJon Mason qp->rx_max_entry = rx_size / qp->rx_max_frame; 633793c20e9SJon Mason qp->rx_index = 0; 634793c20e9SJon Mason 635*a754a8fcSDave Jiang /* 636*a754a8fcSDave Jiang * Checking to see if we have more entries than the default. 637*a754a8fcSDave Jiang * We should add additional entries if that is the case so we 638*a754a8fcSDave Jiang * can be in sync with the transport frames. 639*a754a8fcSDave Jiang */ 640*a754a8fcSDave Jiang node = dev_to_node(&ndev->dev); 641*a754a8fcSDave Jiang for (i = qp->rx_alloc_entry; i < qp->rx_max_entry; i++) { 642*a754a8fcSDave Jiang entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node); 643*a754a8fcSDave Jiang if (!entry) 644*a754a8fcSDave Jiang return -ENOMEM; 645*a754a8fcSDave Jiang 646*a754a8fcSDave Jiang entry->qp = qp; 647*a754a8fcSDave Jiang ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, 648*a754a8fcSDave Jiang &qp->rx_free_q); 649*a754a8fcSDave Jiang qp->rx_alloc_entry++; 650*a754a8fcSDave Jiang } 651*a754a8fcSDave Jiang 652c9d534c8SJon Mason qp->remote_rx_info->entry = qp->rx_max_entry - 1; 653fce8a7bbSJon Mason 654ef114ed5SJon Mason /* setup the hdr offsets with 0's */ 655793c20e9SJon Mason for (i = 0; i < qp->rx_max_entry; i++) { 656e26a5843SAllen Hubbe void *offset = (qp->rx_buff + qp->rx_max_frame * (i + 1) - 657e26a5843SAllen Hubbe sizeof(struct ntb_payload_header)); 658ef114ed5SJon Mason memset(offset, 0, sizeof(struct ntb_payload_header)); 659793c20e9SJon Mason } 660fce8a7bbSJon Mason 661fce8a7bbSJon Mason qp->rx_pkts = 0; 662fce8a7bbSJon Mason qp->tx_pkts = 0; 66390f9e934SJon Mason qp->tx_index = 0; 664e26a5843SAllen Hubbe 665e26a5843SAllen Hubbe return 0; 666fce8a7bbSJon Mason } 667fce8a7bbSJon Mason 668e26a5843SAllen Hubbe static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw) 669113fc505SJon Mason { 670e26a5843SAllen Hubbe struct ntb_transport_mw *mw = &nt->mw_vec[num_mw]; 671e26a5843SAllen Hubbe struct pci_dev *pdev = nt->ndev->pdev; 672113fc505SJon Mason 673113fc505SJon Mason if (!mw->virt_addr) 674113fc505SJon Mason return; 675113fc505SJon Mason 676e26a5843SAllen Hubbe ntb_mw_clear_trans(nt->ndev, num_mw); 677e26a5843SAllen Hubbe dma_free_coherent(&pdev->dev, mw->buff_size, 678e26a5843SAllen Hubbe mw->virt_addr, mw->dma_addr); 679e26a5843SAllen Hubbe mw->xlat_size = 0; 680e26a5843SAllen Hubbe mw->buff_size = 0; 681113fc505SJon Mason mw->virt_addr = NULL; 682113fc505SJon Mason } 683113fc505SJon Mason 684e26a5843SAllen Hubbe static int ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw, 6858c9edf63SAllen Hubbe resource_size_t size) 686b77b2637SJon Mason { 687e26a5843SAllen Hubbe struct ntb_transport_mw *mw = &nt->mw_vec[num_mw]; 688e26a5843SAllen Hubbe struct pci_dev *pdev = nt->ndev->pdev; 6898c9edf63SAllen Hubbe size_t xlat_size, buff_size; 690e26a5843SAllen Hubbe int rc; 691e26a5843SAllen Hubbe 6928c9edf63SAllen Hubbe if (!size) 6938c9edf63SAllen Hubbe return -EINVAL; 6948c9edf63SAllen Hubbe 695e26a5843SAllen Hubbe xlat_size = round_up(size, mw->xlat_align_size); 696e26a5843SAllen Hubbe buff_size = round_up(size, mw->xlat_align); 697b77b2637SJon Mason 698b77b2637SJon Mason /* No need to re-setup */ 699e26a5843SAllen Hubbe if (mw->xlat_size == xlat_size) 700b77b2637SJon Mason return 0; 701b77b2637SJon Mason 702e26a5843SAllen Hubbe if (mw->buff_size) 703b77b2637SJon Mason ntb_free_mw(nt, num_mw); 704b77b2637SJon Mason 705e26a5843SAllen Hubbe /* Alloc memory for receiving data. Must be aligned */ 706e26a5843SAllen Hubbe mw->xlat_size = xlat_size; 707e26a5843SAllen Hubbe mw->buff_size = buff_size; 708b77b2637SJon Mason 709e26a5843SAllen Hubbe mw->virt_addr = dma_alloc_coherent(&pdev->dev, buff_size, 710e26a5843SAllen Hubbe &mw->dma_addr, GFP_KERNEL); 711b77b2637SJon Mason if (!mw->virt_addr) { 712e26a5843SAllen Hubbe mw->xlat_size = 0; 713e26a5843SAllen Hubbe mw->buff_size = 0; 7148c9edf63SAllen Hubbe dev_err(&pdev->dev, "Unable to alloc MW buff of size %zu\n", 715e26a5843SAllen Hubbe buff_size); 716b77b2637SJon Mason return -ENOMEM; 717b77b2637SJon Mason } 718b77b2637SJon Mason 7193cc5ba19SDave Jiang /* 7203cc5ba19SDave Jiang * we must ensure that the memory address allocated is BAR size 7213cc5ba19SDave Jiang * aligned in order for the XLAT register to take the value. This 7223cc5ba19SDave Jiang * is a requirement of the hardware. It is recommended to setup CMA 7233cc5ba19SDave Jiang * for BAR sizes equal or greater than 4MB. 7243cc5ba19SDave Jiang */ 725e26a5843SAllen Hubbe if (!IS_ALIGNED(mw->dma_addr, mw->xlat_align)) { 726e26a5843SAllen Hubbe dev_err(&pdev->dev, "DMA memory %pad is not aligned\n", 7273cc5ba19SDave Jiang &mw->dma_addr); 7283cc5ba19SDave Jiang ntb_free_mw(nt, num_mw); 7293cc5ba19SDave Jiang return -ENOMEM; 7303cc5ba19SDave Jiang } 7313cc5ba19SDave Jiang 732b77b2637SJon Mason /* Notify HW the memory location of the receive buffer */ 733e26a5843SAllen Hubbe rc = ntb_mw_set_trans(nt->ndev, num_mw, mw->dma_addr, mw->xlat_size); 734e26a5843SAllen Hubbe if (rc) { 735e26a5843SAllen Hubbe dev_err(&pdev->dev, "Unable to set mw%d translation", num_mw); 736e26a5843SAllen Hubbe ntb_free_mw(nt, num_mw); 737e26a5843SAllen Hubbe return -EIO; 738e26a5843SAllen Hubbe } 739b77b2637SJon Mason 740b77b2637SJon Mason return 0; 741b77b2637SJon Mason } 742b77b2637SJon Mason 7432849b5d7SAllen Hubbe static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp) 7442849b5d7SAllen Hubbe { 7452849b5d7SAllen Hubbe qp->link_is_up = false; 746e9021331SDave Jiang qp->active = false; 7472849b5d7SAllen Hubbe 7482849b5d7SAllen Hubbe qp->tx_index = 0; 7492849b5d7SAllen Hubbe qp->rx_index = 0; 7502849b5d7SAllen Hubbe qp->rx_bytes = 0; 7512849b5d7SAllen Hubbe qp->rx_pkts = 0; 7522849b5d7SAllen Hubbe qp->rx_ring_empty = 0; 7532849b5d7SAllen Hubbe qp->rx_err_no_buf = 0; 7542849b5d7SAllen Hubbe qp->rx_err_oflow = 0; 7552849b5d7SAllen Hubbe qp->rx_err_ver = 0; 7562849b5d7SAllen Hubbe qp->rx_memcpy = 0; 7572849b5d7SAllen Hubbe qp->rx_async = 0; 7582849b5d7SAllen Hubbe qp->tx_bytes = 0; 7592849b5d7SAllen Hubbe qp->tx_pkts = 0; 7602849b5d7SAllen Hubbe qp->tx_ring_full = 0; 7612849b5d7SAllen Hubbe qp->tx_err_no_buf = 0; 7622849b5d7SAllen Hubbe qp->tx_memcpy = 0; 7632849b5d7SAllen Hubbe qp->tx_async = 0; 7648c874cc1SDave Jiang qp->dma_tx_prep_err = 0; 7658c874cc1SDave Jiang qp->dma_rx_prep_err = 0; 7662849b5d7SAllen Hubbe } 7672849b5d7SAllen Hubbe 768fca4d518SJon Mason static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp) 769fce8a7bbSJon Mason { 770e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = qp->transport; 771e26a5843SAllen Hubbe struct pci_dev *pdev = nt->ndev->pdev; 772fce8a7bbSJon Mason 773e22e0b9dSAllen Hubbe dev_info(&pdev->dev, "qp %d: Link Cleanup\n", qp->qp_num); 7742849b5d7SAllen Hubbe 7752849b5d7SAllen Hubbe cancel_delayed_work_sync(&qp->link_work); 7762849b5d7SAllen Hubbe ntb_qp_link_down_reset(qp); 777e26a5843SAllen Hubbe 778e26a5843SAllen Hubbe if (qp->event_handler) 779e26a5843SAllen Hubbe qp->event_handler(qp->cb_data, qp->link_is_up); 780fca4d518SJon Mason } 781fca4d518SJon Mason 782fca4d518SJon Mason static void ntb_qp_link_cleanup_work(struct work_struct *work) 783fca4d518SJon Mason { 784fca4d518SJon Mason struct ntb_transport_qp *qp = container_of(work, 785fca4d518SJon Mason struct ntb_transport_qp, 786fca4d518SJon Mason link_cleanup); 787e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = qp->transport; 788fca4d518SJon Mason 789fca4d518SJon Mason ntb_qp_link_cleanup(qp); 790fce8a7bbSJon Mason 791e26a5843SAllen Hubbe if (nt->link_is_up) 792fce8a7bbSJon Mason schedule_delayed_work(&qp->link_work, 793fce8a7bbSJon Mason msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT)); 794fce8a7bbSJon Mason } 795fce8a7bbSJon Mason 7967b4f2d3cSJon Mason static void ntb_qp_link_down(struct ntb_transport_qp *qp) 797fce8a7bbSJon Mason { 7987b4f2d3cSJon Mason schedule_work(&qp->link_cleanup); 7997b4f2d3cSJon Mason } 8007b4f2d3cSJon Mason 801e26a5843SAllen Hubbe static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt) 8027b4f2d3cSJon Mason { 803e26a5843SAllen Hubbe struct ntb_transport_qp *qp; 804e26a5843SAllen Hubbe u64 qp_bitmap_alloc; 805fce8a7bbSJon Mason int i; 806fce8a7bbSJon Mason 807e26a5843SAllen Hubbe qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free; 808fca4d518SJon Mason 809e26a5843SAllen Hubbe /* Pass along the info to any clients */ 810e26a5843SAllen Hubbe for (i = 0; i < nt->qp_count; i++) 811e26a5843SAllen Hubbe if (qp_bitmap_alloc & BIT_ULL(i)) { 812e26a5843SAllen Hubbe qp = &nt->qp_vec[i]; 813e26a5843SAllen Hubbe ntb_qp_link_cleanup(qp); 814e26a5843SAllen Hubbe cancel_work_sync(&qp->link_cleanup); 815e26a5843SAllen Hubbe cancel_delayed_work_sync(&qp->link_work); 816e26a5843SAllen Hubbe } 817e26a5843SAllen Hubbe 818e26a5843SAllen Hubbe if (!nt->link_is_up) 819fce8a7bbSJon Mason cancel_delayed_work_sync(&nt->link_work); 820fce8a7bbSJon Mason 821fce8a7bbSJon Mason /* The scratchpad registers keep the values if the remote side 822fce8a7bbSJon Mason * goes down, blast them now to give them a sane value the next 823fce8a7bbSJon Mason * time they are accessed 824fce8a7bbSJon Mason */ 825fce8a7bbSJon Mason for (i = 0; i < MAX_SPAD; i++) 826e26a5843SAllen Hubbe ntb_spad_write(nt->ndev, i, 0); 827fce8a7bbSJon Mason } 828fce8a7bbSJon Mason 829fca4d518SJon Mason static void ntb_transport_link_cleanup_work(struct work_struct *work) 830fca4d518SJon Mason { 831e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = 832e26a5843SAllen Hubbe container_of(work, struct ntb_transport_ctx, link_cleanup); 833fca4d518SJon Mason 834fca4d518SJon Mason ntb_transport_link_cleanup(nt); 835fca4d518SJon Mason } 836fca4d518SJon Mason 837e26a5843SAllen Hubbe static void ntb_transport_event_callback(void *data) 838fce8a7bbSJon Mason { 839e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = data; 840fce8a7bbSJon Mason 841e26a5843SAllen Hubbe if (ntb_link_is_up(nt->ndev, NULL, NULL) == 1) 842fce8a7bbSJon Mason schedule_delayed_work(&nt->link_work, 0); 843e26a5843SAllen Hubbe else 8447b4f2d3cSJon Mason schedule_work(&nt->link_cleanup); 845fce8a7bbSJon Mason } 846fce8a7bbSJon Mason 847fce8a7bbSJon Mason static void ntb_transport_link_work(struct work_struct *work) 848fce8a7bbSJon Mason { 849e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = 850e26a5843SAllen Hubbe container_of(work, struct ntb_transport_ctx, link_work.work); 851e26a5843SAllen Hubbe struct ntb_dev *ndev = nt->ndev; 852e26a5843SAllen Hubbe struct pci_dev *pdev = ndev->pdev; 853e26a5843SAllen Hubbe resource_size_t size; 854fce8a7bbSJon Mason u32 val; 85584f76685SDave Jiang int rc = 0, i, spad; 856fce8a7bbSJon Mason 857113fc505SJon Mason /* send the local info, in the opposite order of the way we read it */ 858e26a5843SAllen Hubbe for (i = 0; i < nt->mw_count; i++) { 859e26a5843SAllen Hubbe size = nt->mw_vec[i].phys_size; 860e26a5843SAllen Hubbe 861e26a5843SAllen Hubbe if (max_mw_size && size > max_mw_size) 862e26a5843SAllen Hubbe size = max_mw_size; 863e26a5843SAllen Hubbe 864e26a5843SAllen Hubbe spad = MW0_SZ_HIGH + (i * 2); 865fdcb4b2eSArnd Bergmann ntb_peer_spad_write(ndev, spad, upper_32_bits(size)); 866e26a5843SAllen Hubbe 867e26a5843SAllen Hubbe spad = MW0_SZ_LOW + (i * 2); 868fdcb4b2eSArnd Bergmann ntb_peer_spad_write(ndev, spad, lower_32_bits(size)); 869fce8a7bbSJon Mason } 870fce8a7bbSJon Mason 871e26a5843SAllen Hubbe ntb_peer_spad_write(ndev, NUM_MWS, nt->mw_count); 872fce8a7bbSJon Mason 873e26a5843SAllen Hubbe ntb_peer_spad_write(ndev, NUM_QPS, nt->qp_count); 874fce8a7bbSJon Mason 875e26a5843SAllen Hubbe ntb_peer_spad_write(ndev, VERSION, NTB_TRANSPORT_VERSION); 876fce8a7bbSJon Mason 877fce8a7bbSJon Mason /* Query the remote side for its info */ 8780f69a7dfSDave Jiang val = ntb_spad_read(ndev, VERSION); 879e26a5843SAllen Hubbe dev_dbg(&pdev->dev, "Remote version = %d\n", val); 880fce8a7bbSJon Mason if (val != NTB_TRANSPORT_VERSION) 881fce8a7bbSJon Mason goto out; 882fce8a7bbSJon Mason 8830f69a7dfSDave Jiang val = ntb_spad_read(ndev, NUM_QPS); 884fce8a7bbSJon Mason dev_dbg(&pdev->dev, "Remote max number of qps = %d\n", val); 885e26a5843SAllen Hubbe if (val != nt->qp_count) 886fce8a7bbSJon Mason goto out; 887fce8a7bbSJon Mason 8880f69a7dfSDave Jiang val = ntb_spad_read(ndev, NUM_MWS); 889113fc505SJon Mason dev_dbg(&pdev->dev, "Remote number of mws = %d\n", val); 890e26a5843SAllen Hubbe if (val != nt->mw_count) 891e26a5843SAllen Hubbe goto out; 892fce8a7bbSJon Mason 893e26a5843SAllen Hubbe for (i = 0; i < nt->mw_count; i++) { 894113fc505SJon Mason u64 val64; 895fce8a7bbSJon Mason 8960f69a7dfSDave Jiang val = ntb_spad_read(ndev, MW0_SZ_HIGH + (i * 2)); 897113fc505SJon Mason val64 = (u64)val << 32; 898fce8a7bbSJon Mason 8990f69a7dfSDave Jiang val = ntb_spad_read(ndev, MW0_SZ_LOW + (i * 2)); 900113fc505SJon Mason val64 |= val; 901113fc505SJon Mason 902e26a5843SAllen Hubbe dev_dbg(&pdev->dev, "Remote MW%d size = %#llx\n", i, val64); 903113fc505SJon Mason 904113fc505SJon Mason rc = ntb_set_mw(nt, i, val64); 905fce8a7bbSJon Mason if (rc) 906113fc505SJon Mason goto out1; 907113fc505SJon Mason } 908fce8a7bbSJon Mason 909e26a5843SAllen Hubbe nt->link_is_up = true; 910fce8a7bbSJon Mason 911e26a5843SAllen Hubbe for (i = 0; i < nt->qp_count; i++) { 912e26a5843SAllen Hubbe struct ntb_transport_qp *qp = &nt->qp_vec[i]; 913fce8a7bbSJon Mason 914fce8a7bbSJon Mason ntb_transport_setup_qp_mw(nt, i); 915fce8a7bbSJon Mason 916e26a5843SAllen Hubbe if (qp->client_ready) 917fce8a7bbSJon Mason schedule_delayed_work(&qp->link_work, 0); 918fce8a7bbSJon Mason } 919fce8a7bbSJon Mason 920fce8a7bbSJon Mason return; 921fce8a7bbSJon Mason 922113fc505SJon Mason out1: 923e26a5843SAllen Hubbe for (i = 0; i < nt->mw_count; i++) 924113fc505SJon Mason ntb_free_mw(nt, i); 92584f76685SDave Jiang 92684f76685SDave Jiang /* if there's an actual failure, we should just bail */ 92784f76685SDave Jiang if (rc < 0) { 92884f76685SDave Jiang ntb_link_disable(ndev); 92984f76685SDave Jiang return; 93084f76685SDave Jiang } 93184f76685SDave Jiang 932fce8a7bbSJon Mason out: 933e26a5843SAllen Hubbe if (ntb_link_is_up(ndev, NULL, NULL) == 1) 934fce8a7bbSJon Mason schedule_delayed_work(&nt->link_work, 935fce8a7bbSJon Mason msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT)); 936fce8a7bbSJon Mason } 937fce8a7bbSJon Mason 938fce8a7bbSJon Mason static void ntb_qp_link_work(struct work_struct *work) 939fce8a7bbSJon Mason { 940fce8a7bbSJon Mason struct ntb_transport_qp *qp = container_of(work, 941fce8a7bbSJon Mason struct ntb_transport_qp, 942fce8a7bbSJon Mason link_work.work); 943e26a5843SAllen Hubbe struct pci_dev *pdev = qp->ndev->pdev; 944e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = qp->transport; 945e26a5843SAllen Hubbe int val; 946fce8a7bbSJon Mason 947e26a5843SAllen Hubbe WARN_ON(!nt->link_is_up); 948fce8a7bbSJon Mason 949e26a5843SAllen Hubbe val = ntb_spad_read(nt->ndev, QP_LINKS); 950fce8a7bbSJon Mason 951e26a5843SAllen Hubbe ntb_peer_spad_write(nt->ndev, QP_LINKS, val | BIT(qp->qp_num)); 952fce8a7bbSJon Mason 953fce8a7bbSJon Mason /* query remote spad for qp ready bits */ 954e26a5843SAllen Hubbe ntb_peer_spad_read(nt->ndev, QP_LINKS); 95528762289SAllen Hubbe dev_dbg_ratelimited(&pdev->dev, "Remote QP link status = %x\n", val); 956fce8a7bbSJon Mason 957fce8a7bbSJon Mason /* See if the remote side is up */ 958e26a5843SAllen Hubbe if (val & BIT(qp->qp_num)) { 959fce8a7bbSJon Mason dev_info(&pdev->dev, "qp %d: Link Up\n", qp->qp_num); 960e26a5843SAllen Hubbe qp->link_is_up = true; 961e9021331SDave Jiang qp->active = true; 962e26a5843SAllen Hubbe 963fce8a7bbSJon Mason if (qp->event_handler) 964e26a5843SAllen Hubbe qp->event_handler(qp->cb_data, qp->link_is_up); 9658b5a22d8SAllen Hubbe 966e9021331SDave Jiang if (qp->active) 9678b5a22d8SAllen Hubbe tasklet_schedule(&qp->rxc_db_work); 968e26a5843SAllen Hubbe } else if (nt->link_is_up) 969fce8a7bbSJon Mason schedule_delayed_work(&qp->link_work, 970fce8a7bbSJon Mason msecs_to_jiffies(NTB_LINK_DOWN_TIMEOUT)); 971fce8a7bbSJon Mason } 972fce8a7bbSJon Mason 973e26a5843SAllen Hubbe static int ntb_transport_init_queue(struct ntb_transport_ctx *nt, 974fce8a7bbSJon Mason unsigned int qp_num) 975fce8a7bbSJon Mason { 976fce8a7bbSJon Mason struct ntb_transport_qp *qp; 977e26a5843SAllen Hubbe phys_addr_t mw_base; 978e26a5843SAllen Hubbe resource_size_t mw_size; 979ef114ed5SJon Mason unsigned int num_qps_mw, tx_size; 980e26a5843SAllen Hubbe unsigned int mw_num, mw_count, qp_count; 981282a2feeSJon Mason u64 qp_offset; 982948d3a65SJon Mason 983e26a5843SAllen Hubbe mw_count = nt->mw_count; 984e26a5843SAllen Hubbe qp_count = nt->qp_count; 985fce8a7bbSJon Mason 986e26a5843SAllen Hubbe mw_num = QP_TO_MW(nt, qp_num); 987e26a5843SAllen Hubbe 988e26a5843SAllen Hubbe qp = &nt->qp_vec[qp_num]; 989fce8a7bbSJon Mason qp->qp_num = qp_num; 990fce8a7bbSJon Mason qp->transport = nt; 991fce8a7bbSJon Mason qp->ndev = nt->ndev; 992e26a5843SAllen Hubbe qp->client_ready = false; 993fce8a7bbSJon Mason qp->event_handler = NULL; 9942849b5d7SAllen Hubbe ntb_qp_link_down_reset(qp); 995fce8a7bbSJon Mason 996e26a5843SAllen Hubbe if (qp_count % mw_count && mw_num + 1 < qp_count / mw_count) 997e26a5843SAllen Hubbe num_qps_mw = qp_count / mw_count + 1; 998ef114ed5SJon Mason else 999e26a5843SAllen Hubbe num_qps_mw = qp_count / mw_count; 1000ef114ed5SJon Mason 1001e26a5843SAllen Hubbe mw_base = nt->mw_vec[mw_num].phys_addr; 1002e26a5843SAllen Hubbe mw_size = nt->mw_vec[mw_num].phys_size; 1003e26a5843SAllen Hubbe 1004e26a5843SAllen Hubbe tx_size = (unsigned int)mw_size / num_qps_mw; 1005c92ba3c5SJon Mason qp_offset = tx_size * (qp_num / mw_count); 1006e26a5843SAllen Hubbe 1007e26a5843SAllen Hubbe qp->tx_mw = nt->mw_vec[mw_num].vbase + qp_offset; 1008282a2feeSJon Mason if (!qp->tx_mw) 1009282a2feeSJon Mason return -EINVAL; 1010793c20e9SJon Mason 1011e26a5843SAllen Hubbe qp->tx_mw_phys = mw_base + qp_offset; 1012282a2feeSJon Mason if (!qp->tx_mw_phys) 1013282a2feeSJon Mason return -EINVAL; 1014282a2feeSJon Mason 1015282a2feeSJon Mason tx_size -= sizeof(struct ntb_rx_info); 1016282a2feeSJon Mason qp->rx_info = qp->tx_mw + tx_size; 1017282a2feeSJon Mason 1018c9d534c8SJon Mason /* Due to housekeeping, there must be atleast 2 buffs */ 1019c9d534c8SJon Mason qp->tx_max_frame = min(transport_mtu, tx_size / 2); 1020793c20e9SJon Mason qp->tx_max_entry = tx_size / qp->tx_max_frame; 1021ef114ed5SJon Mason 1022c8650fd0SDave Jiang if (nt->debugfs_node_dir) { 1023fce8a7bbSJon Mason char debugfs_name[4]; 1024fce8a7bbSJon Mason 1025fce8a7bbSJon Mason snprintf(debugfs_name, 4, "qp%d", qp_num); 1026fce8a7bbSJon Mason qp->debugfs_dir = debugfs_create_dir(debugfs_name, 1027c8650fd0SDave Jiang nt->debugfs_node_dir); 1028fce8a7bbSJon Mason 1029fce8a7bbSJon Mason qp->debugfs_stats = debugfs_create_file("stats", S_IRUSR, 1030fce8a7bbSJon Mason qp->debugfs_dir, qp, 1031fce8a7bbSJon Mason &ntb_qp_debugfs_stats); 1032e26a5843SAllen Hubbe } else { 1033e26a5843SAllen Hubbe qp->debugfs_dir = NULL; 1034e26a5843SAllen Hubbe qp->debugfs_stats = NULL; 1035fce8a7bbSJon Mason } 1036fce8a7bbSJon Mason 1037fce8a7bbSJon Mason INIT_DELAYED_WORK(&qp->link_work, ntb_qp_link_work); 1038fca4d518SJon Mason INIT_WORK(&qp->link_cleanup, ntb_qp_link_cleanup_work); 1039fce8a7bbSJon Mason 1040da2e5ae5SAllen Hubbe spin_lock_init(&qp->ntb_rx_q_lock); 1041fce8a7bbSJon Mason spin_lock_init(&qp->ntb_tx_free_q_lock); 1042fce8a7bbSJon Mason 1043da2e5ae5SAllen Hubbe INIT_LIST_HEAD(&qp->rx_post_q); 1044fce8a7bbSJon Mason INIT_LIST_HEAD(&qp->rx_pend_q); 1045fce8a7bbSJon Mason INIT_LIST_HEAD(&qp->rx_free_q); 1046fce8a7bbSJon Mason INIT_LIST_HEAD(&qp->tx_free_q); 1047282a2feeSJon Mason 1048e26a5843SAllen Hubbe tasklet_init(&qp->rxc_db_work, ntb_transport_rxc_db, 1049e26a5843SAllen Hubbe (unsigned long)qp); 1050e26a5843SAllen Hubbe 1051282a2feeSJon Mason return 0; 1052fce8a7bbSJon Mason } 1053fce8a7bbSJon Mason 1054e26a5843SAllen Hubbe static int ntb_transport_probe(struct ntb_client *self, struct ntb_dev *ndev) 1055fce8a7bbSJon Mason { 1056e26a5843SAllen Hubbe struct ntb_transport_ctx *nt; 1057e26a5843SAllen Hubbe struct ntb_transport_mw *mw; 1058e26a5843SAllen Hubbe unsigned int mw_count, qp_count; 1059e26a5843SAllen Hubbe u64 qp_bitmap; 10601199aa61SAllen Hubbe int node; 1061fce8a7bbSJon Mason int rc, i; 1062fce8a7bbSJon Mason 1063e26a5843SAllen Hubbe if (ntb_db_is_unsafe(ndev)) 1064e26a5843SAllen Hubbe dev_dbg(&ndev->dev, 1065e26a5843SAllen Hubbe "doorbell is unsafe, proceed anyway...\n"); 1066e26a5843SAllen Hubbe if (ntb_spad_is_unsafe(ndev)) 1067e26a5843SAllen Hubbe dev_dbg(&ndev->dev, 1068e26a5843SAllen Hubbe "scratchpad is unsafe, proceed anyway...\n"); 1069e26a5843SAllen Hubbe 10701199aa61SAllen Hubbe node = dev_to_node(&ndev->dev); 10711199aa61SAllen Hubbe 10721199aa61SAllen Hubbe nt = kzalloc_node(sizeof(*nt), GFP_KERNEL, node); 1073fce8a7bbSJon Mason if (!nt) 1074fce8a7bbSJon Mason return -ENOMEM; 1075fce8a7bbSJon Mason 1076e26a5843SAllen Hubbe nt->ndev = ndev; 1077e26a5843SAllen Hubbe 1078e26a5843SAllen Hubbe mw_count = ntb_mw_count(ndev); 1079e26a5843SAllen Hubbe 1080e26a5843SAllen Hubbe nt->mw_count = mw_count; 1081e26a5843SAllen Hubbe 10821199aa61SAllen Hubbe nt->mw_vec = kzalloc_node(mw_count * sizeof(*nt->mw_vec), 10831199aa61SAllen Hubbe GFP_KERNEL, node); 1084e26a5843SAllen Hubbe if (!nt->mw_vec) { 1085e26a5843SAllen Hubbe rc = -ENOMEM; 1086fce8a7bbSJon Mason goto err; 1087fce8a7bbSJon Mason } 1088fce8a7bbSJon Mason 1089e26a5843SAllen Hubbe for (i = 0; i < mw_count; i++) { 1090e26a5843SAllen Hubbe mw = &nt->mw_vec[i]; 1091e26a5843SAllen Hubbe 1092e26a5843SAllen Hubbe rc = ntb_mw_get_range(ndev, i, &mw->phys_addr, &mw->phys_size, 1093e26a5843SAllen Hubbe &mw->xlat_align, &mw->xlat_align_size); 1094e26a5843SAllen Hubbe if (rc) 1095e26a5843SAllen Hubbe goto err1; 1096e26a5843SAllen Hubbe 109706917f75SDave Jiang mw->vbase = ioremap_wc(mw->phys_addr, mw->phys_size); 1098e26a5843SAllen Hubbe if (!mw->vbase) { 1099948d3a65SJon Mason rc = -ENOMEM; 1100948d3a65SJon Mason goto err1; 1101948d3a65SJon Mason } 1102948d3a65SJon Mason 1103e26a5843SAllen Hubbe mw->buff_size = 0; 1104e26a5843SAllen Hubbe mw->xlat_size = 0; 1105e26a5843SAllen Hubbe mw->virt_addr = NULL; 1106e26a5843SAllen Hubbe mw->dma_addr = 0; 1107e26a5843SAllen Hubbe } 1108fce8a7bbSJon Mason 1109e26a5843SAllen Hubbe qp_bitmap = ntb_db_valid_mask(ndev); 1110e26a5843SAllen Hubbe 1111e26a5843SAllen Hubbe qp_count = ilog2(qp_bitmap); 1112e26a5843SAllen Hubbe if (max_num_clients && max_num_clients < qp_count) 1113e26a5843SAllen Hubbe qp_count = max_num_clients; 1114e26a5843SAllen Hubbe else if (mw_count < qp_count) 1115e26a5843SAllen Hubbe qp_count = mw_count; 1116e26a5843SAllen Hubbe 1117e26a5843SAllen Hubbe qp_bitmap &= BIT_ULL(qp_count) - 1; 1118e26a5843SAllen Hubbe 1119e26a5843SAllen Hubbe nt->qp_count = qp_count; 1120e26a5843SAllen Hubbe nt->qp_bitmap = qp_bitmap; 1121e26a5843SAllen Hubbe nt->qp_bitmap_free = qp_bitmap; 1122e26a5843SAllen Hubbe 11231199aa61SAllen Hubbe nt->qp_vec = kzalloc_node(qp_count * sizeof(*nt->qp_vec), 11241199aa61SAllen Hubbe GFP_KERNEL, node); 1125e26a5843SAllen Hubbe if (!nt->qp_vec) { 1126fce8a7bbSJon Mason rc = -ENOMEM; 1127d4adee09SSudip Mukherjee goto err1; 1128fce8a7bbSJon Mason } 1129fce8a7bbSJon Mason 1130c8650fd0SDave Jiang if (nt_debugfs_dir) { 1131c8650fd0SDave Jiang nt->debugfs_node_dir = 1132c8650fd0SDave Jiang debugfs_create_dir(pci_name(ndev->pdev), 1133c8650fd0SDave Jiang nt_debugfs_dir); 1134c8650fd0SDave Jiang } 1135c8650fd0SDave Jiang 1136e26a5843SAllen Hubbe for (i = 0; i < qp_count; i++) { 1137282a2feeSJon Mason rc = ntb_transport_init_queue(nt, i); 1138282a2feeSJon Mason if (rc) 1139d4adee09SSudip Mukherjee goto err2; 1140282a2feeSJon Mason } 1141fce8a7bbSJon Mason 1142fce8a7bbSJon Mason INIT_DELAYED_WORK(&nt->link_work, ntb_transport_link_work); 1143fca4d518SJon Mason INIT_WORK(&nt->link_cleanup, ntb_transport_link_cleanup_work); 1144fce8a7bbSJon Mason 1145e26a5843SAllen Hubbe rc = ntb_set_ctx(ndev, nt, &ntb_transport_ops); 1146fce8a7bbSJon Mason if (rc) 1147d4adee09SSudip Mukherjee goto err2; 1148fce8a7bbSJon Mason 1149fce8a7bbSJon Mason INIT_LIST_HEAD(&nt->client_devs); 1150fce8a7bbSJon Mason rc = ntb_bus_init(nt); 1151fce8a7bbSJon Mason if (rc) 1152d4adee09SSudip Mukherjee goto err3; 1153fce8a7bbSJon Mason 1154e26a5843SAllen Hubbe nt->link_is_up = false; 1155e26a5843SAllen Hubbe ntb_link_enable(ndev, NTB_SPEED_AUTO, NTB_WIDTH_AUTO); 1156e26a5843SAllen Hubbe ntb_link_event(ndev); 1157fce8a7bbSJon Mason 1158fce8a7bbSJon Mason return 0; 1159fce8a7bbSJon Mason 1160948d3a65SJon Mason err3: 1161d4adee09SSudip Mukherjee ntb_clear_ctx(ndev); 1162948d3a65SJon Mason err2: 1163d4adee09SSudip Mukherjee kfree(nt->qp_vec); 1164fce8a7bbSJon Mason err1: 1165e26a5843SAllen Hubbe while (i--) { 1166e26a5843SAllen Hubbe mw = &nt->mw_vec[i]; 1167e26a5843SAllen Hubbe iounmap(mw->vbase); 1168e26a5843SAllen Hubbe } 1169d4adee09SSudip Mukherjee kfree(nt->mw_vec); 1170fce8a7bbSJon Mason err: 1171fce8a7bbSJon Mason kfree(nt); 1172fce8a7bbSJon Mason return rc; 1173fce8a7bbSJon Mason } 1174fce8a7bbSJon Mason 1175e26a5843SAllen Hubbe static void ntb_transport_free(struct ntb_client *self, struct ntb_dev *ndev) 1176fce8a7bbSJon Mason { 1177e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = ndev->ctx; 1178e26a5843SAllen Hubbe struct ntb_transport_qp *qp; 1179e26a5843SAllen Hubbe u64 qp_bitmap_alloc; 1180fce8a7bbSJon Mason int i; 1181fce8a7bbSJon Mason 1182fca4d518SJon Mason ntb_transport_link_cleanup(nt); 1183e26a5843SAllen Hubbe cancel_work_sync(&nt->link_cleanup); 1184e26a5843SAllen Hubbe cancel_delayed_work_sync(&nt->link_work); 1185e26a5843SAllen Hubbe 1186e26a5843SAllen Hubbe qp_bitmap_alloc = nt->qp_bitmap & ~nt->qp_bitmap_free; 1187fce8a7bbSJon Mason 1188fce8a7bbSJon Mason /* verify that all the qp's are freed */ 1189e26a5843SAllen Hubbe for (i = 0; i < nt->qp_count; i++) { 1190e26a5843SAllen Hubbe qp = &nt->qp_vec[i]; 1191e26a5843SAllen Hubbe if (qp_bitmap_alloc & BIT_ULL(i)) 1192e26a5843SAllen Hubbe ntb_transport_free_queue(qp); 1193e26a5843SAllen Hubbe debugfs_remove_recursive(qp->debugfs_dir); 11941517a3f2SJon Mason } 1195fce8a7bbSJon Mason 1196e26a5843SAllen Hubbe ntb_link_disable(ndev); 1197e26a5843SAllen Hubbe ntb_clear_ctx(ndev); 1198e26a5843SAllen Hubbe 1199fce8a7bbSJon Mason ntb_bus_remove(nt); 1200fce8a7bbSJon Mason 1201e26a5843SAllen Hubbe for (i = nt->mw_count; i--; ) { 1202113fc505SJon Mason ntb_free_mw(nt, i); 1203e26a5843SAllen Hubbe iounmap(nt->mw_vec[i].vbase); 1204e26a5843SAllen Hubbe } 1205fce8a7bbSJon Mason 1206e26a5843SAllen Hubbe kfree(nt->qp_vec); 1207e26a5843SAllen Hubbe kfree(nt->mw_vec); 1208fce8a7bbSJon Mason kfree(nt); 1209fce8a7bbSJon Mason } 1210fce8a7bbSJon Mason 1211da2e5ae5SAllen Hubbe static void ntb_complete_rxc(struct ntb_transport_qp *qp) 1212fce8a7bbSJon Mason { 1213da2e5ae5SAllen Hubbe struct ntb_queue_entry *entry; 1214da2e5ae5SAllen Hubbe void *cb_data; 1215da2e5ae5SAllen Hubbe unsigned int len; 1216da2e5ae5SAllen Hubbe unsigned long irqflags; 1217448c6fb3SJon Mason 1218da2e5ae5SAllen Hubbe spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags); 1219282a2feeSJon Mason 1220da2e5ae5SAllen Hubbe while (!list_empty(&qp->rx_post_q)) { 1221da2e5ae5SAllen Hubbe entry = list_first_entry(&qp->rx_post_q, 1222da2e5ae5SAllen Hubbe struct ntb_queue_entry, entry); 1223da2e5ae5SAllen Hubbe if (!(entry->flags & DESC_DONE_FLAG)) 1224da2e5ae5SAllen Hubbe break; 1225da2e5ae5SAllen Hubbe 1226da2e5ae5SAllen Hubbe entry->rx_hdr->flags = 0; 1227282a2feeSJon Mason iowrite32(entry->index, &qp->rx_info->entry); 1228fce8a7bbSJon Mason 1229da2e5ae5SAllen Hubbe cb_data = entry->cb_data; 1230da2e5ae5SAllen Hubbe len = entry->len; 1231da2e5ae5SAllen Hubbe 1232da2e5ae5SAllen Hubbe list_move_tail(&entry->entry, &qp->rx_free_q); 1233da2e5ae5SAllen Hubbe 1234da2e5ae5SAllen Hubbe spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags); 1235448c6fb3SJon Mason 1236e26a5843SAllen Hubbe if (qp->rx_handler && qp->client_ready) 1237448c6fb3SJon Mason qp->rx_handler(qp, qp->cb_data, cb_data, len); 1238da2e5ae5SAllen Hubbe 1239da2e5ae5SAllen Hubbe spin_lock_irqsave(&qp->ntb_rx_q_lock, irqflags); 1240da2e5ae5SAllen Hubbe } 1241da2e5ae5SAllen Hubbe 1242da2e5ae5SAllen Hubbe spin_unlock_irqrestore(&qp->ntb_rx_q_lock, irqflags); 1243da2e5ae5SAllen Hubbe } 1244da2e5ae5SAllen Hubbe 1245da2e5ae5SAllen Hubbe static void ntb_rx_copy_callback(void *data) 1246da2e5ae5SAllen Hubbe { 1247da2e5ae5SAllen Hubbe struct ntb_queue_entry *entry = data; 1248da2e5ae5SAllen Hubbe 1249da2e5ae5SAllen Hubbe entry->flags |= DESC_DONE_FLAG; 1250da2e5ae5SAllen Hubbe 1251da2e5ae5SAllen Hubbe ntb_complete_rxc(entry->qp); 1252fce8a7bbSJon Mason } 1253fce8a7bbSJon Mason 1254282a2feeSJon Mason static void ntb_memcpy_rx(struct ntb_queue_entry *entry, void *offset) 1255282a2feeSJon Mason { 1256282a2feeSJon Mason void *buf = entry->buf; 1257282a2feeSJon Mason size_t len = entry->len; 1258282a2feeSJon Mason 1259282a2feeSJon Mason memcpy(buf, offset, len); 1260282a2feeSJon Mason 1261e26a5843SAllen Hubbe /* Ensure that the data is fully copied out before clearing the flag */ 1262e26a5843SAllen Hubbe wmb(); 1263e26a5843SAllen Hubbe 1264282a2feeSJon Mason ntb_rx_copy_callback(entry); 1265282a2feeSJon Mason } 1266282a2feeSJon Mason 1267da2e5ae5SAllen Hubbe static void ntb_async_rx(struct ntb_queue_entry *entry, void *offset) 1268282a2feeSJon Mason { 1269282a2feeSJon Mason struct dma_async_tx_descriptor *txd; 1270282a2feeSJon Mason struct ntb_transport_qp *qp = entry->qp; 1271569410caSDave Jiang struct dma_chan *chan = qp->rx_dma_chan; 1272282a2feeSJon Mason struct dma_device *device; 1273da2e5ae5SAllen Hubbe size_t pay_off, buff_off, len; 12746f57fd05SBartlomiej Zolnierkiewicz struct dmaengine_unmap_data *unmap; 1275282a2feeSJon Mason dma_cookie_t cookie; 1276282a2feeSJon Mason void *buf = entry->buf; 12778c874cc1SDave Jiang int retries = 0; 1278282a2feeSJon Mason 1279da2e5ae5SAllen Hubbe len = entry->len; 1280282a2feeSJon Mason 1281282a2feeSJon Mason if (!chan) 1282282a2feeSJon Mason goto err; 1283282a2feeSJon Mason 1284282a2feeSJon Mason if (len < copy_bytes) 1285905921e7SAllen Hubbe goto err; 1286282a2feeSJon Mason 1287282a2feeSJon Mason device = chan->device; 1288282a2feeSJon Mason pay_off = (size_t)offset & ~PAGE_MASK; 1289282a2feeSJon Mason buff_off = (size_t)buf & ~PAGE_MASK; 1290282a2feeSJon Mason 1291282a2feeSJon Mason if (!is_dma_copy_aligned(device, pay_off, buff_off, len)) 1292905921e7SAllen Hubbe goto err; 1293282a2feeSJon Mason 12946f57fd05SBartlomiej Zolnierkiewicz unmap = dmaengine_get_unmap_data(device->dev, 2, GFP_NOWAIT); 12956f57fd05SBartlomiej Zolnierkiewicz if (!unmap) 1296905921e7SAllen Hubbe goto err; 1297282a2feeSJon Mason 12986f57fd05SBartlomiej Zolnierkiewicz unmap->len = len; 12996f57fd05SBartlomiej Zolnierkiewicz unmap->addr[0] = dma_map_page(device->dev, virt_to_page(offset), 13006f57fd05SBartlomiej Zolnierkiewicz pay_off, len, DMA_TO_DEVICE); 13016f57fd05SBartlomiej Zolnierkiewicz if (dma_mapping_error(device->dev, unmap->addr[0])) 13026f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap; 1303282a2feeSJon Mason 13046f57fd05SBartlomiej Zolnierkiewicz unmap->to_cnt = 1; 13056f57fd05SBartlomiej Zolnierkiewicz 13066f57fd05SBartlomiej Zolnierkiewicz unmap->addr[1] = dma_map_page(device->dev, virt_to_page(buf), 13076f57fd05SBartlomiej Zolnierkiewicz buff_off, len, DMA_FROM_DEVICE); 13086f57fd05SBartlomiej Zolnierkiewicz if (dma_mapping_error(device->dev, unmap->addr[1])) 13096f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap; 13106f57fd05SBartlomiej Zolnierkiewicz 13116f57fd05SBartlomiej Zolnierkiewicz unmap->from_cnt = 1; 13126f57fd05SBartlomiej Zolnierkiewicz 13138c874cc1SDave Jiang for (retries = 0; retries < DMA_RETRIES; retries++) { 13146f57fd05SBartlomiej Zolnierkiewicz txd = device->device_prep_dma_memcpy(chan, unmap->addr[1], 13150776ae7bSBartlomiej Zolnierkiewicz unmap->addr[0], len, 13160776ae7bSBartlomiej Zolnierkiewicz DMA_PREP_INTERRUPT); 13178c874cc1SDave Jiang if (txd) 13188c874cc1SDave Jiang break; 13198c874cc1SDave Jiang 13208c874cc1SDave Jiang set_current_state(TASK_INTERRUPTIBLE); 13218c874cc1SDave Jiang schedule_timeout(DMA_OUT_RESOURCE_TO); 13228c874cc1SDave Jiang } 13238c874cc1SDave Jiang 13248c874cc1SDave Jiang if (!txd) { 13258c874cc1SDave Jiang qp->dma_rx_prep_err++; 13266f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap; 13278c874cc1SDave Jiang } 1328282a2feeSJon Mason 1329282a2feeSJon Mason txd->callback = ntb_rx_copy_callback; 1330282a2feeSJon Mason txd->callback_param = entry; 13316f57fd05SBartlomiej Zolnierkiewicz dma_set_unmap(txd, unmap); 1332282a2feeSJon Mason 1333282a2feeSJon Mason cookie = dmaengine_submit(txd); 1334282a2feeSJon Mason if (dma_submit_error(cookie)) 13356f57fd05SBartlomiej Zolnierkiewicz goto err_set_unmap; 13366f57fd05SBartlomiej Zolnierkiewicz 13376f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap); 1338282a2feeSJon Mason 1339282a2feeSJon Mason qp->last_cookie = cookie; 1340282a2feeSJon Mason 1341282a2feeSJon Mason qp->rx_async++; 1342282a2feeSJon Mason 1343282a2feeSJon Mason return; 1344282a2feeSJon Mason 13456f57fd05SBartlomiej Zolnierkiewicz err_set_unmap: 13466f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap); 13476f57fd05SBartlomiej Zolnierkiewicz err_get_unmap: 13486f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap); 1349282a2feeSJon Mason err: 1350282a2feeSJon Mason ntb_memcpy_rx(entry, offset); 1351282a2feeSJon Mason qp->rx_memcpy++; 1352282a2feeSJon Mason } 1353282a2feeSJon Mason 1354fce8a7bbSJon Mason static int ntb_process_rxc(struct ntb_transport_qp *qp) 1355fce8a7bbSJon Mason { 1356fce8a7bbSJon Mason struct ntb_payload_header *hdr; 1357fce8a7bbSJon Mason struct ntb_queue_entry *entry; 1358fce8a7bbSJon Mason void *offset; 1359fce8a7bbSJon Mason 1360793c20e9SJon Mason offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index; 1361793c20e9SJon Mason hdr = offset + qp->rx_max_frame - sizeof(struct ntb_payload_header); 1362793c20e9SJon Mason 1363e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "qp %d: RX ver %u len %d flags %x\n", 1364e26a5843SAllen Hubbe qp->qp_num, hdr->ver, hdr->len, hdr->flags); 1365fce8a7bbSJon Mason 1366fce8a7bbSJon Mason if (!(hdr->flags & DESC_DONE_FLAG)) { 1367e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "done flag not set\n"); 1368fce8a7bbSJon Mason qp->rx_ring_empty++; 1369fce8a7bbSJon Mason return -EAGAIN; 1370fce8a7bbSJon Mason } 1371fce8a7bbSJon Mason 1372e26a5843SAllen Hubbe if (hdr->flags & LINK_DOWN_FLAG) { 1373e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "link down flag set\n"); 1374e26a5843SAllen Hubbe ntb_qp_link_down(qp); 1375e26a5843SAllen Hubbe hdr->flags = 0; 1376c0900b33SAllen Hubbe return -EAGAIN; 1377e26a5843SAllen Hubbe } 1378e26a5843SAllen Hubbe 137974465645SJon Mason if (hdr->ver != (u32)qp->rx_pkts) { 1380e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, 1381e26a5843SAllen Hubbe "version mismatch, expected %llu - got %u\n", 1382e26a5843SAllen Hubbe qp->rx_pkts, hdr->ver); 1383fce8a7bbSJon Mason qp->rx_err_ver++; 1384fce8a7bbSJon Mason return -EIO; 1385fce8a7bbSJon Mason } 1386fce8a7bbSJon Mason 1387da2e5ae5SAllen Hubbe entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q); 1388e26a5843SAllen Hubbe if (!entry) { 1389e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "no receive buffer\n"); 1390e26a5843SAllen Hubbe qp->rx_err_no_buf++; 1391da2e5ae5SAllen Hubbe return -EAGAIN; 1392fce8a7bbSJon Mason } 1393fce8a7bbSJon Mason 1394da2e5ae5SAllen Hubbe entry->rx_hdr = hdr; 1395da2e5ae5SAllen Hubbe entry->index = qp->rx_index; 1396da2e5ae5SAllen Hubbe 1397e26a5843SAllen Hubbe if (hdr->len > entry->len) { 1398e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, 1399e26a5843SAllen Hubbe "receive buffer overflow! Wanted %d got %d\n", 1400e26a5843SAllen Hubbe hdr->len, entry->len); 1401e26a5843SAllen Hubbe qp->rx_err_oflow++; 1402e26a5843SAllen Hubbe 1403da2e5ae5SAllen Hubbe entry->len = -EIO; 1404da2e5ae5SAllen Hubbe entry->flags |= DESC_DONE_FLAG; 1405e26a5843SAllen Hubbe 1406da2e5ae5SAllen Hubbe ntb_complete_rxc(qp); 1407da2e5ae5SAllen Hubbe } else { 1408e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, 1409e26a5843SAllen Hubbe "RX OK index %u ver %u size %d into buf size %d\n", 1410793c20e9SJon Mason qp->rx_index, hdr->ver, hdr->len, entry->len); 1411fce8a7bbSJon Mason 1412282a2feeSJon Mason qp->rx_bytes += hdr->len; 1413282a2feeSJon Mason qp->rx_pkts++; 1414fce8a7bbSJon Mason 1415da2e5ae5SAllen Hubbe entry->len = hdr->len; 1416282a2feeSJon Mason 1417da2e5ae5SAllen Hubbe ntb_async_rx(entry, offset); 1418da2e5ae5SAllen Hubbe } 1419fce8a7bbSJon Mason 1420282a2feeSJon Mason qp->rx_index++; 1421282a2feeSJon Mason qp->rx_index %= qp->rx_max_entry; 1422282a2feeSJon Mason 1423282a2feeSJon Mason return 0; 1424fce8a7bbSJon Mason } 1425fce8a7bbSJon Mason 1426e26a5843SAllen Hubbe static void ntb_transport_rxc_db(unsigned long data) 1427fce8a7bbSJon Mason { 1428e26a5843SAllen Hubbe struct ntb_transport_qp *qp = (void *)data; 1429c336acd3SJon Mason int rc, i; 1430fce8a7bbSJon Mason 1431e26a5843SAllen Hubbe dev_dbg(&qp->ndev->pdev->dev, "%s: doorbell %d received\n", 1432e26a5843SAllen Hubbe __func__, qp->qp_num); 1433e8aeb60cSJon Mason 1434c336acd3SJon Mason /* Limit the number of packets processed in a single interrupt to 1435c336acd3SJon Mason * provide fairness to others 1436c336acd3SJon Mason */ 1437c336acd3SJon Mason for (i = 0; i < qp->rx_max_entry; i++) { 1438fce8a7bbSJon Mason rc = ntb_process_rxc(qp); 1439c336acd3SJon Mason if (rc) 1440c336acd3SJon Mason break; 1441c336acd3SJon Mason } 1442282a2feeSJon Mason 1443569410caSDave Jiang if (i && qp->rx_dma_chan) 1444569410caSDave Jiang dma_async_issue_pending(qp->rx_dma_chan); 1445fce8a7bbSJon Mason 1446e26a5843SAllen Hubbe if (i == qp->rx_max_entry) { 1447e26a5843SAllen Hubbe /* there is more work to do */ 1448e9021331SDave Jiang if (qp->active) 1449e26a5843SAllen Hubbe tasklet_schedule(&qp->rxc_db_work); 1450e26a5843SAllen Hubbe } else if (ntb_db_read(qp->ndev) & BIT_ULL(qp->qp_num)) { 1451e26a5843SAllen Hubbe /* the doorbell bit is set: clear it */ 1452e26a5843SAllen Hubbe ntb_db_clear(qp->ndev, BIT_ULL(qp->qp_num)); 1453e26a5843SAllen Hubbe /* ntb_db_read ensures ntb_db_clear write is committed */ 1454e26a5843SAllen Hubbe ntb_db_read(qp->ndev); 1455e26a5843SAllen Hubbe 1456e26a5843SAllen Hubbe /* an interrupt may have arrived between finishing 1457e26a5843SAllen Hubbe * ntb_process_rxc and clearing the doorbell bit: 1458e26a5843SAllen Hubbe * there might be some more work to do. 1459e26a5843SAllen Hubbe */ 1460e9021331SDave Jiang if (qp->active) 1461e26a5843SAllen Hubbe tasklet_schedule(&qp->rxc_db_work); 1462e26a5843SAllen Hubbe } 1463fce8a7bbSJon Mason } 1464fce8a7bbSJon Mason 1465282a2feeSJon Mason static void ntb_tx_copy_callback(void *data) 1466fce8a7bbSJon Mason { 1467282a2feeSJon Mason struct ntb_queue_entry *entry = data; 1468282a2feeSJon Mason struct ntb_transport_qp *qp = entry->qp; 1469282a2feeSJon Mason struct ntb_payload_header __iomem *hdr = entry->tx_hdr; 1470fce8a7bbSJon Mason 147174465645SJon Mason iowrite32(entry->flags | DESC_DONE_FLAG, &hdr->flags); 1472fce8a7bbSJon Mason 1473e26a5843SAllen Hubbe ntb_peer_db_set(qp->ndev, BIT_ULL(qp->qp_num)); 1474fce8a7bbSJon Mason 1475fce8a7bbSJon Mason /* The entry length can only be zero if the packet is intended to be a 1476fce8a7bbSJon Mason * "link down" or similar. Since no payload is being sent in these 1477fce8a7bbSJon Mason * cases, there is nothing to add to the completion queue. 1478fce8a7bbSJon Mason */ 1479fce8a7bbSJon Mason if (entry->len > 0) { 1480fce8a7bbSJon Mason qp->tx_bytes += entry->len; 1481fce8a7bbSJon Mason 1482fce8a7bbSJon Mason if (qp->tx_handler) 1483fce8a7bbSJon Mason qp->tx_handler(qp, qp->cb_data, entry->cb_data, 1484fce8a7bbSJon Mason entry->len); 1485fce8a7bbSJon Mason } 1486fce8a7bbSJon Mason 1487fce8a7bbSJon Mason ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, &qp->tx_free_q); 1488fce8a7bbSJon Mason } 1489fce8a7bbSJon Mason 1490282a2feeSJon Mason static void ntb_memcpy_tx(struct ntb_queue_entry *entry, void __iomem *offset) 1491282a2feeSJon Mason { 149206917f75SDave Jiang #ifdef ARCH_HAS_NOCACHE_UACCESS 149306917f75SDave Jiang /* 149406917f75SDave Jiang * Using non-temporal mov to improve performance on non-cached 149506917f75SDave Jiang * writes, even though we aren't actually copying from user space. 149606917f75SDave Jiang */ 149706917f75SDave Jiang __copy_from_user_inatomic_nocache(offset, entry->buf, entry->len); 149806917f75SDave Jiang #else 1499282a2feeSJon Mason memcpy_toio(offset, entry->buf, entry->len); 150006917f75SDave Jiang #endif 1501282a2feeSJon Mason 1502e26a5843SAllen Hubbe /* Ensure that the data is fully copied out before setting the flags */ 1503e26a5843SAllen Hubbe wmb(); 1504e26a5843SAllen Hubbe 1505282a2feeSJon Mason ntb_tx_copy_callback(entry); 1506282a2feeSJon Mason } 1507282a2feeSJon Mason 1508282a2feeSJon Mason static void ntb_async_tx(struct ntb_transport_qp *qp, 1509282a2feeSJon Mason struct ntb_queue_entry *entry) 1510282a2feeSJon Mason { 1511282a2feeSJon Mason struct ntb_payload_header __iomem *hdr; 1512282a2feeSJon Mason struct dma_async_tx_descriptor *txd; 1513569410caSDave Jiang struct dma_chan *chan = qp->tx_dma_chan; 1514282a2feeSJon Mason struct dma_device *device; 1515282a2feeSJon Mason size_t dest_off, buff_off; 15166f57fd05SBartlomiej Zolnierkiewicz struct dmaengine_unmap_data *unmap; 15176f57fd05SBartlomiej Zolnierkiewicz dma_addr_t dest; 1518282a2feeSJon Mason dma_cookie_t cookie; 1519282a2feeSJon Mason void __iomem *offset; 1520282a2feeSJon Mason size_t len = entry->len; 1521282a2feeSJon Mason void *buf = entry->buf; 15228c874cc1SDave Jiang int retries = 0; 1523282a2feeSJon Mason 1524282a2feeSJon Mason offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index; 1525282a2feeSJon Mason hdr = offset + qp->tx_max_frame - sizeof(struct ntb_payload_header); 1526282a2feeSJon Mason entry->tx_hdr = hdr; 1527282a2feeSJon Mason 1528282a2feeSJon Mason iowrite32(entry->len, &hdr->len); 1529282a2feeSJon Mason iowrite32((u32)qp->tx_pkts, &hdr->ver); 1530282a2feeSJon Mason 1531282a2feeSJon Mason if (!chan) 1532282a2feeSJon Mason goto err; 1533282a2feeSJon Mason 1534282a2feeSJon Mason if (len < copy_bytes) 1535282a2feeSJon Mason goto err; 1536282a2feeSJon Mason 1537282a2feeSJon Mason device = chan->device; 1538282a2feeSJon Mason dest = qp->tx_mw_phys + qp->tx_max_frame * qp->tx_index; 1539282a2feeSJon Mason buff_off = (size_t)buf & ~PAGE_MASK; 1540282a2feeSJon Mason dest_off = (size_t)dest & ~PAGE_MASK; 1541282a2feeSJon Mason 1542282a2feeSJon Mason if (!is_dma_copy_aligned(device, buff_off, dest_off, len)) 1543282a2feeSJon Mason goto err; 1544282a2feeSJon Mason 15456f57fd05SBartlomiej Zolnierkiewicz unmap = dmaengine_get_unmap_data(device->dev, 1, GFP_NOWAIT); 15466f57fd05SBartlomiej Zolnierkiewicz if (!unmap) 1547282a2feeSJon Mason goto err; 1548282a2feeSJon Mason 15496f57fd05SBartlomiej Zolnierkiewicz unmap->len = len; 15506f57fd05SBartlomiej Zolnierkiewicz unmap->addr[0] = dma_map_page(device->dev, virt_to_page(buf), 15516f57fd05SBartlomiej Zolnierkiewicz buff_off, len, DMA_TO_DEVICE); 15526f57fd05SBartlomiej Zolnierkiewicz if (dma_mapping_error(device->dev, unmap->addr[0])) 15536f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap; 15546f57fd05SBartlomiej Zolnierkiewicz 15556f57fd05SBartlomiej Zolnierkiewicz unmap->to_cnt = 1; 15566f57fd05SBartlomiej Zolnierkiewicz 15578c874cc1SDave Jiang for (retries = 0; retries < DMA_RETRIES; retries++) { 15588c874cc1SDave Jiang txd = device->device_prep_dma_memcpy(chan, dest, unmap->addr[0], 15598c874cc1SDave Jiang len, DMA_PREP_INTERRUPT); 15608c874cc1SDave Jiang if (txd) 15618c874cc1SDave Jiang break; 15628c874cc1SDave Jiang 15638c874cc1SDave Jiang set_current_state(TASK_INTERRUPTIBLE); 15648c874cc1SDave Jiang schedule_timeout(DMA_OUT_RESOURCE_TO); 15658c874cc1SDave Jiang } 15668c874cc1SDave Jiang 15678c874cc1SDave Jiang if (!txd) { 15688c874cc1SDave Jiang qp->dma_tx_prep_err++; 15696f57fd05SBartlomiej Zolnierkiewicz goto err_get_unmap; 15708c874cc1SDave Jiang } 1571282a2feeSJon Mason 1572282a2feeSJon Mason txd->callback = ntb_tx_copy_callback; 1573282a2feeSJon Mason txd->callback_param = entry; 15746f57fd05SBartlomiej Zolnierkiewicz dma_set_unmap(txd, unmap); 1575282a2feeSJon Mason 1576282a2feeSJon Mason cookie = dmaengine_submit(txd); 1577282a2feeSJon Mason if (dma_submit_error(cookie)) 15786f57fd05SBartlomiej Zolnierkiewicz goto err_set_unmap; 15796f57fd05SBartlomiej Zolnierkiewicz 15806f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap); 1581282a2feeSJon Mason 1582282a2feeSJon Mason dma_async_issue_pending(chan); 1583282a2feeSJon Mason qp->tx_async++; 1584282a2feeSJon Mason 1585282a2feeSJon Mason return; 15866f57fd05SBartlomiej Zolnierkiewicz err_set_unmap: 15876f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap); 15886f57fd05SBartlomiej Zolnierkiewicz err_get_unmap: 15896f57fd05SBartlomiej Zolnierkiewicz dmaengine_unmap_put(unmap); 1590282a2feeSJon Mason err: 1591282a2feeSJon Mason ntb_memcpy_tx(entry, offset); 1592282a2feeSJon Mason qp->tx_memcpy++; 1593282a2feeSJon Mason } 1594282a2feeSJon Mason 1595fce8a7bbSJon Mason static int ntb_process_tx(struct ntb_transport_qp *qp, 1596fce8a7bbSJon Mason struct ntb_queue_entry *entry) 1597fce8a7bbSJon Mason { 1598793c20e9SJon Mason if (qp->tx_index == qp->remote_rx_info->entry) { 1599fce8a7bbSJon Mason qp->tx_ring_full++; 1600fce8a7bbSJon Mason return -EAGAIN; 1601fce8a7bbSJon Mason } 1602fce8a7bbSJon Mason 1603ef114ed5SJon Mason if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) { 1604fce8a7bbSJon Mason if (qp->tx_handler) 1605179f912aSJon Mason qp->tx_handler(qp, qp->cb_data, NULL, -EIO); 1606fce8a7bbSJon Mason 1607fce8a7bbSJon Mason ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, 1608fce8a7bbSJon Mason &qp->tx_free_q); 1609fce8a7bbSJon Mason return 0; 1610fce8a7bbSJon Mason } 1611fce8a7bbSJon Mason 1612282a2feeSJon Mason ntb_async_tx(qp, entry); 1613fce8a7bbSJon Mason 1614793c20e9SJon Mason qp->tx_index++; 1615793c20e9SJon Mason qp->tx_index %= qp->tx_max_entry; 1616fce8a7bbSJon Mason 1617fce8a7bbSJon Mason qp->tx_pkts++; 1618fce8a7bbSJon Mason 1619fce8a7bbSJon Mason return 0; 1620fce8a7bbSJon Mason } 1621fce8a7bbSJon Mason 1622fce8a7bbSJon Mason static void ntb_send_link_down(struct ntb_transport_qp *qp) 1623fce8a7bbSJon Mason { 1624e26a5843SAllen Hubbe struct pci_dev *pdev = qp->ndev->pdev; 1625fce8a7bbSJon Mason struct ntb_queue_entry *entry; 1626fce8a7bbSJon Mason int i, rc; 1627fce8a7bbSJon Mason 1628e26a5843SAllen Hubbe if (!qp->link_is_up) 1629fce8a7bbSJon Mason return; 1630fce8a7bbSJon Mason 1631e22e0b9dSAllen Hubbe dev_info(&pdev->dev, "qp %d: Send Link Down\n", qp->qp_num); 1632fce8a7bbSJon Mason 1633fce8a7bbSJon Mason for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) { 1634f766755cSJon Mason entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q); 1635fce8a7bbSJon Mason if (entry) 1636fce8a7bbSJon Mason break; 1637fce8a7bbSJon Mason msleep(100); 1638fce8a7bbSJon Mason } 1639fce8a7bbSJon Mason 1640fce8a7bbSJon Mason if (!entry) 1641fce8a7bbSJon Mason return; 1642fce8a7bbSJon Mason 1643fce8a7bbSJon Mason entry->cb_data = NULL; 1644fce8a7bbSJon Mason entry->buf = NULL; 1645fce8a7bbSJon Mason entry->len = 0; 1646fce8a7bbSJon Mason entry->flags = LINK_DOWN_FLAG; 1647fce8a7bbSJon Mason 1648fce8a7bbSJon Mason rc = ntb_process_tx(qp, entry); 1649fce8a7bbSJon Mason if (rc) 1650fce8a7bbSJon Mason dev_err(&pdev->dev, "ntb: QP%d unable to send linkdown msg\n", 1651fce8a7bbSJon Mason qp->qp_num); 16522849b5d7SAllen Hubbe 16532849b5d7SAllen Hubbe ntb_qp_link_down_reset(qp); 1654fce8a7bbSJon Mason } 1655fce8a7bbSJon Mason 16561199aa61SAllen Hubbe static bool ntb_dma_filter_fn(struct dma_chan *chan, void *node) 16571199aa61SAllen Hubbe { 16581199aa61SAllen Hubbe return dev_to_node(&chan->dev->device) == (int)(unsigned long)node; 16591199aa61SAllen Hubbe } 16601199aa61SAllen Hubbe 1661fce8a7bbSJon Mason /** 1662fce8a7bbSJon Mason * ntb_transport_create_queue - Create a new NTB transport layer queue 1663fce8a7bbSJon Mason * @rx_handler: receive callback function 1664fce8a7bbSJon Mason * @tx_handler: transmit callback function 1665fce8a7bbSJon Mason * @event_handler: event callback function 1666fce8a7bbSJon Mason * 1667fce8a7bbSJon Mason * Create a new NTB transport layer queue and provide the queue with a callback 1668fce8a7bbSJon Mason * routine for both transmit and receive. The receive callback routine will be 1669fce8a7bbSJon Mason * used to pass up data when the transport has received it on the queue. The 1670fce8a7bbSJon Mason * transmit callback routine will be called when the transport has completed the 1671fce8a7bbSJon Mason * transmission of the data on the queue and the data is ready to be freed. 1672fce8a7bbSJon Mason * 1673fce8a7bbSJon Mason * RETURNS: pointer to newly created ntb_queue, NULL on error. 1674fce8a7bbSJon Mason */ 1675fce8a7bbSJon Mason struct ntb_transport_qp * 1676e26a5843SAllen Hubbe ntb_transport_create_queue(void *data, struct device *client_dev, 1677fce8a7bbSJon Mason const struct ntb_queue_handlers *handlers) 1678fce8a7bbSJon Mason { 1679e26a5843SAllen Hubbe struct ntb_dev *ndev; 1680e26a5843SAllen Hubbe struct pci_dev *pdev; 1681e26a5843SAllen Hubbe struct ntb_transport_ctx *nt; 1682fce8a7bbSJon Mason struct ntb_queue_entry *entry; 1683fce8a7bbSJon Mason struct ntb_transport_qp *qp; 1684e26a5843SAllen Hubbe u64 qp_bit; 1685fce8a7bbSJon Mason unsigned int free_queue; 16861199aa61SAllen Hubbe dma_cap_mask_t dma_mask; 16871199aa61SAllen Hubbe int node; 1688e26a5843SAllen Hubbe int i; 1689fce8a7bbSJon Mason 1690e26a5843SAllen Hubbe ndev = dev_ntb(client_dev->parent); 1691e26a5843SAllen Hubbe pdev = ndev->pdev; 1692e26a5843SAllen Hubbe nt = ndev->ctx; 1693fce8a7bbSJon Mason 16941199aa61SAllen Hubbe node = dev_to_node(&ndev->dev); 16951199aa61SAllen Hubbe 1696fce8a7bbSJon Mason free_queue = ffs(nt->qp_bitmap); 1697fce8a7bbSJon Mason if (!free_queue) 1698fce8a7bbSJon Mason goto err; 1699fce8a7bbSJon Mason 1700fce8a7bbSJon Mason /* decrement free_queue to make it zero based */ 1701fce8a7bbSJon Mason free_queue--; 1702fce8a7bbSJon Mason 1703e26a5843SAllen Hubbe qp = &nt->qp_vec[free_queue]; 1704e26a5843SAllen Hubbe qp_bit = BIT_ULL(qp->qp_num); 1705fce8a7bbSJon Mason 1706e26a5843SAllen Hubbe nt->qp_bitmap_free &= ~qp_bit; 1707e26a5843SAllen Hubbe 1708fce8a7bbSJon Mason qp->cb_data = data; 1709fce8a7bbSJon Mason qp->rx_handler = handlers->rx_handler; 1710fce8a7bbSJon Mason qp->tx_handler = handlers->tx_handler; 1711fce8a7bbSJon Mason qp->event_handler = handlers->event_handler; 1712fce8a7bbSJon Mason 17131199aa61SAllen Hubbe dma_cap_zero(dma_mask); 17141199aa61SAllen Hubbe dma_cap_set(DMA_MEMCPY, dma_mask); 17151199aa61SAllen Hubbe 1716a41ef053SDave Jiang if (use_dma) { 1717569410caSDave Jiang qp->tx_dma_chan = 1718569410caSDave Jiang dma_request_channel(dma_mask, ntb_dma_filter_fn, 17191199aa61SAllen Hubbe (void *)(unsigned long)node); 1720569410caSDave Jiang if (!qp->tx_dma_chan) 1721569410caSDave Jiang dev_info(&pdev->dev, "Unable to allocate TX DMA channel\n"); 1722569410caSDave Jiang 1723569410caSDave Jiang qp->rx_dma_chan = 1724569410caSDave Jiang dma_request_channel(dma_mask, ntb_dma_filter_fn, 1725569410caSDave Jiang (void *)(unsigned long)node); 1726569410caSDave Jiang if (!qp->rx_dma_chan) 1727569410caSDave Jiang dev_info(&pdev->dev, "Unable to allocate RX DMA channel\n"); 1728a41ef053SDave Jiang } else { 1729569410caSDave Jiang qp->tx_dma_chan = NULL; 1730569410caSDave Jiang qp->rx_dma_chan = NULL; 1731a41ef053SDave Jiang } 1732569410caSDave Jiang 1733569410caSDave Jiang dev_dbg(&pdev->dev, "Using %s memcpy for TX\n", 1734569410caSDave Jiang qp->tx_dma_chan ? "DMA" : "CPU"); 1735569410caSDave Jiang 1736569410caSDave Jiang dev_dbg(&pdev->dev, "Using %s memcpy for RX\n", 1737569410caSDave Jiang qp->rx_dma_chan ? "DMA" : "CPU"); 1738282a2feeSJon Mason 1739fce8a7bbSJon Mason for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) { 17401199aa61SAllen Hubbe entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node); 1741fce8a7bbSJon Mason if (!entry) 1742fce8a7bbSJon Mason goto err1; 1743fce8a7bbSJon Mason 1744282a2feeSJon Mason entry->qp = qp; 1745da2e5ae5SAllen Hubbe ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, 1746fce8a7bbSJon Mason &qp->rx_free_q); 1747fce8a7bbSJon Mason } 1748*a754a8fcSDave Jiang qp->rx_alloc_entry = NTB_QP_DEF_NUM_ENTRIES; 1749fce8a7bbSJon Mason 1750*a754a8fcSDave Jiang for (i = 0; i < qp->tx_max_entry; i++) { 17511199aa61SAllen Hubbe entry = kzalloc_node(sizeof(*entry), GFP_ATOMIC, node); 1752fce8a7bbSJon Mason if (!entry) 1753fce8a7bbSJon Mason goto err2; 1754fce8a7bbSJon Mason 1755282a2feeSJon Mason entry->qp = qp; 1756fce8a7bbSJon Mason ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, 1757fce8a7bbSJon Mason &qp->tx_free_q); 1758fce8a7bbSJon Mason } 1759fce8a7bbSJon Mason 1760e26a5843SAllen Hubbe ntb_db_clear(qp->ndev, qp_bit); 1761e26a5843SAllen Hubbe ntb_db_clear_mask(qp->ndev, qp_bit); 1762fce8a7bbSJon Mason 1763fce8a7bbSJon Mason dev_info(&pdev->dev, "NTB Transport QP %d created\n", qp->qp_num); 1764fce8a7bbSJon Mason 1765fce8a7bbSJon Mason return qp; 1766fce8a7bbSJon Mason 1767fce8a7bbSJon Mason err2: 1768f766755cSJon Mason while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q))) 1769fce8a7bbSJon Mason kfree(entry); 1770fce8a7bbSJon Mason err1: 1771*a754a8fcSDave Jiang qp->rx_alloc_entry = 0; 1772da2e5ae5SAllen Hubbe while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q))) 1773fce8a7bbSJon Mason kfree(entry); 1774569410caSDave Jiang if (qp->tx_dma_chan) 1775569410caSDave Jiang dma_release_channel(qp->tx_dma_chan); 1776569410caSDave Jiang if (qp->rx_dma_chan) 1777569410caSDave Jiang dma_release_channel(qp->rx_dma_chan); 1778e26a5843SAllen Hubbe nt->qp_bitmap_free |= qp_bit; 1779fce8a7bbSJon Mason err: 1780fce8a7bbSJon Mason return NULL; 1781fce8a7bbSJon Mason } 1782fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_create_queue); 1783fce8a7bbSJon Mason 1784fce8a7bbSJon Mason /** 1785fce8a7bbSJon Mason * ntb_transport_free_queue - Frees NTB transport queue 1786fce8a7bbSJon Mason * @qp: NTB queue to be freed 1787fce8a7bbSJon Mason * 1788fce8a7bbSJon Mason * Frees NTB transport queue 1789fce8a7bbSJon Mason */ 1790fce8a7bbSJon Mason void ntb_transport_free_queue(struct ntb_transport_qp *qp) 1791fce8a7bbSJon Mason { 1792186f27ffSJon Mason struct pci_dev *pdev; 1793fce8a7bbSJon Mason struct ntb_queue_entry *entry; 1794e26a5843SAllen Hubbe u64 qp_bit; 1795fce8a7bbSJon Mason 1796fce8a7bbSJon Mason if (!qp) 1797fce8a7bbSJon Mason return; 1798fce8a7bbSJon Mason 1799e26a5843SAllen Hubbe pdev = qp->ndev->pdev; 1800186f27ffSJon Mason 1801e9021331SDave Jiang qp->active = false; 1802e9021331SDave Jiang 1803569410caSDave Jiang if (qp->tx_dma_chan) { 1804569410caSDave Jiang struct dma_chan *chan = qp->tx_dma_chan; 1805282a2feeSJon Mason /* Putting the dma_chan to NULL will force any new traffic to be 1806282a2feeSJon Mason * processed by the CPU instead of the DAM engine 1807282a2feeSJon Mason */ 1808569410caSDave Jiang qp->tx_dma_chan = NULL; 1809569410caSDave Jiang 1810569410caSDave Jiang /* Try to be nice and wait for any queued DMA engine 1811569410caSDave Jiang * transactions to process before smashing it with a rock 1812569410caSDave Jiang */ 1813569410caSDave Jiang dma_sync_wait(chan, qp->last_cookie); 1814569410caSDave Jiang dmaengine_terminate_all(chan); 1815569410caSDave Jiang dma_release_channel(chan); 1816569410caSDave Jiang } 1817569410caSDave Jiang 1818569410caSDave Jiang if (qp->rx_dma_chan) { 1819569410caSDave Jiang struct dma_chan *chan = qp->rx_dma_chan; 1820569410caSDave Jiang /* Putting the dma_chan to NULL will force any new traffic to be 1821569410caSDave Jiang * processed by the CPU instead of the DAM engine 1822569410caSDave Jiang */ 1823569410caSDave Jiang qp->rx_dma_chan = NULL; 1824282a2feeSJon Mason 1825282a2feeSJon Mason /* Try to be nice and wait for any queued DMA engine 1826282a2feeSJon Mason * transactions to process before smashing it with a rock 1827282a2feeSJon Mason */ 1828282a2feeSJon Mason dma_sync_wait(chan, qp->last_cookie); 1829282a2feeSJon Mason dmaengine_terminate_all(chan); 18301199aa61SAllen Hubbe dma_release_channel(chan); 1831282a2feeSJon Mason } 1832fce8a7bbSJon Mason 1833e26a5843SAllen Hubbe qp_bit = BIT_ULL(qp->qp_num); 1834e26a5843SAllen Hubbe 1835e26a5843SAllen Hubbe ntb_db_set_mask(qp->ndev, qp_bit); 1836e9021331SDave Jiang tasklet_kill(&qp->rxc_db_work); 1837fce8a7bbSJon Mason 1838282a2feeSJon Mason cancel_delayed_work_sync(&qp->link_work); 1839282a2feeSJon Mason 1840e26a5843SAllen Hubbe qp->cb_data = NULL; 1841e26a5843SAllen Hubbe qp->rx_handler = NULL; 1842e26a5843SAllen Hubbe qp->tx_handler = NULL; 1843e26a5843SAllen Hubbe qp->event_handler = NULL; 1844e26a5843SAllen Hubbe 1845da2e5ae5SAllen Hubbe while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q))) 1846fce8a7bbSJon Mason kfree(entry); 1847fce8a7bbSJon Mason 1848da2e5ae5SAllen Hubbe while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q))) { 1849da2e5ae5SAllen Hubbe dev_warn(&pdev->dev, "Freeing item from non-empty rx_pend_q\n"); 1850da2e5ae5SAllen Hubbe kfree(entry); 1851da2e5ae5SAllen Hubbe } 1852da2e5ae5SAllen Hubbe 1853da2e5ae5SAllen Hubbe while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q))) { 1854da2e5ae5SAllen Hubbe dev_warn(&pdev->dev, "Freeing item from non-empty rx_post_q\n"); 1855fce8a7bbSJon Mason kfree(entry); 1856fce8a7bbSJon Mason } 1857fce8a7bbSJon Mason 1858f766755cSJon Mason while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q))) 1859fce8a7bbSJon Mason kfree(entry); 1860fce8a7bbSJon Mason 186130a4bb1eSAllen Hubbe qp->transport->qp_bitmap_free |= qp_bit; 1862fce8a7bbSJon Mason 1863fce8a7bbSJon Mason dev_info(&pdev->dev, "NTB Transport QP %d freed\n", qp->qp_num); 1864fce8a7bbSJon Mason } 1865fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_free_queue); 1866fce8a7bbSJon Mason 1867fce8a7bbSJon Mason /** 1868fce8a7bbSJon Mason * ntb_transport_rx_remove - Dequeues enqueued rx packet 1869fce8a7bbSJon Mason * @qp: NTB queue to be freed 1870fce8a7bbSJon Mason * @len: pointer to variable to write enqueued buffers length 1871fce8a7bbSJon Mason * 1872fce8a7bbSJon Mason * Dequeues unused buffers from receive queue. Should only be used during 1873fce8a7bbSJon Mason * shutdown of qp. 1874fce8a7bbSJon Mason * 1875fce8a7bbSJon Mason * RETURNS: NULL error value on error, or void* for success. 1876fce8a7bbSJon Mason */ 1877fce8a7bbSJon Mason void *ntb_transport_rx_remove(struct ntb_transport_qp *qp, unsigned int *len) 1878fce8a7bbSJon Mason { 1879fce8a7bbSJon Mason struct ntb_queue_entry *entry; 1880fce8a7bbSJon Mason void *buf; 1881fce8a7bbSJon Mason 1882e26a5843SAllen Hubbe if (!qp || qp->client_ready) 1883fce8a7bbSJon Mason return NULL; 1884fce8a7bbSJon Mason 1885da2e5ae5SAllen Hubbe entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q); 1886fce8a7bbSJon Mason if (!entry) 1887fce8a7bbSJon Mason return NULL; 1888fce8a7bbSJon Mason 1889fce8a7bbSJon Mason buf = entry->cb_data; 1890fce8a7bbSJon Mason *len = entry->len; 1891fce8a7bbSJon Mason 1892da2e5ae5SAllen Hubbe ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_free_q); 1893fce8a7bbSJon Mason 1894fce8a7bbSJon Mason return buf; 1895fce8a7bbSJon Mason } 1896fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_rx_remove); 1897fce8a7bbSJon Mason 1898fce8a7bbSJon Mason /** 1899fce8a7bbSJon Mason * ntb_transport_rx_enqueue - Enqueue a new NTB queue entry 1900fce8a7bbSJon Mason * @qp: NTB transport layer queue the entry is to be enqueued on 1901fce8a7bbSJon Mason * @cb: per buffer pointer for callback function to use 1902fce8a7bbSJon Mason * @data: pointer to data buffer that incoming packets will be copied into 1903fce8a7bbSJon Mason * @len: length of the data buffer 1904fce8a7bbSJon Mason * 1905fce8a7bbSJon Mason * Enqueue a new receive buffer onto the transport queue into which a NTB 1906fce8a7bbSJon Mason * payload can be received into. 1907fce8a7bbSJon Mason * 1908fce8a7bbSJon Mason * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 1909fce8a7bbSJon Mason */ 1910fce8a7bbSJon Mason int ntb_transport_rx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data, 1911fce8a7bbSJon Mason unsigned int len) 1912fce8a7bbSJon Mason { 1913fce8a7bbSJon Mason struct ntb_queue_entry *entry; 1914fce8a7bbSJon Mason 1915fce8a7bbSJon Mason if (!qp) 1916fce8a7bbSJon Mason return -EINVAL; 1917fce8a7bbSJon Mason 1918da2e5ae5SAllen Hubbe entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_free_q); 1919fce8a7bbSJon Mason if (!entry) 1920fce8a7bbSJon Mason return -ENOMEM; 1921fce8a7bbSJon Mason 1922fce8a7bbSJon Mason entry->cb_data = cb; 1923fce8a7bbSJon Mason entry->buf = data; 1924fce8a7bbSJon Mason entry->len = len; 1925da2e5ae5SAllen Hubbe entry->flags = 0; 1926fce8a7bbSJon Mason 1927da2e5ae5SAllen Hubbe ntb_list_add(&qp->ntb_rx_q_lock, &entry->entry, &qp->rx_pend_q); 1928da2e5ae5SAllen Hubbe 1929e9021331SDave Jiang if (qp->active) 1930da2e5ae5SAllen Hubbe tasklet_schedule(&qp->rxc_db_work); 1931fce8a7bbSJon Mason 1932fce8a7bbSJon Mason return 0; 1933fce8a7bbSJon Mason } 1934fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_rx_enqueue); 1935fce8a7bbSJon Mason 1936fce8a7bbSJon Mason /** 1937fce8a7bbSJon Mason * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry 1938fce8a7bbSJon Mason * @qp: NTB transport layer queue the entry is to be enqueued on 1939fce8a7bbSJon Mason * @cb: per buffer pointer for callback function to use 1940fce8a7bbSJon Mason * @data: pointer to data buffer that will be sent 1941fce8a7bbSJon Mason * @len: length of the data buffer 1942fce8a7bbSJon Mason * 1943fce8a7bbSJon Mason * Enqueue a new transmit buffer onto the transport queue from which a NTB 1944f9a2cf89SJon Mason * payload will be transmitted. This assumes that a lock is being held to 1945fce8a7bbSJon Mason * serialize access to the qp. 1946fce8a7bbSJon Mason * 1947fce8a7bbSJon Mason * RETURNS: An appropriate -ERRNO error value on error, or zero for success. 1948fce8a7bbSJon Mason */ 1949fce8a7bbSJon Mason int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data, 1950fce8a7bbSJon Mason unsigned int len) 1951fce8a7bbSJon Mason { 1952fce8a7bbSJon Mason struct ntb_queue_entry *entry; 1953fce8a7bbSJon Mason int rc; 1954fce8a7bbSJon Mason 1955e26a5843SAllen Hubbe if (!qp || !qp->link_is_up || !len) 1956fce8a7bbSJon Mason return -EINVAL; 1957fce8a7bbSJon Mason 1958fce8a7bbSJon Mason entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q); 1959282a2feeSJon Mason if (!entry) { 1960282a2feeSJon Mason qp->tx_err_no_buf++; 1961e74bfeedSDave Jiang return -EBUSY; 1962282a2feeSJon Mason } 1963fce8a7bbSJon Mason 1964fce8a7bbSJon Mason entry->cb_data = cb; 1965fce8a7bbSJon Mason entry->buf = data; 1966fce8a7bbSJon Mason entry->len = len; 1967fce8a7bbSJon Mason entry->flags = 0; 1968fce8a7bbSJon Mason 1969fce8a7bbSJon Mason rc = ntb_process_tx(qp, entry); 1970fce8a7bbSJon Mason if (rc) 1971fce8a7bbSJon Mason ntb_list_add(&qp->ntb_tx_free_q_lock, &entry->entry, 1972fce8a7bbSJon Mason &qp->tx_free_q); 1973fce8a7bbSJon Mason 1974fce8a7bbSJon Mason return rc; 1975fce8a7bbSJon Mason } 1976fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_tx_enqueue); 1977fce8a7bbSJon Mason 1978fce8a7bbSJon Mason /** 1979fce8a7bbSJon Mason * ntb_transport_link_up - Notify NTB transport of client readiness to use queue 1980fce8a7bbSJon Mason * @qp: NTB transport layer queue to be enabled 1981fce8a7bbSJon Mason * 1982fce8a7bbSJon Mason * Notify NTB transport layer of client readiness to use queue 1983fce8a7bbSJon Mason */ 1984fce8a7bbSJon Mason void ntb_transport_link_up(struct ntb_transport_qp *qp) 1985fce8a7bbSJon Mason { 1986fce8a7bbSJon Mason if (!qp) 1987fce8a7bbSJon Mason return; 1988fce8a7bbSJon Mason 1989e26a5843SAllen Hubbe qp->client_ready = true; 1990fce8a7bbSJon Mason 1991e26a5843SAllen Hubbe if (qp->transport->link_is_up) 1992fce8a7bbSJon Mason schedule_delayed_work(&qp->link_work, 0); 1993fce8a7bbSJon Mason } 1994fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_link_up); 1995fce8a7bbSJon Mason 1996fce8a7bbSJon Mason /** 1997fce8a7bbSJon Mason * ntb_transport_link_down - Notify NTB transport to no longer enqueue data 1998fce8a7bbSJon Mason * @qp: NTB transport layer queue to be disabled 1999fce8a7bbSJon Mason * 2000fce8a7bbSJon Mason * Notify NTB transport layer of client's desire to no longer receive data on 2001fce8a7bbSJon Mason * transport queue specified. It is the client's responsibility to ensure all 2002f9a2cf89SJon Mason * entries on queue are purged or otherwise handled appropriately. 2003fce8a7bbSJon Mason */ 2004fce8a7bbSJon Mason void ntb_transport_link_down(struct ntb_transport_qp *qp) 2005fce8a7bbSJon Mason { 2006e26a5843SAllen Hubbe int val; 2007fce8a7bbSJon Mason 2008fce8a7bbSJon Mason if (!qp) 2009fce8a7bbSJon Mason return; 2010fce8a7bbSJon Mason 2011e26a5843SAllen Hubbe qp->client_ready = false; 2012fce8a7bbSJon Mason 2013e26a5843SAllen Hubbe val = ntb_spad_read(qp->ndev, QP_LINKS); 2014fce8a7bbSJon Mason 2015e26a5843SAllen Hubbe ntb_peer_spad_write(qp->ndev, QP_LINKS, 2016e26a5843SAllen Hubbe val & ~BIT(qp->qp_num)); 2017fce8a7bbSJon Mason 2018e26a5843SAllen Hubbe if (qp->link_is_up) 2019fce8a7bbSJon Mason ntb_send_link_down(qp); 2020fce8a7bbSJon Mason else 2021fce8a7bbSJon Mason cancel_delayed_work_sync(&qp->link_work); 2022fce8a7bbSJon Mason } 2023fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_link_down); 2024fce8a7bbSJon Mason 2025fce8a7bbSJon Mason /** 2026fce8a7bbSJon Mason * ntb_transport_link_query - Query transport link state 2027fce8a7bbSJon Mason * @qp: NTB transport layer queue to be queried 2028fce8a7bbSJon Mason * 2029fce8a7bbSJon Mason * Query connectivity to the remote system of the NTB transport queue 2030fce8a7bbSJon Mason * 2031fce8a7bbSJon Mason * RETURNS: true for link up or false for link down 2032fce8a7bbSJon Mason */ 2033fce8a7bbSJon Mason bool ntb_transport_link_query(struct ntb_transport_qp *qp) 2034fce8a7bbSJon Mason { 2035186f27ffSJon Mason if (!qp) 2036186f27ffSJon Mason return false; 2037186f27ffSJon Mason 2038e26a5843SAllen Hubbe return qp->link_is_up; 2039fce8a7bbSJon Mason } 2040fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_link_query); 2041fce8a7bbSJon Mason 2042fce8a7bbSJon Mason /** 2043fce8a7bbSJon Mason * ntb_transport_qp_num - Query the qp number 2044fce8a7bbSJon Mason * @qp: NTB transport layer queue to be queried 2045fce8a7bbSJon Mason * 2046fce8a7bbSJon Mason * Query qp number of the NTB transport queue 2047fce8a7bbSJon Mason * 2048fce8a7bbSJon Mason * RETURNS: a zero based number specifying the qp number 2049fce8a7bbSJon Mason */ 2050fce8a7bbSJon Mason unsigned char ntb_transport_qp_num(struct ntb_transport_qp *qp) 2051fce8a7bbSJon Mason { 2052186f27ffSJon Mason if (!qp) 2053186f27ffSJon Mason return 0; 2054186f27ffSJon Mason 2055fce8a7bbSJon Mason return qp->qp_num; 2056fce8a7bbSJon Mason } 2057fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_qp_num); 2058fce8a7bbSJon Mason 2059fce8a7bbSJon Mason /** 2060fce8a7bbSJon Mason * ntb_transport_max_size - Query the max payload size of a qp 2061fce8a7bbSJon Mason * @qp: NTB transport layer queue to be queried 2062fce8a7bbSJon Mason * 2063fce8a7bbSJon Mason * Query the maximum payload size permissible on the given qp 2064fce8a7bbSJon Mason * 2065fce8a7bbSJon Mason * RETURNS: the max payload size of a qp 2066fce8a7bbSJon Mason */ 2067ef114ed5SJon Mason unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp) 2068fce8a7bbSJon Mason { 206904afde45SDave Jiang unsigned int max_size; 2070569410caSDave Jiang unsigned int copy_align; 207104afde45SDave Jiang struct dma_chan *rx_chan, *tx_chan; 2072282a2feeSJon Mason 2073186f27ffSJon Mason if (!qp) 2074186f27ffSJon Mason return 0; 2075186f27ffSJon Mason 207604afde45SDave Jiang rx_chan = qp->rx_dma_chan; 207704afde45SDave Jiang tx_chan = qp->tx_dma_chan; 2078282a2feeSJon Mason 207904afde45SDave Jiang copy_align = max(rx_chan ? rx_chan->device->copy_align : 0, 208004afde45SDave Jiang tx_chan ? tx_chan->device->copy_align : 0); 2081569410caSDave Jiang 2082282a2feeSJon Mason /* If DMA engine usage is possible, try to find the max size for that */ 208304afde45SDave Jiang max_size = qp->tx_max_frame - sizeof(struct ntb_payload_header); 208404afde45SDave Jiang max_size = round_down(max_size, 1 << copy_align); 2085282a2feeSJon Mason 208604afde45SDave Jiang return max_size; 2087fce8a7bbSJon Mason } 2088fce8a7bbSJon Mason EXPORT_SYMBOL_GPL(ntb_transport_max_size); 2089e26a5843SAllen Hubbe 2090e74bfeedSDave Jiang unsigned int ntb_transport_tx_free_entry(struct ntb_transport_qp *qp) 2091e74bfeedSDave Jiang { 2092e74bfeedSDave Jiang unsigned int head = qp->tx_index; 2093e74bfeedSDave Jiang unsigned int tail = qp->remote_rx_info->entry; 2094e74bfeedSDave Jiang 2095e74bfeedSDave Jiang return tail > head ? tail - head : qp->tx_max_entry + tail - head; 2096e74bfeedSDave Jiang } 2097e74bfeedSDave Jiang EXPORT_SYMBOL_GPL(ntb_transport_tx_free_entry); 2098e74bfeedSDave Jiang 2099e26a5843SAllen Hubbe static void ntb_transport_doorbell_callback(void *data, int vector) 2100e26a5843SAllen Hubbe { 2101e26a5843SAllen Hubbe struct ntb_transport_ctx *nt = data; 2102e26a5843SAllen Hubbe struct ntb_transport_qp *qp; 2103e26a5843SAllen Hubbe u64 db_bits; 2104e26a5843SAllen Hubbe unsigned int qp_num; 2105e26a5843SAllen Hubbe 2106e26a5843SAllen Hubbe db_bits = (nt->qp_bitmap & ~nt->qp_bitmap_free & 2107e26a5843SAllen Hubbe ntb_db_vector_mask(nt->ndev, vector)); 2108e26a5843SAllen Hubbe 2109e26a5843SAllen Hubbe while (db_bits) { 2110e26a5843SAllen Hubbe qp_num = __ffs(db_bits); 2111e26a5843SAllen Hubbe qp = &nt->qp_vec[qp_num]; 2112e26a5843SAllen Hubbe 2113e9021331SDave Jiang if (qp->active) 2114e26a5843SAllen Hubbe tasklet_schedule(&qp->rxc_db_work); 2115e26a5843SAllen Hubbe 2116e26a5843SAllen Hubbe db_bits &= ~BIT_ULL(qp_num); 2117e26a5843SAllen Hubbe } 2118e26a5843SAllen Hubbe } 2119e26a5843SAllen Hubbe 2120e26a5843SAllen Hubbe static const struct ntb_ctx_ops ntb_transport_ops = { 2121e26a5843SAllen Hubbe .link_event = ntb_transport_event_callback, 2122e26a5843SAllen Hubbe .db_event = ntb_transport_doorbell_callback, 2123e26a5843SAllen Hubbe }; 2124e26a5843SAllen Hubbe 2125e26a5843SAllen Hubbe static struct ntb_client ntb_transport_client = { 2126e26a5843SAllen Hubbe .ops = { 2127e26a5843SAllen Hubbe .probe = ntb_transport_probe, 2128e26a5843SAllen Hubbe .remove = ntb_transport_free, 2129e26a5843SAllen Hubbe }, 2130e26a5843SAllen Hubbe }; 2131e26a5843SAllen Hubbe 2132e26a5843SAllen Hubbe static int __init ntb_transport_init(void) 2133e26a5843SAllen Hubbe { 2134e26a5843SAllen Hubbe int rc; 2135e26a5843SAllen Hubbe 21367eb38781SDave Jiang pr_info("%s, version %s\n", NTB_TRANSPORT_DESC, NTB_TRANSPORT_VER); 21377eb38781SDave Jiang 2138e26a5843SAllen Hubbe if (debugfs_initialized()) 2139e26a5843SAllen Hubbe nt_debugfs_dir = debugfs_create_dir(KBUILD_MODNAME, NULL); 2140e26a5843SAllen Hubbe 2141e26a5843SAllen Hubbe rc = bus_register(&ntb_transport_bus); 2142e26a5843SAllen Hubbe if (rc) 2143e26a5843SAllen Hubbe goto err_bus; 2144e26a5843SAllen Hubbe 2145e26a5843SAllen Hubbe rc = ntb_register_client(&ntb_transport_client); 2146e26a5843SAllen Hubbe if (rc) 2147e26a5843SAllen Hubbe goto err_client; 2148e26a5843SAllen Hubbe 2149e26a5843SAllen Hubbe return 0; 2150e26a5843SAllen Hubbe 2151e26a5843SAllen Hubbe err_client: 2152e26a5843SAllen Hubbe bus_unregister(&ntb_transport_bus); 2153e26a5843SAllen Hubbe err_bus: 2154e26a5843SAllen Hubbe debugfs_remove_recursive(nt_debugfs_dir); 2155e26a5843SAllen Hubbe return rc; 2156e26a5843SAllen Hubbe } 2157e26a5843SAllen Hubbe module_init(ntb_transport_init); 2158e26a5843SAllen Hubbe 2159e26a5843SAllen Hubbe static void __exit ntb_transport_exit(void) 2160e26a5843SAllen Hubbe { 2161e26a5843SAllen Hubbe debugfs_remove_recursive(nt_debugfs_dir); 2162e26a5843SAllen Hubbe 2163e26a5843SAllen Hubbe ntb_unregister_client(&ntb_transport_client); 2164e26a5843SAllen Hubbe bus_unregister(&ntb_transport_bus); 2165e26a5843SAllen Hubbe } 2166e26a5843SAllen Hubbe module_exit(ntb_transport_exit); 2167