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