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